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