dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0023-libblkid-fix-file-descriptor-leak-in-blkid_verify.patch

22a545
From e431bfeca0f3e7be2eba30be83260f20976f871d Mon Sep 17 00:00:00 2001
22a545
From: Karel Zak <kzak@redhat.com>
22a545
Date: Wed, 31 Jul 2019 16:18:27 +0200
22a545
Subject: [PATCH 23/24] libblkid: fix file descriptor leak in blkid_verify()
22a545
22a545
The function blkid_verify() uses private device file descriptor and
22a545
uses blkid_probe_set_device() to assign the descriptor to low-level
22a545
probing code. Unfortunately, close() in this case is not enough as the
22a545
prober can internally open whole-disk device too.
22a545
22a545
The library API has been extended so blkid_probe_set_device()
22a545
deallocates and close previously used prober for whole-disk. This new
22a545
functionality is used in blkid_verify() now.
22a545
22a545
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1734553
22a545
Upstream: http://github.com/karelzak/util-linux/commit/c4d6d1c54dcd0eff701236d396d88b1fc6251768
22a545
Signed-off-by: Karel Zak <kzak@redhat.com>
22a545
---
22a545
 libblkid/src/probe.c  | 19 ++++++++++++++++---
22a545
 libblkid/src/verify.c |  4 +++-
22a545
 2 files changed, 19 insertions(+), 4 deletions(-)
22a545
22a545
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
22a545
index 647ae416a..a6dc8416a 100644
22a545
--- a/libblkid/src/probe.c
22a545
+++ b/libblkid/src/probe.c
22a545
@@ -848,10 +848,15 @@ failed:
22a545
  * @off: begin of probing area
22a545
  * @size: size of probing area (zero means whole device/file)
22a545
  *
22a545
- * Assigns the device to probe control struct, resets internal buffers and
22a545
- * resets the current probing.
22a545
+ * Assigns the device to probe control struct, resets internal buffers, resets
22a545
+ * the current probing, and close previously associated device (if open by
22a545
+ * libblkid).
22a545
  *
22a545
- * Returns: -1 in case of failure, or 0 on success.
22a545
+ * If @fd is < 0 than only resets the prober and returns 1. Note that
22a545
+ * blkid_reset_probe() keeps the device associated with the prober, but
22a545
+ * blkid_probe_set_device() does complete reset.
22a545
+ *
22a545
+ * Returns: -1 in case of failure, 0 on success and 1 on reset.
22a545
  */
22a545
 int blkid_probe_set_device(blkid_probe pr, int fd,
22a545
 		blkid_loff_t off, blkid_loff_t size)
22a545
@@ -866,6 +871,11 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
22a545
 	if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
22a545
 		close(pr->fd);
22a545
 
22a545
+	if (pr->disk_probe) {
22a545
+		blkid_free_probe(pr->disk_probe);
22a545
+		pr->disk_probe = NULL;
22a545
+	}
22a545
+
22a545
 	pr->flags &= ~BLKID_FL_PRIVATE_FD;
22a545
 	pr->flags &= ~BLKID_FL_TINY_DEV;
22a545
 	pr->flags &= ~BLKID_FL_CDROM_DEV;
22a545
@@ -881,6 +891,9 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
22a545
 	pr->wipe_size = 0;
22a545
 	pr->wipe_chain = NULL;
22a545
 
22a545
+	if (fd < 0)
22a545
+		return 1;
22a545
+
22a545
 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
22a545
 	/* Disable read-ahead */
22a545
 	posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
22a545
diff --git a/libblkid/src/verify.c b/libblkid/src/verify.c
22a545
index 7f44f5497..750378f8c 100644
22a545
--- a/libblkid/src/verify.c
22a545
+++ b/libblkid/src/verify.c
22a545
@@ -184,9 +184,11 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
22a545
 			   dev->bid_name, (long long)st.st_rdev, dev->bid_type));
22a545
 	}
22a545
 
22a545
-	blkid_reset_probe(cache->probe);
22a545
+	/* reset prober */
22a545
 	blkid_probe_reset_superblocks_filter(cache->probe);
22a545
+	blkid_probe_set_device(cache->probe, -1, 0, 0);
22a545
 	close(fd);
22a545
+
22a545
 	return dev;
22a545
 }
22a545
 
22a545
-- 
22a545
2.21.0
22a545