Add three tests: - a chrooted task without capabilities can neither create nor join a user namespace, not even one it owns - a chrooted task with CAP_SYS_CHROOT can still join one - without the chroot joining keeps working All of them run unprivileged in a user namespace. Signed-off-by: Christian Brauner (Amutable) --- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 4 +- .../selftests/namespaces/userns_chroot_test.c | 124 +++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/namespaces/.gitignore b/tools/testing/selftests/namespaces/.gitignore index 995b082357fa6..43903588ff13f 100644 --- a/tools/testing/selftests/namespaces/.gitignore +++ b/tools/testing/selftests/namespaces/.gitignore @@ -11,3 +11,4 @@ stress_test listns_pagination_bug regression_pidfd_setns_test uid_map_range_test +userns_chroot_test diff --git a/tools/testing/selftests/namespaces/Makefile b/tools/testing/selftests/namespaces/Makefile index 9b22d12708e1a..b5f74e1d8d60a 100644 --- a/tools/testing/selftests/namespaces/Makefile +++ b/tools/testing/selftests/namespaces/Makefile @@ -14,7 +14,8 @@ TEST_GEN_PROGS := nsid_test \ stress_test \ listns_pagination_bug \ regression_pidfd_setns_test \ - uid_map_range_test + uid_map_range_test \ + userns_chroot_test include ../lib.mk @@ -27,4 +28,5 @@ $(OUTPUT)/cred_change_test: ../filesystems/utils.c $(OUTPUT)/stress_test: ../filesystems/utils.c $(OUTPUT)/listns_pagination_bug: ../filesystems/utils.c $(OUTPUT)/regression_pidfd_setns_test: ../filesystems/utils.c +$(OUTPUT)/userns_chroot_test: ../filesystems/utils.c diff --git a/tools/testing/selftests/namespaces/userns_chroot_test.c b/tools/testing/selftests/namespaces/userns_chroot_test.c new file mode 100644 index 0000000000000..1275d20521510 --- /dev/null +++ b/tools/testing/selftests/namespaces/userns_chroot_test.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../kselftest_harness.h" +#include "../filesystems/utils.h" + +/* Park a child in a new user namespace owned by us, return a pidfd to it. */ +static int park_in_userns(pid_t *pid) +{ + int pipefd[2]; + char c; + + if (pipe(pipefd)) + return -1; + + *pid = fork(); + if (*pid < 0) + return -1; + + if (*pid == 0) { + close(pipefd[0]); + if (unshare(CLONE_NEWUSER)) + _exit(1); + if (write(pipefd[1], "1", 1) != 1) + _exit(1); + pause(); + _exit(0); + } + + close(pipefd[1]); + if (read(pipefd[0], &c, 1) != 1) + return -1; + close(pipefd[0]); + + return syscall(SYS_pidfd_open, *pid, 0); +} + +/* Chroot into an empty directory and remove it right away. */ +static int enter_jail(void) +{ + char jail[] = "/tmp/userns_chroot_test.XXXXXX"; + int tmpfd, ret = -1; + + tmpfd = open("/tmp", O_PATH | O_DIRECTORY); + if (tmpfd < 0) + return -1; + if (mkdtemp(jail) && !chroot(jail) && !chdir("/")) + ret = unlinkat(tmpfd, jail + strlen("/tmp/"), AT_REMOVEDIR); + close(tmpfd); + return ret; +} + +/* + * Without CAP_SYS_CHROOT a chrooted task can neither create nor join a + * user namespace. + */ +TEST(setns_userns_from_chroot) +{ + pid_t pid; + int pidfd; + + ASSERT_EQ(enter_userns(), 0); + pidfd = park_in_userns(&pid); + ASSERT_GE(pidfd, 0); + ASSERT_EQ(enter_jail(), 0); + + /* Owning the user namespace is enough to join it, no caps needed. */ + ASSERT_TRUE(caps_down()); + + ASSERT_EQ(unshare(CLONE_NEWUSER), -1); + ASSERT_EQ(errno, EPERM); + + ASSERT_EQ(setns(pidfd, CLONE_NEWUSER), -1); + ASSERT_EQ(errno, EPERM); + + ASSERT_EQ(kill(pid, SIGKILL), 0); + ASSERT_EQ(waitpid(pid, NULL, 0), pid); +} + +/* With CAP_SYS_CHROOT it could leave the chroot anyway so joining works. */ +TEST(setns_userns_from_chroot_with_cap_sys_chroot) +{ + pid_t pid; + int pidfd; + + ASSERT_EQ(enter_userns(), 0); + pidfd = park_in_userns(&pid); + ASSERT_GE(pidfd, 0); + ASSERT_EQ(enter_jail(), 0); + + ASSERT_EQ(setns(pidfd, CLONE_NEWUSER), 0); + + ASSERT_EQ(kill(pid, SIGKILL), 0); + ASSERT_EQ(waitpid(pid, NULL, 0), pid); +} + +/* Without a chroot joining keeps working. */ +TEST(setns_userns) +{ + pid_t pid; + int pidfd; + + ASSERT_EQ(enter_userns(), 0); + pidfd = park_in_userns(&pid); + ASSERT_GE(pidfd, 0); + ASSERT_TRUE(caps_down()); + + ASSERT_EQ(setns(pidfd, CLONE_NEWUSER), 0); + + ASSERT_EQ(kill(pid, SIGKILL), 0); + ASSERT_EQ(waitpid(pid, NULL, 0), pid); +} + +TEST_HARNESS_MAIN -- 2.53.0