* [PATCH] selftests/seccomp: set no_new_privs after unsharing a user namespace
@ 2026-11-24 19:22 TAKANO Mitsuhiro
0 siblings, 0 replies; only message in thread
From: TAKANO Mitsuhiro @ 2026-11-24 19:22 UTC (permalink / raw)
To: Kees Cook, Shuah Khan
Cc: Andy Lutomirski, Will Drewry, linux-kselftest, linux-kernel,
TAKANO Mitsuhiro
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-24 19:22 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 19:22 [PATCH] selftests/seccomp: set no_new_privs after unsharing a user namespace TAKANO Mitsuhiro
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®