mirror of https://github.com/torvalds/linux.git
selftests/namespaces: fourth listns() permission test
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>
This commit is contained in:
parent
2635f93989
commit
6f360f2b2f
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue