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=1795219740; b=bwX643yXup7WQZh/N97+IrtX0ktr02CcI4Mplr7DR7xBBoFlzlGy+O0yjb7BG86pCCSy kMDGAo+JkGQifTlClOp/SNsAQK7hnxAzmRBtzsuL8f1ZFpewCVDf/MU5evRll23kYFaQo ta2o/4WSoYK+uOs3gg643MN/FIQZQbMrmqSOmyBQFW09yPX3tL7vCwuQvsDNCHpMg7K5o brRce44MLHt90e58iUeRSXvWD+XHVCauddJmh6bFOU5Nh4+hjp5FDAa9JHcmhTTk6dXI2 ZeYGiTIgG2HmPUoxZbOyD54t1i3fxhYuNKhhVd8BP88vf9a+pKMCo6XTjLz3V1iIbew== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795219740; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=HMiFFGmw0v9nxAge20cDn397wiErWOHZ9jOEttk4raU=; b=VqvLrWzFiMhOypVr3dCYhnGGuBcQwWg4a/nJvYn1EdACMiH79bkcETb4vu0jxnNNjn9J 1gXe3a0Me48DWH6WzYqlugEPki+k6mszh3oGBpg+Ac9qwrkdv3VQnDpPQRi211hqpz8us CVQ/eqRoxQX/QZ0locwlItk4dx5KZ4zxhy0O2pIrJf/sfxxpi/MJdSSbh2I3PYa6mWNMQ QtX0Dxm/BAwciVys/r71XxbijXW9NxAsXwE5EkBQ4ANjEJ1crHa9Wr/wYL68vwxYZAVK5 wrTDb48RtMIRTQ7ZK4KRIq115dvZfq20mkB3sU6YOUek27BxWxzBHCx3Sabe4JwZhUQ== 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=MCG9Jh12; 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 66BA3606A335; Sat, 21 Nov 2026 00:09:00 +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=MCG9Jh12 Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 254671F000C0; Sat, 21 Nov 2026 00:08:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795219739; bh=HMiFFGmw0v9nxAge20cDn397wiErWOHZ9jOEttk4raU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MCG9Jh1256gFmx/kJBdpvtrZ3dAFy6JucQNKjq+UEDDHvSfsCjIvK79puHbjOkQ2j yLwGkP/zWuSJdgoNpsZpPJQqe8beWIamFh64TiVd3IFdL4sWKjuuMoYrsyBe6B9GXr HxXJuEwWmpevvo9m2YuFz4UqgRwPQqBnef719+RqEXVE7M3RreB4FCdF3wgRro0Wdl xFVLfcZe1dOF6Xy5OXE79lBtJZxDpFc7EsLy4EZbJz29c5cxL2VpA0CJQeacS31Zdo G2VE4SZqQrVeumqNyU2QjuDFDNsNiqZXrhWfoS6+GYb0Qq33QmgNO2NoEm+TVjVmxv f0f7bDxjNsjbg== From: Jarkko Sakkinen To: linux-fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Alexander Mikhalitsyn , Alexander Viro , Andy Lutomirski , Christian Brauner , David Howells , Jan Kara , Jonathan Corbet , Josef Bacik , Kees Cook , Neal Gompa , Paul Moore , Serge Hallyn , Zygmunt Krynicki Subject: [RFC PATCH v5 01/25] userns: Factor out idmap validation and commit helpers Date: Sat, 21 Nov 2026 02:08:20 +0200 Message-ID: <20261121000844.1863324-2-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121000844.1863324-1-jarkko@kernel.org> References: <20261121000844.1863324-1-jarkko@kernel.org> X-Mailing-List: linux-api@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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