]> www.infradead.org Git - users/willy/xarray.git/commitdiff
af_unix: introduce and use scm_replace_pid() helper
authorAlexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Thu, 3 Jul 2025 22:23:07 +0000 (00:23 +0200)
committerChristian Brauner <brauner@kernel.org>
Fri, 4 Jul 2025 07:32:35 +0000 (09:32 +0200)
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>
net/core/scm.c

index 0225bd94170f79d61b6a05aeb40647965a3f8fd6..045ab5bdac7d1640d980a4b3b39b2403df8f414a 100644 (file)
@@ -145,6 +145,16 @@ void __scm_destroy(struct scm_cookie *scm)
 }
 EXPORT_SYMBOL(__scm_destroy);
 
+static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
+{
+       /* drop all previous references */
+       scm_destroy_cred(scm);
+
+       scm->pid = pid;
+       scm->creds.pid = pid_vnr(pid);
+       return 0;
+}
+
 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
 {
        const struct proto_ops *ops = READ_ONCE(sock->ops);
@@ -189,15 +199,21 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
                        if (err)
                                goto error;
 
-                       p->creds.pid = creds.pid;
                        if (!p->pid || pid_vnr(p->pid) != creds.pid) {
                                struct pid *pid;
                                err = -ESRCH;
                                pid = find_get_pid(creds.pid);
                                if (!pid)
                                        goto error;
-                               put_pid(p->pid);
-                               p->pid = pid;
+
+                               /* pass a struct pid reference from
+                                * find_get_pid() to scm_replace_pid().
+                                */
+                               err = scm_replace_pid(p, pid);
+                               if (err) {
+                                       put_pid(pid);
+                                       goto error;
+                               }
                        }
 
                        err = -EINVAL;