From cb61d2294aedb2b7e47807b8599be32912c54e80 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Thu, 31 Aug 2023 18:50:33 +0200 Subject: [PATCH] filesystem: move stat after open check To avoid time-of-check-time-of-use clash, take the stat() after successful open with fstat(). Also add some debugs for failing sys calls. --- lib/device/filesystem.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/device/filesystem.c b/lib/device/filesystem.c index 53cbc2d02..29b57d0c9 100644 --- a/lib/device/filesystem.c +++ b/lib/device/filesystem.c @@ -155,26 +155,26 @@ int fs_get_info(struct cmd_context *cmd, struct logical_volume *lv, return 0; } - if (stat(crypt_path, &st_crypt) < 0) { - log_error("Failed to get crypt path %s", crypt_path); - return 0; - } - memset(&info, 0, sizeof(info)); log_print_unless_silent("Checking crypt device %s on LV %s.", crypt_path, display_lvname(lv)); if ((fd = open(crypt_path, O_RDONLY)) < 0) { - log_error("Failed to open crypt path %s", crypt_path); + log_error("Failed to open crypt path %s.", crypt_path); return 0; } - if (ioctl(fd, BLKGETSIZE64, &info.crypt_dev_size_bytes) < 0) { - log_error("Failed to get crypt device size %s", crypt_path); - close(fd); + + if ((ret = fstat(fd, &st_crypt)) < 0) + log_sys_error("fstat", crypt_path); + else if ((ret = ioctl(fd, BLKGETSIZE64, &info.crypt_dev_size_bytes)) < 0) + log_error("Failed to get crypt device size %s.", crypt_path); + + if (close(fd)) + log_sys_debug("close", crypt_path); + + if (ret < 0) return 0; - } - close(fd); if (!fs_get_blkid(crypt_path, &info)) { log_error("No file system info from blkid for dm-crypt device %s on LV %s.",