🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
5 messages
5 total messages Started by Christian Brauner Tue, 17 Nov 2026 15:25
[PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen
#17943
Author: Christian Brauner
Date: Tue, 17 Nov 2026 15:25
31 lines
9920 bytes
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
[PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails
#17944
Author: Christian Brauner
Date: Tue, 17 Nov 2026 15:25
77 lines
10519 bytes
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
Re: [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails
#17981
Author: Alexander Mikhalitsyn
Date: Tue, 17 Nov 2026 16:39
85 lines
13967 bytes
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
Re: [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen
#18507
Author: Christian Brauner
Date: Wed, 18 Nov 2026 12:37
24 lines
8819 bytes
On Tue, Nov 17, 2026 at 04:25:29PM +0100, Christian Brauner wrote:
> 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>
> ---

Are you taking this, Jakub, or should I throw this into the vfs.fixes pile?
Re: [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen
#20203
Author: patchwork-bot+netdevbpf
Date: Sat, 21 Nov 2026 19:30
29 lines
9013 bytes
Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Tue, 17 Nov 2026 16:25:29 +0100 you wrote:
> 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.
> 
> [...]

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads