tree-wide: use stat_verify_directory() more

This commit is contained in:
Lennart Poettering
2026-04-13 13:20:43 +02:00
parent 8e0756c0b5
commit 4031a73a4b
5 changed files with 23 additions and 22 deletions
+3 -6
View 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);
+3 -6
View 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)
+5 -2
View 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 */
+9 -6
View 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);
+3 -2
View 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)