map_write() and its permission helpers validate extent ranges, check
root and unprivileged self-mappings, translate parent ID ranges, sort
extents, and install the resulting map inline for procfs text writes,
preventing non-procfs callers from reusing the same validation and
commit path.

Extract idmap_maps_root(), unpriv_idmap_permitted(),
validate_and_insert_extent(), free_new_idmap(), resolve_new_idmap(), and
commit_new_idmap() with no functional change.

Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 kernel/user_namespace.c | 229 +++++++++++++++++++++-------------------
 1 file changed, 122 insertions(+), 107 deletions(-)

diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index cd748e1cbace..caba2ac33077 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -884,6 +884,22 @@ VISIBLE_IF_KUNIT int uid_gid_map_sort(struct uid_gid_map 
*map)
 }
 EXPORT_SYMBOL_IF_KUNIT(uid_gid_map_sort);
 
+static bool idmap_maps_root(const struct uid_gid_map *new_map)
+{
+       unsigned int idx;
+
+       for (idx = 0; idx < new_map->nr_extents; idx++) {
+               const struct uid_gid_extent *e;
+
+               if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+                       e = &new_map->extent[idx];
+               else
+                       e = &new_map->forward[idx];
+               if (e->lower_first == 0)
+                       return true;
+       }
+       return false;
+}
 /**
  * verify_root_map() - check the uid 0 mapping
  * @file: idmapping file
@@ -900,22 +916,9 @@ static bool verify_root_map(const struct file *file,
                            struct user_namespace *map_ns,
                            struct uid_gid_map *new_map)
 {
-       int idx;
        const struct user_namespace *file_ns = file->f_cred->user_ns;
-       struct uid_gid_extent *extent0 = NULL;
-
-       for (idx = 0; idx < new_map->nr_extents; idx++) {
-               if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
-                       extent0 = &new_map->extent[idx];
-               else
-                       extent0 = &new_map->forward[idx];
-               if (extent0->lower_first == 0)
-                       break;
-
-               extent0 = NULL;
-       }
 
-       if (!extent0)
+       if (!idmap_maps_root(new_map))
                return true;
 
        if (map_ns == file_ns) {
@@ -938,6 +941,103 @@ 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_range_up(),
+        * and m_start().
+        */
+       smp_wmb();
+       map->nr_extents = new_map->nr_extents;
+       memset(new_map, 0, sizeof(*new_map));
+}
+
+static bool unpriv_idmap_permitted(const struct user_namespace *ns,
+                                  const struct cred *cred, int cap_setid,
+                                  unsigned long ns_flags,
+                                  const struct uid_gid_map *new_map)
+{
+       u32 id;
+
+       if (new_map->nr_extents != 1 || new_map->extent[0].count != 1 ||
+           !uid_eq(ns->owner, cred->euid))
+               return false;
+
+       id = new_map->extent[0].lower_first;
+       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) &&
+                      gid_eq(make_kgid(ns->parent, id), cred->egid);
+       return false;
+}
+
 /*
  * Parse the decimal u32 at *pos, up to the next whitespace or NUL, and
  * leave *pos pointing at that character.
@@ -966,8 +1066,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 +1101,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 +1149,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;
@@ -1213,20 +1240,8 @@ static bool new_idmap_permitted(const struct file *file,
        /* Don't allow mappings that would allow anything that wouldn't
         * be allowed without the establishment of unprivileged mappings.
         */
-       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 (unpriv_idmap_permitted(ns, cred, cap_setid, ns->flags, new_map))
+               return true;
 
        /* Allow anyone to set a mapping that doesn't require privilege */
        if (!cap_valid(cap_setid))
-- 
2.47.3


Reply via email to