]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: avoid overflow in MODE_TO_PTR() with MODE_INVALID 38341/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 25 Jul 2025 18:38:56 +0000 (03:38 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 25 Jul 2025 20:00:02 +0000 (05:00 +0900)
Note, currently MODE_TO_PTR() and PTR_TO_MODE() are only used in
src/udev/udev-rules.c .

Fixes CID#1548060.

src/basic/fs-util.h

index b04fe71f037351260bfa2de203c1b433d1938082..74c1411556155c82b21ce37e01ca663fc0e9201f 100644 (file)
@@ -6,8 +6,12 @@
 
 /* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
  * NULL is special */
-#define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
-#define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1))
+static inline mode_t PTR_TO_MODE(void *p) {
+        return p ? (mode_t) ((uintptr_t) p - 1) : MODE_INVALID;
+}
+static inline void* MODE_TO_PTR(mode_t m) {
+        return m == MODE_INVALID ? NULL : (void *) ((uintptr_t) m + 1);
+}
 
 int rmdir_parents(const char *path, const char *stop);