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=1796128680; b=rt8XvTwxvstd5sNr2VdfabeE3WisM6WUl3iQm+oBGSKZaedaOafjq+X9B2bLfLL6AgbM 80SjeaTnve8pX30aTpmzq9uJiop/VRObr+jxTKbaiwiVO+e1j3dEDLhxKh1dfOuaGuqrm EneSavVeZDxecD++kUVA7pLxZ7QyyFbNFwh8CQUR/QR+HQpk0AfFNbvYq38+5MnOQUhSa Ykpr4rtAHQdLD/U8U+aUeWaEcN6IzTdp5IuPXyNM0GCXL5PMWL4GxgYeaQ7+G+1lrGQvB Zn3GY13FmwPgjiLso7sZjAmCyiwhV6cyPaHAIPxIhOZQo8KuRyawiqtPN2mg3FmQNgw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796128680; 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=laUd/QNB81ja9N+5KvhQTfBHF6qYBplkOAkDgmH2yGk=; b=2CNeqdx27LvGmD/690mG8qEMs7RaBecBKtRLJ6dR12L74MyT6lSbif6BzcyRz7WbjB0t ilGlO65klIc6GEHRZylOmj3DBDACcEPW3wMnFFQrjeP8pI8VjMUUFymOZFfAcQRSdKqYV gHV2kcGItXEfz+sSudMKRgPnDIarpE495Jo2IlUV3zO6zEr6B0/8oC9nS96Sk+ACRK0gR UNCs923x+YoW7x4MJPX2Zi1ka5Zg7ZrAAE7+ws3pVI/muD23Q42YhC/E4j/ag1o4EfrCR 1MvN/dVkciXH+IWlIrwIm4Tt5SwJJkOeycxl73j/Kzgtg4018PU8yYN3o8LHwlJGcpQ== 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=N8kfSFHP; 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 7C1BE606A31A; Tue, 1 Dec 2026 12:38:00 +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=N8kfSFHP Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32A181F00931; Tue, 1 Dec 2026 12:37:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796128680; bh=laUd/QNB81ja9N+5KvhQTfBHF6qYBplkOAkDgmH2yGk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=N8kfSFHPIkcpo3SOveibBCoHJOm+HI6XyONX8bZigIoqhNhfB4ciMJXggyBP5rLYH w6zi1vF/kZ1m6YqHS58f3gdP9eVlur0IvREoD6tUWESjJhEzckxZXO4++3ofFXJOzS HGZyAbZvnt/qxb+ero1VKIAaIfGthwooGeQi1jQv2SpRSPRScua4Sm3N/QAhT2mww0 UxoRy/1xc/dTgk+KSggnTc+oLZAKmi05i3k42tvyjC7XTgmeXKMnomUmqSZ6+bUZj4 20Rmft8ztiiIM8Bom4nNMTPOkM8mh5mQKsvdnNIR4cmZ0lp+2+1ukJH0tOPpWCJBK1 esvWwRYBDgBhA== From: Christian Brauner Date: Tue, 01 Dec 2026 13:37:27 +0100 Subject: [PATCH 3/4] selftests/namespaces: test joining a user namespace 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-3-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=4904; i=brauner@kernel.org; h=from:subject:message-id; bh=TuNjETPn6Jv5880QYBM1fcAexvXxiOAZWNrjerT8h6M=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTz7Z8Zcvzsl9U5zSI3vngU6e58vPmaS9+VEobNH18JX bTTarhr31HKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjCRzhCGv/J7tVYZnFyqOCEt u+0s22mv/rvzb71eKueTL/L4TyezQgwjw8mdqSKcgetOdVy5/H6K//8LV1J9cqzNv33UfuesxH2 wkhcA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 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