AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/08/31 12:30 flow patch-triage
14m Args:
null

Results:
{
  "EnableConfigs": [
    "UNIX",
    "COREDUMP"
  ],
  "FocusSymbols": [
    "sk_setsockopt",
    "sk_getsockopt",
    "scm_recv_unix",
    "__scm_send",
    "__pidfs_register_pids",
    "pidfs_coredump"
  ],
  "KMSANReasoning": "The patch series introduces support for thread pidfds in UNIX domain sockets and coredump mechanisms (SO_PASSPIDFD_THREAD, SO_PEERPIDFD_THREAD).\n\nKey areas analyzed for uninitialized memory risks:\n1. `DECLARE_PIDS` arrays in `struct scm_cookie`, `struct sock`, `struct coredump_params`, `struct unix_peercred`, and `struct unix_skb_parms`:\n   - `struct sock` instances are allocated with `sk_alloc()` (zeroed) and explicitly reset in `sock_init_data_uid()` with `memset(sk-\u003esk_peer_pid, 0, ...)`.\n   - `struct scm_cookie` objects are consistently zero-initialized (either via designated zero initializers `= {}` or `memset(..., 0, sizeof(...))`) across all callers (`scm_send`, `unix_destruct_scm`, `__unix_dgram_recvmsg`, `unix_stream_read_generic`, `hci_sock_recvmsg`, `netlink_recvmsg`).\n   - `struct unix_peercred` instances are zero-initialized (`= {}`) in `unix_listen`, `unix_stream_connect`, and `unix_socketpair` before `prepare_peercred()`.\n   - `skb-\u003ecb` containing `struct unix_skb_parms` is zeroed during `__alloc_skb()`.\n   - `struct coredump_params` is zero-initialized via designated initializers in `vfs_coredump()`.\n2. Socket option handlers and cmsg helpers:\n   - `sk_getsockopt_peerpidfd()` copies an initialized `int pidfd` and `int len` to user space.\n   - `scm_pidfd_recv()` initializes `pidfd` from `pidfd_prepare()` before writing via `put_cmsg()`.\n   - No structure padding or uninitialized fields are exposed to userspace or read in conditional logic.\n3. Potential issues in this patch series (e.g. pid reference management, use-after-free, locking around `sk_peer_lock`, NULL dereference) are detectable by KASAN, LOCKDEP, and standard kernel debugging facilities.\n\nThus, there are no uninitialized memory risks warranting a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch introduces the SO_PASSPIDFD_THREAD and SO_PEERPIDFD_THREAD socket options to allow passing and querying thread-specific pidfds over UNIX domain sockets. It updates struct sock, SCM credential passing, UNIX socket read/write credential matching, pidfs pid registration, and coredump notification mechanisms.",
  "WorthFuzzing": true
}

1/1 2026/08/31 12:30 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 332b03a59787671294e517d035f545dd992063b1\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Mon Aug 31 12:30:23 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h\nindex 946a5fad26912..5d3524c26b2bc 100644\n--- a/arch/alpha/include/uapi/asm/socket.h\n+++ b/arch/alpha/include/uapi/asm/socket.h\n@@ -157,6 +157,10 @@\n \n #define SO_RIGHTS_NOTRUNC      85\n \n+#define SO_PASSPIDFD_THREAD    86\n+\n+#define SO_PEERPIDFD_THREAD    87\n+\n #if !defined(__KERNEL__)\n \n #if __BITS_PER_LONG == 64\ndiff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h\nindex f1641dde135fc..245a43f52fb1a 100644\n--- a/arch/mips/include/uapi/asm/socket.h\n+++ b/arch/mips/include/uapi/asm/socket.h\n@@ -168,6 +168,10 @@\n \n #define SO_RIGHTS_NOTRUNC      85\n \n+#define SO_PASSPIDFD_THREAD    86\n+\n+#define SO_PEERPIDFD_THREAD    87\n+\n #if !defined(__KERNEL__)\n \n #if __BITS_PER_LONG == 64\ndiff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h\nindex f3a3815c7dc29..f23710e1c6714 100644\n--- a/arch/parisc/include/uapi/asm/socket.h\n+++ b/arch/parisc/include/uapi/asm/socket.h\n@@ -149,6 +149,10 @@\n \n #define SO_RIGHTS_NOTRUNC\t0x4053\n \n+#define SO_PASSPIDFD_THREAD\t0x4054\n+\n+#define SO_PEERPIDFD_THREAD\t0x4055\n+\n #if !defined(__KERNEL__)\n \n #if __BITS_PER_LONG == 64\ndiff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h\nindex 7907f3b1f0ee0..b35b25bdefc2b 100644\n--- a/arch/sparc/include/uapi/asm/socket.h\n+++ b/arch/sparc/include/uapi/asm/socket.h\n@@ -150,6 +150,10 @@\n \n #define SO_RIGHTS_NOTRUNC        0x005e\n \n+#define SO_PASSPIDFD_THREAD      0x005f\n+\n+#define SO_PEERPIDFD_THREAD      0x0060\n+\n #if !defined(__KERNEL__)\n \n \ndiff --git a/fs/coredump.c b/fs/coredump.c\nindex ac3cd74808c64..b5ff4b3e1831e 100644\n--- a/fs/coredump.c\n+++ b/fs/coredump.c\n@@ -454,7 +454,7 @@ static bool coredump_parse(struct core_name *cn, struct coredump_params *cprm,\n \t\t\t\t * leader we know that the thread-group leader\n \t\t\t\t * cannot be reaped until @current has exited.\n \t\t\t\t */\n-\t\t\t\tcprm-\u003epid = task_tgid(current);\n+\t\t\t\ttask_pids(cprm-\u003epid, current);\n \t\t\t\terr = cn_printf(cn, \"%d\", COREDUMP_PIDFD_NUMBER);\n \t\t\t\tbreak;\n \t\t\t}\n@@ -626,13 +626,17 @@ static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)\n \tstruct coredump_params *cp = (struct coredump_params *)info-\u003edata;\n \tint err;\n \n-\tif (cp-\u003epid) {\n+\tif (cp-\u003epid[PIDTYPE_TGID]) {\n \t\tstruct file *pidfs_file __free(fput) = NULL;\n \n-\t\tpidfs_file = pidfs_alloc_file(cp-\u003epid, 0);\n+\t\tpidfs_file = pidfs_alloc_file(cp-\u003epid[PIDTYPE_TGID], 0);\n \t\tif (IS_ERR(pidfs_file))\n \t\t\treturn PTR_ERR(pidfs_file);\n \n+\t\terr = pidfs_register_pids(cp-\u003epid);\n+\t\tif (err)\n+\t\t\treturn err;\n+\n \t\tpidfs_coredump(cp);\n \n \t\t/*\n@@ -695,12 +699,12 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *\n \t\treturn false;\n \n \t/*\n-\t * Set the thread-group leader pid which is used for the peer\n-\t * credentials during connect() below. Then immediately register\n-\t * it in pidfs...\n+\t * Set the pids of the dumping thread and its thread-group leader\n+\t * which are used for the peer credentials during connect() below.\n+\t * Then immediately register them in pidfs...\n \t */\n-\tcprm-\u003epid = task_tgid(current);\n-\tretval = pidfs_register_pid(cprm-\u003epid);\n+\ttask_pids(cprm-\u003epid, current);\n+\tretval = pidfs_register_pids(cprm-\u003epid);\n \tif (retval)\n \t\treturn false;\n \n@@ -722,7 +726,7 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *\n \t}\n \n \t/* ... and validate that @sk_peer_pid matches @cprm.pid. */\n-\tif (WARN_ON_ONCE(unix_peer(socket-\u003esk)-\u003esk_peer_pid != cprm-\u003epid))\n+\tif (WARN_ON_ONCE(!pids_equal(unix_peer(socket-\u003esk)-\u003esk_peer_pid, cprm-\u003epid)))\n \t\treturn false;\n \n \tcprm-\u003elimit = RLIM_INFINITY;\ndiff --git a/fs/pidfs.c b/fs/pidfs.c\nindex a6a643f15d08a..29299b2c7ca71 100644\n--- a/fs/pidfs.c\n+++ b/fs/pidfs.c\n@@ -793,9 +793,9 @@ void pidfs_exit(struct task_struct *tsk)\n }\n \n #ifdef CONFIG_COREDUMP\n-void pidfs_coredump(const struct coredump_params *cprm)\n+static void pidfs_coredump_pid(struct pid *pid,\n+\t\t\t       const struct coredump_params *cprm)\n {\n-\tstruct pid *pid = cprm-\u003epid;\n \tstruct pidfs_attr *attr;\n \n \tattr = READ_ONCE(pid-\u003eattr);\n@@ -814,6 +814,13 @@ void pidfs_coredump(const struct coredump_params *cprm)\n \tsmp_wmb();\n \tset_bit(PIDFS_ATTR_BIT_COREDUMP, \u0026attr-\u003eattr_mask);\n }\n+\n+void pidfs_coredump(const struct coredump_params *cprm)\n+{\n+\t/* The dumping thread's pidfd reports the coredump as well. */\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= pids_last(cprm-\u003epid); type++)\n+\t\tpidfs_coredump_pid(cprm-\u003epid[type], cprm);\n+}\n #endif\n \n static struct vfsmount *pidfs_mnt __ro_after_init;\n@@ -1070,6 +1077,22 @@ int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)\n \treturn 0;\n }\n \n+/* Register the pids up to pid type @last of @pids in pidfs. */\n+int __pidfs_register_pids(struct pid *const *pids, enum pid_type last)\n+{\n+\tif (WARN_ON_ONCE(last \u003e= PIDTYPE_MAX))\n+\t\treturn -EINVAL;\n+\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++) {\n+\t\tint ret = pidfs_register_pid(pids[type]);\n+\n+\t\tif (unlikely(ret))\n+\t\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n static struct dentry *pidfs_stash_dentry(struct dentry **stashed,\n \t\t\t\t\t struct dentry *dentry)\n {\ndiff --git a/include/linux/coredump.h b/include/linux/coredump.h\nindex 7b38ee2e7913b..0bbb7de6a4021 100644\n--- a/include/linux/coredump.h\n+++ b/include/linux/coredump.h\n@@ -5,6 +5,7 @@\n #include \u003clinux/types.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/fs.h\u003e\n+#include \u003clinux/pid_types.h\u003e\n #include \u003clinux/sched/coredump.h\u003e\n #include \u003casm/siginfo.h\u003e\n \n@@ -32,7 +33,8 @@ struct coredump_params {\n \tint vma_count;\n \tsize_t vma_data_size;\n \tstruct core_vma_metadata *vma_meta;\n-\tstruct pid *pid;\n+\t/* Dumping thread and its thread-group leader by pid type. */\n+\tDECLARE_PIDS(pid, PIDTYPE_TGID);\n };\n \n extern unsigned int core_file_note_size_limit;\ndiff --git a/include/linux/pid.h b/include/linux/pid.h\nindex ddaef0bbc8ba3..87635d0cc1f71 100644\n--- a/include/linux/pid.h\n+++ b/include/linux/pid.h\n@@ -2,6 +2,9 @@\n #ifndef _LINUX_PID_H\n #define _LINUX_PID_H\n \n+#include \u003clinux/array_size.h\u003e\n+#include \u003clinux/build_bug.h\u003e\n+#include \u003clinux/minmax.h\u003e\n #include \u003clinux/pid_types.h\u003e\n #include \u003clinux/rculist.h\u003e\n #include \u003clinux/rcupdate.h\u003e\n@@ -92,6 +95,56 @@ static inline struct pid *get_pid(struct pid *pid)\n }\n \n extern void put_pid(struct pid *pid);\n+\n+/*\n+ * Helpers for arrays of struct pid indexed by pid type declared with\n+ * DECLARE_PIDS(). The array covers PIDTYPE_PID up to the pid type it\n+ * was declared with and the helpers take that bound from the array.\n+ */\n+static inline void __get_pids(struct pid **dst, struct pid *const *src,\n+\t\t\t      enum pid_type last)\n+{\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++)\n+\t\tdst[type] = get_pid(src[type]);\n+}\n+\n+static inline void __put_pids(struct pid **pids, enum pid_type last)\n+{\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++) {\n+\t\tput_pid(pids[type]);\n+\t\tpids[type] = NULL;\n+\t}\n+}\n+\n+static inline void __swap_pids(struct pid **a, struct pid **b,\n+\t\t\t       enum pid_type last)\n+{\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++)\n+\t\tswap(a[type], b[type]);\n+}\n+\n+static inline bool __pids_equal(struct pid *const *a, struct pid *const *b,\n+\t\t\t\tenum pid_type last)\n+{\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++)\n+\t\tif (a[type] != b[type])\n+\t\t\treturn false;\n+\treturn true;\n+}\n+\n+/* The last pid type an array declared with DECLARE_PIDS() covers. */\n+#define pids_last(pids)\t\t\t\t\t\t\t\\\n+\t((enum pid_type)(ARRAY_SIZE(pids) - 1 +\t\t\t\t\\\n+\t\t\t BUILD_BUG_ON_ZERO(ARRAY_SIZE(pids) \u003e PIDTYPE_MAX)))\n+\n+#define __pids_last2(a, b)\t\t\t\t\t\t\\\n+\t(pids_last(a) + BUILD_BUG_ON_ZERO(ARRAY_SIZE(a) != ARRAY_SIZE(b)))\n+\n+#define get_pids(dst, src)\t__get_pids(dst, src, __pids_last2(dst, src))\n+#define put_pids(pids)\t\t__put_pids(pids, pids_last(pids))\n+#define swap_pids(a, b)\t\t__swap_pids(a, b, __pids_last2(a, b))\n+#define pids_equal(a, b)\t__pids_equal(a, b, __pids_last2(a, b))\n+\n extern struct task_struct *pid_task(struct pid *pid, enum pid_type);\n static inline bool pid_has_task(struct pid *pid, enum pid_type type)\n {\ndiff --git a/include/linux/pid_types.h b/include/linux/pid_types.h\nindex c2aee1d91dcfd..3302690a2a281 100644\n--- a/include/linux/pid_types.h\n+++ b/include/linux/pid_types.h\n@@ -2,6 +2,8 @@\n #ifndef _LINUX_PID_TYPES_H\n #define _LINUX_PID_TYPES_H\n \n+#include \u003clinux/build_bug.h\u003e\n+\n enum pid_type {\n \tPIDTYPE_PID,\n \tPIDTYPE_TGID,\n@@ -10,6 +12,12 @@ enum pid_type {\n \tPIDTYPE_MAX,\n };\n \n+struct pid;\n+\n+/* An array of struct pid indexed by pid type, PIDTYPE_PID up to @last. */\n+#define DECLARE_PIDS(name, last)\t\t\t\t\t\\\n+\tstruct pid *name[(last) + 1 + BUILD_BUG_ON_ZERO((last) \u003e= PIDTYPE_MAX)]\n+\n struct pid_namespace;\n extern struct pid_namespace init_pid_ns;\n \ndiff --git a/include/linux/pidfs.h b/include/linux/pidfs.h\nindex 0abf7da9ab236..3c1e82f1a3691 100644\n--- a/include/linux/pidfs.h\n+++ b/include/linux/pidfs.h\n@@ -3,6 +3,7 @@\n #define _LINUX_PID_FS_H\n \n #include \u003clinux/gfp_types.h\u003e\n+#include \u003clinux/pid.h\u003e\n \n struct coredump_params;\n \n@@ -32,5 +33,9 @@ static inline int pidfs_register_pid(struct pid *pid)\n }\n \n void pidfs_free_pid(struct pid *pid);\n+int __pidfs_register_pids(struct pid *const *pids, enum pid_type last);\n+\n+/* Register the pids of an array declared with DECLARE_PIDS(). */\n+#define pidfs_register_pids(pids)\t__pidfs_register_pids(pids, pids_last(pids))\n \n #endif /* _LINUX_PID_FS_H */\ndiff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h\nindex 584ae88b435e9..9444b47789a08 100644\n--- a/include/linux/sched/signal.h\n+++ b/include/linux/sched/signal.h\n@@ -677,6 +677,24 @@ struct pid *task_pid_type(struct task_struct *task, enum pid_type type)\n \treturn pid;\n }\n \n+/* Fill @pids with the pid types of @task up to @last, without references. */\n+static inline void __task_pids(struct pid **pids, enum pid_type last,\n+\t\t\t       struct task_struct *task)\n+{\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++)\n+\t\tpids[type] = task_pid_type(task, type);\n+}\n+\n+static inline void __get_task_pids(struct pid **pids, enum pid_type last,\n+\t\t\t\t   struct task_struct *task)\n+{\n+\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= last; type++)\n+\t\tpids[type] = get_pid(task_pid_type(task, type));\n+}\n+\n+#define task_pids(pids, task)\t\t__task_pids(pids, pids_last(pids), task)\n+#define get_task_pids(pids, task)\t__get_task_pids(pids, pids_last(pids), task)\n+\n static inline struct pid *task_tgid(struct task_struct *task)\n {\n \treturn task-\u003esignal-\u003epids[PIDTYPE_TGID];\ndiff --git a/include/net/scm.h b/include/net/scm.h\nindex 86ae6bc109ec6..aa7d15c5fc271 100644\n--- a/include/net/scm.h\n+++ b/include/net/scm.h\n@@ -42,7 +42,7 @@ struct scm_fp_list {\n };\n \n struct scm_cookie {\n-\tstruct pid\t\t*pid;\t\t/* Skb credentials */\n+\tDECLARE_PIDS(pid, PIDTYPE_TGID);\t/* Skb credentials by pid type */\n \tstruct scm_fp_list\t*fp;\t\t/* Passed files\t\t*/\n \tstruct scm_creds\tcreds;\t\t/* Skb credentials\t*/\n #ifdef CONFIG_SECURITY_NETWORK\n@@ -69,7 +69,7 @@ static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_co\n static __inline__ void scm_set_cred(struct scm_cookie *scm,\n \t\t\t\t    struct pid *pid, kuid_t uid, kgid_t gid)\n {\n-\tscm-\u003epid = get_pid(pid);\n+\tscm-\u003epid[PIDTYPE_TGID] = get_pid(pid);\n \tscm-\u003ecreds.pid = pid_vnr(pid);\n \tscm-\u003ecreds.uid = uid;\n \tscm-\u003ecreds.gid = gid;\n@@ -77,8 +77,7 @@ static __inline__ void scm_set_cred(struct scm_cookie *scm,\n \n static __inline__ void scm_destroy_cred(struct scm_cookie *scm)\n {\n-\tput_pid(scm-\u003epid);\n-\tscm-\u003epid = NULL;\n+\tput_pids(scm-\u003epid);\n }\n \n static __inline__ void scm_destroy(struct scm_cookie *scm)\ndiff --git a/include/net/sock.h b/include/net/sock.h\nindex 51185222aac29..67b743bab220e 100644\n--- a/include/net/sock.h\n+++ b/include/net/sock.h\n@@ -301,7 +301,7 @@ struct sk_filter;\n   *\t@sk_type: socket type (%SOCK_STREAM, etc)\n   *\t@sk_protocol: which protocol this socket belongs in this network family\n   *\t@sk_peer_lock: lock protecting @sk_peer_pid and @sk_peer_cred\n-  *\t@sk_peer_pid: \u0026struct pid for this socket's peer\n+  *\t@sk_peer_pid: \u0026struct pid for this socket's peer, by pid type\n   *\t@sk_peer_cred: %SO_PEERCRED setting\n   *\t@sk_rcvlowat: %SO_RCVLOWAT setting\n   *\t@sk_rcvtimeo: %SO_RCVTIMEO setting\n@@ -356,6 +356,7 @@ struct sk_filter;\n   *\t@sk_scm_security: flagged by SO_PASSSEC to recv SCM_SECURITY\n   *\t@sk_scm_pidfd: flagged by SO_PASSPIDFD to recv SCM_PIDFD\n   *\t@sk_scm_rights: flagged by SO_PASSRIGHTS to recv SCM_RIGHTS\n+  *\t@sk_scm_pidfd_thread: flagged by SO_PASSPIDFD_THREAD to recv a thread SCM_PIDFD\n   *\t@sk_scm_unused: unused flags for scm_recv()\n   *\t@ns_tracker: tracker for netns reference\n   *\t@sk_user_frags: xarray of pages the user is holding a reference on.\n@@ -545,7 +546,7 @@ struct sock {\n \tu64\t\t\tsk_ino;\n \tspinlock_t\t\tsk_peer_lock;\n \tint\t\t\tsk_bind_phc;\n-\tstruct pid\t\t*sk_peer_pid;\n+\tDECLARE_PIDS(sk_peer_pid, PIDTYPE_TGID);\n \tconst struct cred\t*sk_peer_cred;\n \n \tktime_t\t\t\tsk_stamp;\n@@ -562,7 +563,8 @@ struct sock {\n \t\t\t\tsk_scm_security : 1,\n \t\t\t\tsk_scm_pidfd : 1,\n \t\t\t\tsk_scm_rights : 1,\n-\t\t\t\tsk_scm_unused : 4;\n+\t\t\t\tsk_scm_pidfd_thread : 1,\n+\t\t\t\tsk_scm_unused : 3;\n \t\t};\n \t};\n \tu8\t\t\tsk_clockid;\n@@ -2986,6 +2988,12 @@ static inline bool sk_is_stream_unix(const struct sock *sk)\n \treturn sk_is_unix(sk) \u0026\u0026 sk-\u003esk_type == SOCK_STREAM;\n }\n \n+/* SO_PASSPIDFD or SO_PASSPIDFD_THREAD asked for an SCM_PIDFD. */\n+static inline bool sk_scm_pidfd_wanted(const struct sock *sk)\n+{\n+\treturn sk-\u003esk_scm_pidfd || sk-\u003esk_scm_pidfd_thread;\n+}\n+\n static inline bool sk_is_vsock(const struct sock *sk)\n {\n \treturn sk-\u003esk_family == AF_VSOCK;\ndiff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h\nindex 84ea7b92936e2..56fed7ab27ab8 100644\n--- a/include/uapi/asm-generic/socket.h\n+++ b/include/uapi/asm-generic/socket.h\n@@ -152,6 +152,10 @@\n \n #define SO_RIGHTS_NOTRUNC\t85\n \n+#define SO_PASSPIDFD_THREAD\t86\n+\n+#define SO_PEERPIDFD_THREAD\t87\n+\n #if !defined(__KERNEL__)\n \n #if __BITS_PER_LONG == 64 || (defined(__x86_64__) \u0026\u0026 defined(__ILP32__))\ndiff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c\nindex 411d66f24393d..7758e9ea3848c 100644\n--- a/net/bluetooth/af_bluetooth.c\n+++ b/net/bluetooth/af_bluetooth.c\n@@ -161,7 +161,7 @@ struct sock *bt_sock_alloc(struct net *net, struct socket *sock,\n \t/* Init peer information so it can be properly monitored */\n \tif (!kern) {\n \t\tspin_lock(\u0026sk-\u003esk_peer_lock);\n-\t\tsk-\u003esk_peer_pid  = get_pid(task_tgid(current));\n+\t\tsk-\u003esk_peer_pid[PIDTYPE_TGID]  = get_pid(task_tgid(current));\n \t\tsk-\u003esk_peer_cred = get_current_cred();\n \t\tspin_unlock(\u0026sk-\u003esk_peer_lock);\n \t}\n@@ -235,9 +235,9 @@ void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)\n \t * socket is allocated by the kernel.\n \t */\n \tspin_lock(\u0026sk-\u003esk_peer_lock);\n-\told_pid = sk-\u003esk_peer_pid;\n+\told_pid = sk-\u003esk_peer_pid[PIDTYPE_TGID];\n \told_cred = sk-\u003esk_peer_cred;\n-\tsk-\u003esk_peer_pid = get_pid(parent-\u003esk_peer_pid);\n+\tsk-\u003esk_peer_pid[PIDTYPE_TGID] = get_pid(parent-\u003esk_peer_pid[PIDTYPE_TGID]);\n \tsk-\u003esk_peer_cred = get_cred(parent-\u003esk_peer_cred);\n \tspin_unlock(\u0026sk-\u003esk_peer_lock);\n \ndiff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c\nindex 070ca388f9ace..91e4738eabdb0 100644\n--- a/net/bluetooth/hci_sock.c\n+++ b/net/bluetooth/hci_sock.c\n@@ -273,21 +273,21 @@ static void hci_sock_copy_creds(struct sock *sk, struct sk_buff *skb)\n \tcreds = \u0026bt_cb(skb)-\u003ecreds;\n \n \t/* Check if peer credentials is set */\n-\tif (!sk-\u003esk_peer_pid) {\n+\tif (!sk-\u003esk_peer_pid[PIDTYPE_TGID]) {\n \t\t/* Check if parent peer credentials is set */\n-\t\tif (bt_sk(sk)-\u003eparent \u0026\u0026 bt_sk(sk)-\u003eparent-\u003esk_peer_pid)\n+\t\tif (bt_sk(sk)-\u003eparent \u0026\u0026 bt_sk(sk)-\u003eparent-\u003esk_peer_pid[PIDTYPE_TGID])\n \t\t\tsk = bt_sk(sk)-\u003eparent;\n \t\telse\n \t\t\treturn;\n \t}\n \n \t/* Check if scm_creds already set */\n-\tif (creds-\u003epid == pid_vnr(sk-\u003esk_peer_pid))\n+\tif (creds-\u003epid == pid_vnr(sk-\u003esk_peer_pid[PIDTYPE_TGID]))\n \t\treturn;\n \n \tmemset(creds, 0, sizeof(*creds));\n \n-\tcreds-\u003epid = pid_vnr(sk-\u003esk_peer_pid);\n+\tcreds-\u003epid = pid_vnr(sk-\u003esk_peer_pid[PIDTYPE_TGID]);\n \tif (sk-\u003esk_peer_cred) {\n \t\tcreds-\u003euid = sk-\u003esk_peer_cred-\u003euid;\n \t\tcreds-\u003egid = sk-\u003esk_peer_cred-\u003egid;\ndiff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c\nindex 1194c37e466f1..872d8fb31b6fb 100644\n--- a/net/bluetooth/l2cap_sock.c\n+++ b/net/bluetooth/l2cap_sock.c\n@@ -1890,7 +1890,7 @@ static struct pid *l2cap_sock_get_peer_pid_cb(struct l2cap_chan *chan)\n {\n \tstruct sock *sk = chan-\u003edata;\n \n-\treturn sk-\u003esk_peer_pid;\n+\treturn sk-\u003esk_peer_pid[PIDTYPE_TGID];\n }\n \n static void l2cap_sock_suspend_cb(struct l2cap_chan *chan)\ndiff --git a/net/core/scm.c b/net/core/scm.c\nindex f0d44ecdb11fc..d69768414af48 100644\n--- a/net/core/scm.c\n+++ b/net/core/scm.c\n@@ -149,6 +149,7 @@ EXPORT_SYMBOL(__scm_destroy);\n \n static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)\n {\n+\tstruct pid *thread_pid;\n \tint err;\n \n \t/* drop all previous references */\n@@ -158,7 +159,18 @@ static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)\n \tif (unlikely(err))\n \t\treturn err;\n \n-\tscm-\u003epid = pid;\n+\t/* A sender naming its own thread-group sends from the current thread. */\n+\tif (pid == task_tgid(current))\n+\t\tthread_pid = task_pid(current);\n+\telse\n+\t\tthread_pid = pid;\n+\n+\terr = pidfs_register_pid(thread_pid);\n+\tif (unlikely(err))\n+\t\treturn err;\n+\n+\tscm-\u003epid[PIDTYPE_TGID] = pid;\n+\tscm-\u003epid[PIDTYPE_PID] = get_pid(thread_pid);\n \tscm-\u003ecreds.pid = pid_vnr(pid);\n \treturn 0;\n }\n@@ -207,7 +219,8 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)\n \t\t\tif (err)\n \t\t\t\tgoto error;\n \n-\t\t\tif (!p-\u003epid || pid_vnr(p-\u003epid) != creds.pid) {\n+\t\t\tif (!p-\u003epid[PIDTYPE_TGID] ||\n+\t\t\t    pid_vnr(p-\u003epid[PIDTYPE_TGID]) != creds.pid) {\n \t\t\t\tstruct pid *pid;\n \t\t\t\terr = -ESRCH;\n \t\t\t\tpid = find_get_pid(creds.pid);\n@@ -486,9 +499,13 @@ static bool scm_has_secdata(struct sock *sk)\n }\n #endif\n \n-static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)\n+static void scm_pidfd_recv(struct sock *sk, struct msghdr *msg,\n+\t\t\t   struct scm_cookie *scm)\n {\n+\tenum pid_type type = sk-\u003esk_scm_pidfd_thread ? PIDTYPE_PID : PIDTYPE_TGID;\n+\tstruct pid *pid = scm-\u003epid[type];\n \tstruct file *pidfd_file = NULL;\n+\tunsigned int flags = PIDFD_STALE;\n \tint len, pidfd;\n \n \t/* put_cmsg() doesn't return an error if CMSG is truncated,\n@@ -504,10 +521,13 @@ static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)\n \t\treturn;\n \t}\n \n-\tif (!scm-\u003epid)\n+\tif (!pid)\n \t\treturn;\n \n-\tpidfd = pidfd_prepare(scm-\u003epid, PIDFD_STALE, \u0026pidfd_file);\n+\tif (type == PIDTYPE_PID)\n+\t\tflags |= PIDFD_THREAD;\n+\n+\tpidfd = pidfd_prepare(pid, flags, \u0026pidfd_file);\n \n \tif (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), \u0026pidfd)) {\n \t\tif (pidfd_file) {\n@@ -526,7 +546,7 @@ static bool __scm_recv_common(struct sock *sk, struct msghdr *msg,\n \t\t\t      struct scm_cookie *scm, int flags)\n {\n \tif (!msg-\u003emsg_control) {\n-\t\tif (sk-\u003esk_scm_credentials || sk-\u003esk_scm_pidfd ||\n+\t\tif (sk-\u003esk_scm_credentials || sk_scm_pidfd_wanted(sk) ||\n \t\t    scm-\u003efp || scm_has_secdata(sk))\n \t\t\tmsg-\u003emsg_flags |= MSG_CTRUNC;\n \n@@ -573,8 +593,8 @@ void scm_recv_unix(struct socket *sock, struct msghdr *msg,\n \t\tscm_detach_fds(msg, scm, READ_ONCE(u-\u003escm_rights_notrunc));\n \t}\n \n-\tif (sock-\u003esk-\u003esk_scm_pidfd)\n-\t\tscm_pidfd_recv(msg, scm);\n+\tif (sk_scm_pidfd_wanted(sock-\u003esk))\n+\t\tscm_pidfd_recv(sock-\u003esk, msg, scm);\n \n \tscm_destroy_cred(scm);\n }\ndiff --git a/net/core/sock.c b/net/core/sock.c\nindex 1ad41904db25b..cb2ffd329bc67 100644\n--- a/net/core/sock.c\n+++ b/net/core/sock.c\n@@ -1571,10 +1571,25 @@ int sk_setsockopt(struct sock *sk, int level, int optname,\n \t\tbreak;\n \n \tcase SO_PASSPIDFD:\n-\t\tif (sk_is_unix(sk))\n+\t\tif (sk_is_unix(sk)) {\n+\t\t\t/* Mutually exclusive with SO_PASSPIDFD_THREAD. */\n \t\t\tsk-\u003esk_scm_pidfd = valbool;\n-\t\telse\n+\t\t\tif (valbool)\n+\t\t\t\tsk-\u003esk_scm_pidfd_thread = 0;\n+\t\t} else {\n+\t\t\tret = -EOPNOTSUPP;\n+\t\t}\n+\t\tbreak;\n+\n+\tcase SO_PASSPIDFD_THREAD:\n+\t\tif (sk_is_unix(sk)) {\n+\t\t\t/* Mutually exclusive with SO_PASSPIDFD. */\n+\t\t\tsk-\u003esk_scm_pidfd_thread = valbool;\n+\t\t\tif (valbool)\n+\t\t\t\tsk-\u003esk_scm_pidfd = 0;\n+\t\t} else {\n \t\t\tret = -EOPNOTSUPP;\n+\t\t}\n \t\tbreak;\n \n \tcase SO_PASSRIGHTS:\n@@ -1728,6 +1743,50 @@ static int groups_to_user(sockptr_t dst, const struct group_info *src)\n \treturn 0;\n }\n \n+/* Hand out a pidfd for @type of the socket's peer via SO_PEERPIDFD*. */\n+static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type,\n+\t\t\t\t   sockptr_t optval, sockptr_t optlen, int len)\n+{\n+\tstruct file *pidfd_file = NULL;\n+\tunsigned int flags = 0;\n+\tstruct pid *peer_pid;\n+\tint pidfd;\n+\n+\tif (len \u003e sizeof(pidfd))\n+\t\tlen = sizeof(pidfd);\n+\n+\tspin_lock(\u0026sk-\u003esk_peer_lock);\n+\tpeer_pid = get_pid(sk-\u003esk_peer_pid[type]);\n+\tspin_unlock(\u0026sk-\u003esk_peer_lock);\n+\n+\tif (!peer_pid)\n+\t\treturn -ENODATA;\n+\n+\t/* The use of PIDFD_STALE requires stashing of struct pid on pidfs\n+\t * with pidfs_register_pid() and only AF_UNIX were prepared for this.\n+\t */\n+\tif (sk-\u003esk_family == AF_UNIX)\n+\t\tflags |= PIDFD_STALE;\n+\tif (type == PIDTYPE_PID)\n+\t\tflags |= PIDFD_THREAD;\n+\n+\tpidfd = pidfd_prepare(peer_pid, flags, \u0026pidfd_file);\n+\tput_pid(peer_pid);\n+\tif (pidfd \u003c 0)\n+\t\treturn pidfd;\n+\n+\tif (copy_to_sockptr(optval, \u0026pidfd, len) ||\n+\t    copy_to_sockptr(optlen, \u0026len, sizeof(int))) {\n+\t\tput_unused_fd(pidfd);\n+\t\tfput(pidfd_file);\n+\n+\t\treturn -EFAULT;\n+\t}\n+\n+\tfd_install(pidfd, pidfd_file);\n+\treturn 0;\n+}\n+\n int sk_getsockopt(struct sock *sk, int level, int optname,\n \t\t  sockptr_t optval, sockptr_t optlen)\n {\n@@ -1892,6 +1951,13 @@ int sk_getsockopt(struct sock *sk, int level, int optname,\n \t\tv.val = sk-\u003esk_scm_pidfd;\n \t\tbreak;\n \n+\tcase SO_PASSPIDFD_THREAD:\n+\t\tif (!sk_is_unix(sk))\n+\t\t\treturn -EOPNOTSUPP;\n+\n+\t\tv.val = sk-\u003esk_scm_pidfd_thread;\n+\t\tbreak;\n+\n \tcase SO_PASSRIGHTS:\n \t\tif (!sk_is_unix(sk))\n \t\t\treturn -EOPNOTSUPP;\n@@ -1906,7 +1972,8 @@ int sk_getsockopt(struct sock *sk, int level, int optname,\n \t\t\tlen = sizeof(peercred);\n \n \t\tspin_lock(\u0026sk-\u003esk_peer_lock);\n-\t\tcred_to_ucred(sk-\u003esk_peer_pid, sk-\u003esk_peer_cred, \u0026peercred);\n+\t\tcred_to_ucred(sk-\u003esk_peer_pid[PIDTYPE_TGID], sk-\u003esk_peer_cred,\n+\t\t\t      \u0026peercred);\n \t\tspin_unlock(\u0026sk-\u003esk_peer_lock);\n \n \t\tif (copy_to_sockptr(optval, \u0026peercred, len))\n@@ -1915,45 +1982,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,\n \t}\n \n \tcase SO_PEERPIDFD:\n-\t{\n-\t\tstruct pid *peer_pid;\n-\t\tstruct file *pidfd_file = NULL;\n-\t\tunsigned int flags = 0;\n-\t\tint pidfd;\n-\n-\t\tif (len \u003e sizeof(pidfd))\n-\t\t\tlen = sizeof(pidfd);\n-\n-\t\tspin_lock(\u0026sk-\u003esk_peer_lock);\n-\t\tpeer_pid = get_pid(sk-\u003esk_peer_pid);\n-\t\tspin_unlock(\u0026sk-\u003esk_peer_lock);\n+\t\treturn sk_getsockopt_peerpidfd(sk, PIDTYPE_TGID, optval, optlen, len);\n \n-\t\tif (!peer_pid)\n-\t\t\treturn -ENODATA;\n-\n-\t\t/* The use of PIDFD_STALE requires stashing of struct pid\n-\t\t * on pidfs with pidfs_register_pid() and only AF_UNIX\n-\t\t * were prepared for this.\n-\t\t */\n-\t\tif (sk-\u003esk_family == AF_UNIX)\n-\t\t\tflags = PIDFD_STALE;\n-\n-\t\tpidfd = pidfd_prepare(peer_pid, flags, \u0026pidfd_file);\n-\t\tput_pid(peer_pid);\n-\t\tif (pidfd \u003c 0)\n-\t\t\treturn pidfd;\n-\n-\t\tif (copy_to_sockptr(optval, \u0026pidfd, len) ||\n-\t\t    copy_to_sockptr(optlen, \u0026len, sizeof(int))) {\n-\t\t\tput_unused_fd(pidfd);\n-\t\t\tfput(pidfd_file);\n-\n-\t\t\treturn -EFAULT;\n-\t\t}\n+\tcase SO_PEERPIDFD_THREAD:\n+\t\t/* Only AF_UNIX records the peer's connecting thread. */\n+\t\tif (!sk_is_unix(sk))\n+\t\t\treturn -EOPNOTSUPP;\n \n-\t\tfd_install(pidfd, pidfd_file);\n-\t\treturn 0;\n-\t}\n+\t\treturn sk_getsockopt_peerpidfd(sk, PIDTYPE_PID, optval, optlen, len);\n \n \tcase SO_PEERGROUPS:\n \t{\n@@ -2379,7 +2415,7 @@ static void __sk_destruct(struct rcu_head *head)\n \n \t/* We do not need to acquire sk-\u003esk_peer_lock, we are the last user. */\n \tput_cred(sk-\u003esk_peer_cred);\n-\tput_pid(sk-\u003esk_peer_pid);\n+\tput_pids(sk-\u003esk_peer_pid);\n \n \tif (likely(sk-\u003esk_net_refcnt)) {\n \t\tput_net_track(net, \u0026sk-\u003ens_tracker);\n@@ -3775,7 +3811,7 @@ void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)\n \tsk-\u003esk_frag.offset\t=\t0;\n \tsk-\u003esk_peek_off\t\t=\t-1;\n \n-\tsk-\u003esk_peer_pid \t=\tNULL;\n+\tmemset(sk-\u003esk_peer_pid, 0, sizeof(sk-\u003esk_peer_pid));\n \tsk-\u003esk_peer_cred\t=\tNULL;\n \tspin_lock_init(\u0026sk-\u003esk_peer_lock);\n \ndiff --git a/net/unix/af_unix.c b/net/unix/af_unix.c\nindex 13f9926bf2057..563d9827c5cc5 100644\n--- a/net/unix/af_unix.c\n+++ b/net/unix/af_unix.c\n@@ -737,57 +737,53 @@ static void unix_release_sock(struct sock *sk, int embrion)\n }\n \n struct unix_peercred {\n-\tstruct pid *peer_pid;\n+\tDECLARE_PIDS(peer_pid, PIDTYPE_TGID);\n \tconst struct cred *peer_cred;\n };\n \n static inline int prepare_peercred(struct unix_peercred *peercred)\n {\n-\tstruct pid *pid;\n \tint err;\n \n-\tpid = task_tgid(current);\n-\terr = pidfs_register_pid(pid);\n-\tif (likely(!err)) {\n-\t\tpeercred-\u003epeer_pid = get_pid(pid);\n-\t\tpeercred-\u003epeer_cred = get_current_cred();\n+\tget_task_pids(peercred-\u003epeer_pid, current);\n+\terr = pidfs_register_pids(peercred-\u003epeer_pid);\n+\tif (unlikely(err)) {\n+\t\tput_pids(peercred-\u003epeer_pid);\n+\t\treturn err;\n \t}\n-\treturn err;\n+\n+\tpeercred-\u003epeer_cred = get_current_cred();\n+\treturn 0;\n }\n \n static void drop_peercred(struct unix_peercred *peercred)\n {\n \tconst struct cred *cred = NULL;\n-\tstruct pid *pid = NULL;\n \n \tmight_sleep();\n \n-\tswap(peercred-\u003epeer_pid, pid);\n+\tput_pids(peercred-\u003epeer_pid);\n \tswap(peercred-\u003epeer_cred, cred);\n-\n-\tput_pid(pid);\n \tput_cred(cred);\n }\n \n static inline void init_peercred(struct sock *sk,\n \t\t\t\t const struct unix_peercred *peercred)\n {\n-\tsk-\u003esk_peer_pid = peercred-\u003epeer_pid;\n+\tmemcpy(sk-\u003esk_peer_pid, peercred-\u003epeer_pid, sizeof(sk-\u003esk_peer_pid));\n \tsk-\u003esk_peer_cred = peercred-\u003epeer_cred;\n }\n \n static void update_peercred(struct sock *sk, struct unix_peercred *peercred)\n {\n \tconst struct cred *old_cred;\n-\tstruct pid *old_pid;\n \n \tspin_lock(\u0026sk-\u003esk_peer_lock);\n-\told_pid = sk-\u003esk_peer_pid;\n+\tswap_pids(sk-\u003esk_peer_pid, peercred-\u003epeer_pid);\n \told_cred = sk-\u003esk_peer_cred;\n-\tinit_peercred(sk, peercred);\n+\tsk-\u003esk_peer_cred = peercred-\u003epeer_cred;\n \tspin_unlock(\u0026sk-\u003esk_peer_lock);\n \n-\tpeercred-\u003epeer_pid = old_pid;\n \tpeercred-\u003epeer_cred = old_cred;\n }\n \n@@ -796,14 +792,14 @@ static void copy_peercred(struct sock *sk, struct sock *peersk)\n \tlockdep_assert_held(\u0026unix_sk(peersk)-\u003elock);\n \n \tspin_lock(\u0026sk-\u003esk_peer_lock);\n-\tsk-\u003esk_peer_pid = get_pid(peersk-\u003esk_peer_pid);\n+\tget_pids(sk-\u003esk_peer_pid, peersk-\u003esk_peer_pid);\n \tsk-\u003esk_peer_cred = get_cred(peersk-\u003esk_peer_cred);\n \tspin_unlock(\u0026sk-\u003esk_peer_lock);\n }\n \n static bool unix_may_passcred(const struct sock *sk)\n {\n-\treturn sk-\u003esk_scm_credentials || sk-\u003esk_scm_pidfd;\n+\treturn sk-\u003esk_scm_credentials || sk_scm_pidfd_wanted(sk);\n }\n \n static int unix_listen(struct socket *sock, int backlog)\n@@ -1060,6 +1056,7 @@ static bool unix_bpf_bypass_getsockopt(int level, int optname)\n \tif (level == SOL_SOCKET) {\n \t\tswitch (optname) {\n \t\tcase SO_PEERPIDFD:\n+\t\tcase SO_PEERPIDFD_THREAD:\n \t\t\treturn true;\n \t\tdefault:\n \t\t\treturn false;\n@@ -1973,7 +1970,7 @@ static void unix_destruct_scm(struct sk_buff *skb)\n {\n \tstruct scm_cookie scm = {};\n \n-\tswap(scm.pid, UNIXCB(skb).pid);\n+\tswap_pids(scm.pid, UNIXCB(skb).pid);\n \n \tif (UNIXCB(skb).fp)\n \t\tunix_detach_fds(\u0026scm, skb);\n@@ -1991,7 +1988,7 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen\n {\n \tint err = 0;\n \n-\tUNIXCB(skb).pid = get_pid(scm-\u003epid);\n+\tget_pids(UNIXCB(skb).pid, scm-\u003epid);\n \tUNIXCB(skb).uid = scm-\u003ecreds.uid;\n \tUNIXCB(skb).gid = scm-\u003ecreds.gid;\n \tUNIXCB(skb).fp = NULL;\n@@ -2005,7 +2002,10 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen\n \n static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)\n {\n-\tscm_set_cred(scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);\n+\tget_pids(scm-\u003epid, UNIXCB(skb).pid);\n+\tscm-\u003ecreds.pid = pid_vnr(scm-\u003epid[PIDTYPE_TGID]);\n+\tscm-\u003ecreds.uid = UNIXCB(skb).uid;\n+\tscm-\u003ecreds.gid = UNIXCB(skb).gid;\n \tunix_set_secdata(scm, skb);\n }\n \n@@ -2025,30 +2025,35 @@ static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)\n static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,\n \t\t\t\tconst struct sock *other)\n {\n-\tif (UNIXCB(skb).pid)\n+\tif (UNIXCB(skb).pid[PIDTYPE_TGID])\n \t\treturn 0;\n \n \tif (unix_may_passcred(sk) || unix_may_passcred(other) ||\n \t    !other-\u003esk_socket) {\n-\t\tstruct pid *pid;\n \t\tint err;\n \n-\t\tpid = task_tgid(current);\n-\t\terr = pidfs_register_pid(pid);\n-\t\tif (unlikely(err))\n+\t\tget_task_pids(UNIXCB(skb).pid, current);\n+\t\terr = pidfs_register_pids(UNIXCB(skb).pid);\n+\t\tif (unlikely(err)) {\n+\t\t\tput_pids(UNIXCB(skb).pid);\n \t\t\treturn err;\n+\t\t}\n \n-\t\tUNIXCB(skb).pid = get_pid(pid);\n \t\tcurrent_uid_gid(\u0026UNIXCB(skb).uid, \u0026UNIXCB(skb).gid);\n \t}\n \n \treturn 0;\n }\n \n-static bool unix_skb_scm_eq(struct sk_buff *skb,\n+static bool unix_skb_scm_eq(const struct sock *sk, struct sk_buff *skb,\n \t\t\t    struct scm_cookie *scm)\n {\n-\treturn UNIXCB(skb).pid == scm-\u003epid \u0026\u0026\n+\t/* Only a thread pidfd receiver can tell threads of one process apart. */\n+\tif (sk-\u003esk_scm_pidfd_thread \u0026\u0026\n+\t    UNIXCB(skb).pid[PIDTYPE_PID] != scm-\u003epid[PIDTYPE_PID])\n+\t\treturn false;\n+\n+\treturn UNIXCB(skb).pid[PIDTYPE_TGID] == scm-\u003epid[PIDTYPE_TGID] \u0026\u0026\n \t       uid_eq(UNIXCB(skb).uid, scm-\u003ecreds.uid) \u0026\u0026\n \t       gid_eq(UNIXCB(skb).gid, scm-\u003ecreds.gid) \u0026\u0026\n \t       unix_secdata_eq(scm, skb);\n@@ -3026,7 +3031,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,\n \n \t\tif (check_creds) {\n \t\t\t/* Never glue messages from different writers */\n-\t\t\tif (!unix_skb_scm_eq(skb, \u0026scm))\n+\t\t\tif (!unix_skb_scm_eq(sk, skb, \u0026scm))\n \t\t\t\tbreak;\n \t\t} else if (unix_may_passcred(sk)) {\n \t\t\t/* Copy credentials */\ndiff --git a/net/unix/af_unix.h b/net/unix/af_unix.h\nindex 8119dbeef3a3c..402742895acca 100644\n--- a/net/unix/af_unix.h\n+++ b/net/unix/af_unix.h\n@@ -2,6 +2,7 @@\n #ifndef __AF_UNIX_H\n #define __AF_UNIX_H\n \n+#include \u003clinux/pid_types.h\u003e\n #include \u003clinux/uidgid.h\u003e\n \n #define UNIX_HASH_MOD\t(256 - 1)\n@@ -11,7 +12,7 @@\n struct sock *unix_peer_get(struct sock *sk);\n \n struct unix_skb_parms {\n-\tstruct pid\t\t*pid;\t\t/* skb credentials\t*/\n+\tDECLARE_PIDS(pid, PIDTYPE_TGID);\t/* skb credentials by pid type */\n \tkuid_t\t\t\tuid;\n \tkgid_t\t\t\tgid;\n \tstruct scm_fp_list\t*fp;\t\t/* Passed files\t\t*/\ndiff --git a/tools/testing/selftests/coredump/coredump_socket_test.c b/tools/testing/selftests/coredump/coredump_socket_test.c\nindex 422728f632ca6..ec73bb690bbcb 100644\n--- a/tools/testing/selftests/coredump/coredump_socket_test.c\n+++ b/tools/testing/selftests/coredump/coredump_socket_test.c\n@@ -592,6 +592,181 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)\n \twait_and_check_coredump_server(pid_coredump_server, _metadata, self);\n }\n \n+static bool check_coredump_info(const struct pidfd_info *info, const char *what)\n+{\n+\tif (!(info-\u003emask \u0026 PIDFD_INFO_COREDUMP)) {\n+\t\tfprintf(stderr, \"%s: PIDFD_INFO_COREDUMP not set in mask\\n\", what);\n+\t\treturn false;\n+\t}\n+\n+\tif (!(info-\u003ecoredump_mask \u0026 PIDFD_COREDUMPED)) {\n+\t\tfprintf(stderr, \"%s: PIDFD_COREDUMPED not set in coredump_mask\\n\", what);\n+\t\treturn false;\n+\t}\n+\n+\tif (!(info-\u003emask \u0026 PIDFD_INFO_COREDUMP_SIGNAL) || info-\u003ecoredump_signal != SIGSEGV) {\n+\t\tfprintf(stderr, \"%s: coredump_signal=%d, expected SIGSEGV=%d\\n\",\n+\t\t\twhat, info-\u003ecoredump_signal, SIGSEGV);\n+\t\treturn false;\n+\t}\n+\n+\tif (!(info-\u003emask \u0026 PIDFD_INFO_COREDUMP_CODE) || info-\u003ecoredump_code != SEGV_MAPERR) {\n+\t\tfprintf(stderr, \"%s: coredump_code=%d, expected SEGV_MAPERR=%d\\n\",\n+\t\t\twhat, info-\u003ecoredump_code, SEGV_MAPERR);\n+\t\treturn false;\n+\t}\n+\n+\treturn true;\n+}\n+\n+/*\n+ * Test: PIDFD_INFO_COREDUMP on the dumping thread's pidfd\n+ *\n+ * Crash from a non-leader thread and verify that the pidfd from\n+ * SO_PEERPIDFD_THREAD refers to that thread and reports the coredump\n+ * like the thread-group leader's pidfd from SO_PEERPIDFD does.\n+ */\n+TEST_F(coredump, socket_coredump_thread)\n+{\n+\tint pidfd, ret, status;\n+\tpid_t pid, pid_coredump_server;\n+\tstruct pidfd_info info = {};\n+\tint ipc_sockets[2];\n+\tchar c;\n+\n+\tASSERT_TRUE(set_core_pattern(\"@/tmp/coredump.socket\"));\n+\n+\tret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);\n+\tASSERT_EQ(ret, 0);\n+\n+\tpid_coredump_server = fork();\n+\tASSERT_GE(pid_coredump_server, 0);\n+\tif (pid_coredump_server == 0) {\n+\t\tint fd_server = -1, fd_coredump = -1, fd_peer_pidfd = -1;\n+\t\tint fd_thread_pidfd = -1, fd_core_file = -1;\n+\t\tstruct pidfd_info thread_info = {};\n+\t\tint exit_code = EXIT_FAILURE;\n+\n+\t\tclose(ipc_sockets[0]);\n+\n+\t\tfd_server = create_and_listen_unix_socket(\"/tmp/coredump.socket\");\n+\t\tif (fd_server \u003c 0) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: listen socket failed: %m\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tif (write_nointr(ipc_sockets[1], \"1\", 1) \u003c 0) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: ipc write failed: %m\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tclose(ipc_sockets[1]);\n+\n+\t\tfd_coredump = accept4(fd_server, NULL, NULL, SOCK_CLOEXEC);\n+\t\tif (fd_coredump \u003c 0) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: accept4 failed: %m\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tfd_peer_pidfd = get_peer_pidfd(fd_coredump);\n+\t\tif (fd_peer_pidfd \u003c 0) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: get_peer_pidfd failed\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tfd_thread_pidfd = get_peer_pidfd_thread(fd_coredump);\n+\t\tif (fd_thread_pidfd \u003c 0) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: get_peer_pidfd_thread failed\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tif (!get_pidfd_info(fd_peer_pidfd, \u0026info) ||\n+\t\t    !get_pidfd_info(fd_thread_pidfd, \u0026thread_info)) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: get_pidfd_info failed\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\t/* The peer is the thread-group leader, the dumping thread is not. */\n+\t\tif (info.pid != info.tgid || thread_info.tgid != info.tgid ||\n+\t\t    thread_info.pid == thread_info.tgid) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: unexpected ids %d/%d and %d/%d\\n\",\n+\t\t\t\tinfo.pid, info.tgid, thread_info.pid, thread_info.tgid);\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tif (!check_coredump_info(\u0026info, \"SO_PEERPIDFD\") ||\n+\t\t    !check_coredump_info(\u0026thread_info, \"SO_PEERPIDFD_THREAD\"))\n+\t\t\tgoto out;\n+\n+\t\tfd_core_file = open_coredump_tmpfile(self-\u003efd_tmpfs_detached);\n+\t\tif (fd_core_file \u003c 0) {\n+\t\t\tfprintf(stderr, \"socket_coredump_thread: core tmpfile failed: %m\\n\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tfor (;;) {\n+\t\t\tchar buffer[4096];\n+\t\t\tssize_t bytes_read, bytes_write;\n+\n+\t\t\tbytes_read = read(fd_coredump, buffer, sizeof(buffer));\n+\t\t\tif (bytes_read \u003c 0) {\n+\t\t\t\tfprintf(stderr, \"socket_coredump_thread: core read failed: %m\\n\");\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\n+\t\t\tif (bytes_read == 0)\n+\t\t\t\tbreak;\n+\n+\t\t\tbytes_write = write(fd_core_file, buffer, bytes_read);\n+\t\t\tif (bytes_read != bytes_write) {\n+\t\t\t\tfprintf(stderr, \"socket_coredump_thread: core write %zd/%zd: %m\\n\",\n+\t\t\t\t\tbytes_read, bytes_write);\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t}\n+\n+\t\texit_code = EXIT_SUCCESS;\n+\t\tfprintf(stderr, \"socket_coredump_thread: completed successfully\\n\");\n+out:\n+\t\tif (fd_core_file \u003e= 0)\n+\t\t\tclose(fd_core_file);\n+\t\tif (fd_thread_pidfd \u003e= 0)\n+\t\t\tclose(fd_thread_pidfd);\n+\t\tif (fd_peer_pidfd \u003e= 0)\n+\t\t\tclose(fd_peer_pidfd);\n+\t\tif (fd_coredump \u003e= 0)\n+\t\t\tclose(fd_coredump);\n+\t\tif (fd_server \u003e= 0)\n+\t\t\tclose(fd_server);\n+\t\t_exit(exit_code);\n+\t}\n+\tself-\u003epid_coredump_server = pid_coredump_server;\n+\n+\tEXPECT_EQ(close(ipc_sockets[1]), 0);\n+\tASSERT_EQ(read_nointr(ipc_sockets[0], \u0026c, 1), 1);\n+\tEXPECT_EQ(close(ipc_sockets[0]), 0);\n+\n+\tpid = fork();\n+\tASSERT_GE(pid, 0);\n+\tif (pid == 0)\n+\t\tcrashing_child_thread();\n+\n+\tpidfd = sys_pidfd_open(pid, 0);\n+\tASSERT_GE(pidfd, 0);\n+\n+\twaitpid(pid, \u0026status, 0);\n+\tASSERT_TRUE(WIFSIGNALED(status));\n+\tASSERT_EQ(WTERMSIG(status), SIGSEGV);\n+\tASSERT_TRUE(WCOREDUMP(status));\n+\n+\tASSERT_TRUE(get_pidfd_info(pidfd, \u0026info));\n+\tASSERT_TRUE(!!(info.mask \u0026 PIDFD_INFO_COREDUMP));\n+\tASSERT_TRUE(!!(info.coredump_mask \u0026 PIDFD_COREDUMPED));\n+\tASSERT_EQ(info.coredump_signal, SIGSEGV);\n+\n+\twait_and_check_coredump_server(pid_coredump_server, _metadata, self);\n+}\n+\n /*\n  * Test: PIDFD_INFO_COREDUMP_SIGNAL via simple socket coredump with SIGABRT\n  *\ndiff --git a/tools/testing/selftests/coredump/coredump_test.h b/tools/testing/selftests/coredump/coredump_test.h\nindex ed47f01fa53c5..4212656e31f01 100644\n--- a/tools/testing/selftests/coredump/coredump_test.h\n+++ b/tools/testing/selftests/coredump/coredump_test.h\n@@ -27,10 +27,12 @@ FIXTURE(coredump)\n /* Shared helper function declarations */\n void *do_nothing(void *arg);\n void crashing_child(void);\n+void crashing_child_thread(void);\n int create_detached_tmpfs(void);\n int create_and_listen_unix_socket(const char *path);\n bool set_core_pattern(const char *pattern);\n int get_peer_pidfd(int fd);\n+int get_peer_pidfd_thread(int fd);\n bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info);\n \n /* Inline helper that uses harness types */\ndiff --git a/tools/testing/selftests/coredump/coredump_test_helpers.c b/tools/testing/selftests/coredump/coredump_test_helpers.c\nindex 2a20faf9cb0ad..36306069f62e5 100644\n--- a/tools/testing/selftests/coredump/coredump_test_helpers.c\n+++ b/tools/testing/selftests/coredump/coredump_test_helpers.c\n@@ -13,6 +13,7 @@\n #include \u003cstring.h\u003e\n #include \u003csys/epoll.h\u003e\n #include \u003csys/ioctl.h\u003e\n+#include \u003csys/mman.h\u003e\n #include \u003csys/socket.h\u003e\n #include \u003csys/types.h\u003e\n #include \u003csys/un.h\u003e\n@@ -38,6 +39,10 @@ struct _fixture_coredump_data {\n \n #define NUM_THREAD_SPAWN 128\n \n+#ifndef SO_PEERPIDFD_THREAD\n+#define SO_PEERPIDFD_THREAD 87\n+#endif\n+\n void *do_nothing(void *arg)\n {\n \t(void)arg;\n@@ -59,6 +64,36 @@ void crashing_child(void)\n \ti = *(volatile int *)NULL;\n }\n \n+static void *crashing_thread(void *arg)\n+{\n+\tint *p;\n+\n+\t(void)arg;\n+\n+\t/* crash on purpose with SEGV_MAPERR */\n+\tp = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,\n+\t\t MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n+\tif (p == MAP_FAILED)\n+\t\treturn NULL;\n+\tmunmap(p, PAGE_SIZE);\n+\t*p = 0;\n+\n+\treturn NULL;\n+}\n+\n+void crashing_child_thread(void)\n+{\n+\tpthread_t thread;\n+\tint i;\n+\n+\tfor (i = 0; i \u003c NUM_THREAD_SPAWN; ++i)\n+\t\tpthread_create(\u0026thread, NULL, do_nothing, NULL);\n+\n+\t/* crash from a non-leader thread */\n+\tpthread_create(\u0026thread, NULL, crashing_thread, NULL);\n+\tpause();\n+}\n+\n int create_detached_tmpfs(void)\n {\n \tint fd_context, fd_tmpfs;\n@@ -138,6 +173,20 @@ int get_peer_pidfd(int fd)\n \treturn fd_peer_pidfd;\n }\n \n+int get_peer_pidfd_thread(int fd)\n+{\n+\tint fd_peer_pidfd;\n+\tsocklen_t fd_peer_pidfd_len = sizeof(fd_peer_pidfd);\n+\tint ret = getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD_THREAD, \u0026fd_peer_pidfd,\n+\t\t\t     \u0026fd_peer_pidfd_len);\n+\tif (ret \u003c 0) {\n+\t\tfprintf(stderr, \"%s: getsockopt(SO_PEERPIDFD_THREAD) failed: %m\\n\", __func__);\n+\t\treturn -1;\n+\t}\n+\tfprintf(stderr, \"%s: successfully retrieved pidfd %d\\n\", __func__, fd_peer_pidfd);\n+\treturn fd_peer_pidfd;\n+}\n+\n bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info)\n {\n \tint ret;\ndiff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile\nindex a66f10fb0c231..45b841758f1bf 100644\n--- a/tools/testing/selftests/net/af_unix/Makefile\n+++ b/tools/testing/selftests/net/af_unix/Makefile\n@@ -23,6 +23,8 @@ TEST_GEN_FILES := scm_rights_denial_lsm.bpf.o\n include ../../lib.mk\n include ../bpf.mk\n \n+$(OUTPUT)/scm_pidfd: CFLAGS += -pthread\n+\n $(OUTPUT)/scm_rights_denial_lsm: $(BPFOBJ)\n $(OUTPUT)/scm_rights_denial_lsm: CFLAGS += -I$(SCRATCH_DIR)/include\n $(OUTPUT)/scm_rights_denial_lsm: LDLIBS += -lelf -lz\ndiff --git a/tools/testing/selftests/net/af_unix/scm_pidfd.c b/tools/testing/selftests/net/af_unix/scm_pidfd.c\nindex 2c18b92a26035..bbbd5e7b4fa78 100644\n--- a/tools/testing/selftests/net/af_unix/scm_pidfd.c\n+++ b/tools/testing/selftests/net/af_unix/scm_pidfd.c\n@@ -10,6 +10,7 @@\n #include \u003cunistd.h\u003e\n #include \u003cstring.h\u003e\n #include \u003cerrno.h\u003e\n+#include \u003cpthread.h\u003e\n #include \u003csys/un.h\u003e\n #include \u003csys/signal.h\u003e\n #include \u003csys/types.h\u003e\n@@ -27,6 +28,14 @@\n #define SCM_PIDFD 0x04\n #endif\n \n+#ifndef SO_PASSPIDFD_THREAD\n+#define SO_PASSPIDFD_THREAD 86\n+#endif\n+\n+#ifndef SO_PEERPIDFD_THREAD\n+#define SO_PEERPIDFD_THREAD 87\n+#endif\n+\n #define CHILD_EXIT_CODE_OK 123\n \n static void child_die()\n@@ -553,4 +562,361 @@ TEST_F(scm_pidfd, test)\n \tclose(pfd);\n }\n \n+struct thread_ids {\n+\tpid_t pid;\n+\tpid_t tid;\n+};\n+\n+static void *send_ids_thread(void *arg)\n+{\n+\tint fd = *(int *)arg;\n+\tstruct thread_ids ids = {\n+\t\t.pid = getpid(),\n+\t\t.tid = gettid(),\n+\t};\n+\tchar sync;\n+\n+\tif (send(fd, \u0026ids, sizeof(ids), 0) != sizeof(ids))\n+\t\treturn (void *)1;\n+\n+\t/* stay alive until the receiver has looked at our pidfd */\n+\tif (read(fd, \u0026sync, 1) != 1)\n+\t\treturn (void *)1;\n+\n+\treturn NULL;\n+}\n+\n+static void *send_ids_creds_thread(void *arg)\n+{\n+\tint fd = *(int *)arg;\n+\tstruct thread_ids ids = {\n+\t\t.pid = getpid(),\n+\t\t.tid = gettid(),\n+\t};\n+\tstruct ucred ucred = {\n+\t\t.pid = getpid(),\n+\t\t.uid = getuid(),\n+\t\t.gid = getgid(),\n+\t};\n+\tchar control[CMSG_SPACE(sizeof(ucred))] = { 0 };\n+\tstruct iovec iov;\n+\tstruct msghdr msg = { 0 };\n+\tstruct cmsghdr *cmsg;\n+\tchar sync;\n+\n+\tiov.iov_base = \u0026ids;\n+\tiov.iov_len = sizeof(ids);\n+\n+\tmsg.msg_iov = \u0026iov;\n+\tmsg.msg_iovlen = 1;\n+\tmsg.msg_control = control;\n+\tmsg.msg_controllen = sizeof(control);\n+\n+\tcmsg = CMSG_FIRSTHDR(\u0026msg);\n+\tcmsg-\u003ecmsg_level = SOL_SOCKET;\n+\tcmsg-\u003ecmsg_type = SCM_CREDENTIALS;\n+\tcmsg-\u003ecmsg_len = CMSG_LEN(sizeof(ucred));\n+\tmemcpy(CMSG_DATA(cmsg), \u0026ucred, sizeof(ucred));\n+\n+\tif (sendmsg(fd, \u0026msg, 0) != sizeof(ids))\n+\t\treturn (void *)1;\n+\n+\tif (read(fd, \u0026sync, 1) != 1)\n+\t\treturn (void *)1;\n+\n+\treturn NULL;\n+}\n+\n+static void thread_client(int fd, int syncfd, void *(*sender)(void *))\n+{\n+\tpthread_t thread;\n+\tvoid *ret;\n+\tchar sync;\n+\n+\t/* wait until the receiver enabled SO_PASSPIDFD */\n+\tif (read(syncfd, \u0026sync, 1) != 1)\n+\t\tchild_die();\n+\n+\tif (pthread_create(\u0026thread, NULL, sender, \u0026fd))\n+\t\tchild_die();\n+\n+\tif (pthread_join(thread, \u0026ret) || ret)\n+\t\tchild_die();\n+\n+\texit(0);\n+}\n+\n+static int recv_pidfd(int fd, struct thread_ids *ids)\n+{\n+\tchar control[CMSG_SPACE(sizeof(int))] = { 0 };\n+\tstruct iovec iov;\n+\tstruct msghdr msg = { 0 };\n+\tstruct cmsghdr *cmsg;\n+\tint pidfd = -1;\n+\n+\tiov.iov_base = ids;\n+\tiov.iov_len = sizeof(*ids);\n+\n+\tmsg.msg_iov = \u0026iov;\n+\tmsg.msg_iovlen = 1;\n+\tmsg.msg_control = control;\n+\tmsg.msg_controllen = sizeof(control);\n+\n+\tif (recvmsg(fd, \u0026msg, 0) != sizeof(*ids)) {\n+\t\tlog_err(\"recvmsg\");\n+\t\treturn -1;\n+\t}\n+\n+\tif (msg.msg_flags \u0026 (MSG_TRUNC | MSG_CTRUNC)) {\n+\t\tlog_err(\"recvmsg: truncated\");\n+\t\treturn -1;\n+\t}\n+\n+\tfor (cmsg = CMSG_FIRSTHDR(\u0026msg); cmsg != NULL;\n+\t     cmsg = CMSG_NXTHDR(\u0026msg, cmsg)) {\n+\t\tif (cmsg-\u003ecmsg_level == SOL_SOCKET \u0026\u0026\n+\t\t    cmsg-\u003ecmsg_type == SCM_PIDFD)\n+\t\t\tmemcpy(\u0026pidfd, CMSG_DATA(cmsg), sizeof(pidfd));\n+\t}\n+\n+\treturn pidfd;\n+}\n+\n+static int thread_pidfd_flow(void *(*sender)(void *), int optname,\n+\t\t\t     struct thread_ids *ids, struct pidfd_info *info)\n+{\n+\tint sk[2];\n+\tint syncpipe[2];\n+\tint pidfd;\n+\tint child_status = 0;\n+\tpid_t child;\n+\n+\tif (socketpair(AF_UNIX, SOCK_STREAM, 0, sk))\n+\t\treturn -1;\n+\n+\tif (pipe(syncpipe))\n+\t\treturn -1;\n+\n+\tchild = fork();\n+\tif (child \u003c 0)\n+\t\treturn -1;\n+\n+\tif (child == 0) {\n+\t\tclose(sk[0]);\n+\t\tclose(syncpipe[1]);\n+\t\tthread_client(sk[1], syncpipe[0], sender);\n+\t}\n+\tclose(sk[1]);\n+\tclose(syncpipe[0]);\n+\n+\tint on = 1;\n+\n+\tif (setsockopt(sk[0], SOL_SOCKET, optname, \u0026on, sizeof(on))) {\n+\t\tlog_err(\"Failed to set pidfd passing option\");\n+\t\treturn -1;\n+\t}\n+\n+\tif (write(syncpipe[1], \"1\", 1) != 1)\n+\t\treturn -1;\n+\tclose(syncpipe[1]);\n+\n+\tpidfd = recv_pidfd(sk[0], ids);\n+\tif (pidfd \u003c 0)\n+\t\treturn -1;\n+\n+\tinfo-\u003emask = PIDFD_INFO_PID;\n+\tif (ioctl(pidfd, PIDFD_GET_INFO, info)) {\n+\t\tlog_err(\"ioctl(PIDFD_GET_INFO)\");\n+\t\tclose(pidfd);\n+\t\treturn -1;\n+\t}\n+\tclose(pidfd);\n+\n+\t/* release the sending thread */\n+\tif (write(sk[0], \"x\", 1) != 1)\n+\t\treturn -1;\n+\tclose(sk[0]);\n+\n+\twaitpid(child, \u0026child_status, 0);\n+\tif (!WIFEXITED(child_status) || WEXITSTATUS(child_status))\n+\t\treturn -1;\n+\n+\treturn 0;\n+}\n+\n+static int sockopt_set(int fd, int optname, int val)\n+{\n+\treturn setsockopt(fd, SOL_SOCKET, optname, \u0026val, sizeof(val));\n+}\n+\n+static int sockopt_get(int fd, int optname)\n+{\n+\tsocklen_t len = sizeof(int);\n+\tint val = -1;\n+\n+\tif (getsockopt(fd, SOL_SOCKET, optname, \u0026val, \u0026len))\n+\t\treturn -1;\n+\n+\treturn val;\n+}\n+\n+TEST(scm_pidfd_setsockopt_values)\n+{\n+\tint sk[2];\n+\n+\tASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sk));\n+\n+\tASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 1));\n+\tASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));\n+\tASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));\n+\n+\t/* The option set last wins. */\n+\tASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD, 1));\n+\tASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD));\n+\tASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));\n+\n+\tASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 1));\n+\tASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));\n+\tASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));\n+\n+\t/* Disabling one option leaves the other alone. */\n+\tASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD, 0));\n+\tASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));\n+\tASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 0));\n+\tASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));\n+\tASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));\n+\n+\tclose(sk[0]);\n+\tclose(sk[1]);\n+}\n+\n+TEST(scm_pidfd_thread)\n+{\n+\tstruct thread_ids ids;\n+\tstruct pidfd_info info;\n+\n+\tASSERT_EQ(0, thread_pidfd_flow(send_ids_thread, SO_PASSPIDFD_THREAD,\n+\t\t\t\t       \u0026ids, \u0026info));\n+\tASSERT_NE(ids.pid, ids.tid);\n+\tEXPECT_EQ(ids.tid, info.pid);\n+\tEXPECT_EQ(ids.pid, info.tgid);\n+}\n+\n+TEST(scm_pidfd_thread_group)\n+{\n+\tstruct thread_ids ids;\n+\tstruct pidfd_info info;\n+\n+\tASSERT_EQ(0, thread_pidfd_flow(send_ids_thread, SO_PASSPIDFD,\n+\t\t\t\t       \u0026ids, \u0026info));\n+\tASSERT_NE(ids.pid, ids.tid);\n+\tEXPECT_EQ(ids.pid, info.pid);\n+\tEXPECT_EQ(ids.pid, info.tgid);\n+}\n+\n+TEST(scm_pidfd_thread_creds)\n+{\n+\tstruct thread_ids ids;\n+\tstruct pidfd_info info;\n+\n+\tASSERT_EQ(0, thread_pidfd_flow(send_ids_creds_thread, SO_PASSPIDFD_THREAD,\n+\t\t\t\t       \u0026ids, \u0026info));\n+\tASSERT_NE(ids.pid, ids.tid);\n+\tEXPECT_EQ(ids.tid, info.pid);\n+\tEXPECT_EQ(ids.pid, info.tgid);\n+}\n+\n+static void *peer_connect_thread(void *arg)\n+{\n+\tstruct sock_addr *sa = arg;\n+\tstruct thread_ids ids = {\n+\t\t.pid = getpid(),\n+\t\t.tid = gettid(),\n+\t};\n+\tint fd;\n+\tchar sync;\n+\n+\tfd = socket(AF_UNIX, SOCK_STREAM, 0);\n+\tif (fd \u003c 0)\n+\t\treturn (void *)1;\n+\n+\tif (connect(fd, (struct sockaddr *)\u0026sa-\u003elisten_addr, sa-\u003eaddrlen))\n+\t\treturn (void *)1;\n+\n+\tif (send(fd, \u0026ids, sizeof(ids), 0) != sizeof(ids))\n+\t\treturn (void *)1;\n+\n+\t/* stay alive until the server has looked at our pidfd */\n+\tif (read(fd, \u0026sync, 1) != 1)\n+\t\treturn (void *)1;\n+\n+\tclose(fd);\n+\treturn NULL;\n+}\n+\n+static int peer_pidfd_info(int fd, int optname, struct pidfd_info *info)\n+{\n+\tint pidfd;\n+\tsocklen_t len = sizeof(pidfd);\n+\n+\tif (getsockopt(fd, SOL_SOCKET, optname, \u0026pidfd, \u0026len)) {\n+\t\tlog_err(\"getsockopt(SO_PEERPIDFD*)\");\n+\t\treturn -1;\n+\t}\n+\n+\tinfo-\u003emask = PIDFD_INFO_PID;\n+\tif (ioctl(pidfd, PIDFD_GET_INFO, info)) {\n+\t\tlog_err(\"ioctl(PIDFD_GET_INFO)\");\n+\t\tclose(pidfd);\n+\t\treturn -1;\n+\t}\n+\n+\tclose(pidfd);\n+\treturn 0;\n+}\n+\n+/* SO_PEERPIDFD_THREAD returns a pidfd for the peer's connecting thread. */\n+TEST(so_peerpidfd_thread)\n+{\n+\tstruct sock_addr sa;\n+\tstruct thread_ids ids;\n+\tstruct pidfd_info info;\n+\tpthread_t thread;\n+\tvoid *tret;\n+\tint server, cfd;\n+\n+\tserver = socket(AF_UNIX, SOCK_STREAM, 0);\n+\tASSERT_LE(0, server);\n+\n+\tfill_sockaddr(\u0026sa, true);\n+\tASSERT_EQ(0, bind(server, (struct sockaddr *)\u0026sa.listen_addr, sa.addrlen));\n+\tASSERT_EQ(0, listen(server, 1));\n+\n+\tASSERT_EQ(0, pthread_create(\u0026thread, NULL, peer_connect_thread, \u0026sa));\n+\n+\tcfd = accept(server, NULL, NULL);\n+\tASSERT_LE(0, cfd);\n+\n+\tASSERT_EQ(sizeof(ids), recv(cfd, \u0026ids, sizeof(ids), MSG_WAITALL));\n+\tASSERT_NE(ids.pid, ids.tid);\n+\n+\t/* SO_PEERPIDFD refers to the peer's thread-group. */\n+\tASSERT_EQ(0, peer_pidfd_info(cfd, SO_PEERPIDFD, \u0026info));\n+\tEXPECT_EQ(ids.pid, info.pid);\n+\tEXPECT_EQ(ids.pid, info.tgid);\n+\n+\t/* SO_PEERPIDFD_THREAD refers to the connecting thread. */\n+\tASSERT_EQ(0, peer_pidfd_info(cfd, SO_PEERPIDFD_THREAD, \u0026info));\n+\tEXPECT_EQ(ids.tid, info.pid);\n+\tEXPECT_EQ(ids.pid, info.tgid);\n+\n+\t/* release the connecting thread */\n+\tASSERT_EQ(1, write(cfd, \"x\", 1));\n+\tASSERT_EQ(0, pthread_join(thread, \u0026tret));\n+\tASSERT_EQ(NULL, tret);\n+\n+\tclose(cfd);\n+\tclose(server);\n+}\n+\n TEST_HARNESS_MAIN\n"
}

2/1 2026/08/31 12:30 agent patch-evaluator
4m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "UNIX",
    "COREDUMP"
  ],
  "FocusSymbols": [
    "sk_setsockopt",
    "sk_getsockopt",
    "scm_recv_unix",
    "__scm_send",
    "__pidfs_register_pids",
    "pidfs_coredump"
  ],
  "Reasoning": "The patch introduces the SO_PASSPIDFD_THREAD and SO_PEERPIDFD_THREAD socket options to allow passing and querying thread-specific pidfds over UNIX domain sockets. It updates struct sock, SCM credential passing, UNIX socket read/write credential matching, pidfs pid registration, and coredump notification mechanisms.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer.
Your job is to review a provided patch series and determine
if it makes functional changes to the kernel that should be fuzzed.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively
use your code access tools to examine the actual source code and confirm any assumptions.

Return WorthFuzzing=false if the patch only contains:
- Modifications to Documentation/, Kconfig files, or code comments.
- Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints.
- Changes to numeric constants or macros that do not functionally alter execution flow.
- Code paths that are impossible to reach in virtualized environments like GCE or QEMU,
  even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim).
- Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed,
  ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU.
- Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove)
  that are executed only during PCI hot-unplug or sysfs driver unbind operations.

If it modifies reachable core kernel logic, drivers, or architectures, use your code search
tools to verify the code can be executed, then return WorthFuzzing=true.

When returning WorthFuzzing=true, you MUST ALSO:
1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols.
   Avoid listing generic hot-path functions to prevent skewed test distributions.
   Prefer non-static, non-inlined API entrypoint functions over internal static helper functions
   (which are inlined by the compiler and do not have distinct symbol addresses).
2. Identify any specific CONFIG_ options required to properly test this new/modified feature.
   Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions.
   Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs
   needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs
   output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit 332b03a59787671294e517d035f545dd992063b1
Author: syz-cluster <triage@syzkaller.com>
Date:   Mon Aug 31 12:30:23 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h
index 946a5fad26912..5d3524c26b2bc 100644
--- a/arch/alpha/include/uapi/asm/socket.h
+++ b/arch/alpha/include/uapi/asm/socket.h
@@ -157,6 +157,10 @@
 
 #define SO_RIGHTS_NOTRUNC      85
 
+#define SO_PASSPIDFD_THREAD    86
+
+#define SO_PEERPIDFD_THREAD    87
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h
index f1641dde135fc..245a43f52fb1a 100644
--- a/arch/mips/include/uapi/asm/socket.h
+++ b/arch/mips/include/uapi/asm/socket.h
@@ -168,6 +168,10 @@
 
 #define SO_RIGHTS_NOTRUNC      85
 
+#define SO_PASSPIDFD_THREAD    86
+
+#define SO_PEERPIDFD_THREAD    87
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h
index f3a3815c7dc29..f23710e1c6714 100644
--- a/arch/parisc/include/uapi/asm/socket.h
+++ b/arch/parisc/include/uapi/asm/socket.h
@@ -149,6 +149,10 @@
 
 #define SO_RIGHTS_NOTRUNC	0x4053
 
+#define SO_PASSPIDFD_THREAD	0x4054
+
+#define SO_PEERPIDFD_THREAD	0x4055
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h
index 7907f3b1f0ee0..b35b25bdefc2b 100644
--- a/arch/sparc/include/uapi/asm/socket.h
+++ b/arch/sparc/include/uapi/asm/socket.h
@@ -150,6 +150,10 @@
 
 #define SO_RIGHTS_NOTRUNC        0x005e
 
+#define SO_PASSPIDFD_THREAD      0x005f
+
+#define SO_PEERPIDFD_THREAD      0x0060
+
 #if !defined(__KERNEL__)
 
 
diff --git a/fs/coredump.c b/fs/coredump.c
index ac3cd74808c64..b5ff4b3e1831e 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -454,7 +454,7 @@ static bool coredump_parse(struct core_name *cn, struct coredump_params *cprm,
 				 * leader we know that the thread-group leader
 				 * cannot be reaped until @current has exited.
 				 */
-				cprm->pid = task_tgid(current);
+				task_pids(cprm->pid, current);
 				err = cn_printf(cn, "%d", COREDUMP_PIDFD_NUMBER);
 				break;
 			}
@@ -626,13 +626,17 @@ static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)
 	struct coredump_params *cp = (struct coredump_params *)info->data;
 	int err;
 
-	if (cp->pid) {
+	if (cp->pid[PIDTYPE_TGID]) {
 		struct file *pidfs_file __free(fput) = NULL;
 
-		pidfs_file = pidfs_alloc_file(cp->pid, 0);
+		pidfs_file = pidfs_alloc_file(cp->pid[PIDTYPE_TGID], 0);
 		if (IS_ERR(pidfs_file))
 			return PTR_ERR(pidfs_file);
 
+		err = pidfs_register_pids(cp->pid);
+		if (err)
+			return err;
+
 		pidfs_coredump(cp);
 
 		/*
@@ -695,12 +699,12 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *
 		return false;
 
 	/*
-	 * Set the thread-group leader pid which is used for the peer
-	 * credentials during connect() below. Then immediately register
-	 * it in pidfs...
+	 * Set the pids of the dumping thread and its thread-group leader
+	 * which are used for the peer credentials during connect() below.
+	 * Then immediately register them in pidfs...
 	 */
-	cprm->pid = task_tgid(current);
-	retval = pidfs_register_pid(cprm->pid);
+	task_pids(cprm->pid, current);
+	retval = pidfs_register_pids(cprm->pid);
 	if (retval)
 		return false;
 
@@ -722,7 +726,7 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *
 	}
 
 	/* ... and validate that @sk_peer_pid matches @cprm.pid. */
-	if (WARN_ON_ONCE(unix_peer(socket->sk)->sk_peer_pid != cprm->pid))
+	if (WARN_ON_ONCE(!pids_equal(unix_peer(socket->sk)->sk_peer_pid, cprm->pid)))
 		return false;
 
 	cprm->limit = RLIM_INFINITY;
diff --git a/fs/pidfs.c b/fs/pidfs.c
index a6a643f15d08a..29299b2c7ca71 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -793,9 +793,9 @@ void pidfs_exit(struct task_struct *tsk)
 }
 
 #ifdef CONFIG_COREDUMP
-void pidfs_coredump(const struct coredump_params *cprm)
+static void pidfs_coredump_pid(struct pid *pid,
+			       const struct coredump_params *cprm)
 {
-	struct pid *pid = cprm->pid;
 	struct pidfs_attr *attr;
 
 	attr = READ_ONCE(pid->attr);
@@ -814,6 +814,13 @@ void pidfs_coredump(const struct coredump_params *cprm)
 	smp_wmb();
 	set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
 }
+
+void pidfs_coredump(const struct coredump_params *cprm)
+{
+	/* The dumping thread's pidfd reports the coredump as well. */
+	for (enum pid_type type = PIDTYPE_PID; type <= pids_last(cprm->pid); type++)
+		pidfs_coredump_pid(cprm->pid[type], cprm);
+}
 #endif
 
 static struct vfsmount *pidfs_mnt __ro_after_init;
@@ -1070,6 +1077,22 @@ int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)
 	return 0;
 }
 
+/* Register the pids up to pid type @last of @pids in pidfs. */
+int __pidfs_register_pids(struct pid *const *pids, enum pid_type last)
+{
+	if (WARN_ON_ONCE(last >= PIDTYPE_MAX))
+		return -EINVAL;
+
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++) {
+		int ret = pidfs_register_pid(pids[type]);
+
+		if (unlikely(ret))
+			return ret;
+	}
+
+	return 0;
+}
+
 static struct dentry *pidfs_stash_dentry(struct dentry **stashed,
 					 struct dentry *dentry)
 {
diff --git a/include/linux/coredump.h b/include/linux/coredump.h
index 7b38ee2e7913b..0bbb7de6a4021 100644
--- a/include/linux/coredump.h
+++ b/include/linux/coredump.h
@@ -5,6 +5,7 @@
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/fs.h>
+#include <linux/pid_types.h>
 #include <linux/sched/coredump.h>
 #include <asm/siginfo.h>
 
@@ -32,7 +33,8 @@ struct coredump_params {
 	int vma_count;
 	size_t vma_data_size;
 	struct core_vma_metadata *vma_meta;
-	struct pid *pid;
+	/* Dumping thread and its thread-group leader by pid type. */
+	DECLARE_PIDS(pid, PIDTYPE_TGID);
 };
 
 extern unsigned int core_file_note_size_limit;
diff --git a/include/linux/pid.h b/include/linux/pid.h
index ddaef0bbc8ba3..87635d0cc1f71 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -2,6 +2,9 @@
 #ifndef _LINUX_PID_H
 #define _LINUX_PID_H
 
+#include <linux/array_size.h>
+#include <linux/build_bug.h>
+#include <linux/minmax.h>
 #include <linux/pid_types.h>
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
@@ -92,6 +95,56 @@ static inline struct pid *get_pid(struct pid *pid)
 }
 
 extern void put_pid(struct pid *pid);
+
+/*
+ * Helpers for arrays of struct pid indexed by pid type declared with
+ * DECLARE_PIDS(). The array covers PIDTYPE_PID up to the pid type it
+ * was declared with and the helpers take that bound from the array.
+ */
+static inline void __get_pids(struct pid **dst, struct pid *const *src,
+			      enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		dst[type] = get_pid(src[type]);
+}
+
+static inline void __put_pids(struct pid **pids, enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++) {
+		put_pid(pids[type]);
+		pids[type] = NULL;
+	}
+}
+
+static inline void __swap_pids(struct pid **a, struct pid **b,
+			       enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		swap(a[type], b[type]);
+}
+
+static inline bool __pids_equal(struct pid *const *a, struct pid *const *b,
+				enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		if (a[type] != b[type])
+			return false;
+	return true;
+}
+
+/* The last pid type an array declared with DECLARE_PIDS() covers. */
+#define pids_last(pids)							\
+	((enum pid_type)(ARRAY_SIZE(pids) - 1 +				\
+			 BUILD_BUG_ON_ZERO(ARRAY_SIZE(pids) > PIDTYPE_MAX)))
+
+#define __pids_last2(a, b)						\
+	(pids_last(a) + BUILD_BUG_ON_ZERO(ARRAY_SIZE(a) != ARRAY_SIZE(b)))
+
+#define get_pids(dst, src)	__get_pids(dst, src, __pids_last2(dst, src))
+#define put_pids(pids)		__put_pids(pids, pids_last(pids))
+#define swap_pids(a, b)		__swap_pids(a, b, __pids_last2(a, b))
+#define pids_equal(a, b)	__pids_equal(a, b, __pids_last2(a, b))
+
 extern struct task_struct *pid_task(struct pid *pid, enum pid_type);
 static inline bool pid_has_task(struct pid *pid, enum pid_type type)
 {
diff --git a/include/linux/pid_types.h b/include/linux/pid_types.h
index c2aee1d91dcfd..3302690a2a281 100644
--- a/include/linux/pid_types.h
+++ b/include/linux/pid_types.h
@@ -2,6 +2,8 @@
 #ifndef _LINUX_PID_TYPES_H
 #define _LINUX_PID_TYPES_H
 
+#include <linux/build_bug.h>
+
 enum pid_type {
 	PIDTYPE_PID,
 	PIDTYPE_TGID,
@@ -10,6 +12,12 @@ enum pid_type {
 	PIDTYPE_MAX,
 };
 
+struct pid;
+
+/* An array of struct pid indexed by pid type, PIDTYPE_PID up to @last. */
+#define DECLARE_PIDS(name, last)					\
+	struct pid *name[(last) + 1 + BUILD_BUG_ON_ZERO((last) >= PIDTYPE_MAX)]
+
 struct pid_namespace;
 extern struct pid_namespace init_pid_ns;
 
diff --git a/include/linux/pidfs.h b/include/linux/pidfs.h
index 0abf7da9ab236..3c1e82f1a3691 100644
--- a/include/linux/pidfs.h
+++ b/include/linux/pidfs.h
@@ -3,6 +3,7 @@
 #define _LINUX_PID_FS_H
 
 #include <linux/gfp_types.h>
+#include <linux/pid.h>
 
 struct coredump_params;
 
@@ -32,5 +33,9 @@ static inline int pidfs_register_pid(struct pid *pid)
 }
 
 void pidfs_free_pid(struct pid *pid);
+int __pidfs_register_pids(struct pid *const *pids, enum pid_type last);
+
+/* Register the pids of an array declared with DECLARE_PIDS(). */
+#define pidfs_register_pids(pids)	__pidfs_register_pids(pids, pids_last(pids))
 
 #endif /* _LINUX_PID_FS_H */
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 584ae88b435e9..9444b47789a08 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -677,6 +677,24 @@ struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
 	return pid;
 }
 
+/* Fill @pids with the pid types of @task up to @last, without references. */
+static inline void __task_pids(struct pid **pids, enum pid_type last,
+			       struct task_struct *task)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		pids[type] = task_pid_type(task, type);
+}
+
+static inline void __get_task_pids(struct pid **pids, enum pid_type last,
+				   struct task_struct *task)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		pids[type] = get_pid(task_pid_type(task, type));
+}
+
+#define task_pids(pids, task)		__task_pids(pids, pids_last(pids), task)
+#define get_task_pids(pids, task)	__get_task_pids(pids, pids_last(pids), task)
+
 static inline struct pid *task_tgid(struct task_struct *task)
 {
 	return task->signal->pids[PIDTYPE_TGID];
diff --git a/include/net/scm.h b/include/net/scm.h
index 86ae6bc109ec6..aa7d15c5fc271 100644
--- a/include/net/scm.h
+++ b/include/net/scm.h
@@ -42,7 +42,7 @@ struct scm_fp_list {
 };
 
 struct scm_cookie {
-	struct pid		*pid;		/* Skb credentials */
+	DECLARE_PIDS(pid, PIDTYPE_TGID);	/* Skb credentials by pid type */
 	struct scm_fp_list	*fp;		/* Passed files		*/
 	struct scm_creds	creds;		/* Skb credentials	*/
 #ifdef CONFIG_SECURITY_NETWORK
@@ -69,7 +69,7 @@ static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_co
 static __inline__ void scm_set_cred(struct scm_cookie *scm,
 				    struct pid *pid, kuid_t uid, kgid_t gid)
 {
-	scm->pid = get_pid(pid);
+	scm->pid[PIDTYPE_TGID] = get_pid(pid);
 	scm->creds.pid = pid_vnr(pid);
 	scm->creds.uid = uid;
 	scm->creds.gid = gid;
@@ -77,8 +77,7 @@ static __inline__ void scm_set_cred(struct scm_cookie *scm,
 
 static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
 {
-	put_pid(scm->pid);
-	scm->pid = NULL;
+	put_pids(scm->pid);
 }
 
 static __inline__ void scm_destroy(struct scm_cookie *scm)
diff --git a/include/net/sock.h b/include/net/sock.h
index 51185222aac29..67b743bab220e 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -301,7 +301,7 @@ struct sk_filter;
   *	@sk_type: socket type (%SOCK_STREAM, etc)
   *	@sk_protocol: which protocol this socket belongs in this network family
   *	@sk_peer_lock: lock protecting @sk_peer_pid and @sk_peer_cred
-  *	@sk_peer_pid: &struct pid for this socket's peer
+  *	@sk_peer_pid: &struct pid for this socket's peer, by pid type
   *	@sk_peer_cred: %SO_PEERCRED setting
   *	@sk_rcvlowat: %SO_RCVLOWAT setting
   *	@sk_rcvtimeo: %SO_RCVTIMEO setting
@@ -356,6 +356,7 @@ struct sk_filter;
   *	@sk_scm_security: flagged by SO_PASSSEC to recv SCM_SECURITY
   *	@sk_scm_pidfd: flagged by SO_PASSPIDFD to recv SCM_PIDFD
   *	@sk_scm_rights: flagged by SO_PASSRIGHTS to recv SCM_RIGHTS
+  *	@sk_scm_pidfd_thread: flagged by SO_PASSPIDFD_THREAD to recv a thread SCM_PIDFD
   *	@sk_scm_unused: unused flags for scm_recv()
   *	@ns_tracker: tracker for netns reference
   *	@sk_user_frags: xarray of pages the user is holding a reference on.
@@ -545,7 +546,7 @@ struct sock {
 	u64			sk_ino;
 	spinlock_t		sk_peer_lock;
 	int			sk_bind_phc;
-	struct pid		*sk_peer_pid;
+	DECLARE_PIDS(sk_peer_pid, PIDTYPE_TGID);
 	const struct cred	*sk_peer_cred;
 
 	ktime_t			sk_stamp;
@@ -562,7 +563,8 @@ struct sock {
 				sk_scm_security : 1,
 				sk_scm_pidfd : 1,
 				sk_scm_rights : 1,
-				sk_scm_unused : 4;
+				sk_scm_pidfd_thread : 1,
+				sk_scm_unused : 3;
 		};
 	};
 	u8			sk_clockid;
@@ -2986,6 +2988,12 @@ static inline bool sk_is_stream_unix(const struct sock *sk)
 	return sk_is_unix(sk) && sk->sk_type == SOCK_STREAM;
 }
 
+/* SO_PASSPIDFD or SO_PASSPIDFD_THREAD asked for an SCM_PIDFD. */
+static inline bool sk_scm_pidfd_wanted(const struct sock *sk)
+{
+	return sk->sk_scm_pidfd || sk->sk_scm_pidfd_thread;
+}
+
 static inline bool sk_is_vsock(const struct sock *sk)
 {
 	return sk->sk_family == AF_VSOCK;
diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h
index 84ea7b92936e2..56fed7ab27ab8 100644
--- a/include/uapi/asm-generic/socket.h
+++ b/include/uapi/asm-generic/socket.h
@@ -152,6 +152,10 @@
 
 #define SO_RIGHTS_NOTRUNC	85
 
+#define SO_PASSPIDFD_THREAD	86
+
+#define SO_PEERPIDFD_THREAD	87
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 411d66f24393d..7758e9ea3848c 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -161,7 +161,7 @@ struct sock *bt_sock_alloc(struct net *net, struct socket *sock,
 	/* Init peer information so it can be properly monitored */
 	if (!kern) {
 		spin_lock(&sk->sk_peer_lock);
-		sk->sk_peer_pid  = get_pid(task_tgid(current));
+		sk->sk_peer_pid[PIDTYPE_TGID]  = get_pid(task_tgid(current));
 		sk->sk_peer_cred = get_current_cred();
 		spin_unlock(&sk->sk_peer_lock);
 	}
@@ -235,9 +235,9 @@ void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)
 	 * socket is allocated by the kernel.
 	 */
 	spin_lock(&sk->sk_peer_lock);
-	old_pid = sk->sk_peer_pid;
+	old_pid = sk->sk_peer_pid[PIDTYPE_TGID];
 	old_cred = sk->sk_peer_cred;
-	sk->sk_peer_pid = get_pid(parent->sk_peer_pid);
+	sk->sk_peer_pid[PIDTYPE_TGID] = get_pid(parent->sk_peer_pid[PIDTYPE_TGID]);
 	sk->sk_peer_cred = get_cred(parent->sk_peer_cred);
 	spin_unlock(&sk->sk_peer_lock);
 
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 070ca388f9ace..91e4738eabdb0 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -273,21 +273,21 @@ static void hci_sock_copy_creds(struct sock *sk, struct sk_buff *skb)
 	creds = &bt_cb(skb)->creds;
 
 	/* Check if peer credentials is set */
-	if (!sk->sk_peer_pid) {
+	if (!sk->sk_peer_pid[PIDTYPE_TGID]) {
 		/* Check if parent peer credentials is set */
-		if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid)
+		if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid[PIDTYPE_TGID])
 			sk = bt_sk(sk)->parent;
 		else
 			return;
 	}
 
 	/* Check if scm_creds already set */
-	if (creds->pid == pid_vnr(sk->sk_peer_pid))
+	if (creds->pid == pid_vnr(sk->sk_peer_pid[PIDTYPE_TGID]))
 		return;
 
 	memset(creds, 0, sizeof(*creds));
 
-	creds->pid = pid_vnr(sk->sk_peer_pid);
+	creds->pid = pid_vnr(sk->sk_peer_pid[PIDTYPE_TGID]);
 	if (sk->sk_peer_cred) {
 		creds->uid = sk->sk_peer_cred->uid;
 		creds->gid = sk->sk_peer_cred->gid;
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 1194c37e466f1..872d8fb31b6fb 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1890,7 +1890,7 @@ static struct pid *l2cap_sock_get_peer_pid_cb(struct l2cap_chan *chan)
 {
 	struct sock *sk = chan->data;
 
-	return sk->sk_peer_pid;
+	return sk->sk_peer_pid[PIDTYPE_TGID];
 }
 
 static void l2cap_sock_suspend_cb(struct l2cap_chan *chan)
diff --git a/net/core/scm.c b/net/core/scm.c
index f0d44ecdb11fc..d69768414af48 100644
--- a/net/core/scm.c
+++ b/net/core/scm.c
@@ -149,6 +149,7 @@ EXPORT_SYMBOL(__scm_destroy);
 
 static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
 {
+	struct pid *thread_pid;
 	int err;
 
 	/* drop all previous references */
@@ -158,7 +159,18 @@ static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
 	if (unlikely(err))
 		return err;
 
-	scm->pid = pid;
+	/* A sender naming its own thread-group sends from the current thread. */
+	if (pid == task_tgid(current))
+		thread_pid = task_pid(current);
+	else
+		thread_pid = pid;
+
+	err = pidfs_register_pid(thread_pid);
+	if (unlikely(err))
+		return err;
+
+	scm->pid[PIDTYPE_TGID] = pid;
+	scm->pid[PIDTYPE_PID] = get_pid(thread_pid);
 	scm->creds.pid = pid_vnr(pid);
 	return 0;
 }
@@ -207,7 +219,8 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
 			if (err)
 				goto error;
 
-			if (!p->pid || pid_vnr(p->pid) != creds.pid) {
+			if (!p->pid[PIDTYPE_TGID] ||
+			    pid_vnr(p->pid[PIDTYPE_TGID]) != creds.pid) {
 				struct pid *pid;
 				err = -ESRCH;
 				pid = find_get_pid(creds.pid);
@@ -486,9 +499,13 @@ static bool scm_has_secdata(struct sock *sk)
 }
 #endif
 
-static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
+static void scm_pidfd_recv(struct sock *sk, struct msghdr *msg,
+			   struct scm_cookie *scm)
 {
+	enum pid_type type = sk->sk_scm_pidfd_thread ? PIDTYPE_PID : PIDTYPE_TGID;
+	struct pid *pid = scm->pid[type];
 	struct file *pidfd_file = NULL;
+	unsigned int flags = PIDFD_STALE;
 	int len, pidfd;
 
 	/* put_cmsg() doesn't return an error if CMSG is truncated,
@@ -504,10 +521,13 @@ static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
 		return;
 	}
 
-	if (!scm->pid)
+	if (!pid)
 		return;
 
-	pidfd = pidfd_prepare(scm->pid, PIDFD_STALE, &pidfd_file);
+	if (type == PIDTYPE_PID)
+		flags |= PIDFD_THREAD;
+
+	pidfd = pidfd_prepare(pid, flags, &pidfd_file);
 
 	if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) {
 		if (pidfd_file) {
@@ -526,7 +546,7 @@ static bool __scm_recv_common(struct sock *sk, struct msghdr *msg,
 			      struct scm_cookie *scm, int flags)
 {
 	if (!msg->msg_control) {
-		if (sk->sk_scm_credentials || sk->sk_scm_pidfd ||
+		if (sk->sk_scm_credentials || sk_scm_pidfd_wanted(sk) ||
 		    scm->fp || scm_has_secdata(sk))
 			msg->msg_flags |= MSG_CTRUNC;
 
@@ -573,8 +593,8 @@ void scm_recv_unix(struct socket *sock, struct msghdr *msg,
 		scm_detach_fds(msg, scm, READ_ONCE(u->scm_rights_notrunc));
 	}
 
-	if (sock->sk->sk_scm_pidfd)
-		scm_pidfd_recv(msg, scm);
+	if (sk_scm_pidfd_wanted(sock->sk))
+		scm_pidfd_recv(sock->sk, msg, scm);
 
 	scm_destroy_cred(scm);
 }
diff --git a/net/core/sock.c b/net/core/sock.c
index 1ad41904db25b..cb2ffd329bc67 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -1571,10 +1571,25 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 		break;
 
 	case SO_PASSPIDFD:
-		if (sk_is_unix(sk))
+		if (sk_is_unix(sk)) {
+			/* Mutually exclusive with SO_PASSPIDFD_THREAD. */
 			sk->sk_scm_pidfd = valbool;
-		else
+			if (valbool)
+				sk->sk_scm_pidfd_thread = 0;
+		} else {
+			ret = -EOPNOTSUPP;
+		}
+		break;
+
+	case SO_PASSPIDFD_THREAD:
+		if (sk_is_unix(sk)) {
+			/* Mutually exclusive with SO_PASSPIDFD. */
+			sk->sk_scm_pidfd_thread = valbool;
+			if (valbool)
+				sk->sk_scm_pidfd = 0;
+		} else {
 			ret = -EOPNOTSUPP;
+		}
 		break;
 
 	case SO_PASSRIGHTS:
@@ -1728,6 +1743,50 @@ static int groups_to_user(sockptr_t dst, const struct group_info *src)
 	return 0;
 }
 
+/* Hand out a pidfd for @type of the socket's peer via SO_PEERPIDFD*. */
+static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type,
+				   sockptr_t optval, sockptr_t optlen, int len)
+{
+	struct file *pidfd_file = NULL;
+	unsigned int flags = 0;
+	struct pid *peer_pid;
+	int pidfd;
+
+	if (len > sizeof(pidfd))
+		len = sizeof(pidfd);
+
+	spin_lock(&sk->sk_peer_lock);
+	peer_pid = get_pid(sk->sk_peer_pid[type]);
+	spin_unlock(&sk->sk_peer_lock);
+
+	if (!peer_pid)
+		return -ENODATA;
+
+	/* The use of PIDFD_STALE requires stashing of struct pid on pidfs
+	 * with pidfs_register_pid() and only AF_UNIX were prepared for this.
+	 */
+	if (sk->sk_family == AF_UNIX)
+		flags |= PIDFD_STALE;
+	if (type == PIDTYPE_PID)
+		flags |= PIDFD_THREAD;
+
+	pidfd = pidfd_prepare(peer_pid, flags, &pidfd_file);
+	put_pid(peer_pid);
+	if (pidfd < 0)
+		return pidfd;
+
+	if (copy_to_sockptr(optval, &pidfd, len) ||
+	    copy_to_sockptr(optlen, &len, sizeof(int))) {
+		put_unused_fd(pidfd);
+		fput(pidfd_file);
+
+		return -EFAULT;
+	}
+
+	fd_install(pidfd, pidfd_file);
+	return 0;
+}
+
 int sk_getsockopt(struct sock *sk, int level, int optname,
 		  sockptr_t optval, sockptr_t optlen)
 {
@@ -1892,6 +1951,13 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 		v.val = sk->sk_scm_pidfd;
 		break;
 
+	case SO_PASSPIDFD_THREAD:
+		if (!sk_is_unix(sk))
+			return -EOPNOTSUPP;
+
+		v.val = sk->sk_scm_pidfd_thread;
+		break;
+
 	case SO_PASSRIGHTS:
 		if (!sk_is_unix(sk))
 			return -EOPNOTSUPP;
@@ -1906,7 +1972,8 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 			len = sizeof(peercred);
 
 		spin_lock(&sk->sk_peer_lock);
-		cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
+		cred_to_ucred(sk->sk_peer_pid[PIDTYPE_TGID], sk->sk_peer_cred,
+			      &peercred);
 		spin_unlock(&sk->sk_peer_lock);
 
 		if (copy_to_sockptr(optval, &peercred, len))
@@ -1915,45 +1982,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 	}
 
 	case SO_PEERPIDFD:
-	{
-		struct pid *peer_pid;
-		struct file *pidfd_file = NULL;
-		unsigned int flags = 0;
-		int pidfd;
-
-		if (len > sizeof(pidfd))
-			len = sizeof(pidfd);
-
-		spin_lock(&sk->sk_peer_lock);
-		peer_pid = get_pid(sk->sk_peer_pid);
-		spin_unlock(&sk->sk_peer_lock);
+		return sk_getsockopt_peerpidfd(sk, PIDTYPE_TGID, optval, optlen, len);
 
-		if (!peer_pid)
-			return -ENODATA;
-
-		/* The use of PIDFD_STALE requires stashing of struct pid
-		 * on pidfs with pidfs_register_pid() and only AF_UNIX
-		 * were prepared for this.
-		 */
-		if (sk->sk_family == AF_UNIX)
-			flags = PIDFD_STALE;
-
-		pidfd = pidfd_prepare(peer_pid, flags, &pidfd_file);
-		put_pid(peer_pid);
-		if (pidfd < 0)
-			return pidfd;
-
-		if (copy_to_sockptr(optval, &pidfd, len) ||
-		    copy_to_sockptr(optlen, &len, sizeof(int))) {
-			put_unused_fd(pidfd);
-			fput(pidfd_file);
-
-			return -EFAULT;
-		}
+	case SO_PEERPIDFD_THREAD:
+		/* Only AF_UNIX records the peer's connecting thread. */
+		if (!sk_is_unix(sk))
+			return -EOPNOTSUPP;
 
-		fd_install(pidfd, pidfd_file);
-		return 0;
-	}
+		return sk_getsockopt_peerpidfd(sk, PIDTYPE_PID, optval, optlen, len);
 
 	case SO_PEERGROUPS:
 	{
@@ -2379,7 +2415,7 @@ static void __sk_destruct(struct rcu_head *head)
 
 	/* We do not need to acquire sk->sk_peer_lock, we are the last user. */
 	put_cred(sk->sk_peer_cred);
-	put_pid(sk->sk_peer_pid);
+	put_pids(sk->sk_peer_pid);
 
 	if (likely(sk->sk_net_refcnt)) {
 		put_net_track(net, &sk->ns_tracker);
@@ -3775,7 +3811,7 @@ void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
 	sk->sk_frag.offset	=	0;
 	sk->sk_peek_off		=	-1;
 
-	sk->sk_peer_pid 	=	NULL;
+	memset(sk->sk_peer_pid, 0, sizeof(sk->sk_peer_pid));
 	sk->sk_peer_cred	=	NULL;
 	spin_lock_init(&sk->sk_peer_lock);
 
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 13f9926bf2057..563d9827c5cc5 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -737,57 +737,53 @@ static void unix_release_sock(struct sock *sk, int embrion)
 }
 
 struct unix_peercred {
-	struct pid *peer_pid;
+	DECLARE_PIDS(peer_pid, PIDTYPE_TGID);
 	const struct cred *peer_cred;
 };
 
 static inline int prepare_peercred(struct unix_peercred *peercred)
 {
-	struct pid *pid;
 	int err;
 
-	pid = task_tgid(current);
-	err = pidfs_register_pid(pid);
-	if (likely(!err)) {
-		peercred->peer_pid = get_pid(pid);
-		peercred->peer_cred = get_current_cred();
+	get_task_pids(peercred->peer_pid, current);
+	err = pidfs_register_pids(peercred->peer_pid);
+	if (unlikely(err)) {
+		put_pids(peercred->peer_pid);
+		return err;
 	}
-	return err;
+
+	peercred->peer_cred = get_current_cred();
+	return 0;
 }
 
 static void drop_peercred(struct unix_peercred *peercred)
 {
 	const struct cred *cred = NULL;
-	struct pid *pid = NULL;
 
 	might_sleep();
 
-	swap(peercred->peer_pid, pid);
+	put_pids(peercred->peer_pid);
 	swap(peercred->peer_cred, cred);
-
-	put_pid(pid);
 	put_cred(cred);
 }
 
 static inline void init_peercred(struct sock *sk,
 				 const struct unix_peercred *peercred)
 {
-	sk->sk_peer_pid = peercred->peer_pid;
+	memcpy(sk->sk_peer_pid, peercred->peer_pid, sizeof(sk->sk_peer_pid));
 	sk->sk_peer_cred = peercred->peer_cred;
 }
 
 static void update_peercred(struct sock *sk, struct unix_peercred *peercred)
 {
 	const struct cred *old_cred;
-	struct pid *old_pid;
 
 	spin_lock(&sk->sk_peer_lock);
-	old_pid = sk->sk_peer_pid;
+	swap_pids(sk->sk_peer_pid, peercred->peer_pid);
 	old_cred = sk->sk_peer_cred;
-	init_peercred(sk, peercred);
+	sk->sk_peer_cred = peercred->peer_cred;
 	spin_unlock(&sk->sk_peer_lock);
 
-	peercred->peer_pid = old_pid;
 	peercred->peer_cred = old_cred;
 }
 
@@ -796,14 +792,14 @@ static void copy_peercred(struct sock *sk, struct sock *peersk)
 	lockdep_assert_held(&unix_sk(peersk)->lock);
 
 	spin_lock(&sk->sk_peer_lock);
-	sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
+	get_pids(sk->sk_peer_pid, peersk->sk_peer_pid);
 	sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
 	spin_unlock(&sk->sk_peer_lock);
 }
 
 static bool unix_may_passcred(const struct sock *sk)
 {
-	return sk->sk_scm_credentials || sk->sk_scm_pidfd;
+	return sk->sk_scm_credentials || sk_scm_pidfd_wanted(sk);
 }
 
 static int unix_listen(struct socket *sock, int backlog)
@@ -1060,6 +1056,7 @@ static bool unix_bpf_bypass_getsockopt(int level, int optname)
 	if (level == SOL_SOCKET) {
 		switch (optname) {
 		case SO_PEERPIDFD:
+		case SO_PEERPIDFD_THREAD:
 			return true;
 		default:
 			return false;
@@ -1973,7 +1970,7 @@ static void unix_destruct_scm(struct sk_buff *skb)
 {
 	struct scm_cookie scm = {};
 
-	swap(scm.pid, UNIXCB(skb).pid);
+	swap_pids(scm.pid, UNIXCB(skb).pid);
 
 	if (UNIXCB(skb).fp)
 		unix_detach_fds(&scm, skb);
@@ -1991,7 +1988,7 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
 {
 	int err = 0;
 
-	UNIXCB(skb).pid = get_pid(scm->pid);
+	get_pids(UNIXCB(skb).pid, scm->pid);
 	UNIXCB(skb).uid = scm->creds.uid;
 	UNIXCB(skb).gid = scm->creds.gid;
 	UNIXCB(skb).fp = NULL;
@@ -2005,7 +2002,10 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
 
 static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)
 {
-	scm_set_cred(scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
+	get_pids(scm->pid, UNIXCB(skb).pid);
+	scm->creds.pid = pid_vnr(scm->pid[PIDTYPE_TGID]);
+	scm->creds.uid = UNIXCB(skb).uid;
+	scm->creds.gid = UNIXCB(skb).gid;
 	unix_set_secdata(scm, skb);
 }
 
@@ -2025,30 +2025,35 @@ static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)
 static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
 				const struct sock *other)
 {
-	if (UNIXCB(skb).pid)
+	if (UNIXCB(skb).pid[PIDTYPE_TGID])
 		return 0;
 
 	if (unix_may_passcred(sk) || unix_may_passcred(other) ||
 	    !other->sk_socket) {
-		struct pid *pid;
 		int err;
 
-		pid = task_tgid(current);
-		err = pidfs_register_pid(pid);
-		if (unlikely(err))
+		get_task_pids(UNIXCB(skb).pid, current);
+		err = pidfs_register_pids(UNIXCB(skb).pid);
+		if (unlikely(err)) {
+			put_pids(UNIXCB(skb).pid);
 			return err;
+		}
 
-		UNIXCB(skb).pid = get_pid(pid);
 		current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
 	}
 
 	return 0;
 }
 
-static bool unix_skb_scm_eq(struct sk_buff *skb,
+static bool unix_skb_scm_eq(const struct sock *sk, struct sk_buff *skb,
 			    struct scm_cookie *scm)
 {
-	return UNIXCB(skb).pid == scm->pid &&
+	/* Only a thread pidfd receiver can tell threads of one process apart. */
+	if (sk->sk_scm_pidfd_thread &&
+	    UNIXCB(skb).pid[PIDTYPE_PID] != scm->pid[PIDTYPE_PID])
+		return false;
+
+	return UNIXCB(skb).pid[PIDTYPE_TGID] == scm->pid[PIDTYPE_TGID] &&
 	       uid_eq(UNIXCB(skb).uid, scm->creds.uid) &&
 	       gid_eq(UNIXCB(skb).gid, scm->creds.gid) &&
 	       unix_secdata_eq(scm, skb);
@@ -3026,7 +3031,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 
 		if (check_creds) {
 			/* Never glue messages from different writers */
-			if (!unix_skb_scm_eq(skb, &scm))
+			if (!unix_skb_scm_eq(sk, skb, &scm))
 				break;
 		} else if (unix_may_passcred(sk)) {
 			/* Copy credentials */
diff --git a/net/unix/af_unix.h b/net/unix/af_unix.h
index 8119dbeef3a3c..402742895acca 100644
--- a/net/unix/af_unix.h
+++ b/net/unix/af_unix.h
@@ -2,6 +2,7 @@
 #ifndef __AF_UNIX_H
 #define __AF_UNIX_H
 
+#include <linux/pid_types.h>
 #include <linux/uidgid.h>
 
 #define UNIX_HASH_MOD	(256 - 1)
@@ -11,7 +12,7 @@
 struct sock *unix_peer_get(struct sock *sk);
 
 struct unix_skb_parms {
-	struct pid		*pid;		/* skb credentials	*/
+	DECLARE_PIDS(pid, PIDTYPE_TGID);	/* skb credentials by pid type */
 	kuid_t			uid;
 	kgid_t			gid;
 	struct scm_fp_list	*fp;		/* Passed files		*/
diff --git a/tools/testing/selftests/coredump/coredump_socket_test.c b/tools/testing/selftests/coredump/coredump_socket_test.c
index 422728f632ca6..ec73bb690bbcb 100644
--- a/tools/testing/selftests/coredump/coredump_socket_test.c
+++ b/tools/testing/selftests/coredump/coredump_socket_test.c
@@ -592,6 +592,181 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)
 	wait_and_check_coredump_server(pid_coredump_server, _metadata, self);
 }
 
+static bool check_coredump_info(const struct pidfd_info *info, const char *what)
+{
+	if (!(info->mask & PIDFD_INFO_COREDUMP)) {
+		fprintf(stderr, "%s: PIDFD_INFO_COREDUMP not set in mask\n", what);
+		return false;
+	}
+
+	if (!(info->coredump_mask & PIDFD_COREDUMPED)) {
+		fprintf(stderr, "%s: PIDFD_COREDUMPED not set in coredump_mask\n", what);
+		return false;
+	}
+
+	if (!(info->mask & PIDFD_INFO_COREDUMP_SIGNAL) || info->coredump_signal != SIGSEGV) {
+		fprintf(stderr, "%s: coredump_signal=%d, expected SIGSEGV=%d\n",
+			what, info->coredump_signal, SIGSEGV);
+		return false;
+	}
+
+	if (!(info->mask & PIDFD_INFO_COREDUMP_CODE) || info->coredump_code != SEGV_MAPERR) {
+		fprintf(stderr, "%s: coredump_code=%d, expected SEGV_MAPERR=%d\n",
+			what, info->coredump_code, SEGV_MAPERR);
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * Test: PIDFD_INFO_COREDUMP on the dumping thread's pidfd
+ *
+ * Crash from a non-leader thread and verify that the pidfd from
+ * SO_PEERPIDFD_THREAD refers to that thread and reports the coredump
+ * like the thread-group leader's pidfd from SO_PEERPIDFD does.
+ */
+TEST_F(coredump, socket_coredump_thread)
+{
+	int pidfd, ret, status;
+	pid_t pid, pid_coredump_server;
+	struct pidfd_info info = {};
+	int ipc_sockets[2];
+	char c;
+
+	ASSERT_TRUE(set_core_pattern("@/tmp/coredump.socket"));
+
+	ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
+	ASSERT_EQ(ret, 0);
+
+	pid_coredump_server = fork();
+	ASSERT_GE(pid_coredump_server, 0);
+	if (pid_coredump_server == 0) {
+		int fd_server = -1, fd_coredump = -1, fd_peer_pidfd = -1;
+		int fd_thread_pidfd = -1, fd_core_file = -1;
+		struct pidfd_info thread_info = {};
+		int exit_code = EXIT_FAILURE;
+
+		close(ipc_sockets[0]);
+
+		fd_server = create_and_listen_unix_socket("/tmp/coredump.socket");
+		if (fd_server < 0) {
+			fprintf(stderr, "socket_coredump_thread: listen socket failed: %m\n");
+			goto out;
+		}
+
+		if (write_nointr(ipc_sockets[1], "1", 1) < 0) {
+			fprintf(stderr, "socket_coredump_thread: ipc write failed: %m\n");
+			goto out;
+		}
+
+		close(ipc_sockets[1]);
+
+		fd_coredump = accept4(fd_server, NULL, NULL, SOCK_CLOEXEC);
+		if (fd_coredump < 0) {
+			fprintf(stderr, "socket_coredump_thread: accept4 failed: %m\n");
+			goto out;
+		}
+
+		fd_peer_pidfd = get_peer_pidfd(fd_coredump);
+		if (fd_peer_pidfd < 0) {
+			fprintf(stderr, "socket_coredump_thread: get_peer_pidfd failed\n");
+			goto out;
+		}
+
+		fd_thread_pidfd = get_peer_pidfd_thread(fd_coredump);
+		if (fd_thread_pidfd < 0) {
+			fprintf(stderr, "socket_coredump_thread: get_peer_pidfd_thread failed\n");
+			goto out;
+		}
+
+		if (!get_pidfd_info(fd_peer_pidfd, &info) ||
+		    !get_pidfd_info(fd_thread_pidfd, &thread_info)) {
+			fprintf(stderr, "socket_coredump_thread: get_pidfd_info failed\n");
+			goto out;
+		}
+
+		/* The peer is the thread-group leader, the dumping thread is not. */
+		if (info.pid != info.tgid || thread_info.tgid != info.tgid ||
+		    thread_info.pid == thread_info.tgid) {
+			fprintf(stderr, "socket_coredump_thread: unexpected ids %d/%d and %d/%d\n",
+				info.pid, info.tgid, thread_info.pid, thread_info.tgid);
+			goto out;
+		}
+
+		if (!check_coredump_info(&info, "SO_PEERPIDFD") ||
+		    !check_coredump_info(&thread_info, "SO_PEERPIDFD_THREAD"))
+			goto out;
+
+		fd_core_file = open_coredump_tmpfile(self->fd_tmpfs_detached);
+		if (fd_core_file < 0) {
+			fprintf(stderr, "socket_coredump_thread: core tmpfile failed: %m\n");
+			goto out;
+		}
+
+		for (;;) {
+			char buffer[4096];
+			ssize_t bytes_read, bytes_write;
+
+			bytes_read = read(fd_coredump, buffer, sizeof(buffer));
+			if (bytes_read < 0) {
+				fprintf(stderr, "socket_coredump_thread: core read failed: %m\n");
+				goto out;
+			}
+
+			if (bytes_read == 0)
+				break;
+
+			bytes_write = write(fd_core_file, buffer, bytes_read);
+			if (bytes_read != bytes_write) {
+				fprintf(stderr, "socket_coredump_thread: core write %zd/%zd: %m\n",
+					bytes_read, bytes_write);
+				goto out;
+			}
+		}
+
+		exit_code = EXIT_SUCCESS;
+		fprintf(stderr, "socket_coredump_thread: completed successfully\n");
+out:
+		if (fd_core_file >= 0)
+			close(fd_core_file);
+		if (fd_thread_pidfd >= 0)
+			close(fd_thread_pidfd);
+		if (fd_peer_pidfd >= 0)
+			close(fd_peer_pidfd);
+		if (fd_coredump >= 0)
+			close(fd_coredump);
+		if (fd_server >= 0)
+			close(fd_server);
+		_exit(exit_code);
+	}
+	self->pid_coredump_server = pid_coredump_server;
+
+	EXPECT_EQ(close(ipc_sockets[1]), 0);
+	ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
+	EXPECT_EQ(close(ipc_sockets[0]), 0);
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+	if (pid == 0)
+		crashing_child_thread();
+
+	pidfd = sys_pidfd_open(pid, 0);
+	ASSERT_GE(pidfd, 0);
+
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFSIGNALED(status));
+	ASSERT_EQ(WTERMSIG(status), SIGSEGV);
+	ASSERT_TRUE(WCOREDUMP(status));
+
+	ASSERT_TRUE(get_pidfd_info(pidfd, &info));
+	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_COREDUMP));
+	ASSERT_TRUE(!!(info.coredump_mask & PIDFD_COREDUMPED));
+	ASSERT_EQ(info.coredump_signal, SIGSEGV);
+
+	wait_and_check_coredump_server(pid_coredump_server, _metadata, self);
+}
+
 /*
  * Test: PIDFD_INFO_COREDUMP_SIGNAL via simple socket coredump with SIGABRT
  *
diff --git a/tools/testing/selftests/coredump/coredump_test.h b/tools/testing/selftests/coredump/coredump_test.h
index ed47f01fa53c5..4212656e31f01 100644
--- a/tools/testing/selftests/coredump/coredump_test.h
+++ b/tools/testing/selftests/coredump/coredump_test.h
@@ -27,10 +27,12 @@ FIXTURE(coredump)
 /* Shared helper function declarations */
 void *do_nothing(void *arg);
 void crashing_child(void);
+void crashing_child_thread(void);
 int create_detached_tmpfs(void);
 int create_and_listen_unix_socket(const char *path);
 bool set_core_pattern(const char *pattern);
 int get_peer_pidfd(int fd);
+int get_peer_pidfd_thread(int fd);
 bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info);
 
 /* Inline helper that uses harness types */
diff --git a/tools/testing/selftests/coredump/coredump_test_helpers.c b/tools/testing/selftests/coredump/coredump_test_helpers.c
index 2a20faf9cb0ad..36306069f62e5 100644
--- a/tools/testing/selftests/coredump/coredump_test_helpers.c
+++ b/tools/testing/selftests/coredump/coredump_test_helpers.c
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <sys/epoll.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
@@ -38,6 +39,10 @@ struct _fixture_coredump_data {
 
 #define NUM_THREAD_SPAWN 128
 
+#ifndef SO_PEERPIDFD_THREAD
+#define SO_PEERPIDFD_THREAD 87
+#endif
+
 void *do_nothing(void *arg)
 {
 	(void)arg;
@@ -59,6 +64,36 @@ void crashing_child(void)
 	i = *(volatile int *)NULL;
 }
 
+static void *crashing_thread(void *arg)
+{
+	int *p;
+
+	(void)arg;
+
+	/* crash on purpose with SEGV_MAPERR */
+	p = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
+		 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (p == MAP_FAILED)
+		return NULL;
+	munmap(p, PAGE_SIZE);
+	*p = 0;
+
+	return NULL;
+}
+
+void crashing_child_thread(void)
+{
+	pthread_t thread;
+	int i;
+
+	for (i = 0; i < NUM_THREAD_SPAWN; ++i)
+		pthread_create(&thread, NULL, do_nothing, NULL);
+
+	/* crash from a non-leader thread */
+	pthread_create(&thread, NULL, crashing_thread, NULL);
+	pause();
+}
+
 int create_detached_tmpfs(void)
 {
 	int fd_context, fd_tmpfs;
@@ -138,6 +173,20 @@ int get_peer_pidfd(int fd)
 	return fd_peer_pidfd;
 }
 
+int get_peer_pidfd_thread(int fd)
+{
+	int fd_peer_pidfd;
+	socklen_t fd_peer_pidfd_len = sizeof(fd_peer_pidfd);
+	int ret = getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD_THREAD, &fd_peer_pidfd,
+			     &fd_peer_pidfd_len);
+	if (ret < 0) {
+		fprintf(stderr, "%s: getsockopt(SO_PEERPIDFD_THREAD) failed: %m\n", __func__);
+		return -1;
+	}
+	fprintf(stderr, "%s: successfully retrieved pidfd %d\n", __func__, fd_peer_pidfd);
+	return fd_peer_pidfd;
+}
+
 bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info)
 {
 	int ret;
diff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile
index a66f10fb0c231..45b841758f1bf 100644
--- a/tools/testing/selftests/net/af_unix/Makefile
+++ b/tools/testing/selftests/net/af_unix/Makefile
@@ -23,6 +23,8 @@ TEST_GEN_FILES := scm_rights_denial_lsm.bpf.o
 include ../../lib.mk
 include ../bpf.mk
 
+$(OUTPUT)/scm_pidfd: CFLAGS += -pthread
+
 $(OUTPUT)/scm_rights_denial_lsm: $(BPFOBJ)
 $(OUTPUT)/scm_rights_denial_lsm: CFLAGS += -I$(SCRATCH_DIR)/include
 $(OUTPUT)/scm_rights_denial_lsm: LDLIBS += -lelf -lz
diff --git a/tools/testing/selftests/net/af_unix/scm_pidfd.c b/tools/testing/selftests/net/af_unix/scm_pidfd.c
index 2c18b92a26035..bbbd5e7b4fa78 100644
--- a/tools/testing/selftests/net/af_unix/scm_pidfd.c
+++ b/tools/testing/selftests/net/af_unix/scm_pidfd.c
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
+#include <pthread.h>
 #include <sys/un.h>
 #include <sys/signal.h>
 #include <sys/types.h>
@@ -27,6 +28,14 @@
 #define SCM_PIDFD 0x04
 #endif
 
+#ifndef SO_PASSPIDFD_THREAD
+#define SO_PASSPIDFD_THREAD 86
+#endif
+
+#ifndef SO_PEERPIDFD_THREAD
+#define SO_PEERPIDFD_THREAD 87
+#endif
+
 #define CHILD_EXIT_CODE_OK 123
 
 static void child_die()
@@ -553,4 +562,361 @@ TEST_F(scm_pidfd, test)
 	close(pfd);
 }
 
+struct thread_ids {
+	pid_t pid;
+	pid_t tid;
+};
+
+static void *send_ids_thread(void *arg)
+{
+	int fd = *(int *)arg;
+	struct thread_ids ids = {
+		.pid = getpid(),
+		.tid = gettid(),
+	};
+	char sync;
+
+	if (send(fd, &ids, sizeof(ids), 0) != sizeof(ids))
+		return (void *)1;
+
+	/* stay alive until the receiver has looked at our pidfd */
+	if (read(fd, &sync, 1) != 1)
+		return (void *)1;
+
+	return NULL;
+}
+
+static void *send_ids_creds_thread(void *arg)
+{
+	int fd = *(int *)arg;
+	struct thread_ids ids = {
+		.pid = getpid(),
+		.tid = gettid(),
+	};
+	struct ucred ucred = {
+		.pid = getpid(),
+		.uid = getuid(),
+		.gid = getgid(),
+	};
+	char control[CMSG_SPACE(sizeof(ucred))] = { 0 };
+	struct iovec iov;
+	struct msghdr msg = { 0 };
+	struct cmsghdr *cmsg;
+	char sync;
+
+	iov.iov_base = &ids;
+	iov.iov_len = sizeof(ids);
+
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = control;
+	msg.msg_controllen = sizeof(control);
+
+	cmsg = CMSG_FIRSTHDR(&msg);
+	cmsg->cmsg_level = SOL_SOCKET;
+	cmsg->cmsg_type = SCM_CREDENTIALS;
+	cmsg->cmsg_len = CMSG_LEN(sizeof(ucred));
+	memcpy(CMSG_DATA(cmsg), &ucred, sizeof(ucred));
+
+	if (sendmsg(fd, &msg, 0) != sizeof(ids))
+		return (void *)1;
+
+	if (read(fd, &sync, 1) != 1)
+		return (void *)1;
+
+	return NULL;
+}
+
+static void thread_client(int fd, int syncfd, void *(*sender)(void *))
+{
+	pthread_t thread;
+	void *ret;
+	char sync;
+
+	/* wait until the receiver enabled SO_PASSPIDFD */
+	if (read(syncfd, &sync, 1) != 1)
+		child_die();
+
+	if (pthread_create(&thread, NULL, sender, &fd))
+		child_die();
+
+	if (pthread_join(thread, &ret) || ret)
+		child_die();
+
+	exit(0);
+}
+
+static int recv_pidfd(int fd, struct thread_ids *ids)
+{
+	char control[CMSG_SPACE(sizeof(int))] = { 0 };
+	struct iovec iov;
+	struct msghdr msg = { 0 };
+	struct cmsghdr *cmsg;
+	int pidfd = -1;
+
+	iov.iov_base = ids;
+	iov.iov_len = sizeof(*ids);
+
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = control;
+	msg.msg_controllen = sizeof(control);
+
+	if (recvmsg(fd, &msg, 0) != sizeof(*ids)) {
+		log_err("recvmsg");
+		return -1;
+	}
+
+	if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
+		log_err("recvmsg: truncated");
+		return -1;
+	}
+
+	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+		if (cmsg->cmsg_level == SOL_SOCKET &&
+		    cmsg->cmsg_type == SCM_PIDFD)
+			memcpy(&pidfd, CMSG_DATA(cmsg), sizeof(pidfd));
+	}
+
+	return pidfd;
+}
+
+static int thread_pidfd_flow(void *(*sender)(void *), int optname,
+			     struct thread_ids *ids, struct pidfd_info *info)
+{
+	int sk[2];
+	int syncpipe[2];
+	int pidfd;
+	int child_status = 0;
+	pid_t child;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sk))
+		return -1;
+
+	if (pipe(syncpipe))
+		return -1;
+
+	child = fork();
+	if (child < 0)
+		return -1;
+
+	if (child == 0) {
+		close(sk[0]);
+		close(syncpipe[1]);
+		thread_client(sk[1], syncpipe[0], sender);
+	}
+	close(sk[1]);
+	close(syncpipe[0]);
+
+	int on = 1;
+
+	if (setsockopt(sk[0], SOL_SOCKET, optname, &on, sizeof(on))) {
+		log_err("Failed to set pidfd passing option");
+		return -1;
+	}
+
+	if (write(syncpipe[1], "1", 1) != 1)
+		return -1;
+	close(syncpipe[1]);
+
+	pidfd = recv_pidfd(sk[0], ids);
+	if (pidfd < 0)
+		return -1;
+
+	info->mask = PIDFD_INFO_PID;
+	if (ioctl(pidfd, PIDFD_GET_INFO, info)) {
+		log_err("ioctl(PIDFD_GET_INFO)");
+		close(pidfd);
+		return -1;
+	}
+	close(pidfd);
+
+	/* release the sending thread */
+	if (write(sk[0], "x", 1) != 1)
+		return -1;
+	close(sk[0]);
+
+	waitpid(child, &child_status, 0);
+	if (!WIFEXITED(child_status) || WEXITSTATUS(child_status))
+		return -1;
+
+	return 0;
+}
+
+static int sockopt_set(int fd, int optname, int val)
+{
+	return setsockopt(fd, SOL_SOCKET, optname, &val, sizeof(val));
+}
+
+static int sockopt_get(int fd, int optname)
+{
+	socklen_t len = sizeof(int);
+	int val = -1;
+
+	if (getsockopt(fd, SOL_SOCKET, optname, &val, &len))
+		return -1;
+
+	return val;
+}
+
+TEST(scm_pidfd_setsockopt_values)
+{
+	int sk[2];
+
+	ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sk));
+
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 1));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));
+
+	/* The option set last wins. */
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD, 1));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 1));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));
+
+	/* Disabling one option leaves the other alone. */
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD, 0));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 0));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+
+	close(sk[0]);
+	close(sk[1]);
+}
+
+TEST(scm_pidfd_thread)
+{
+	struct thread_ids ids;
+	struct pidfd_info info;
+
+	ASSERT_EQ(0, thread_pidfd_flow(send_ids_thread, SO_PASSPIDFD_THREAD,
+				       &ids, &info));
+	ASSERT_NE(ids.pid, ids.tid);
+	EXPECT_EQ(ids.tid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+}
+
+TEST(scm_pidfd_thread_group)
+{
+	struct thread_ids ids;
+	struct pidfd_info info;
+
+	ASSERT_EQ(0, thread_pidfd_flow(send_ids_thread, SO_PASSPIDFD,
+				       &ids, &info));
+	ASSERT_NE(ids.pid, ids.tid);
+	EXPECT_EQ(ids.pid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+}
+
+TEST(scm_pidfd_thread_creds)
+{
+	struct thread_ids ids;
+	struct pidfd_info info;
+
+	ASSERT_EQ(0, thread_pidfd_flow(send_ids_creds_thread, SO_PASSPIDFD_THREAD,
+				       &ids, &info));
+	ASSERT_NE(ids.pid, ids.tid);
+	EXPECT_EQ(ids.tid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+}
+
+static void *peer_connect_thread(void *arg)
+{
+	struct sock_addr *sa = arg;
+	struct thread_ids ids = {
+		.pid = getpid(),
+		.tid = gettid(),
+	};
+	int fd;
+	char sync;
+
+	fd = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (fd < 0)
+		return (void *)1;
+
+	if (connect(fd, (struct sockaddr *)&sa->listen_addr, sa->addrlen))
+		return (void *)1;
+
+	if (send(fd, &ids, sizeof(ids), 0) != sizeof(ids))
+		return (void *)1;
+
+	/* stay alive until the server has looked at our pidfd */
+	if (read(fd, &sync, 1) != 1)
+		return (void *)1;
+
+	close(fd);
+	return NULL;
+}
+
+static int peer_pidfd_info(int fd, int optname, struct pidfd_info *info)
+{
+	int pidfd;
+	socklen_t len = sizeof(pidfd);
+
+	if (getsockopt(fd, SOL_SOCKET, optname, &pidfd, &len)) {
+		log_err("getsockopt(SO_PEERPIDFD*)");
+		return -1;
+	}
+
+	info->mask = PIDFD_INFO_PID;
+	if (ioctl(pidfd, PIDFD_GET_INFO, info)) {
+		log_err("ioctl(PIDFD_GET_INFO)");
+		close(pidfd);
+		return -1;
+	}
+
+	close(pidfd);
+	return 0;
+}
+
+/* SO_PEERPIDFD_THREAD returns a pidfd for the peer's connecting thread. */
+TEST(so_peerpidfd_thread)
+{
+	struct sock_addr sa;
+	struct thread_ids ids;
+	struct pidfd_info info;
+	pthread_t thread;
+	void *tret;
+	int server, cfd;
+
+	server = socket(AF_UNIX, SOCK_STREAM, 0);
+	ASSERT_LE(0, server);
+
+	fill_sockaddr(&sa, true);
+	ASSERT_EQ(0, bind(server, (struct sockaddr *)&sa.listen_addr, sa.addrlen));
+	ASSERT_EQ(0, listen(server, 1));
+
+	ASSERT_EQ(0, pthread_create(&thread, NULL, peer_connect_thread, &sa));
+
+	cfd = accept(server, NULL, NULL);
+	ASSERT_LE(0, cfd);
+
+	ASSERT_EQ(sizeof(ids), recv(cfd, &ids, sizeof(ids), MSG_WAITALL));
+	ASSERT_NE(ids.pid, ids.tid);
+
+	/* SO_PEERPIDFD refers to the peer's thread-group. */
+	ASSERT_EQ(0, peer_pidfd_info(cfd, SO_PEERPIDFD, &info));
+	EXPECT_EQ(ids.pid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+
+	/* SO_PEERPIDFD_THREAD refers to the connecting thread. */
+	ASSERT_EQ(0, peer_pidfd_info(cfd, SO_PEERPIDFD_THREAD, &info));
+	EXPECT_EQ(ids.tid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+
+	/* release the connecting thread */
+	ASSERT_EQ(1, write(cfd, "x", 1));
+	ASSERT_EQ(0, pthread_join(thread, &tret));
+	ASSERT_EQ(NULL, tret);
+
+	close(cfd);
+	close(server);
+}
+
 TEST_HARNESS_MAIN

3/2 2026/08/31 12:30 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 22866
							output: 22
							thoughts: 963
						

Thoughts:
**Fuzzing Target Identification and Analysis**

Okay, let's break this down. My initial impression is that this patch is definitely worth fuzzing. We're looking at functional changes within the core kernel networking stack, unix sockets, pidfs, and coredump mechanisms. The introduction of new socket options, `SO_PASSPIDFD_THREAD` and `SO_PEERPIDFD_THREAD`, is a clear indication that new code paths are being opened, and that’s always a good sign for fuzzing. The fact that it's touching `scm_pidfd_recv`, `__pidfs_register_pids`, `sk_getsockopt_peerpidfd`, `unix_stream_read_generic` (through `unix_skb_scm_eq`), SCM credential handling, and coredump logic across several subsystems makes it a juicy target. These are all critical areas, and subtle errors here can lead to significant vulnerabilities.

Now, the next step is to nail down the specific focus points. I need to identify the key functions that are modified, as well as the relevant Kconfig options. This will help me narrow down the fuzzing scope and tailor the fuzzing campaigns for maximum effectiveness. I'll likely need to use `grepper` or just manually go through the code to identify functions. We have a lot of areas where pidfds are being used so let's start with those.



4/2 2026/08/31 12:30 tool grepper
0m Args:
{
  "Expression": "__pidfs_register_pids"
}

Results:
{
  "Output": "fs/pidfs.c=1045=int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)\n--\nfs/pidfs.c-1080-/* Register the pids up to pid type @last of @pids in pidfs. */\nfs/pidfs.c:1081:int __pidfs_register_pids(struct pid *const *pids, enum pid_type last)\nfs/pidfs.c-1082-{\n--\ninclude/linux/pidfs.h=35=void pidfs_free_pid(struct pid *pid);\ninclude/linux/pidfs.h:36:int __pidfs_register_pids(struct pid *const *pids, enum pid_type last);\ninclude/linux/pidfs.h-37-\ninclude/linux/pidfs.h-38-/* Register the pids of an array declared with DECLARE_PIDS(). */\ninclude/linux/pidfs.h:39:#define pidfs_register_pids(pids)\t__pidfs_register_pids(pids, pids_last(pids))\ninclude/linux/pidfs.h-40-\n"
}

5/2 2026/08/31 12:30 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 24451
							output: 19
							thoughts: 20
						

Thoughts:
Here's my summary, as though I were thinking through the process:

**Diving into the `pidfs_coredump` Function**

Okay, time to get my hands dirty. I'm going to start by taking a look at the `pidfs_coredump` function. It's located in `fs/pidfs.c`, which makes sense; this is the logical place to find core dump-related functionality within the pidfs framework. I need to understand how it handles core dumps, especially in relation to process IDs. I'm hoping to get a clearer picture of how this function interacts with the kernel's core dumping mechanisms and what specific roles pidfs plays in the overall flow. I'll need to pay close attention to the data structures used, the error handling, and any potential security considerations. This should be an interesting exploration into how the kernel orchestrates core dumps within a PID filesystem context. I expect to see some interesting interactions with process states and memory mapping.



6/2 2026/08/31 12:30 tool grepper
0m Args:
{
  "Expression": "pidfs_coredump"
}

Results:
{
  "Output": "fs/coredump.c=623=static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)\n--\nfs/coredump.c-639-\nfs/coredump.c:640:\t\tpidfs_coredump(cp);\nfs/coredump.c-641-\n--\nfs/coredump.c=672=static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *cprm)\n--\nfs/coredump.c-714-\t */\nfs/coredump.c:715:\tpidfs_coredump(cprm);\nfs/coredump.c-716-\n--\nfs/pidfs.c=329=static inline bool pid_in_current_pidns(const struct pid *pid)\n--\nfs/pidfs.c-338-\nfs/pidfs.c:339:static __u32 pidfs_coredump_mask(enum task_dumpable dumpable)\nfs/pidfs.c-340-{\n--\nfs/pidfs.c=365=static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)\n--\nfs/pidfs.c-433-\tif ((mask \u0026 PIDFD_INFO_COREDUMP) \u0026\u0026 !kinfo.coredump_mask) {\nfs/pidfs.c:434:\t\tkinfo.coredump_mask = pidfs_coredump_mask(task_exec_state_get_dumpable(task));\nfs/pidfs.c-435-\t\tkinfo.mask |= PIDFD_INFO_COREDUMP;\n--\nfs/pidfs.c=749=void pidfs_exit(struct task_struct *tsk)\n--\nfs/pidfs.c-795-#ifdef CONFIG_COREDUMP\nfs/pidfs.c:796:static void pidfs_coredump_pid(struct pid *pid,\nfs/pidfs.c-797-\t\t\t       const struct coredump_params *cprm)\n--\nfs/pidfs.c-806-\t/* Note how we were coredumped and that we coredumped. */\nfs/pidfs.c:807:\tattr-\u003ecoredump_mask = pidfs_coredump_mask(cprm-\u003edumpable) |\nfs/pidfs.c-808-\t\t\t      PIDFD_COREDUMPED;\n--\nfs/pidfs.c-817-\nfs/pidfs.c:818:void pidfs_coredump(const struct coredump_params *cprm)\nfs/pidfs.c-819-{\n--\nfs/pidfs.c-821-\tfor (enum pid_type type = PIDTYPE_PID; type \u003c= pids_last(cprm-\u003epid); type++)\nfs/pidfs.c:822:\t\tpidfs_coredump_pid(cprm-\u003epid[type], cprm);\nfs/pidfs.c-823-}\n--\ninclude/linux/pidfs.h=15=void pidfs_exit(struct task_struct *tsk);\ninclude/linux/pidfs.h-16-#ifdef CONFIG_COREDUMP\ninclude/linux/pidfs.h:17:void pidfs_coredump(const struct coredump_params *cprm);\ninclude/linux/pidfs.h-18-#endif\n"
}

7/2 2026/08/31 12:31 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 25535
							output: 21
							thoughts: 329
						

Thoughts:
**My Initial Code Inspection: Socket-Related Changes**

Okay, so I need to understand these code modifications better. My initial thought is to dive into the core networking code, specifically examining how sockets are interacting with the changes. I'm going to start with a systematic review, hitting the key files: `net/core/scm.c`, `net/unix/af_unix.c`, and `net/core/sock.c`. These seem like the most likely candidates for relevant changes. I'll also be sure to double-check their symbols to confirm my assumption, and to note any non-static functions as those are more likely to be entry points.

Next, I need to understand what's been *modified*. I can see a couple of functions from `fs/pidfs.c` - `__pidfs_register_pids` (exported) and `pidfs_coredump` (non-static) - that seem relevant to process ID handling during a core dump. Then, I can see changes in `net/core/scm.c`, where `scm_recv_unix` calls `scm_pidfd_recv` and `__scm_send` is present. It seems like the System Control Message (SCM) path has some updates. `net/core/sock.c` has `sk_setsockopt` and `sk_getsockopt` - socket options seem to be affected, which could be connected. In `net/unix/af_unix.c`, there's `unix_stream_read_generic` (static, but called by non-static functions like `unix_stream_recvmsg` and `unix_dgram_recvmsg`) along with `unix_listen`, `unix_stream_connect`, etc. - basic Unix domain socket functions, also likely to be pertinent. And finally, `fs/coredump.c`, where `do_coredump` now calls functions like `coredump_sock_connect` and `umh_coredump_setup`, which again points to socket interaction during coredumps.

So, overall, this looks like a multifaceted change, likely related to how sockets, PID files, and coredumps interact. There seems to be some refactoring going on, along with more calls to `pidfd_recv` within the SCM path. I should also note any relationships between these modifications, and see how they are interlinked. I also need to make sure to double-check the symbols in `net/core/scm.c` and `net/unix/af_unix.c` to confirm my understanding of the non-static functions and their potential entry points. This will give me a clear picture of the modified code pathways.



8/2 2026/08/31 12:31 tool grepper
0m Args:
{
  "Expression": "void scm_recv_unix"
}

Results:
{
  "Output": "include/net/scm.h=104=void scm_recv(struct socket *sock, struct msghdr *msg,\ninclude/net/scm.h-105-\t      struct scm_cookie *scm, int flags);\ninclude/net/scm.h:106:void scm_recv_unix(struct socket *sock, struct msghdr *msg,\ninclude/net/scm.h-107-\t\t   struct scm_cookie *scm, int flags);\n--\nnet/core/scm.c=581=EXPORT_SYMBOL(scm_recv);\nnet/core/scm.c-582-\nnet/core/scm.c:583:void scm_recv_unix(struct socket *sock, struct msghdr *msg,\nnet/core/scm.c-584-\t\t   struct scm_cookie *scm, int flags)\n"
}

9/2 2026/08/31 12:32 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 26682
							output: 19
							thoughts: 13
						

Thoughts:
Here's the summary, assuming I'm an expert and this is part of a debugging/analysis process:

**Diving Deeper: Exploring `__scm_send`**

Okay, I've just been looking at some related functions and behaviors, and before I move on to anything else, I need to circle back and double-check `__scm_send`. I'm already pretty familiar with the system-level communication, but I have a nagging feeling that a potential issue might be lurking within that particular function. I need to make absolutely sure that it's handling the data flow correctly, that the error checking is robust enough, and that there aren't any subtle race conditions or memory issues that could be triggered under specific circumstances. It's a key piece of the puzzle, so I need to make sure I'm not missing anything. I'll take a close look at its interaction with the other components I've reviewed.



10/2 2026/08/31 12:32 tool grepper
0m Args:
{
  "Expression": "int __scm_send"
}

Results:
{
  "Output": "include/net/scm.h=54=void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm, bool notrunc);\ninclude/net/scm.h:55:int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);\ninclude/net/scm.h-56-void __scm_destroy(struct scm_cookie *scm);\n--\nnet/core/scm.c=150=static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)\n--\nnet/core/scm.c-177-\nnet/core/scm.c:178:int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)\nnet/core/scm.c-179-{\n"
}

11/2 2026/08/31 12:32 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 27134
							output: 20
							thoughts: 80
						

Thoughts:
Okay, let's dive into this.

**Investigating Socket Option Management in the Linux Kernel**

Right, I need to get to the bottom of how these socket options are handled. I'm going to start by examining the `sk_setsockopt` and `sk_getsockopt` functions. My instincts tell me those are the key functions to trace. I'll head over to `net/core/sock.c` - that seems like the most logical place to begin. Wait a second, are `sk_setsockopt` and `sk_getsockopt` *not* declared as static? That throws a slight wrench into my immediate mental model. I'll need to confirm that. Okay, to make sure, I'll double check the declarations. Time to check `include/net/sock.h` or potentially `net/core/sock.c` to see how those are set up. Need to make sure I have the full picture before I start digging in!



12/2 2026/08/31 12:32 tool grepper
0m Args:
{
  "Expression": "int sk_getsockopt"
}

Results:
{
  "Output": "include/net/sock.h=1894=int do_sock_getsockopt(struct socket *sock, bool compat, int level,\n--\ninclude/net/sock.h-1896-\ninclude/net/sock.h:1897:int sk_getsockopt(struct sock *sk, int level, int optname,\ninclude/net/sock.h-1898-\t\t  sockptr_t optval, sockptr_t optlen);\n--\nnet/core/sock.c=1731=static int groups_to_user(sockptr_t dst, const struct group_info *src)\n--\nnet/core/sock.c-1746-/* Hand out a pidfd for @type of the socket's peer via SO_PEERPIDFD*. */\nnet/core/sock.c:1747:static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type,\nnet/core/sock.c-1748-\t\t\t\t   sockptr_t optval, sockptr_t optlen, int len)\n--\nnet/core/sock.c-1789-\nnet/core/sock.c:1790:int sk_getsockopt(struct sock *sk, int level, int optname,\nnet/core/sock.c-1791-\t\t  sockptr_t optval, sockptr_t optlen)\n"
}

13/2 2026/08/31 12:32 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 27796
							output: 20
							thoughts: 21
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Diving into `sk_setsockopt`**

Alright, let's get down to it. I need to understand the ins and outs of socket option setting at the core level. Specifically, I'm thinking about `sk_setsockopt` within `net/core/sock.c`. That's where I expect to find the fundamental socket option management logic, the heart of how these options get handled before they ripple out to the appropriate protocol-specific handlers. I'm hoping to see how the option data is validated, where the option gets dispatched based on its level and name, and, of course, how the kernel handles potential errors. I'll need to pay close attention to the data structures involved, like `sock` itself and any associated option structures, to get a clear picture of what's happening. My goal here is to establish a solid grasp of this critical area.



14/2 2026/08/31 12:32 tool grepper
0m Args:
{
  "Expression": "int sk_setsockopt"
}

Results:
{
  "Output": "include/net/sock.h=1876=static inline void skb_set_owner_edemux(struct sk_buff *skb, struct sock *sk)\n--\ninclude/net/sock.h-1887-\ninclude/net/sock.h:1888:int sk_setsockopt(struct sock *sk, int level, int optname,\ninclude/net/sock.h-1889-\t\t  sockptr_t optval, unsigned int optlen);\n--\nnet/core/sock.c=1178=static int sockopt_validate_clockid(__kernel_clockid_t value)\n--\nnet/core/sock.c-1193-\nnet/core/sock.c:1194:int sk_setsockopt(struct sock *sk, int level, int optname,\nnet/core/sock.c-1195-\t\t  sockptr_t optval, unsigned int optlen)\n"
}

15/2 2026/08/31 12:32 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 28283
							output: 42
							thoughts: 71
						

Thoughts:
**Examining Unix Socket Credential Handling**

Okay, let's dive in. My immediate thought is to zero in on `net/unix/af_unix.c`. That's where I expect to find the functions involved in Unix domain socket credential management. I need to understand what's been changed or what might be relevant to my area of expertise.

Specifically, I need to trace the usage of functions like `prepare_peercred`, `update_peercred`, `copy_peercred`, and `unix_maybe_add_creds`. Understanding the call graph for these will give me a solid grasp on how credentials are handled within the context of Unix sockets and any potential vulnerabilities or improvements in the recent code changes.



16/2 2026/08/31 12:32 tool grepper
0m Args:
{
  "Expression": "prepare_peercred|unix_maybe_add_creds",
  "PathPrefix": "net/unix/af_unix.c"
}

Results:
{
  "Output": "net/unix/af_unix.c=739=struct unix_peercred {\n--\nnet/unix/af_unix.c-743-\nnet/unix/af_unix.c:744:static inline int prepare_peercred(struct unix_peercred *peercred)\nnet/unix/af_unix.c-745-{\n--\nnet/unix/af_unix.c=805=static int unix_listen(struct socket *sock, int backlog)\n--\nnet/unix/af_unix.c-817-\t\tgoto out;\t/* No listens on an unbound socket */\nnet/unix/af_unix.c:818:\terr = prepare_peercred(\u0026peercred);\nnet/unix/af_unix.c-819-\tif (err)\n--\nnet/unix/af_unix.c=1631=static int unix_stream_connect(struct socket *sock, struct sockaddr_unsized *uaddr,\n--\nnet/unix/af_unix.c-1659-\nnet/unix/af_unix.c:1660:\terr = prepare_peercred(\u0026peercred);\nnet/unix/af_unix.c-1661-\tif (err)\n--\nnet/unix/af_unix.c=1811=static int unix_socketpair(struct socket *socka, struct socket *sockb)\n--\nnet/unix/af_unix.c-1816-\nnet/unix/af_unix.c:1817:\terr = prepare_peercred(\u0026ska_peercred);\nnet/unix/af_unix.c-1818-\tif (err)\n--\nnet/unix/af_unix.c-1820-\nnet/unix/af_unix.c:1821:\terr = prepare_peercred(\u0026skb_peercred);\nnet/unix/af_unix.c-1822-\tif (err) {\n--\nnet/unix/af_unix.c=2003=static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)\n--\nnet/unix/af_unix.c-2012-/**\nnet/unix/af_unix.c:2013: * unix_maybe_add_creds() - Adds current task uid/gid and struct pid to skb if needed.\nnet/unix/af_unix.c-2014- * @skb: skb to attach creds to.\n--\nnet/unix/af_unix.c-2024- */\nnet/unix/af_unix.c:2025:static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,\nnet/unix/af_unix.c-2026-\t\t\t\tconst struct sock *other)\n--\nnet/unix/af_unix.c=2095=static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/unix/af_unix.c-2189-\nnet/unix/af_unix.c:2190:\terr = unix_maybe_add_creds(skb, sk, other);\nnet/unix/af_unix.c-2191-\tif (err)\n--\nnet/unix/af_unix.c=2329=static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,\n--\nnet/unix/af_unix.c-2344-\nnet/unix/af_unix.c:2345:\terr = unix_maybe_add_creds(skb, sk, other);\nnet/unix/af_unix.c-2346-\tif (err)\n--\nnet/unix/af_unix.c=2389=static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/unix/af_unix.c-2462-\nnet/unix/af_unix.c:2463:\t\terr = unix_maybe_add_creds(skb, sk, other);\nnet/unix/af_unix.c-2464-\t\tif (err)\n"
}

17/2 2026/08/31 12:32 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 29594
							output: 37
							thoughts: 21
						

Thoughts:
Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:

**Analyzing `net/unix/af_unix.c` for Non-Static Functions**

Right, time to get my hands dirty. I need to take a look at `net/unix/af_unix.c`. Specifically, I want to zero in on the *non-static* functions within that file. I know the static ones are likely internal implementation details, scoped only to that compilation unit, but the non-static ones… *those* are the interface points. Those are the functions that the rest of the kernel, or possibly even user space, might be calling into. That's where the interesting stuff usually lives, the stuff I need to understand if I want to grasp the bigger picture of how Unix domain sockets are implemented within the kernel. I'll need to see how the socket creation, connection handling, data transfer, and cleanup are exposed. It’s all about tracing the entry points and the communication flow. So, let's start with a thorough scan of that file. Let's see what we've got.



18/2 2026/08/31 12:33 tool grepper
0m Error:
no matches

Args:
{
  "Expression": "^int unix_|^void unix_",
  "PathPrefix": "net/unix/af_unix.c"
}

Results:
{
  "error": "no matches"
}

19/2 2026/08/31 12:33 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 29910
							output: 33
							thoughts: 21
						

Thoughts:
Here's my take on those thoughts:

**Analyzing the Unix Domain Socket Implementation**

Okay, time to dive into the nitty-gritty. I need to get a handle on the internals of the Unix domain socket implementation, and specifically, I'm interested in any functions that aren't declared as `static`. I'm heading straight for `net/unix/af_unix.c`. Why? Because that's where the core functionality for this stuff typically resides. My gut tells me this will be the heart of it, but I could be wrong, of course. My experience says this is a good place to start hunting for entry points and interactions with the kernel's networking stack. The absence of `static` is a quick tell, indicating functions likely exposed for use elsewhere in the kernel or perhaps even to user-space through some clever trickery. Let's see what we can dig up. This should tell me a lot about the architecture and how things are meant to flow. I'm expecting to find the key functions for socket creation, connection handling, data transfer, and cleanup, and these functions will offer key insights for my analysis.



20/2 2026/08/31 12:33 tool grepper
0m Args:
{
  "Expression": "^EXPORT_SYMBOL",
  "PathPrefix": "net/unix/af_unix.c"
}

Results:
{
  "Output": "net/unix/af_unix.c=293=struct sock *unix_peer_get(struct sock *s)\n--\nnet/unix/af_unix.c-303-}\nnet/unix/af_unix.c:304:EXPORT_SYMBOL_GPL(unix_peer_get);\nnet/unix/af_unix.c-305-\n--\nnet/unix/af_unix.c=3254=long unix_inq_len(struct sock *sk)\n--\nnet/unix/af_unix.c-3277-}\nnet/unix/af_unix.c:3278:EXPORT_SYMBOL_GPL(unix_inq_len);\nnet/unix/af_unix.c-3279-\nnet/unix/af_unix.c=3280=long unix_outq_len(struct sock *sk)\n--\nnet/unix/af_unix.c-3283-}\nnet/unix/af_unix.c:3284:EXPORT_SYMBOL_GPL(unix_outq_len);\nnet/unix/af_unix.c-3285-\n"
}

21/2 2026/08/31 12:33 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 30493
							output: 22
							thoughts: 320
						

Thoughts:
**My Initial Thoughts on Socket Security**

Okay, let's dive in. I need to get a handle on how process IDs are managed, particularly as they relate to socket security. My mind is immediately drawn to `af_unix.c`. That's where I know the UNIX socket family is defined. Specifically, the `proto_ops` structures like `unix_stream_ops`, `unix_dgram_ops`, and `unix_seqpacket_ops` are the key entry points I need to examine for how they handle UNIX sockets.

I need to quickly recall the core functions involved in socket option setting/getting and ancillary data. I remember the critical entry points: `sk_setsockopt`, `sk_getsockopt`, `scm_recv_unix`, `__scm_send`, as well as `pidfs_coredump` and `__pidfs_register_pids` are the initial areas to target.

Now, let's switch gears and investigate the Bluetooth implementation. I'm thinking about those recent changes, and I remember `sk->sk_peer_pid[PIDTYPE_TGID]` getting touched in the Bluetooth code. Looking at `net/bluetooth/af_bluetooth.c`, I see `bt_sock_alloc` and `bt_accept_enqueue` specifically touching that field.  I'll also need to check `net/bluetooth/hci_sock.c` and `net/bluetooth/l2cap_sock.c` since I've got a feeling something's lurking there as well. Looks like `hci_sock_copy_creds` and `l2cap_sock_get_peer_pid_cb` are also responsible for updates.

Finally, I need to remind myself of the relevant Kconfig options. Obviously, `CONFIG_UNIX` controls the availability of AF_UNIX sockets. Then, `CONFIG_COREDUMP` seems to be relevant. Time to see if the `pidfs` features or any other capabilities are controlled by Kconfig options. I need to make sure I understand the conditional compilation landscape.



22/2 2026/08/31 12:33 tool grepper
0m Args:
{
  "Expression": "CONFIG_PID_FS|PIDFS"
}

Results:
{
  "Output": "fs/fhandle.c=170=static int get_path_anchor(int fd, struct path *root)\n--\nfs/fhandle.c-185-\nfs/fhandle.c:186:\tif (fd == FD_PIDFS_ROOT) {\nfs/fhandle.c-187-\t\tpidfs_get_root(root);\n--\nfs/pidfs.c-34-\nfs/pidfs.c:35:#define PIDFS_PID_DEAD ERR_PTR(-ESRCH)\nfs/pidfs.c-36-\n--\nfs/pidfs.c=49=enum pidfs_attr_mask_bits {\nfs/pidfs.c:50:\tPIDFS_ATTR_BIT_EXIT\t= 0,\nfs/pidfs.c:51:\tPIDFS_ATTR_BIT_COREDUMP\t= 1,\nfs/pidfs.c-52-};\n--\nfs/pidfs.c=365=static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)\n--\nfs/pidfs.c-395-\tif (mask \u0026 PIDFD_INFO_EXIT) {\nfs/pidfs.c:396:\t\tif (test_bit(PIDFS_ATTR_BIT_EXIT, \u0026attr-\u003eattr_mask)) {\nfs/pidfs.c-397-\t\t\tsmp_rmb();\n--\nfs/pidfs.c-407-\tif (mask \u0026 PIDFD_INFO_COREDUMP) {\nfs/pidfs.c:408:\t\tif (test_bit(PIDFS_ATTR_BIT_COREDUMP, \u0026attr-\u003eattr_mask)) {\nfs/pidfs.c-409-\t\t\tsmp_rmb();\n--\nfs/pidfs.c=749=void pidfs_exit(struct task_struct *tsk)\n--\nfs/pidfs.c-768-\t\t\t */\nfs/pidfs.c:769:\t\t\tpid-\u003eattr = PIDFS_PID_DEAD;\nfs/pidfs.c-770-\t\t\treturn;\n--\nfs/pidfs.c-791-\tsmp_wmb();\nfs/pidfs.c:792:\tset_bit(PIDFS_ATTR_BIT_EXIT, \u0026attr-\u003eattr_mask);\nfs/pidfs.c-793-}\n--\nfs/pidfs.c=796=static void pidfs_coredump_pid(struct pid *pid,\n--\nfs/pidfs.c-803-\tVFS_WARN_ON_ONCE(!attr);\nfs/pidfs.c:804:\tVFS_WARN_ON_ONCE(attr == PIDFS_PID_DEAD);\nfs/pidfs.c-805-\n--\nfs/pidfs.c-814-\tsmp_wmb();\nfs/pidfs.c:815:\tset_bit(PIDFS_ATTR_BIT_COREDUMP, \u0026attr-\u003eattr_mask);\nfs/pidfs.c-816-}\n--\nfs/pidfs.c=904=static struct pid *pidfs_ino_get_pid(u64 ino)\n--\nfs/pidfs.c-915-\t\treturn NULL;\nfs/pidfs.c:916:\tif (test_bit(PIDFS_ATTR_BIT_EXIT, \u0026attr-\u003eattr_mask))\nfs/pidfs.c-917-\t\treturn NULL;\n--\nfs/pidfs.c=1045=int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)\n--\nfs/pidfs.c-1055-\tattr = READ_ONCE(pid-\u003eattr);\nfs/pidfs.c:1056:\tif (unlikely(attr == PIDFS_PID_DEAD))\nfs/pidfs.c:1057:\t\treturn PTR_ERR(PIDFS_PID_DEAD);\nfs/pidfs.c-1058-\tif (attr)\n--\nfs/pidfs.c-1070-\tattr = pid-\u003eattr;\nfs/pidfs.c:1071:\tif (unlikely(attr == PIDFS_PID_DEAD))\nfs/pidfs.c:1072:\t\treturn PTR_ERR(PIDFS_PID_DEAD);\nfs/pidfs.c-1073-\tif (unlikely(attr))\n--\ninclude/uapi/linux/fcntl.h=88=struct delegation {\n--\ninclude/uapi/linux/fcntl.h-124-\ninclude/uapi/linux/fcntl.h:125:#define FD_PIDFS_ROOT\t\t\t-10002 /* Root of the pidfs filesystem */\ninclude/uapi/linux/fcntl.h-126-#define FD_NSFS_ROOT\t\t\t-10003 /* Root of the nsfs filesystem */\n--\ninclude/uapi/linux/pidfd.h=61=struct pidfd_info {\n--\ninclude/uapi/linux/pidfd.h-109-\ninclude/uapi/linux/pidfd.h:110:#define PIDFS_IOCTL_MAGIC 0xFF\ninclude/uapi/linux/pidfd.h-111-\ninclude/uapi/linux/pidfd.h:112:#define PIDFD_GET_CGROUP_NAMESPACE            _IO(PIDFS_IOCTL_MAGIC, 1)\ninclude/uapi/linux/pidfd.h:113:#define PIDFD_GET_IPC_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 2)\ninclude/uapi/linux/pidfd.h:114:#define PIDFD_GET_MNT_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 3)\ninclude/uapi/linux/pidfd.h:115:#define PIDFD_GET_NET_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 4)\ninclude/uapi/linux/pidfd.h:116:#define PIDFD_GET_PID_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 5)\ninclude/uapi/linux/pidfd.h:117:#define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE  _IO(PIDFS_IOCTL_MAGIC, 6)\ninclude/uapi/linux/pidfd.h:118:#define PIDFD_GET_TIME_NAMESPACE              _IO(PIDFS_IOCTL_MAGIC, 7)\ninclude/uapi/linux/pidfd.h:119:#define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)\ninclude/uapi/linux/pidfd.h:120:#define PIDFD_GET_USER_NAMESPACE              _IO(PIDFS_IOCTL_MAGIC, 9)\ninclude/uapi/linux/pidfd.h:121:#define PIDFD_GET_UTS_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 10)\ninclude/uapi/linux/pidfd.h:122:#define PIDFD_GET_INFO                        _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)\ninclude/uapi/linux/pidfd.h-123-\n--\ntools/perf/trace/beauty/include/uapi/linux/fcntl.h=88=struct delegation {\n--\ntools/perf/trace/beauty/include/uapi/linux/fcntl.h-124-\ntools/perf/trace/beauty/include/uapi/linux/fcntl.h:125:#define FD_PIDFS_ROOT\t\t\t-10002 /* Root of the pidfs filesystem */\ntools/perf/trace/beauty/include/uapi/linux/fcntl.h-126-#define FD_NSFS_ROOT\t\t\t-10003 /* Root of the nsfs filesystem */\n--\ntools/testing/selftests/filesystems/failfs/failfs_test.c-26-\ntools/testing/selftests/filesystems/failfs/failfs_test.c:27:#ifndef FD_PIDFS_ROOT\ntools/testing/selftests/filesystems/failfs/failfs_test.c:28:#define FD_PIDFS_ROOT -10002\ntools/testing/selftests/filesystems/failfs/failfs_test.c-29-#endif\n--\ntools/testing/selftests/filesystems/failfs/failfs_test.c=142=TEST(fchdir_rejects_other_sentinels)\ntools/testing/selftests/filesystems/failfs/failfs_test.c-143-{\ntools/testing/selftests/filesystems/failfs/failfs_test.c:144:\tASSERT_EQ(fchdir(FD_PIDFS_ROOT), -1);\ntools/testing/selftests/filesystems/failfs/failfs_test.c-145-\tASSERT_EQ(errno, EBADF);\n--\ntools/testing/selftests/filesystems/failfs/failfs_test.c=166=TEST(fchroot_bad_fd)\n--\ntools/testing/selftests/filesystems/failfs/failfs_test.c-171-\t/* Only FD_FAILFS_ROOT is a valid sentinel. */\ntools/testing/selftests/filesystems/failfs/failfs_test.c:172:\tASSERT_EQ(sys_fchroot(FD_PIDFS_ROOT, 0), -1);\ntools/testing/selftests/filesystems/failfs/failfs_test.c-173-\tASSERT_EQ(errno, EBADF);\n--\ntools/testing/selftests/pidfd/pidfd.h-30-\ntools/testing/selftests/pidfd/pidfd.h:31:#ifndef FD_PIDFS_ROOT\ntools/testing/selftests/pidfd/pidfd.h:32:#define FD_PIDFS_ROOT -10002\ntools/testing/selftests/pidfd/pidfd.h-33-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-82-\ntools/testing/selftests/pidfd/pidfd.h:83:#ifndef PIDFS_IOCTL_MAGIC\ntools/testing/selftests/pidfd/pidfd.h:84:#define PIDFS_IOCTL_MAGIC 0xFF\ntools/testing/selftests/pidfd/pidfd.h-85-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-87-#ifndef PIDFD_GET_CGROUP_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:88:#define PIDFD_GET_CGROUP_NAMESPACE            _IO(PIDFS_IOCTL_MAGIC, 1)\ntools/testing/selftests/pidfd/pidfd.h-89-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-91-#ifndef PIDFD_GET_IPC_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:92:#define PIDFD_GET_IPC_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 2)\ntools/testing/selftests/pidfd/pidfd.h-93-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-95-#ifndef PIDFD_GET_MNT_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:96:#define PIDFD_GET_MNT_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 3)\ntools/testing/selftests/pidfd/pidfd.h-97-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-99-#ifndef PIDFD_GET_NET_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:100:#define PIDFD_GET_NET_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 4)\ntools/testing/selftests/pidfd/pidfd.h-101-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-103-#ifndef PIDFD_GET_PID_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:104:#define PIDFD_GET_PID_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 5)\ntools/testing/selftests/pidfd/pidfd.h-105-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-107-#ifndef PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:108:#define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE  _IO(PIDFS_IOCTL_MAGIC, 6)\ntools/testing/selftests/pidfd/pidfd.h-109-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-111-#ifndef PIDFD_GET_TIME_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:112:#define PIDFD_GET_TIME_NAMESPACE              _IO(PIDFS_IOCTL_MAGIC, 7)\ntools/testing/selftests/pidfd/pidfd.h-113-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-115-#ifndef PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:116:#define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)\ntools/testing/selftests/pidfd/pidfd.h-117-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-119-#ifndef PIDFD_GET_USER_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:120:#define PIDFD_GET_USER_NAMESPACE              _IO(PIDFS_IOCTL_MAGIC, 9)\ntools/testing/selftests/pidfd/pidfd.h-121-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-123-#ifndef PIDFD_GET_UTS_NAMESPACE\ntools/testing/selftests/pidfd/pidfd.h:124:#define PIDFD_GET_UTS_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 10)\ntools/testing/selftests/pidfd/pidfd.h-125-#endif\n--\ntools/testing/selftests/pidfd/pidfd.h-127-#ifndef PIDFD_GET_INFO\ntools/testing/selftests/pidfd/pidfd.h:128:#define PIDFD_GET_INFO\t\t\t      _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)\ntools/testing/selftests/pidfd/pidfd.h-129-#endif\n--\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c=507=TEST_F(file_handle, decode_purely_based_on_file_handle)\n--\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c-522-\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c:523:\tpidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, 0);\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c-524-\tASSERT_GE(pidfd, 0);\n--\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c-530-\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c:531:\tpidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, O_CLOEXEC);\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c-532-\tASSERT_GE(pidfd, 0);\n--\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c-538-\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c:539:\tpidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, O_NONBLOCK);\ntools/testing/selftests/pidfd/pidfd_file_handle_test.c-540-\tASSERT_GE(pidfd, 0);\n"
}

23/2 2026/08/31 12:33 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 35369
							output: 25
							thoughts: 20
						

Thoughts:
Here's my summary, as if I were thinking it:

**Investigating the Build Process for `pidfs`**

Okay, so I need to figure out how `pidfs` is actually built within this kernel or file system context. The obvious first place to check is the `Makefile` in the `fs` directory. That's the go-to for build instructions within a file system component. If it's not defined there, or if there's some conditional logic involved, I'll need to dig into the `Kconfig` file within `fs` as well. That's where all the configuration options are declared, and that's likely where the inclusion or exclusion of `pidfs` is governed. I'm expecting to find a conditional statement like `CONFIG_PIDFS=y` or something similar, and then trace the implications of that setting. Let's start with those two files.



24/2 2026/08/31 12:33 tool grepper
0m Args:
{
  "Expression": "pidfs\\.o",
  "PathPrefix": "fs"
}

Results:
{
  "Output": "fs/Makefile=10=obj-y :=\topen.o read_write.o file_table.o super.o \\\n--\nfs/Makefile-17-\t\tfs_dirent.o fs_context.o fs_parser.o fsopen.o init.o \\\nfs/Makefile:18:\t\tkernel_read_file.o mnt_idmapping.o remap_range.o pidfs.o \\\nfs/Makefile-19-\t\tfile_attr.o fserror.o nullfs.o failfs.o\n"
}

25/2 2026/08/31 12:33 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 35744
							output: 25
							thoughts: 46
						

Thoughts:
Okay, here's my take on those thoughts, framed as my own:

**Investigating Potential Dependencies**

Alright, so `pidfs.o` is clearly part of the filesystem subsystem, and the Makefile in `fs/` explicitly declares it with `obj-y`. That gives me a starting point. Now, to get a better handle on its overall role and any possible connections to other parts of the kernel, I need to do a bit of sleuthing. Given the nature of PID file systems, I should probably check out `net/unix/Makefile`, `net/core/Makefile` and `fs/coredump.c` to look for shared resources or dependencies. It makes sense to start with these files, they're likely candidates, and this will tell me if the module is tied into the network stack or related core dumping. This gives me my next logical steps in understanding this module's place in the broader kernel architecture.



26/2 2026/08/31 12:33 tool grepper
0m Args:
{
  "Expression": "coredump\\.o",
  "PathPrefix": "fs"
}

Results:
{
  "Output": "fs/Makefile=46=obj-$(CONFIG_NFS_COMMON)\t+= nfs_common/\nfs/Makefile:47:obj-$(CONFIG_COREDUMP)\t\t+= coredump.o\nfs/Makefile-48-obj-$(CONFIG_SYSCTL)\t\t+= drop_caches.o sysctls.o\n"
}

27/2 2026/08/31 12:33 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 36101
							output: 31
							thoughts: 11
						

Thoughts:
Here's my take on the thought process, summarized as you requested:

**Diving into the Unix Domain Socket Makefile**

Okay, time to dig in. The next logical step to understand this system is to examine the `net/unix/Makefile`. That's where the build instructions for the Unix domain socket functionality should reside. I need to see how the object files are compiled, linked, and potentially what flags are used for the linker. This will tell me a lot about the dependencies, optimizations, and any custom configurations specific to Unix domain sockets within this project. Knowing the makefile will allow me to trace the build process and see exactly how the code turns into a runnable component. That's usually the best place to start when trying to reverse engineer or understand a low-level module like this, and it'll give me a strong foundation for exploring the actual source files later.



28/2 2026/08/31 12:33 tool read-file
0m Args:
{
  "File": "net/unix/Makefile",
  "FirstLine": 1,
  "LineCount": 20
}

Results:
{
  "Contents": "   1:\t# SPDX-License-Identifier: GPL-2.0\n   2:\t#\n   3:\t# Makefile for the Linux unix domain socket layer.\n   4:\t#\n   5:\t\n   6:\tobj-$(CONFIG_UNIX)\t+= unix.o\n   7:\t\n   8:\tunix-y\t\t\t:= af_unix.o garbage.o\n   9:\tunix-$(CONFIG_SYSCTL)\t+= sysctl_net_unix.o\n  10:\tunix-$(CONFIG_BPF_SYSCALL) += unix_bpf.o\n  11:\t\n  12:\tobj-$(CONFIG_UNIX_DIAG)\t+= unix_diag.o\n  13:\tunix_diag-y\t\t:= diag.o\n"
}

29/2 2026/08/31 12:33 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 36523
							output: 31
							thoughts: 11
						

Thoughts:
Here's my take on the thought process, as if I were the one thinking it:

**Diving into the Network Core Makefile**

Okay, time to get my hands dirty. Let's see what's happening with the core networking build process. My instinct tells me the place to start is likely `net/core/Makefile`. That's usually where the top-level definitions and dependencies for the kernel's networking subsystem are managed. I'm expecting to find the key build rules for the fundamental networking components – things like socket handling, protocol registration, memory management, and other crucial infrastructure elements. I'm looking for how different parts of this core are compiled and linked together, and how it interacts with the rest of the kernel build process. I'll be searching for insights into any recent changes or unusual build configurations that might be affecting things. I'll need to pay close attention to the include paths, link flags, and target definitions. This Makefile holds the secrets to understanding the overall structure of the network stack, so let's dig in and see what treasures we can unearth!



30/2 2026/08/31 12:34 tool read-file
0m Args:
{
  "File": "net/core/Makefile",
  "FirstLine": 1,
  "LineCount": 35
}

Results:
{
  "Contents": "   1:\t# SPDX-License-Identifier: GPL-2.0\n   2:\t#\n   3:\t# Makefile for the Linux networking core.\n   4:\t#\n   5:\t\n   6:\tobj-y := sock.o skbuff.o datagram.o stream.o scm.o \\\n   7:\t\t gen_stats.o gen_estimator.o net_namespace.o secure_seq.o \\\n   8:\t\t flow_dissector.o\n   9:\t\n  10:\tobj-$(CONFIG_SYSCTL) += sysctl_net_core.o\n  11:\t\n  12:\tobj-y\t\t     += dev.o dev_api.o dev_addr_lists.o dst.o netevent.o \\\n  13:\t\t\t\tneighbour.o rtnetlink.o utils.o link_watch.o filter.o \\\n  14:\t\t\t\tsock_diag.o dev_ioctl.o tso.o sock_reuseport.o \\\n  15:\t\t\t\tfib_notifier.o xdp.o flow_offload.o gro.o \\\n  16:\t\t\t\tnetdev-genl.o netdev-genl-gen.o netdev_work.o gso.o\n  17:\t\n  18:\tobj-$(CONFIG_NETDEV_ADDR_LIST_TEST) += dev_addr_lists_test.o\n  19:\t\n  20:\tobj-y += net-sysfs.o\n  21:\tobj-y += hotdata.o\n  22:\tobj-y += netdev_config.o\n  23:\tobj-y += netdev_rx_queue.o\n  24:\tobj-y += netdev_queues.o\n  25:\tobj-$(CONFIG_PAGE_POOL) += page_pool.o page_pool_user.o\n  26:\tobj-$(CONFIG_PROC_FS) += net-procfs.o\n  27:\tobj-$(CONFIG_NET_PKTGEN) += pktgen.o\n  28:\tobj-$(CONFIG_NETPOLL) += netpoll.o\n  29:\tobj-$(CONFIG_FIB_RULES) += fib_rules.o\n  30:\tobj-$(CONFIG_TRACEPOINTS) += net-traces.o\n  31:\tobj-$(CONFIG_NET_DROP_MONITOR) += drop_monitor.o\n  32:\tobj-$(CONFIG_NET_IEEE8021Q_HELPERS) += ieee8021q_helpers.o\n  33:\tobj-$(CONFIG_NET_SELFTESTS) += selftests.o\n  34:\tobj-$(CONFIG_NETWORK_PHY_TIMESTAMPING) += timestamping.o\n  35:\tobj-$(CONFIG_NET_PTP_CLASSIFY) += ptp_classifier.o\n"
}

31/2 2026/08/31 12:34 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 37485
							output: 25
							thoughts: 540
						

Thoughts:
**My Analysis of Kernel Object Dependencies and Fuzzing Focus**

Okay, so I'm looking at this patch and figuring out how to approach fuzzing it effectively. First, `sock.o` and `scm.o` are part of `obj-y` when `CONFIG_NET` is enabled, which is standard. `af_unix.o` needs `CONFIG_UNIX`, and `coredump.o` needs `CONFIG_COREDUMP`. Nothing surprising there.

Now, let's get down to the fuzzing specifics. The goal is to identify the right focus symbols. I prefer non-static, non-inlined API entrypoints – the public-facing functions, not internal helpers.

Let's break down the functions touched or created:

1.  `sk_setsockopt` in `net/core/sock.c` – This handles `SO_PASSPIDFD_THREAD` and `SO_PASSPIDFD`, so it's definitely interesting.
2.  `sk_getsockopt` in `net/core/sock.c` – Handles `SO_PASSPIDFD_THREAD`, `SO_PEERPIDFD`, and `SO_PEERPIDFD_THREAD`. Definitely worth investigating.
3.  `scm_recv_unix` in `net/core/scm.c` – This calls `scm_pidfd_recv`, so it's a potential entry point.
4.  `__scm_send` in `net/core/scm.c` – This calls `scm_replace_pid`, so it's a good candidate.
5.  `__pidfs_register_pids` in `fs/pidfs.c` – A new function, always a good target.
6.  `pidfs_coredump` in `fs/pidfs.c` – It's a modified function, so we should look into the changes.

I'll quickly scan for any other non-static functions related to the patch, just to be sure. Let's look at `net/bluetooth/af_bluetooth.c` or others to see. Ah, looks like `bt_sock_alloc`, and `bt_accept_enqueue` in `net/bluetooth/af_bluetooth.c` that are small updates to index `sk_peer_pid[PIDTYPE_TGID]` instead of scalar `sk_peer_pid`. Not a primary focus, but might be indirectly affected, so I'll keep an eye on them.

Alright, back to the `FocusSymbols`:

*   `sk_setsockopt`
*   `sk_getsockopt`
*   `scm_recv_unix`
*   `__scm_send`
*   `__pidfs_register_pids`
*   `pidfs_coredump`

These seem like a solid list of entry points.

Now for the `EnableConfigs`:

*   `UNIX`
*   `COREDUMP`

And I'll double-check that `NET` is also needed since UNIX depends on it. Good to have that documented. Let me check `net/unix/Kconfig` to be sure.

This is a pretty straightforward process; good list of focus functions, and correct configuration requirements. It is a solid approach.



32/2 2026/08/31 12:34 tool grepper
0m Args:
{
  "Expression": "config UNIX",
  "PathPrefix": "net/unix"
}

Results:
{
  "Output": "net/unix/Kconfig-5-\nnet/unix/Kconfig:6:config UNIX\nnet/unix/Kconfig-7-\tbool \"Unix domain sockets\"\n--\nnet/unix/Kconfig=19=config\tAF_UNIX_OOB\n--\nnet/unix/Kconfig-25-\nnet/unix/Kconfig:26:config UNIX_DIAG\nnet/unix/Kconfig-27-\ttristate \"UNIX: socket monitoring interface\"\n"
}

33/2 2026/08/31 12:34 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 38890
							output: 18
							thoughts: 19
						

Thoughts:
Here's my thought process, as it went:

**Examining Kernel Configuration for Core Dumps**

Okay, so I need to figure out the current status of core dumps within the kernel configuration. My instinct says to look at `fs/Kconfig.binfmt`, that's usually where the binary format handling is defined, and core dump behavior is often linked to how executables are handled. I'll need to check the contents of that file to see if there's a specific `COREDUMP` option or something related that I can use to understand if core dump generation is enabled or how it is being configured. Right, let's get to it.



34/2 2026/08/31 12:34 tool grepper
0m Args:
{
  "Expression": "config COREDUMP"
}

Results:
{
  "Output": "fs/Kconfig.binfmt=171=config BINFMT_MISC_BPF\n--\nfs/Kconfig.binfmt-184-\nfs/Kconfig.binfmt:185:config COREDUMP\nfs/Kconfig.binfmt-186-\tbool \"Enable core dump support\" if EXPERT\n"
}

35/2 2026/08/31 12:34 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 39158
							output: 158
							thoughts: 169
						

Thoughts:
**My Assessment: Patch Verification and Integration**

Okay, everything looks clean, verified, and ready to move forward. Time to solidify this in my mind... Let's run through the critical parameters to confirm:

*   **`WorthFuzzing`**: Definitely. We're dealing with new socket options and potential thread-specific interactions. Fuzzing is essential to flush out any edge cases.
*   **`Reasoning`**: This patch is adding `SO_PASSPIDFD_THREAD` and `SO_PEERPIDFD_THREAD` options for UNIX domain sockets. It allows for thread-specific `pidfd` passing and peer credential retrieval – very important additions. It also updates `pidfs` and coredump logic to include thread `pidfd` information. This is substantial, affecting socket operations and debugging.
*   **`EnableConfigs`**: We're enabling the relevant configurations: "UNIX" and "COREDUMP". This confirms we're handling the core components this patch touches.
*   **`FocusSymbols`**: The critical symbols: `sk_setsockopt`, `sk_getsockopt`, `scm_recv_unix`, `__scm_send`, `__pidfs_register_pids`, and `pidfs_coredump`. These are the functions directly impacted by the patch. They're all accounted for, so we're set.

With these parameters verified, I am confident in triggering the `set-results` function.



36/2 2026/08/31 12:34 tool set-results
0m Args:
{
  "EnableConfigs": [
    "UNIX",
    "COREDUMP"
  ],
  "FocusSymbols": [
    "sk_setsockopt",
    "sk_getsockopt",
    "scm_recv_unix",
    "__scm_send",
    "__pidfs_register_pids",
    "pidfs_coredump"
  ],
  "Reasoning": "The patch introduces the SO_PASSPIDFD_THREAD and SO_PEERPIDFD_THREAD socket options to allow passing and querying thread-specific pidfds over UNIX domain sockets. It updates struct sock, SCM credential passing, UNIX socket read/write credential matching, pidfs pid registration, and coredump notification mechanisms.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "UNIX",
    "COREDUMP"
  ],
  "FocusSymbols": [
    "sk_setsockopt",
    "sk_getsockopt",
    "scm_recv_unix",
    "__scm_send",
    "__pidfs_register_pids",
    "pidfs_coredump"
  ],
  "Reasoning": "The patch introduces the SO_PASSPIDFD_THREAD and SO_PEERPIDFD_THREAD socket options to allow passing and querying thread-specific pidfds over UNIX domain sockets. It updates struct sock, SCM credential passing, UNIX socket read/write credential matching, pidfs pid registration, and coredump notification mechanisms.",
  "WorthFuzzing": true
}

37/1 2026/08/31 12:34 action If
10m Args:
{
  "WorthFuzzing": true
}

Results:
null

38/2 2026/08/31 12:34 agent kmsan-evaluator
10m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series introduces support for thread pidfds in UNIX domain sockets and coredump mechanisms (SO_PASSPIDFD_THREAD, SO_PEERPIDFD_THREAD).\n\nKey areas analyzed for uninitialized memory risks:\n1. `DECLARE_PIDS` arrays in `struct scm_cookie`, `struct sock`, `struct coredump_params`, `struct unix_peercred`, and `struct unix_skb_parms`:\n   - `struct sock` instances are allocated with `sk_alloc()` (zeroed) and explicitly reset in `sock_init_data_uid()` with `memset(sk-\u003esk_peer_pid, 0, ...)`.\n   - `struct scm_cookie` objects are consistently zero-initialized (either via designated zero initializers `= {}` or `memset(..., 0, sizeof(...))`) across all callers (`scm_send`, `unix_destruct_scm`, `__unix_dgram_recvmsg`, `unix_stream_read_generic`, `hci_sock_recvmsg`, `netlink_recvmsg`).\n   - `struct unix_peercred` instances are zero-initialized (`= {}`) in `unix_listen`, `unix_stream_connect`, and `unix_socketpair` before `prepare_peercred()`.\n   - `skb-\u003ecb` containing `struct unix_skb_parms` is zeroed during `__alloc_skb()`.\n   - `struct coredump_params` is zero-initialized via designated initializers in `vfs_coredump()`.\n2. Socket option handlers and cmsg helpers:\n   - `sk_getsockopt_peerpidfd()` copies an initialized `int pidfd` and `int len` to user space.\n   - `scm_pidfd_recv()` initializes `pidfd` from `pidfd_prepare()` before writing via `put_cmsg()`.\n   - No structure padding or uninitialized fields are exposed to userspace or read in conditional logic.\n3. Potential issues in this patch series (e.g. pid reference management, use-after-free, locking around `sk_peer_lock`, NULL dereference) are detectable by KASAN, LOCKDEP, and standard kernel debugging facilities.\n\nThus, there are no uninitialized memory risks warranting a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit 332b03a59787671294e517d035f545dd992063b1
Author: syz-cluster <triage@syzkaller.com>
Date:   Mon Aug 31 12:30:23 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h
index 946a5fad26912..5d3524c26b2bc 100644
--- a/arch/alpha/include/uapi/asm/socket.h
+++ b/arch/alpha/include/uapi/asm/socket.h
@@ -157,6 +157,10 @@
 
 #define SO_RIGHTS_NOTRUNC      85
 
+#define SO_PASSPIDFD_THREAD    86
+
+#define SO_PEERPIDFD_THREAD    87
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h
index f1641dde135fc..245a43f52fb1a 100644
--- a/arch/mips/include/uapi/asm/socket.h
+++ b/arch/mips/include/uapi/asm/socket.h
@@ -168,6 +168,10 @@
 
 #define SO_RIGHTS_NOTRUNC      85
 
+#define SO_PASSPIDFD_THREAD    86
+
+#define SO_PEERPIDFD_THREAD    87
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h
index f3a3815c7dc29..f23710e1c6714 100644
--- a/arch/parisc/include/uapi/asm/socket.h
+++ b/arch/parisc/include/uapi/asm/socket.h
@@ -149,6 +149,10 @@
 
 #define SO_RIGHTS_NOTRUNC	0x4053
 
+#define SO_PASSPIDFD_THREAD	0x4054
+
+#define SO_PEERPIDFD_THREAD	0x4055
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h
index 7907f3b1f0ee0..b35b25bdefc2b 100644
--- a/arch/sparc/include/uapi/asm/socket.h
+++ b/arch/sparc/include/uapi/asm/socket.h
@@ -150,6 +150,10 @@
 
 #define SO_RIGHTS_NOTRUNC        0x005e
 
+#define SO_PASSPIDFD_THREAD      0x005f
+
+#define SO_PEERPIDFD_THREAD      0x0060
+
 #if !defined(__KERNEL__)
 
 
diff --git a/fs/coredump.c b/fs/coredump.c
index ac3cd74808c64..b5ff4b3e1831e 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -454,7 +454,7 @@ static bool coredump_parse(struct core_name *cn, struct coredump_params *cprm,
 				 * leader we know that the thread-group leader
 				 * cannot be reaped until @current has exited.
 				 */
-				cprm->pid = task_tgid(current);
+				task_pids(cprm->pid, current);
 				err = cn_printf(cn, "%d", COREDUMP_PIDFD_NUMBER);
 				break;
 			}
@@ -626,13 +626,17 @@ static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)
 	struct coredump_params *cp = (struct coredump_params *)info->data;
 	int err;
 
-	if (cp->pid) {
+	if (cp->pid[PIDTYPE_TGID]) {
 		struct file *pidfs_file __free(fput) = NULL;
 
-		pidfs_file = pidfs_alloc_file(cp->pid, 0);
+		pidfs_file = pidfs_alloc_file(cp->pid[PIDTYPE_TGID], 0);
 		if (IS_ERR(pidfs_file))
 			return PTR_ERR(pidfs_file);
 
+		err = pidfs_register_pids(cp->pid);
+		if (err)
+			return err;
+
 		pidfs_coredump(cp);
 
 		/*
@@ -695,12 +699,12 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *
 		return false;
 
 	/*
-	 * Set the thread-group leader pid which is used for the peer
-	 * credentials during connect() below. Then immediately register
-	 * it in pidfs...
+	 * Set the pids of the dumping thread and its thread-group leader
+	 * which are used for the peer credentials during connect() below.
+	 * Then immediately register them in pidfs...
 	 */
-	cprm->pid = task_tgid(current);
-	retval = pidfs_register_pid(cprm->pid);
+	task_pids(cprm->pid, current);
+	retval = pidfs_register_pids(cprm->pid);
 	if (retval)
 		return false;
 
@@ -722,7 +726,7 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *
 	}
 
 	/* ... and validate that @sk_peer_pid matches @cprm.pid. */
-	if (WARN_ON_ONCE(unix_peer(socket->sk)->sk_peer_pid != cprm->pid))
+	if (WARN_ON_ONCE(!pids_equal(unix_peer(socket->sk)->sk_peer_pid, cprm->pid)))
 		return false;
 
 	cprm->limit = RLIM_INFINITY;
diff --git a/fs/pidfs.c b/fs/pidfs.c
index a6a643f15d08a..29299b2c7ca71 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -793,9 +793,9 @@ void pidfs_exit(struct task_struct *tsk)
 }
 
 #ifdef CONFIG_COREDUMP
-void pidfs_coredump(const struct coredump_params *cprm)
+static void pidfs_coredump_pid(struct pid *pid,
+			       const struct coredump_params *cprm)
 {
-	struct pid *pid = cprm->pid;
 	struct pidfs_attr *attr;
 
 	attr = READ_ONCE(pid->attr);
@@ -814,6 +814,13 @@ void pidfs_coredump(const struct coredump_params *cprm)
 	smp_wmb();
 	set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
 }
+
+void pidfs_coredump(const struct coredump_params *cprm)
+{
+	/* The dumping thread's pidfd reports the coredump as well. */
+	for (enum pid_type type = PIDTYPE_PID; type <= pids_last(cprm->pid); type++)
+		pidfs_coredump_pid(cprm->pid[type], cprm);
+}
 #endif
 
 static struct vfsmount *pidfs_mnt __ro_after_init;
@@ -1070,6 +1077,22 @@ int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)
 	return 0;
 }
 
+/* Register the pids up to pid type @last of @pids in pidfs. */
+int __pidfs_register_pids(struct pid *const *pids, enum pid_type last)
+{
+	if (WARN_ON_ONCE(last >= PIDTYPE_MAX))
+		return -EINVAL;
+
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++) {
+		int ret = pidfs_register_pid(pids[type]);
+
+		if (unlikely(ret))
+			return ret;
+	}
+
+	return 0;
+}
+
 static struct dentry *pidfs_stash_dentry(struct dentry **stashed,
 					 struct dentry *dentry)
 {
diff --git a/include/linux/coredump.h b/include/linux/coredump.h
index 7b38ee2e7913b..0bbb7de6a4021 100644
--- a/include/linux/coredump.h
+++ b/include/linux/coredump.h
@@ -5,6 +5,7 @@
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/fs.h>
+#include <linux/pid_types.h>
 #include <linux/sched/coredump.h>
 #include <asm/siginfo.h>
 
@@ -32,7 +33,8 @@ struct coredump_params {
 	int vma_count;
 	size_t vma_data_size;
 	struct core_vma_metadata *vma_meta;
-	struct pid *pid;
+	/* Dumping thread and its thread-group leader by pid type. */
+	DECLARE_PIDS(pid, PIDTYPE_TGID);
 };
 
 extern unsigned int core_file_note_size_limit;
diff --git a/include/linux/pid.h b/include/linux/pid.h
index ddaef0bbc8ba3..87635d0cc1f71 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -2,6 +2,9 @@
 #ifndef _LINUX_PID_H
 #define _LINUX_PID_H
 
+#include <linux/array_size.h>
+#include <linux/build_bug.h>
+#include <linux/minmax.h>
 #include <linux/pid_types.h>
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
@@ -92,6 +95,56 @@ static inline struct pid *get_pid(struct pid *pid)
 }
 
 extern void put_pid(struct pid *pid);
+
+/*
+ * Helpers for arrays of struct pid indexed by pid type declared with
+ * DECLARE_PIDS(). The array covers PIDTYPE_PID up to the pid type it
+ * was declared with and the helpers take that bound from the array.
+ */
+static inline void __get_pids(struct pid **dst, struct pid *const *src,
+			      enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		dst[type] = get_pid(src[type]);
+}
+
+static inline void __put_pids(struct pid **pids, enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++) {
+		put_pid(pids[type]);
+		pids[type] = NULL;
+	}
+}
+
+static inline void __swap_pids(struct pid **a, struct pid **b,
+			       enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		swap(a[type], b[type]);
+}
+
+static inline bool __pids_equal(struct pid *const *a, struct pid *const *b,
+				enum pid_type last)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		if (a[type] != b[type])
+			return false;
+	return true;
+}
+
+/* The last pid type an array declared with DECLARE_PIDS() covers. */
+#define pids_last(pids)							\
+	((enum pid_type)(ARRAY_SIZE(pids) - 1 +				\
+			 BUILD_BUG_ON_ZERO(ARRAY_SIZE(pids) > PIDTYPE_MAX)))
+
+#define __pids_last2(a, b)						\
+	(pids_last(a) + BUILD_BUG_ON_ZERO(ARRAY_SIZE(a) != ARRAY_SIZE(b)))
+
+#define get_pids(dst, src)	__get_pids(dst, src, __pids_last2(dst, src))
+#define put_pids(pids)		__put_pids(pids, pids_last(pids))
+#define swap_pids(a, b)		__swap_pids(a, b, __pids_last2(a, b))
+#define pids_equal(a, b)	__pids_equal(a, b, __pids_last2(a, b))
+
 extern struct task_struct *pid_task(struct pid *pid, enum pid_type);
 static inline bool pid_has_task(struct pid *pid, enum pid_type type)
 {
diff --git a/include/linux/pid_types.h b/include/linux/pid_types.h
index c2aee1d91dcfd..3302690a2a281 100644
--- a/include/linux/pid_types.h
+++ b/include/linux/pid_types.h
@@ -2,6 +2,8 @@
 #ifndef _LINUX_PID_TYPES_H
 #define _LINUX_PID_TYPES_H
 
+#include <linux/build_bug.h>
+
 enum pid_type {
 	PIDTYPE_PID,
 	PIDTYPE_TGID,
@@ -10,6 +12,12 @@ enum pid_type {
 	PIDTYPE_MAX,
 };
 
+struct pid;
+
+/* An array of struct pid indexed by pid type, PIDTYPE_PID up to @last. */
+#define DECLARE_PIDS(name, last)					\
+	struct pid *name[(last) + 1 + BUILD_BUG_ON_ZERO((last) >= PIDTYPE_MAX)]
+
 struct pid_namespace;
 extern struct pid_namespace init_pid_ns;
 
diff --git a/include/linux/pidfs.h b/include/linux/pidfs.h
index 0abf7da9ab236..3c1e82f1a3691 100644
--- a/include/linux/pidfs.h
+++ b/include/linux/pidfs.h
@@ -3,6 +3,7 @@
 #define _LINUX_PID_FS_H
 
 #include <linux/gfp_types.h>
+#include <linux/pid.h>
 
 struct coredump_params;
 
@@ -32,5 +33,9 @@ static inline int pidfs_register_pid(struct pid *pid)
 }
 
 void pidfs_free_pid(struct pid *pid);
+int __pidfs_register_pids(struct pid *const *pids, enum pid_type last);
+
+/* Register the pids of an array declared with DECLARE_PIDS(). */
+#define pidfs_register_pids(pids)	__pidfs_register_pids(pids, pids_last(pids))
 
 #endif /* _LINUX_PID_FS_H */
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 584ae88b435e9..9444b47789a08 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -677,6 +677,24 @@ struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
 	return pid;
 }
 
+/* Fill @pids with the pid types of @task up to @last, without references. */
+static inline void __task_pids(struct pid **pids, enum pid_type last,
+			       struct task_struct *task)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		pids[type] = task_pid_type(task, type);
+}
+
+static inline void __get_task_pids(struct pid **pids, enum pid_type last,
+				   struct task_struct *task)
+{
+	for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
+		pids[type] = get_pid(task_pid_type(task, type));
+}
+
+#define task_pids(pids, task)		__task_pids(pids, pids_last(pids), task)
+#define get_task_pids(pids, task)	__get_task_pids(pids, pids_last(pids), task)
+
 static inline struct pid *task_tgid(struct task_struct *task)
 {
 	return task->signal->pids[PIDTYPE_TGID];
diff --git a/include/net/scm.h b/include/net/scm.h
index 86ae6bc109ec6..aa7d15c5fc271 100644
--- a/include/net/scm.h
+++ b/include/net/scm.h
@@ -42,7 +42,7 @@ struct scm_fp_list {
 };
 
 struct scm_cookie {
-	struct pid		*pid;		/* Skb credentials */
+	DECLARE_PIDS(pid, PIDTYPE_TGID);	/* Skb credentials by pid type */
 	struct scm_fp_list	*fp;		/* Passed files		*/
 	struct scm_creds	creds;		/* Skb credentials	*/
 #ifdef CONFIG_SECURITY_NETWORK
@@ -69,7 +69,7 @@ static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_co
 static __inline__ void scm_set_cred(struct scm_cookie *scm,
 				    struct pid *pid, kuid_t uid, kgid_t gid)
 {
-	scm->pid = get_pid(pid);
+	scm->pid[PIDTYPE_TGID] = get_pid(pid);
 	scm->creds.pid = pid_vnr(pid);
 	scm->creds.uid = uid;
 	scm->creds.gid = gid;
@@ -77,8 +77,7 @@ static __inline__ void scm_set_cred(struct scm_cookie *scm,
 
 static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
 {
-	put_pid(scm->pid);
-	scm->pid = NULL;
+	put_pids(scm->pid);
 }
 
 static __inline__ void scm_destroy(struct scm_cookie *scm)
diff --git a/include/net/sock.h b/include/net/sock.h
index 51185222aac29..67b743bab220e 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -301,7 +301,7 @@ struct sk_filter;
   *	@sk_type: socket type (%SOCK_STREAM, etc)
   *	@sk_protocol: which protocol this socket belongs in this network family
   *	@sk_peer_lock: lock protecting @sk_peer_pid and @sk_peer_cred
-  *	@sk_peer_pid: &struct pid for this socket's peer
+  *	@sk_peer_pid: &struct pid for this socket's peer, by pid type
   *	@sk_peer_cred: %SO_PEERCRED setting
   *	@sk_rcvlowat: %SO_RCVLOWAT setting
   *	@sk_rcvtimeo: %SO_RCVTIMEO setting
@@ -356,6 +356,7 @@ struct sk_filter;
   *	@sk_scm_security: flagged by SO_PASSSEC to recv SCM_SECURITY
   *	@sk_scm_pidfd: flagged by SO_PASSPIDFD to recv SCM_PIDFD
   *	@sk_scm_rights: flagged by SO_PASSRIGHTS to recv SCM_RIGHTS
+  *	@sk_scm_pidfd_thread: flagged by SO_PASSPIDFD_THREAD to recv a thread SCM_PIDFD
   *	@sk_scm_unused: unused flags for scm_recv()
   *	@ns_tracker: tracker for netns reference
   *	@sk_user_frags: xarray of pages the user is holding a reference on.
@@ -545,7 +546,7 @@ struct sock {
 	u64			sk_ino;
 	spinlock_t		sk_peer_lock;
 	int			sk_bind_phc;
-	struct pid		*sk_peer_pid;
+	DECLARE_PIDS(sk_peer_pid, PIDTYPE_TGID);
 	const struct cred	*sk_peer_cred;
 
 	ktime_t			sk_stamp;
@@ -562,7 +563,8 @@ struct sock {
 				sk_scm_security : 1,
 				sk_scm_pidfd : 1,
 				sk_scm_rights : 1,
-				sk_scm_unused : 4;
+				sk_scm_pidfd_thread : 1,
+				sk_scm_unused : 3;
 		};
 	};
 	u8			sk_clockid;
@@ -2986,6 +2988,12 @@ static inline bool sk_is_stream_unix(const struct sock *sk)
 	return sk_is_unix(sk) && sk->sk_type == SOCK_STREAM;
 }
 
+/* SO_PASSPIDFD or SO_PASSPIDFD_THREAD asked for an SCM_PIDFD. */
+static inline bool sk_scm_pidfd_wanted(const struct sock *sk)
+{
+	return sk->sk_scm_pidfd || sk->sk_scm_pidfd_thread;
+}
+
 static inline bool sk_is_vsock(const struct sock *sk)
 {
 	return sk->sk_family == AF_VSOCK;
diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h
index 84ea7b92936e2..56fed7ab27ab8 100644
--- a/include/uapi/asm-generic/socket.h
+++ b/include/uapi/asm-generic/socket.h
@@ -152,6 +152,10 @@
 
 #define SO_RIGHTS_NOTRUNC	85
 
+#define SO_PASSPIDFD_THREAD	86
+
+#define SO_PEERPIDFD_THREAD	87
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 411d66f24393d..7758e9ea3848c 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -161,7 +161,7 @@ struct sock *bt_sock_alloc(struct net *net, struct socket *sock,
 	/* Init peer information so it can be properly monitored */
 	if (!kern) {
 		spin_lock(&sk->sk_peer_lock);
-		sk->sk_peer_pid  = get_pid(task_tgid(current));
+		sk->sk_peer_pid[PIDTYPE_TGID]  = get_pid(task_tgid(current));
 		sk->sk_peer_cred = get_current_cred();
 		spin_unlock(&sk->sk_peer_lock);
 	}
@@ -235,9 +235,9 @@ void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)
 	 * socket is allocated by the kernel.
 	 */
 	spin_lock(&sk->sk_peer_lock);
-	old_pid = sk->sk_peer_pid;
+	old_pid = sk->sk_peer_pid[PIDTYPE_TGID];
 	old_cred = sk->sk_peer_cred;
-	sk->sk_peer_pid = get_pid(parent->sk_peer_pid);
+	sk->sk_peer_pid[PIDTYPE_TGID] = get_pid(parent->sk_peer_pid[PIDTYPE_TGID]);
 	sk->sk_peer_cred = get_cred(parent->sk_peer_cred);
 	spin_unlock(&sk->sk_peer_lock);
 
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 070ca388f9ace..91e4738eabdb0 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -273,21 +273,21 @@ static void hci_sock_copy_creds(struct sock *sk, struct sk_buff *skb)
 	creds = &bt_cb(skb)->creds;
 
 	/* Check if peer credentials is set */
-	if (!sk->sk_peer_pid) {
+	if (!sk->sk_peer_pid[PIDTYPE_TGID]) {
 		/* Check if parent peer credentials is set */
-		if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid)
+		if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid[PIDTYPE_TGID])
 			sk = bt_sk(sk)->parent;
 		else
 			return;
 	}
 
 	/* Check if scm_creds already set */
-	if (creds->pid == pid_vnr(sk->sk_peer_pid))
+	if (creds->pid == pid_vnr(sk->sk_peer_pid[PIDTYPE_TGID]))
 		return;
 
 	memset(creds, 0, sizeof(*creds));
 
-	creds->pid = pid_vnr(sk->sk_peer_pid);
+	creds->pid = pid_vnr(sk->sk_peer_pid[PIDTYPE_TGID]);
 	if (sk->sk_peer_cred) {
 		creds->uid = sk->sk_peer_cred->uid;
 		creds->gid = sk->sk_peer_cred->gid;
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 1194c37e466f1..872d8fb31b6fb 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1890,7 +1890,7 @@ static struct pid *l2cap_sock_get_peer_pid_cb(struct l2cap_chan *chan)
 {
 	struct sock *sk = chan->data;
 
-	return sk->sk_peer_pid;
+	return sk->sk_peer_pid[PIDTYPE_TGID];
 }
 
 static void l2cap_sock_suspend_cb(struct l2cap_chan *chan)
diff --git a/net/core/scm.c b/net/core/scm.c
index f0d44ecdb11fc..d69768414af48 100644
--- a/net/core/scm.c
+++ b/net/core/scm.c
@@ -149,6 +149,7 @@ EXPORT_SYMBOL(__scm_destroy);
 
 static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
 {
+	struct pid *thread_pid;
 	int err;
 
 	/* drop all previous references */
@@ -158,7 +159,18 @@ static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
 	if (unlikely(err))
 		return err;
 
-	scm->pid = pid;
+	/* A sender naming its own thread-group sends from the current thread. */
+	if (pid == task_tgid(current))
+		thread_pid = task_pid(current);
+	else
+		thread_pid = pid;
+
+	err = pidfs_register_pid(thread_pid);
+	if (unlikely(err))
+		return err;
+
+	scm->pid[PIDTYPE_TGID] = pid;
+	scm->pid[PIDTYPE_PID] = get_pid(thread_pid);
 	scm->creds.pid = pid_vnr(pid);
 	return 0;
 }
@@ -207,7 +219,8 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
 			if (err)
 				goto error;
 
-			if (!p->pid || pid_vnr(p->pid) != creds.pid) {
+			if (!p->pid[PIDTYPE_TGID] ||
+			    pid_vnr(p->pid[PIDTYPE_TGID]) != creds.pid) {
 				struct pid *pid;
 				err = -ESRCH;
 				pid = find_get_pid(creds.pid);
@@ -486,9 +499,13 @@ static bool scm_has_secdata(struct sock *sk)
 }
 #endif
 
-static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
+static void scm_pidfd_recv(struct sock *sk, struct msghdr *msg,
+			   struct scm_cookie *scm)
 {
+	enum pid_type type = sk->sk_scm_pidfd_thread ? PIDTYPE_PID : PIDTYPE_TGID;
+	struct pid *pid = scm->pid[type];
 	struct file *pidfd_file = NULL;
+	unsigned int flags = PIDFD_STALE;
 	int len, pidfd;
 
 	/* put_cmsg() doesn't return an error if CMSG is truncated,
@@ -504,10 +521,13 @@ static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
 		return;
 	}
 
-	if (!scm->pid)
+	if (!pid)
 		return;
 
-	pidfd = pidfd_prepare(scm->pid, PIDFD_STALE, &pidfd_file);
+	if (type == PIDTYPE_PID)
+		flags |= PIDFD_THREAD;
+
+	pidfd = pidfd_prepare(pid, flags, &pidfd_file);
 
 	if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) {
 		if (pidfd_file) {
@@ -526,7 +546,7 @@ static bool __scm_recv_common(struct sock *sk, struct msghdr *msg,
 			      struct scm_cookie *scm, int flags)
 {
 	if (!msg->msg_control) {
-		if (sk->sk_scm_credentials || sk->sk_scm_pidfd ||
+		if (sk->sk_scm_credentials || sk_scm_pidfd_wanted(sk) ||
 		    scm->fp || scm_has_secdata(sk))
 			msg->msg_flags |= MSG_CTRUNC;
 
@@ -573,8 +593,8 @@ void scm_recv_unix(struct socket *sock, struct msghdr *msg,
 		scm_detach_fds(msg, scm, READ_ONCE(u->scm_rights_notrunc));
 	}
 
-	if (sock->sk->sk_scm_pidfd)
-		scm_pidfd_recv(msg, scm);
+	if (sk_scm_pidfd_wanted(sock->sk))
+		scm_pidfd_recv(sock->sk, msg, scm);
 
 	scm_destroy_cred(scm);
 }
diff --git a/net/core/sock.c b/net/core/sock.c
index 1ad41904db25b..cb2ffd329bc67 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -1571,10 +1571,25 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 		break;
 
 	case SO_PASSPIDFD:
-		if (sk_is_unix(sk))
+		if (sk_is_unix(sk)) {
+			/* Mutually exclusive with SO_PASSPIDFD_THREAD. */
 			sk->sk_scm_pidfd = valbool;
-		else
+			if (valbool)
+				sk->sk_scm_pidfd_thread = 0;
+		} else {
+			ret = -EOPNOTSUPP;
+		}
+		break;
+
+	case SO_PASSPIDFD_THREAD:
+		if (sk_is_unix(sk)) {
+			/* Mutually exclusive with SO_PASSPIDFD. */
+			sk->sk_scm_pidfd_thread = valbool;
+			if (valbool)
+				sk->sk_scm_pidfd = 0;
+		} else {
 			ret = -EOPNOTSUPP;
+		}
 		break;
 
 	case SO_PASSRIGHTS:
@@ -1728,6 +1743,50 @@ static int groups_to_user(sockptr_t dst, const struct group_info *src)
 	return 0;
 }
 
+/* Hand out a pidfd for @type of the socket's peer via SO_PEERPIDFD*. */
+static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type,
+				   sockptr_t optval, sockptr_t optlen, int len)
+{
+	struct file *pidfd_file = NULL;
+	unsigned int flags = 0;
+	struct pid *peer_pid;
+	int pidfd;
+
+	if (len > sizeof(pidfd))
+		len = sizeof(pidfd);
+
+	spin_lock(&sk->sk_peer_lock);
+	peer_pid = get_pid(sk->sk_peer_pid[type]);
+	spin_unlock(&sk->sk_peer_lock);
+
+	if (!peer_pid)
+		return -ENODATA;
+
+	/* The use of PIDFD_STALE requires stashing of struct pid on pidfs
+	 * with pidfs_register_pid() and only AF_UNIX were prepared for this.
+	 */
+	if (sk->sk_family == AF_UNIX)
+		flags |= PIDFD_STALE;
+	if (type == PIDTYPE_PID)
+		flags |= PIDFD_THREAD;
+
+	pidfd = pidfd_prepare(peer_pid, flags, &pidfd_file);
+	put_pid(peer_pid);
+	if (pidfd < 0)
+		return pidfd;
+
+	if (copy_to_sockptr(optval, &pidfd, len) ||
+	    copy_to_sockptr(optlen, &len, sizeof(int))) {
+		put_unused_fd(pidfd);
+		fput(pidfd_file);
+
+		return -EFAULT;
+	}
+
+	fd_install(pidfd, pidfd_file);
+	return 0;
+}
+
 int sk_getsockopt(struct sock *sk, int level, int optname,
 		  sockptr_t optval, sockptr_t optlen)
 {
@@ -1892,6 +1951,13 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 		v.val = sk->sk_scm_pidfd;
 		break;
 
+	case SO_PASSPIDFD_THREAD:
+		if (!sk_is_unix(sk))
+			return -EOPNOTSUPP;
+
+		v.val = sk->sk_scm_pidfd_thread;
+		break;
+
 	case SO_PASSRIGHTS:
 		if (!sk_is_unix(sk))
 			return -EOPNOTSUPP;
@@ -1906,7 +1972,8 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 			len = sizeof(peercred);
 
 		spin_lock(&sk->sk_peer_lock);
-		cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
+		cred_to_ucred(sk->sk_peer_pid[PIDTYPE_TGID], sk->sk_peer_cred,
+			      &peercred);
 		spin_unlock(&sk->sk_peer_lock);
 
 		if (copy_to_sockptr(optval, &peercred, len))
@@ -1915,45 +1982,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 	}
 
 	case SO_PEERPIDFD:
-	{
-		struct pid *peer_pid;
-		struct file *pidfd_file = NULL;
-		unsigned int flags = 0;
-		int pidfd;
-
-		if (len > sizeof(pidfd))
-			len = sizeof(pidfd);
-
-		spin_lock(&sk->sk_peer_lock);
-		peer_pid = get_pid(sk->sk_peer_pid);
-		spin_unlock(&sk->sk_peer_lock);
+		return sk_getsockopt_peerpidfd(sk, PIDTYPE_TGID, optval, optlen, len);
 
-		if (!peer_pid)
-			return -ENODATA;
-
-		/* The use of PIDFD_STALE requires stashing of struct pid
-		 * on pidfs with pidfs_register_pid() and only AF_UNIX
-		 * were prepared for this.
-		 */
-		if (sk->sk_family == AF_UNIX)
-			flags = PIDFD_STALE;
-
-		pidfd = pidfd_prepare(peer_pid, flags, &pidfd_file);
-		put_pid(peer_pid);
-		if (pidfd < 0)
-			return pidfd;
-
-		if (copy_to_sockptr(optval, &pidfd, len) ||
-		    copy_to_sockptr(optlen, &len, sizeof(int))) {
-			put_unused_fd(pidfd);
-			fput(pidfd_file);
-
-			return -EFAULT;
-		}
+	case SO_PEERPIDFD_THREAD:
+		/* Only AF_UNIX records the peer's connecting thread. */
+		if (!sk_is_unix(sk))
+			return -EOPNOTSUPP;
 
-		fd_install(pidfd, pidfd_file);
-		return 0;
-	}
+		return sk_getsockopt_peerpidfd(sk, PIDTYPE_PID, optval, optlen, len);
 
 	case SO_PEERGROUPS:
 	{
@@ -2379,7 +2415,7 @@ static void __sk_destruct(struct rcu_head *head)
 
 	/* We do not need to acquire sk->sk_peer_lock, we are the last user. */
 	put_cred(sk->sk_peer_cred);
-	put_pid(sk->sk_peer_pid);
+	put_pids(sk->sk_peer_pid);
 
 	if (likely(sk->sk_net_refcnt)) {
 		put_net_track(net, &sk->ns_tracker);
@@ -3775,7 +3811,7 @@ void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
 	sk->sk_frag.offset	=	0;
 	sk->sk_peek_off		=	-1;
 
-	sk->sk_peer_pid 	=	NULL;
+	memset(sk->sk_peer_pid, 0, sizeof(sk->sk_peer_pid));
 	sk->sk_peer_cred	=	NULL;
 	spin_lock_init(&sk->sk_peer_lock);
 
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 13f9926bf2057..563d9827c5cc5 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -737,57 +737,53 @@ static void unix_release_sock(struct sock *sk, int embrion)
 }
 
 struct unix_peercred {
-	struct pid *peer_pid;
+	DECLARE_PIDS(peer_pid, PIDTYPE_TGID);
 	const struct cred *peer_cred;
 };
 
 static inline int prepare_peercred(struct unix_peercred *peercred)
 {
-	struct pid *pid;
 	int err;
 
-	pid = task_tgid(current);
-	err = pidfs_register_pid(pid);
-	if (likely(!err)) {
-		peercred->peer_pid = get_pid(pid);
-		peercred->peer_cred = get_current_cred();
+	get_task_pids(peercred->peer_pid, current);
+	err = pidfs_register_pids(peercred->peer_pid);
+	if (unlikely(err)) {
+		put_pids(peercred->peer_pid);
+		return err;
 	}
-	return err;
+
+	peercred->peer_cred = get_current_cred();
+	return 0;
 }
 
 static void drop_peercred(struct unix_peercred *peercred)
 {
 	const struct cred *cred = NULL;
-	struct pid *pid = NULL;
 
 	might_sleep();
 
-	swap(peercred->peer_pid, pid);
+	put_pids(peercred->peer_pid);
 	swap(peercred->peer_cred, cred);
-
-	put_pid(pid);
 	put_cred(cred);
 }
 
 static inline void init_peercred(struct sock *sk,
 				 const struct unix_peercred *peercred)
 {
-	sk->sk_peer_pid = peercred->peer_pid;
+	memcpy(sk->sk_peer_pid, peercred->peer_pid, sizeof(sk->sk_peer_pid));
 	sk->sk_peer_cred = peercred->peer_cred;
 }
 
 static void update_peercred(struct sock *sk, struct unix_peercred *peercred)
 {
 	const struct cred *old_cred;
-	struct pid *old_pid;
 
 	spin_lock(&sk->sk_peer_lock);
-	old_pid = sk->sk_peer_pid;
+	swap_pids(sk->sk_peer_pid, peercred->peer_pid);
 	old_cred = sk->sk_peer_cred;
-	init_peercred(sk, peercred);
+	sk->sk_peer_cred = peercred->peer_cred;
 	spin_unlock(&sk->sk_peer_lock);
 
-	peercred->peer_pid = old_pid;
 	peercred->peer_cred = old_cred;
 }
 
@@ -796,14 +792,14 @@ static void copy_peercred(struct sock *sk, struct sock *peersk)
 	lockdep_assert_held(&unix_sk(peersk)->lock);
 
 	spin_lock(&sk->sk_peer_lock);
-	sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
+	get_pids(sk->sk_peer_pid, peersk->sk_peer_pid);
 	sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
 	spin_unlock(&sk->sk_peer_lock);
 }
 
 static bool unix_may_passcred(const struct sock *sk)
 {
-	return sk->sk_scm_credentials || sk->sk_scm_pidfd;
+	return sk->sk_scm_credentials || sk_scm_pidfd_wanted(sk);
 }
 
 static int unix_listen(struct socket *sock, int backlog)
@@ -1060,6 +1056,7 @@ static bool unix_bpf_bypass_getsockopt(int level, int optname)
 	if (level == SOL_SOCKET) {
 		switch (optname) {
 		case SO_PEERPIDFD:
+		case SO_PEERPIDFD_THREAD:
 			return true;
 		default:
 			return false;
@@ -1973,7 +1970,7 @@ static void unix_destruct_scm(struct sk_buff *skb)
 {
 	struct scm_cookie scm = {};
 
-	swap(scm.pid, UNIXCB(skb).pid);
+	swap_pids(scm.pid, UNIXCB(skb).pid);
 
 	if (UNIXCB(skb).fp)
 		unix_detach_fds(&scm, skb);
@@ -1991,7 +1988,7 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
 {
 	int err = 0;
 
-	UNIXCB(skb).pid = get_pid(scm->pid);
+	get_pids(UNIXCB(skb).pid, scm->pid);
 	UNIXCB(skb).uid = scm->creds.uid;
 	UNIXCB(skb).gid = scm->creds.gid;
 	UNIXCB(skb).fp = NULL;
@@ -2005,7 +2002,10 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
 
 static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)
 {
-	scm_set_cred(scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
+	get_pids(scm->pid, UNIXCB(skb).pid);
+	scm->creds.pid = pid_vnr(scm->pid[PIDTYPE_TGID]);
+	scm->creds.uid = UNIXCB(skb).uid;
+	scm->creds.gid = UNIXCB(skb).gid;
 	unix_set_secdata(scm, skb);
 }
 
@@ -2025,30 +2025,35 @@ static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)
 static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
 				const struct sock *other)
 {
-	if (UNIXCB(skb).pid)
+	if (UNIXCB(skb).pid[PIDTYPE_TGID])
 		return 0;
 
 	if (unix_may_passcred(sk) || unix_may_passcred(other) ||
 	    !other->sk_socket) {
-		struct pid *pid;
 		int err;
 
-		pid = task_tgid(current);
-		err = pidfs_register_pid(pid);
-		if (unlikely(err))
+		get_task_pids(UNIXCB(skb).pid, current);
+		err = pidfs_register_pids(UNIXCB(skb).pid);
+		if (unlikely(err)) {
+			put_pids(UNIXCB(skb).pid);
 			return err;
+		}
 
-		UNIXCB(skb).pid = get_pid(pid);
 		current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
 	}
 
 	return 0;
 }
 
-static bool unix_skb_scm_eq(struct sk_buff *skb,
+static bool unix_skb_scm_eq(const struct sock *sk, struct sk_buff *skb,
 			    struct scm_cookie *scm)
 {
-	return UNIXCB(skb).pid == scm->pid &&
+	/* Only a thread pidfd receiver can tell threads of one process apart. */
+	if (sk->sk_scm_pidfd_thread &&
+	    UNIXCB(skb).pid[PIDTYPE_PID] != scm->pid[PIDTYPE_PID])
+		return false;
+
+	return UNIXCB(skb).pid[PIDTYPE_TGID] == scm->pid[PIDTYPE_TGID] &&
 	       uid_eq(UNIXCB(skb).uid, scm->creds.uid) &&
 	       gid_eq(UNIXCB(skb).gid, scm->creds.gid) &&
 	       unix_secdata_eq(scm, skb);
@@ -3026,7 +3031,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 
 		if (check_creds) {
 			/* Never glue messages from different writers */
-			if (!unix_skb_scm_eq(skb, &scm))
+			if (!unix_skb_scm_eq(sk, skb, &scm))
 				break;
 		} else if (unix_may_passcred(sk)) {
 			/* Copy credentials */
diff --git a/net/unix/af_unix.h b/net/unix/af_unix.h
index 8119dbeef3a3c..402742895acca 100644
--- a/net/unix/af_unix.h
+++ b/net/unix/af_unix.h
@@ -2,6 +2,7 @@
 #ifndef __AF_UNIX_H
 #define __AF_UNIX_H
 
+#include <linux/pid_types.h>
 #include <linux/uidgid.h>
 
 #define UNIX_HASH_MOD	(256 - 1)
@@ -11,7 +12,7 @@
 struct sock *unix_peer_get(struct sock *sk);
 
 struct unix_skb_parms {
-	struct pid		*pid;		/* skb credentials	*/
+	DECLARE_PIDS(pid, PIDTYPE_TGID);	/* skb credentials by pid type */
 	kuid_t			uid;
 	kgid_t			gid;
 	struct scm_fp_list	*fp;		/* Passed files		*/
diff --git a/tools/testing/selftests/coredump/coredump_socket_test.c b/tools/testing/selftests/coredump/coredump_socket_test.c
index 422728f632ca6..ec73bb690bbcb 100644
--- a/tools/testing/selftests/coredump/coredump_socket_test.c
+++ b/tools/testing/selftests/coredump/coredump_socket_test.c
@@ -592,6 +592,181 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)
 	wait_and_check_coredump_server(pid_coredump_server, _metadata, self);
 }
 
+static bool check_coredump_info(const struct pidfd_info *info, const char *what)
+{
+	if (!(info->mask & PIDFD_INFO_COREDUMP)) {
+		fprintf(stderr, "%s: PIDFD_INFO_COREDUMP not set in mask\n", what);
+		return false;
+	}
+
+	if (!(info->coredump_mask & PIDFD_COREDUMPED)) {
+		fprintf(stderr, "%s: PIDFD_COREDUMPED not set in coredump_mask\n", what);
+		return false;
+	}
+
+	if (!(info->mask & PIDFD_INFO_COREDUMP_SIGNAL) || info->coredump_signal != SIGSEGV) {
+		fprintf(stderr, "%s: coredump_signal=%d, expected SIGSEGV=%d\n",
+			what, info->coredump_signal, SIGSEGV);
+		return false;
+	}
+
+	if (!(info->mask & PIDFD_INFO_COREDUMP_CODE) || info->coredump_code != SEGV_MAPERR) {
+		fprintf(stderr, "%s: coredump_code=%d, expected SEGV_MAPERR=%d\n",
+			what, info->coredump_code, SEGV_MAPERR);
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * Test: PIDFD_INFO_COREDUMP on the dumping thread's pidfd
+ *
+ * Crash from a non-leader thread and verify that the pidfd from
+ * SO_PEERPIDFD_THREAD refers to that thread and reports the coredump
+ * like the thread-group leader's pidfd from SO_PEERPIDFD does.
+ */
+TEST_F(coredump, socket_coredump_thread)
+{
+	int pidfd, ret, status;
+	pid_t pid, pid_coredump_server;
+	struct pidfd_info info = {};
+	int ipc_sockets[2];
+	char c;
+
+	ASSERT_TRUE(set_core_pattern("@/tmp/coredump.socket"));
+
+	ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
+	ASSERT_EQ(ret, 0);
+
+	pid_coredump_server = fork();
+	ASSERT_GE(pid_coredump_server, 0);
+	if (pid_coredump_server == 0) {
+		int fd_server = -1, fd_coredump = -1, fd_peer_pidfd = -1;
+		int fd_thread_pidfd = -1, fd_core_file = -1;
+		struct pidfd_info thread_info = {};
+		int exit_code = EXIT_FAILURE;
+
+		close(ipc_sockets[0]);
+
+		fd_server = create_and_listen_unix_socket("/tmp/coredump.socket");
+		if (fd_server < 0) {
+			fprintf(stderr, "socket_coredump_thread: listen socket failed: %m\n");
+			goto out;
+		}
+
+		if (write_nointr(ipc_sockets[1], "1", 1) < 0) {
+			fprintf(stderr, "socket_coredump_thread: ipc write failed: %m\n");
+			goto out;
+		}
+
+		close(ipc_sockets[1]);
+
+		fd_coredump = accept4(fd_server, NULL, NULL, SOCK_CLOEXEC);
+		if (fd_coredump < 0) {
+			fprintf(stderr, "socket_coredump_thread: accept4 failed: %m\n");
+			goto out;
+		}
+
+		fd_peer_pidfd = get_peer_pidfd(fd_coredump);
+		if (fd_peer_pidfd < 0) {
+			fprintf(stderr, "socket_coredump_thread: get_peer_pidfd failed\n");
+			goto out;
+		}
+
+		fd_thread_pidfd = get_peer_pidfd_thread(fd_coredump);
+		if (fd_thread_pidfd < 0) {
+			fprintf(stderr, "socket_coredump_thread: get_peer_pidfd_thread failed\n");
+			goto out;
+		}
+
+		if (!get_pidfd_info(fd_peer_pidfd, &info) ||
+		    !get_pidfd_info(fd_thread_pidfd, &thread_info)) {
+			fprintf(stderr, "socket_coredump_thread: get_pidfd_info failed\n");
+			goto out;
+		}
+
+		/* The peer is the thread-group leader, the dumping thread is not. */
+		if (info.pid != info.tgid || thread_info.tgid != info.tgid ||
+		    thread_info.pid == thread_info.tgid) {
+			fprintf(stderr, "socket_coredump_thread: unexpected ids %d/%d and %d/%d\n",
+				info.pid, info.tgid, thread_info.pid, thread_info.tgid);
+			goto out;
+		}
+
+		if (!check_coredump_info(&info, "SO_PEERPIDFD") ||
+		    !check_coredump_info(&thread_info, "SO_PEERPIDFD_THREAD"))
+			goto out;
+
+		fd_core_file = open_coredump_tmpfile(self->fd_tmpfs_detached);
+		if (fd_core_file < 0) {
+			fprintf(stderr, "socket_coredump_thread: core tmpfile failed: %m\n");
+			goto out;
+		}
+
+		for (;;) {
+			char buffer[4096];
+			ssize_t bytes_read, bytes_write;
+
+			bytes_read = read(fd_coredump, buffer, sizeof(buffer));
+			if (bytes_read < 0) {
+				fprintf(stderr, "socket_coredump_thread: core read failed: %m\n");
+				goto out;
+			}
+
+			if (bytes_read == 0)
+				break;
+
+			bytes_write = write(fd_core_file, buffer, bytes_read);
+			if (bytes_read != bytes_write) {
+				fprintf(stderr, "socket_coredump_thread: core write %zd/%zd: %m\n",
+					bytes_read, bytes_write);
+				goto out;
+			}
+		}
+
+		exit_code = EXIT_SUCCESS;
+		fprintf(stderr, "socket_coredump_thread: completed successfully\n");
+out:
+		if (fd_core_file >= 0)
+			close(fd_core_file);
+		if (fd_thread_pidfd >= 0)
+			close(fd_thread_pidfd);
+		if (fd_peer_pidfd >= 0)
+			close(fd_peer_pidfd);
+		if (fd_coredump >= 0)
+			close(fd_coredump);
+		if (fd_server >= 0)
+			close(fd_server);
+		_exit(exit_code);
+	}
+	self->pid_coredump_server = pid_coredump_server;
+
+	EXPECT_EQ(close(ipc_sockets[1]), 0);
+	ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
+	EXPECT_EQ(close(ipc_sockets[0]), 0);
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+	if (pid == 0)
+		crashing_child_thread();
+
+	pidfd = sys_pidfd_open(pid, 0);
+	ASSERT_GE(pidfd, 0);
+
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFSIGNALED(status));
+	ASSERT_EQ(WTERMSIG(status), SIGSEGV);
+	ASSERT_TRUE(WCOREDUMP(status));
+
+	ASSERT_TRUE(get_pidfd_info(pidfd, &info));
+	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_COREDUMP));
+	ASSERT_TRUE(!!(info.coredump_mask & PIDFD_COREDUMPED));
+	ASSERT_EQ(info.coredump_signal, SIGSEGV);
+
+	wait_and_check_coredump_server(pid_coredump_server, _metadata, self);
+}
+
 /*
  * Test: PIDFD_INFO_COREDUMP_SIGNAL via simple socket coredump with SIGABRT
  *
diff --git a/tools/testing/selftests/coredump/coredump_test.h b/tools/testing/selftests/coredump/coredump_test.h
index ed47f01fa53c5..4212656e31f01 100644
--- a/tools/testing/selftests/coredump/coredump_test.h
+++ b/tools/testing/selftests/coredump/coredump_test.h
@@ -27,10 +27,12 @@ FIXTURE(coredump)
 /* Shared helper function declarations */
 void *do_nothing(void *arg);
 void crashing_child(void);
+void crashing_child_thread(void);
 int create_detached_tmpfs(void);
 int create_and_listen_unix_socket(const char *path);
 bool set_core_pattern(const char *pattern);
 int get_peer_pidfd(int fd);
+int get_peer_pidfd_thread(int fd);
 bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info);
 
 /* Inline helper that uses harness types */
diff --git a/tools/testing/selftests/coredump/coredump_test_helpers.c b/tools/testing/selftests/coredump/coredump_test_helpers.c
index 2a20faf9cb0ad..36306069f62e5 100644
--- a/tools/testing/selftests/coredump/coredump_test_helpers.c
+++ b/tools/testing/selftests/coredump/coredump_test_helpers.c
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <sys/epoll.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
@@ -38,6 +39,10 @@ struct _fixture_coredump_data {
 
 #define NUM_THREAD_SPAWN 128
 
+#ifndef SO_PEERPIDFD_THREAD
+#define SO_PEERPIDFD_THREAD 87
+#endif
+
 void *do_nothing(void *arg)
 {
 	(void)arg;
@@ -59,6 +64,36 @@ void crashing_child(void)
 	i = *(volatile int *)NULL;
 }
 
+static void *crashing_thread(void *arg)
+{
+	int *p;
+
+	(void)arg;
+
+	/* crash on purpose with SEGV_MAPERR */
+	p = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
+		 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (p == MAP_FAILED)
+		return NULL;
+	munmap(p, PAGE_SIZE);
+	*p = 0;
+
+	return NULL;
+}
+
+void crashing_child_thread(void)
+{
+	pthread_t thread;
+	int i;
+
+	for (i = 0; i < NUM_THREAD_SPAWN; ++i)
+		pthread_create(&thread, NULL, do_nothing, NULL);
+
+	/* crash from a non-leader thread */
+	pthread_create(&thread, NULL, crashing_thread, NULL);
+	pause();
+}
+
 int create_detached_tmpfs(void)
 {
 	int fd_context, fd_tmpfs;
@@ -138,6 +173,20 @@ int get_peer_pidfd(int fd)
 	return fd_peer_pidfd;
 }
 
+int get_peer_pidfd_thread(int fd)
+{
+	int fd_peer_pidfd;
+	socklen_t fd_peer_pidfd_len = sizeof(fd_peer_pidfd);
+	int ret = getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD_THREAD, &fd_peer_pidfd,
+			     &fd_peer_pidfd_len);
+	if (ret < 0) {
+		fprintf(stderr, "%s: getsockopt(SO_PEERPIDFD_THREAD) failed: %m\n", __func__);
+		return -1;
+	}
+	fprintf(stderr, "%s: successfully retrieved pidfd %d\n", __func__, fd_peer_pidfd);
+	return fd_peer_pidfd;
+}
+
 bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info)
 {
 	int ret;
diff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile
index a66f10fb0c231..45b841758f1bf 100644
--- a/tools/testing/selftests/net/af_unix/Makefile
+++ b/tools/testing/selftests/net/af_unix/Makefile
@@ -23,6 +23,8 @@ TEST_GEN_FILES := scm_rights_denial_lsm.bpf.o
 include ../../lib.mk
 include ../bpf.mk
 
+$(OUTPUT)/scm_pidfd: CFLAGS += -pthread
+
 $(OUTPUT)/scm_rights_denial_lsm: $(BPFOBJ)
 $(OUTPUT)/scm_rights_denial_lsm: CFLAGS += -I$(SCRATCH_DIR)/include
 $(OUTPUT)/scm_rights_denial_lsm: LDLIBS += -lelf -lz
diff --git a/tools/testing/selftests/net/af_unix/scm_pidfd.c b/tools/testing/selftests/net/af_unix/scm_pidfd.c
index 2c18b92a26035..bbbd5e7b4fa78 100644
--- a/tools/testing/selftests/net/af_unix/scm_pidfd.c
+++ b/tools/testing/selftests/net/af_unix/scm_pidfd.c
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
+#include <pthread.h>
 #include <sys/un.h>
 #include <sys/signal.h>
 #include <sys/types.h>
@@ -27,6 +28,14 @@
 #define SCM_PIDFD 0x04
 #endif
 
+#ifndef SO_PASSPIDFD_THREAD
+#define SO_PASSPIDFD_THREAD 86
+#endif
+
+#ifndef SO_PEERPIDFD_THREAD
+#define SO_PEERPIDFD_THREAD 87
+#endif
+
 #define CHILD_EXIT_CODE_OK 123
 
 static void child_die()
@@ -553,4 +562,361 @@ TEST_F(scm_pidfd, test)
 	close(pfd);
 }
 
+struct thread_ids {
+	pid_t pid;
+	pid_t tid;
+};
+
+static void *send_ids_thread(void *arg)
+{
+	int fd = *(int *)arg;
+	struct thread_ids ids = {
+		.pid = getpid(),
+		.tid = gettid(),
+	};
+	char sync;
+
+	if (send(fd, &ids, sizeof(ids), 0) != sizeof(ids))
+		return (void *)1;
+
+	/* stay alive until the receiver has looked at our pidfd */
+	if (read(fd, &sync, 1) != 1)
+		return (void *)1;
+
+	return NULL;
+}
+
+static void *send_ids_creds_thread(void *arg)
+{
+	int fd = *(int *)arg;
+	struct thread_ids ids = {
+		.pid = getpid(),
+		.tid = gettid(),
+	};
+	struct ucred ucred = {
+		.pid = getpid(),
+		.uid = getuid(),
+		.gid = getgid(),
+	};
+	char control[CMSG_SPACE(sizeof(ucred))] = { 0 };
+	struct iovec iov;
+	struct msghdr msg = { 0 };
+	struct cmsghdr *cmsg;
+	char sync;
+
+	iov.iov_base = &ids;
+	iov.iov_len = sizeof(ids);
+
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = control;
+	msg.msg_controllen = sizeof(control);
+
+	cmsg = CMSG_FIRSTHDR(&msg);
+	cmsg->cmsg_level = SOL_SOCKET;
+	cmsg->cmsg_type = SCM_CREDENTIALS;
+	cmsg->cmsg_len = CMSG_LEN(sizeof(ucred));
+	memcpy(CMSG_DATA(cmsg), &ucred, sizeof(ucred));
+
+	if (sendmsg(fd, &msg, 0) != sizeof(ids))
+		return (void *)1;
+
+	if (read(fd, &sync, 1) != 1)
+		return (void *)1;
+
+	return NULL;
+}
+
+static void thread_client(int fd, int syncfd, void *(*sender)(void *))
+{
+	pthread_t thread;
+	void *ret;
+	char sync;
+
+	/* wait until the receiver enabled SO_PASSPIDFD */
+	if (read(syncfd, &sync, 1) != 1)
+		child_die();
+
+	if (pthread_create(&thread, NULL, sender, &fd))
+		child_die();
+
+	if (pthread_join(thread, &ret) || ret)
+		child_die();
+
+	exit(0);
+}
+
+static int recv_pidfd(int fd, struct thread_ids *ids)
+{
+	char control[CMSG_SPACE(sizeof(int))] = { 0 };
+	struct iovec iov;
+	struct msghdr msg = { 0 };
+	struct cmsghdr *cmsg;
+	int pidfd = -1;
+
+	iov.iov_base = ids;
+	iov.iov_len = sizeof(*ids);
+
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = control;
+	msg.msg_controllen = sizeof(control);
+
+	if (recvmsg(fd, &msg, 0) != sizeof(*ids)) {
+		log_err("recvmsg");
+		return -1;
+	}
+
+	if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
+		log_err("recvmsg: truncated");
+		return -1;
+	}
+
+	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+		if (cmsg->cmsg_level == SOL_SOCKET &&
+		    cmsg->cmsg_type == SCM_PIDFD)
+			memcpy(&pidfd, CMSG_DATA(cmsg), sizeof(pidfd));
+	}
+
+	return pidfd;
+}
+
+static int thread_pidfd_flow(void *(*sender)(void *), int optname,
+			     struct thread_ids *ids, struct pidfd_info *info)
+{
+	int sk[2];
+	int syncpipe[2];
+	int pidfd;
+	int child_status = 0;
+	pid_t child;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sk))
+		return -1;
+
+	if (pipe(syncpipe))
+		return -1;
+
+	child = fork();
+	if (child < 0)
+		return -1;
+
+	if (child == 0) {
+		close(sk[0]);
+		close(syncpipe[1]);
+		thread_client(sk[1], syncpipe[0], sender);
+	}
+	close(sk[1]);
+	close(syncpipe[0]);
+
+	int on = 1;
+
+	if (setsockopt(sk[0], SOL_SOCKET, optname, &on, sizeof(on))) {
+		log_err("Failed to set pidfd passing option");
+		return -1;
+	}
+
+	if (write(syncpipe[1], "1", 1) != 1)
+		return -1;
+	close(syncpipe[1]);
+
+	pidfd = recv_pidfd(sk[0], ids);
+	if (pidfd < 0)
+		return -1;
+
+	info->mask = PIDFD_INFO_PID;
+	if (ioctl(pidfd, PIDFD_GET_INFO, info)) {
+		log_err("ioctl(PIDFD_GET_INFO)");
+		close(pidfd);
+		return -1;
+	}
+	close(pidfd);
+
+	/* release the sending thread */
+	if (write(sk[0], "x", 1) != 1)
+		return -1;
+	close(sk[0]);
+
+	waitpid(child, &child_status, 0);
+	if (!WIFEXITED(child_status) || WEXITSTATUS(child_status))
+		return -1;
+
+	return 0;
+}
+
+static int sockopt_set(int fd, int optname, int val)
+{
+	return setsockopt(fd, SOL_SOCKET, optname, &val, sizeof(val));
+}
+
+static int sockopt_get(int fd, int optname)
+{
+	socklen_t len = sizeof(int);
+	int val = -1;
+
+	if (getsockopt(fd, SOL_SOCKET, optname, &val, &len))
+		return -1;
+
+	return val;
+}
+
+TEST(scm_pidfd_setsockopt_values)
+{
+	int sk[2];
+
+	ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sk));
+
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 1));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));
+
+	/* The option set last wins. */
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD, 1));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 1));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));
+
+	/* Disabling one option leaves the other alone. */
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD, 0));
+	ASSERT_EQ(1, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+	ASSERT_EQ(0, sockopt_set(sk[0], SO_PASSPIDFD_THREAD, 0));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD));
+	ASSERT_EQ(0, sockopt_get(sk[0], SO_PASSPIDFD_THREAD));
+
+	close(sk[0]);
+	close(sk[1]);
+}
+
+TEST(scm_pidfd_thread)
+{
+	struct thread_ids ids;
+	struct pidfd_info info;
+
+	ASSERT_EQ(0, thread_pidfd_flow(send_ids_thread, SO_PASSPIDFD_THREAD,
+				       &ids, &info));
+	ASSERT_NE(ids.pid, ids.tid);
+	EXPECT_EQ(ids.tid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+}
+
+TEST(scm_pidfd_thread_group)
+{
+	struct thread_ids ids;
+	struct pidfd_info info;
+
+	ASSERT_EQ(0, thread_pidfd_flow(send_ids_thread, SO_PASSPIDFD,
+				       &ids, &info));
+	ASSERT_NE(ids.pid, ids.tid);
+	EXPECT_EQ(ids.pid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+}
+
+TEST(scm_pidfd_thread_creds)
+{
+	struct thread_ids ids;
+	struct pidfd_info info;
+
+	ASSERT_EQ(0, thread_pidfd_flow(send_ids_creds_thread, SO_PASSPIDFD_THREAD,
+				       &ids, &info));
+	ASSERT_NE(ids.pid, ids.tid);
+	EXPECT_EQ(ids.tid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+}
+
+static void *peer_connect_thread(void *arg)
+{
+	struct sock_addr *sa = arg;
+	struct thread_ids ids = {
+		.pid = getpid(),
+		.tid = gettid(),
+	};
+	int fd;
+	char sync;
+
+	fd = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (fd < 0)
+		return (void *)1;
+
+	if (connect(fd, (struct sockaddr *)&sa->listen_addr, sa->addrlen))
+		return (void *)1;
+
+	if (send(fd, &ids, sizeof(ids), 0) != sizeof(ids))
+		return (void *)1;
+
+	/* stay alive until the server has looked at our pidfd */
+	if (read(fd, &sync, 1) != 1)
+		return (void *)1;
+
+	close(fd);
+	return NULL;
+}
+
+static int peer_pidfd_info(int fd, int optname, struct pidfd_info *info)
+{
+	int pidfd;
+	socklen_t len = sizeof(pidfd);
+
+	if (getsockopt(fd, SOL_SOCKET, optname, &pidfd, &len)) {
+		log_err("getsockopt(SO_PEERPIDFD*)");
+		return -1;
+	}
+
+	info->mask = PIDFD_INFO_PID;
+	if (ioctl(pidfd, PIDFD_GET_INFO, info)) {
+		log_err("ioctl(PIDFD_GET_INFO)");
+		close(pidfd);
+		return -1;
+	}
+
+	close(pidfd);
+	return 0;
+}
+
+/* SO_PEERPIDFD_THREAD returns a pidfd for the peer's connecting thread. */
+TEST(so_peerpidfd_thread)
+{
+	struct sock_addr sa;
+	struct thread_ids ids;
+	struct pidfd_info info;
+	pthread_t thread;
+	void *tret;
+	int server, cfd;
+
+	server = socket(AF_UNIX, SOCK_STREAM, 0);
+	ASSERT_LE(0, server);
+
+	fill_sockaddr(&sa, true);
+	ASSERT_EQ(0, bind(server, (struct sockaddr *)&sa.listen_addr, sa.addrlen));
+	ASSERT_EQ(0, listen(server, 1));
+
+	ASSERT_EQ(0, pthread_create(&thread, NULL, peer_connect_thread, &sa));
+
+	cfd = accept(server, NULL, NULL);
+	ASSERT_LE(0, cfd);
+
+	ASSERT_EQ(sizeof(ids), recv(cfd, &ids, sizeof(ids), MSG_WAITALL));
+	ASSERT_NE(ids.pid, ids.tid);
+
+	/* SO_PEERPIDFD refers to the peer's thread-group. */
+	ASSERT_EQ(0, peer_pidfd_info(cfd, SO_PEERPIDFD, &info));
+	EXPECT_EQ(ids.pid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+
+	/* SO_PEERPIDFD_THREAD refers to the connecting thread. */
+	ASSERT_EQ(0, peer_pidfd_info(cfd, SO_PEERPIDFD_THREAD, &info));
+	EXPECT_EQ(ids.tid, info.pid);
+	EXPECT_EQ(ids.pid, info.tgid);
+
+	/* release the connecting thread */
+	ASSERT_EQ(1, write(cfd, "x", 1));
+	ASSERT_EQ(0, pthread_join(thread, &tret));
+	ASSERT_EQ(NULL, tret);
+
+	close(cfd);
+	close(server);
+}
+
 TEST_HARNESS_MAIN

39/3 2026/08/31 12:34 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 22863
							output: 33
							thoughts: 779
						

Thoughts:
**Analyzing the Patch for Uninitialized Memory Risks**

Okay, let's dive into this patch and figure out if it introduces any uninitialized memory vulnerabilities that would necessitate running KMSAN. My first step is to thoroughly understand exactly what this patch is doing.

Alright, so it's adding `SO_PASSPIDFD_THREAD` and `SO_PEERPIDFD_THREAD` socket options. That seems straightforward enough, both in the uapi headers and asm headers. It's also updating several key structures: `coredump_params`, `scm_cookie`, `sock`, `unix_peercred`, and `unix_skb_parms`. It appears that these structures are being modified to store arrays of `struct pid *`, indexed by PID type, using `DECLARE_PIDS` macro, using the new `PIDTYPE_TGID` type.

Then there are new helper functions in `include/linux/pid.h` and `include/linux/sched/signal.h`.  We have `DECLARE_PIDS`, which expands to a `struct pid *name[... ]` array, and functions like `__get_pids`, `__put_pids`, `__swap_pids`, `__pids_equal`, `__task_pids`, `__get_task_pids`, and `__pidfs_register_pids`.  Interesting. Let's see how these are used.

Looking at the socket option updates in `net/core/sock.c`, `SO_PASSPIDFD` and `SO_PASSPIDFD_THREAD` are being added to `sk_setsockopt` and `sk_getsockopt`.  `SO_PEERPIDFD` and `SO_PEERPIDFD_THREAD` are in `sk_getsockopt` via helper functions.  And a `memset` is happening to `sk->sk_peer_pid` in `sock_init_data_uid`.

Furthermore, there are updates for AF_UNIX, focusing on `prepare_peercred`, `init_peercred`, `update_peercred`, `copy_peercred`,  and helper functions around `scm` structures. AF_BLUETOOTH also sees changes, particularly in allocation and credential copying: `bt_sock_alloc`, `bt_accept_enqueue`, `hci_sock_copy_creds`, and `l2cap_sock_get_peer_pid_cb`. Finally, there are changes related to coredumps and pidfs, with new functions like `coredump_parse`, `umh_coredump_setup`, `coredump_sock_connect`, `pidfs_coredump`, and `__pidfs_register_pids`.

Now, the critical part. We need to meticulously scrutinize every instance where memory is allocated, read, copied to userspace, or involved in branching conditions.  The primary concern is ensuring that uninitialized memory is never read, either within the kernel or, more importantly, leaked to userspace.

Let's begin with `DECLARE_PIDS`: `DECLARE_PIDS(name, last)` expands to `struct pid *name[(last) + 1 + BUILD_BUG_ON_ZERO((last) >= PIDTYPE_MAX)]`. We need to be careful with the array sizes. `last` appears to be `PIDTYPE_TGID`. Let me pause and double-check those enum values in `pid_types.h`, and `pid.h`.  I'll grep and read the header files now to be sure of the values to assess potential off-by-one errors.



40/3 2026/08/31 12:34 tool grepper
0m Args:
{
  "Expression": "enum pid_type",
  "PathPrefix": "include/linux/pid_types.h"
}

Results:
{
  "Output": "include/linux/pid_types.h-6-\ninclude/linux/pid_types.h:7:enum pid_type {\ninclude/linux/pid_types.h-8-\tPIDTYPE_PID,\n"
}

41/3 2026/08/31 12:34 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 24510
							output: 35
							thoughts: 15
						

Thoughts:
Here's a summary of my thought process, as if I were the one thinking it:

**Diving into PID Internals**

Okay, time to get into the nitty-gritty of the kernel's PID management. Let's start by cracking open `include/linux/pid_types.h`. This is where the core data structures and definitions related to process IDs are likely housed. Understanding this file is fundamental. I need to refresh my understanding of how the kernel actually *represents* and *uses* PIDs internally. I'm hoping to see the structure of the `pid` type, any related flags, and the overall approach to PID allocation and usage. I expect to see the basic building blocks that support the rest of the PID-related code. This will be the foundation for figuring out how the kernel tracks processes and relates them to userspace.



42/3 2026/08/31 12:34 tool read-file
0m Args:
{
  "File": "include/linux/pid_types.h",
  "FirstLine": 1,
  "LineCount": 25
}

Results:
{
  "Contents": "   1:\t/* SPDX-License-Identifier: GPL-2.0 */\n   2:\t#ifndef _LINUX_PID_TYPES_H\n   3:\t#define _LINUX_PID_TYPES_H\n   4:\t\n   5:\t#include \u003clinux/build_bug.h\u003e\n   6:\t\n   7:\tenum pid_type {\n   8:\t\tPIDTYPE_PID,\n   9:\t\tPIDTYPE_TGID,\n  10:\t\tPIDTYPE_PGID,\n  11:\t\tPIDTYPE_SID,\n  12:\t\tPIDTYPE_MAX,\n  13:\t};\n  14:\t\n  15:\tstruct pid;\n  16:\t\n  17:\t/* An array of struct pid indexed by pid type, PIDTYPE_PID up to @last. */\n  18:\t#define DECLARE_PIDS(name, last)\t\t\t\t\t\\\n  19:\t\tstruct pid *name[(last) + 1 + BUILD_BUG_ON_ZERO((last) \u003e= PIDTYPE_MAX)]\n  20:\t\n  21:\tstruct pid_namespace;\n  22:\textern struct pid_namespace init_pid_ns;\n  23:\t\n  24:\t#endif /* _LINUX_PID_TYPES_H */\n"
}

43/3 2026/08/31 12:34 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 25062
							output: 19
							thoughts: 491
						

Thoughts:
**Diving into scm_cookie Initialization and Usage**

Okay, let's take a look at how `struct scm_cookie` is initialized and used throughout the codebase. I'm focusing on the details of how the PID information is handled, especially considering potential reference counting or leaks. I just looked at `net/core/scm.c` and saw `scm_replace_pid`. Hmm, in `scm_replace_pid`, it looks like there's some kind of PID registration using `pidfs_register_pid`. The comments say "drop all previous references," which makes me wonder if there's an explicit unregistration or refcount decrement happening, or if the registrations themselves handle reference counts. That's a point to investigate more closely, because I need to make sure this is done correctly to avoid memory corruption.

Then I jumped to `include/net/scm.h` to look at `scm_set_cred`. The code looks straightforward, setting the TGID, PID, UID, and GID. But wait, where is the `PIDTYPE_PID` field being set in `scm_set_cred`? I need to understand that. I'm a bit concerned because it seems like the `PIDTYPE_PID` field is being set in `scm_replace_pid` but not here. That seems like an inconsistency to me, and I need to figure out the exact initialization path.

I'm thinking, where is `scm_set_cred` even *called*? How is the `scm_cookie` allocated and initialized in the first place? I need to track down all the calls to `scm_set_cred` and also all the places where `struct scm_cookie` is declared. A complete picture of the allocation, initialization, and usage is essential here, so I'll need to go through all instances across the codebase.



44/3 2026/08/31 12:35 tool grepper
0m Args:
{
  "Expression": "scm_set_cred"
}

Results:
{
  "Output": "include/net/scm.h=65=static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)\n--\ninclude/net/scm.h-68-\ninclude/net/scm.h:69:static __inline__ void scm_set_cred(struct scm_cookie *scm,\ninclude/net/scm.h-70-\t\t\t\t    struct pid *pid, kuid_t uid, kgid_t gid)\n--\ninclude/net/scm.h=90=static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,\n--\ninclude/net/scm.h-96-\tif (forcecreds)\ninclude/net/scm.h:97:\t\tscm_set_cred(scm, task_tgid(current), current_uid(), current_gid());\ninclude/net/scm.h-98-\tunix_get_peersec_dgram(sock, scm);\n"
}

45/3 2026/08/31 12:35 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 26217
							output: 20
							thoughts: 44
						

Thoughts:
**Investigating the `scm` Structure's Usage**

Okay, so I need to understand how the `scm` data structure is being used within the kernel, specifically in relation to `scm_send` and `scm_set_cred`. The goal here is to get a clearer picture of how these system call mechanisms are structured, especially looking for potential points where that `scm_cookie` structure might be used.

My immediate plan is to search the codebase for where the `scm` variable is defined within any function that has calls to `scm_send` or `scm_set_cred`, this way I can identify relevant data flows. After that I think I will search the entire kernel codebase for occurrences of `struct scm_cookie`, with the idea of pinpointing every instance where that structure is declared or referenced. This should paint a much broader picture of its purpose and impact. I’m hoping to glean more about the architecture of these particular system call interactions.



46/3 2026/08/31 12:35 tool grepper
0m Args:
{
  "Expression": "struct scm_cookie "
}

Results:
{
  "Output": "Documentation/security/lsm.rst=91=network device security information, security fields were added to\nDocumentation/security/lsm.rst-92-:c:type:`struct sk_buff \u003csk_buff\u003e` and\nDocumentation/security/lsm.rst:93::c:type:`struct scm_cookie \u003cscm_cookie\u003e`.\nDocumentation/security/lsm.rst-94-Unlike the other security module data, the data used here is a\n--\nDocumentation/translations/zh_CN/security/lsm.rst=68=struct shmid_kernel的定义被移动到头文件中( ``include/linux/msg.h`` 和\n--\nDocumentation/translations/zh_CN/security/lsm.rst-71-对于数据包和网络设备的安全信息,安全域被添加到 :c:type:\nDocumentation/translations/zh_CN/security/lsm.rst:72:`struct sk_buff \u003csk_buff\u003e` 和 :c:type: `struct scm_cookie \u003cscm_cookie\u003e` 中。\nDocumentation/translations/zh_CN/security/lsm.rst-73-与其他安全模块数据不同,这里使用的数据是一个32位整数。安全模块需要将这些值\n--\ninclude/net/scm.h=30=struct scm_fp_list {\n--\ninclude/net/scm.h-43-\ninclude/net/scm.h:44:struct scm_cookie {\ninclude/net/scm.h-45-\tDECLARE_PIDS(pid, PIDTYPE_TGID);\t/* Skb credentials by pid type */\n--\ninclude/net/scm.h-52-\ninclude/net/scm.h:53:void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm, bool notrunc);\ninclude/net/scm.h:54:void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm, bool notrunc);\ninclude/net/scm.h:55:int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);\ninclude/net/scm.h:56:void __scm_destroy(struct scm_cookie *scm);\ninclude/net/scm.h-57-struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl);\n--\ninclude/net/scm.h-59-#ifdef CONFIG_SECURITY_NETWORK\ninclude/net/scm.h:60:static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)\ninclude/net/scm.h-61-{\n--\ninclude/net/scm.h-64-#else\ninclude/net/scm.h:65:static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)\ninclude/net/scm.h-66-{ }\n--\ninclude/net/scm.h-68-\ninclude/net/scm.h:69:static __inline__ void scm_set_cred(struct scm_cookie *scm,\ninclude/net/scm.h-70-\t\t\t\t    struct pid *pid, kuid_t uid, kgid_t gid)\n--\ninclude/net/scm.h-77-\ninclude/net/scm.h:78:static __inline__ void scm_destroy_cred(struct scm_cookie *scm)\ninclude/net/scm.h-79-{\n--\ninclude/net/scm.h-82-\ninclude/net/scm.h:83:static __inline__ void scm_destroy(struct scm_cookie *scm)\ninclude/net/scm.h-84-{\n--\ninclude/net/scm.h=90=static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,\ninclude/net/scm.h:91:\t\t\t       struct scm_cookie *scm, bool forcecreds)\ninclude/net/scm.h-92-{\n--\ninclude/net/scm.h=104=void scm_recv(struct socket *sock, struct msghdr *msg,\ninclude/net/scm.h:105:\t      struct scm_cookie *scm, int flags);\ninclude/net/scm.h-106-void scm_recv_unix(struct socket *sock, struct msghdr *msg,\ninclude/net/scm.h:107:\t\t   struct scm_cookie *scm, int flags);\ninclude/net/scm.h-108-\n--\nnet/bluetooth/hci_sock.c=1556=static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/bluetooth/hci_sock.c-1558-{\nnet/bluetooth/hci_sock.c:1559:\tstruct scm_cookie scm;\nnet/bluetooth/hci_sock.c-1560-\tstruct sock *sk = sock-\u003esk;\n--\nnet/compat.c=282=static int scm_max_fds_compat(struct msghdr *msg)\n--\nnet/compat.c-288-\nnet/compat.c:289:void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm, bool notrunc)\nnet/compat.c-290-{\n--\nnet/core/scm.c=69=static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)\n--\nnet/core/scm.c-134-\nnet/core/scm.c:135:void __scm_destroy(struct scm_cookie *scm)\nnet/core/scm.c-136-{\n--\nnet/core/scm.c=148=EXPORT_SYMBOL(__scm_destroy);\nnet/core/scm.c-149-\nnet/core/scm.c:150:static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)\nnet/core/scm.c-151-{\n--\nnet/core/scm.c-177-\nnet/core/scm.c:178:int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)\nnet/core/scm.c-179-{\n--\nnet/core/scm.c=367=int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,\n--\nnet/core/scm.c-390-\nnet/core/scm.c:391:void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm, bool notrunc)\nnet/core/scm.c-392-{\n--\nnet/core/scm.c=467=EXPORT_SYMBOL(scm_fp_dup);\n--\nnet/core/scm.c-469-#ifdef CONFIG_SECURITY_NETWORK\nnet/core/scm.c:470:static void scm_passec(struct sock *sk, struct msghdr *msg, struct scm_cookie *scm)\nnet/core/scm.c-471-{\n--\nnet/core/scm.c=487=static bool scm_has_secdata(struct sock *sk)\n--\nnet/core/scm.c-491-#else\nnet/core/scm.c:492:static void scm_passec(struct sock *sk, struct msghdr *msg, struct scm_cookie *scm)\nnet/core/scm.c-493-{\n--\nnet/core/scm.c=502=static void scm_pidfd_recv(struct sock *sk, struct msghdr *msg,\nnet/core/scm.c:503:\t\t\t   struct scm_cookie *scm)\nnet/core/scm.c-504-{\n--\nnet/core/scm.c=545=static bool __scm_recv_common(struct sock *sk, struct msghdr *msg,\nnet/core/scm.c:546:\t\t\t      struct scm_cookie *scm, int flags)\nnet/core/scm.c-547-{\n--\nnet/core/scm.c=573=void scm_recv(struct socket *sock, struct msghdr *msg,\nnet/core/scm.c:574:\t      struct scm_cookie *scm, int flags)\nnet/core/scm.c-575-{\n--\nnet/core/scm.c=583=void scm_recv_unix(struct socket *sock, struct msghdr *msg,\nnet/core/scm.c:584:\t\t   struct scm_cookie *scm, int flags)\nnet/core/scm.c-585-{\n--\nnet/netlink/af_netlink.c=1818=static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)\n--\nnet/netlink/af_netlink.c-1826-\tint err;\nnet/netlink/af_netlink.c:1827:\tstruct scm_cookie scm;\nnet/netlink/af_netlink.c-1828-\tu32 netlink_skb_flags = 0;\n--\nnet/netlink/af_netlink.c=1907=static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,\n--\nnet/netlink/af_netlink.c-1909-{\nnet/netlink/af_netlink.c:1910:\tstruct scm_cookie scm;\nnet/netlink/af_netlink.c-1911-\tstruct sock *sk = sock-\u003esk;\n--\nnet/unix/af_unix.c=243=static void unix_table_double_unlock(struct net *net,\n--\nnet/unix/af_unix.c-255-#ifdef CONFIG_SECURITY_NETWORK\nnet/unix/af_unix.c:256:static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-257-{\n--\nnet/unix/af_unix.c-260-\nnet/unix/af_unix.c:261:static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-262-{\n--\nnet/unix/af_unix.c-265-\nnet/unix/af_unix.c:266:static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-267-{\n--\nnet/unix/af_unix.c-270-#else\nnet/unix/af_unix.c:271:static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-272-{ }\nnet/unix/af_unix.c-273-\nnet/unix/af_unix.c:274:static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-275-{ }\nnet/unix/af_unix.c-276-\nnet/unix/af_unix.c:277:static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-278-{\n--\nnet/unix/af_unix.c=1931=static inline bool too_many_unix_fds(struct task_struct *p)\n--\nnet/unix/af_unix.c-1939-\nnet/unix/af_unix.c:1940:static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-1941-{\n--\nnet/unix/af_unix.c-1953-\nnet/unix/af_unix.c:1954:static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-1955-{\n--\nnet/unix/af_unix.c-1961-\nnet/unix/af_unix.c:1962:static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-1963-{\n--\nnet/unix/af_unix.c=1969=static void unix_destruct_scm(struct sk_buff *skb)\nnet/unix/af_unix.c-1970-{\nnet/unix/af_unix.c:1971:\tstruct scm_cookie scm = {};\nnet/unix/af_unix.c-1972-\n--\nnet/unix/af_unix.c=1981=static void unix_wfree(struct sk_buff *skb)\n--\nnet/unix/af_unix.c-1986-\nnet/unix/af_unix.c:1987:static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)\nnet/unix/af_unix.c-1988-{\n--\nnet/unix/af_unix.c-2002-\nnet/unix/af_unix.c:2003:static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)\nnet/unix/af_unix.c-2004-{\n--\nnet/unix/af_unix.c=2048=static bool unix_skb_scm_eq(const struct sock *sk, struct sk_buff *skb,\nnet/unix/af_unix.c:2049:\t\t\t    struct scm_cookie *scm)\nnet/unix/af_unix.c-2050-{\n--\nnet/unix/af_unix.c=2095=static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/unix/af_unix.c-2099-\tstruct unix_sock *u = unix_sk(sk);\nnet/unix/af_unix.c:2100:\tstruct scm_cookie scm;\nnet/unix/af_unix.c-2101-\tstruct sk_buff *skb;\n--\nnet/unix/af_unix.c=2329=static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,\nnet/unix/af_unix.c:2330:\t\t     struct scm_cookie *scm, bool fds_sent)\nnet/unix/af_unix.c-2331-{\n--\nnet/unix/af_unix.c=2389=static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/unix/af_unix.c-2395-\tstruct unix_sock *otheru;\nnet/unix/af_unix.c:2396:\tstruct scm_cookie scm;\nnet/unix/af_unix.c-2397-\tbool fds_sent = false;\n--\nnet/unix/af_unix.c=2574=int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,\n--\nnet/unix/af_unix.c-2576-{\nnet/unix/af_unix.c:2577:\tstruct scm_cookie scm;\nnet/unix/af_unix.c-2578-\tstruct socket *sock = sk-\u003esk_socket;\n--\nnet/unix/af_unix.c=2919=static int unix_stream_read_generic(struct unix_stream_read_state *state,\n--\nnet/unix/af_unix.c-2928-\tbool check_creds = false;\nnet/unix/af_unix.c:2929:\tstruct scm_cookie scm;\nnet/unix/af_unix.c-2930-\tstruct unix_sock *u;\n"
}

47/3 2026/08/31 12:35 llm kmsan-evaluator
2m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 30559
							output: 34
							thoughts: 39
						

Thoughts:
Okay, let's dive into this `struct scm_cookie` situation. I need to get a clear picture of its initialization across the codebase. My immediate goal is to map out every single location where that structure is brought into existence. This is crucial for understanding its lifecycle and potential pitfalls. I'll need to meticulously trace its initial state from its birth, I'm thinking I need to find all uses in this file and understand the initialization pattern.

Then, I should shift my focus to `scm_send` found in `include/net/scm.h`. That function is probably a key player in how `scm_cookie` is used and how those structures play together. Understanding `scm_send` is almost certainly going to provide valuable context around what I found in the last step. By getting a handle on those two pieces, I'll be in a much stronger position to diagnose any issues, understand how the data flows, or even optimize the code later on. So, `scm_cookie` initialization first, then `scm_send`. That's the plan.



48/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "include/net/scm.h",
  "FirstLine": 60,
  "LineCount": 50
}

Results:
{
  "Contents": "  60:\tstatic __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)\n  61:\t{\n  62:\t\tsecurity_socket_getpeersec_dgram(sock, NULL, \u0026scm-\u003esecid);\n  63:\t}\n  64:\t#else\n  65:\tstatic __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)\n  66:\t{ }\n  67:\t#endif /* CONFIG_SECURITY_NETWORK */\n  68:\t\n  69:\tstatic __inline__ void scm_set_cred(struct scm_cookie *scm,\n  70:\t\t\t\t\t    struct pid *pid, kuid_t uid, kgid_t gid)\n  71:\t{\n  72:\t\tscm-\u003epid[PIDTYPE_TGID] = get_pid(pid);\n  73:\t\tscm-\u003ecreds.pid = pid_vnr(pid);\n  74:\t\tscm-\u003ecreds.uid = uid;\n  75:\t\tscm-\u003ecreds.gid = gid;\n  76:\t}\n  77:\t\n  78:\tstatic __inline__ void scm_destroy_cred(struct scm_cookie *scm)\n  79:\t{\n  80:\t\tput_pids(scm-\u003epid);\n  81:\t}\n  82:\t\n  83:\tstatic __inline__ void scm_destroy(struct scm_cookie *scm)\n  84:\t{\n  85:\t\tscm_destroy_cred(scm);\n  86:\t\tif (scm-\u003efp)\n  87:\t\t\t__scm_destroy(scm);\n  88:\t}\n  89:\t\n  90:\tstatic __inline__ int scm_send(struct socket *sock, struct msghdr *msg,\n  91:\t\t\t\t       struct scm_cookie *scm, bool forcecreds)\n  92:\t{\n  93:\t\tmemset(scm, 0, sizeof(*scm));\n  94:\t\tscm-\u003ecreds.uid = INVALID_UID;\n  95:\t\tscm-\u003ecreds.gid = INVALID_GID;\n  96:\t\tif (forcecreds)\n  97:\t\t\tscm_set_cred(scm, task_tgid(current), current_uid(), current_gid());\n  98:\t\tunix_get_peersec_dgram(sock, scm);\n  99:\t\tif (msg-\u003emsg_controllen \u003c= 0)\n 100:\t\t\treturn 0;\n 101:\t\treturn __scm_send(sock, msg, scm);\n 102:\t}\n 103:\t\n 104:\tvoid scm_recv(struct socket *sock, struct msghdr *msg,\n 105:\t\t      struct scm_cookie *scm, int flags);\n 106:\tvoid scm_recv_unix(struct socket *sock, struct msghdr *msg,\n 107:\t\t\t   struct scm_cookie *scm, int flags);\n 108:\t\n 109:\tint scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,\n"
}

49/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 31663
							output: 38
							thoughts: 117
						

Thoughts:
**Investigating Potential Data Corruption**

Okay, so `scm_send` is definitely zeroing out the `scm` struct with `memset`. That's good hygiene, but it's got me thinking: are we consistently handling this initialization across the board? I need to be sure. My expert intuition is telling me to check the other places where `scm` is used. Specifically, I'm concerned about potential data leaks or unexpected behavior if these other functions aren't also meticulously initializing the struct.

Let's quickly scan the source for `hci_sock_recvmsg`, `netlink_sendmsg`, `netlink_recvmsg`, `unix_dgram_sendmsg`, `unix_stream_sendmsg`, `__unix_dgram_recvmsg`, and `unix_stream_read_generic`. I need to read through each of them to make sure there aren't any gotchas. I'm looking for where `scm` is allocated, how it's used before being sent, and whether zeroing or explicit initialization is taking place. This is just a quick sanity check but a vital one. It is important to eliminate any possibility of a security vulnerability.



50/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "net/bluetooth/hci_sock.c",
  "FirstLine": 1555,
  "LineCount": 40
}

Results:
{
  "Contents": "1555:\t\n1556:\tstatic int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,\n1557:\t\t\t\t    size_t len, int flags)\n1558:\t{\n1559:\t\tstruct scm_cookie scm;\n1560:\t\tstruct sock *sk = sock-\u003esk;\n1561:\t\tstruct sk_buff *skb;\n1562:\t\tint copied, err;\n1563:\t\tunsigned int skblen;\n1564:\t\n1565:\t\tBT_DBG(\"sock %p, sk %p\", sock, sk);\n1566:\t\n1567:\t\tif (flags \u0026 MSG_OOB)\n1568:\t\t\treturn -EOPNOTSUPP;\n1569:\t\n1570:\t\tif (hci_pi(sk)-\u003echannel == HCI_CHANNEL_LOGGING)\n1571:\t\t\treturn -EOPNOTSUPP;\n1572:\t\n1573:\t\tif (sk-\u003esk_state == BT_CLOSED)\n1574:\t\t\treturn 0;\n1575:\t\n1576:\t\tskb = skb_recv_datagram(sk, flags, \u0026err);\n1577:\t\tif (!skb)\n1578:\t\t\treturn err;\n1579:\t\n1580:\t\tskblen = skb-\u003elen;\n1581:\t\tcopied = skb-\u003elen;\n1582:\t\tif (len \u003c copied) {\n1583:\t\t\tmsg-\u003emsg_flags |= MSG_TRUNC;\n1584:\t\t\tcopied = len;\n1585:\t\t}\n1586:\t\n1587:\t\tskb_reset_transport_header(skb);\n1588:\t\terr = skb_copy_datagram_msg(skb, 0, msg, copied);\n1589:\t\n1590:\t\tswitch (hci_pi(sk)-\u003echannel) {\n1591:\t\tcase HCI_CHANNEL_RAW:\n1592:\t\t\thci_sock_cmsg(sk, msg, skb);\n1593:\t\t\tbreak;\n1594:\t\tcase HCI_CHANNEL_USER:\n"
}

51/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 32640
							output: 38
							thoughts: 19
						

Thoughts:
Here's a summary of my thought process, as requested:

**Diving into `hci_sock_recvmsg` and its `scm` Usage**

Okay, so I need to understand how the `scm` (presumably referring to socket control messages) is used within the `hci_sock_recvmsg` function. This is likely related to handling ancillary data associated with a Bluetooth HCI socket – things like credentials or other contextual information. My initial approach is to mentally trace the execution flow of `hci_sock_recvmsg`. I will essentially "step" through the function in my mind. Where does `scm` come into play? Is it being *received* with the message? Or perhaps created and added to the message? Is it directly manipulated inside the function, or just passed along? I want to pinpoint the *specific* points where the `scm` variable, or something related to socket control messages, is accessed, modified, or utilized. This will give me the contextual information I need to understand the role of `scm` in this specific function, and the dataflow around that variable.



52/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "net/bluetooth/hci_sock.c",
  "FirstLine": 1595,
  "LineCount": 20
}

Results:
{
  "Contents": "1595:\t\tcase HCI_CHANNEL_MONITOR:\n1596:\t\t\tsock_recv_timestamp(msg, sk, skb);\n1597:\t\t\tbreak;\n1598:\t\tdefault:\n1599:\t\t\tif (hci_mgmt_chan_find(hci_pi(sk)-\u003echannel))\n1600:\t\t\t\tsock_recv_timestamp(msg, sk, skb);\n1601:\t\t\tbreak;\n1602:\t\t}\n1603:\t\n1604:\t\tmemset(\u0026scm, 0, sizeof(scm));\n1605:\t\tscm.creds = bt_cb(skb)-\u003ecreds;\n1606:\t\n1607:\t\tskb_free_datagram(sk, skb);\n1608:\t\n1609:\t\tif (flags \u0026 MSG_TRUNC)\n1610:\t\t\tcopied = skblen;\n1611:\t\n1612:\t\tscm_recv(sock, msg, \u0026scm, flags);\n1613:\t\n1614:\t\treturn err ? : copied;\n"
}

53/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 33209
							output: 40
							thoughts: 43
						

Thoughts:
Okay, here's my take on those thoughts, framed as an expert's internal monologue:

**Investigating Socket Message Handling in HCI**

Alright, let's see... first, in `hci_sock_recvmsg`, we're clearing out the control message structure (`scm`) with `memset`. Makes sense, good practice to initialize it before we start stuffing data into it. Prevents any nasty surprises from residual values.

Now, my mind naturally jumps to the broader picture. How does this fit into the bigger Netlink messaging scheme? Let's quickly review the `netlink_sendmsg` and `netlink_recvmsg` implementations. I need to understand the relationship between this HCI-specific function and the more generic Netlink layer. Is this initialization consistent across the Netlink stack? Are there any potential gotchas I should be aware of regarding memory management or potential race conditions at the Netlink level that interact with this `memset` call? Good to keep that context sharp. I need to ensure my understanding of the system's architecture is thorough to get the full picture.



54/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "net/netlink/af_netlink.c",
  "FirstLine": 1825,
  "LineCount": 20
}

Results:
{
  "Contents": "1825:\t\tstruct sk_buff *skb;\n1826:\t\tint err;\n1827:\t\tstruct scm_cookie scm;\n1828:\t\tu32 netlink_skb_flags = 0;\n1829:\t\n1830:\t\tif (msg-\u003emsg_flags \u0026 MSG_OOB)\n1831:\t\t\treturn -EOPNOTSUPP;\n1832:\t\n1833:\t\tif (len == 0) {\n1834:\t\t\tpr_warn_once(\"Zero length message leads to an empty skb\\n\");\n1835:\t\t\treturn -ENODATA;\n1836:\t\t}\n1837:\t\n1838:\t\terr = scm_send(sock, msg, \u0026scm, true);\n1839:\t\tif (err \u003c 0)\n1840:\t\t\treturn err;\n1841:\t\n1842:\t\tif (msg-\u003emsg_namelen) {\n1843:\t\t\terr = -EINVAL;\n1844:\t\t\tif (msg-\u003emsg_namelen \u003c sizeof(struct sockaddr_nl))\n"
}

55/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 33817
							output: 40
							thoughts: 31
						

Thoughts:
Here's my summary, as though I'm thinking through this process:

**Diving into Netlink Message Handling**

Okay, so I'm digging into the Netlink message flow and I'm seeing `netlink_sendmsg` calling `scm_send`. Now, that's interesting. The code uses `memset` on `scm` within that function. I'm wondering what is being set and why. To get a better grasp on the send path, I should probably understand the receive path as well. Let's shift our focus and start with `netlink_recvmsg` to understand how the messages are received and what data structures are in play. It's time to build a complete picture of the message lifecycle.



56/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "net/netlink/af_netlink.c",
  "FirstLine": 1905,
  "LineCount": 35
}

Results:
{
  "Contents": "1905:\t}\n1906:\t\n1907:\tstatic int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,\n1908:\t\t\t\t   int flags)\n1909:\t{\n1910:\t\tstruct scm_cookie scm;\n1911:\t\tstruct sock *sk = sock-\u003esk;\n1912:\t\tstruct netlink_sock *nlk = nlk_sk(sk);\n1913:\t\tsize_t copied, max_recvmsg_len;\n1914:\t\tstruct sk_buff *skb, *data_skb;\n1915:\t\tint err, ret;\n1916:\t\n1917:\t\tif (flags \u0026 MSG_OOB)\n1918:\t\t\treturn -EOPNOTSUPP;\n1919:\t\n1920:\t\tcopied = 0;\n1921:\t\n1922:\t\tskb = skb_recv_datagram(sk, flags, \u0026err);\n1923:\t\tif (skb == NULL)\n1924:\t\t\tgoto out;\n1925:\t\n1926:\t\tdata_skb = skb;\n1927:\t\n1928:\t#ifdef CONFIG_COMPAT_NETLINK_MESSAGES\n1929:\t\tif (unlikely(skb_shinfo(skb)-\u003efrag_list)) {\n1930:\t\t\t/*\n1931:\t\t\t * If this skb has a frag_list, then here that means that we\n1932:\t\t\t * will have to use the frag_list skb's data for compat tasks\n1933:\t\t\t * and the regular skb's data for normal (non-compat) tasks.\n1934:\t\t\t *\n1935:\t\t\t * If we need to send the compat skb, assign it to the\n1936:\t\t\t * 'data_skb' variable so that it will be used below for data\n1937:\t\t\t * copying. We keep 'skb' for everything else, including\n1938:\t\t\t * freeing both later.\n1939:\t\t\t */\n"
}

57/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 34593
							output: 40
							thoughts: 14
						

Thoughts:
Okay, here's my attempt at summarizing the thought process, as requested, in the first person and targeted towards an expert:

**Diving Deeper into `netlink_recvmsg`**

Alright, so we're looking at the remainder of the `netlink_recvmsg` function, huh? Let's get down to brass tacks. I've got a pretty good grasp of the Netlink architecture, and I'm interested in how this particular function handles the complexities of receiving messages. I'm keen to see how they're managing things like socket address structures, buffer management, error handling, and of course, the interaction with the underlying network interface. My intuition tells me we'll be dealing with some delicate threading issues potentially, particularly if they are handling multiple sockets or connections. I’m thinking there will be nuances in how they manage the socket's receive buffer, and what the flow control looks like. I'm also betting the interaction with the Netlink family-specific handlers is going to be pivotal here. So let's crack this code, and see exactly what gems are hidden!



58/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "net/netlink/af_netlink.c",
  "FirstLine": 1940,
  "LineCount": 40
}

Results:
{
  "Contents": "1940:\t\t\tif (flags \u0026 MSG_CMSG_COMPAT)\n1941:\t\t\t\tdata_skb = skb_shinfo(skb)-\u003efrag_list;\n1942:\t\t}\n1943:\t#endif\n1944:\t\n1945:\t\t/* Record the max length of recvmsg() calls for future allocations */\n1946:\t\tmax_recvmsg_len = max(READ_ONCE(nlk-\u003emax_recvmsg_len), len);\n1947:\t\tmax_recvmsg_len = min_t(size_t, max_recvmsg_len,\n1948:\t\t\t\t\tSKB_WITH_OVERHEAD(32768));\n1949:\t\tWRITE_ONCE(nlk-\u003emax_recvmsg_len, max_recvmsg_len);\n1950:\t\n1951:\t\tcopied = data_skb-\u003elen;\n1952:\t\tif (len \u003c copied) {\n1953:\t\t\tmsg-\u003emsg_flags |= MSG_TRUNC;\n1954:\t\t\tcopied = len;\n1955:\t\t}\n1956:\t\n1957:\t\terr = skb_copy_datagram_msg(data_skb, 0, msg, copied);\n1958:\t\n1959:\t\tif (msg-\u003emsg_name) {\n1960:\t\t\tDECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg-\u003emsg_name);\n1961:\t\t\taddr-\u003enl_family = AF_NETLINK;\n1962:\t\t\taddr-\u003enl_pad    = 0;\n1963:\t\t\taddr-\u003enl_pid\t= NETLINK_CB(skb).portid;\n1964:\t\t\taddr-\u003enl_groups\t= netlink_group_mask(NETLINK_CB(skb).dst_group);\n1965:\t\t\tmsg-\u003emsg_namelen = sizeof(*addr);\n1966:\t\t}\n1967:\t\n1968:\t\tif (nlk_test_bit(RECV_PKTINFO, sk))\n1969:\t\t\tnetlink_cmsg_recv_pktinfo(msg, skb);\n1970:\t\tif (nlk_test_bit(LISTEN_ALL_NSID, sk))\n1971:\t\t\tnetlink_cmsg_listen_all_nsid(sk, msg, skb);\n1972:\t\n1973:\t\tmemset(\u0026scm, 0, sizeof(scm));\n1974:\t\tscm.creds = *NETLINK_CREDS(skb);\n1975:\t\tif (flags \u0026 MSG_TRUNC)\n1976:\t\t\tcopied = data_skb-\u003elen;\n1977:\t\n1978:\t\tskb_free_datagram(sk, skb);\n1979:\t\n"
}

59/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 35593
							output: 38
							thoughts: 78
						

Thoughts:
Here's my thought process, as if I were analyzing that code:

**Analyzing the AF_UNIX Socket Code**

Okay, so I've spotted a `memset` on a `scm` structure at line 1973. It's initializing the structure to zero, which is pretty standard practice for security and consistency when dealing with socket control messages. Now I'm going to dig into the `net/unix/af_unix.c` file to understand how this `scm` is used.

I need to quickly grasp the relevant send and receive functions. I'll focus on `unix_dgram_sendmsg`, and `unix_stream_sendmsg` for sending, and then `__unix_dgram_recvmsg`, and `unix_stream_read_generic` for receiving. I'll scrutinize those functions to see how `scm` is being used and why that zero initialization is crucial in the context of AF_UNIX socket control messages and data transfer. Gotta make sure nothing unexpected happens with uninitialized memory. Gotta keep the data in order and secure.



60/3 2026/08/31 12:38 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.c",
  "FirstLine": 2095,
  "LineCount": 40
}

Results:
{
  "Contents": "2095:\tstatic int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,\n2096:\t\t\t\t      size_t len)\n2097:\t{\n2098:\t\tstruct sock *sk = sock-\u003esk, *other = NULL;\n2099:\t\tstruct unix_sock *u = unix_sk(sk);\n2100:\t\tstruct scm_cookie scm;\n2101:\t\tstruct sk_buff *skb;\n2102:\t\tint data_len = 0;\n2103:\t\tint sk_locked;\n2104:\t\tlong timeo;\n2105:\t\tint err;\n2106:\t\n2107:\t\terr = scm_send(sock, msg, \u0026scm, false);\n2108:\t\tif (err \u003c 0)\n2109:\t\t\treturn err;\n2110:\t\n2111:\t\tif (msg-\u003emsg_flags \u0026 MSG_OOB) {\n2112:\t\t\terr = -EOPNOTSUPP;\n2113:\t\t\tgoto out;\n2114:\t\t}\n2115:\t\n2116:\t\tif (msg-\u003emsg_namelen) {\n2117:\t\t\terr = unix_validate_addr(msg-\u003emsg_name, msg-\u003emsg_namelen);\n2118:\t\t\tif (err)\n2119:\t\t\t\tgoto out;\n2120:\t\n2121:\t\t\terr = BPF_CGROUP_RUN_PROG_UNIX_SENDMSG_LOCK(sk,\n2122:\t\t\t\t\t\t\t\t    msg-\u003emsg_name,\n2123:\t\t\t\t\t\t\t\t    \u0026msg-\u003emsg_namelen,\n2124:\t\t\t\t\t\t\t\t    NULL);\n2125:\t\t\tif (err)\n2126:\t\t\t\tgoto out;\n2127:\t\t}\n2128:\t\n2129:\t\tif (unix_may_passcred(sk) \u0026\u0026 !READ_ONCE(u-\u003eaddr)) {\n2130:\t\t\terr = unix_autobind(sk);\n2131:\t\t\tif (err)\n2132:\t\t\t\tgoto out;\n2133:\t\t}\n2134:\t\n"
}

61/3 2026/08/31 12:38 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 36508
							output: 38
							thoughts: 33
						

Thoughts:
Okay, here's my interpretation of those thoughts, presented as a first-person expert summary:

**Digging into Socket Send Operations**

Alright, so I'm thinking about the low-level socket operations, specifically the `sendmsg` family. I'm focusing on the Unix domain sockets, naturally. I know `unix_dgram_sendmsg` calls `scm_send` to handle the ancillary data. And I recall that `scm_send` starts by using `memset` on the `scm` structure. That's good, predictable behavior – ensuring a clean slate before populating the control message. My brain's now asking: what about the stream sockets? I wonder if `unix_stream_sendmsg` has similar behavior in that it uses `scm_send`, or if it handles the ancillary data differently due to the connection-oriented nature of the stream sockets. There might be some subtle differences to keep in mind, and I should really make a note to check that code path too.



62/3 2026/08/31 12:39 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.c",
  "FirstLine": 2389,
  "LineCount": 40
}

Results:
{
  "Contents": "2389:\tstatic int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,\n2390:\t\t\t\t       size_t len)\n2391:\t{\n2392:\t\tstruct sock *sk = sock-\u003esk;\n2393:\t\tstruct sk_buff *skb = NULL;\n2394:\t\tstruct sock *other = NULL;\n2395:\t\tstruct unix_sock *otheru;\n2396:\t\tstruct scm_cookie scm;\n2397:\t\tbool fds_sent = false;\n2398:\t\tint err, sent = 0;\n2399:\t\n2400:\t\terr = scm_send(sock, msg, \u0026scm, false);\n2401:\t\tif (err \u003c 0)\n2402:\t\t\treturn err;\n2403:\t\n2404:\t\tif (msg-\u003emsg_flags \u0026 MSG_OOB) {\n2405:\t\t\terr = -EOPNOTSUPP;\n2406:\t#if IS_ENABLED(CONFIG_AF_UNIX_OOB)\n2407:\t\t\tif (len)\n2408:\t\t\t\tlen--;\n2409:\t\t\telse\n2410:\t#endif\n2411:\t\t\t\tgoto out_err;\n2412:\t\t}\n2413:\t\n2414:\t\tif (msg-\u003emsg_namelen) {\n2415:\t\t\terr = READ_ONCE(sk-\u003esk_state) == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;\n2416:\t\t\tgoto out_err;\n2417:\t\t}\n2418:\t\n2419:\t\tother = unix_peer(sk);\n2420:\t\tif (!other) {\n2421:\t\t\terr = -ENOTCONN;\n2422:\t\t\tgoto out_err;\n2423:\t\t}\n2424:\t\n2425:\t\totheru = unix_sk(other);\n2426:\t\n2427:\t\tif (READ_ONCE(sk-\u003esk_shutdown) \u0026 SEND_SHUTDOWN)\n2428:\t\t\tgoto out_pipe;\n"
}

63/3 2026/08/31 12:39 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 37355
							output: 38
							thoughts: 46
						

Thoughts:
Okay, here's my interpretation of those thoughts, presented as my own mental process, given my expertise:

**Diving into the Unix Socket Implementation**

Right, `unix_stream_sendmsg` has me interested. It looks like it utilizes `scm_send`, and that function's memsetting of `scm` is something I need to keep in mind. I want to understand what's happening more deeply. Now I'm going to look at the corresponding receive path. I'll need to check how the datagram case is handled, specifically `__unix_dgram_recvmsg`. Then, to round things out, I'll need to follow how the stream data is read in general, so let's check `unix_stream_read_generic`. That should give me a good handle on the complete data flow. This will likely expose the interaction and synchronization around those SCM structures.



64/3 2026/08/31 12:39 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.c",
  "FirstLine": 2574,
  "LineCount": 40
}

Results:
{
  "Contents": "2574:\tint __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,\n2575:\t\t\t\t int flags)\n2576:\t{\n2577:\t\tstruct scm_cookie scm;\n2578:\t\tstruct socket *sock = sk-\u003esk_socket;\n2579:\t\tstruct unix_sock *u = unix_sk(sk);\n2580:\t\tstruct sk_buff *skb, *last;\n2581:\t\tlong timeo;\n2582:\t\tint skip;\n2583:\t\tint err;\n2584:\t\n2585:\t\terr = -EOPNOTSUPP;\n2586:\t\tif (flags\u0026MSG_OOB)\n2587:\t\t\tgoto out;\n2588:\t\n2589:\t\ttimeo = sock_rcvtimeo(sk, flags \u0026 MSG_DONTWAIT);\n2590:\t\n2591:\t\tdo {\n2592:\t\t\tmutex_lock(\u0026u-\u003eiolock);\n2593:\t\n2594:\t\t\tskip = sk_peek_offset(sk, flags);\n2595:\t\t\tskb = __skb_try_recv_datagram(sk, \u0026sk-\u003esk_receive_queue, flags,\n2596:\t\t\t\t\t\t      \u0026skip, \u0026err, \u0026last);\n2597:\t\t\tif (skb) {\n2598:\t\t\t\tif (!(flags \u0026 MSG_PEEK))\n2599:\t\t\t\t\tscm_stat_del(sk, skb);\n2600:\t\t\t\tbreak;\n2601:\t\t\t}\n2602:\t\n2603:\t\t\tmutex_unlock(\u0026u-\u003eiolock);\n2604:\t\n2605:\t\t\tif (err != -EAGAIN)\n2606:\t\t\t\tbreak;\n2607:\t\t} while (timeo \u0026\u0026\n2608:\t\t\t !__skb_wait_for_more_packets(sk, \u0026sk-\u003esk_receive_queue,\n2609:\t\t\t\t\t\t      \u0026err, \u0026timeo, last));\n2610:\t\n2611:\t\tif (!skb) { /* implies iolock unlocked */\n2612:\t\t\t/* Signal EOF on disconnected non-blocking SEQPACKET socket. */\n2613:\t\t\tif (sk-\u003esk_type == SOCK_SEQPACKET \u0026\u0026 err == -EAGAIN \u0026\u0026\n"
}

65/3 2026/08/31 12:39 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 38245
							output: 38
							thoughts: 17
						

Thoughts:
Here's a summary of my thought process, as if I were analyzing that code snippet:

**Diving Deeper into `__unix_dgram_recvmsg`**

Okay, so we're looking at the remainder of the `__unix_dgram_recvmsg` function. I need to understand its complete operational flow to truly grasp its implications for the overall system behavior. What are the specific conditions that trigger particular paths within this routine? How does it handle potential errors, and what are the implications of those error conditions? Are there any subtle race conditions or concurrency issues that might emerge within this function? I'll need to carefully examine how data is validated and copied, as socket-related functions are often a source of potential security vulnerabilities. Now, let's start dissecting it line by line, paying close attention to data structure interactions. The critical path would be where the data gets marshaled, and if any locks are needed.



66/3 2026/08/31 12:39 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.c",
  "FirstLine": 2650,
  "LineCount": 40
}

Results:
{
  "Contents": "2650:\t\t\t\tunix_detach_fds(\u0026scm, skb);\n2651:\t\n2652:\t\t\tsk_peek_offset_bwd(sk, skb-\u003elen);\n2653:\t\t} else {\n2654:\t\t\t/* It is questionable: on PEEK we could:\n2655:\t\t\t   - do not return fds - good, but too simple 8)\n2656:\t\t\t   - return fds, and do not return them on read (old strategy,\n2657:\t\t\t     apparently wrong)\n2658:\t\t\t   - clone fds (I chose it for now, it is the most universal\n2659:\t\t\t     solution)\n2660:\t\n2661:\t\t\t   POSIX 1003.1g does not actually define this clearly\n2662:\t\t\t   at all. POSIX 1003.1g doesn't define a lot of things\n2663:\t\t\t   clearly however!\n2664:\t\n2665:\t\t\t*/\n2666:\t\n2667:\t\t\tsk_peek_offset_fwd(sk, size);\n2668:\t\n2669:\t\t\tif (UNIXCB(skb).fp)\n2670:\t\t\t\tunix_peek_fds(\u0026scm, skb);\n2671:\t\t}\n2672:\t\terr = (flags \u0026 MSG_TRUNC) ? skb-\u003elen - skip : size;\n2673:\t\n2674:\t\tscm_recv_unix(sock, msg, \u0026scm, flags);\n2675:\t\n2676:\tout_free:\n2677:\t\tskb_free_datagram(sk, skb);\n2678:\t\tmutex_unlock(\u0026u-\u003eiolock);\n2679:\tout:\n2680:\t\treturn err;\n2681:\t}\n2682:\t\n2683:\tstatic int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,\n2684:\t\t\t\t      int flags)\n2685:\t{\n2686:\t\tstruct sock *sk = sock-\u003esk;\n2687:\t\n2688:\t#ifdef CONFIG_BPF_SYSCALL\n2689:\t\tconst struct proto *prot = READ_ONCE(sk-\u003esk_prot);\n"
}

67/3 2026/08/31 12:39 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 39101
							output: 38
							thoughts: 51
						

Thoughts:
Here's my take on the thought process, as requested:

**Diving into `__unix_dgram_recvmsg` Initialization**

Okay, so I need to understand how `scm` is initialized within the `__unix_dgram_recvmsg` function. Let's get right to it. I'm going to start by examining lines 2615-2650 of that function. That's usually a good place to start for this kind of thing. Let's see what the code is telling us. I'll need to look closely at how the control message data is handled and allocated, particularly focusing on any potential race conditions or resource limitations. This is likely where the core of the problem lies.



68/3 2026/08/31 12:39 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.c",
  "FirstLine": 2625,
  "LineCount": 30
}

Results:
{
  "Contents": "2625:\t\t\tunix_copy_addr(msg, skb-\u003esk);\n2626:\t\n2627:\t\t\tBPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,\n2628:\t\t\t\t\t\t\t      msg-\u003emsg_name,\n2629:\t\t\t\t\t\t\t      \u0026msg-\u003emsg_namelen);\n2630:\t\t}\n2631:\t\n2632:\t\tif (size \u003e skb-\u003elen - skip)\n2633:\t\t\tsize = skb-\u003elen - skip;\n2634:\t\telse if (size \u003c skb-\u003elen - skip)\n2635:\t\t\tmsg-\u003emsg_flags |= MSG_TRUNC;\n2636:\t\n2637:\t\terr = skb_copy_datagram_msg(skb, skip, msg, size);\n2638:\t\tif (err)\n2639:\t\t\tgoto out_free;\n2640:\t\n2641:\t\tif (sock_flag(sk, SOCK_RCVTSTAMP))\n2642:\t\t\t__sock_recv_timestamp(msg, sk, skb);\n2643:\t\n2644:\t\tmemset(\u0026scm, 0, sizeof(scm));\n2645:\t\n2646:\t\tunix_skb_to_scm(skb, \u0026scm);\n2647:\t\n2648:\t\tif (!(flags \u0026 MSG_PEEK)) {\n2649:\t\t\tif (UNIXCB(skb).fp)\n2650:\t\t\t\tunix_detach_fds(\u0026scm, skb);\n2651:\t\n2652:\t\t\tsk_peek_offset_bwd(sk, skb-\u003elen);\n2653:\t\t} else {\n2654:\t\t\t/* It is questionable: on PEEK we could:\n"
}

69/3 2026/08/31 12:39 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 39812
							output: 38
							thoughts: 53
						

Thoughts:
**Analyzing `unix_stream_read_generic` after SCM Initialization**

Okay, so I've just zeroed out the `scm` struct with `memset` and then populated it with `unix_skb_to_scm` using the current `skb`. That's standard initialization, ensuring a clean slate before potentially reading control data. Now, I need to understand what's happening within `unix_stream_read_generic`. I'm going to carefully trace its logic and how it interacts with the initialized `scm`. I expect it'll use this `scm` information to determine how the read operation proceeds, particularly with respect to any attached ancillary data or control messages. Time to dive in and see how the data flow is handled from here.



70/3 2026/08/31 12:40 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.c",
  "FirstLine": 2925,
  "LineCount": 45
}

Results:
{
  "Contents": "2925:\t\tstruct sock *sk = sock-\u003esk;\n2926:\t\tsize_t size = state-\u003esize;\n2927:\t\tint flags = state-\u003eflags;\n2928:\t\tbool check_creds = false;\n2929:\t\tstruct scm_cookie scm;\n2930:\t\tstruct unix_sock *u;\n2931:\t\tint copied = 0;\n2932:\t\tint err = 0;\n2933:\t\tlong timeo;\n2934:\t\tint target;\n2935:\t\tint skip;\n2936:\t\n2937:\t\tif (unlikely(READ_ONCE(sk-\u003esk_state) != TCP_ESTABLISHED)) {\n2938:\t\t\terr = -EINVAL;\n2939:\t\t\tgoto out;\n2940:\t\t}\n2941:\t\n2942:\t\tif (unlikely(flags \u0026 MSG_OOB)) {\n2943:\t\t\terr = -EOPNOTSUPP;\n2944:\t#if IS_ENABLED(CONFIG_AF_UNIX_OOB)\n2945:\t\t\terr = unix_stream_recv_urg(state);\n2946:\t#endif\n2947:\t\t\tgoto out;\n2948:\t\t}\n2949:\t\n2950:\t\ttarget = sock_rcvlowat(sk, flags \u0026 MSG_WAITALL, size);\n2951:\t\ttimeo = sock_rcvtimeo(sk, noblock);\n2952:\t\n2953:\t\tmemset(\u0026scm, 0, sizeof(scm));\n2954:\t\n2955:\t\tu = unix_sk(sk);\n2956:\t\n2957:\tredo:\n2958:\t\t/* Lock the socket to prevent queue disordering\n2959:\t\t * while sleeps in memcpy_tomsg\n2960:\t\t */\n2961:\t\tmutex_lock(\u0026u-\u003eiolock);\n2962:\t\n2963:\t\tskip = max(sk_peek_offset(sk, flags), 0);\n2964:\t\n2965:\t\tdo {\n2966:\t\t\tstruct sk_buff *skb, *last;\n2967:\t\t\tint chunk;\n2968:\t\n2969:\t\t\tunix_state_lock(sk);\n"
}

71/3 2026/08/31 12:40 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 40675
							output: 26
							thoughts: 79
						

Thoughts:
Here's my thought process, as an expert in the field:

**Debugging a Potential Socket Control Message (SCM) Issue**

Okay, so `unix_stream_read_generic` is zeroing out the `scm` structure with `memset` on line 2953. That's good practice, ensuring a clean slate before potentially receiving control messages. Now, a critical question arises: is the `UNIXCB(skb)` data structure properly initialized or reset *before* that point? My immediate concern is that a stale or uninitialized `UNIXCB(skb)` could lead to unexpected behavior or memory corruption, especially related to the handling of socket control messages. I need to trace the lifecycle of `UNIXCB(skb)`. Let's use `grep` within the `net/unix/` directory to hunt down every instance of `UNIXCB`. That will show me its initialization points. I want to see how and where it's set and reset to confirm it is handled correctly.



72/3 2026/08/31 12:40 tool grepper
0m Args:
{
  "Expression": "UNIXCB",
  "PathPrefix": "net/unix"
}

Results:
{
  "Output": "net/unix/af_unix.c=256=static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-257-{\nnet/unix/af_unix.c:258:\tUNIXCB(skb).secid = scm-\u003esecid;\nnet/unix/af_unix.c-259-}\n--\nnet/unix/af_unix.c=261=static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-262-{\nnet/unix/af_unix.c:263:\tscm-\u003esecid = UNIXCB(skb).secid;\nnet/unix/af_unix.c-264-}\n--\nnet/unix/af_unix.c=266=static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-267-{\nnet/unix/af_unix.c:268:\treturn (scm-\u003esecid == UNIXCB(skb).secid);\nnet/unix/af_unix.c-269-}\n--\nnet/unix/af_unix.c=660=static unsigned int unix_skb_len(const struct sk_buff *skb)\nnet/unix/af_unix.c-661-{\nnet/unix/af_unix.c:662:\treturn skb-\u003elen - UNIXCB(skb).consumed;\nnet/unix/af_unix.c-663-}\n--\nnet/unix/af_unix.c=1940=static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)\n--\nnet/unix/af_unix.c-1944-\nnet/unix/af_unix.c:1945:\tUNIXCB(skb).fp = scm-\u003efp;\nnet/unix/af_unix.c-1946-\tscm-\u003efp = NULL;\nnet/unix/af_unix.c-1947-\nnet/unix/af_unix.c:1948:\tif (unix_prepare_fpl(UNIXCB(skb).fp))\nnet/unix/af_unix.c-1949-\t\treturn -ENOMEM;\n--\nnet/unix/af_unix.c=1954=static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-1955-{\nnet/unix/af_unix.c:1956:\tscm-\u003efp = UNIXCB(skb).fp;\nnet/unix/af_unix.c:1957:\tUNIXCB(skb).fp = NULL;\nnet/unix/af_unix.c-1958-\n--\nnet/unix/af_unix.c=1962=static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)\nnet/unix/af_unix.c-1963-{\nnet/unix/af_unix.c:1964:\tscm-\u003efp = scm_fp_dup(UNIXCB(skb).fp);\nnet/unix/af_unix.c-1965-\n--\nnet/unix/af_unix.c=1969=static void unix_destruct_scm(struct sk_buff *skb)\n--\nnet/unix/af_unix.c-1972-\nnet/unix/af_unix.c:1973:\tswap_pids(scm.pid, UNIXCB(skb).pid);\nnet/unix/af_unix.c-1974-\nnet/unix/af_unix.c:1975:\tif (UNIXCB(skb).fp)\nnet/unix/af_unix.c-1976-\t\tunix_detach_fds(\u0026scm, skb);\n--\nnet/unix/af_unix.c=1987=static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)\n--\nnet/unix/af_unix.c-1990-\nnet/unix/af_unix.c:1991:\tget_pids(UNIXCB(skb).pid, scm-\u003epid);\nnet/unix/af_unix.c:1992:\tUNIXCB(skb).uid = scm-\u003ecreds.uid;\nnet/unix/af_unix.c:1993:\tUNIXCB(skb).gid = scm-\u003ecreds.gid;\nnet/unix/af_unix.c:1994:\tUNIXCB(skb).fp = NULL;\nnet/unix/af_unix.c-1995-\tunix_get_secdata(scm, skb);\n--\nnet/unix/af_unix.c=2003=static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)\nnet/unix/af_unix.c-2004-{\nnet/unix/af_unix.c:2005:\tget_pids(scm-\u003epid, UNIXCB(skb).pid);\nnet/unix/af_unix.c-2006-\tscm-\u003ecreds.pid = pid_vnr(scm-\u003epid[PIDTYPE_TGID]);\nnet/unix/af_unix.c:2007:\tscm-\u003ecreds.uid = UNIXCB(skb).uid;\nnet/unix/af_unix.c:2008:\tscm-\u003ecreds.gid = UNIXCB(skb).gid;\nnet/unix/af_unix.c-2009-\tunix_set_secdata(scm, skb);\n--\nnet/unix/af_unix.c=2025=static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,\n--\nnet/unix/af_unix.c-2027-{\nnet/unix/af_unix.c:2028:\tif (UNIXCB(skb).pid[PIDTYPE_TGID])\nnet/unix/af_unix.c-2029-\t\treturn 0;\n--\nnet/unix/af_unix.c-2034-\nnet/unix/af_unix.c:2035:\t\tget_task_pids(UNIXCB(skb).pid, current);\nnet/unix/af_unix.c:2036:\t\terr = pidfs_register_pids(UNIXCB(skb).pid);\nnet/unix/af_unix.c-2037-\t\tif (unlikely(err)) {\nnet/unix/af_unix.c:2038:\t\t\tput_pids(UNIXCB(skb).pid);\nnet/unix/af_unix.c-2039-\t\t\treturn err;\n--\nnet/unix/af_unix.c-2041-\nnet/unix/af_unix.c:2042:\t\tcurrent_uid_gid(\u0026UNIXCB(skb).uid, \u0026UNIXCB(skb).gid);\nnet/unix/af_unix.c-2043-\t}\n--\nnet/unix/af_unix.c=2048=static bool unix_skb_scm_eq(const struct sock *sk, struct sk_buff *skb,\n--\nnet/unix/af_unix.c-2052-\tif (sk-\u003esk_scm_pidfd_thread \u0026\u0026\nnet/unix/af_unix.c:2053:\t    UNIXCB(skb).pid[PIDTYPE_PID] != scm-\u003epid[PIDTYPE_PID])\nnet/unix/af_unix.c-2054-\t\treturn false;\nnet/unix/af_unix.c-2055-\nnet/unix/af_unix.c:2056:\treturn UNIXCB(skb).pid[PIDTYPE_TGID] == scm-\u003epid[PIDTYPE_TGID] \u0026\u0026\nnet/unix/af_unix.c:2057:\t       uid_eq(UNIXCB(skb).uid, scm-\u003ecreds.uid) \u0026\u0026\nnet/unix/af_unix.c:2058:\t       gid_eq(UNIXCB(skb).gid, scm-\u003ecreds.gid) \u0026\u0026\nnet/unix/af_unix.c-2059-\t       unix_secdata_eq(scm, skb);\n--\nnet/unix/af_unix.c=2062=static void scm_stat_add(struct sock *sk, struct sk_buff *skb)\nnet/unix/af_unix.c-2063-{\nnet/unix/af_unix.c:2064:\tstruct scm_fp_list *fp = UNIXCB(skb).fp;\nnet/unix/af_unix.c-2065-\tstruct unix_sock *u = unix_sk(sk);\n--\nnet/unix/af_unix.c=2073=static void scm_stat_del(struct sock *sk, struct sk_buff *skb)\nnet/unix/af_unix.c-2074-{\nnet/unix/af_unix.c:2075:\tstruct scm_fp_list *fp = UNIXCB(skb).fp;\nnet/unix/af_unix.c-2076-\tstruct unix_sock *u = unix_sk(sk);\n--\nnet/unix/af_unix.c=2095=static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/unix/af_unix.c-2249-\nnet/unix/af_unix.c:2250:\tif (UNIXCB(skb).fp \u0026\u0026 !other-\u003esk_scm_rights) {\nnet/unix/af_unix.c-2251-\t\terr = -EPERM;\n--\nnet/unix/af_unix.c=2329=static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,\n--\nnet/unix/af_unix.c-2362-\nnet/unix/af_unix.c:2363:\tif (UNIXCB(skb).fp \u0026\u0026 !other-\u003esk_scm_rights) {\nnet/unix/af_unix.c-2364-\t\terr = -EPERM;\n--\nnet/unix/af_unix.c=2389=static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/unix/af_unix.c-2489-\nnet/unix/af_unix.c:2490:\t\tif (UNIXCB(skb).fp \u0026\u0026 !other-\u003esk_scm_rights) {\nnet/unix/af_unix.c-2491-\t\t\tunix_state_unlock(other);\n--\nnet/unix/af_unix.c=2574=int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,\n--\nnet/unix/af_unix.c-2648-\tif (!(flags \u0026 MSG_PEEK)) {\nnet/unix/af_unix.c:2649:\t\tif (UNIXCB(skb).fp)\nnet/unix/af_unix.c-2650-\t\t\tunix_detach_fds(\u0026scm, skb);\n--\nnet/unix/af_unix.c-2668-\nnet/unix/af_unix.c:2669:\t\tif (UNIXCB(skb).fp)\nnet/unix/af_unix.c-2670-\t\t\tunix_peek_fds(\u0026scm, skb);\n--\nnet/unix/af_unix.c=2769=static int unix_stream_recv_urg(struct unix_stream_read_state *state)\n--\nnet/unix/af_unix.c-2806-\tif (!(state-\u003eflags \u0026 MSG_PEEK))\nnet/unix/af_unix.c:2807:\t\tUNIXCB(oob_skb).consumed += 1;\nnet/unix/af_unix.c-2808-\n--\nnet/unix/af_unix.c=2919=static int unix_stream_read_generic(struct unix_stream_read_state *state,\n--\nnet/unix/af_unix.c-3064-\t\tif (!(flags \u0026 MSG_PEEK)) {\nnet/unix/af_unix.c:3065:\t\t\tUNIXCB(skb).consumed += chunk;\nnet/unix/af_unix.c-3066-\n--\nnet/unix/af_unix.c-3068-\nnet/unix/af_unix.c:3069:\t\t\tif (UNIXCB(skb).fp) {\nnet/unix/af_unix.c-3070-\t\t\t\tscm_stat_del(sk, skb);\n--\nnet/unix/af_unix.c-3089-\t\t\t */\nnet/unix/af_unix.c:3090:\t\t\tif (UNIXCB(skb).fp)\nnet/unix/af_unix.c-3091-\t\t\t\tunix_peek_fds(\u0026scm, skb);\n--\nnet/unix/af_unix.c-3094-\nnet/unix/af_unix.c:3095:\t\t\tif (UNIXCB(skb).fp)\nnet/unix/af_unix.c-3096-\t\t\t\tbreak;\n--\nnet/unix/af_unix.c=3128=static int unix_stream_read_actor(struct sk_buff *skb,\n--\nnet/unix/af_unix.c-3133-\nnet/unix/af_unix.c:3134:\tret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,\nnet/unix/af_unix.c-3135-\t\t\t\t    state-\u003emsg, chunk);\n--\nnet/unix/af_unix.c=3174=static int unix_stream_splice_actor(struct sk_buff *skb,\n--\nnet/unix/af_unix.c-3178-\treturn skb_splice_bits(skb, state-\u003esocket-\u003esk,\nnet/unix/af_unix.c:3179:\t\t\t       UNIXCB(skb).consumed + skip,\nnet/unix/af_unix.c-3180-\t\t\t       state-\u003epipe, chunk, state-\u003esplice_flags);\n--\nnet/unix/af_unix.h=14=struct unix_skb_parms {\n--\nnet/unix/af_unix.h-24-\nnet/unix/af_unix.h:25:#define UNIXCB(skb)\t(*(struct unix_skb_parms *)\u0026((skb)-\u003ecb))\nnet/unix/af_unix.h-26-\n--\nnet/unix/garbage.c=606=static void unix_gc(struct work_struct *work)\n--\nnet/unix/garbage.c-629-\tskb_queue_walk(\u0026hitlist, skb) {\nnet/unix/garbage.c:630:\t\tif (UNIXCB(skb).fp)\nnet/unix/garbage.c:631:\t\t\tUNIXCB(skb).fp-\u003edead = true;\nnet/unix/garbage.c-632-\t}\n"
}

73/3 2026/08/31 12:40 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 44849
							output: 35
							thoughts: 43
						

Thoughts:
Okay, here's my take on those thoughts, framed as my own mental process, given my expertise:

**Examining the Unix Socket Control Block Parameters**

Alright, time to get to the core of this. I need to understand how `struct unix_skb_parms` is laid out, especially in relation to the `skb->cb` field. I know that's where a lot of the crucial per-socket information is stored in the Linux networking stack, so figuring out its structure is key to understanding the Unix socket implementation details. Let's delve into that `af_unix.h` file and get a good grasp of what data members constitute that structure, their types, and crucially, what the overall memory footprint of `struct unix_skb_parms` will be, so I can account for it in any calculations related to socket buffer management. That header file should have all the answers. Let's get to it.



74/3 2026/08/31 12:41 tool read-file
0m Args:
{
  "File": "net/unix/af_unix.h",
  "FirstLine": 1,
  "LineCount": 35
}

Results:
{
  "Contents": "   1:\t/* SPDX-License-Identifier: GPL-2.0 */\n   2:\t#ifndef __AF_UNIX_H\n   3:\t#define __AF_UNIX_H\n   4:\t\n   5:\t#include \u003clinux/pid_types.h\u003e\n   6:\t#include \u003clinux/uidgid.h\u003e\n   7:\t\n   8:\t#define UNIX_HASH_MOD\t(256 - 1)\n   9:\t#define UNIX_HASH_SIZE\t(256 * 2)\n  10:\t#define UNIX_HASH_BITS\t8\n  11:\t\n  12:\tstruct sock *unix_peer_get(struct sock *sk);\n  13:\t\n  14:\tstruct unix_skb_parms {\n  15:\t\tDECLARE_PIDS(pid, PIDTYPE_TGID);\t/* skb credentials by pid type */\n  16:\t\tkuid_t\t\t\tuid;\n  17:\t\tkgid_t\t\t\tgid;\n  18:\t\tstruct scm_fp_list\t*fp;\t\t/* Passed files\t\t*/\n  19:\t#ifdef CONFIG_SECURITY_NETWORK\n  20:\t\tu32\t\t\tsecid;\t\t/* Security ID\t\t*/\n  21:\t#endif\n  22:\t\tu32\t\t\tconsumed;\n  23:\t} __randomize_layout;\n  24:\t\n  25:\t#define UNIXCB(skb)\t(*(struct unix_skb_parms *)\u0026((skb)-\u003ecb))\n  26:\t\n  27:\t/* GC for SCM_RIGHTS */\n  28:\tvoid unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver);\n  29:\tvoid unix_del_edges(struct scm_fp_list *fpl);\n  30:\tvoid unix_update_edges(struct unix_sock *receiver);\n  31:\tint unix_prepare_fpl(struct scm_fp_list *fpl);\n  32:\tvoid unix_destroy_fpl(struct scm_fp_list *fpl);\n  33:\tvoid unix_peek_fpl(struct scm_fp_list *fpl);\n  34:\tvoid unix_schedule_gc(struct user_struct *user);\n  35:\t\n"
}

75/3 2026/08/31 12:41 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 45706
							output: 35
							thoughts: 1577
						

Thoughts:
**Analyzing UNIX Domain Socket Credentials in SKBs**

Okay, let's break this down systematically. First, I need to confirm the size of the `unix_skb_parms` structure within the `sk_buff`'s control buffer (`skb->cb`).  It looks like the `pid` field (two pointers) takes 16 bytes, the `uid` and `gid` take 8 bytes, the `fp` pointer another 8, and `secid` and `consumed` an additional 8, summing to a total of 40 bytes.  Since the `skb->cb` is 48 bytes on a 64-bit system, that seems like a good fit. But wait, whenever an skb is allocated (e.g. `sock_alloc_send_pskb`), `skb->cb` is initialized to zero using `memset`.

Now I need to see how the data within `unix_skb_parms` is populated. `unix_scm_to_skb` appears to be the key function. It's calling `get_pids` to copy the PIDs, and then it sets `uid`, `gid`, and `fp`.  I'll need to check the exact `get_pids` implementation. I'll need to look at `unix_maybe_add_creds` function.

Alright, `unix_maybe_add_creds` is interesting. If the `UNIXCB(skb).pid[PIDTYPE_TGID]` is not set and the conditions for passing credentials are not met, the code *doesn't* populate `UNIXCB(skb).pid`.  This means `UNIXCB(skb).pid` remains uninitialized. Critically, if `pidfs_register_pids` fails, then `put_pids` is called. Let's look at `__put_pids`, it will set all `pids[type]` to `NULL`.

The next function to check is `unix_skb_to_scm`, which copies the credentials back out. `get_pids` will handle NULL pointers. Okay, if the `pid` is NULL in the `UNIXCB(skb)`, the `get_pids` will be called with NULL, and returns NULL, so no refcount issue. And `pid_vnr(NULL)` is 0.

Now I remember a potential `scm_replace_pid` function. I have to look at how `scm_replace_pid` functions. It uses `pidfs_register_pid` and `get_pid`. Let's focus on the comment that says `/* drop all previous references */`. I suspect there might be a potential issue related to the reference counting of the `pid` structures. Let's review the code's history using git, so I can see what changed. The git diff before the patch shows that before the patch, the code didn't actually drop all previous references, it just overwrote the old pointer, so there was a leak!

I will check the original implementation in the lines 145-230.



76/3 2026/08/31 12:41 tool read-file
0m Args:
{
  "File": "net/core/scm.c",
  "FirstLine": 145,
  "LineCount": 85
}

Results:
{
  "Contents": " 145:\t\t\tkfree(fpl);\n 146:\t\t}\n 147:\t}\n 148:\tEXPORT_SYMBOL(__scm_destroy);\n 149:\t\n 150:\tstatic inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)\n 151:\t{\n 152:\t\tstruct pid *thread_pid;\n 153:\t\tint err;\n 154:\t\n 155:\t\t/* drop all previous references */\n 156:\t\tscm_destroy_cred(scm);\n 157:\t\n 158:\t\terr = pidfs_register_pid(pid);\n 159:\t\tif (unlikely(err))\n 160:\t\t\treturn err;\n 161:\t\n 162:\t\t/* A sender naming its own thread-group sends from the current thread. */\n 163:\t\tif (pid == task_tgid(current))\n 164:\t\t\tthread_pid = task_pid(current);\n 165:\t\telse\n 166:\t\t\tthread_pid = pid;\n 167:\t\n 168:\t\terr = pidfs_register_pid(thread_pid);\n 169:\t\tif (unlikely(err))\n 170:\t\t\treturn err;\n 171:\t\n 172:\t\tscm-\u003epid[PIDTYPE_TGID] = pid;\n 173:\t\tscm-\u003epid[PIDTYPE_PID] = get_pid(thread_pid);\n 174:\t\tscm-\u003ecreds.pid = pid_vnr(pid);\n 175:\t\treturn 0;\n 176:\t}\n 177:\t\n 178:\tint __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)\n 179:\t{\n 180:\t\tconst struct proto_ops *ops = READ_ONCE(sock-\u003eops);\n 181:\t\tstruct cmsghdr *cmsg;\n 182:\t\tint err;\n 183:\t\n 184:\t\tfor_each_cmsghdr(cmsg, msg) {\n 185:\t\t\terr = -EINVAL;\n 186:\t\n 187:\t\t\t/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */\n 188:\t\t\t/* The first check was omitted in \u003c= 2.2.5. The reasoning was\n 189:\t\t\t   that parser checks cmsg_len in any case, so that\n 190:\t\t\t   additional check would be work duplication.\n 191:\t\t\t   But if cmsg_level is not SOL_SOCKET, we do not check\n 192:\t\t\t   for too short ancillary data object at all! Oops.\n 193:\t\t\t   OK, let's add it...\n 194:\t\t\t */\n 195:\t\t\tif (!CMSG_OK(msg, cmsg))\n 196:\t\t\t\tgoto error;\n 197:\t\n 198:\t\t\tif (cmsg-\u003ecmsg_level != SOL_SOCKET)\n 199:\t\t\t\tcontinue;\n 200:\t\n 201:\t\t\tswitch (cmsg-\u003ecmsg_type)\n 202:\t\t\t{\n 203:\t\t\tcase SCM_RIGHTS:\n 204:\t\t\t\tif (!ops || ops-\u003efamily != PF_UNIX)\n 205:\t\t\t\t\tgoto error;\n 206:\t\t\t\terr=scm_fp_copy(cmsg, \u0026p-\u003efp);\n 207:\t\t\t\tif (err\u003c0)\n 208:\t\t\t\t\tgoto error;\n 209:\t\t\t\tbreak;\n 210:\t\t\tcase SCM_CREDENTIALS:\n 211:\t\t\t{\n 212:\t\t\t\tstruct ucred creds;\n 213:\t\t\t\tkuid_t uid;\n 214:\t\t\t\tkgid_t gid;\n 215:\t\t\t\tif (cmsg-\u003ecmsg_len != CMSG_LEN(sizeof(struct ucred)))\n 216:\t\t\t\t\tgoto error;\n 217:\t\t\t\tmemcpy(\u0026creds, CMSG_DATA(cmsg), sizeof(struct ucred));\n 218:\t\t\t\terr = scm_check_creds(\u0026creds);\n 219:\t\t\t\tif (err)\n 220:\t\t\t\t\tgoto error;\n 221:\t\n 222:\t\t\t\tif (!p-\u003epid[PIDTYPE_TGID] ||\n 223:\t\t\t\t    pid_vnr(p-\u003epid[PIDTYPE_TGID]) != creds.pid) {\n 224:\t\t\t\t\tstruct pid *pid;\n 225:\t\t\t\t\terr = -ESRCH;\n 226:\t\t\t\t\tpid = find_get_pid(creds.pid);\n 227:\t\t\t\t\tif (!pid)\n 228:\t\t\t\t\t\tgoto error;\n 229:\t\n"
}

77/3 2026/08/31 12:41 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 49228
							output: 18
							thoughts: 512
						

Thoughts:
**Analyzing the Potential Issue with scm_pidfd_recv**

Okay, so I'm looking at `scm_destroy_cred(scm)` which, as expected, calls `put_pids(scm->pid)`. Makes sense. Now, let's dive into `scm_pidfd_recv` since that's where things get interesting. Line 156 of the code, calls `scm_destroy_cred(scm)`.

In `scm_pidfd_recv`, I see `pidfd` is a local integer initialized via `pidfd = pidfd_prepare(...)`. The function includes an early return if the `pid` is `NULL`, which is normal. The core question is around `pidfd_prepare`. The code suggests `pidfd` could be negative if `pidfd_prepare` fails (or even through `put_cmsg`). I can see that `put_cmsg` might return an error when using `&pidfd`. But hang on... what if `pidfd_prepare` returns a *negative error code* itself? I need to check the definition of `pidfd_prepare` to confirm the possible error handling. I need to know what those error codes are, and what happens when they get passed around in the other function calls. Is the negative `pidfd` value being checked properly throughout the process? I'm going to follow the logic of `pidfd_prepare` and then `put_cmsg` calls to confirm.



78/3 2026/08/31 12:41 tool grepper
0m Args:
{
  "Expression": "pidfd_prepare"
}

Results:
{
  "Output": "fs/notify/fanotify/fanotify_user.c=836=static ssize_t copy_event_to_user(struct fsnotify_group *group,\n--\nfs/notify/fanotify/fanotify_user.c-914-\t\tif (metadata.pid)\nfs/notify/fanotify/fanotify_user.c:915:\t\t\tpidfd = pidfd_prepare(event-\u003epid, pidfd_flags, \u0026pidfd_file);\nfs/notify/fanotify/fanotify_user.c-916-\n--\ninclude/linux/pid.h=86=struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);\ninclude/linux/pid.h:87:int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file);\ninclude/linux/pid.h-88-void do_notify_pidfd(struct task_struct *task);\n--\nkernel/fork.c=1862=static inline void rcu_copy_process(struct task_struct *p)\n--\nkernel/fork.c-1881-/**\nkernel/fork.c:1882: * pidfd_prepare - allocate a new pidfd_file and reserve a pidfd\nkernel/fork.c-1883- * @pid:   the struct pid for which to create a pidfd\n--\nkernel/fork.c-1907- */\nkernel/fork.c:1908:int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file)\nkernel/fork.c-1909-{\n--\nkernel/fork.c=2012=__latent_entropy struct task_struct *copy_process(\n--\nkernel/fork.c-2347-\t\t */\nkernel/fork.c:2348:\t\tretval = pidfd_prepare(pid, flags, \u0026pidfile);\nkernel/fork.c-2349-\t\tif (retval \u003c 0)\n--\nkernel/pid.c=671=static int pidfd_create(struct pid *pid, unsigned int flags)\n--\nkernel/pid.c-675-\nkernel/pid.c:676:\tpidfd = pidfd_prepare(pid, flags, \u0026pidfd_file);\nkernel/pid.c-677-\tif (pidfd \u003c 0)\n--\nnet/core/scm.c=502=static void scm_pidfd_recv(struct sock *sk, struct msghdr *msg,\n--\nnet/core/scm.c-529-\nnet/core/scm.c:530:\tpidfd = pidfd_prepare(pid, flags, \u0026pidfd_file);\nnet/core/scm.c-531-\n--\nnet/core/sock.c=1747=static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type,\n--\nnet/core/sock.c-1772-\nnet/core/sock.c:1773:\tpidfd = pidfd_prepare(peer_pid, flags, \u0026pidfd_file);\nnet/core/sock.c-1774-\tput_pid(peer_pid);\n"
}

79/3 2026/08/31 12:42 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 50875
							output: 35
							thoughts: 718
						

Thoughts:
**Analyzing `sk_getsockopt_peerpidfd` and `SO_PASSPIDFD_THREAD` for PIDFD Handling**

Okay, let's break this down. I'm looking at how we retrieve PIDFDs related to socket peers, specifically through `sk_getsockopt_peerpidfd`. The main thing I'm focused on is the data transfer and error handling.

First, within `sk_getsockopt_peerpidfd`, `len` is critical; it's being bounded by `sizeof(pidfd)` ensuring that we don't overflow the buffer. `pidfd` itself is an `int`, and initialized by `pidfd_prepare`. The code seems careful, checking the return values of `copy_to_sockptr`, and cleaning up with `put_unused_fd` and `fput` if an error occurs. I see we have two copy operations: the first, `copy_to_sockptr(optval, &pidfd, len)`, copies up to 4 bytes from the initialized `pidfd`, and the second one, `copy_to_sockptr(optlen, &len, sizeof(int))`, copies 4 bytes from the initialized `len` to the caller. So far, the direct PIDFD retrieval and transfer look pretty sound, with defensive programming around potential copy failures.

Then, I'm checking how the `SO_PASSPIDFD_THREAD` option is handled within `sk_getsockopt`. If the socket is not AF_UNIX, `EOPNOTSUPP` is returned. In the `SO_PASSPIDFD_THREAD` case, the value `v.val` is set with `sk->sk_scm_pidfd_thread`. I need to track down how `v.val` is ultimately used by `sk_getsockopt`.

In general, the code seems well-structured, but I need to see the complete picture of how the data flows from the socket internals to the user space to be confident, especially in regard to the PIDFD handling. I'll need to drill down deeper into the `net/core/sock.c` and potentially how those values are consumed by the calling application.



80/3 2026/08/31 12:42 tool grepper
0m Args:
{
  "Expression": "case SO_PASSPIDFD:",
  "PathPrefix": "net/core/sock.c"
}

Results:
{
  "Output": "net/core/sock.c=1194=int sk_setsockopt(struct sock *sk, int level, int optname,\n--\nnet/core/sock.c-1572-\nnet/core/sock.c:1573:\tcase SO_PASSPIDFD:\nnet/core/sock.c-1574-\t\tif (sk_is_unix(sk)) {\n--\nnet/core/sock.c=1790=int sk_getsockopt(struct sock *sk, int level, int optname,\n--\nnet/core/sock.c-1946-\nnet/core/sock.c:1947:\tcase SO_PASSPIDFD:\nnet/core/sock.c-1948-\t\tif (!sk_is_unix(sk))\n"
}

81/3 2026/08/31 12:42 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 52308
							output: 36
							thoughts: 25
						

Thoughts:
Here's a summary of my thought process, as requested:

**Diving into `net/core/sock.c`**

Alright, time to dig in. The problem at hand necessitates a look at the core socket code. I'm going to start by reviewing lines 1945-1960 within `net/core/sock.c`. These lines likely involve the crucial part of socket functionality that's causing this issue. I'll be looking for any hints on where the problem might exist by reviewing the surrounding context and functions this area of the code interacts with. Let's see what we can find. I expect this section deals with [insert expectation based on expertise and assumed problem domain - e.g., connection establishment, data transmission, error handling, etc.].



82/3 2026/08/31 12:42 tool read-file
0m Args:
{
  "File": "net/core/sock.c",
  "FirstLine": 1945,
  "LineCount": 20
}

Results:
{
  "Contents": "1945:\t\t\tbreak;\n1946:\t\n1947:\t\tcase SO_PASSPIDFD:\n1948:\t\t\tif (!sk_is_unix(sk))\n1949:\t\t\t\treturn -EOPNOTSUPP;\n1950:\t\n1951:\t\t\tv.val = sk-\u003esk_scm_pidfd;\n1952:\t\t\tbreak;\n1953:\t\n1954:\t\tcase SO_PASSPIDFD_THREAD:\n1955:\t\t\tif (!sk_is_unix(sk))\n1956:\t\t\t\treturn -EOPNOTSUPP;\n1957:\t\n1958:\t\t\tv.val = sk-\u003esk_scm_pidfd_thread;\n1959:\t\t\tbreak;\n1960:\t\n1961:\t\tcase SO_PASSRIGHTS:\n1962:\t\t\tif (!sk_is_unix(sk))\n1963:\t\t\t\treturn -EOPNOTSUPP;\n1964:\t\n"
}

83/3 2026/08/31 12:42 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 52802
							output: 30
							thoughts: 152
						

Thoughts:
**Analyzing Core Dump Parameter Initialization**

Okay, so I'm digging into this. `v.val` being the standard for integer return values in `sk_getsockopt` is a good starting point to keep in mind. Now, let's dive into the core dump mechanism, specifically focusing on how the parameters are being handled. My initial thought is to examine `coredump.c` to understand the flow of execution.

Let's begin by checking for changes within `fs/coredump.c`, hopefully we find the source of this problem there. Within the file, I need to focus on the `coredump_parse` function; that sounds like a relevant starting point. I see `task_pids(cprm->pid, current);` being called in `coredump_parse`. Okay, and within `struct coredump_params`, I see `DECLARE_PIDS(pid, PIDTYPE_TGID);`.

Now, the crucial question is how this `struct coredump_params` is allocated and initialized. I need to understand the lifecycle of this structure to understand how it can go wrong. So, I need to look for where it is allocated or created. Let's go ahead and search for instances of `struct coredump_params` within `fs/coredump.c` to get a better handle on its creation. That should give me the context I need.



84/3 2026/08/31 12:42 tool grepper
0m Args:
{
  "Expression": "coredump_params",
  "PathPrefix": "fs/coredump.c"
}

Results:
{
  "Output": "fs/coredump.c-68-\nfs/coredump.c:69:static bool dump_vma_snapshot(struct coredump_params *cprm);\nfs/coredump.c:70:static void free_vma_snapshot(struct coredump_params *cprm);\nfs/coredump.c-71-\n--\nfs/coredump.c=198=static int cn_print_exe_file(struct core_name *cn, bool name_only)\n--\nfs/coredump.c-238- */\nfs/coredump.c:239:static bool coredump_parse(struct core_name *cn, struct coredump_params *cprm,\nfs/coredump.c-240-\t\t\t   size_t **argv, int *argc)\n--\nfs/coredump.c=623=static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)\n--\nfs/coredump.c-625-\tstruct file *files[2];\nfs/coredump.c:626:\tstruct coredump_params *cp = (struct coredump_params *)info-\u003edata;\nfs/coredump.c-627-\tint err;\n--\nfs/coredump.c-671-#ifdef CONFIG_UNIX\nfs/coredump.c:672:static bool coredump_sock_connect(struct core_name *cn, struct coredump_params *cprm)\nfs/coredump.c-673-{\n--\nfs/coredump.c=787=static inline void coredump_sock_shutdown(struct file *file)\n--\nfs/coredump.c-798-\nfs/coredump.c:799:static bool coredump_sock_request(struct core_name *cn, struct coredump_params *cprm)\nfs/coredump.c-800-{\n--\nfs/coredump.c-861-\nfs/coredump.c:862:static bool coredump_socket(struct core_name *cn, struct coredump_params *cprm)\nfs/coredump.c-863-{\n--\nfs/coredump.c=871=static inline void coredump_sock_shutdown(struct file *file) { }\nfs/coredump.c:872:static inline bool coredump_socket(struct core_name *cn, struct coredump_params *cprm) { return false; }\nfs/coredump.c-873-#endif\n--\nfs/coredump.c-875-/* cprm-\u003edumpable is the snapshot of task dumpability at dump start. */\nfs/coredump.c:876:static inline bool coredump_force_suid_safe(const struct coredump_params *cprm)\nfs/coredump.c-877-{\n--\nfs/coredump.c-881-\nfs/coredump.c:882:static bool coredump_file(struct core_name *cn, struct coredump_params *cprm,\nfs/coredump.c-883-\t\t\t  const struct linux_binfmt *binfmt)\n--\nfs/coredump.c-972-\nfs/coredump.c:973:static bool coredump_pipe(struct core_name *cn, struct coredump_params *cprm,\nfs/coredump.c-974-\t\t\t  size_t *argv, int argc)\n--\nfs/coredump.c=1039=static bool coredump_write(struct core_name *cn,\nfs/coredump.c:1040:\t\t\t  struct coredump_params *cprm,\nfs/coredump.c-1041-\t\t\t  const struct linux_binfmt *binfmt)\n--\nfs/coredump.c-1066-\nfs/coredump.c:1067:static void coredump_cleanup(struct core_name *cn, struct coredump_params *cprm)\nfs/coredump.c-1068-{\n--\nfs/coredump.c-1078-\nfs/coredump.c:1079:static inline bool coredump_skip(const struct coredump_params *cprm,\nfs/coredump.c-1080-\t\t\t\t const struct linux_binfmt *binfmt)\n--\nfs/coredump.c-1090-\nfs/coredump.c:1091:static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\nfs/coredump.c-1092-\t\t\tsize_t **argv, int *argc, const struct linux_binfmt *binfmt)\n--\nfs/coredump.c=1160=void vfs_coredump(const kernel_siginfo_t *siginfo)\n--\nfs/coredump.c-1167-\tint argc = 0;\nfs/coredump.c:1168:\tstruct coredump_params cprm = {\nfs/coredump.c-1169-\t\t.siginfo = siginfo,\n--\nfs/coredump.c-1208- */\nfs/coredump.c:1209:static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)\nfs/coredump.c-1210-{\n--\nfs/coredump.c-1228-\nfs/coredump.c:1229:static int __dump_skip(struct coredump_params *cprm, size_t nr)\nfs/coredump.c-1230-{\n--\nfs/coredump.c-1249-\nfs/coredump.c:1250:int dump_emit(struct coredump_params *cprm, const void *addr, int nr)\nfs/coredump.c-1251-{\n--\nfs/coredump.c=1259=EXPORT_SYMBOL(dump_emit);\nfs/coredump.c-1260-\nfs/coredump.c:1261:void dump_skip_to(struct coredump_params *cprm, unsigned long pos)\nfs/coredump.c-1262-{\n--\nfs/coredump.c=1265=EXPORT_SYMBOL(dump_skip_to);\nfs/coredump.c-1266-\nfs/coredump.c:1267:void dump_skip(struct coredump_params *cprm, size_t nr)\nfs/coredump.c-1268-{\n--\nfs/coredump.c=1271=EXPORT_SYMBOL(dump_skip);\n--\nfs/coredump.c-1273-#ifdef CONFIG_ELF_CORE\nfs/coredump.c:1274:static int dump_emit_page(struct coredump_params *cprm, struct page *page)\nfs/coredump.c-1275-{\n--\nfs/coredump.c=1330=static inline struct page *dump_page_copy(struct page *src, struct page *dst)\n--\nfs/coredump.c-1335-\nfs/coredump.c:1336:int dump_user_range(struct coredump_params *cprm, unsigned long start,\nfs/coredump.c-1337-\t\t    unsigned long len)\n--\nfs/coredump.c-1399-\nfs/coredump.c:1400:int dump_align(struct coredump_params *cprm, int align)\nfs/coredump.c-1401-{\n--\nfs/coredump.c=1676=static struct vm_area_struct *coredump_next_vma(struct vma_iterator *vmi,\n--\nfs/coredump.c-1688-\nfs/coredump.c:1689:static void free_vma_snapshot(struct coredump_params *cprm)\nfs/coredump.c-1690-{\n--\nfs/coredump.c=1703=static int cmp_vma_size(const void *vma_meta_lhs_ptr, const void *vma_meta_rhs_ptr)\n--\nfs/coredump.c-1718- */\nfs/coredump.c:1719:static bool dump_vma_snapshot(struct coredump_params *cprm)\nfs/coredump.c-1720-{\n"
}

85/3 2026/08/31 12:42 llm kmsan-evaluator
1m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 55370
							output: 35
							thoughts: 20
						

Thoughts:
Here's my take on those thoughts:

**Analyzing `vfs_coredump` and `cprm`**

Okay, so I'm digging into `vfs_coredump` and trying to understand its role. Specifically, I need to see how `cprm` is declared and utilized within that context. The goal here is to get a handle on the interaction and any potential dependencies or constraints. I'm assuming there's some interesting logic or data flow that I need to dissect. Let's see what the code reveals. Time to put on the detective hat!



86/3 2026/08/31 12:43 tool read-file
0m Args:
{
  "File": "fs/coredump.c",
  "FirstLine": 1165,
  "LineCount": 20
}

Results:
{
  "Contents": "1165:\t\tconst struct mm_struct *mm = current-\u003emm;\n1166:\t\tconst struct linux_binfmt *binfmt = mm-\u003ebinfmt;\n1167:\t\tint argc = 0;\n1168:\t\tstruct coredump_params cprm = {\n1169:\t\t\t.siginfo = siginfo,\n1170:\t\t\t.limit = rlimit(RLIMIT_CORE),\n1171:\t\t\t/* Snapshot MMF_DUMP_FILTER_* (unlocked) and dumpable for the dump. */\n1172:\t\t\t.mm_flags = __mm_flags_get_word(mm),\n1173:\t\t\t.dumpable = task_exec_state_get_dumpable(current),\n1174:\t\t\t.vma_meta = NULL,\n1175:\t\t\t.cpu = raw_smp_processor_id(),\n1176:\t\t};\n1177:\t\n1178:\t\taudit_core_dumps(siginfo-\u003esi_signo);\n1179:\t\n1180:\t\tif (coredump_skip(\u0026cprm, binfmt))\n1181:\t\t\treturn;\n1182:\t\n1183:\t\tCLASS(prepare_creds, cred)();\n1184:\t\tif (!cred)\n"
}

87/3 2026/08/31 12:43 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 55881
							output: 27
							thoughts: 2114
						

Thoughts:
**Analyzing Potential NULL Pointer Dereferences with PID Handling**

Okay, so I'm digging into this `coredump_params` struct, specifically the `cprm.pid` array, which, as I see it, is initialized to zero thanks to the struct's initialization. I'm focusing on potential null pointer dereferences when working with PIDs. My initial thought is to follow the code paths where the `pid` array is utilized.

First, I examined `pidfs.c` and found `__pidfs_register_pids`, which iterates through `pids` up to a `last` pid type.  I then looked into `pidfs_coredump`, where the loop `for (enum pid_type type = PIDTYPE_PID; type <= pids_last(cprm->pid); type++)` is utilized, which in turn calls `pidfs_coredump_pid`. That's where things get interesting.

I took a look at `pids_last` and figured out how the loop bound is calculated when `DECLARE_PIDS(pid, PIDTYPE_TGID);` is used.  Essentially, the loop iterates over the `PIDTYPE_PID` and `PIDTYPE_TGID` types. Then I went to `pidfs_coredump_pid`, and saw that there's a dereference `attr = READ_ONCE(pid->attr);` and I thought, "Wait, what if `pid` is NULL?" That'd be a crash, which is what I'm looking for!

I went back to trace where `cprm->pid[type]` gets populated. In `coredump_parse` and `coredump_sock_connect`, I saw `task_pids(cprm->pid, current);`, so I followed that.  `task_pids` calls `__task_pids`, which uses a loop that, for `current`, should *never* return NULL `pid` values for `PIDTYPE_PID` or `PIDTYPE_TGID`. That seems safe.

I moved on to look through the `include/linux/pid.h` file, and found other functions, such as `__get_pids`, `__put_pids`, `__swap_pids`, `__pids_equal`, `__task_pids`, and `__get_task_pids`.  These all look reasonable in terms of null-checking because they generally iterate over a known valid range of pid types, or, in the case of `task_pids` functions, receive a `task` which guarantees a PID.

I then started to check the bluetooth changes. In `net/bluetooth/af_bluetooth.c`, I noticed that `sk->sk_peer_pid[PIDTYPE_TGID]` can potentially get initialized via `get_pid(task_tgid(current))`. The struct `sock` uses `DECLARE_PIDS(sk_peer_pid, PIDTYPE_TGID);`, which means `sk->sk_peer_pid[PIDTYPE_PID]` will be 0 (NULL). This looks promising!

I continued to check the functions: `bt_accept_enqueue`, `net/bluetooth/hci_sock.c`, `net/bluetooth/l2cap_sock.c`, and `__sk_destruct`, and realized the PID can be NULL in those functions.

Finally, I checked `net/unix/af_unix.c` to see how `unix_peercred` is used since it uses the same `DECLARE_PIDS` macro.

So, in summary, I'm finding places where PIDs are potentially NULL and then being dereferenced, which is what I was looking for. Now I need to decide if these potential NULLs represent actual vulnerabilities or are handled safely elsewhere.



88/3 2026/08/31 12:44 tool grepper
0m Args:
{
  "Expression": "unix_peercred",
  "PathPrefix": "net/unix"
}

Results:
{
  "Output": "net/unix/af_unix.c=665=static void unix_release_sock(struct sock *sk, int embrion)\n--\nnet/unix/af_unix.c-738-\nnet/unix/af_unix.c:739:struct unix_peercred {\nnet/unix/af_unix.c-740-\tDECLARE_PIDS(peer_pid, PIDTYPE_TGID);\n--\nnet/unix/af_unix.c-743-\nnet/unix/af_unix.c:744:static inline int prepare_peercred(struct unix_peercred *peercred)\nnet/unix/af_unix.c-745-{\n--\nnet/unix/af_unix.c-758-\nnet/unix/af_unix.c:759:static void drop_peercred(struct unix_peercred *peercred)\nnet/unix/af_unix.c-760-{\n--\nnet/unix/af_unix.c=770=static inline void init_peercred(struct sock *sk,\nnet/unix/af_unix.c:771:\t\t\t\t const struct unix_peercred *peercred)\nnet/unix/af_unix.c-772-{\n--\nnet/unix/af_unix.c-776-\nnet/unix/af_unix.c:777:static void update_peercred(struct sock *sk, struct unix_peercred *peercred)\nnet/unix/af_unix.c-778-{\n--\nnet/unix/af_unix.c=805=static int unix_listen(struct socket *sock, int backlog)\n--\nnet/unix/af_unix.c-809-\tstruct unix_sock *u = unix_sk(sk);\nnet/unix/af_unix.c:810:\tstruct unix_peercred peercred = {};\nnet/unix/af_unix.c-811-\n--\nnet/unix/af_unix.c=1631=static int unix_stream_connect(struct socket *sock, struct sockaddr_unsized *uaddr,\n--\nnet/unix/af_unix.c-1636-\tstruct unix_sock *u = unix_sk(sk), *newu, *otheru;\nnet/unix/af_unix.c:1637:\tstruct unix_peercred peercred = {};\nnet/unix/af_unix.c-1638-\tstruct net *net = sock_net(sk);\n--\nnet/unix/af_unix.c=1811=static int unix_socketpair(struct socket *socka, struct socket *sockb)\nnet/unix/af_unix.c-1812-{\nnet/unix/af_unix.c:1813:\tstruct unix_peercred ska_peercred = {}, skb_peercred = {};\nnet/unix/af_unix.c-1814-\tstruct sock *ska = socka-\u003esk, *skb = sockb-\u003esk;\n"
}

89/3 2026/08/31 12:44 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 59578
							output: 20
							thoughts: 253
						

Thoughts:
**My Analysis of Unix Socket Peer Credentials**

Okay, so I'm looking at how peer credentials are handled in these Unix socket functions – `unix_listen`, `unix_stream_connect`, and `unix_socketpair`. I see that in all three, the `struct unix_peercred` is initialized using `{}`. That's the baseline. Now, the key seems to be in `prepare_peercred`.

In `prepare_peercred`, first I see that the process IDs are obtained using `get_task_pids`. Then, these IDs are registered using `pidfs_register_pids`. If there's an error during PID registration, `put_pids` is called for cleanup, and I assume the structure elements become invalid or NULL. If that fails, the function returns an error and sets the struct elements to NULL. This is important to note - failure to register PIDs requires that resources are cleaned up.

If `prepare_peercred` *succeeds*, however, then `peer_pid` and `peer_cred` are fully initialized. It looks like `peer_cred` is populated with the current credential using `get_current_cred()`. So, the entire struct is well defined with all needed credentials.

Now, my next move is to understand how this `struct unix_peercred` is ultimately used. I need to investigate `cred_to_ucred`. Let me check `net/core/sock.c` or wherever else it is called. A search for `cred_to_ucred` should reveal where this credential information is actually translated or used in the overall network stack.



90/3 2026/08/31 12:44 tool grepper
0m Args:
{
  "Expression": "cred_to_ucred"
}

Results:
{
  "Output": "net/core/sock.c=1707=static const struct cred *sk_get_peer_cred(struct sock *sk)\n--\nnet/core/sock.c-1717-\nnet/core/sock.c:1718:static void cred_to_ucred(struct pid *pid, const struct cred *cred,\nnet/core/sock.c-1719-\t\t\t  struct ucred *ucred)\n--\nnet/core/sock.c=1790=int sk_getsockopt(struct sock *sk, int level, int optname,\n--\nnet/core/sock.c-1974-\t\tspin_lock(\u0026sk-\u003esk_peer_lock);\nnet/core/sock.c:1975:\t\tcred_to_ucred(sk-\u003esk_peer_pid[PIDTYPE_TGID], sk-\u003esk_peer_cred,\nnet/core/sock.c-1976-\t\t\t      \u0026peercred);\n"
}

91/3 2026/08/31 12:44 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 60443
							output: 36
							thoughts: 22
						

Thoughts:
Here's my take on those thoughts, tailored for an expert audience:

**Diving into `cred_to_ucred`**

Okay, time to get to the heart of the matter. I need to understand how credentials are being handled at the socket layer. My instinct tells me the place to start is `cred_to_ucred` within `net/core/sock.c`. That's where I anticipate the core mapping between the kernel's credential structures and the user space credentials (ucred) will reside. It's a critical function for security context propagation, so I need to see how it's implemented and identify any potential gotchas or edge cases. I'll be looking for things like how it handles different types of sockets, any implicit assumptions about credential sources, and how it deals with situations where credential information might be incomplete or stale. This function's implementation should give me a solid foundation to understand the behavior of the socket layer.



92/3 2026/08/31 12:44 tool read-file
0m Args:
{
  "File": "net/core/sock.c",
  "FirstLine": 1715,
  "LineCount": 25
}

Results:
{
  "Contents": "1715:\t\treturn cred;\n1716:\t}\n1717:\t\n1718:\tstatic void cred_to_ucred(struct pid *pid, const struct cred *cred,\n1719:\t\t\t\t  struct ucred *ucred)\n1720:\t{\n1721:\t\tucred-\u003epid = pid_vnr(pid);\n1722:\t\tucred-\u003euid = ucred-\u003egid = -1;\n1723:\t\tif (cred) {\n1724:\t\t\tstruct user_namespace *current_ns = current_user_ns();\n1725:\t\n1726:\t\t\tucred-\u003euid = from_kuid_munged(current_ns, cred-\u003eeuid);\n1727:\t\t\tucred-\u003egid = from_kgid_munged(current_ns, cred-\u003eegid);\n1728:\t\t}\n1729:\t}\n1730:\t\n1731:\tstatic int groups_to_user(sockptr_t dst, const struct group_info *src)\n1732:\t{\n1733:\t\tstruct user_namespace *user_ns = current_user_ns();\n1734:\t\tint i;\n1735:\t\n1736:\t\tfor (i = 0; i \u003c src-\u003engroups; i++) {\n1737:\t\t\tgid_t gid = from_kgid_munged(user_ns, src-\u003egid[i]);\n1738:\t\n1739:\t\t\tif (copy_to_sockptr_offset(dst, i * sizeof(gid), \u0026gid, sizeof(gid)))\n"
}

93/3 2026/08/31 12:44 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 61124
							output: 535
							thoughts: 1398
						

Thoughts:
**KMSAN Assessment: No Leaks or Uninitialized Memory Detected**

Okay, let's break this down. My analysis focuses on potential information leaks and the presence of uninitialized memory in the relevant socket code, specifically concerning the `SO_PEERCRED` and `SO_PEERPIDFD` socket options and their associated data structures.

First, I examined `sk_getsockopt`.  Within `case SO_PEERCRED`, the code retrieves peer credentials into a `struct ucred`. I see that `cred_to_ucred` populates all the fields (PID, UID, GID) of `peercred` before copying it to user space.  Also, `struct ucred` is tightly packed; there's no padding on either 32-bit or 64-bit architectures, so there are no concerns about accidentally leaking uninitialized data within the struct.

Next, I looked at `sk_getsockopt_peerpidfd`. Here, the code prepares and returns a file descriptor (`pidfd`) representing the peer's process. The `pidfd` is created by `pidfd_prepare` and then copied to user space. The code properly handles the length and there is no evidence of uninitialized memory being leaked.

The `scm_pidfd_recv` function was next. This function also writes a `pidfd` to user space within an SCM message. It calls `pidfd_prepare` which assigns a value to `pidfd` before sending it.

Then, I went through a checklist to look for uninitialized memory usage. I confirmed that:

1.  `scm_cookie` instances are zeroed during initialization.
2.  `sk->sk_peer_pid` is explicitly zeroed during socket initialization, preventing any potential leaks from uninitialized PIDs.
3.  The `cprm.pid` field within `struct coredump_params` is zero-initialized.
4.  `unix_peercred` structures are initialized and fully populated.
5.  `skb->cb` (where `UNIXCB(skb)` is located) is zeroed upon SKB allocation.
6.  Bitfields within `struct sock` are all correctly initialized (relevant to `sk_scm_pidfd_thread` and related flags) during allocation, specifically by `kzalloc` or `sk_alloc`, both of which zero memory.
7.  I found no instances where uninitialized memory is read or leaked across the user/kernel boundary.
8.  I considered the possibility of memory corruption, use-after-free, reference counting errors, NULL dereferences, or locking bugs, but this would be caught by KASAN or standard debuggers, like LOCKDEP or PROVE\_LOCKING.

I then did a final review against KMSAN-specific criteria:

*   I saw no cases of kernel structures being sent to user space where fields or padding might not be fully initialized.
*   I found no conditional logic or branching that relies on uninitialized variables.
*   I observed no allocations or initializations of complex data structures with uninitialized fields that could lead to issues.
*   There are no cases of bounds checks or length checks allowing access to uninitialized bytes.

Based on this comprehensive analysis, I'm confident that there are no KMSAN-relevant issues. The code appears to be clean with no potential for information leaks from uninitialized memory. Therefore, NeedsKMSAN should be false, and I'll call set-results with this conclusion and my detailed reasoning.



94/3 2026/08/31 12:45 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series introduces support for thread pidfds in UNIX domain sockets and coredump mechanisms (SO_PASSPIDFD_THREAD, SO_PEERPIDFD_THREAD).\n\nKey areas analyzed for uninitialized memory risks:\n1. `DECLARE_PIDS` arrays in `struct scm_cookie`, `struct sock`, `struct coredump_params`, `struct unix_peercred`, and `struct unix_skb_parms`:\n   - `struct sock` instances are allocated with `sk_alloc()` (zeroed) and explicitly reset in `sock_init_data_uid()` with `memset(sk-\u003esk_peer_pid, 0, ...)`.\n   - `struct scm_cookie` objects are consistently zero-initialized (either via designated zero initializers `= {}` or `memset(..., 0, sizeof(...))`) across all callers (`scm_send`, `unix_destruct_scm`, `__unix_dgram_recvmsg`, `unix_stream_read_generic`, `hci_sock_recvmsg`, `netlink_recvmsg`).\n   - `struct unix_peercred` instances are zero-initialized (`= {}`) in `unix_listen`, `unix_stream_connect`, and `unix_socketpair` before `prepare_peercred()`.\n   - `skb-\u003ecb` containing `struct unix_skb_parms` is zeroed during `__alloc_skb()`.\n   - `struct coredump_params` is zero-initialized via designated initializers in `vfs_coredump()`.\n2. Socket option handlers and cmsg helpers:\n   - `sk_getsockopt_peerpidfd()` copies an initialized `int pidfd` and `int len` to user space.\n   - `scm_pidfd_recv()` initializes `pidfd` from `pidfd_prepare()` before writing via `put_cmsg()`.\n   - No structure padding or uninitialized fields are exposed to userspace or read in conditional logic.\n3. Potential issues in this patch series (e.g. pid reference management, use-after-free, locking around `sk_peer_lock`, NULL dereference) are detectable by KASAN, LOCKDEP, and standard kernel debugging facilities.\n\nThus, there are no uninitialized memory risks warranting a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series introduces support for thread pidfds in UNIX domain sockets and coredump mechanisms (SO_PASSPIDFD_THREAD, SO_PEERPIDFD_THREAD).\n\nKey areas analyzed for uninitialized memory risks:\n1. `DECLARE_PIDS` arrays in `struct scm_cookie`, `struct sock`, `struct coredump_params`, `struct unix_peercred`, and `struct unix_skb_parms`:\n   - `struct sock` instances are allocated with `sk_alloc()` (zeroed) and explicitly reset in `sock_init_data_uid()` with `memset(sk-\u003esk_peer_pid, 0, ...)`.\n   - `struct scm_cookie` objects are consistently zero-initialized (either via designated zero initializers `= {}` or `memset(..., 0, sizeof(...))`) across all callers (`scm_send`, `unix_destruct_scm`, `__unix_dgram_recvmsg`, `unix_stream_read_generic`, `hci_sock_recvmsg`, `netlink_recvmsg`).\n   - `struct unix_peercred` instances are zero-initialized (`= {}`) in `unix_listen`, `unix_stream_connect`, and `unix_socketpair` before `prepare_peercred()`.\n   - `skb-\u003ecb` containing `struct unix_skb_parms` is zeroed during `__alloc_skb()`.\n   - `struct coredump_params` is zero-initialized via designated initializers in `vfs_coredump()`.\n2. Socket option handlers and cmsg helpers:\n   - `sk_getsockopt_peerpidfd()` copies an initialized `int pidfd` and `int len` to user space.\n   - `scm_pidfd_recv()` initializes `pidfd` from `pidfd_prepare()` before writing via `put_cmsg()`.\n   - No structure padding or uninitialized fields are exposed to userspace or read in conditional logic.\n3. Potential issues in this patch series (e.g. pid reference management, use-after-free, locking around `sk_peer_lock`, NULL dereference) are detectable by KASAN, LOCKDEP, and standard kernel debugging facilities.\n\nThus, there are no uninitialized memory risks warranting a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)