From nobody Sun Feb 8 14:31:29 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 633D8358D26; Wed, 22 Oct 2025 16:09:55 +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=1761149395; cv=none; b=qVChMW+yr2W4nZDxk0Bi2yEKD7xAZgBtdDvs2s08gxfnbEB5QlYHK76kGCKBZfxvx+dxzGrHxV/DSQXyp5Fen8PJlgDWv4gigWvijp+f9DckRFDlIgTZeYJFGWVj9xRol/fHqK4KYvWeOD9X+aDM8BAfe2DyqoWVgmYHxASj0J0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761149395; c=relaxed/simple; bh=V4fLSyijbME7l6bI490uSVspsEhpOL9UHRqTJM6dwA8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=q6Yrt2AchikRDyYMkJXtl1zap6TKJWYJV33E6p2J+IROmnx5pL7q0/MJIG7DJqmuvxULl31YqCK7yEghOfK70eEo4FYOccacEcNzcg2OdNzbWJPi0Wiq3HUs6uOG5e2XkVQLMN44bngXxpBJzri6Xb6xLuWwCS92JqwZdjMdOnE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r9+5Q+wQ; 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="r9+5Q+wQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B2A5C4CEF7; Wed, 22 Oct 2025 16:09:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1761149395; bh=V4fLSyijbME7l6bI490uSVspsEhpOL9UHRqTJM6dwA8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=r9+5Q+wQtrvIl/42nprWF6mcRZjwgXTGkTMjLqYZyHohNs+fbs9fTzMm1Fwpma9gE qVa6l7QLDO3EgOZoEf78DDt+tnrCH8H3iysm0Q4dhx1zco7A4cMbVjysm1RjlmheqS FOAtWWE/KBoZL2PjGtZat0OTYM/3GC/wj4eAb03Rl8v1EnQ54uu1x8QFw8XY5Rimd2 7O9xGSbPduNKQmOR4G/baupt5JJPfM8QzFeVK79l0Nfjs8j5NNDl9P4zdqpkPz/nn4 IiWD2+GG4uqmv1yZmvItLUcq6mCkv+w254tIICqWpYvygcskUAqboWryTAzv1DfZdQ 78Vlo/P4yGpYw== From: Christian Brauner Date: Wed, 22 Oct 2025 18:06:21 +0200 Subject: [PATCH v2 43/63] selftests/namespaces: ninth 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-43-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=3882; i=brauner@kernel.org; h=from:subject:message-id; bh=V4fLSyijbME7l6bI490uSVspsEhpOL9UHRqTJM6dwA8=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWT8ZHi2/fNrydyog4pnb1wL0eLa/EFrf+TZiCkZPouSF p9avSOlqqOUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAiB9Yx/Hd03X7Srb9Iwqt7 RcX5/Op8nefPj5f/15m9IeNQ68bXLrUMf7jtnb7Ly5y36XDzFV6rPHHi2q1uR6TXRpnI3831e86 3nQ8A X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Test that hierarchical active reference propagation keeps parent user namespaces visible in listns(). Signed-off-by: Christian Brauner --- tools/testing/selftests/namespaces/listns_test.c | 135 +++++++++++++++++++= ++++ 1 file changed, 135 insertions(+) diff --git a/tools/testing/selftests/namespaces/listns_test.c b/tools/testi= ng/selftests/namespaces/listns_test.c index 6afffddf9764..ddf4509d5cd6 100644 --- a/tools/testing/selftests/namespaces/listns_test.c +++ b/tools/testing/selftests/namespaces/listns_test.c @@ -467,4 +467,139 @@ TEST(listns_multiple_types) TH_LOG(" [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]); } =20 +/* + * Test that hierarchical active reference propagation keeps parent + * user namespaces visible in listns(). + */ +TEST(listns_hierarchical_visibility) +{ + struct ns_id_req req =3D { + .size =3D sizeof(req), + .spare =3D 0, + .ns_id =3D 0, + .ns_type =3D CLONE_NEWUSER, + .spare2 =3D 0, + .user_ns_id =3D 0, + }; + __u64 parent_ns_id =3D 0, child_ns_id =3D 0; + int pipefd[2]; + pid_t pid; + int status; + int bytes; + __u64 ns_ids[100]; + ssize_t ret; + bool found_parent, found_child; + + ASSERT_EQ(pipe(pipefd), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + int fd; + char buf; + + close(pipefd[0]); + + /* Create parent user namespace */ + if (setup_userns() < 0) { + close(pipefd[1]); + exit(1); + } + + fd =3D open("/proc/self/ns/user", O_RDONLY); + if (fd < 0) { + close(pipefd[1]); + exit(1); + } + + if (ioctl(fd, NS_GET_ID, &parent_ns_id) < 0) { + close(fd); + close(pipefd[1]); + exit(1); + } + close(fd); + + /* Create child user namespace */ + if (setup_userns() < 0) { + close(pipefd[1]); + exit(1); + } + + fd =3D open("/proc/self/ns/user", O_RDONLY); + if (fd < 0) { + close(pipefd[1]); + exit(1); + } + + if (ioctl(fd, NS_GET_ID, &child_ns_id) < 0) { + close(fd); + close(pipefd[1]); + exit(1); + } + close(fd); + + /* Send both IDs to parent */ + write(pipefd[1], &parent_ns_id, sizeof(parent_ns_id)); + write(pipefd[1], &child_ns_id, sizeof(child_ns_id)); + + /* Wait for parent signal */ + read(pipefd[1], &buf, 1); + close(pipefd[1]); + exit(0); + } + + /* Parent */ + close(pipefd[1]); + + /* Read both namespace IDs */ + bytes =3D read(pipefd[0], &parent_ns_id, sizeof(parent_ns_id)); + bytes +=3D read(pipefd[0], &child_ns_id, sizeof(child_ns_id)); + + if (bytes !=3D (int)(2 * sizeof(__u64))) { + close(pipefd[0]); + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + SKIP(return, "Failed to get namespace IDs from child"); + } + + TH_LOG("Parent user namespace ID: %llu", (unsigned long long)parent_ns_id= ); + TH_LOG("Child user namespace ID: %llu", (unsigned long long)child_ns_id); + + /* List all user namespaces */ + ret =3D sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0); + + if (ret < 0 && errno =3D=3D ENOSYS) { + close(pipefd[0]); + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + SKIP(return, "listns() not supported"); + } + + ASSERT_GE(ret, 0); + TH_LOG("Found %zd active user namespaces", ret); + + /* Both parent and child should be visible (active due to child process) = */ + found_parent =3D false; + found_child =3D false; + for (ssize_t i =3D 0; i < ret; i++) { + if (ns_ids[i] =3D=3D parent_ns_id) + found_parent =3D true; + if (ns_ids[i] =3D=3D child_ns_id) + found_child =3D true; + } + + TH_LOG("Parent namespace %s, child namespace %s", + found_parent ? "found" : "NOT FOUND", + found_child ? "found" : "NOT FOUND"); + + ASSERT_TRUE(found_child); + /* With hierarchical propagation, parent should also be active */ + ASSERT_TRUE(found_parent); + + /* Signal child to exit */ + close(pipefd[0]); + waitpid(pid, &status, 0); +} + TEST_HARNESS_MAIN --=20 2.47.3