From nobody Sat Feb 7 06:15:00 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D815A34EEEC; Wed, 29 Oct 2025 12:25:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761740704; cv=none; b=ZVinDhXhNzc3Z6qQoqNMnK/BOjevH4IKHPOi7iOcXDugkzK+iTWQYd3YPb1w/WJPMPG8ONLzYqcsd9vFH4cSeGcToazu/WoPPkrXztU5Aqj5sJswOu7pWKhEEgf/wYdAq1JasO8DEw45lvke/UBhfcKe52tCgPGnA5Hf+x/A34s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761740704; c=relaxed/simple; bh=2cIPwZqTW4MWIZYsYx6FhB5M6WCZGsZgp86akiGTfiE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DKjjDl1/tXDlqiy7FQSkFrJdaLpRDyD7ASD6YTNH2bhjU6ntq74kzIAqHzBjD0WzCO+LnEiki2niPdlvGAgHJokQFMABXQK2JZewWq5hp6xJbhEocBadgQP5UCH9g2lpooZyPUl7+qLOfNGiGubqgTZK4Y/j2SzMhHRHbc73SUw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oM7La0BY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oM7La0BY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01E74C4CEF7; Wed, 29 Oct 2025 12:24:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1761740702; bh=2cIPwZqTW4MWIZYsYx6FhB5M6WCZGsZgp86akiGTfiE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oM7La0BYyPwUb6Lc2201zNbBK2oLQ3UWNP50eMDrpMpNG8vOKWbZmMY1D5YDI/jaP TRrJkKo825K2W8Xg4838/e0OlNgU5alhm6doqKrBa7MCpDdoWr8BTdCVG6a2PiZSg6 XsEGEOa8M/A6odUjJh+QT/QAhz/BDZky0B/mqc4NCyUQj7ynnRjFAbb2sd+JxsvnVD NCAlgG7R0hvca8xVj7EOaEKRNUJa87HuUuGL/8Xc76+b6Ppk1D+N4bNQ9Qa4vl+1RQ oDRVkDAPKDjQp6uzaL5dpygtgYKYXPVUrwgPOtN2/yXrjty5YG/GXR440NqFSM5s1f doEv5q6/wT1rw== From: Christian Brauner Date: Wed, 29 Oct 2025 13:21:05 +0100 Subject: [PATCH v4 52/72] selftests/namespaces: fifth listns() permission test Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251029-work-namespace-nstree-listns-v4-52-2e6f823ebdc0@kernel.org> References: <20251029-work-namespace-nstree-listns-v4-0-2e6f823ebdc0@kernel.org> In-Reply-To: <20251029-work-namespace-nstree-listns-v4-0-2e6f823ebdc0@kernel.org> To: linux-fsdevel@vger.kernel.org, Josef Bacik , Jeff Layton Cc: Jann Horn , Mike Yuan , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , Lennart Poettering , Daan De Meyer , Aleksa Sarai , Amir Goldstein , Tejun Heo , Johannes Weiner , Thomas Gleixner , Alexander Viro , Jan Kara , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, Eric Dumazet , Jakub Kicinski , netdev@vger.kernel.org, Arnd Bergmann , Christian Brauner X-Mailer: b4 0.15-dev-96507 X-Developer-Signature: v=1; a=openpgp-sha256; l=3453; i=brauner@kernel.org; h=from:subject:message-id; bh=2cIPwZqTW4MWIZYsYx6FhB5M6WCZGsZgp86akiGTfiE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQysfXvfvC76xh7l8YbH2GZF0cMJmXeEO1n/3u5f6bBW 6bHX37N6ShlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZiIoQ3Df2dp44LLc3kXO6r5 q7l93ef6c+k9PgGelt9ZXvVB7lv2lTD8Fdg8p0Ug3POXIsfxL6KFMRvnmvduf/fs7Pqce/ktk7q D2QE= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Test that CAP_SYS_ADMIN in parent user namespace allows seeing child user namespace's owned namespaces. Signed-off-by: Christian Brauner --- .../selftests/namespaces/listns_permissions_test.c | 119 +++++++++++++++++= ++++ 1 file changed, 119 insertions(+) diff --git a/tools/testing/selftests/namespaces/listns_permissions_test.c b= /tools/testing/selftests/namespaces/listns_permissions_test.c index 7727c5964104..b990b785dd7f 100644 --- a/tools/testing/selftests/namespaces/listns_permissions_test.c +++ b/tools/testing/selftests/namespaces/listns_permissions_test.c @@ -442,4 +442,123 @@ TEST(listns_current_user_permissions) TH_LOG("LISTNS_CURRENT_USER returned %zd namespaces", count); } =20 +/* + * Test that CAP_SYS_ADMIN in parent user namespace allows seeing + * child user namespace's owned namespaces. + */ +TEST(listns_parent_userns_cap_sys_admin) +{ + int pipefd[2]; + pid_t pid; + int status; + bool found_child_userns; + ssize_t count; + + ASSERT_EQ(pipe(pipefd), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + int fd; + __u64 parent_userns_id; + __u64 child_userns_id; + struct ns_id_req req; + __u64 ns_ids[100]; + ssize_t ret; + bool found_child_userns; + + close(pipefd[0]); + + /* Create parent user namespace - we have CAP_SYS_ADMIN in it */ + if (setup_userns() < 0) { + close(pipefd[1]); + exit(1); + } + + /* Get parent user namespace ID */ + fd =3D open("/proc/self/ns/user", O_RDONLY); + if (fd < 0) { + close(pipefd[1]); + exit(1); + } + + if (ioctl(fd, NS_GET_ID, &parent_userns_id) < 0) { + close(fd); + close(pipefd[1]); + exit(1); + } + close(fd); + + /* Create child user namespace */ + if (setup_userns() < 0) { + close(pipefd[1]); + exit(1); + } + + /* Get child user namespace ID */ + fd =3D open("/proc/self/ns/user", O_RDONLY); + if (fd < 0) { + close(pipefd[1]); + exit(1); + } + + if (ioctl(fd, NS_GET_ID, &child_userns_id) < 0) { + close(fd); + close(pipefd[1]); + exit(1); + } + close(fd); + + /* Create namespaces owned by child user namespace */ + if (unshare(CLONE_NEWNET) < 0) { + close(pipefd[1]); + exit(1); + } + + /* List namespaces owned by parent user namespace */ + req.size =3D sizeof(req); + req.spare =3D 0; + req.ns_id =3D 0; + req.ns_type =3D 0; + req.spare2 =3D 0; + req.user_ns_id =3D parent_userns_id; + + ret =3D sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0); + + /* Should see child user namespace in the list */ + found_child_userns =3D false; + if (ret > 0) { + for (ssize_t i =3D 0; i < ret; i++) { + if (ns_ids[i] =3D=3D child_userns_id) { + found_child_userns =3D true; + break; + } + } + } + + write(pipefd[1], &found_child_userns, sizeof(found_child_userns)); + write(pipefd[1], &ret, sizeof(ret)); + close(pipefd[1]); + exit(0); + } + + /* Parent */ + close(pipefd[1]); + + found_child_userns =3D false; + count =3D 0; + read(pipefd[0], &found_child_userns, sizeof(found_child_userns)); + read(pipefd[0], &count, sizeof(count)); + close(pipefd[0]); + + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(WEXITSTATUS(status), 0); + + ASSERT_TRUE(found_child_userns); + TH_LOG("Process with CAP_SYS_ADMIN in parent user namespace saw child use= r namespace (total: %zd)", + count); +} + TEST_HARNESS_MAIN --=20 2.47.3