From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796128682; b=DJkjCFufRDZf3x1VRpJ69qiN1kxIXizYwV1NJnPA4rUWK5AE06vkn4w2nwiUGZWF4hi/ jEo0aVB+WoearAckxLZBsKOEvAcveb//9QxIzZa5hJ/XRzycjGesj8J4JawKXyVv4sS0f XfJBP89sW6S6u7UlmKXbvtY38wegdvrjs/rXwjEDdMGNfHUWIm1OYj58L6WYioSQWHBY1 xqYYUEh1FIDXgwtf0Rt5olfdV7S3jEvJNky6+3vAEqP55HtJRYiubxVAWm5cQ4fqq0evn s/2T5oR4ZKpMK1jyvfTIKWV5tIiqBeP0x8xd5x1+z3tdnIoEzNEMUl1UDbwbdJTab/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796128682; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=HZuTxNt0AwkTtq3+eU+F56q4pzCm1zzCUvwph7buRdU=; b=Q2C8rOR0UjzfC+tT1LLymdVrNu0ZNHkcuRkaTprzlHEhlB8ccgqJujc+9sNXsRhWTp5x XH8D2d/7z46R4AlyBuTQDEiEbXSpc2MPWUPIabvpBCCGovg/yxTh6zkDIkDmsnKJvwKMI SWS/PxYKO4N1jpBEt7a446vICptOK9Q1kZ7SBKRjYlPEHeod4u18H/TeCPA6WkhUGj+JT 8Q3ARTHWUuCU4Xde8GVzuI5HAxMplmBjFifdRKxruQudcgVTYOkCrCR+5ZbhVsF9AFj+5 wBjXtIu3JCrLZ2Yp71Rs2RnAd0MH9wKD1k4dQM9pZaUUZG+ofK7qbmbUm5Qt7p3GZgQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kms+wPC5; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F408606A32F; Tue, 1 Dec 2026 12:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kms+wPC5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 922F51F00897; Tue, 1 Dec 2026 12:38:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796128682; bh=HZuTxNt0AwkTtq3+eU+F56q4pzCm1zzCUvwph7buRdU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=kms+wPC5SfaedNgeo0m8G60iqH8f6o2USCNxOxixfXu+nsftHjaYu2JxfcO+DMywV bnF4dHV2YZKnURr5X5PJkg8KCC4bYFexZIAJXflzJmtKl3n36zdZvtWsf5thi7TVj9 MnrXNw6cJYUYzIAo4lAfYiVcIYSW0TLM52EQvbCv98BPVvH+9XO1L+LjLeg+ESv5lf 7zAE4ZxkKKcy4MMpjWb2E2etG8lWtqQWraYbEwR03qaOktY50wtidYmQzUAl08hble ybk5HeMrjv/lHq58ArwzhgADLh8wi+Fr1H8xMdak+luB5iinsUxKSexN7sFDxhNpSz VJ5Z/AndHptUQ== From: Christian Brauner Date: Tue, 01 Dec 2026 13:37:28 +0100 Subject: [PATCH 4/4] selftests/filesystems: test empty mount namespaces from a chroot X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-work-mount-empty_mntns_chroot-v1-4-070c96c234bc@kernel.org> References: <20261201-work-mount-empty_mntns_chroot-v1-0-070c96c234bc@kernel.org> In-Reply-To: <20261201-work-mount-empty_mntns_chroot-v1-0-070c96c234bc@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jann Horn , Josef Bacik , Serge Hallyn , linux-kernel@vger.kernel.org, Alexander Viro , Jan Kara , "Christian Brauner (Amutable)" X-Mailer: b4 0.17-dev-04696 X-Developer-Signature: v=1; a=openpgp-sha256; l=7132; i=brauner@kernel.org; h=from:subject:message-id; bh=2jt6EyvjYsg8f/+6IB9rEuXzPQpIVg4jsq2hqjFVfAE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTz7Z/1dPoM5ZzVIhMKD9mL+6lv+FbM+of1xryzf9bzh Sfpv/5xraOUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAiNZEM/+xuvLizoG3FFLvJ c5y8JPpP9/66cbV/Y8q8zuf8KVFV32YzMvzw4JsVFT5xw5yDxqHXxY692aMuktPWqHLzrfwedsv ANRwA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 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) --- .../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 +#include #include +#include +#include #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 #include #include +#include #include #include #include @@ -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