The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/4] namespaces: close two ways out of a chroot
@ 2026-12-01 12:37 Christian Brauner
  2026-12-01 12:37 ` [PATCH 1/4] user: don't let a chrooted task join a user namespace Christian Brauner
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
	Alexander Viro, Jan Kara, Christian Brauner (Amutable), stable,
	Ozan Ağdepe (NullSecurityX), Forest Huang

CLONE_EMPTY_MNTNS lets a chrooted caller with only CAP_SYS_ADMIN reset
its root to the empty root of a new mount namespace. That doesn't make
any files reachable but afterwards the caller isn't chrooted anymore and
can create user namespaces again. setns() requires CAP_SYS_CHROOT for
the same thing. So require it here as well if the caller is chrooted.

Ozan pointed out that joining a user namespace never got the chroot
restriction that creating one has. A chrooted task without any
capabilities can setns() into a user namespace owned by its uid that
some unchrooted process created and chroot() its way out. That one is a
lot older. Refuse it unless the caller has CAP_SYS_CHROOT in its current
user namespace, i.e., unless it could chroot() its way out anyway.

A task without CAP_SYS_CHROOT that setns()es into a user namespace from
inside a chroot gets EPERM now. Creating one there already fails so I
don't expect anyone to rely on that.

Both come with selftests.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (4):
      user: don't let a chrooted task join a user namespace
      mount: don't let an empty mount namespace undo a chroot
      selftests/namespaces: test joining a user namespace from a chroot
      selftests/filesystems: test empty mount namespaces from a chroot

 fs/namespace.c                                     |   9 ++
 kernel/user_namespace.c                            |   9 ++
 .../empty_mntns/clone3_empty_mntns_test.c          |  36 ++++++
 .../filesystems/empty_mntns/empty_mntns.h          |  18 +++
 .../filesystems/empty_mntns/empty_mntns_test.c     |  85 ++++++++++++++
 .../selftests/filesystems/failfs/failfs_test.c     |  38 +++++++
 tools/testing/selftests/namespaces/.gitignore      |   1 +
 tools/testing/selftests/namespaces/Makefile        |   4 +-
 .../selftests/namespaces/userns_chroot_test.c      | 124 +++++++++++++++++++++
 9 files changed, 323 insertions(+), 1 deletion(-)
---
base-commit: a63f17c4cacafcc9ff8c88651b42cd62b936d729
change-id: 20261201-work-mount-empty_mntns_chroot-7372785b88f6


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] user: don't let a chrooted task join a user namespace
  2026-12-01 12:37 [PATCH 0/4] namespaces: close two ways out of a chroot Christian Brauner
@ 2026-12-01 12:37 ` Christian Brauner
  2026-12-01 12:37 ` [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot Christian Brauner
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
	Alexander Viro, Jan Kara, Christian Brauner (Amutable), stable,
	Ozan Ağdepe (NullSecurityX)

create_user_ns() refuses chrooted callers because CAP_SYS_CHROOT in the
new user namespace would let them chroot() their way out. Joining a user
namespace hands out the same capabilities but userns_install() never
checks. So a chrooted task can setns() into any user namespace owned by
its uid, e.g., one an unchrooted process of the same user created, and
escape. It doesn't need any capabilities for that and with pidfds it
doesn't need access to /proc either.

Refuse it unless the caller has CAP_SYS_CHROOT in its current user
namespace. Such a caller can chroot() its way out anyway and this way we
don't break root in a chroot that uses nsenter to join a user namespace.

Fixes: 3151527ee007 ("userns:  Don't allow creation if the user is chrooted")
Cc: stable@vger.kernel.org
Reported-by: Ozan Ağdepe (NullSecurityX) <agdepeozan@gmail.com>
Closes: https://lore.kernel.org/20261121193446.105396-1-agdepeozan@gmail.com
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/user_namespace.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index cd748e1cbacef..5b046195015eb 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -1390,6 +1390,15 @@ static int userns_install(struct nsset *nsset, struct ns_common *ns)
 	if (current->fs->users != 1)
 		return -EINVAL;
 
+	/*
+	 * Joining grants CAP_SYS_CHROOT in @user_ns, which lets a chrooted
+	 * caller leave its chroot. Ask for CAP_SYS_CHROOT in the caller's
+	 * user namespace then, like chroot() does.
+	 */
+	if (current_chrooted() &&
+	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT))
+		return -EPERM;
+
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot
  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 ` Christian Brauner
  2026-12-01 12:37 ` [PATCH 3/4] selftests/namespaces: test joining a user namespace from " Christian Brauner
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
	Alexander Viro, Jan Kara, Christian Brauner (Amutable), stable,
	Forest Huang

CLONE_EMPTY_MNTNS resets root and pwd to the root of the new mount
namespace. For a chrooted caller that means leaving the chroot. setns()
requires CAP_SYS_CHROOT for that but here CAP_SYS_ADMIN was enough.

The new root is an empty nullfs so no files become reachable. But the
caller isn't chrooted anymore and can create user namespaces again. The
same goes for a task rooted in failfs.

Require CAP_SYS_CHROOT if the caller is chrooted.

Fixes: 9d4e752a24f7 ("namespace: allow creating empty mount namespaces")
Cc: stable@vger.kernel.org # v7.1+
Reported-by: Forest Huang <foresthuang3@gmail.com>
Closes: https://lore.kernel.org/CA+_=DuRsVXZuvxJuGBmLX-9=p-K9wa0k+4EW9F_hOJU_ATbCQQ@mail.gmail.com
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/namespace.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/fs/namespace.c b/fs/namespace.c
index 82738b818c1d0..4acd6f0773530 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4431,6 +4431,15 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
 		return ns;
 	}
 
+	/*
+	 * An empty mount namespace resets root and pwd, which takes a
+	 * chrooted caller out of its chroot. Ask for CAP_SYS_CHROOT then,
+	 * like setns() does.
+	 */
+	if ((flags & CLONE_EMPTY_MNTNS) && current_chrooted() &&
+	    !ns_capable(user_ns, CAP_SYS_CHROOT))
+		return ERR_PTR(-EPERM);
+
 	old = ns->root;
 
 	new_ns = alloc_mnt_ns(user_ns, 0);

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] selftests/namespaces: test joining a user namespace from a chroot
  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
  2026-12-01 12:37 ` [PATCH 4/4] selftests/filesystems: test empty mount namespaces " Christian Brauner
  2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
	Alexander Viro, Jan Kara, Christian Brauner (Amutable)

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] selftests/filesystems: test empty mount namespaces from a chroot
  2026-12-01 12:37 [PATCH 0/4] namespaces: close two ways out of a chroot Christian Brauner
                   ` (2 preceding siblings ...)
  2026-12-01 12:37 ` [PATCH 3/4] selftests/namespaces: test joining a user namespace from " Christian Brauner
