From nobody Mon Feb 9 21:19:38 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 BF2052773D2; Wed, 22 Oct 2025 16:09:44 +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=1761149384; cv=none; b=LlB3kVqhpcIVuHPrggLivEchkXD23b+3MihBWAUv0LHOsjAYQudPXqupaV+al70DVrpleERTI+xeWQMor9iasUE0+GqDeV4nqJYPW+l35/vivZvE/hKX1YzBG+f8idrgK6B+MvW65ZOmK259JRG4JoopJ2UBGcb1FAE+BGwy03s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761149384; c=relaxed/simple; bh=4vBs0iC1GliO+F4XrLBJxRIywFUw/24vdpBSDdc51/s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZDqvT4dtN6YuUTVhCxP8yiBkgFDhWcbMqk3Yek7B2Pe6viz0bHZ8aoAdcRUDiDsE3OfxHWk7vFb7PF3o/vkpEW9X7VFyM8Rk570aYoQgTsiotdMMUnpfyzaTlALhOVN9HqrZJ99wRun3hFNyq79aTi1Y9aEdeCCfQgZy9z664Ic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=segBop61; 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="segBop61" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 487EFC116B1; Wed, 22 Oct 2025 16:09:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1761149384; bh=4vBs0iC1GliO+F4XrLBJxRIywFUw/24vdpBSDdc51/s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=segBop61K5CiFuINcw38i+jzw/Ejfd4any0YnKLytyJjAfjWwfjj5bsytutEQzXOH NZ8UK/ak2PNLzajBn3fTTcmW1lOWkae1lcRCFl6L4GcHsTO5kJMyAmOlsMtB0XNGr+ vGGF3bv2Vrt8ZUDNZmA9qOJ5rweRGu+vmt/hIZg144J4CiYp1vGUXEhM/wyYzyFJke zDVNmznDs7z7sReoyEoy0V74F2pLhd/Ncrio0CpfCTroeFW4EWtgsVJ5nbRTaIgOMu 2grX3+W2pRWCoG7JwY+mGJo+7e8yTGWn1bWu9rKFbC+5B/tZ8qUv5CfIxMRyxhkXDF 2GFFzEcc/g8qQ== From: Christian Brauner Date: Wed, 22 Oct 2025 18:06:19 +0200 Subject: [PATCH v2 41/63] selftests/namespaces: sixth listns() 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: <20251022-work-namespace-nstree-listns-v2-41-71a588572371@kernel.org> References: <20251022-work-namespace-nstree-listns-v2-0-71a588572371@kernel.org> In-Reply-To: <20251022-work-namespace-nstree-listns-v2-0-71a588572371@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=3170; i=brauner@kernel.org; h=from:subject:message-id; bh=4vBs0iC1GliO+F4XrLBJxRIywFUw/24vdpBSDdc51/s=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWT8ZHgm+EP3RaW4VNXkwo8RsRkOefM1N1h9Wn1w4p8n6 xoLD5xv7ChlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZjIkfWMDGe/iu68J+63qmn7 Wa2Mtw/+cd6ZyBzy4Al/xdy92R/eljAx/E+NvrDqfVfew6Xie0KTrMp+hc63edu27YqF7qke9V1 bZ7ADAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Test listns() with specific user namespace ID. Create a user namespace and list namespaces it owns. Signed-off-by: Christian Brauner --- tools/testing/selftests/namespaces/listns_test.c | 109 +++++++++++++++++++= ++++ 1 file changed, 109 insertions(+) diff --git a/tools/testing/selftests/namespaces/listns_test.c b/tools/testi= ng/selftests/namespaces/listns_test.c index e854794abe56..e1e90ef933cf 100644 --- a/tools/testing/selftests/namespaces/listns_test.c +++ b/tools/testing/selftests/namespaces/listns_test.c @@ -327,4 +327,113 @@ TEST(listns_only_active) } } =20 +/* + * Test listns() with specific user namespace ID. + * Create a user namespace and list namespaces it owns. + */ +TEST(listns_specific_userns) +{ + struct ns_id_req req =3D { + .size =3D sizeof(req), + .spare =3D 0, + .ns_id =3D 0, + .ns_type =3D 0, + .spare2 =3D 0, + .user_ns_id =3D 0, /* Will be filled with created userns ID */ + }; + __u64 ns_ids[100]; + int pipefd[2]; + pid_t pid; + int status; + __u64 user_ns_id =3D 0; + int bytes; + ssize_t ret; + + ASSERT_EQ(pipe(pipefd), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + int fd; + __u64 ns_id; + char buf; + + close(pipefd[0]); + + /* Create new user namespace */ + if (setup_userns() < 0) { + close(pipefd[1]); + exit(1); + } + + /* Get 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, &ns_id) < 0) { + close(fd); + close(pipefd[1]); + exit(1); + } + close(fd); + + /* Send ID to parent */ + write(pipefd[1], &ns_id, sizeof(ns_id)); + + /* Create some namespaces owned by this user namespace */ + unshare(CLONE_NEWNET); + unshare(CLONE_NEWUTS); + + /* Wait for parent signal */ + read(pipefd[1], &buf, 1); + close(pipefd[1]); + exit(0); + } + + /* Parent */ + close(pipefd[1]); + bytes =3D read(pipefd[0], &user_ns_id, sizeof(user_ns_id)); + + if (bytes !=3D sizeof(user_ns_id)) { + close(pipefd[0]); + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + SKIP(return, "Failed to get user namespace ID from child"); + } + + TH_LOG("Child created user namespace with ID %llu", (unsigned long long)u= ser_ns_id); + + /* List namespaces owned by this user namespace */ + req.user_ns_id =3D user_ns_id; + ret =3D sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0); + + if (ret < 0) { + TH_LOG("listns failed: %s (errno=3D%d)", strerror(errno), errno); + close(pipefd[0]); + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + if (errno =3D=3D ENOSYS) { + SKIP(return, "listns() not supported"); + } + ASSERT_GE(ret, 0); + } + + TH_LOG("Found %zd namespaces owned by user namespace %llu", ret, + (unsigned long long)user_ns_id); + + /* Should find at least the network and UTS namespaces we created */ + if (ret > 0) { + for (ssize_t i =3D 0; i < ret && i < 10; i++) + TH_LOG(" [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]); + } + + /* Signal child to exit */ + close(pipefd[0]); + waitpid(pid, &status, 0); +} + TEST_HARNESS_MAIN --=20 2.47.3