diff mbox series

[v4,11/14] disk: part_dos: Number at most 255 partitions, as Linux does

Message ID c6adb9f0-ca33-4d3d-9f33-ce211ca84e81@gmail.com
State Deferred
Delegated to: Tom Rini
Headers show
Series block, disk: Check block sizes and partition tables of devices | expand

Commit Message

Ana Bobillo Nov. 4, 2026, 12:51 p.m. UTC
From: Ana Bobillo <anabobillo3@gmail.com>

part_get_info_extended() makes up the UUID of a DOS partition as
"%08x-%02x" from the disk signature and the partition number, into a
buffer of 12 bytes. That is enough up to partition 255, the most Linux
numbers (DISK_MAX_PARTS - 1), so that its MSDOS UUID always takes 11
characters. But MAX_EXT_PARTS lets the number reach 256, and as it is
checked once per table, the entries of the last table read can take
it up to 259: their three digits make sprintf() write the terminating
NUL past the end of the buffer.

Lower MAX_EXT_PARTS to 255 and check it before each entry, as Linux's
parse_extended() does, so that no partition gets a higher number and
every UUID keeps its 11 characters.

Add a test with 256 partitions, which catches the overflow on sandbox
with CONFIG_ASAN.

Signed-off-by: Ana Bobillo <anabobillo3@gmail.com>
---
Changes in v4:
- Number at most 255 partitions, as Linux does, so that the UUID keeps
  its 11 characters, rather than making room for a three-digit
  partition number (Heinrich): lower MAX_EXT_PARTS to 255 and check it
  before each entry, and test that there is no partition 256; new
  subject, and drop Simon's Reviewed-by

 disk/part_dos.c | 13 ++++++-----
 test/dm/part.c  | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/disk/part_dos.c b/disk/part_dos.c
index bc40d9d2829..bd9504a05bb 100644
--- a/disk/part_dos.c
+++ b/disk/part_dos.c
@@ -24,9 +24,12 @@ 
 
 #define DOS_PART_DEFAULT_SECTOR 512
 
-/* should this be configurable? It looks like it's not very common at all
- * to use large numbers of partitions */
-#define MAX_EXT_PARTS 256
+/*
+ * Number at most 255 partitions, as Linux does (DISK_MAX_PARTS - 1), so
+ * that the UUID made up for each from the disk signature and the partition
+ * number, "%08x-%02x", takes 11 characters
+ */
+#define MAX_EXT_PARTS 255
 
 /*
  * A link to the next extended partition table adds no partition, so tables
@@ -214,7 +217,7 @@  static void print_partition_extended(struct blk_desc *desc,
 
 		/* Print all primary/logical partitions */
 		pt = (dos_partition_t *)(buffer + DOS_PART_TBL_OFFSET);
-		for (i = 0; i < 4; i++, pt++) {
+		for (i = 0; i < 4 && part_num <= MAX_EXT_PARTS; i++, pt++) {
 			/*
 			 * fdisk does not show the extended partitions that
 			 * are not in the MBR
@@ -302,7 +305,7 @@  static int part_get_info_extended(struct blk_desc *desc,
 
 		/* Print all primary/logical partitions */
 		pt = (dos_partition_t *)(buffer + DOS_PART_TBL_OFFSET);
-		for (i = 0; i < 4; i++, pt++) {
+		for (i = 0; i < 4 && part_num <= MAX_EXT_PARTS; i++, pt++) {
 			/*
 			 * fdisk does not show the extended partitions that
 			 * are not in the MBR
diff --git a/test/dm/part.c b/test/dm/part.c
index 4938d962144..89190d8bed6 100644
--- a/test/dm/part.c
+++ b/test/dm/part.c
@@ -423,3 +423,62 @@  static int dm_test_part_dos_ext_empty(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_part_dos_ext_empty, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
+
+/*
+ * As Linux, number at most 255 DOS partitions, so that the UUID made up for
+ * each from the disk signature and the partition number takes 11 characters
+ */
+static int dm_test_part_dos_uuid(struct unit_test_state *uts)
+{
+	struct disk_partition info;
+	dos_partition_t pt[4];
+	struct blk_desc *desc;
+	int part;
+
+	if (!CONFIG_IS_ENABLED(PARTITION_UUIDS))
+		return -EAGAIN;
+
+	ut_asserteq(2, blk_get_device_by_str("mmc", "2", &desc));
+	ut_asserteq(512, desc->blksz);
+
+	/* an extended partition from sector 2 */
+	memset(pt, '\0', sizeof(pt));
+	dos_set_entry(&pt[0], DOS_PART_TYPE_EXTENDED, 2, 512);
+	ut_assertok(dos_write_table(desc, 0, pt));
+
+	/*
+	 * holding logical partitions 5 to 256, each of one sector after its
+	 * table, with the tables two sectors apart from sector 2 on
+	 */
+	for (part = 5; part <= 256; part++) {
+		memset(pt, '\0', sizeof(pt));
+		dos_set_entry(&pt[0], 0x83, 1, 1);
+		if (part < 256)
+			dos_set_entry(&pt[1], DOS_PART_TYPE_EXTENDED,
+				      (part - 4) * 2, 2);
+		ut_assertok(dos_write_table(desc, 2 + (part - 5) * 2, pt));
+	}
+
+	ut_assertok(part_get_info_by_type(desc, 255, PART_TYPE_DOS, &info));
+	ut_asserteq(2 + 250 * 2 + 1, info.start);
+	ut_asserteq_str("00000000-ff", disk_partition_uuid(&info));
+	ut_assert_console_end();
+
+	/* there is no partition 256 */
+	ut_asserteq(-ENOENT,
+		    part_get_info_by_type(desc, 256, PART_TYPE_DOS, &info));
+	ut_assert_nextline("** Nested DOS partitions detected, stopping **");
+	ut_assert_console_end();
+
+	/* and listing them stops at partition 255 too */
+	part_init(desc);
+	ut_asserteq(PART_TYPE_DOS, desc->part_type);
+	part_print(desc);
+	ut_assert_skip_to_line("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x", 255,
+			       2 + 250 * 2 + 1, 1, 0, 255, 0x83);
+	ut_assert_nextline("** Nested DOS partitions detected, stopping **");
+	ut_assert_console_end();
+
+	return 0;
+}
+DM_TEST(dm_test_part_dos_uuid, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);