From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796478473; b=Ymc9fywWcsOhWD0Fivv3o9yb68F4Yk9iE3g3p2ruMYradw7DDBL4JxDrw5y0osEJJ2fk tpUoB092uIDHqnjIwDRAt2LZVtqunXAMiaRgHk1MEI8H4wQdXKgaqZmXSMFo1qHdRaq2L JrYZhnWWmrNs32BGsL3tfof7fFLkWsdqV1f6yyrP0NmlOzjzA2oVwqiwsQN7uY1tf6fRV yNMRsU5sWntxvaxuCyzD4fy9JP4xSlvJ9O+uzEgaScdN29kBaCFbu5EoRczMOa3lrXyVp GtMZWLPlFin3MN3fwHf0wuh/EuIZhVbuj+RPOinTUZOWwgwLXBx8G9Pn0Jw04eBCQFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796478473; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=4A7sKf0/Dz1VYTrJ84aF3VifVp6nUPV7DEGrZqdIjYM=; b=y9CazYT3E+apu1dAcongfX0OSnwK4LFaJ081nobu8RIJigL7bp0DqJZ9kNNgsYlcfrVX noVyp5J1aNyt13jVRWO78crN7ClIYK6GpZlY5cwiP8xi1uEafjl/7PdWiy9fhlQm3JL1K xbvc+5WtrLUadvK8GBf3gSy+3M4a0Ef+f2w6+2PqvaXDhFUUfUwumbKQ/vkmgLcihFrb5 iv35rzHVrS0gagK95Nn+Zlm2QyjR5Tt75KhA0lRe9Ow2nKp13V9sSuvu6sH+9g+JkyHR9 nYfvZBkeFIF6g3HNO66TVt8cVcVK2WtfjV9eCkM8n4f+OgDesd8h7wVTYkqFeF6my4g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X3hq1Ob2; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id E71E3606A322; Sat, 5 Dec 2026 13:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X3hq1Ob2 Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id BEA9E1F0092B; Sat, 5 Dec 2026 13:47:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796478472; bh=4A7sKf0/Dz1VYTrJ84aF3VifVp6nUPV7DEGrZqdIjYM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=X3hq1Ob29GlBFmCq9LKSIgOZhWpLsnvpgJA8IU1nooMe9ml4/C/hXPeLRwokbhy8C 1gtfIGAvothBEFnnKiZYfjKoM3k+tq8e1xdB37U3K7gwRQnZsSRVYWJ+bTjqFNxIph Q59P7ckHqcwZXODJWGcOxTUnMURMbWbBKSO6LV2AlD1fDnneKklBwurffDqgA94R14 qNpFQT4blBmf9Oed9V7h27Bxoo9MxPp6/IKy1ySsqKqWbJr5+MLH/w7OcjTF7WHtCo 8b2XV6E3YCyCkphXwRTp2EhGy1PFVMJE69ugCT2nb0BBomH4GWXd5Y/nYQQuk4okLq VrGg7VOHjyQTA== From: Jarkko Sakkinen To: linux-fsdevel@vger.kernel.org Cc: bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, aleksa@amutable.com, alex@amutable.com, viro@zeniv.linux.org.uk, ast@kernel.org, akpm@linux-foundation.org, andrii@kernel.org, luto@amacapital.net, arnd@arndb.de, baolin.wang@linux.alibaba.com, bsegall@google.com, brauner@kernel.org, daniel@iogearbox.net, david@kernel.org, dhowells@redhat.com, dwmw2@infradead.org, dave@stgolabs.net, dietmar.eggemann@arm.com, eddyz87@gmail.com, emil@etsalapatis.com, ebiggers@kernel.org, georgia.garcia@canonical.com, giuseppe@scrivano.org, gnoack@google.com, hughd@google.com, ihor.solodrai@linux.dev, mingo@redhat.com, jmorris@namei.org, jack@suse.cz, jgg@nvidia.com, jlayton@kernel.org, jolsa@kernel.org, john.johansen@canonical.com, corbet@lwn.net, josef@toxicpanda.com, juri.lelli@redhat.com, kees@kernel.org, kprateek.nayak@amd.com, kpsingh@kernel.org, memxor@gmail.com, lennart@poettering.net, liam@infradead.org, manfred@colorfullife.com, martin.lau@linux.dev, matt@bobrowski.net, mgorman@suse.de, mhocko@suse.com, mic@digikod.net, rppt@kernel.org, neal@gompa.dev, oleg@redhat.com, paul@paul-moore.com, ptikhomirov@virtuozzo.com, peterz@infradead.org, rdunlap@infradead.org, serge@hallyn.com, sergeh@kernel.org, skhan@linuxfoundation.org, song@kernel.org, stephen.smalley.work@gmail.com, rostedt@goodmis.org, vschneid@redhat.com, vincent.guittot@linaro.org, yonghong.song@linux.dev, zygmunt.krynicki@canonical.com Subject: Re: [RFC PATCH v10 02/28] userns: Add NS_SET_IDMAP ioctl Date: Sat, 5 Dec 2026 15:47:43 +0200 Message-ID: <20261205134743.1870007-1-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205032633.1702772-3-jarkko@kernel.org> References: <20261205032633.1702772-1-jarkko@kernel.org> <20261205032633.1702772-3-jarkko@kernel.org> X-Mailing-List: containers@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable diff --git a/fs/nsfs.c b/fs/nsfs.c index c3c511133d80..f246930c9fa3 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -366,7 +366,7 @@ static long ns_ioctl(struct file *filp, unsigned int io= ctl, if (ns->ns_type !=3D CLONE_NEWUSER) return -EINVAL; =20 - return userns_set_idmap(filp, to_user_ns(ns), + return userns_set_idmap(to_user_ns(ns), (const struct userns_idmap __user *)arg, _IOC_SIZE(ioctl)); default: diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index 7181c6011b44..079c66cbfa1c 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -35,7 +35,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */ }; }; =20 -#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED +#define USERNS_STATE_SETGROUPS_ALLOWED 1UL +#define USERNS_INIT_FLAGS USERNS_STATE_SETGROUPS_ALLOWED =20 struct ucounts; struct userns_idmap; @@ -207,7 +208,7 @@ extern bool in_userns(const struct user_namespace *ance= stor, const struct user_namespace *child); extern bool current_in_userns(const struct user_namespace *target_ns); struct ns_common *ns_get_owner(struct ns_common *ns); -long userns_set_idmap(struct file *file, struct user_namespace *ns, +long userns_set_idmap(struct user_namespace *ns, const struct userns_idmap __user *map, size_t size); =20 #if IS_ENABLED(CONFIG_KUNIT) @@ -267,8 +268,7 @@ static inline struct ns_common *ns_get_owner(struct ns_= common *ns) return ERR_PTR(-EPERM); } =20 -static inline long userns_set_idmap(struct file *file, - struct user_namespace *ns, +static inline long userns_set_idmap(struct user_namespace *ns, const struct userns_idmap __user *map, size_t size) { diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 0a6d77b57798..1ab18f621980 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -150,7 +150,7 @@ int create_user_ns(struct cred *new) set_userns_rlimit_max(ns, UCOUNT_RLIMIT_MEMLOCK, rlimit(RLIMIT_MEMLOCK)); ns->ucounts =3D ucounts; =20 - /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */ + /* Inherit the setgroups policy from our parent. */ mutex_lock(&userns_state_mutex); ns->flags =3D parent_ns->flags; mutex_unlock(&userns_state_mutex); @@ -1034,7 +1034,7 @@ static bool unpriv_idmap_permitted(const struct user_= namespace *ns, if (cap_setid =3D=3D CAP_SETUID) return uid_eq(make_kuid(ns->parent, id), cred->euid); if (cap_setid =3D=3D CAP_SETGID) - return !(ns_flags & USERNS_SETGROUPS_ALLOWED) && + return !(ns_flags & USERNS_STATE_SETGROUPS_ALLOWED) && gid_eq(make_kgid(ns->parent, id), cred->egid); return false; } @@ -1246,27 +1246,24 @@ static int parse_user_idmap_extents(__u64 uptr, __u= 32 nr_extents, =20 /** * userns_set_idmap - Handle %NS_SET_IDMAP on a user namespace nsfs file - * @filp: ``nsfs`` file referencing @ns * @ns: Target user namespace * @map: User pointer to &struct userns_idmap configuration * @size: Size of @map in bytes, encoded in the ioctl number * * Atomically configure UID, GID, and project ID mappings (and, when - * @map->nr_gid_extents is non-zero, the %USERNS_SETGROUPS_ALLOWED policy)= of - * @ns. Requires ``CAP_SYS_ADMIN`` over @ns in the caller's or @filp opene= r's - * credentials, with parent ID ranges authorized against the caller's - * credentials. + * @map->nr_gid_extents is non-zero, the setgroups policy) of @ns. The + * caller must have ``CAP_SYS_ADMIN`` over @ns; the descriptor does not + * confer authority. Parent ID ranges are authorized against the caller. * * Return: 0 on success, negative error code on failure. */ -long userns_set_idmap(struct file *filp, struct user_namespace *ns, +long userns_set_idmap(struct user_namespace *ns, const struct userns_idmap __user *map, size_t size) { struct uid_gid_map new_uid =3D {}, new_gid =3D {}, new_projid =3D {}; struct userns_idmap req =3D {}; unsigned long new_flags; unsigned int flags; - bool file_ok; int ret; =20 BUILD_BUG_ON(sizeof(struct userns_idmap) !=3D USERNS_IDMAP_SIZE_VER0); @@ -1292,10 +1289,7 @@ long userns_set_idmap(struct file *filp, struct user= _namespace *ns, if (current_user_ns() !=3D ns && current_user_ns() !=3D ns->parent) return -EPERM; =20 - file_ok =3D (filp->f_cred->user_ns =3D=3D ns || - filp->f_cred->user_ns =3D=3D ns->parent) && - file_ns_capable(filp, ns, CAP_SYS_ADMIN); - if (!ns_capable(ns, CAP_SYS_ADMIN) && !file_ok) + if (!ns_capable(ns, CAP_SYS_ADMIN)) return -EPERM; =20 if (req.nr_uid_extents) { @@ -1331,10 +1325,10 @@ long userns_set_idmap(struct file *filp, struct use= r_namespace *ns, =20 if (req.nr_gid_extents) { if ((flags & USERNS_SETGROUPS_ALLOWED) && - !(new_flags & USERNS_SETGROUPS_ALLOWED)) + !(new_flags & USERNS_STATE_SETGROUPS_ALLOWED)) goto out_unlock; if (!(flags & USERNS_SETGROUPS_ALLOWED)) - new_flags &=3D ~USERNS_SETGROUPS_ALLOWED; + new_flags &=3D ~USERNS_STATE_SETGROUPS_ALLOWED; } =20 if (req.nr_uid_extents) { @@ -1465,7 +1459,7 @@ int proc_setgroups_show(struct seq_file *seq, void *v) unsigned long userns_flags =3D READ_ONCE(ns->flags); =20 seq_printf(seq, "%s\n", - (userns_flags & USERNS_SETGROUPS_ALLOWED) ? + (userns_flags & USERNS_STATE_SETGROUPS_ALLOWED) ? "allow" : "deny"); return 0; } @@ -1515,7 +1509,7 @@ ssize_t proc_setgroups_write(struct file *file, const= char __user *buf, /* Enabling setgroups after setgroups has been disabled * is not allowed. */ - if (!(ns->flags & USERNS_SETGROUPS_ALLOWED)) + if (!(ns->flags & USERNS_STATE_SETGROUPS_ALLOWED)) goto out_unlock; } else { /* Permanently disabling setgroups after setgroups has @@ -1523,7 +1517,7 @@ ssize_t proc_setgroups_write(struct file *file, const= char __user *buf, */ if (ns->gid_map.nr_extents !=3D 0) goto out_unlock; - ns->flags &=3D ~USERNS_SETGROUPS_ALLOWED; + ns->flags &=3D ~USERNS_STATE_SETGROUPS_ALLOWED; } mutex_unlock(&userns_state_mutex); =20 @@ -1547,7 +1541,7 @@ bool userns_may_setgroups(const struct user_namespace= *ns) */ allowed =3D ns->gid_map.nr_extents !=3D 0; /* Is setgroups allowed? */ - allowed =3D allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED); + allowed =3D allowed && (ns->flags & USERNS_STATE_SETGROUPS_ALLOWED); mutex_unlock(&userns_state_mutex); =20 return allowed;