64664a
From 55540ea3dfdc707dc998333fd0715549522464fb Mon Sep 17 00:00:00 2001
64664a
From: Sebastian Krahmer <krahmer@suse.de>
64664a
Date: Fri, 5 Dec 2014 10:06:42 +0100
64664a
Subject: [PATCH 095/116] libblkid: fix potential bufer overflows
64664a
64664a
While digging deeper into libblk probing, I found that some
64664a
computations might wrap and allocate too few buffer space which then
64664a
overflows. In particular on 32bit systems (chromebook) where size_t is
64664a
32bit, this is problematic (for 64bit the result fits into the calloc
64664a
size_t).
64664a
64664a
Upstream: Upstream: https://github.com/karelzak/util-linux/commit/109df14fad4e9570e26950913ebace6c79289400
64664a
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1392656
64664a
Signed-off-by: Karel Zak <kzak@redhat.com>
64664a
---
64664a
 libblkid/src/partitions/gpt.c  | 12 ++++++++----
64664a
 libblkid/src/probe.c           |  7 +++++++
64664a
 libblkid/src/superblocks/zfs.c |  3 +++
64664a
 3 files changed, 18 insertions(+), 4 deletions(-)
64664a
64664a
diff --git a/libblkid/src/partitions/gpt.c b/libblkid/src/partitions/gpt.c
64664a
index 6ab0dc6..e801ea3 100644
64664a
--- a/libblkid/src/partitions/gpt.c
64664a
+++ b/libblkid/src/partitions/gpt.c
64664a
@@ -17,6 +17,7 @@
64664a
 #include <stdlib.h>
64664a
 #include <stdint.h>
64664a
 #include <stddef.h>
64664a
+#include <limits.h>
64664a
 
64664a
 #include "partitions.h"
64664a
 #include "crc32.h"
64664a
@@ -266,14 +267,17 @@ static struct gpt_header *get_gpt_header(
64664a
 		return NULL;
64664a
 	}
64664a
 
64664a
-	/* Size of blocks with GPT entries */
64664a
-	esz = le32_to_cpu(h->num_partition_entries) *
64664a
-			le32_to_cpu(h->sizeof_partition_entry);
64664a
-	if (!esz) {
64664a
+	if (le32_to_cpu(h->num_partition_entries) == 0 ||
64664a
+	    le32_to_cpu(h->sizeof_partition_entry) == 0 ||
64664a
+	    ULONG_MAX / le32_to_cpu(h->num_partition_entries) < le32_to_cpu(h->sizeof_partition_entry)) {
64664a
 		DBG(LOWPROBE, blkid_debug("GPT entries undefined"));
64664a
 		return NULL;
64664a
 	}
64664a
 
64664a
+	/* Size of blocks with GPT entries */
64664a
+	esz = le32_to_cpu(h->num_partition_entries) *
64664a
+			le32_to_cpu(h->sizeof_partition_entry);
64664a
+
64664a
 	/* The header seems valid, save it
64664a
 	 * (we don't care about zeros in hdr->reserved2 area) */
64664a
 	memcpy(hdr, h, sizeof(*h));
64664a
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
64664a
index f9fab5b..9cf099a 100644
64664a
--- a/libblkid/src/probe.c
64664a
+++ b/libblkid/src/probe.c
64664a
@@ -103,6 +103,7 @@
64664a
 #include <inttypes.h>
64664a
 #include <stdint.h>
64664a
 #include <stdarg.h>
64664a
+#include <limits.h>
64664a
 
64664a
 #ifdef HAVE_LIBUUID
64664a
 # include <uuid.h>
64664a
@@ -565,6 +566,12 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr,
64664a
 			return NULL;
64664a
 		}
64664a
 
64664a
+		/* someone trying to overflow some buffers? */
64664a
+		if (len > ULONG_MAX - sizeof(struct blkid_bufinfo)) {
64664a
+			errno = ENOMEM;
64664a
+			return NULL;
64664a
+		}
64664a
+
64664a
 		/* allocate info and space for data by why call */
64664a
 		bf = calloc(1, sizeof(struct blkid_bufinfo) + len);
64664a
 		if (!bf) {
64664a
diff --git a/libblkid/src/superblocks/zfs.c b/libblkid/src/superblocks/zfs.c
64664a
index 406ba2b..56ee472 100644
64664a
--- a/libblkid/src/superblocks/zfs.c
64664a
+++ b/libblkid/src/superblocks/zfs.c
64664a
@@ -12,6 +12,7 @@
64664a
 #include <errno.h>
64664a
 #include <ctype.h>
64664a
 #include <inttypes.h>
64664a
+#include <limits.h>
64664a
 
64664a
 #include "superblocks.h"
64664a
 
64664a
@@ -108,6 +109,8 @@ static void zfs_extract_guid_name(blkid_probe pr, loff_t offset)
64664a
 
64664a
 			nvs->nvs_type = be32_to_cpu(nvs->nvs_type);
64664a
 			nvs->nvs_strlen = be32_to_cpu(nvs->nvs_strlen);
64664a
+			if (nvs->nvs_strlen > UINT_MAX - sizeof(*nvs))
64664a
+				break;
64664a
 			avail -= nvs->nvs_strlen + sizeof(*nvs);
64664a
 			nvdebug("nvstring: type %u string %*s\n", nvs->nvs_type,
64664a
 				nvs->nvs_strlen, nvs->nvs_string);
64664a
-- 
64664a
2.9.3
64664a