From 8125a64ff9e98d09c659dbd5adbca521d63a268b Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 7 Jul 2016 14:22:41 +0200
Subject: [PATCH 86/86] libblkid: avoid recursion in EBR

Upstream: http://github.com/karelzak/util-linux/commit/7164a1c34d18831ac61c6744ad14ce916d389b3f
Upstream: http://github.com/karelzak/util-linux/commit/50d1594c2e6142a3b51d2143c74027480df082e0
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1349536
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 libblkid/src/partitions/dos.c        | 21 +++++++++++++++++++--
 libblkid/src/partitions/partitions.c | 14 ++++++++++++++
 libblkid/src/partitions/partitions.h |  2 ++
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/libblkid/src/partitions/dos.c b/libblkid/src/partitions/dos.c
index 2d4a537..563fe9a 100644
--- a/libblkid/src/partitions/dos.c
+++ b/libblkid/src/partitions/dos.c
@@ -48,6 +48,12 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
	int ct_nodata = 0;	/* count ext.partitions without data partitions */
	int i;

+	DBG(LOWPROBE, blkid_debug("parse EBR [start=%d, size=%d]", ex_start/ssf, ex_size/ssf));
+	if (ex_start == 0) {
+		DBG(LOWPROBE, blkid_debug("Bad offset in primary extended partition -- ignore"));
+		return 0;
+	}
+
	while (1) {
		struct dos_partition *p, *p0;
		uint32_t start, size;
@@ -100,6 +106,13 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
					continue;
			}

+			/* Avoid recursive non-empty links, see ct_nodata counter */
+			if (blkid_partlist_get_partition_by_start(ls, abs_start)) {
+				DBG(LOWPROBE, blkid_debug("#%d: EBR duplicate data partition [abs start=%u] -- ignore",
+							i + 1, abs_start));
+				continue;
+			}
+
			par = blkid_partlist_add_partition(ls, tab, abs_start, size);
			if (!par)
				return -ENOMEM;
@@ -116,8 +129,12 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
			start = dos_partition_start(p) * ssf;
			size = dos_partition_size(p) * ssf;

-			if (size && is_extended(p))
-				break;
+			if (size && is_extended(p)) {
+				if (start == 0)
+					DBG(LOWPROBE, blkid_debug("#%d: EBR link offset is zero -- ignore", i + 1));
+				else
+					break;
+			}
		}
		if (i == 4)
			goto leave;
diff --git a/libblkid/src/partitions/partitions.c b/libblkid/src/partitions/partitions.c
index 752fc95..9d846ff 100644
--- a/libblkid/src/partitions/partitions.c
+++ b/libblkid/src/partitions/partitions.c
@@ -928,6 +928,20 @@ blkid_partition blkid_partlist_get_partition(blkid_partlist ls, int n)
	return &ls->parts[n];
 }

+blkid_partition blkid_partlist_get_partition_by_start(blkid_partlist ls, uint64_t start)
+{
+	int i, nparts;
+	blkid_partition par;
+
+	nparts = blkid_partlist_numof_partitions(ls);
+	for (i = 0; i < nparts; i++) {
+		par = blkid_partlist_get_partition(ls, i);
+		if ((uint64_t) blkid_partition_get_start(par) == start)
+			return par;
+	}
+	return NULL;
+}
+
 /**
  * blkid_partlist_devno_to_partition:
  * @ls: partitions list
diff --git a/libblkid/src/partitions/partitions.h b/libblkid/src/partitions/partitions.h
index 61763bf..4e99e2a 100644
--- a/libblkid/src/partitions/partitions.h
+++ b/libblkid/src/partitions/partitions.h
@@ -20,6 +20,8 @@ extern int blkid_partlist_increment_partno(blkid_partlist ls);

 extern blkid_partition blkid_partlist_get_parent(blkid_partlist ls);

+extern blkid_partition blkid_partlist_get_partition_by_start(blkid_partlist ls, uint64_t start);
+
 extern int blkid_partitions_do_subprobe(blkid_probe pr,
			blkid_partition parent, const struct blkid_idinfo *id);

--
2.7.4
