Unverified Commit 20b70e58 authored by Christian Brauner's avatar Christian Brauner
Browse files

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 <aleksandr.mikhalitsyn@canonical.com>
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: default avatarDavid Rheinsberg <david@readahead.eu>
Reviewed-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent a71f402a
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -148,6 +148,8 @@

#include <linux/ethtool.h>

#include <uapi/linux/pidfd.h>

#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);
		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.
		/* 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 (pidfd == -ESRCH)
				pidfd = -EINVAL;
		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))) {