mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] seccomp: support O_PATH descriptors in addfd
@ 2026-09-24 22:30 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 ` [PATCH 2/2] selftests/seccomp: test addfd with an O_PATH descriptor Cong Wang
  0 siblings, 2 replies; 3+ messages in thread
From: Cong Wang @ 2026-09-24 22:30 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Kees Cook, linux-kernel, Will Drewry, Christian Brauner

From: Cong Wang <cwang@multikernel.io>

The Sandlock project uses seccomp user notification to broker opens. To
maintain user-space application compatibility, it needs to return a
genuine O_PATH descriptor when an application requests one. Currently,
SECCOMP_IOCTL_NOTIF_ADDFD rejects the supervisor's O_PATH descriptor
with EBADF.

Allow addfd to transfer O_PATH descriptors and add a regression test for
their flags and read behavior. All 112 seccomp functional tests and all
7 benchmark checks passed.

Cong Wang (2):
  seccomp: allow addfd to transfer O_PATH descriptors
  selftests/seccomp: test addfd with an O_PATH descriptor

 kernel/seccomp.c                              |  2 +-
 tools/testing/selftests/seccomp/seccomp_bpf.c | 59 +++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)

-- 
2.43.0

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] seccomp: allow addfd to transfer O_PATH descriptors
  2026-09-24 22:30 [PATCH 0/2] seccomp: support O_PATH descriptors in addfd Cong Wang
@ 2026-09-24 22:30 ` Cong Wang
  2026-09-24 22:30 ` [PATCH 2/2] selftests/seccomp: test addfd with an O_PATH descriptor Cong Wang
  1 sibling, 0 replies; 3+ messages in thread
From: Cong Wang @ 2026-09-24 22:30 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Kees Cook, linux-kernel, Will Drewry, Christian Brauner

From: Cong Wang <cwang@multikernel.io>

SECCOMP_IOCTL_NOTIF_ADDFD uses fget() to acquire the supervisor's
source descriptor. fget() rejects FMODE_PATH files, so injecting an
O_PATH descriptor fails with EBADF before the receiving task can
install it.

Use fget_raw() to acquire the source file. This takes the same
reference while allowing FMODE_PATH. The existing receive_fd() and
receive_fd_replace() paths retain their security_file_receive()
checks and preserve the file's O_PATH semantics.

Fixes: 7cf97b125455 ("seccomp: Introduce addfd ioctl to seccomp user notifier")
Assisted-by: Codex:gpt-6
Signed-off-by: Cong Wang <cwang@multikernel.io>
---
 kernel/seccomp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 86cf4460d69e..4ce59be6d89f 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -1735,7 +1735,7 @@ static long seccomp_notify_addfd(struct seccomp_filter *filter,
 	if (addfd.newfd && !(addfd.flags & SECCOMP_ADDFD_FLAG_SETFD))
 		return -EINVAL;
 
-	kaddfd.file = fget(addfd.srcfd);
+	kaddfd.file = fget_raw(addfd.srcfd);
 	if (!kaddfd.file)
 		return -EBADF;
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] selftests/seccomp: test addfd with an O_PATH descriptor
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Cong Wang @ 2026-09-24 22:30 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Kees Cook, linux-kernel, Will Drewry, Christian Brauner

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-24 22:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 2/2] selftests/seccomp: test addfd with an O_PATH descriptor Cong Wang

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®