[RFC PATCH v3 12/13] selftests/rootns: Add root namespace test suite

sashiko-bot at kernel.org sashiko-bot at kernel.org
Thu Nov 19 19:37:03 UTC 2026


Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] selftests/rootns: shared mount propagation in
  move_mount_existing_mntns()
- [Medium] selftests/rootns: missing SKIP() on ENOSYS and silent skips
  in child helpers
- [Low] selftests/rootns: stack direction and lifetime hazard in
  clone_with_flags()
--

commit 101069d69b43912a16a7832bfad97b5398c581ce
Author: Jarkko Sakkinen <jarkko at kernel.org>

selftests/rootns: Add root namespace test suite

This commit adds a kselftest harness suite under
tools/testing/selftests/rootns to verify the root namespace and mount
namespace configuration uAPI and security invariants.

> diff --git a/tools/testing/selftests/rootns/smoke.c b/tools/testing/selftests/rootns/smoke.c
> new file mode 100644
> index 0000000000000..f0e3158ea4bec
> --- /dev/null
> +++ b/tools/testing/selftests/rootns/smoke.c
> @@ -0,0 +1,1934 @@

[ ... ]

> +static int clone_with_flags(int flags)
> +{
> +	const size_t stack_size = 64 * 1024;
> +	void *stack;
> +	pid_t pid;
> +	int status;
> +	int ret;
> +
> +	stack = malloc(stack_size);
> +	if (!stack)
> +		return -ENOMEM;
> +
> +	pid = clone(clone_exit, (char *)stack + stack_size,
> +		    SIGCHLD | flags, NULL);
> +	if (pid < 0)
> +		ret = -errno;
> +	else if (waitpid(pid, &status, 0) < 0)
> +		ret = -errno;
> +	else if (!WIFEXITED(status) || WEXITSTATUS(status))
> +		ret = -EIO;
> +	else
> +		ret = 0;
> +
> +	free(stack);
> +	return ret;
> +}

[Severity: Low]
Can passing (char *)stack + stack_size to clone() write past the end of the
heap buffer on architectures where the stack grows upward, such as PA-RISC?
Also, if waitpid() fails (for example on EINTR), free(stack) runs while the
child may still be executing on that stack.  Using sys_clone3() with stack = 0
and stack_size = 0 would avoid manual stack allocation and work regardless of
stack growth direction.

[ ... ]

> +FIXTURE_SETUP(rootns) {
> +	self->cfd = create_test_rootns(false, ROOTNS_KILL_ON_CLOSE |
> +				       ROOTNS_CLOSE_ON_EXEC);
> +	if (self->cfd < 0 && geteuid() != 0 &&
> +	    (errno == EPERM || errno == EACCES || errno == EINVAL ||
> +	     errno == ENOSPC || errno == EOVERFLOW))
> +		SKIP(return, "pre-idmap submount setup requires mapped userns");
> +	ASSERT_GE(self->cfd, 0);
> +}

[Severity: Medium]
Will this fail instead of skipping when CONFIG_ROOTNS is disabled (errno ==
ENOSYS) or when geteuid() == 0 hits EPERM, EACCES, or ENOSPC?  Because rootns
is added unconditionally to TARGETS in tools/testing/selftests/Makefile while
CONFIG_ROOTNS defaults to n, running make run_tests on a kernel without
CONFIG_ROOTNS will fail all tests in this binary instead of skipping them.

The same issue applies to TEST(get_nsfd), while TEST_F(rootns, mount_perms)
omits -ENOSPC when checking the return value of create_rootns_as_uid().  In
addition, the forked child helpers in mount_perms, clone_perms, fsconfig, and
unpriv_no_userns call _exit(0) when unprivileged namespace creation fails
with EPERM, EACCES, EINVAL, or ENOSPC, causing the parent to report a silent
pass instead of a skip with a reason string (and unpriv_no_userns fails with
exit status 4 on ENOSYS).

