diff --git a/lib/utils_devpath.c b/lib/utils_devpath.c index fff02459..a7fb582e 100644 --- a/lib/utils_devpath.c +++ b/lib/utils_devpath.c @@ -420,7 +420,7 @@ int lookup_by_disk_id(const char *dm_uuid) { struct dirent *entry; struct stat st; - int r = 0; /* not found */ + int dfd, r = 0; /* not found */ DIR *dir = opendir("/dev/disk/by-id"); if (!dir) @@ -432,7 +432,8 @@ int lookup_by_disk_id(const char *dm_uuid) !strncmp(entry->d_name, "..", 2)) continue; - if (fstatat(dirfd(dir), entry->d_name, &st, AT_SYMLINK_NOFOLLOW)) { + dfd = dirfd(dir); + if (dfd < 0 || fstatat(dfd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW)) { r = -EINVAL; break; } @@ -457,7 +458,7 @@ int lookup_by_sysfs_uuid_field(const char *dm_uuid) char subpath[PATH_MAX], uuid[DM_UUID_LEN]; ssize_t s; struct stat st; - int fd, len, r = 0; /* not found */ + int fd, dfd, len, r = 0; /* not found */ DIR *dir = opendir("/sys/block/"); if (!dir) @@ -476,7 +477,10 @@ int lookup_by_sysfs_uuid_field(const char *dm_uuid) } /* looking for dm-X/dm/uuid file, symlinks are fine */ - fd = openat(dirfd(dir), subpath, O_RDONLY | O_CLOEXEC); + dfd = dirfd(dir); + if (dfd < 0) + continue; + fd = openat(dfd, subpath, O_RDONLY | O_CLOEXEC); if (fd < 0) continue; diff --git a/src/utils_blockdev.c b/src/utils_blockdev.c index e33b8397..dab541d3 100644 --- a/src/utils_blockdev.c +++ b/src/utils_blockdev.c @@ -51,7 +51,7 @@ static int lookup_holder_dm_name(const char *dm_uuid, dev_t devno, char **r_dm_n char dm_subpath[PATH_MAX], data_dev_dir[PATH_MAX], uuid[DM_UUID_LEN], dm_name[PATH_MAX] = {}; ssize_t s; struct stat st; - int dmfd, fd, len, r = 0; /* not found */ + int dmfd, fd, dfd, len, r = 0; /* not found */ DIR *dir; if (!r_dm_name) @@ -84,7 +84,10 @@ static int lookup_holder_dm_name(const char *dm_uuid, dev_t devno, char **r_dm_n } /* looking for dm-X/dm directory, symlinks are fine */ - dmfd = openat(dirfd(dir), dm_subpath, O_DIRECTORY | O_RDONLY); + dfd = dirfd(dir); + if (dfd < 0) + continue; + dmfd = openat(dfd, dm_subpath, O_DIRECTORY | O_RDONLY); if (dmfd < 0) continue;