]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
user-util,storagectl: introduce USERNS_RANGE_SIZE macro
authorChristian Brauner <brauner@kernel.org>
Fri, 1 May 2026 12:58:53 +0000 (14:58 +0200)
committerChristian Brauner <brauner@kernel.org>
Wed, 6 May 2026 08:30:13 +0000 (10:30 +0200)
The mount.storage helper open-codes the conventional 64K UID/GID
delegation block size as 0x10000 / 0x10000U in four places. Several
other places in the tree do the same (nspawn's arg_uid_range default,
homed's mount setup, …), but with no shared name.

Add USERNS_RANGE_SIZE in user-util.h alongside UID_NOBODY and friends,
and switch storagectl over to it. Other call sites can adopt it
incrementally.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
src/basic/user-util.h
src/shared/nsresource.h
src/storage/storagectl.c

index 003420dbe3b0d7fa9aee9b8a10fc08d8ec7ee663..a8902aca6150b80361e7a0254876c211f2426143 100644 (file)
@@ -90,6 +90,10 @@ int take_etc_passwd_lock(const char *root);
 #define UID_NOBODY ((uid_t) 65534U)
 #define GID_NOBODY ((gid_t) 65534U)
 
+/* Conventional size of a user-namespace UID/GID delegation block (64K).
+ * Untyped so it can be used in both UID and GID contexts without casts. */
+#define USERNS_RANGE_SIZE 0x10000U
+
 /* If REMOUNT_IDMAPPING_HOST_ROOT is set for remount_idmap() we'll include a mapping here that maps the host
  * root user accessing the idmapped mount to the this user ID on the backing fs. This is the last valid UID in
  * the *signed* 32-bit range. You might wonder why precisely use this specific UID for this purpose? Well, we
index 5633fd9bf35bc3494b1de02b822e047e0d709193..c26dd4f8a553f31998d007d528d4b69c111f4bbc 100644 (file)
@@ -2,9 +2,10 @@
 #pragma once
 
 #include "shared-forward.h"
+#include "user-util.h"
 
 /* Helpful constants for the only numbers of UIDs that can currently be allocated */
-#define NSRESOURCE_UIDS_64K 0x10000U
+#define NSRESOURCE_UIDS_64K USERNS_RANGE_SIZE
 #define NSRESOURCE_UIDS_1 1U
 
 int nsresource_connect(sd_varlink **ret);
index f88dff29bc861cdd327a95486d21aa7fbf7c85e0..23d91d6ba12eed39d9dadc8751462c6fe16a1f80 100644 (file)
@@ -723,25 +723,25 @@ static int run_as_mount_helper(int argc, char *argv[]) {
                 if (!uid_is_valid(p.base_uid) || !gid_is_valid(p.base_gid))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Provider did not report base UID/GID, cannot mount.");
 
-                if (p.base_uid > UINT32_MAX - 0x10000U ||
-                    p.base_gid > UINT32_MAX - 0x10000U)
+                if (p.base_uid > UINT32_MAX - USERNS_RANGE_SIZE ||
+                    p.base_gid > UINT32_MAX - USERNS_RANGE_SIZE)
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Returned base UID/GID out of range.");
 
                 r = stat_verify_directory(&st);
                 if (r < 0)
                         return log_error_errno(r, "File descriptor for directory volume is not a directory inode: %m");
 
-                if (st.st_uid < p.base_uid || st.st_uid >= p.base_uid + 0x10000 ||
-                    st.st_gid < p.base_gid || st.st_gid >= p.base_gid + 0x10000)
+                if (st.st_uid < p.base_uid || st.st_uid >= p.base_uid + USERNS_RANGE_SIZE ||
+                    st.st_gid < p.base_gid || st.st_gid >= p.base_gid + USERNS_RANGE_SIZE)
                         return log_error_errno(SYNTHETIC_ERRNO(EPERM), "File descriptor for directory volume is not owned by base UID/GID range, refusing.");
 
                 /* Now move the mount into our own UID/GID range */
                 _cleanup_free_ char *uid_line = asprintf_safe(
                                 UID_FMT " " UID_FMT " " UID_FMT "\n",
-                                p.base_uid, (uid_t) 0, (uid_t) 0x10000);
+                                p.base_uid, (uid_t) 0, USERNS_RANGE_SIZE);
                 _cleanup_free_ char *gid_line = asprintf_safe(
                                 GID_FMT " " GID_FMT " " GID_FMT "\n",
-                                p.base_gid, (gid_t) 0, (gid_t) 0x10000);
+                                p.base_gid, (gid_t) 0, USERNS_RANGE_SIZE);
                 if (!uid_line || !gid_line)
                         return log_oom();