]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: check for kernel.apparmor_restrict_unprivileged_userns
authorNick Rosbrook <enr0n@ubuntu.com>
Mon, 4 Mar 2024 20:43:57 +0000 (15:43 -0500)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 5 Mar 2024 11:15:49 +0000 (11:15 +0000)
Some tests in test-execute are already skipped if we do not have
unprivileged user namespaces. Extend this check to look for an apparmor
specific sysctl indicating that unprivileged userns creation is
restricted.

src/test/test-execute.c

index 148e44b13dbd5a26de1713460025d932cb094753..6a574b495f1b610e07bec9c4a401dfe888335a96 100644 (file)
@@ -28,6 +28,7 @@
 #include "signal-util.h"
 #include "static-destruct.h"
 #include "stat-util.h"
+#include "sysctl-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 #include "unit.h"
@@ -218,10 +219,30 @@ static void start_parent_slices(Unit *unit) {
         }
 }
 
+static bool apparmor_restrict_unprivileged_userns(void) {
+        _cleanup_free_ char *v = NULL;
+        int r;
+
+        /* If kernel.apparmor_restrict_unprivileged_userns=1, then we cannot
+         * use unprivileged user namespaces. */
+        r = sysctl_read("kernel/apparmor_restrict_unprivileged_userns", &v);
+        if (r < 0) {
+                if (r != -ENOENT)
+                        log_debug_errno(r, "Failed to read kernel.apparmor_restrict_unprivileged_userns sysctl, ignoring: %m");
+
+                return false;
+        }
+
+        return streq(v, "1");
+}
+
 static bool have_userns_privileges(void) {
         pid_t pid;
         int r;
 
+        if (apparmor_restrict_unprivileged_userns())
+                return false;
+
         r = safe_fork("(sd-test-check-userns)",
                       FORK_RESET_SIGNALS |
                       FORK_CLOSE_ALL_FDS |