Skip to contentSkip to conversation

libfdisk: Fix randomly generated GPT UUID's

PATCH Applied Toomas Losin 2 participants 2 messages Mar 5, 2023 – Mar 9, 2023 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libfdisk: Fix randomly generated GPT UUID's

Fdisk commands that create random GPT UUID's result in values that are not UEFI-compliant being written to disk: The "g" command creates a new GPT whose in-core DiskGUID value is entirely big-endian; the "n" command creates a GPT partition whose in-core UniquePartitionGUID value is entirely big-endian. Those big-endian values are written to disk by the "w" command rather than the mix of little- and big-endian spec'd by UEFI.

This was caused by a libfdisk patch in 2017 that was addressing warnings about "taking address of packed member". Reading gpt.c finds two instances of dead code which suggests that perhaps there was some confusion between a struct and a pointer to a struct. The intent must have been to convert the randomly generated big-endian RFC 4122 UUID values to UEFI's mixed-endian but the confusion(?) resulted in some dead code and non-conversion of the UUID's.

This patch corrects the breakage while still avoiding "taking address of packed member" warnings. The "w" command will once again write UEFI-compliant values to disk.

Fixes 92e486f80ef8 ("libfdisk: fix guid usage of packed struct gpt_entry")
---
 libfdisk/src/gpt.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libfdisk/src/gpt.c +4 -4
index d7b3e1c70..c3c0347cb 100644
@@ -878,9 +878,9 @@ static int gpt_mknew_header(struct fdisk_context *cxt,
 	if (!has_id) {
 		struct gpt_guid guid;
 
-		uuid_generate_random((unsigned char *) &header->disk_guid);
-		guid = header->disk_guid;
+		uuid_generate_random((unsigned char *) &guid);
 		swap_efi_guid(&guid);
+		header->disk_guid = guid;
 	}
 	return 0;
 }
@@ -2621,9 +2621,9 @@ static int gpt_add_partition(
 		 */
 		struct gpt_guid guid;
 
-		uuid_generate_random((unsigned char *) &e->partition_guid);
-		guid = e->partition_guid;
+		uuid_generate_random((unsigned char *) &guid);
 		swap_efi_guid(&guid);
+		e->partition_guid = guid;
 	}
 
 	if (pa && pa->name && *pa->name)
Signature · 1 line
2.30.2
maintainer Applied
#1 lore raw
Quoted 2 lines · Toomas Losin
 libfdisk/src/gpt.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Applied, thanks!

Quoted 6 lines
libfdisk/src/gpt.c +0 -0
index d7b3e1c70..c3c0347cb 100644

libfdisk/src/gpt.c static int gpt_mknew_header(struct fdisk_context *cxt, · line 884

Quoted 9 lines
libfdisk/src/gpt.c +2 -2
@@ -878,9 +878,9 @@ static int gpt_mknew_header(struct fdisk_context *cxt,
 	if (!has_id) {
 		struct gpt_guid guid;
 
-		uuid_generate_random((unsigned char *) &header->disk_guid);
-		guid = header->disk_guid;
+		uuid_generate_random((unsigned char *) &guid);
 		swap_efi_guid(&guid);
+		header->disk_guid = guid;
 	}

Good catch, stupid bug.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com