* [PATCH 2/2] selftests/seccomp: test filter reporting in /proc/PID/status
2026-12-02 8:13 [PATCH 0/2] seccomp: document and test Seccomp_filters in /proc/PID/status Gabriel Changamire
2026-12-02 8:13 ` [PATCH 1/2] docs: proc: document " Gabriel Changamire
@ 2026-12-02 8:13 ` Gabriel Changamire
1 sibling, 0 replies; 3+ messages in thread
From: Gabriel Changamire @ 2026-12-02 8:13 UTC (permalink / raw)
To: Kees Cook
Cc: Andy Lutomirski, Will Drewry, Shuah Khan, Jonathan Corbet,
Randy Dunlap, linux-kselftest, linux-doc, linux-fsdevel,
linux-kernel, Gabriel Changamire
/proc/PID/status reports the seccomp mode ("Seccomp:") and, since
commit c818c03b661c ("seccomp: Report number of loaded filters in
/proc/$pid/status"), the number of attached filters
("Seccomp_filters:"). Neither field is covered by the seccomp
selftests.
Add tests that check:
- the mode reported in status matches PR_GET_SECCOMP, and the filter
count goes up by one for each filter attached;
- a forked child inherits the parent's filter count, and filters the
child attaches afterwards do not affect the parent;
- a filter attached without SECCOMP_FILTER_FLAG_TSYNC only changes
the calling thread's count, while one attached with TSYNC brings a
sibling thread's count in line with the caller's.
The counts are checked relative to a baseline, since the test runner
may already be running under seccomp (for example, inside a container
with a default seccomp profile). The tests are skipped if the kernel
does not report Seccomp_filters.
Signed-off-by: Gabriel Changamire <gaberashawn@gmail.com>
---
tools/testing/selftests/seccomp/seccomp_bpf.c | 178 ++++++++++++++++++
1 file changed, 178 insertions(+)
diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index 66bb53bad93b..a238a2701130 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -576,6 +576,184 @@ TEST(mode_filter_get_seccomp)
}
+/*
+ * Return the value of a numeric field from /proc/self/task/<tid>/status,
+ * or -1 if the field is not present (e.g. CONFIG_SECCOMP_FILTER=n).
+ */
+static long read_status_field(pid_t tid, const char *field)
+{
+ char path[64], line[256];
+ size_t len = strlen(field);
+ long value = -1;
+ FILE *f;
+
+ snprintf(path, sizeof(path), "/proc/self/task/%d/status", tid);
+ f = fopen(path, "r");
+ if (!f)
+ return -1;
+
+ while (fgets(line, sizeof(line), f)) {
+ if (!strncmp(line, field, len) && line[len] == ':') {
+ value = strtol(line + len + 1, NULL, 10);
+ break;
+ }
+ }
+ fclose(f);
+
+ return value;
+}
+
+static pid_t sys_gettid(void)
+{
+ return syscall(__NR_gettid);
+}
+
+TEST(status_filter_count)
+{
+ struct sock_filter filter[] = {
+ BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+ };
+ struct sock_fprog prog = {
+ .len = (unsigned short)ARRAY_SIZE(filter),
+ .filter = filter,
+ };
+ long base, ret;
+ int i;
+
+ /* The test runner may itself be confined, so count from a baseline. */
+ base = read_status_field(sys_gettid(), "Seccomp_filters");
+ if (base < 0)
+ SKIP(return, "Kernel does not report Seccomp_filters");
+
+ EXPECT_EQ(prctl(PR_GET_SECCOMP, 0, 0, 0, 0),
+ read_status_field(sys_gettid(), "Seccomp"));
+
+ ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+ ASSERT_EQ(0, ret);
+
+ for (i = 1; i <= 3; i++) {
+ ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+ ASSERT_EQ(0, ret);
+
+ EXPECT_EQ(SECCOMP_MODE_FILTER,
+ read_status_field(sys_gettid(), "Seccomp"));
+ EXPECT_EQ(base + i,
+ read_status_field(sys_gettid(), "Seccomp_filters"));
+ }
+}
+
+TEST(status_filter_count_fork)
+{
+ struct sock_filter filter[] = {
+ BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+ };
+ struct sock_fprog prog = {
+ .len = (unsigned short)ARRAY_SIZE(filter),
+ .filter = filter,
+ };
+ long base, ret;
+ int status;
+ pid_t pid;
+
+ base = read_status_field(sys_gettid(), "Seccomp_filters");
+ if (base < 0)
+ SKIP(return, "Kernel does not report Seccomp_filters");
+
+ ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+ ASSERT_EQ(0, ret);
+ ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+ ASSERT_EQ(0, ret);
+ ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+ ASSERT_EQ(0, ret);
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ /* The child inherits the parent's filters... */
+ if (read_status_field(sys_gettid(), "Seccomp_filters") != base + 2)
+ _exit(1);
+ /* ...and filters it adds are its own. */
+ if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0))
+ _exit(2);
+ if (read_status_field(sys_gettid(), "Seccomp_filters") != base + 3)
+ _exit(3);
+ _exit(0);
+ }
+
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_TRUE(WIFEXITED(status));
+ EXPECT_EQ(0, WEXITSTATUS(status));
+
+ EXPECT_EQ(base + 2, read_status_field(sys_gettid(), "Seccomp_filters"));
+}
+
+struct status_sibling {
+ pid_t tid;
+ sem_t started;
+ int pipefd[2];
+};
+
+static void *status_sibling_wait(void *data)
+{
+ struct status_sibling *sib = data;
+ char c;
+
+ sib->tid = sys_gettid();
+ sem_post(&sib->started);
+ /* Block until the main thread closes the write end. */
+ while (read(sib->pipefd[0], &c, 1) > 0)
+ ;
+ return NULL;
+}
+
+TEST(status_filter_count_tsync)
+{
+ struct sock_filter filter[] = {
+ BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+ };
+ struct sock_fprog prog = {
+ .len = (unsigned short)ARRAY_SIZE(filter),
+ .filter = filter,
+ };
+ struct status_sibling sib = { };
+ pthread_t thread;
+ long base, ret;
+
+ base = read_status_field(sys_gettid(), "Seccomp_filters");
+ if (base < 0)
+ SKIP(return, "Kernel does not report Seccomp_filters");
+
+ ASSERT_EQ(0, sem_init(&sib.started, 0, 0));
+ ASSERT_EQ(0, pipe(sib.pipefd));
+ ASSERT_EQ(0, pthread_create(&thread, NULL, status_sibling_wait, &sib));
+ ASSERT_EQ(0, sem_wait(&sib.started));
+
+ ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+ ASSERT_EQ(0, ret);
+
+ /* Without TSYNC, only the calling thread's count changes. */
+ ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
+ ASSERT_NE(ENOSYS, errno) {
+ TH_LOG("Kernel does not support seccomp syscall!");
+ }
+ ASSERT_EQ(0, ret);
+ EXPECT_EQ(base + 1, read_status_field(sys_gettid(), "Seccomp_filters"));
+ EXPECT_EQ(base, read_status_field(sib.tid, "Seccomp_filters"));
+
+ /* With TSYNC, the sibling picks up the caller's whole filter chain. */
+ ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, &prog);
+ ASSERT_EQ(0, ret);
+ EXPECT_EQ(base + 2, read_status_field(sys_gettid(), "Seccomp_filters"));
+ EXPECT_EQ(base + 2, read_status_field(sib.tid, "Seccomp_filters"));
+ EXPECT_EQ(SECCOMP_MODE_FILTER, read_status_field(sib.tid, "Seccomp"));
+
+ close(sib.pipefd[1]);
+ EXPECT_EQ(0, pthread_join(thread, NULL));
+ close(sib.pipefd[0]);
+ sem_destroy(&sib.started);
+}
+
TEST(ALLOW_all)
{
struct sock_filter filter[] = {
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread