]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/namespaces: fourth listns() permission test
authorChristian Brauner <brauner@kernel.org>
Wed, 29 Oct 2025 12:21:04 +0000 (13:21 +0100)
committerChristian Brauner <brauner@kernel.org>
Mon, 3 Nov 2025 16:41:22 +0000 (17:41 +0100)
Test permission checking with LISTNS_CURRENT_USER.
Verify that listing with LISTNS_CURRENT_USER respects permissions.

Link: https://patch.msgid.link/20251029-work-namespace-nstree-listns-v4-51-2e6f823ebdc0@kernel.org
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/namespaces/listns_permissions_test.c

index aed7288c7eca65b1024e97b61965f051f35139e3..7727c59641047e3e341200c46133c03e602f0d24 100644 (file)
@@ -366,4 +366,80 @@ TEST(listns_cannot_see_sibling_userns_namespaces)
        TH_LOG("User namespace B correctly could not see sibling namespace A's network namespace");
 }
 
+/*
+ * Test permission checking with LISTNS_CURRENT_USER.
+ * Verify that listing with LISTNS_CURRENT_USER respects permissions.
+ */
+TEST(listns_current_user_permissions)
+{
+       int pipefd[2];
+       pid_t pid;
+       int status;
+       bool success;
+       ssize_t count;
+
+       ASSERT_EQ(pipe(pipefd), 0);
+
+       pid = fork();
+       ASSERT_GE(pid, 0);
+
+       if (pid == 0) {
+               struct ns_id_req req = {
+                       .size = sizeof(req),
+                       .spare = 0,
+                       .ns_id = 0,
+                       .ns_type = 0,
+                       .spare2 = 0,
+                       .user_ns_id = LISTNS_CURRENT_USER,
+               };
+               __u64 ns_ids[100];
+               ssize_t ret;
+               bool success;
+
+               close(pipefd[0]);
+
+               /* Create user namespace */
+               if (setup_userns() < 0) {
+                       close(pipefd[1]);
+                       exit(1);
+               }
+
+               /* Create some namespaces owned by this user namespace */
+               if (unshare(CLONE_NEWNET) < 0) {
+                       close(pipefd[1]);
+                       exit(1);
+               }
+
+               if (unshare(CLONE_NEWUTS) < 0) {
+                       close(pipefd[1]);
+                       exit(1);
+               }
+
+               /* List with LISTNS_CURRENT_USER - should see our owned namespaces */
+               ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
+
+               success = (ret >= 3);  /* At least user, net, uts */
+               write(pipefd[1], &success, sizeof(success));
+               write(pipefd[1], &ret, sizeof(ret));
+               close(pipefd[1]);
+               exit(0);
+       }
+
+       /* Parent */
+       close(pipefd[1]);
+
+       success = false;
+       count = 0;
+       read(pipefd[0], &success, sizeof(success));
+       read(pipefd[0], &count, sizeof(count));
+       close(pipefd[0]);
+
+       waitpid(pid, &status, 0);
+       ASSERT_TRUE(WIFEXITED(status));
+       ASSERT_EQ(WEXITSTATUS(status), 0);
+
+       ASSERT_TRUE(success);
+       TH_LOG("LISTNS_CURRENT_USER returned %zd namespaces", count);
+}
+
 TEST_HARNESS_MAIN