Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

af_unix: introduce and use scm_replace_pid() helper

Existing logic in __scm_send() related to filling an struct scm_cookie
with a proper struct pid reference is already pretty tricky. Let's
simplify it a bit by introducing a new helper. This helper will be
extended in one of the next patches.

Cc: linux-kernel@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Simon Horman <horms@kernel.org>
Cc: Willem de Bruijn <willemb@google.com>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Kuniyuki Iwashima <kuniyu@google.com>
Cc: Lennart Poettering <mzxreary@0pointer.de>
Cc: Luca Boccassi <bluca@debian.org>
Cc: David Rheinsberg <david@readahead.eu>
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Link: https://lore.kernel.org/20250703222314.309967-4-aleksandr.mikhalitsyn@canonical.com
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Alexander Mikhalitsyn and committed by
Christian Brauner
30580dc9 ee479762

+19 -3
+19 -3
net/core/scm.c
··· 145 145 } 146 146 EXPORT_SYMBOL(__scm_destroy); 147 147 148 + static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid) 149 + { 150 + /* drop all previous references */ 151 + scm_destroy_cred(scm); 152 + 153 + scm->pid = pid; 154 + scm->creds.pid = pid_vnr(pid); 155 + return 0; 156 + } 157 + 148 158 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p) 149 159 { 150 160 const struct proto_ops *ops = READ_ONCE(sock->ops); ··· 199 189 if (err) 200 190 goto error; 201 191 202 - p->creds.pid = creds.pid; 203 192 if (!p->pid || pid_vnr(p->pid) != creds.pid) { 204 193 struct pid *pid; 205 194 err = -ESRCH; 206 195 pid = find_get_pid(creds.pid); 207 196 if (!pid) 208 197 goto error; 209 - put_pid(p->pid); 210 - p->pid = pid; 198 + 199 + /* pass a struct pid reference from 200 + * find_get_pid() to scm_replace_pid(). 201 + */ 202 + err = scm_replace_pid(p, pid); 203 + if (err) { 204 + put_pid(pid); 205 + goto error; 206 + } 211 207 } 212 208 213 209 err = -EINVAL;