All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Jann Horn <jannh@google.com>, Josef Bacik <josef@toxicpanda.com>,
	 Serge Hallyn <sergeh@kernel.org>,
	linux-kernel@vger.kernel.org,
	 Alexander Viro <viro@zeniv.linux.org.uk>,
	Jan Kara <jack@suse.cz>,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 3/4] selftests/namespaces: test joining a user namespace from a chroot
Date: Tue, 01 Dec 2026 13:37:27 +0100	[thread overview]
Message-ID: <20261201-work-mount-empty_mntns_chroot-v1-3-070c96c234bc@kernel.org> (raw)
In-Reply-To: <20261201-work-mount-empty_mntns_chroot-v1-0-070c96c234bc@kernel.org>

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) <brauner@kernel.org>
---
 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 <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#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


  parent reply	other threads:[~2026-12-01 12:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:37 [PATCH 0/4] namespaces: close two ways out of a chroot Christian Brauner
2026-12-01 12:37 ` [PATCH 1/4] user: don't let a chrooted task join a user namespace Christian Brauner
2026-12-01 12:37 ` [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot Christian Brauner
2026-12-01 12:37 ` Christian Brauner [this message]
2026-12-01 12:37 ` [PATCH 4/4] selftests/filesystems: test empty mount namespaces from " Christian Brauner
2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
2026-12-01 18:52   ` Christian Brauner
2026-12-02  9:10 ` Alexander Mikhalitsyn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-work-mount-empty_mntns_chroot-v1-3-070c96c234bc@kernel.org \
    --to=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sergeh@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.