Skip to content

Commit

Permalink
test: check for kernel.apparmor_restrict_unprivileged_userns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
enr0n authored and bluca committed Mar 5, 2024
1 parent 7360be9 commit 70aece8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/test/test-execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 |
Expand Down

0 comments on commit 70aece8

Please sign in to comment.