[RFC PATCH v5 01/25] userns: Factor out idmap validation and commit helpers

From: Jarkko Sakkinen

Date: Fri Nov 20 2026 - 19:09:23 EST


map_write() validates extent ranges, translates parent ID ranges, sorts
extents, and installs the resulting map inline for procfs text writes,
preventing non-procfs callers from reusing the same validation and
commit path.

Extract validate_and_insert_extent(), free_new_idmap(),
resolve_new_idmap(), and commit_new_idmap() out of map_write() with no
functional change.

Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
kernel/user_namespace.c | 159 ++++++++++++++++++++--------------------
1 file changed, 80 insertions(+), 79 deletions(-)

diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index cd748e1cbace..6b9673dcdc67 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -938,6 +938,80 @@ static bool verify_root_map(const struct file *file,
return true;
}

+static int validate_and_insert_extent(struct uid_gid_map *new_map,
+ struct uid_gid_extent *extent)
+{
+ if ((extent->first == (u32)-1) || (extent->lower_first == (u32)-1))
+ return -EINVAL;
+
+ if ((extent->first + extent->count) <= extent->first)
+ return -EINVAL;
+ if ((extent->lower_first + extent->count) <= extent->lower_first)
+ return -EINVAL;
+
+ if (mappings_overlap(new_map, extent))
+ return -EINVAL;
+
+ if (new_map->nr_extents >= UID_GID_MAP_MAX_EXTENTS)
+ return -EINVAL;
+
+ return uid_gid_map_insert_extent(new_map, extent);
+}
+
+static void free_new_idmap(struct uid_gid_map *new_map)
+{
+ if (new_map->nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
+ kfree(new_map->forward);
+ kfree(new_map->reverse);
+ }
+ memset(new_map, 0, sizeof(*new_map));
+}
+
+static int resolve_new_idmap(struct uid_gid_map *new_map,
+ struct uid_gid_map *parent_map)
+{
+ unsigned int idx;
+
+ if (new_map->nr_extents == 0)
+ return -EINVAL;
+
+ for (idx = 0; idx < new_map->nr_extents; idx++) {
+ struct uid_gid_extent *e;
+ u32 lower_first;
+
+ if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+ e = &new_map->extent[idx];
+ else
+ e = &new_map->forward[idx];
+
+ lower_first = map_id_range_down(parent_map,
+ e->lower_first,
+ e->count);
+ if (lower_first == (u32)-1)
+ return -EPERM;
+
+ e->lower_first = lower_first;
+ }
+
+ return uid_gid_map_sort(new_map);
+}
+
+static void commit_new_idmap(struct uid_gid_map *map,
+ struct uid_gid_map *new_map)
+{
+ if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
+ memcpy(map->extent, new_map->extent,
+ new_map->nr_extents * sizeof(new_map->extent[0]));
+ } else {
+ map->forward = new_map->forward;
+ map->reverse = new_map->reverse;
+ }
+ /* Pairs with smp_rmb() in map_id_range_down() / map_id_up(). */
+ smp_wmb();
+ map->nr_extents = new_map->nr_extents;
+ memset(new_map, 0, sizeof(*new_map));
+}
+
/*
* Parse the decimal u32 at *pos, up to the next whitespace or NUL, and
* leave *pos pointing at that character.
@@ -966,8 +1040,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
{
struct seq_file *seq = file->private_data;
struct user_namespace *map_ns = seq->private;
- struct uid_gid_map new_map;
- unsigned idx;
+ struct uid_gid_map new_map = {};
struct uid_gid_extent extent;
char *kbuf, *pos, *next_line;
ssize_t ret;
@@ -1002,8 +1075,6 @@ static ssize_t map_write(struct file *file, const char __user *buf,
*/
mutex_lock(&userns_state_mutex);

- memset(&new_map, 0, sizeof(struct uid_gid_map));
-
ret = -EPERM;
/* Only allow one successful write to the map */
if (map->nr_extents != 0)
@@ -1052,98 +1123,28 @@ static ssize_t map_write(struct file *file, const char __user *buf,
if (*pos != '\0')
goto out;

- /* Verify we have been given valid starting values */
- if ((extent.first == (u32) -1) ||
- (extent.lower_first == (u32) -1))
- goto out;
-
- /* Verify count is not zero and does not cause the
- * extent to wrap
- */
- if ((extent.first + extent.count) <= extent.first)
- goto out;
- if ((extent.lower_first + extent.count) <=
- extent.lower_first)
- goto out;
-
- /* Do the ranges in extent overlap any previous extents? */
- if (mappings_overlap(&new_map, &extent))
- goto out;
-
- if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
- (next_line != NULL))
- goto out;
-
- ret = uid_gid_map_insert_extent(&new_map, &extent);
+ ret = validate_and_insert_extent(&new_map, &extent);
if (ret < 0)
goto out;
ret = -EINVAL;
}
- /* Be very certain the new map actually exists */
+
if (new_map.nr_extents == 0)
goto out;

ret = -EPERM;
- /* Validate the user is allowed to use user id's mapped to. */
if (!new_idmap_permitted(file, map_ns, cap_setid, &new_map))
goto out;

- ret = -EPERM;
- /* Map the lower ids from the parent user namespace to the
- * kernel global id space.
- */
- for (idx = 0; idx < new_map.nr_extents; idx++) {
- struct uid_gid_extent *e;
- u32 lower_first;
-
- if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
- e = &new_map.extent[idx];
- else
- e = &new_map.forward[idx];
-
- lower_first = map_id_range_down(parent_map,
- e->lower_first,
- e->count);
-
- /* Fail if we can not map the specified extent to
- * the kernel global id space.
- */
- if (lower_first == (u32) -1)
- goto out;
-
- e->lower_first = lower_first;
- }
-
- /*
- * If we want to use binary search for lookup, this clones the extent
- * array and sorts both copies.
- */
- ret = uid_gid_map_sort(&new_map);
+ ret = resolve_new_idmap(&new_map, parent_map);
if (ret < 0)
goto out;

- /* Install the map */
- if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
- memcpy(map->extent, new_map.extent,
- new_map.nr_extents * sizeof(new_map.extent[0]));
- } else {
- map->forward = new_map.forward;
- map->reverse = new_map.reverse;
- }
- smp_wmb();
- map->nr_extents = new_map.nr_extents;
-
+ commit_new_idmap(map, &new_map);
*ppos = count;
ret = count;
out:
- if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
- kfree(new_map.forward);
- kfree(new_map.reverse);
- map->forward = NULL;
- map->reverse = NULL;
- map->nr_extents = 0;
- }
-
+ free_new_idmap(&new_map);
mutex_unlock(&userns_state_mutex);
kfree(kbuf);
return ret;
--
2.47.3