net: fix a pidfd leak with a short SO_PEERPIDFD optlen
Subsystems (7)
Christian Brauner
maintainercover net: fix a pidfd leak with a short SO_PEERPIDFD optlen
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.
---
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
Christian Brauner
maintainerPATCH 1/2 net: don't leak pidfds through a short SO_PEERPIDFD optlen
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.
--- net/core/sock.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/core/sock.c +4 -2
@@ -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]);
Signature · 1 line
2.53.0
Re: [PATCH net 1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen
Quoted 27 lines · Christian Brauner
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 viapidfd_prepare(), copy zero to three bytes of the fd number to userspace, write the short optlen back and thenfd_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 forSCM_PIDFD. Sinceput_cmsg()doesn't fail on truncation it checks the size of the control
buffer itself and setsMSG_CTRUNCbefore 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: addSO_PEERPIDFD_THREADto get a thread-specific pidfd")SO_PEERPIDFDandSO_PEERPIDFD_THREADshare the helper and the latter inherited the bug,
so this fixes both.
Quoted 4 lines
Fixes 7b26952a91cf ("net: core: add getsockopt SO_PEERPIDFD")Reported-by Sashiko <[email protected]>
Ugh, I wonder how I didn't notice this before... :(
Thanks, Christian!
Quoted 24 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>---
net/core/sock.c | 6 ++++--1 file changed, 4 insertions(+), 2 deletions(-)net/core/sock.c +0
-0@@ -1746,8 +1746,10 @@ static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type, struct pid *peer_pid; int pidfd;net/core/sock.c +1
-2-if (len >sizeof (pidfd)) - len =sizeof (pidfd); +/* Userspace can't close a pidfd whose number it didn't get. */
net/core/sock.c +3
-0+if (len <sizeof (pidfd)) +return -EINVAL ; + len =sizeof (pidfd);
spin_lock(&sk->sk_peer_lock);peer_pid=get_pid(sk->sk_peer_pid[type]);
Re: [PATCH net 1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen
In reply to Christian BraunerQuoted 23 lines · Christian Brauner
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 viapidfd_prepare(), copy zero to three bytes of the fd number to userspace, write the short optlen back and thenfd_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 forSCM_PIDFD. Sinceput_cmsg()doesn't fail on truncation it checks the size of the control buffer itself and setsMSG_CTRUNCbefore 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: addSO_PEERPIDFD_THREADto get a thread-specific pidfd")SO_PEERPIDFDandSO_PEERPIDFD_THREADshare the helper and the latter inherited the bug, so this fixes both.
Quoted 5 lines
Fixes 7b26952a91cf ("net: core: add getsockopt SO_PEERPIDFD")Reported-by Sashiko <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Christian Brauner
maintainerPATCH 2/2 selftests/net: check that a short SO_PEERPIDFD optlen fails
In reply to Christian BraunerCover 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.
--- tools/testing/selftests/net/af_unix/scm_pidfd.c | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
tools/testing/selftests/net/af_unix/scm_pidfd.c +51 -0
@@ -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
Signature · 1 line
2.53.0
Re: [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails
Quoted 8 lines · Christian Brauner
Cover
SO_PEERPIDFDandSO_PEERPIDFD_THREADwith an optlen of 0 to 3:-
getsockopt()fails with EINVAL
- optlen and the buffer stay untouched
- no pidfd is installed in the file descriptor tableCheck that a full-sized buffer still gets a pidfd.
Quoted 72 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>---
tools/testing/selftests/net/af_unix/scm_pidfd.c | 51
+++++++++++++++++++++++++1 file changed, 51 insertions(+)tools/testing/selftests/net/af_unix/scm_pidfd.c +0
-0@@ -1155,4 +1155,55 @@ TEST(so_peerpidfd_thread) close(server); }
+/* The lowest free file descriptor, where the next pidfd would land. */tools/testing/selftests/net/af_unix/scm_pidfd.c +14
-0+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 };
tools/testing/selftests/net/af_unix/scm_pidfd.c +13
-0+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],
tools/testing/selftests/net/af_unix/scm_pidfd.c +6
-0+ &pidfd, &optlen)); +ASSERT_EQ (EINVAL , errno); +EXPECT_EQ (len, optlen); +EXPECT_EQ (-1 , pidfd); + +/* No pidfd was installed behind our back. */
tools/testing/selftests/net/af_unix/scm_pidfd.c +7
-0+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],
tools/testing/selftests/net/af_unix/scm_pidfd.c +10
-0+ &pidfd, &optlen)); +EXPECT_EQ (sizeof (pidfd), optlen); +EXPECT_EQ (free_fd, pidfd); +close (pidfd); + } + +close (sv[0 ]); +close (sv[1 ]); +} +TEST_HARNESS_MAIN
Re: [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails
In reply to Christian BraunerQuoted 9 lines · Christian Brauner
Cover
SO_PEERPIDFDandSO_PEERPIDFD_THREADwith an optlen of 0 to 3:-
getsockopt()fails with EINVAL
- optlen and the buffer stay untouched
- no pidfd is installed in the file descriptor tableCheck that a full-sized buffer still gets a pidfd.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Christian Brauner
maintainerQuoted 14 lines · Christian Brauner
SO_PEERPIDFDcaps optlen at sizeof(int) but never rejects a shorter one. With an optlen of 0 to 3getsockopt()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_PEERPIDFDwent in for v6.5. When I moved the code into a helper forSO_PEERPIDFD_THREADin 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_PIDFDalready does the equivalent and setsMSG_CTRUNCbefore it allocates anything. The second patch adds a selftest.
Quoted 6 lines
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) <[email protected]>---
Are you taking this, Jakub, or should I throw this into the vfs.fixes pile?
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Tue, 17 Nov 2026 16:25:29 +0100 you wrote:
SO_PEERPIDFDcaps optlen at sizeof(int) but never rejects a shorter one. With an optlen of 0 to 3getsockopt()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_PEERPIDFDwent in for v6.5. When I moved the code into a helper forSO_PEERPIDFD_THREADin v7.4-rc1 I kept the bug, so the new option has it too.[...]
Here is the summary with links:
- [net,1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen
https://git.kernel.org/netdev/net/c/fa827805a629 - [net,2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails https://git.kernel.org/netdev/net/c/c9b3eb7bfd7f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html