]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: make more use of stat_verify_device_node()
authorLennart Poettering <lennart@amutable.com>
Mon, 13 Apr 2026 08:28:54 +0000 (10:28 +0200)
committerLennart Poettering <lennart@amutable.com>
Mon, 13 Apr 2026 11:20:43 +0000 (13:20 +0200)
src/basic/device-nodes.c
src/core/bpf-devices.c
src/nspawn/nspawn.c

index 37af18fce419c8e0f1d64b2f6151a03e4b96c155..15abe31236968aa29dd50c687e84388b98f0ddb8 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "device-nodes.h"
 #include "path-util.h"
+#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "utf8.h"
@@ -63,6 +64,7 @@ int encode_devnode_name(const char *str, char *str_enc, size_t len) {
 
 int devnode_same(const char *a, const char *b) {
         struct stat sa, sb;
+        int r;
 
         assert(a);
         assert(b);
@@ -72,13 +74,15 @@ int devnode_same(const char *a, const char *b) {
 
         if (stat(a, &sa) < 0)
                 return -errno;
+        r = stat_verify_device_node(&sa);
+        if (r < 0)
+                return r;
+
         if (stat(b, &sb) < 0)
                 return -errno;
-
-        if (!S_ISBLK(sa.st_mode) && !S_ISCHR(sa.st_mode))
-                return -ENODEV;
-        if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode))
-                return -ENODEV;
+        r = stat_verify_device_node(&sb);
+        if (r < 0)
+                return r;
 
         if (((sa.st_mode ^ sb.st_mode) & S_IFMT) != 0) /* both inode same device node type? */
                 return false;
index aeb01e85753954120676302b809cf5c8dde9b3aa..432b3d0162555aa5524967b33540a1d4bae7d172 100644 (file)
@@ -16,6 +16,7 @@
 #include "nulstr-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "stat-util.h"
 #include "string-util.h"
 
 #define PASS_JUMP_OFF 4096
@@ -308,8 +309,9 @@ int bpf_devices_allow_list_device(
                         return log_warning_errno(errno, "Couldn't stat device %s: %m", node);
                 }
 
-                if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
-                        return log_warning_errno(SYNTHETIC_ERRNO(ENODEV), "%s is not a device.", node);
+                r = stat_verify_device_node(&st);
+                if (r < 0)
+                        return log_warning_errno(r, "'%s' is not a device node.", node);
 
                 mode = st.st_mode;
                 rdev = (dev_t) st.st_rdev;
index b6332844db80c379933e6f4204479d985b7471d8..74364ae1e7d6077bf0ad889f225090522116d61e 100644 (file)
@@ -2049,8 +2049,9 @@ static int copy_devnode_one(const char *dest, const char *node, bool check) {
                 log_debug_errno(errno, "Device node %s does not exist, ignoring.", from);
                 return 0;
         }
-        if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
-                return log_error_errno(SYNTHETIC_ERRNO(ESTALE), "%s is not a device node.", from);
+        r = stat_verify_device_node(&st);
+        if (r < 0)
+                return log_error_errno(r, "'%s' is not a device node.", from);
 
         /* Create the parent directory of the device node. Here, we assume that the path has at most one
          * subdirectory under /dev/, e.g. /dev/net/tun. */