@ 2026-12-01 12:37 ` Christian Brauner
  2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
	Alexander Viro, Jan Kara, Christian Brauner (Amutable)

Add tests for CLONE_EMPTY_MNTNS from a chroot:

- chrooted without CAP_SYS_CHROOT: EPERM for unshare() and clone3(),
  and still no user namespaces
- chrooted with CAP_SYS_CHROOT: allowed, just like setns()
- not chrooted without CAP_SYS_CHROOT: allowed as before
- rooted in failfs without CAP_SYS_CHROOT: EPERM

The failfs one needs root, the rest runs in a user namespace.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 .../empty_mntns/clone3_empty_mntns_test.c          | 36 +++++++++
 .../filesystems/empty_mntns/empty_mntns.h          | 18 +++++
 .../filesystems/empty_mntns/empty_mntns_test.c     | 85 ++++++++++++++++++++++
 .../selftests/filesystems/failfs/failfs_test.c     | 38 ++++++++++
 4 files changed, 177 insertions(+)

diff --git a/tools/testing/selftests/filesystems/empty_mntns/clone3_empty_mntns_test.c b/tools/testing/selftests/filesystems/empty_mntns/clone3_empty_mntns_test.c
index 6370086f886df..e0ca10f84d866 100644
--- a/tools/testing/selftests/filesystems/empty_mntns/clone3_empty_mntns_test.c
+++ b/tools/testing/selftests/filesystems/empty_mntns/clone3_empty_mntns_test.c
@@ -935,4 +935,40 @@ TEST(clone3_newns_full_copy)
 	ASSERT_EQ(wait_for_pid(pid), 0);
 }
 
+/* From a chroot clone3() needs CAP_SYS_CHROOT as well. */
+TEST_F(clone3_empty_mntns, chrooted_without_cap_sys_chroot)
+{
+	pid_t pid;
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (pid == 0) {
+		pid_t ret;
+
+		if (enter_userns())
+			_exit(1);
+
+		if (enter_jail())
+			_exit(2);
+
+		if (!cap_down(CAP_SYS_CHROOT))
+			_exit(3);
+
+		ret = clone3_empty_mntns(0);
+		if (ret == 0)
+			_exit(0);
+		if (ret > 0) {
+			wait_for_pid(ret);
+			_exit(4);
+		}
+		if (errno != EPERM)
+			_exit(5);
+
+		_exit(0);
+	}
+
+	ASSERT_EQ(wait_for_pid(pid), 0);
+}
+
 TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h b/tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h
index 3d9c6b14bbef9..4283754b8d7a3 100644
--- a/tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h
+++ b/tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h
@@ -3,7 +3,10 @@
 #define EMPTY_MNTNS_H
 
 #include <errno.h>
+#include <fcntl.h>
 #include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "../statmount/statmount.h"
 
@@ -22,4 +25,19 @@ static inline ssize_t count_mounts(void)
 	return listmount(LSMT_ROOT, 0, 0, list, sizeof(list) / sizeof(list[0]), 0);
 }
 
+/* chroot() into a fresh directory under /tmp and remove it right away. */
+static inline int enter_jail(void)
+{
+	char jail[] = "/tmp/empty_mntns_jail.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;
+}
+
 #endif /* EMPTY_MNTNS_H */
diff --git a/tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c b/tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c
index 43e296b97d84b..1e59e04344d39 100644
--- a/tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c
+++ b/tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c
@@ -722,4 +722,89 @@ TEST(other_ns_unaffected)
 	ASSERT_EQ(wait_for_pid(pid), 0);
 }
 
+/* A chrooted caller needs CAP_SYS_CHROOT to leave its chroot that way. */
+TEST_F(empty_mntns, chrooted_without_cap_sys_chroot)
+{
+	pid_t pid;
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (pid == 0) {
+		if (enter_userns())
+			_exit(1);
+
+		if (enter_jail())
+			_exit(2);
+
+		if (!cap_down(CAP_SYS_CHROOT))
+			_exit(3);
+
+		if (unshare(UNSHARE_EMPTY_MNTNS) == 0 || errno != EPERM)
+			_exit(4);
+
+		/* Still chrooted, so still no user namespaces. */
+		if (unshare(CLONE_NEWUSER) == 0 || errno != EPERM)
+			_exit(5);
+
+		_exit(0);
+	}
+
+	ASSERT_EQ(wait_for_pid(pid), 0);
+}
+
+/* With CAP_SYS_CHROOT a chrooted caller may, just like with setns(). */
+TEST_F(empty_mntns, chrooted_with_cap_sys_chroot)
+{
+	pid_t pid;
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (pid == 0) {
+		if (enter_userns())
+			_exit(1);
+
+		if (enter_jail())
+			_exit(2);
+
+		if (unshare(UNSHARE_EMPTY_MNTNS))
+			_exit(3);
+
+		if (count_mounts() != 1)
+			_exit(4);
+
+		_exit(0);
+	}
+
+	ASSERT_EQ(wait_for_pid(pid), 0);
+}
+
+/* Without a chroot CAP_SYS_CHROOT isn't needed. */
+TEST_F(empty_mntns, unchrooted_without_cap_sys_chroot)
+{
+	pid_t pid;
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (pid == 0) {
+		if (enter_userns())
+			_exit(1);
+
+		if (!cap_down(CAP_SYS_CHROOT))
+			_exit(2);
+
+		if (unshare(UNSHARE_EMPTY_MNTNS))
+			_exit(3);
+
+		if (count_mounts() != 1)
+			_exit(4);
+
+		_exit(0);
+	}
+
+	ASSERT_EQ(wait_for_pid(pid), 0);
+}
+
 TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/failfs/failfs_test.c b/tools/testing/selftests/filesystems/failfs/failfs_test.c
index 29a3c294127e3..9d09f0be3a6bc 100644
--- a/tools/testing/selftests/filesystems/failfs/failfs_test.c
+++ b/tools/testing/selftests/filesystems/failfs/failfs_test.c
@@ -4,6 +4,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <link.h>
+#include <linux/capability.h>
 #include <sched.h>
 #include <signal.h>
 #include <stdio.h>
@@ -36,6 +37,10 @@
 #define FD_FAILFS_ROOT -10004
 #endif
 
+#ifndef UNSHARE_EMPTY_MNTNS
+#define UNSHARE_EMPTY_MNTNS 0x00100000
+#endif
+
 #define NOBODY_UID 65534
 
 /* Child sentinel exit code: the exec was blocked as expected. */
@@ -63,6 +68,21 @@ static int drop_to_nobody(void)
 	return setresuid(NOBODY_UID, NOBODY_UID, NOBODY_UID);
 }
 
+/* Drop @cap from the effective and permitted sets. */
+static int drop_cap(int cap)
+{
+	struct __user_cap_header_struct hdr = {
+		.version = _LINUX_CAPABILITY_VERSION_3,
+	};
+	struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
+
+	if (syscall(SYS_capget, &hdr, data))
+		return -1;
+	data[CAP_TO_INDEX(cap)].effective &= ~CAP_TO_MASK(cap);
+	data[CAP_TO_INDEX(cap)].permitted &= ~CAP_TO_MASK(cap);
+	return syscall(SYS_capset, &hdr, data);
+}
+
 /* Parked CLONE_FS child; dies with its parent so it never leaks. */
 static int failfs_park(void *arg)
 {
@@ -500,6 +520,24 @@ TEST(fchroot_sentinel_setns_escape)
 	ASSERT_EQ(st.st_ino, realroot.st_ino);
 }
 
+TEST(fchroot_sentinel_empty_mntns)
+{
+	if (geteuid() != 0)
+		SKIP(return, "unsharing a mount namespace requires privileges");
+
+	/* Keep CAP_SYS_ADMIN, give up CAP_SYS_CHROOT. */
+	ASSERT_EQ(drop_cap(CAP_SYS_CHROOT), 0);
+	ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), 0);
+	ASSERT_EQ(sys_fchroot(FD_FAILFS_ROOT, 0), 0);
+
+	/* An empty mount namespace would move the root out of failfs. */
+	ASSERT_EQ(unshare(UNSHARE_EMPTY_MNTNS), -1);
+	ASSERT_EQ(errno, EPERM);
+
+	ASSERT_EQ(unshare(CLONE_NEWUSER), -1);
+	ASSERT_EQ(errno, EPERM);
+}
+
 TEST(fchroot_sentinel_exec)
 {
 	pid_t pid;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/4] namespaces: close two ways out of a chroot
  2026-12-01 12:37 [PATCH 0/4] namespaces: close two ways out of a chroot Christian Brauner
                   ` (3 preceding siblings ...)
  2026-12-01 12:37 ` [PATCH 4/4] selftests/filesystems: test empty mount namespaces " Christian Brauner
@ 2026-12-01 15:24 ` Serge E. Hallyn
  4 siblings, 0 replies; 6+ messages in thread
