selftests/namespaces: fourteenth active reference count tests

Test that user namespace as a child also propagates correctly.
Create user_A -> user_B, verify when user_B is active that user_A
is also active. This is different from non-user namespace children.

Link: https://patch.msgid.link/20251029-work-namespace-nstree-listns-v4-36-2e6f823ebdc0@kernel.org
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Christian Brauner
2025-10-29 13:20:49 +01:00
parent 2a94bf7bb8
commit a9d84bf7bf

View File

@@ -1817,4 +1817,136 @@ TEST(ns_parent_multiple_children_refcount)
ASSERT_LT(p_fd, 0);
}
/*
* Test that user namespace as a child also propagates correctly.
* Create user_A -> user_B, verify when user_B is active that user_A
* is also active. This is different from non-user namespace children.
*/
TEST(ns_userns_child_propagation)
{
struct file_handle *ua_handle, *ub_handle;
int ret, pipefd[2];
pid_t pid;
int status;
__u64 ua_id, ub_id;
char ua_buf[sizeof(*ua_handle) + MAX_HANDLE_SZ];
char ub_buf[sizeof(*ub_handle) + MAX_HANDLE_SZ];
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
close(pipefd[0]);
/* Create user_A */
if (setup_userns() < 0) {
close(pipefd[1]);
exit(1);
}
int ua_fd = open("/proc/self/ns/user", O_RDONLY);
if (ua_fd < 0) {
close(pipefd[1]);
exit(1);
}
if (ioctl(ua_fd, NS_GET_ID, &ua_id) < 0) {
close(ua_fd);
close(pipefd[1]);
exit(1);
}
close(ua_fd);
/* Create user_B (child of user_A) */
if (setup_userns() < 0) {
close(pipefd[1]);
exit(1);
}
int ub_fd = open("/proc/self/ns/user", O_RDONLY);
if (ub_fd < 0) {
close(pipefd[1]);
exit(1);
}
if (ioctl(ub_fd, NS_GET_ID, &ub_id) < 0) {
close(ub_fd);
close(pipefd[1]);
exit(1);
}
close(ub_fd);
/* Send both namespace IDs */
write(pipefd[1], &ua_id, sizeof(ua_id));
write(pipefd[1], &ub_id, sizeof(ub_id));
close(pipefd[1]);
exit(0);
}
close(pipefd[1]);
/* Read both namespace IDs - fixed size, no parsing needed */
ret = read(pipefd[0], &ua_id, sizeof(ua_id));
if (ret != sizeof(ua_id)) {
close(pipefd[0]);
waitpid(pid, NULL, 0);
SKIP(return, "Failed to read user_A namespace ID");
}
ret = read(pipefd[0], &ub_id, sizeof(ub_id));
close(pipefd[0]);
if (ret != sizeof(ub_id)) {
waitpid(pid, NULL, 0);
SKIP(return, "Failed to read user_B namespace ID");
}
/* Construct file handles from namespace IDs */
ua_handle = (struct file_handle *)ua_buf;
ua_handle->handle_bytes = sizeof(struct nsfs_file_handle);
ua_handle->handle_type = FILEID_NSFS;
struct nsfs_file_handle *ua_fh = (struct nsfs_file_handle *)ua_handle->f_handle;
ua_fh->ns_id = ua_id;
ua_fh->ns_type = 0;
ua_fh->ns_inum = 0;
ub_handle = (struct file_handle *)ub_buf;
ub_handle->handle_bytes = sizeof(struct nsfs_file_handle);
ub_handle->handle_type = FILEID_NSFS;
struct nsfs_file_handle *ub_fh = (struct nsfs_file_handle *)ub_handle->f_handle;
ub_fh->ns_id = ub_id;
ub_fh->ns_type = 0;
ub_fh->ns_inum = 0;
/* Open user_B before child exits */
int ub_fd = open_by_handle_at(FD_NSFS_ROOT, ub_handle, O_RDONLY);
if (ub_fd < 0) {
waitpid(pid, NULL, 0);
SKIP(return, "Failed to open user_B");
}
waitpid(pid, &status, 0);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
/* With user_B active, user_A should also be active */
TH_LOG("Testing user_A active when child user_B is active");
int ua_fd = open_by_handle_at(FD_NSFS_ROOT, ua_handle, O_RDONLY);
ASSERT_GE(ua_fd, 0);
/* Close user_B */
TH_LOG("Closing user_B");
close(ub_fd);
/* user_A should remain active (we hold direct ref) */
int ua_fd2 = open_by_handle_at(FD_NSFS_ROOT, ua_handle, O_RDONLY);
ASSERT_GE(ua_fd2, 0);
close(ua_fd2);
/* Close user_A - should become inactive */
TH_LOG("Closing user_A - should become inactive");
close(ua_fd);
ua_fd = open_by_handle_at(FD_NSFS_ROOT, ua_handle, O_RDONLY);
ASSERT_LT(ua_fd, 0);
}
TEST_HARNESS_MAIN