File 0011-filesystem-move-stat-after-open-check.patch of Package lvm2.38362

From b5b97c2b663565833218f68b196327ef63b1a114 Mon Sep 17 00:00:00 2001
From: Zdenek Kabelac <zkabelac@redhat.com>
Date: Thu, 31 Aug 2023 18:50:33 +0200
Subject: [PATCH 11/24] 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.",
-- 
2.35.3

openSUSE Build Service is sponsored by