From: Christian Brauner <brauner@kernel.org>
To: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>
Cc: Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Alexander Mikhalitsyn <alexander@mihalicyn.com>,
netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org,
"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails
Date: Tue, 17 Nov 2026 16:25:31 +0100 [thread overview]
Message-ID: <20261117-work-net-peerpidfd_short_optlen-v1-2-a93f56288501@kernel.org> (raw)
In-Reply-To: <20261117-work-net-peerpidfd_short_optlen-v1-0-a93f56288501@kernel.org>
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
prev parent reply other threads:[~2026-11-17 15:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117-work-net-peerpidfd_short_optlen-v1-2-a93f56288501@kernel.org \
--to=brauner@kernel.org \
--cc=alexander@mihalicyn.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox