]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Refuse GPT with invalid entry size
authorJan Janssen <medhefgo@web.de>
Mon, 29 Aug 2022 08:09:01 +0000 (10:09 +0200)
committerJan Janssen <medhefgo@web.de>
Wed, 7 Sep 2022 10:55:49 +0000 (12:55 +0200)
SizeOfPartitionEntry must be a multiple of 128 * 2^n.

src/boot/efi/xbootldr.c

index 2433816b4ef973ca5aa95a52476a0737c4ef270f..e5b9ca726848195f8b206475e447c29f263bd6fe 100644 (file)
@@ -67,7 +67,7 @@ static bool verify_gpt(union GptHeaderBuffer *gpt_header_buffer, EFI_LBA lba_exp
         if (h->MyLBA != lba_expected)
                 return false;
 
-        if (h->SizeOfPartitionEntry < sizeof(EFI_PARTITION_ENTRY))
+        if ((h->SizeOfPartitionEntry % sizeof(EFI_PARTITION_ENTRY)) != 0)
                 return false;
 
         if (h->NumberOfPartitionEntries <= 0 || h->NumberOfPartitionEntries > 1024)
@@ -130,19 +130,13 @@ static EFI_STATUS try_gpt(
 
         /* Now we can finally look for xbootloader partitions. */
         for (UINTN i = 0; i < gpt.gpt_header.NumberOfPartitionEntries; i++) {
-                EFI_PARTITION_ENTRY *entry;
-                EFI_LBA start, end;
-
-                entry = (EFI_PARTITION_ENTRY*) ((uint8_t*) entries + gpt.gpt_header.SizeOfPartitionEntry * i);
+                EFI_PARTITION_ENTRY *entry =
+                                (EFI_PARTITION_ENTRY *) ((uint8_t *) entries + gpt.gpt_header.SizeOfPartitionEntry * i);
 
                 if (memcmp(&entry->PartitionTypeGUID, XBOOTLDR_GUID, sizeof(entry->PartitionTypeGUID)) != 0)
                         continue;
 
-                /* Let's use memcpy(), in case the structs are not aligned (they really should be though) */
-                memcpy(&start, &entry->StartingLBA, sizeof(start));
-                memcpy(&end, &entry->EndingLBA, sizeof(end));
-
-                if (end < start) /* Bogus? */
+                if (entry->EndingLBA < entry->StartingLBA) /* Bogus? */
                         continue;
 
                 *ret_hd = (HARDDRIVE_DEVICE_PATH) {
@@ -151,8 +145,8 @@ static EFI_STATUS try_gpt(
                                 .SubType = MEDIA_HARDDRIVE_DP,
                         },
                         .PartitionNumber = i + 1,
-                        .PartitionStart = start,
-                        .PartitionSize = end - start + 1,
+                        .PartitionStart = entry->StartingLBA,
+                        .PartitionSize = entry->EndingLBA - entry->StartingLBA + 1,
                         .MBRType = MBR_TYPE_EFI_PARTITION_TABLE_HEADER,
                         .SignatureType = SIGNATURE_TYPE_GUID,
                 };