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