From: Cong Wang <xiyou.wangcong@gmail.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Kees Cook <kees@kernel.org>,
linux-kernel@vger.kernel.org, Will Drewry <wad@chromium.org>,
Christian Brauner <brauner@kernel.org>
Subject: [PATCH 2/2] selftests/seccomp: test addfd with an O_PATH descriptor
Date: Thu, 24 Sep 2026 15:30:17 -0700 [thread overview]
Message-ID: <20260924223018.564548-3-xiyou.wangcong@gmail.com> (raw)
In-Reply-To: <20260924223018.564548-1-xiyou.wangcong@gmail.com>
From: Cong Wang <cwang@multikernel.io>
SECCOMP_IOCTL_NOTIF_ADDFD used to reject O_PATH source descriptors with
EBADF. Add a regression test that injects one into a notified task.
Verify that the injected descriptor retains O_PATH semantics and the
requested close-on-exec flag. The test fails on the original fget()
path and passes with fget_raw().
Assisted-by: Codex:gpt-6
Signed-off-by: Cong Wang <cwang@multikernel.io>
---
tools/testing/selftests/seccomp/seccomp_bpf.c | 59 +++++++++++++++++++
1 file changed, 59 insertions(+)
diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index 0622bc2acad4..9765869f66b8 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -4298,6 +4298,65 @@ TEST(user_notification_addfd)
close(memfd);
}
+TEST(user_notification_addfd_opath)
+{
+ struct seccomp_notif req = {};
+ struct seccomp_notif_addfd addfd = {};
+ struct seccomp_notif_resp resp = {};
+ pid_t pid;
+ int listener, pathfd, fd, status;
+
+ pathfd = open("/dev/null", O_PATH | O_CLOEXEC);
+ ASSERT_GE(pathfd, 0);
+ ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), 0);
+ listener = user_notif_syscall(__NR_getppid,
+ SECCOMP_FILTER_FLAG_NEW_LISTENER);
+ ASSERT_GE(listener, 0);
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+ if (pid == 0) {
+ int flags;
+ char byte;
+
+ close(pathfd);
+ fd = syscall(__NR_getppid);
+ if (fd < 0)
+ _exit(1);
+ flags = fcntl(fd, F_GETFL);
+ if (flags < 0 || !(flags & O_PATH))
+ _exit(2);
+ flags = fcntl(fd, F_GETFD);
+ if (flags < 0 || !(flags & FD_CLOEXEC))
+ _exit(3);
+ errno = 0;
+ if (read(fd, &byte, 1) != -1 || errno != EBADF)
+ _exit(4);
+ _exit(0);
+ }
+
+ ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
+ addfd.id = req.id;
+ addfd.flags = SECCOMP_ADDFD_FLAG_SEND;
+ addfd.srcfd = pathfd;
+ addfd.newfd_flags = O_CLOEXEC;
+ fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
+ if (fd < 0) {
+ resp.id = req.id;
+ resp.error = -errno;
+ TH_LOG("ADDFD failed with errno %d", -resp.error);
+ ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0);
+ }
+
+ ASSERT_EQ(waitpid(pid, &status, 0), pid);
+ EXPECT_GE(fd, 0);
+ EXPECT_EQ(true, WIFEXITED(status));
+ if (WIFEXITED(status))
+ EXPECT_EQ(0, WEXITSTATUS(status));
+ close(pathfd);
+ close(listener);
+}
+
TEST(user_notification_addfd_rlimit)
{
pid_t pid;
--
2.43.0
prev parent reply other threads:[~2026-09-24 22:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 22:30 [PATCH 0/2] seccomp: support O_PATH descriptors in addfd Cong Wang
2026-09-24 22:30 ` [PATCH 1/2] seccomp: allow addfd to transfer O_PATH descriptors Cong Wang
2026-09-24 22:30 ` Cong Wang [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=20260924223018.564548-3-xiyou.wangcong@gmail.com \
--to=xiyou.wangcong@gmail.com \
--cc=brauner@kernel.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=wad@chromium.org \
/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
all inboxes | Powered by JetHome®