mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gabriel Changamire <gaberashawn@gmail.com>
To: Kees Cook <kees@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>,
	Will Drewry <wad@chromium.org>, Shuah Khan <shuah@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Randy Dunlap <rdunlap@infradead.org>,
	linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Gabriel Changamire <gaberashawn@gmail.com>
Subject: [PATCH 2/2] selftests/seccomp: test filter reporting in /proc/PID/status
Date: Wed,  2 Dec 2026 08:13:43 +0000	[thread overview]
Message-ID: <20261202081343.9325-3-gaberashawn@gmail.com> (raw)
In-Reply-To: <20261202081343.9325-1-gaberashawn@gmail.com>

/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


      parent reply	other threads:[~2026-12-02  8:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  8:13 [PATCH 0/2] seccomp: document and test Seccomp_filters " Gabriel Changamire
2026-12-02  8:13 ` [PATCH 1/2] docs: proc: document " Gabriel Changamire
2026-12-02  8:13 ` Gabriel Changamire [this message]

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=20261202081343.9325-3-gaberashawn@gmail.com \
    --to=gaberashawn@gmail.com \
    --cc=corbet@lwn.net \
    --cc=kees@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=rdunlap@infradead.org \
    --cc=shuah@kernel.org \
    --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®