]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use stat_verify_directory() more
authorLennart Poettering <lennart@amutable.com>
Mon, 13 Apr 2026 09:10:03 +0000 (11:10 +0200)
committerLennart Poettering <lennart@amutable.com>
Mon, 13 Apr 2026 11:20:43 +0000 (13:20 +0200)
src/libsystemd/sd-journal/sd-journal.c
src/shared/btrfs-util.c
src/shared/chown-recursive.c
src/shared/find-esp.c
src/shared/rm-rf.c

index 3e5185b23f7e08fbe89791f63b96459aae1aa0ce..52b3e616a172edadc7ff1c0d064ecb269cbbd816 100644 (file)
@@ -2456,7 +2456,6 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla
 
 _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
         _cleanup_(sd_journal_closep) sd_journal *j = NULL;
-        struct stat st;
         bool take_fd;
         int r;
 
@@ -2464,11 +2463,9 @@ _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
         assert_return(fd >= 0, -EBADF);
         assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
 
-        if (fstat(fd, &st) < 0)
-                return -errno;
-
-        if (!S_ISDIR(st.st_mode))
-                return -EBADFD;
+        r = fd_verify_directory(fd);
+        if (r < 0)
+                return r;
 
         take_fd = FLAGS_SET(flags, SD_JOURNAL_TAKE_DIRECTORY_FD);
         j = journal_new(flags & ~SD_JOURNAL_TAKE_DIRECTORY_FD, NULL, NULL);
index 8e41f569ba2352e330c2b9b4e01a8e23dd5560c9..cde21bd6029656dfaaf348e6d77b9fce9d102b3c 100644 (file)
@@ -879,18 +879,15 @@ static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol
 
         struct btrfs_ioctl_vol_args vol_args = {};
         _cleanup_close_ int subvol_fd = -EBADF;
-        struct stat st;
         bool made_writable = false;
         int r;
 
         assert(fd >= 0);
         assert(subvolume);
 
-        if (fstat(fd, &st) < 0)
-                return -errno;
-
-        if (!S_ISDIR(st.st_mode))
-                return -EINVAL;
+        r = fd_verify_directory(fd);
+        if (r < 0)
+                return r;
 
         subvol_fd = openat(fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
         if (subvol_fd < 0)
index 850e495836b535241f3790bca3bd60c6f7eb1000..a0e885ece954779d143d5c04369fb51e8d18e326 100644 (file)
@@ -9,6 +9,7 @@
 #include "fd-util.h"
 #include "fs-util.h"
 #include "path-util.h"
+#include "stat-util.h"
 #include "strv.h"
 #include "user-util.h"
 #include "xattr-util.h"
@@ -144,6 +145,7 @@ int fd_chown_recursive(
 
         int duplicated_fd = -EBADF;
         struct stat st;
+        int r;
 
         /* Note that the slightly different order of fstat() and the checks here and in
          * path_chown_recursive(). That's because when we open the directory ourselves we can specify
@@ -153,8 +155,9 @@ int fd_chown_recursive(
         if (fstat(fd, &st) < 0)
                 return -errno;
 
-        if (!S_ISDIR(st.st_mode))
-                return -ENOTDIR;
+        r = stat_verify_directory(&st);
+        if (r < 0)
+                return r;
 
         if (!uid_is_valid(uid) && !gid_is_valid(gid) && FLAGS_SET(mask, 07777))
                 return 0; /* nothing to do */
index 3f490ced714cf00e377ee89f0a7f9093d7c1b574..1d13683f1286e97abeb5d7133c469c5f5377dc21 100644 (file)
@@ -297,8 +297,9 @@ static int verify_fsroot_dir(
                                       (unprivileged_mode && ERRNO_IS_NEG_PRIVILEGE(r)) ? LOG_DEBUG : LOG_ERR, r,
                                       "Failed to determine block device node of \"%s\": %m", path);
 
-        if (!S_ISDIR(sx.stx_mode))
-                return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "Path \"%s\" is not a directory", path);
+        r = statx_verify_directory(&sx);
+        if (r < 0)
+                return log_error_errno(r, "Path \"%s\" is not a directory", path);
 
         if (!FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT))
                 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
@@ -477,8 +478,9 @@ int find_esp_and_warn_at_full(
 
                 if (fstat(fd, &st) < 0)
                         return log_error_errno(errno, "Failed to stat '%s': %m", p);
-                if (!S_ISDIR(st.st_mode))
-                        return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "ESP path '%s' is not a directory.", p);
+                r = stat_verify_directory(&st);
+                if (r < 0)
+                        return log_error_errno(r, "ESP path '%s' is not a directory.", p);
 
                 if (ret_path)
                         *ret_path = TAKE_PTR(p);
@@ -829,8 +831,9 @@ int find_xbootldr_and_warn_at_full(
 
                 if (fstat(fd, &st) < 0)
                         return log_error_errno(errno, "Failed to stat '%s': %m", p);
-                if (!S_ISDIR(st.st_mode))
-                        return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "XBOOTLDR path '%s' is not a directory.", p);
+                r = stat_verify_directory(&st);
+                if (r < 0)
+                        return log_error_errno(r, "XBOOTLDR path '%s' is not a directory.", p);
 
                 if (ret_path)
                         *ret_path = TAKE_PTR(p);
index 8c179bbaca9aa3d9109eb5117252352622630570..ede18318abfe6509b466096a2e05a612b6633a99 100644 (file)
@@ -37,8 +37,9 @@ static int patch_dirfd_mode(
 
         if (fstat(dfd, &st) < 0)
                 return -errno;
-        if (!S_ISDIR(st.st_mode))
-                return -ENOTDIR;
+        r = stat_verify_directory(&st);
+        if (r < 0)
+                return r;
 
         if (FLAGS_SET(st.st_mode, 0700)) { /* Already set? */
                 if (refuse_already_set)