diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c index d66cc97418..a32db7a7df 100644 --- a/src/shared/seccomp-util.c +++ b/src/shared/seccomp-util.c @@ -1429,13 +1429,14 @@ int seccomp_restrict_namespaces(unsigned long retain) { 0); else /* Otherwise, block only the invocations with the appropriate flags in the loop - * below, but also the special invocation with a zero flags argument, right here. */ + * below, but also the special invocation with a zero flags argument, right here. The + * kernel takes nstype as an int and truncates it to 32 bits, hence only compare those. */ r = sym_seccomp_rule_add_exact( seccomp, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(setns), 1, - SCMP_A1(SCMP_CMP_EQ, 0)); + SCMP_A1(SCMP_CMP_MASKED_EQ, UINT32_MAX, 0)); if (r < 0) { log_debug_errno(r, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch)); diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c index 1446e71853..e991442220 100644 --- a/src/test/test-seccomp.c +++ b/src/test/test-seccomp.c @@ -327,6 +327,12 @@ TEST(restrict_namespace) { assert_se(setns(0, 0) == -1); assert_se(errno == EPERM); + /* The kernel truncates nstype to 32 bits, make sure setting upper bits doesn't bypass the filter */ + if (sizeof(long) == 8) { + assert_se(syscall(__NR_setns, 0, (unsigned long) (UINT64_C(1) << 32)) == -1); + assert_se(errno == EPERM); + } + pid_t pid = raw_clone(CLONE_NEWNS); assert_se(pid >= 0); if (pid == 0)