Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jarkko Sakkinen" <jarkko@kernel.org>
Cc: linux-mm@kvack.org, linux-security-module@vger.kernel.org,
	"Alexander Mikhalitsyn" <alex@amutable.com>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Christian Brauner" <brauner@kernel.org>,
	"David Howells" <dhowells@redhat.com>, "Jan Kara" <jack@suse.cz>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Josef Bacik" <josef@toxicpanda.com>,
	"Kees Cook" <kees@kernel.org>, "Neal Gompa" <neal@gompa.dev>,
	"Paul Moore" <paul@paul-moore.com>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"Zygmunt Krynicki" <zygmunt.krynicki@canonical.com>
Subject: Re: [RFC PATCH v5 02/25] userns: Add userns_set_idmap() system call
Date: Sun, 22 Nov 2026 00:10:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-227537@kernel.org> (raw)
In-Reply-To: <20261121000844.1863324-3-jarkko@kernel.org>

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@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


  parent reply	other threads:[~2026-11-22  0:11 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  0:08 [RFC PATCH v5 00/25] rootns: fd-bound process closures Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 01/25] userns: Factor out idmap validation and commit helpers Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 02/25] userns: Add userns_set_idmap() system call Jarkko Sakkinen
2026-11-21 16:09   ` Jarkko Sakkinen
2026-11-21 16:49     ` Jarkko Sakkinen
2026-11-21 16:53       ` Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot [this message]
2026-11-21  0:08 ` [RFC PATCH v5 03/25] arch: Wire up " Jarkko Sakkinen
2026-11-21 15:58   ` Jarkko Sakkinen
2026-11-21 18:12     ` Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 04/25] mount: Add mnt_ns_get_root() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 05/25] mount: Enforce mount_too_revealing() for foreign mntns Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 06/25] mount: Support MOVE_MOUNT_T_MNTNS in move_mount() Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 07/25] mount: Support MOVE_MOUNT_CLONE with MOVE_MOUNT_T_MNTNS Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 08/25] userns: Factor out install_user_ns_cred() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 09/25] nsfs: Add nsproxy_open_namespace() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 10/25] nsproxy: Pass nsproxy to create_new_namespaces() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 11/25] rootns: Add core infrastructure for root namespaces Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 12/25] rootns: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-21  1:32   ` Jarkko Sakkinen
2026-11-21 18:08     ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 13/25] rootns: Add rootns() system call Jarkko Sakkinen
2026-11-21 19:10   ` Jarkko Sakkinen
2026-11-21 19:12     ` Jarkko Sakkinen
2026-11-21 19:54       ` Jarkko Sakkinen
2026-11-21 20:16         ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 14/25] rootns: Add ROOTNS_WAIT ioctl Jarkko Sakkinen
2026-11-21  0:17   ` Jarkko Sakkinen
2026-11-21  0:18     ` Jarkko Sakkinen
2026-11-21  1:09       ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 15/25] rootns: Add ROOTNS_KILL ioctl Jarkko Sakkinen
2026-11-21  1:19   ` Jarkko Sakkinen
2026-11-21 11:24     ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 16/25] fork: Support CLONE_INTO_ROOTNS in clone3() Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 17/25] fs: Support FSCONFIG_SET_ROOTNS in fsconfig() Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 18/25] proc: Implement ->set_rootns() fs_context operation Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 19/25] ipc/mqueue: " Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 20/25] security: Add rootns LSM hooks Jarkko Sakkinen
2026-11-21 11:47   ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 21/25] bpf: Register " Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 22/25] arch: Wire up rootns() system call Jarkko Sakkinen
2026-11-22  0:11   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 23/25] docs: Document root namespace uAPI Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 24/25] selftests/rootns: Add root namespace test suite Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 25/25] MAINTAINERS: Add entry for ROOT NAMESPACE Jarkko Sakkinen
2026-11-21 17:21   ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-227537@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alex@amutable.com \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dhowells@redhat.com \
    --cc=jack@suse.cz \
    --cc=jarkko@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=kees@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=neal@gompa.dev \
    --cc=paul@paul-moore.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sergeh@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zygmunt.krynicki@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox