Re: [RFC PATCH v7 25/26] selftests/userland: Add userland test suite
From: bot+bpf-ci
Date: Sun Nov 22 2026 - 15:28:17 EST
> selftests/userland: Add userland test suite
TL;DR:
- Selftest run as root may propagate a tmpfs over the host's /tmp and leave it
mounted
- Python 3.9-3.11 fails instead of skipping (os.unshare/setns need 3.12)
- E2BIG check may fail on 64K-page kernels
> diff --git a/tools/testing/selftests/userland/test_smoke.sh b/tools/testing/selftests/userland/test_smoke.sh
> new file mode 100755
> --- /dev/null
> +++ b/tools/testing/selftests/userland/test_smoke.sh
> @@ -0,0 +1,10 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +
> +# Kselftest framework requirement - SKIP code is 4.
> +ksft_skip=4
> +
> +command -v python3 >/dev/null 2>&1 || exit $ksft_skip
> +
> +DIR="$(dirname "$(readlink -f "$0")")"
> +exec python3 "$DIR/userland_test.py" "$@"
Does this wrapper need to check for more than the presence of python3?
userland_test.py calls os.unshare() and os.setns() at many sites, and both
functions first appeared in Python 3.12. Documentation/process/changes.rst
gives Python 3.9 as the minimum. On a 3.9 to 3.11 interpreter, with a
kernel that has CONFIG_USERLAND=y, the tests that reach those calls raise
AttributeError. Inside Userland.run() the child does:
except BaseException:
os._exit(126)
so the test only reports assertEqual(126, 0), KtapRunner.main() exits with
KSFT_FAIL, and the run is recorded as "not ok" rather than skipped. Since
the Makefile change adds userland to the default TARGETS, a default
"make kselftest" would show these failures on such hosts.
Would a check like this be more appropriate:
python3 -c 'import os, sys; sys.exit(not (hasattr(os, "unshare") and hasattr(os, "setns")))' || exit $ksft_skip
or an equivalent hasattr() check in KtapRunner.main() that exits with
KSFT_SKIP?
[ ... ]
> diff --git a/tools/testing/selftests/userland/userland_test.py b/tools/testing/selftests/userland/userland_test.py
[ ... ]
> + @classmethod
> + def enter_foreign_pidns(cls, rns: Userland) -> None:
> + with cls.closing_fd(rns.get_nsfd(CloneFlag.NEWUSER)) as ufd:
> + os.setns(ufd, CloneFlag.NEWUSER)
> + os.unshare(CloneFlag.NEWPID)
A subsystem pattern flags this as potentially concerning: a selftest should
not exit with a failure status when a prerequisite is missing, and should
skip instead.
os.setns() and os.unshare() are only available from Python 3.12, so on
Python 3.9 to 3.11 these calls raise AttributeError even though the kernel
is fully configured. enter_foreign_pidns() is reached from
test_clone_flags_and_pidns_ancestry() through check_exdev(), and
check_dead_ancestor_pidns() calls os.unshare(CloneFlag.NEWPID) directly.
Both run under run_forked(), whose "except BaseException" turns the
AttributeError into a failure payload, so the result is "not ok" and a
KSFT_FAIL exit instead of a skip.
The kernel-side prerequisites are handled through SKIP_ERRNOS and
probe_pre_idmap_mkdir(), and the interpreter requirement is the only gap.
The Sys helper already calls the other syscalls through ctypes, so would
routing setns and unshare through libc the same way, for example
Sys._LIBC.setns and Sys._LIBC.unshare, remove the 3.12 dependency? The
alternative is to skip when hasattr(os, "setns") is false.
[ ... ]
> + def check_child_src_slave_locking() -> None:
> + self.enter_userns_if_unpriv()
> + os.unshare(CloneFlag.NEWNS)
> + with self.closing_fd(self.make_plain_mount("tmpfs")) as tfd:
> + os.mkdir("sub", 0o755, dir_fd=tfd)
> + Sys.move_mount(
> + tfd,
> + "",
> + Sys.AT_FDCWD,
> + "/tmp",
> + MoveMountFlag.F_EMPTY_PATH,
> + )
Can this leave a tmpfs mounted over the host's /tmp when the suite runs as
root?
When euid is 0, enter_userns_if_unpriv() returns without creating a user
namespace:
def enter_userns_if_unpriv(cls) -> None:
if os.geteuid() == 0:
return
so os.unshare(CloneFlag.NEWNS) creates a mount namespace in init_user_ns.
copy_mnt_ns() only adds CL_SLAVE when the user namespace changes:
if (user_ns != ns->user_ns)
copy_flags |= CL_SLAVE;
Without CL_SLAVE the copied mounts remain peers of the host mounts. On a
systemd host / is shared and /tmp is usually a separate shared mount, and
nothing in the runner or setUp() makes the mounts private.
The move_mount() onto /tmp then reaches attach_recursive_mnt() with a shared
dest_mnt:
if (IS_MNT_SHARED(dest_mnt)) {
err = invent_group_ids(source_mnt, true);
...
err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
}
so a copy of the tmpfs is propagated on top of the host's /tmp. The
cleanup at the end of the helper is only:
Sys.umount2("/tmp/sub", UmountFlag.DETACH)
and when the run_forked() child exits, put_mnt_ns() calls
umount_tree(ns->root, 0) without UMOUNT_PROPAGATE, so the propagated copy
stays in the host namespace. After one root run, the real /tmp contents
(for example /tmp/.X11-unix/X0 or tmux and ssh-agent sockets) would be hidden
behind an empty tmpfs until it is unmounted by hand. If the test fails
between the T_MNTNS move_mount() and the final umount2(), a propagated
/tmp/sub is left behind as well.
check_standalone_mntns() in test_pivot_root_and_standalone_mntns_hot_mount()
already guards against this:
os.unshare(CloneFlag.NEWNS)
Sys.set_mount_propagation("/")
Should check_child_src_slave_locking() call Sys.set_mount_propagation("/")
(MS_REC|MS_PRIVATE) right after os.unshare(CloneFlag.NEWNS)? The later
set_mount_propagation("/tmp", MountFlag.REC | MountFlag.SHARED) would then
create a peer group local to the test namespace, and the slave locking
scenario would still be exercised.
[ ... ]
> + big_buf = ctypes.create_string_buffer(65536)
> + with self.assertRaises(OSError) as cm:
> + Sys.userns_set_idmap_raw(
> + userns_fd, ctypes.addressof(big_buf), size=65536
> + )
> + self.assertEqual(cm.exception.errno, errno.E2BIG)
Does this oversized-struct check assume PAGE_SIZE is below 64 KiB?
sys_userns_set_idmap() only returns -E2BIG when size is larger than
PAGE_SIZE:
if (unlikely(size > PAGE_SIZE))
return -E2BIG;
On 64K page kernels (PPC_64K_PAGES is the default for PPC_BOOK3S_64, and
arm64 has ARM64_64K_PAGES), size == PAGE_SIZE, so that branch is skipped.
copy_struct_from_user() then finds the trailing bytes zeroed, req is all
zeros, and the syscall returns -EINVAL from the check on
nr_uid_extents, nr_gid_extents and nr_projid_extents. The
assertEqual(..., errno.E2BIG) would then fail on those systems, which
Sys._build_syscall_numbers() explicitly supports.
Would a size derived from the runtime page size, for example
mmap.PAGESIZE + 1 with a buffer of that size, hit the E2BIG branch on every
page size?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45063222848