dcavalca / rpms / util-linux

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