b394b9
From 8125a64ff9e98d09c659dbd5adbca521d63a268b Mon Sep 17 00:00:00 2001
b394b9
From: Karel Zak <kzak@redhat.com>
b394b9
Date: Thu, 7 Jul 2016 14:22:41 +0200
b394b9
Subject: [PATCH 86/86] libblkid: avoid recursion in EBR
b394b9
b394b9
Upstream: http://github.com/karelzak/util-linux/commit/7164a1c34d18831ac61c6744ad14ce916d389b3f
b394b9
Upstream: http://github.com/karelzak/util-linux/commit/50d1594c2e6142a3b51d2143c74027480df082e0
b394b9
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1349536
b394b9
Signed-off-by: Karel Zak <kzak@redhat.com>
b394b9
---
b394b9
 libblkid/src/partitions/dos.c        | 21 +++++++++++++++++++--
b394b9
 libblkid/src/partitions/partitions.c | 14 ++++++++++++++
b394b9
 libblkid/src/partitions/partitions.h |  2 ++
b394b9
 3 files changed, 35 insertions(+), 2 deletions(-)
b394b9
b394b9
diff --git a/libblkid/src/partitions/dos.c b/libblkid/src/partitions/dos.c
b394b9
index 2d4a537..563fe9a 100644
b394b9
--- a/libblkid/src/partitions/dos.c
b394b9
+++ b/libblkid/src/partitions/dos.c
b394b9
@@ -48,6 +48,12 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
b394b9
 	int ct_nodata = 0;	/* count ext.partitions without data partitions */
b394b9
 	int i;
b394b9
 
b394b9
+	DBG(LOWPROBE, blkid_debug("parse EBR [start=%d, size=%d]", ex_start/ssf, ex_size/ssf));
b394b9
+	if (ex_start == 0) {
b394b9
+		DBG(LOWPROBE, blkid_debug("Bad offset in primary extended partition -- ignore"));
b394b9
+		return 0;
b394b9
+	}
b394b9
+
b394b9
 	while (1) {
b394b9
 		struct dos_partition *p, *p0;
b394b9
 		uint32_t start, size;
b394b9
@@ -100,6 +106,13 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
b394b9
 					continue;
b394b9
 			}
b394b9
 
b394b9
+			/* Avoid recursive non-empty links, see ct_nodata counter */
b394b9
+			if (blkid_partlist_get_partition_by_start(ls, abs_start)) {
b394b9
+				DBG(LOWPROBE, blkid_debug("#%d: EBR duplicate data partition [abs start=%u] -- ignore",
b394b9
+							i + 1, abs_start));
b394b9
+				continue;
b394b9
+			}
b394b9
+
b394b9
 			par = blkid_partlist_add_partition(ls, tab, abs_start, size);
b394b9
 			if (!par)
b394b9
 				return -ENOMEM;
b394b9
@@ -116,8 +129,12 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
b394b9
 			start = dos_partition_start(p) * ssf;
b394b9
 			size = dos_partition_size(p) * ssf;
b394b9
 
b394b9
-			if (size && is_extended(p))
b394b9
-				break;
b394b9
+			if (size && is_extended(p)) {
b394b9
+				if (start == 0)
b394b9
+					DBG(LOWPROBE, blkid_debug("#%d: EBR link offset is zero -- ignore", i + 1));
b394b9
+				else
b394b9
+					break;
b394b9
+			}
b394b9
 		}
b394b9
 		if (i == 4)
b394b9
 			goto leave;
b394b9
diff --git a/libblkid/src/partitions/partitions.c b/libblkid/src/partitions/partitions.c
b394b9
index 752fc95..9d846ff 100644
b394b9
--- a/libblkid/src/partitions/partitions.c
b394b9
+++ b/libblkid/src/partitions/partitions.c
b394b9
@@ -928,6 +928,20 @@ blkid_partition blkid_partlist_get_partition(blkid_partlist ls, int n)
b394b9
 	return &ls->parts[n];
b394b9
 }
b394b9
 
b394b9
+blkid_partition blkid_partlist_get_partition_by_start(blkid_partlist ls, uint64_t start)
b394b9
+{
b394b9
+	int i, nparts;
b394b9
+	blkid_partition par;
b394b9
+
b394b9
+	nparts = blkid_partlist_numof_partitions(ls);
b394b9
+	for (i = 0; i < nparts; i++) {
b394b9
+		par = blkid_partlist_get_partition(ls, i);
b394b9
+		if ((uint64_t) blkid_partition_get_start(par) == start)
b394b9
+			return par;
b394b9
+	}
b394b9
+	return NULL;
b394b9
+}
b394b9
+
b394b9
 /**
b394b9
  * blkid_partlist_devno_to_partition:
b394b9
  * @ls: partitions list
b394b9
diff --git a/libblkid/src/partitions/partitions.h b/libblkid/src/partitions/partitions.h
b394b9
index 61763bf..4e99e2a 100644
b394b9
--- a/libblkid/src/partitions/partitions.h
b394b9
+++ b/libblkid/src/partitions/partitions.h
b394b9
@@ -20,6 +20,8 @@ extern int blkid_partlist_increment_partno(blkid_partlist ls);
b394b9
 
b394b9
 extern blkid_partition blkid_partlist_get_parent(blkid_partlist ls);
b394b9
 
b394b9
+extern blkid_partition blkid_partlist_get_partition_by_start(blkid_partlist ls, uint64_t start);
b394b9
+
b394b9
 extern int blkid_partitions_do_subprobe(blkid_probe pr,
b394b9
 			blkid_partition parent, const struct blkid_idinfo *id);
b394b9
 
b394b9
-- 
b394b9
2.7.4
b394b9