Skip to contentSkip to conversation

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

PATCH Superseded Anandu Padinhare 2 participants 2 messages Dec 3

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

Saturate the counter at EDD_INFO_MAX + 1 instead.

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

---
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.

The only other use of the counter in the loop is the fake device number of MBR signatures, now 0x87 rather than 0x88 and up for the 8th and later disks. Neither matches an EDD entry, and Linux ignores it. If the old values should be kept, a separate disk counter can do that.

 xen/arch/x86/efi/efi-boot.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
xen/arch/x86/efi/efi-boot.h +3 -1
index 8bca03a66b..b255b01fa3 100644
@@ -398,7 +398,9 @@ static void __init efi_arch_edd(void)
             params->bytes_per_sector = bio->Media->BlockSize;
             params->dpte_ptr = ~0;
         }
-        ++boot_edd_info_nr;
+        /* Don't wrap, or the checks below would stop skipping extra disks. */
+        if ( boot_edd_info_nr <= EDD_INFO_MAX )
+            ++boot_edd_info_nr;
         status = efi_bs->HandleProtocol(handles[i], &devp_guid,
                                         (void **)&devp);
         if ( EFI_ERROR(status) )

base-commit: 77ff75d3244faa2bc083c0ba8686830f8de30d9e

Signature · 1 line
2.53.0
#1 lore raw
Quoted 21 lines · Anandu Padinhare

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.

Saturate the counter at EDD_INFO_MAX + 1 instead.

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

---
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.

Quoted 4 lines

The only other use of the counter in the loop is the fake device number of MBR signatures, now 0x87 rather than 0x88 and up for the 8th and later disks. Neither matches an EDD entry, and Linux ignores it. If the old values should be kept, a separate disk counter can do that.

Why would that be? You can use the present counter, by ...

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_edd(void) · line 403

Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +3 -1
@@ -398,7 +398,9 @@ static void __init efi_arch_edd(void)
             params->bytes_per_sector = bio->Media->BlockSize;
             params->dpte_ptr = ~0;
         }
-        ++boot_edd_info_nr;
+        /* Don't wrap, or the checks below would stop skipping extra disks. */
+        if ( boot_edd_info_nr <= EDD_INFO_MAX )
+            ++boot_edd_info_nr;

... doing this differently. One possibility would be

        if ( !++boot_edd_info_nr )
            --boot_edd_info_nr;

That said, overall it may indeed be better to introduce a local variable, and only store into boot_edd_info_nr at the end of the function. That'll likely yield better generated code (even if in an __init function this may not matter all this much).

Jan