mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cong Wang <xiyou.wangcong@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Andy Lutomirski <luto@amacapital.net>,
	Kees Cook <kees@kernel.org>, Will Drewry <wad@chromium.org>,
	Christian Brauner <brauner@kernel.org>
Subject: [RFC PATCH 8/9] selftests/seccomp: test held fork results
Date: Mon, 19 Oct 2026 17:11:23 -0700	[thread overview]
Message-ID: <20261020001124.974063-9-xiyou.wangcong@gmail.com> (raw)
In-Reply-To: <20261020001124.974063-1-xiyou.wangcong@gmail.com>

From: Cong Wang <cwang@multikernel.io>

Cover SECCOMP_USER_NOTIF_FLAG_HOLD: a held child does not run until the
held notification is continued, the held notification refuses anything
but a plain CONTINUE as well as ADDFD, a failed fork is held too and
keeps the parent from returning, killing the parent turns the held
notification into a one-way record and releases the child, and closing
the listener releases the child too.

Signed-off-by: Cong Wang <cwang@multikernel.io>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 190 ++++++++++++++++++
 1 file changed, 190 insertions(+)

diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index d5dd5bc22faa..80faa3a35cf4 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -5353,6 +5353,196 @@ TEST(user_notification_fork_result_einval)
 	close(listener);
 }
 
+static int hold_pipe[2];
+
+static int fork_hold_fork(void)
+{
+	int status;
+	pid_t pid;
+
+	pid = fork();
+	if (pid < 0)
+		return 1;
+	if (pid == 0)
+		_exit(write(hold_pipe[1], "x", 1) != 1);
+	if (waitpid(pid, &status, 0) != pid)
+		return 2;
+	return !WIFEXITED(status) || WEXITSTATUS(status);
+}
+
+static int fork_hold_fail(void)
+{
+	struct __clone_args args = {};
+	bool failed = sys_clone3(&args, 1) == -1 && errno == EINVAL;
+
+	return write(hold_pipe[1], "x", 1) != 1 || !failed;
+}
+
+/* Start a sandboxed fork, continue it with HOLD and wait until it is held. */
+static pid_t fork_hold_start(struct __test_metadata *_metadata,
+			     int (*fn)(void), int *listener,
+			     struct seccomp_notif *entry)
+{
+	struct seccomp_notif_resp resp = {};
+	struct pollfd pollfd;
+	pid_t pid;
+
+	ASSERT_EQ(pipe(hold_pipe), 0);
+	pid = fork_result_child(_metadata, fn, listener);
+	close(hold_pipe[1]);
+
+	memset(entry, 0, sizeof(*entry));
+	ASSERT_EQ(ioctl(*listener, SECCOMP_IOCTL_NOTIF_RECV, entry), 0);
+	ASSERT_TRUE(is_fork_nr(entry->data.nr));
+
+	resp.id = entry->id;
+	resp.flags = SECCOMP_USER_NOTIF_FLAG_HOLD;
+	EXPECT_EQ(ioctl(*listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	resp.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE |
+		     SECCOMP_USER_NOTIF_FLAG_RESULT |
+		     SECCOMP_USER_NOTIF_FLAG_HOLD;
+	ASSERT_EQ(ioctl(*listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0) {
+		if (errno == EINVAL)
+			SKIP(return pid, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_HOLD");
+	}
+
+	pollfd.fd = *listener;
+	pollfd.events = POLLIN;
+	EXPECT_EQ(poll(&pollfd, 1, -1), 1);
+	return pid;
+}
+
+/* Has the held task run past the hold? */
+static bool hold_released(int timeout_ms)
+{
+	struct pollfd pollfd = { .fd = hold_pipe[0], .events = POLLIN };
+	char c;
+
+	return poll(&pollfd, 1, timeout_ms) == 1 &&
+	       read(hold_pipe[0], &c, 1) == 1 && c == 'x';
+}
+
+TEST(user_notification_fork_hold)
+{
+	struct seccomp_notif_addfd addfd = {};
+	struct seccomp_notif_resp resp = {};
+	struct seccomp_notif entry, rec;
+	int status, listener;
+	pid_t pid;
+
+	pid = fork_hold_start(_metadata, fork_hold_fork, &listener, &entry);
+
+	recv_result(_metadata, listener, &entry, &rec);
+	EXPECT_EQ(rec.flags, SECCOMP_NOTIF_FLAG_RESULT | SECCOMP_NOTIF_FLAG_HELD);
+	ASSERT_GT(rec.result.val, 0);
+	EXPECT_EQ(rec.result.child_pid, rec.result.val);
+	EXPECT_NE(rec.result.child_ino, 0);
+
+	EXPECT_EQ(hold_released(200), false);
+	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &rec.id), 0);
+
+	/* A held fork can only be continued. */
+	resp.id = rec.id;
+	resp.error = -EPERM;
+	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1);
+	EXPECT_EQ(errno, EINVAL);
+	resp.error = 0;
+	resp.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE |
+		     SECCOMP_USER_NOTIF_FLAG_RESULT;
+	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	addfd.id = rec.id;
+	addfd.srcfd = listener;
+	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	EXPECT_EQ(hold_released(0), false);
+
+	resp.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE;
+	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0);
+	EXPECT_EQ(hold_released(-1), true);
+
+	EXPECT_EQ(waitpid(pid, &status, 0), pid);
+	EXPECT_EQ(true, WIFEXITED(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
+	close(hold_pipe[0]);
+	close(listener);
+}
+
+/* A held result need not be a new task: the parent does not return yet. */
+TEST(user_notification_fork_hold_fail)
+{
+	struct seccomp_notif_resp resp = {};
+	struct seccomp_notif entry, rec;
+	int status, listener;
+	pid_t pid;
+
+	pid = fork_hold_start(_metadata, fork_hold_fail, &listener, &entry);
+
+	recv_result(_metadata, listener, &entry, &rec);
+	EXPECT_EQ(rec.flags, SECCOMP_NOTIF_FLAG_RESULT | SECCOMP_NOTIF_FLAG_HELD);
+	EXPECT_EQ(rec.result.val, -EINVAL);
+	EXPECT_EQ(rec.result.child_pid, 0);
+	EXPECT_EQ(rec.result.child_ino, 0);
+
+	EXPECT_EQ(hold_released(200), false);
+	resp.id = rec.id;
+	resp.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE;
+	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0);
+	EXPECT_EQ(hold_released(-1), true);
+
+	EXPECT_EQ(waitpid(pid, &status, 0), pid);
+	EXPECT_EQ(true, WIFEXITED(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
+	close(hold_pipe[0]);
+	close(listener);
+}
+
+TEST(user_notification_fork_hold_killed)
+{
+	struct seccomp_notif entry, rec;
+	int status, listener;
+	pid_t pid;
+
+	pid = fork_hold_start(_metadata, fork_hold_fork, &listener, &entry);
+
+	/* Kill the parent before the held fork was received. */
+	EXPECT_EQ(kill(pid, SIGKILL), 0);
+	EXPECT_EQ(waitpid(pid, &status, 0), pid);
+	EXPECT_EQ(true, WIFSIGNALED(status));
+	EXPECT_EQ(SIGKILL, WTERMSIG(status));
+
+	/* The child is still reported, no longer held, and runs. */
+	recv_result(_metadata, listener, &entry, &rec);
+	EXPECT_EQ(rec.flags, SECCOMP_NOTIF_FLAG_RESULT);
+	EXPECT_GT(rec.result.val, 0);
+	EXPECT_NE(rec.result.child_ino, 0);
+	EXPECT_EQ(hold_released(-1), true);
+	close(hold_pipe[0]);
+	close(listener);
+}
+
+TEST(user_notification_fork_hold_closed)
+{
+	struct seccomp_notif entry;
+	int status, listener;
+	pid_t pid;
+
+	pid = fork_hold_start(_metadata, fork_hold_fork, &listener, &entry);
+
+	/* Losing the listener releases a held fork. */
+	close(listener);
+	EXPECT_EQ(hold_released(-1), true);
+
+	EXPECT_EQ(waitpid(pid, &status, 0), pid);
+	EXPECT_EQ(true, WIFEXITED(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
+	close(hold_pipe[0]);
+}
+
 struct tsync_vs_thread_leader_args {
 	pthread_t leader;
 };
-- 
2.43.0


  parent reply	other threads:[~2026-10-20  0:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  0:11 [RFC PATCH 0/9] seccomp: report the result of continued system calls Cong Wang
2026-10-20  0:11 ` [RFC PATCH 1/9] seccomp: add result records to the notification listener Cong Wang
2026-10-20  0:11 ` [RFC PATCH 2/9] seccomp: add __NR_seccomp_* aliases for the fork family Cong Wang
2026-10-20  0:11 ` [RFC PATCH 3/9] seccomp: report the result of a continued system call Cong Wang
2026-10-20  0:11 ` [RFC PATCH 4/9] seccomp: report the result of a continued fork Cong Wang
2026-10-20  0:11 ` [RFC PATCH 5/9] seccomp: factor out the user notification queue and wait Cong Wang
2026-10-20  0:11 ` [RFC PATCH 6/9] seccomp: add SECCOMP_USER_NOTIF_FLAG_HOLD Cong Wang
2026-10-20  0:11 ` [RFC PATCH 7/9] selftests/seccomp: test fork result records Cong Wang
2026-10-20  0:11 ` Cong Wang [this message]
2026-10-20  0:11 ` [RFC PATCH 9/9] Documentation: seccomp: describe system call " Cong Wang

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=20261020001124.974063-9-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®