dcavalca / rpms / util-linux

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