From: Christian Brauner Date: Fri, 25 Apr 2025 08:11:33 +0000 (+0200) Subject: net, pidfs: enable handing out pidfds for reaped sk->sk_peer_pid X-Git-Tag: v6.16-rc1~220^2~3^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20b70e58961b855e61f2c6a7a19a38a64ad1bed6;p=thirdparty%2Flinux.git net, pidfs: enable handing out pidfds for reaped sk->sk_peer_pid Now that all preconditions are met, allow handing out pidfs for reaped sk->sk_peer_pids. Thanks to Alexander Mikhalitsyn for pointing out that we need to limit this to AF_UNIX sockets for now. Link: https://lore.kernel.org/20250425-work-pidfs-net-v2-4-450a19461e75@kernel.org Reviewed-by: David Rheinsberg Reviewed-by: Kuniyuki Iwashima Signed-off-by: Christian Brauner --- diff --git a/net/core/sock.c b/net/core/sock.c index b969d22106564..180f441f4ab65 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -148,6 +148,8 @@ #include +#include + #include "dev.h" static DEFINE_MUTEX(proto_list_mutex); @@ -1879,6 +1881,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname, { struct pid *peer_pid; struct file *pidfd_file = NULL; + unsigned int flags = 0; int pidfd; if (len > sizeof(pidfd)) @@ -1891,18 +1894,17 @@ int sk_getsockopt(struct sock *sk, int level, int optname, if (!peer_pid) return -ENODATA; - pidfd = pidfd_prepare(peer_pid, 0, &pidfd_file); + /* 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) { - /* - * dbus-broker relies on -EINVAL being returned - * to indicate ESRCH. Paper over it until this - * is fixed in userspace. - */ - if (pidfd == -ESRCH) - pidfd = -EINVAL; + if (pidfd < 0) return pidfd; - } if (copy_to_sockptr(optval, &pidfd, len) || copy_to_sockptr(optlen, &len, sizeof(int))) {