From: Serge E. Hallyn @ 2026-12-01 15:24 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
	Alexander Viro, Jan Kara, stable,
	Ozan Ağdepe (NullSecurityX), Forest Huang

On Tue, Dec 01, 2026 at 01:37:24PM +0100, Christian Brauner wrote:
> CLONE_EMPTY_MNTNS lets a chrooted caller with only CAP_SYS_ADMIN reset
> its root to the empty root of a new mount namespace. That doesn't make
> any files reachable but afterwards the caller isn't chrooted anymore and
> can create user namespaces again. setns() requires CAP_SYS_CHROOT for
> the same thing. So require it here as well if the caller is chrooted.
> 
> Ozan pointed out that joining a user namespace never got the chroot
> restriction that creating one has. A chrooted task without any
> capabilities can setns() into a user namespace owned by its uid that
> some unchrooted process created and chroot() its way out. That one is a
> lot older. Refuse it unless the caller has CAP_SYS_CHROOT in its current
> user namespace, i.e., unless it could chroot() its way out anyway.
> 
> A task without CAP_SYS_CHROOT that setns()es into a user namespace from
> inside a chroot gets EPERM now. Creating one there already fails so I
> don't expect anyone to rely on that.
> 
> Both come with selftests.
> 
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
> ---
> Christian Brauner (4):
>       user: don't let a chrooted task join a user namespace
>       mount: don't let an empty mount namespace undo a chroot
>       selftests/namespaces: test joining a user namespace from a chroot
>       selftests/filesystems: test empty mount namespaces from a chroot
> 

