[PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen

From
Christian Brauner <brauner@kernel.org>
Date
2026-11-17 15:26:02
Also in
linux-fsdevel, stable
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
Thread (5 messages) 5 messages, 2 authors, last reply 5h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help