Re: [RFC PATCH v10 02/28] userns: Add NS_SET_IDMAP ioctl

From: Jarkko Sakkinen

Date: Sat Dec 05 2026 - 08:48:05 EST


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 ioctl,
if (ns->ns_type != CLONE_NEWUSER)
return -EINVAL;

- 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 */
};
};

-#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
+#define USERNS_STATE_SETGROUPS_ALLOWED 1UL
+#define USERNS_INIT_FLAGS USERNS_STATE_SETGROUPS_ALLOWED

struct ucounts;
struct userns_idmap;
@@ -207,7 +208,7 @@ extern bool in_userns(const struct user_namespace *ancestor,
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);

#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);
}

-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 = ucounts;

- /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
+ /* Inherit the setgroups policy from our parent. */
mutex_lock(&userns_state_mutex);
ns->flags = 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 == CAP_SETUID)
return uid_eq(make_kuid(ns->parent, id), cred->euid);
if (cap_setid == 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, __u32 nr_extents,

/**
* 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 opener'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 = {}, new_gid = {}, new_projid = {};
struct userns_idmap req = {};
unsigned long new_flags;
unsigned int flags;
- bool file_ok;
int ret;

BUILD_BUG_ON(sizeof(struct userns_idmap) != USERNS_IDMAP_SIZE_VER0);
@@ -1292,10 +1289,7 @@ long userns_set_idmap(struct file *filp, struct user_namespace *ns,
if (current_user_ns() != ns && current_user_ns() != ns->parent)
return -EPERM;

- 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)
+ if (!ns_capable(ns, CAP_SYS_ADMIN))
return -EPERM;

if (req.nr_uid_extents) {
@@ -1331,10 +1325,10 @@ long userns_set_idmap(struct file *filp, struct user_namespace *ns,

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 &= ~USERNS_SETGROUPS_ALLOWED;
+ new_flags &= ~USERNS_STATE_SETGROUPS_ALLOWED;
}

if (req.nr_uid_extents) {
@@ -1465,7 +1459,7 @@ int proc_setgroups_show(struct seq_file *seq, void *v)
unsigned long userns_flags = READ_ONCE(ns->flags);

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 != 0)
goto out_unlock;
- ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
+ ns->flags &= ~USERNS_STATE_SETGROUPS_ALLOWED;
}
mutex_unlock(&userns_state_mutex);

@@ -1547,7 +1541,7 @@ bool userns_may_setgroups(const struct user_namespace *ns)
*/
allowed = ns->gid_map.nr_extents != 0;
/* Is setgroups allowed? */
- allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
+ allowed = allowed && (ns->flags & USERNS_STATE_SETGROUPS_ALLOWED);
mutex_unlock(&userns_state_mutex);

return allowed;