mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] seccomp: document and test Seccomp_filters in /proc/PID/status
@ 2026-12-02  8:13 Gabriel Changamire
  2026-12-02  8:13 ` [PATCH 1/2] docs: proc: document " Gabriel Changamire
  2026-12-02  8:13 ` [PATCH 2/2] selftests/seccomp: test filter reporting " Gabriel Changamire
  0 siblings, 2 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

Since v5.9, /proc/PID/status has reported the number of seccomp filters
attached to a task in a Seccomp_filters line. The field is missing from
Documentation/filesystems/proc.rst, and the seccomp selftests never
read /proc/PID/status, so neither the mode nor the filter count shown
there is tested.

Patch 1 documents the field. Patch 2 adds selftests covering the filter
count as filters are attached, its inheritance across fork(), and its
propagation to sibling threads with SECCOMP_FILTER_FLAG_TSYNC.

Kees, if it is convenient for you to take both through the seccomp
tree, that keeps them together; otherwise patch 1 can go through the
documentation tree on its own.

Testing: the new tests pass on a 6.12 kernel both from a process with no
seccomp filter and from inside a container whose runtime already applies
one, which is why the counts are checked against a baseline. I also
changed each expected value to a wrong one to confirm that every test
fails as it should. proc.rst parses with no new docutils warnings.

Gabriel Changamire (2):
  docs: proc: document Seccomp_filters in /proc/PID/status
  selftests/seccomp: test filter reporting in /proc/PID/status

 Documentation/filesystems/proc.rst            |   2 +
 tools/testing/selftests/seccomp/seccomp_bpf.c | 178 ++++++++++++++++++
 2 files changed, 180 insertions(+)

-- 
2.47.3


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

* [PATCH 1/2] docs: proc: document Seccomp_filters 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 ` Gabriel Changamire
  2026-12-02  8:13 ` [PATCH 2/2] selftests/seccomp: test filter reporting " 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

Commit c818c03b661c ("seccomp: Report number of loaded filters in
/proc/$pid/status") added a Seccomp_filters line to /proc/PID/status
in v5.9, but the field was never added to the documentation.

Add it to the example output and to Table 1-2, next to the existing
Seccomp entry.

Signed-off-by: Gabriel Changamire <gaberashawn@gmail.com>
---
 Documentation/filesystems/proc.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
index fa7a468e8edf..36a59f38e1c8 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -202,6 +202,7 @@ read the file /proc/PID/status::
   CapAmb: 0000000000000000
   NoNewPrivs:     0
   Seccomp:        0
+  Seccomp_filters:        0
   Speculation_Store_Bypass:       thread vulnerable
   SpeculationIndirectBranch:      conditional enabled
   voluntary_ctxt_switches:        0
@@ -288,6 +289,7 @@ It's slow but very precise.
  CapAmb                      bitmap of ambient capabilities
  NoNewPrivs                  no_new_privs, like prctl(PR_GET_NO_NEW_PRIV, ...)
  Seccomp                     seccomp mode, like prctl(PR_GET_SECCOMP, ...)
+ Seccomp_filters             number of seccomp filters attached to the task
  Speculation_Store_Bypass    speculative store bypass mitigation status
  SpeculationIndirectBranch   indirect branch speculation mode
  Cpus_allowed                mask of CPUs on which this process may run
-- 
2.47.3


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

* [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	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02  8:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 2/2] selftests/seccomp: test filter reporting " Gabriel Changamire

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®