Skip to contentSkip to conversation

x86/EFI: don't let the EDD disk counter wrap

PATCH New Anandu Padinhare 1 participant 1 message Dec 4

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/EFI: don't let the EDD disk counter wrap

efi_arch_edd() increments the u8 boot_edd_info_nr once per candidate disk, without bound. Disks past EDD_INFO_MAX are meant to be skipped by the "boot_edd_info_nr > EDD_INFO_MAX" checks in the device path handling, but with 256 or more disks the counter wraps to zero. For the 256th disk "info" and "params" then point at boot_edd_info[255], about 20KiB past the end of the array, while the checks pass, so its details are written there. With xen.efi that is currently the alternatives table, and Xen panics with "Unable to apply alternatives". Later disks overwrite the first entries. "edd=" can't avoid this, as it is ignored when booting via EFI.

Count in a local variable instead, and store the result, capped at EDD_INFO_MAX, into boot_edd_info_nr at the end.

Fixes 51f03ec421fb ("x86-64/EFI: construct EDD data from device path protocol information")

---
Changes in v2:
- Use a local counter, as suggested by Jan. Output is now unchanged for up to 255 disks, including the MBR signature device numbers.

Reproduced in QEMU/OVMF with 256 and 260 whole disks (NVMe namespaces):
xen.efi panics; via GRUB/multiboot2 Xen boots, but Dom0 gets wrong EDD entries. With this patch both boot with the first 6 disks, and with 5, 15 and 255 disks Dom0's EDD data is unchanged.

 xen/arch/x86/efi/efi-boot.h | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
xen/arch/x86/efi/efi-boot.h +10 -11
index 8bca03a66b..a38dccc910 100644
@@ -360,7 +360,7 @@ static void __init efi_arch_edd(void)
     static EFI_GUID __initdata bio_guid = BLOCK_IO_PROTOCOL;
     static EFI_GUID __initdata devp_guid = DEVICE_PATH_PROTOCOL;
     EFI_HANDLE *handles = NULL;
-    unsigned int i;
+    unsigned int i, nr = 0;
     UINTN size;
     EFI_STATUS status;
 
@@ -380,7 +380,7 @@ static void __init efi_arch_edd(void)
     {
         EFI_BLOCK_IO *bio;
         EFI_DEV_PATH_PTR devp;
-        struct edd_info *info = boot_edd_info + boot_edd_info_nr;
+        struct edd_info *info = boot_edd_info + nr;
         struct edd_device_params *params = &info->edd_device_params;
         enum { root, acpi, pci, ctrlr } state = root;
 
@@ -389,16 +389,16 @@ static void __init efi_arch_edd(void)
              bio->Media->RemovableMedia ||
              bio->Media->LogicalPartition )
             continue;
-        if ( boot_edd_info_nr < EDD_INFO_MAX )
+        if ( nr < EDD_INFO_MAX )
         {
-            info->device = 0x80 + boot_edd_info_nr; /* fake */
+            info->device = 0x80 + nr; /* fake */
             info->version = 0x11;
             params->length = offsetof(struct edd_device_params, dpte_ptr);
             params->number_of_sectors = bio->Media->LastBlock + 1;
             params->bytes_per_sector = bio->Media->BlockSize;
             params->dpte_ptr = ~0;
         }
-        ++boot_edd_info_nr;
+        ++nr;
         status = efi_bs->HandleProtocol(handles[i], &devp_guid,
                                         (void **)&devp);
         if ( EFI_ERROR(status) )
@@ -411,7 +411,7 @@ static void __init efi_arch_edd(void)
                 const u8 *p;
 
             case ACPI_DEVICE_PATH:
-                if ( state != root || boot_edd_info_nr > EDD_INFO_MAX )
+                if ( state != root || nr > EDD_INFO_MAX )
                     break;
                 switch ( DevicePathSubType(devp.DevPath) )
                 {
@@ -430,7 +430,7 @@ static void __init efi_arch_edd(void)
             case HARDWARE_DEVICE_PATH:
                 if ( state != acpi ||
                      DevicePathSubType(devp.DevPath) != HW_PCI_DP ||
-                     boot_edd_info_nr > EDD_INFO_MAX )
+                     nr > EDD_INFO_MAX )
                     break;
                 state = pci;
                 edd_put_string(params->host_bus_type, "PCI");
@@ -438,7 +438,7 @@ static void __init efi_arch_edd(void)
                 params->interface_path.pci.function = devp.Pci->Function;
                 break;
             case MESSAGING_DEVICE_PATH:
-                if ( state != pci || boot_edd_info_nr > EDD_INFO_MAX )
+                if ( state != pci || nr > EDD_INFO_MAX )
                     break;
                 state = ctrlr;
                 switch ( DevicePathSubType(devp.DevPath) )
@@ -492,7 +492,7 @@ static void __init efi_arch_edd(void)
                     struct mbr_signature *sig = boot_mbr_signature +
                                                 boot_mbr_signature_nr;
 
-                    sig->device = 0x80 + boot_edd_info_nr; /* fake */
+                    sig->device = 0x80 + nr; /* fake */
                     memcpy(&sig->signature, devp.HardDrive->Signature,
                            sizeof(sig->signature));
                     ++boot_mbr_signature_nr;
@@ -503,8 +503,7 @@ static void __init efi_arch_edd(void)
     }
     if ( handles )
         efi_bs->FreePool(handles);
-    if ( boot_edd_info_nr > EDD_INFO_MAX )
-        boot_edd_info_nr = EDD_INFO_MAX;
+    boot_edd_info_nr = min_t(unsigned int, nr, EDD_INFO_MAX);
 }
 
 static void __init efi_arch_console_init(UINTN cols, UINTN rows)

base-commit: 77ff75d3244faa2bc083c0ba8686830f8de30d9e

Signature · 1 line
2.53.0