]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
user-record: extract user_record_image_is_blockdev() common helper 40979/head
authorMike Yuan <me@yhndnzj.com>
Fri, 6 Mar 2026 21:07:31 +0000 (22:07 +0100)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 6 Mar 2026 21:50:48 +0000 (22:50 +0100)
src/shared/user-record.c
test/fuzz/fuzz-user-record/crash-empty-image-path.json [new file with mode: 0644]

index 4b6ba997a60982dfad3360f86252668a0cc66bd0..d5e572dc094db415a26d652a17844ec6f4ac9c1f 100644 (file)
@@ -1829,6 +1829,16 @@ const char* user_record_image_path(UserRecord *h) {
                 user_record_home_directory_real(h) : NULL;
 }
 
+static bool user_record_image_is_blockdev(UserRecord *h) {
+        assert(h);
+
+        const char *p = user_record_image_path(h);
+        if (!p)
+                return false;
+
+        return path_startswith(p, "/dev/");
+}
+
 const char* user_record_cifs_user_name(UserRecord *h) {
         assert(h);
 
@@ -1880,24 +1890,18 @@ const char* user_record_real_name(UserRecord *h) {
 }
 
 bool user_record_luks_discard(UserRecord *h) {
-        const char *ip;
-
         assert(h);
 
         if (h->luks_discard >= 0)
                 return h->luks_discard;
 
-        ip = user_record_image_path(h);
-        if (!ip)
-                return false;
-
         /* Use discard by default if we are referring to a real block device, but not when operating on a
          * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
          * when storing stuff on top of regular file systems in loopback files as doing discard then would
          * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
          * on should the disk space to back our file systems not be available. */
 
-        return path_startswith(ip, "/dev/");
+        return user_record_image_is_blockdev(h);
 }
 
 bool user_record_luks_offline_discard(UserRecord *h) {
@@ -2063,7 +2067,7 @@ int user_record_removable(UserRecord *h) {
                 return -1;
 
         /* For now consider only LUKS home directories with a reference by path as removable */
-        return storage == USER_LUKS && path_startswith(user_record_image_path(h), "/dev/");
+        return storage == USER_LUKS && user_record_image_is_blockdev(h);
 }
 
 uint64_t user_record_ratelimit_interval_usec(UserRecord *h) {
diff --git a/test/fuzz/fuzz-user-record/crash-empty-image-path.json b/test/fuzz/fuzz-user-record/crash-empty-image-path.json
new file mode 100644 (file)
index 0000000..0506a71
--- /dev/null
@@ -0,0 +1,4 @@
+{
+    "userName": "root",
+     "storage": "luks"
+}