]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/dynamic-user: use fd_verify_linked() 40264/head
authorMike Yuan <me@yhndnzj.com>
Sat, 3 Jan 2026 19:25:53 +0000 (20:25 +0100)
committerMike Yuan <me@yhndnzj.com>
Sat, 3 Jan 2026 20:25:02 +0000 (21:25 +0100)
src/core/dynamic-user.c

index 576ce5c8b0eedca02a8590ce3af11607a3b01e01..921622d92c0ead926c4202827badbdab32bf5400 100644 (file)
@@ -21,6 +21,7 @@
 #include "serialize.h"
 #include "siphash24.h"
 #include "socket-util.h"
+#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "uid-classification.h"
@@ -232,8 +233,6 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) {
                 xsprintf(lock_path, "/run/systemd/dynamic-uid/" UID_FMT, candidate);
 
                 for (;;) {
-                        struct stat st;
-
                         lock_fd = open(lock_path, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
                         if (lock_fd < 0)
                                 return -errno;
@@ -246,10 +245,11 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) {
                                 return -errno;
                         }
 
-                        if (fstat(lock_fd, &st) < 0)
-                                return -errno;
-                        if (st.st_nlink > 0)
+                        r = fd_verify_linked(lock_fd);
+                        if (r >= 0)
                                 break;
+                        if (r != -EIDRM)
+                                return r;
 
                         /* Oh, bummer, we got the lock, but the file was unlinked between the time we opened it and
                          * got the lock. Close it, and try again. */