tools/testing/selftests/namespaces/listns_permissions_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/namespaces/listns_permissions_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/namespaces/listns_permissions_test.c- Extension
.c- Size
- 16088 bytes
- Lines
- 760
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
errno.hfcntl.hlimits.hsched.hstdio.hstdlib.hstring.hlinux/nsfs.hsys/capability.hsys/ioctl.hsys/prctl.hsys/stat.hsys/syscall.hsys/types.hsys/wait.hunistd.h../kselftest_harness.h../filesystems/utils.hwrappers.h
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
if (setup_userns() < 0) {
close(pipefd[1]);
exit(1);
}
/* Create a network namespace */
if (unshare(CLONE_NEWNET) < 0) {
close(pipefd[1]);
exit(1);
}
/* Get our network namespace ID */
fd = open("/proc/self/ns/net", O_RDONLY);
if (fd < 0) {
close(pipefd[1]);
exit(1);
}
if (ioctl(fd, NS_GET_ID, &our_netns_id) < 0) {
close(fd);
close(pipefd[1]);
exit(1);
}
close(fd);
/* Now we're unprivileged - list all network namespaces */
ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
if (ret < 0) {
close(pipefd[1]);
exit(1);
}
/* We should only see our own network namespace */
found_ours = false;
unexpected_count = 0;
for (ssize_t i = 0; i < ret; i++) {
if (ns_ids[i] == our_netns_id) {
found_ours = true;
} else {
/* This is either init_net (which we can see) or unexpected */
unexpected_count++;
}
}
/* Send results to parent */
write(pipefd[1], &found_ours, sizeof(found_ours));
write(pipefd[1], &unexpected_count, sizeof(unexpected_count));
close(pipefd[1]);
exit(0);
}
/* Parent */
close(pipefd[1]);
found_ours = false;
unexpected_count = 0;
read(pipefd[0], &found_ours, sizeof(found_ours));
read(pipefd[0], &unexpected_count, sizeof(unexpected_count));
close(pipefd[0]);
waitpid(pid, &status, 0);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
/* Child should have seen its own namespace */
ASSERT_TRUE(found_ours);
TH_LOG("Unprivileged child saw its own namespace, plus %d others (likely init_net)",
unexpected_count);
}
/*
* Test that users with CAP_SYS_ADMIN in a user namespace can see
* all namespaces owned by that user namespace.
*/
TEST(listns_cap_sys_admin_in_userns)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = 0, /* All types */
.spare2 = 0,
.user_ns_id = 0, /* Will be set to our created user namespace */
};
__u64 ns_ids[100];
int pipefd[2];
pid_t pid;
int status;
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `limits.h`, `sched.h`, `stdio.h`, `stdlib.h`, `string.h`, `linux/nsfs.h`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.