Skip to content

Commit 20b70e5

Browse files
committed
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 <[email protected]> for pointing out that we need to limit this to AF_UNIX sockets for now. Link: https://lore.kernel.org/[email protected] Reviewed-by: David Rheinsberg <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent a71f402 commit 20b70e5

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

net/core/sock.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@
148148

149149
#include <linux/ethtool.h>
150150

151+
#include <uapi/linux/pidfd.h>
152+
151153
#include "dev.h"
152154

153155
static DEFINE_MUTEX(proto_list_mutex);
@@ -1879,6 +1881,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
18791881
{
18801882
struct pid *peer_pid;
18811883
struct file *pidfd_file = NULL;
1884+
unsigned int flags = 0;
18821885
int pidfd;
18831886

18841887
if (len > sizeof(pidfd))
@@ -1891,18 +1894,17 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
18911894
if (!peer_pid)
18921895
return -ENODATA;
18931896

1894-
pidfd = pidfd_prepare(peer_pid, 0, &pidfd_file);
1897+
/* The use of PIDFD_STALE requires stashing of struct pid
1898+
* on pidfs with pidfs_register_pid() and only AF_UNIX
1899+
* were prepared for this.
1900+
*/
1901+
if (sk->sk_family == AF_UNIX)
1902+
flags = PIDFD_STALE;
1903+
1904+
pidfd = pidfd_prepare(peer_pid, flags, &pidfd_file);
18951905
put_pid(peer_pid);
1896-
if (pidfd < 0) {
1897-
/*
1898-
* dbus-broker relies on -EINVAL being returned
1899-
* to indicate ESRCH. Paper over it until this
1900-
* is fixed in userspace.
1901-
*/
1902-
if (pidfd == -ESRCH)
1903-
pidfd = -EINVAL;
1906+
if (pidfd < 0)
19041907
return pidfd;
1905-
}
19061908

19071909
if (copy_to_sockptr(optval, &pidfd, len) ||
19081910
copy_to_sockptr(optlen, &len, sizeof(int))) {

0 commit comments

Comments
 (0)