[ ... ]

> +TEST_F(rootns, move_mount_existing_mntns) {
> +	int ready_pipe[2];
> +	int hold_pipe[2];
> +	char path[64];
> +	int marker_fd;
> +	int mntns_fd;
> +	char ch;
> +	pid_t pid;
> +	int fsfd;
> +	int mfd;
> +
> +	ASSERT_EQ(pipe2(ready_pipe, O_CLOEXEC), 0);
> +	ASSERT_EQ(pipe2(hold_pipe, O_CLOEXEC), 0);
> +
> +	pid = fork();
> +	ASSERT_GE(pid, 0);
> +	if (pid == 0) {
> +		struct statfs st;
> +
> +		close(self->cfd);
> +		close(ready_pipe[0]);
> +		close(hold_pipe[1]);
> +
> +		if (unshare(CLONE_NEWNS) < 0)
> +			_exit(1);
> +		if (write(ready_pipe[1], "r", 1) != 1)
> +			_exit(2);
> +		close(ready_pipe[1]);
> +
> +		if (read(hold_pipe[0], &ch, 1) != 1)
> +			_exit(3);
> +		close(hold_pipe[0]);
> +
> +		if (statfs("/tmp", &st) < 0)
> +			_exit(4);
> +		if ((unsigned long)st.f_type != (unsigned long)TMPFS_MAGIC)
> +			_exit(5);
> +		if (access("/tmp/hot_mounted", F_OK) < 0)
> +			_exit(6);
> +		_exit(0);
> +	}
> +	close(ready_pipe[1]);
> +	close(hold_pipe[0]);
> +
> +	ASSERT_EQ(read(ready_pipe[0], &ch, 1), 1);
> +	close(ready_pipe[0]);
> +
> +	snprintf(path, sizeof(path), "/proc/%d/ns/mnt", (int)pid);
> +	mntns_fd = open(path, O_RDONLY | O_CLOEXEC);
> +	ASSERT_GE(mntns_fd, 0);
> +
> +	fsfd = sys_fsopen("tmpfs", 0);
> +	ASSERT_GE(fsfd, 0);
> +	ASSERT_EQ(sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
> +	mfd = sys_fsmount(fsfd, 0, 0);
> +	close(fsfd);
> +	ASSERT_GE(mfd, 0);
> +
> +	marker_fd = openat(mfd, "hot_mounted",
> +			   O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
> +	ASSERT_GE(marker_fd, 0);
> +	close(marker_fd);
> +
> +	/* Existing mnt_ns already has "/", so "/" must fail with EBUSY. */
> +	ASSERT_EQ(sys_move_mount(mfd, "", mntns_fd, "/",
> +				 MOVE_MOUNT_F_EMPTY_PATH |
> +				 MOVE_MOUNT_T_MNTNS), -1);
> +	ASSERT_EQ(errno, EBUSY);
> +
> +	/* Hot-mounting onto a relative path inside the live mnt_ns succeeds. */
> +	ASSERT_EQ(sys_move_mount(mfd, "", mntns_fd, "tmp",
> +				 MOVE_MOUNT_F_EMPTY_PATH |
> +				 MOVE_MOUNT_T_MNTNS), 0);

[Severity: Medium]
Does sys_move_mount() here propagate the new tmpfs mount onto /tmp in the
host mount namespace?

Because the child calls unshare(CLONE_NEWNS) without unsharing a user
namespace or remounting "/" with MS_REC | MS_PRIVATE, its mounts stay in the
same shared peer group as the host mount namespace when "/" or "/tmp" is
mounted MS_SHARED (the default on systemd hosts).  Attaching mfd onto "tmp"
in mntns_fd then propagates a peer tmpfs mount onto the host's /tmp that
remains mounted after the child and mntns_fd exit, hiding the host's /tmp
contents.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118193539.240065-1-jarkko@kernel.org?part=12



More information about the Linux-security-module-archive mailing list