mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: TAKANO Mitsuhiro <takano32@gmail.com>
To: Kees Cook <kees@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>,
	Will Drewry <wad@chromium.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	TAKANO Mitsuhiro <takano32@gmail.com>
Subject: [PATCH] selftests/seccomp: set no_new_privs after unsharing a user namespace
Date: Tue, 24 Nov 2026 19:22:08 +0000	[thread overview]
Message-ID: <20261124192208.1234120-1-takano32@gmail.com> (raw)

Installing a seccomp filter requires CAP_SYS_ADMIN in the caller's user
namespace unless no_new_privs is set.  Two user notification tests
create a user namespace and then install a filter with
SECCOMP_FILTER_FLAG_NEW_LISTENER without setting no_new_privs.
Creating a user namespace gives the caller a full capability set in the
new namespace, so the capability check succeeds on a stock kernel.  But
on a system where an LSM policy denies capabilities in unprivileged user
namespaces, the check fails and the tests report:

  user_notification_child_pid_ns: Expected listener (-1) >= 0 (0)
  user_notification_fault_recv: Expected listener (-1) >= 0 (0)

Set no_new_privs in both tests, as the other notification tests do.
Both tests exercise the notification path rather than the capability
check.  user_notification_child_pid_ns is the only test that covers
pid translation for a notification from a child pid namespace of the
listener's, and user_notification_fault_recv is the only test that
passes a NULL buffer to SECCOMP_IOCTL_NOTIF_RECV.

This gives up unprivileged coverage of the success path of the
CAP_SYS_ADMIN check in a non-initial user namespace, which these two
tests provided only incidentally.  mode_filter_without_nnp still covers
the check itself in the initial user namespace.

Verified on an arm64 guest running a 7.0 distribution kernel with
Ubuntu's apparmor_restrict_unprivileged_userns=1: seccomp_bpf goes from
pass:110 fail:17 skip:15 to pass:112 fail:15 skip:15 out of the same 142
tests.  The remaining failures occur with and without this patch: the
notification_restart variants that ask for
SECCOMP_FILTER_FLAG_RESTART_BEFORE_RECV, and
user_notification_addfd_opath, which need kernel features this 7.0
kernel does not have.  Not tested as root in the initial user
namespace, where the capability is present regardless of no_new_privs.

The code and changelog were written with Hermes Agent, an LLM
assistant; the author reviewed and tested the change.

Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index 66bb53bad93b..584fdd1a4787 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -3711,6 +3711,7 @@ TEST(user_notification_child_pid_ns)
 {
 	pid_t pid;
 	int status, listener;
+	long ret;
 	struct seccomp_notif req = {};
 	struct seccomp_notif_resp resp = {};
 
@@ -3719,6 +3720,18 @@ TEST(user_notification_child_pid_ns)
 			SKIP(return, "kernel missing CLONE_NEWUSER support");
 	};
 
+	/*
+	 * Installing a filter requires CAP_SYS_ADMIN in the caller's user
+	 * namespace unless no_new_privs is set.  This task has that
+	 * capability in the user namespace created above, but an LSM
+	 * policy can still deny the check, so ask for no_new_privs first,
+	 * as the other notification tests do.
+	 */
+	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	ASSERT_EQ(0, ret) {
+		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
+	}
+
 	listener = user_notif_syscall(__NR_getppid,
 				      SECCOMP_FILTER_FLAG_NEW_LISTENER);
 	ASSERT_GE(listener, 0);
@@ -3829,6 +3842,7 @@ TEST(user_notification_fault_recv)
 {
 	pid_t pid;
 	int status, listener;
+	long ret;
 	struct seccomp_notif req = {};
 	struct seccomp_notif_resp resp = {};
 
@@ -3837,6 +3851,17 @@ TEST(user_notification_fault_recv)
 			SKIP(return, "kernel missing CLONE_NEWUSER support");
 	}
 
+	/*
+	 * As in user_notification_child_pid_ns, ask for no_new_privs
+	 * before installing the filter: the capability check can be
+	 * denied by an LSM policy even though the task holds
+	 * CAP_SYS_ADMIN in the user namespace created above.
+	 */
+	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	ASSERT_EQ(0, ret) {
+		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
+	}
+
 	listener = user_notif_syscall(__NR_getppid,
 				      SECCOMP_FILTER_FLAG_NEW_LISTENER);
 	ASSERT_GE(listener, 0);

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
2.53.0


                 reply	other threads:[~2026-11-24 19:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261124192208.1234120-1-takano32@gmail.com \
    --to=takano32@gmail.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@amacapital.net \
    --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®