selftests/seccomp: set no_new_privs after unsharing a user namespace
Subsystems (2)
PATCH 1/1 selftests/seccomp: set no_new_privs after unsharing a user namespace
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.
--- tools/testing/selftests/seccomp/seccomp_bpf.c | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+)
tools/testing/selftests/seccomp/seccomp_bpf.c +25 -0
@@ -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
Signature · 1 line
2.53.0