Looks good, thanks.

Reviewed-by: Serge Hallyn <serge@hallyn.com>

Only thing I noticed is it might be nice if enter_jail() didn't need to be
duplicated in both tools/testing/selftests/namespaces/userns_chroot_test.c
and tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h .

>  fs/namespace.c                                     |   9 ++
>  kernel/user_namespace.c                            |   9 ++
>  .../empty_mntns/clone3_empty_mntns_test.c          |  36 ++++++
>  .../filesystems/empty_mntns/empty_mntns.h          |  18 +++
>  .../filesystems/empty_mntns/empty_mntns_test.c     |  85 ++++++++++++++
>  .../selftests/filesystems/failfs/failfs_test.c     |  38 +++++++
>  tools/testing/selftests/namespaces/.gitignore      |   1 +
>  tools/testing/selftests/namespaces/Makefile        |   4 +-
>  .../selftests/namespaces/userns_chroot_test.c      | 124 +++++++++++++++++++++
>  9 files changed, 323 insertions(+), 1 deletion(-)
> ---
> base-commit: a63f17c4cacafcc9ff8c88651b42cd62b936d729
> change-id: 20261201-work-mount-empty_mntns_chroot-7372785b88f6

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 15:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 3/4] selftests/namespaces: test joining a user namespace from " Christian Brauner
2026-12-01 12:37 ` [PATCH 4/4] selftests/filesystems: test empty mount namespaces " Christian Brauner
2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox