* [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen
@ 2026-11-17 15:25 Christian Brauner
2026-11-17 15:25 ` [PATCH net 1/2] net: don't leak pidfds through " Christian Brauner
2026-11-17 15:25 ` [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails Christian Brauner
0 siblings, 2 replies; 5+ messages in thread
From: Christian Brauner @ 2026-11-17 15:25 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, Alexander Mikhalitsyn, netdev,
linux-fsdevel, Christian Brauner (Amutable), stable, Sashiko
SO_PEERPIDFD caps optlen at sizeof(int) but never rejects a shorter one.
With an optlen of 0 to 3 getsockopt() still installs a pidfd and copies
only part of the fd number to userspace, or none of it. The caller can't
close an fd it doesn't know about, so the pidfd sticks around until the
process exits.
That's been the case since SO_PEERPIDFD went in for v6.5. When I moved
the code into a helper for SO_PEERPIDFD_THREAD in v7.4-rc1 I kept the
bug, so the new option has it too.
So just fail with EINVAL before we prepare the pidfd. SCM_PIDFD already
does the equivalent and sets MSG_CTRUNC before it allocates anything.
The second patch adds a selftest.
Note that before v7.4 the code still lives inline in sk_getsockopt(). So
the fix won't apply to the stable trees as is but the change there is
the same.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (2):
net: don't leak pidfds through a short SO_PEERPIDFD optlen
selftests/net: check that a short SO_PEERPIDFD optlen fails
net/core/sock.c | 6 ++-
tools/testing/selftests/net/af_unix/scm_pidfd.c | 51 +++++++++++++++++++++++++
2 files changed, 55 insertions(+), 2 deletions(-)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261117-work-net-peerpidfd_short_optlen-91a1806c0d6d
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net 1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen 2026-11-17 15:25 [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen Christian Brauner @ 2026-11-17 15:25 ` Christian Brauner 2026-11-17 16:39 ` Alexander Mikhalitsyn 2026-11-17 15:25 ` [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails Christian Brauner 1 sibling, 1 reply; 5+ messages in thread From: Christian Brauner @ 2026-11-17 15:25 UTC (permalink / raw) To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni Cc: Simon Horman, Kuniyuki Iwashima, Alexander Mikhalitsyn, netdev, linux-fsdevel, Christian Brauner (Amutable), stable, Sashiko sk_getsockopt_peerpidfd() caps the caller's optlen at sizeof(int) but never checks that it is at least that large. sk_getsockopt() only rejects a negative optlen. So with an optlen of 0 to 3 we still allocate a pidfd via pidfd_prepare(), copy zero to three bytes of the fd number to userspace, write the short optlen back and then fd_install() the pidfd anyway. getsockopt() reports success but the caller only got part of the fd number or none of it at all. It has no sane way to find out which fd it has to close and the pidfd stays in its file descriptor table until the process exits. Calling it in a loop fills up the table. scm_pidfd_recv() deals with the same problem for SCM_PIDFD. Since put_cmsg() doesn't fail on truncation it checks the size of the control buffer itself and sets MSG_CTRUNC before it allocates a pidfd. There's no point in handing out part of a file descriptor number. So fail with EINVAL if optlen is smaller than sizeof(int) before we prepare a pidfd. Since commit e10899b2a098 ("net: add SO_PEERPIDFD_THREAD to get a thread-specific pidfd") SO_PEERPIDFD and SO_PEERPIDFD_THREAD share the helper and the latter inherited the bug, so this fixes both. Fixes: 7b26952a91cf ("net: core: add getsockopt SO_PEERPIDFD") Cc: stable@vger.kernel.org Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://lore.kernel.org/all/sashiko-outbox-205984@kernel.org Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> --- net/core/sock.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index ad414878dc5ba..74a00a5e2d116 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1746,8 +1746,10 @@ static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type, struct pid *peer_pid; int pidfd; - if (len > sizeof(pidfd)) - len = sizeof(pidfd); + /* Userspace can't close a pidfd whose number it didn't get. */ + if (len < sizeof(pidfd)) + return -EINVAL; + len = sizeof(pidfd); spin_lock(&sk->sk_peer_lock); peer_pid = get_pid(sk->sk_peer_pid[type]); -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net 1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen 2026-11-17 15:25 ` [PATCH net 1/2] net: don't leak pidfds through " Christian Brauner @ 2026-11-17 16:39 ` Alexander Mikhalitsyn 0 siblings, 0 replies; 5+ messages in thread From: Alexander Mikhalitsyn @ 2026-11-17 16:39 UTC (permalink / raw) To: Christian Brauner, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni Cc: Simon Horman, Kuniyuki Iwashima, Alexander Mikhalitsyn, netdev, linux-fsdevel, stable, Sashiko On Tue, 2026-11-17 at 16:25 +0100, Christian Brauner wrote: > sk_getsockopt_peerpidfd() caps the caller's optlen at sizeof(int) but > never checks that it is at least that large. sk_getsockopt() only > rejects a negative optlen. So with an optlen of 0 to 3 we still > allocate a pidfd via pidfd_prepare(), copy zero to three bytes of the > fd number to userspace, write the short optlen back and then > fd_install() the pidfd anyway. > > getsockopt() reports success but the caller only got part of the fd > number or none of it at all. It has no sane way to find out which fd > it > has to close and the pidfd stays in its file descriptor table until > the > process exits. Calling it in a loop fills up the table. > > scm_pidfd_recv() deals with the same problem for SCM_PIDFD. Since > put_cmsg() doesn't fail on truncation it checks the size of the > control > buffer itself and sets MSG_CTRUNC before it allocates a pidfd. > > There's no point in handing out part of a file descriptor number. So > fail with EINVAL if optlen is smaller than sizeof(int) before we > prepare a pidfd. Since commit e10899b2a098 ("net: add > SO_PEERPIDFD_THREAD to get a thread-specific pidfd") SO_PEERPIDFD and > SO_PEERPIDFD_THREAD share the helper and the latter inherited the > bug, > so this fixes both. > > Fixes: 7b26952a91cf ("net: core: add getsockopt SO_PEERPIDFD") > Cc: stable@vger.kernel.org > Reported-by: Sashiko <sashiko-bot@kernel.org> > Closes: https://lore.kernel.org/all/sashiko-outbox-205984@kernel.org Ugh, I wonder how I didn't notice this before... :( Thanks, Christian! Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com> > Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> > --- > net/core/sock.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/net/core/sock.c b/net/core/sock.c > index ad414878dc5ba..74a00a5e2d116 100644 > --- a/net/core/sock.c > +++ b/net/core/sock.c > @@ -1746,8 +1746,10 @@ static int sk_getsockopt_peerpidfd(struct sock > *sk, enum pid_type type, > struct pid *peer_pid; > int pidfd; > > - if (len > sizeof(pidfd)) > - len = sizeof(pidfd); > + /* Userspace can't close a pidfd whose number it didn't get. > */ > + if (len < sizeof(pidfd)) > + return -EINVAL; > + len = sizeof(pidfd); > > spin_lock(&sk->sk_peer_lock); > peer_pid = get_pid(sk->sk_peer_pid[type]); ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails 2026-11-17 15:25 [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen Christian Brauner 2026-11-17 15:25 ` [PATCH net 1/2] net: don't leak pidfds through " Christian Brauner @ 2026-11-17 15:25 ` Christian Brauner 2026-11-17 16:39 ` Alexander Mikhalitsyn 1 sibling, 1 reply; 5+ messages in thread From: Christian Brauner @ 2026-11-17 15:25 UTC (permalink / raw) To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni Cc: Simon Horman, Kuniyuki Iwashima, Alexander Mikhalitsyn, netdev, linux-fsdevel, Christian Brauner (Amutable) Cover SO_PEERPIDFD and SO_PEERPIDFD_THREAD with an optlen of 0 to 3: - getsockopt() fails with EINVAL - optlen and the buffer stay untouched - no pidfd is installed in the file descriptor table Check that a full-sized buffer still gets a pidfd. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> --- tools/testing/selftests/net/af_unix/scm_pidfd.c | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tools/testing/selftests/net/af_unix/scm_pidfd.c b/tools/testing/selftests/net/af_unix/scm_pidfd.c index cbe53fab44fc5..c26f055a19032 100644 --- a/tools/testing/selftests/net/af_unix/scm_pidfd.c +++ b/tools/testing/selftests/net/af_unix/scm_pidfd.c @@ -1155,4 +1155,55 @@ TEST(so_peerpidfd_thread) close(server); } +/* The lowest free file descriptor, where the next pidfd would land. */ +static int lowest_free_fd(int fd) +{ + int nfd; + + nfd = dup(fd); + if (nfd >= 0) + close(nfd); + return nfd; +} + +/* A short optlen must fail without installing a pidfd. */ +TEST(so_peerpidfd_short_optlen) +{ + const int optnames[] = { SO_PEERPIDFD, SO_PEERPIDFD_THREAD }; + int sv[2], i, free_fd, pidfd; + socklen_t len, optlen; + + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sv)); + + for (i = 0; i < ARRAY_SIZE(optnames); i++) { + for (len = 0; len < sizeof(pidfd); len++) { + free_fd = lowest_free_fd(sv[0]); + ASSERT_LE(0, free_fd); + + pidfd = -1; + optlen = len; + ASSERT_EQ(-1, getsockopt(sv[0], SOL_SOCKET, optnames[i], + &pidfd, &optlen)); + ASSERT_EQ(EINVAL, errno); + EXPECT_EQ(len, optlen); + EXPECT_EQ(-1, pidfd); + + /* No pidfd was installed behind our back. */ + ASSERT_EQ(free_fd, lowest_free_fd(sv[0])); + } + + /* A full-sized buffer still gets the pidfd. */ + free_fd = lowest_free_fd(sv[0]); + optlen = sizeof(pidfd); + ASSERT_EQ(0, getsockopt(sv[0], SOL_SOCKET, optnames[i], + &pidfd, &optlen)); + EXPECT_EQ(sizeof(pidfd), optlen); + EXPECT_EQ(free_fd, pidfd); + close(pidfd); + } + + close(sv[0]); + close(sv[1]); +} + TEST_HARNESS_MAIN -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails 2026-11-17 15:25 ` [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails Christian Brauner @ 2026-11-17 16:39 ` Alexander Mikhalitsyn 0 siblings, 0 replies; 5+ messages in thread From: Alexander Mikhalitsyn @ 2026-11-17 16:39 UTC (permalink / raw) To: Christian Brauner, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni Cc: Simon Horman, Kuniyuki Iwashima, Alexander Mikhalitsyn, netdev, linux-fsdevel On Tue, 2026-11-17 at 16:25 +0100, Christian Brauner wrote: > Cover SO_PEERPIDFD and SO_PEERPIDFD_THREAD with an optlen of 0 to 3: > > - getsockopt() fails with EINVAL > - optlen and the buffer stay untouched > - no pidfd is installed in the file descriptor table > > Check that a full-sized buffer still gets a pidfd. > Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com> > Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> > --- > tools/testing/selftests/net/af_unix/scm_pidfd.c | 51 > +++++++++++++++++++++++++ > 1 file changed, 51 insertions(+) > > diff --git a/tools/testing/selftests/net/af_unix/scm_pidfd.c > b/tools/testing/selftests/net/af_unix/scm_pidfd.c > index cbe53fab44fc5..c26f055a19032 100644 > --- a/tools/testing/selftests/net/af_unix/scm_pidfd.c > +++ b/tools/testing/selftests/net/af_unix/scm_pidfd.c > @@ -1155,4 +1155,55 @@ TEST(so_peerpidfd_thread) > close(server); > } > > +/* The lowest free file descriptor, where the next pidfd would land. > */ > +static int lowest_free_fd(int fd) > +{ > + int nfd; > + > + nfd = dup(fd); > + if (nfd >= 0) > + close(nfd); > + return nfd; > +} > + > +/* A short optlen must fail without installing a pidfd. */ > +TEST(so_peerpidfd_short_optlen) > +{ > + const int optnames[] = { SO_PEERPIDFD, SO_PEERPIDFD_THREAD > }; > + int sv[2], i, free_fd, pidfd; > + socklen_t len, optlen; > + > + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sv)); > + > + for (i = 0; i < ARRAY_SIZE(optnames); i++) { > + for (len = 0; len < sizeof(pidfd); len++) { > + free_fd = lowest_free_fd(sv[0]); > + ASSERT_LE(0, free_fd); > + > + pidfd = -1; > + optlen = len; > + ASSERT_EQ(-1, getsockopt(sv[0], SOL_SOCKET, > optnames[i], > + &pidfd, &optlen)); > + ASSERT_EQ(EINVAL, errno); > + EXPECT_EQ(len, optlen); > + EXPECT_EQ(-1, pidfd); > + > + /* No pidfd was installed behind our back. > */ > + ASSERT_EQ(free_fd, lowest_free_fd(sv[0])); > + } > + > + /* A full-sized buffer still gets the pidfd. */ > + free_fd = lowest_free_fd(sv[0]); > + optlen = sizeof(pidfd); > + ASSERT_EQ(0, getsockopt(sv[0], SOL_SOCKET, > optnames[i], > + &pidfd, &optlen)); > + EXPECT_EQ(sizeof(pidfd), optlen); > + EXPECT_EQ(free_fd, pidfd); > + close(pidfd); > + } > + > + close(sv[0]); > + close(sv[1]); > +} > + > TEST_HARNESS_MAIN ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-17 16:39 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-17 15:25 [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen Christian Brauner 2026-11-17 15:25 ` [PATCH net 1/2] net: don't leak pidfds through " Christian Brauner 2026-11-17 16:39 ` Alexander Mikhalitsyn 2026-11-17 15:25 ` [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails Christian Brauner 2026-11-17 16:39 ` Alexander Mikhalitsyn
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox