[RFC PATCH 8/9] selftests/seccomp: test held fork results

From: Cong Wang

Date: Mon Oct 19 2026 - 20:13:41 EST


From: Cong Wang <cwang@xxxxxxxxxxxxxx>

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@xxxxxxxxxxxxxx>
---
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