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 4/4] selftests/filesystems: test empty mount namespaces from a chroot
Date: Tue, 01 Dec 2026 13:37:28 +0100 [thread overview]
Message-ID: <20261201-work-mount-empty_mntns_chroot-v1-4-070c96c234bc@kernel.org> (raw)
In-Reply-To: <20261201-work-mount-empty_mntns_chroot-v1-0-070c96c234bc@kernel.org>
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
prev parent reply other threads:[~2026-12-01 12:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 12:37 [PATCH 0/4] namespaces: close two ways out of " 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 ` Christian Brauner [this message]
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-4-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®