[RFC PATCH v5 02/25] userns: Add userns_set_idmap() system call
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Sun Nov 22 00:10:52 UTC 2026
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] userns: missing target CAP_SYS_ADMIN and caller checks in
sys_userns_set_idmap()
- [Medium] userns: unbounded size passed to copy_struct_from_user() in
sys_userns_set_idmap()
- [Medium] userns: transient ns->flags mutation in
sys_userns_set_idmap() before validation
--
commit cd58304550496b7580b53ae3de9d67b848eccae2
Author: Jarkko Sakkinen <jarkko at kernel.org>
userns: Add userns_set_idmap() system call
This commit introduces the userns_set_idmap() system call to atomically
configure setgroups policy and UID, GID, and project ID mappings directly
on a user namespace file descriptor.
> diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
> index 6b9673dcdc670..d7f2cad388c8c 100644
> --- a/kernel/user_namespace.c
> +++ b/kernel/user_namespace.c
[ ... ]
> +static bool caller_idmap_permitted(struct user_namespace *ns, int cap_setid,
> + const struct uid_gid_map *new_map)
> +{
> + const struct cred *cred = current_cred();
> +
> + if (cap_setid == CAP_SETUID && idmap_maps_root(new_map)) {
> + if (cred->user_ns == ns) {
> + if (!ns->parent_could_setfcap)
> + return false;
> + } else {
> + if (!ns_capable(ns->parent, CAP_SETFCAP))
> + return false;
> + }
> + }
> +
> + if (new_map->nr_extents == 1 && new_map->extent[0].count == 1 &&
> + uid_eq(ns->owner, cred->euid)) {
> + u32 id = new_map->extent[0].lower_first;
> +
> + if (cap_setid == CAP_SETUID) {
> + kuid_t uid = make_kuid(ns->parent, id);
> +
> + if (uid_eq(uid, cred->euid))
> + return true;
> + } else if (cap_setid == CAP_SETGID) {
> + kgid_t gid = make_kgid(ns->parent, id);
> +
> + if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
> + gid_eq(gid, cred->egid))
> + return true;
> + }
> + }
> +
> + if (!cap_valid(cap_setid))
> + return true;
> +
> + return ns_capable(ns->parent, cap_setid);
> +}
[ ... ]
> +SYSCALL_DEFINE4(userns_set_idmap, int, fd,
> + const struct userns_idmap __user *, map,
> + size_t, size, unsigned int, flags)
> +{
[ ... ]
> + if (!map || size < USERNS_IDMAP_SIZE_VER0)
> + return -EINVAL;
> +
> + ret = copy_struct_from_user(&req, sizeof(req), map, size);
> + if (ret)
> + return ret;
[Severity: Medium]
Should sys_userns_set_idmap() reject size > PAGE_SIZE with -E2BIG before
calling copy_struct_from_user()? When size exceeds sizeof(req),
copy_struct_from_user() calls check_zeroed_user() on the trailing bytes,
which scans the user range word-by-word inside user_read_access_begin()
without rescheduling or checking for pending signals. An unprivileged
caller can pass a multi-gigabyte zero-filled mapping and stall a CPU in
the kernel.
> +
> + if (!req.flags || (req.flags & ~USERNS_IDMAP_ALL_FLAGS))
> + return -EINVAL;
> + if (!(req.flags & USERNS_IDMAP_UID) &&
> + (req.nr_uid_extents || req.uid_extents))
> + return -EINVAL;
> + if (!(req.flags & USERNS_IDMAP_GID) &&
> + (req.nr_gid_extents || req.gid_extents))
> + return -EINVAL;
> + if (!(req.flags & USERNS_IDMAP_PROJID) &&
> + (req.nr_projid_extents || req.projid_extents))
> + return -EINVAL;
> +
> + if (!ns->parent)
> + return -EPERM;
> + if (current_user_ns() != ns && current_user_ns() != ns->parent)
> + return -EPERM;
> +
> + if (req.flags & (USERNS_IDMAP_UID | USERNS_IDMAP_GID |
> + USERNS_SETGROUPS_DENY)) {
> + bool file_ok = (filp->f_cred->user_ns == ns ||
> + filp->f_cred->user_ns == ns->parent) &&
> + file_ns_capable(filp, ns, CAP_SYS_ADMIN);
> +
> + if (!ns_capable(ns, CAP_SYS_ADMIN) && !file_ok)
> + return -EPERM;
> + }
[Severity: High]
Does omitting USERNS_IDMAP_PROJID from this CAP_SYS_ADMIN check allow any
unprivileged process holding a read-only userns fd to write ns->projid_map?
For /proc/<pid>/projid_map, writing requires opening the procfs file
(mode 0644, owned by the target task) for write, so VFS permission checks
block unrelated users in ns->parent and unprivileged non-owner tasks inside
ns. By contrast, nsfs files have mode 0444 and are always opened O_RDONLY,
and caller_idmap_permitted() returns true unconditionally when cap_setid is
-1. As a result, an unprivileged task inside ns that dropped capabilities,
or an unrelated user in ns->parent that obtained an nsfs fd (for example via
NS_GET_USERNS), can call userns_set_idmap() with USERNS_IDMAP_PROJID and
lock in the write-once projid_map.
In addition, when file_ok is true on a delegated nsfs fd and the caller
lacks ns_capable(ns, CAP_SYS_ADMIN), neither USERNS_SETGROUPS_DENY nor
USERNS_IDMAP_PROJID verifies any privilege on current_cred() in ns->parent,
allowing an unprivileged recipient of a read-only userns fd to disable
setgroups(2) or configure projid_map on ns.
[ ... ]
> + if (req.flags & USERNS_SETGROUPS_DENY)
> + ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
> +
> + if (req.flags & USERNS_IDMAP_UID) {
> + ret = prepare_caller_idmap(ns, CAP_SETUID, &new_uid,
> + &ns->parent->uid_map);
> + if (ret < 0)
> + goto out_restore;
> + }
> + if (req.flags & USERNS_IDMAP_GID) {
> + ret = prepare_caller_idmap(ns, CAP_SETGID, &new_gid,
> + &ns->parent->gid_map);
> + if (ret < 0)
> + goto out_restore;
> + }
> + if (req.flags & USERNS_IDMAP_PROJID) {
> + ret = prepare_caller_idmap(ns, -1, &new_projid,
> + &ns->parent->projid_map);
> + if (ret < 0)
> + goto out_restore;
> + }
> +
> + if (req.flags & USERNS_IDMAP_UID)
> + commit_new_idmap(&ns->uid_map, &new_uid);
> + if (req.flags & USERNS_IDMAP_GID)
> + commit_new_idmap(&ns->gid_map, &new_gid);
> + if (req.flags & USERNS_IDMAP_PROJID)
> + commit_new_idmap(&ns->projid_map, &new_projid);
> +
> + mutex_unlock(&userns_state_mutex);
> + return 0;
> +
> +out_restore:
> + ns->flags = old_flags;
[Severity: Medium]
Can clearing USERNS_SETGROUPS_ALLOWED in ns->flags before
prepare_caller_idmap() expose a transient "deny" state to concurrent
readers if preparation fails?
proc_setgroups_show() reads READ_ONCE(ns->flags) without holding
userns_state_mutex:
kernel/user_namespace.c:proc_setgroups_show() {
struct user_namespace *ns = seq->private;
unsigned long userns_flags = READ_ONCE(ns->flags);
seq_printf(seq, "%s\n",
(userns_flags & USERNS_SETGROUPS_ALLOWED) ?
"allow" : "deny");
return 0;
}
While prepare_caller_idmap() needs USERNS_SETGROUPS_ALLOWED cleared when
checking an unprivileged caller's GID mapping, mutating ns->flags in place
and restoring old_flags on failure lets a concurrent reader of
/proc/<pid>/setgroups observe "deny" before it reverts to "allow". Could
caller_idmap_permitted() check the effective flags value and leave the
update to ns->flags until the commit phase after all prepare_caller_idmap()
calls succeed?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121000844.1863324-1-jarkko@kernel.org?part=2
More information about the Linux-security-module-archive
mailing list