mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
selftests/namespaces: fifteenth active reference count tests
Test different namespace types (net, uts, ipc) all contributing active references to the same owning user namespace. Link: https://patch.msgid.link/20251029-work-namespace-nstree-listns-v4-37-2e6f823ebdc0@kernel.org Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
@@ -1949,4 +1949,168 @@ TEST(ns_userns_child_propagation)
|
||||
ASSERT_LT(ua_fd, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test different namespace types (net, uts, ipc) all contributing
|
||||
* active references to the same owning user namespace.
|
||||
*/
|
||||
TEST(ns_mixed_types_same_owner)
|
||||
{
|
||||
struct file_handle *user_handle, *net_handle, *uts_handle;
|
||||
int ret, pipefd[2];
|
||||
pid_t pid;
|
||||
int status;
|
||||
__u64 u_id, n_id, ut_id;
|
||||
char u_buf[sizeof(*user_handle) + MAX_HANDLE_SZ];
|
||||
char n_buf[sizeof(*net_handle) + MAX_HANDLE_SZ];
|
||||
char ut_buf[sizeof(*uts_handle) + MAX_HANDLE_SZ];
|
||||
|
||||
ASSERT_EQ(pipe(pipefd), 0);
|
||||
pid = fork();
|
||||
ASSERT_GE(pid, 0);
|
||||
|
||||
if (pid == 0) {
|
||||
close(pipefd[0]);
|
||||
|
||||
if (setup_userns() < 0) {
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int u_fd = open("/proc/self/ns/user", O_RDONLY);
|
||||
if (u_fd < 0) {
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
if (ioctl(u_fd, NS_GET_ID, &u_id) < 0) {
|
||||
close(u_fd);
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
close(u_fd);
|
||||
|
||||
if (unshare(CLONE_NEWNET) < 0) {
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int n_fd = open("/proc/self/ns/net", O_RDONLY);
|
||||
if (n_fd < 0) {
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
if (ioctl(n_fd, NS_GET_ID, &n_id) < 0) {
|
||||
close(n_fd);
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
close(n_fd);
|
||||
|
||||
if (unshare(CLONE_NEWUTS) < 0) {
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int ut_fd = open("/proc/self/ns/uts", O_RDONLY);
|
||||
if (ut_fd < 0) {
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
if (ioctl(ut_fd, NS_GET_ID, &ut_id) < 0) {
|
||||
close(ut_fd);
|
||||
close(pipefd[1]);
|
||||
exit(1);
|
||||
}
|
||||
close(ut_fd);
|
||||
|
||||
/* Send all namespace IDs */
|
||||
write(pipefd[1], &u_id, sizeof(u_id));
|
||||
write(pipefd[1], &n_id, sizeof(n_id));
|
||||
write(pipefd[1], &ut_id, sizeof(ut_id));
|
||||
close(pipefd[1]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
close(pipefd[1]);
|
||||
|
||||
/* Read all three namespace IDs - fixed size, no parsing needed */
|
||||
ret = read(pipefd[0], &u_id, sizeof(u_id));
|
||||
if (ret != sizeof(u_id)) {
|
||||
close(pipefd[0]);
|
||||
waitpid(pid, NULL, 0);
|
||||
SKIP(return, "Failed to read user namespace ID");
|
||||
}
|
||||
|
||||
ret = read(pipefd[0], &n_id, sizeof(n_id));
|
||||
if (ret != sizeof(n_id)) {
|
||||
close(pipefd[0]);
|
||||
waitpid(pid, NULL, 0);
|
||||
SKIP(return, "Failed to read network namespace ID");
|
||||
}
|
||||
|
||||
ret = read(pipefd[0], &ut_id, sizeof(ut_id));
|
||||
close(pipefd[0]);
|
||||
if (ret != sizeof(ut_id)) {
|
||||
waitpid(pid, NULL, 0);
|
||||
SKIP(return, "Failed to read UTS namespace ID");
|
||||
}
|
||||
|
||||
/* Construct file handles from namespace IDs */
|
||||
user_handle = (struct file_handle *)u_buf;
|
||||
user_handle->handle_bytes = sizeof(struct nsfs_file_handle);
|
||||
user_handle->handle_type = FILEID_NSFS;
|
||||
struct nsfs_file_handle *u_fh = (struct nsfs_file_handle *)user_handle->f_handle;
|
||||
u_fh->ns_id = u_id;
|
||||
u_fh->ns_type = 0;
|
||||
u_fh->ns_inum = 0;
|
||||
|
||||
net_handle = (struct file_handle *)n_buf;
|
||||
net_handle->handle_bytes = sizeof(struct nsfs_file_handle);
|
||||
net_handle->handle_type = FILEID_NSFS;
|
||||
struct nsfs_file_handle *n_fh = (struct nsfs_file_handle *)net_handle->f_handle;
|
||||
n_fh->ns_id = n_id;
|
||||
n_fh->ns_type = 0;
|
||||
n_fh->ns_inum = 0;
|
||||
|
||||
uts_handle = (struct file_handle *)ut_buf;
|
||||
uts_handle->handle_bytes = sizeof(struct nsfs_file_handle);
|
||||
uts_handle->handle_type = FILEID_NSFS;
|
||||
struct nsfs_file_handle *ut_fh = (struct nsfs_file_handle *)uts_handle->f_handle;
|
||||
ut_fh->ns_id = ut_id;
|
||||
ut_fh->ns_type = 0;
|
||||
ut_fh->ns_inum = 0;
|
||||
|
||||
/* Open both non-user namespaces */
|
||||
int n_fd = open_by_handle_at(FD_NSFS_ROOT, net_handle, O_RDONLY);
|
||||
int ut_fd = open_by_handle_at(FD_NSFS_ROOT, uts_handle, O_RDONLY);
|
||||
if (n_fd < 0 || ut_fd < 0) {
|
||||
if (n_fd >= 0) close(n_fd);
|
||||
if (ut_fd >= 0) close(ut_fd);
|
||||
waitpid(pid, NULL, 0);
|
||||
SKIP(return, "Failed to open namespaces");
|
||||
}
|
||||
|
||||
waitpid(pid, &status, 0);
|
||||
ASSERT_TRUE(WIFEXITED(status));
|
||||
ASSERT_EQ(WEXITSTATUS(status), 0);
|
||||
|
||||
/* User namespace should be active (2 active children) */
|
||||
TH_LOG("Both net and uts active - user ns should be active");
|
||||
int u_fd = open_by_handle_at(FD_NSFS_ROOT, user_handle, O_RDONLY);
|
||||
ASSERT_GE(u_fd, 0);
|
||||
close(u_fd);
|
||||
|
||||
/* Close net - user ns should STILL be active (uts still active) */
|
||||
TH_LOG("Closing net - user ns should still be active");
|
||||
close(n_fd);
|
||||
u_fd = open_by_handle_at(FD_NSFS_ROOT, user_handle, O_RDONLY);
|
||||
ASSERT_GE(u_fd, 0);
|
||||
close(u_fd);
|
||||
|
||||
/* Close uts - user ns should become inactive */
|
||||
TH_LOG("Closing uts - user ns should become inactive");
|
||||
close(ut_fd);
|
||||
u_fd = open_by_handle_at(FD_NSFS_ROOT, user_handle, O_RDONLY);
|
||||
ASSERT_LT(u_fd, 0);
|
||||
}
|
||||
|
||||
TEST_HARNESS_MAIN
|
||||
|
||||
Reference in New Issue
Block a user