efi: Ignore unrealistically large option roms
PATCH v2 1/1 efi: Ignore unrealistically large option roms
setup_efi_pci() tries to save a copy of each PCI option ROM as this may be necessary for the device driver for the PCI device to have access too.
On some systems the efi_pci_io_protocol_64's romimage and romsize fields contain invalid data, which looks a bit like pointers pointing back into other EFI code or data. Interpreting these pointers as romsize leads to a very large value and if we then try to alloc this amount of memory to save a copy the alloc call fails.
This leads to a "Failed to alloc mem for rom" error being printed on the EFI console for each PCI device.
This commit avoids the printing of these errors, by checking romsize before doing the alloc and if it is larger then 256M silently ignore the ROM fields instead of trying to alloc mem and fail.
---
Changes in v2:
-Add the check to both __setup_efi_pci32 and __setup_efi_pci64 instead of
only to __setup_efi_pci64, some CHT devices which need this still use a 32 bit UEFI
--- arch/x86/boot/compressed/eboot.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/eboot.c +14 -2
@@ -122,7 +122,13 @@ __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)if (status !=EFI_SUCCESS )return status; -if (!pci->romimage || !pci->romsize) +/* +* Some firmwares contain EFI function pointers at the place where the +* romimage and romsize fields are supposed to be. Typically the EFI +* code is mapped at high addresses, translating to an unrealistically +* large romsize. We reject any roms over 256M in size to catch this. +*/ +if (!pci->romimage || !pci->romsize || pci->romsize >0x10000000 )return EFI_INVALID_PARAMETER ; size = pci->romsize +sizeof (*rom);
@@ -230,7 +236,13 @@ __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)if (status !=EFI_SUCCESS )return status; -if (!pci->romimage || !pci->romsize) +/* +* Some firmwares contain EFI function pointers at the place where the +* romimage and romsize fields are supposed to be. Typically the EFI +* code is mapped at high addresses, translating to an unrealistically +* large romsize. We reject any roms over 256M in size to catch this. +*/ +if (!pci->romimage || !pci->romsize || pci->romsize >0x10000000 )return EFI_INVALID_PARAMETER ; size = pci->romsize +sizeof (*rom);
Signature · 1 line
2.16.2
Hi Hans,
Quoted 9 lines · Hans de Goede
setup_efi_pci()tries to save a copy of each PCI option ROM as this may be necessary for the device driver for the PCI device to have access too.On some systems the efi_pci_io_protocol_64's romimage and romsize fields contain invalid data, which looks a bit like pointers pointing back into other EFI code or data. Interpreting these pointers as romsize leads to a very large value and if we then try to alloc this amount of memory to save a copy the alloc call fails.
Quoted 7 lines
This leads to a "Failed to alloc mem for rom" error being printed on the EFI console for each PCI device.
This commit avoids the printing of these errors, by checking romsize before doing the alloc and if it is larger then 256M silently ignore the ROM fields instead of trying to alloc mem and fail.
The UEFI spec limits the size of option ROMs to 16 MiB, so I'd prefer we use that as the upper bound instead.
With that changed,
or I can take it via the EFI tree if desired.
Signature · 1 line
Ard.
Quoted 47 lines
Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:(quoted diff) +0
-2-Add the check to both __setup_efi_pci32 and __setup_efi_pci64 instead of only to __setup_efi_pci64, some CHT devices which need this still use a 32 bit UEFI --- arch/x86/boot/compressed/eboot.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)arch/x86/boot/compressed/eboot.c +14
-3@@ -122,7 +122,13 @@ __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)if (status !=EFI_SUCCESS )return status; -if (!pci->romimage || !pci->romsize) +/* +* Some firmwares contain EFI function pointers at the place where the +* romimage and romsize fields are supposed to be. Typically the EFI +* code is mapped at high addresses, translating to an unrealistically +* large romsize. We reject any roms over 256M in size to catch this. +*/ +if (!pci->romimage || !pci->romsize || pci->romsize >0x10000000 )return EFI_INVALID_PARAMETER ; size = pci->romsize +sizeof (*rom);@@ -230,7 +236,13 @@ __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)if (status !=EFI_SUCCESS )return status; -if (!pci->romimage || !pci->romsize) +/* +* Some firmwares contain EFI function pointers at the place where the +* romimage and romsize fields are supposed to be. Typically the EFI +* code is mapped at high addresses, translating to an unrealistically +* large romsize. We reject any roms over 256M in size to catch this. +*/ +if (!pci->romimage || !pci->romsize || pci->romsize >0x10000000 )return EFI_INVALID_PARAMETER ; size = pci->romsize +sizeof (*rom); --2.16.2
Hi,
Quoted 20 lines · Ard Biesheuvel
Hi Hans,
Quoted 9 lines · Hans de Goede
setup_efi_pci()tries to save a copy of each PCI option ROM as this may be necessary for the device driver for the PCI device to have access too.On some systems the efi_pci_io_protocol_64's romimage and romsize fields contain invalid data, which looks a bit like pointers pointing back into other EFI code or data. Interpreting these pointers as romsize leads to a very large value and if we then try to alloc this amount of memory to save a copy the alloc call fails.
Quoted 7 lines
This leads to a "Failed to alloc mem for rom" error being printed on the EFI console for each PCI device.
This commit avoids the printing of these errors, by checking romsize before doing the alloc and if it is larger then 256M silently ignore the ROM fields instead of trying to alloc mem and fail.
Quoted 6 lines
The UEFI spec limits the size of option ROMs to 16 MiB, so I'd prefer we use that as the upper bound instead.
With that changed,
Reviewed-by Ard Biesheuvel <[email protected]>
Thanks for the review, fixed for v3 which I'm about to send.
Quoted 1 line
or I can take it via the EFI tree if desired.
I've no preference for how this goes upstream. x86 folks, please let us know if you will take this, or if you would prefer for this to go upstream through the EFI tree.
Regards,
Hans
From: Ard Biesheuvel
Quoted 14 lines
Sent: 28 April 2018 07:41
Quoted 12 lines · Hans de Goede
setup_efi_pci()tries to save a copy of each PCI option ROM as this may be necessary for the device driver for the PCI device to have access too.On some systems the efi_pci_io_protocol_64's romimage and romsize fields contain invalid data, which looks a bit like pointers pointing back into other EFI code or data. Interpreting these pointers as romsize leads to a very large value and if we then try to alloc this amount of memory to save a copy the alloc call fails.
This leads to a "Failed to alloc mem for rom" error being printed on the EFI console for each PCI device.
Quoted 7 lines
Quoted 4 lines
This commit avoids the printing of these errors, by checking romsize before doing the alloc and if it is larger then 256M silently ignore the ROM fields instead of trying to alloc mem and fail.
The UEFI spec limits the size of option ROMs to 16 MiB, so I'd prefer we use that as the upper bound instead.
Copying even 16MB of rom data into physical memory on the 'off chance' that the kernel might need it seems a waste of memory.
I can't help feeling that some kind of caching would be more appropriate.
David
Quoted 20 lines · David Laight
From: Ard Biesheuvel
Quoted 19 lines
Sent: 28 April 2018 07:41
Quoted 16 lines · Hans de Goede
setup_efi_pci()tries to save a copy of each PCI option ROM as this may be necessary for the device driver for the PCI device to have access too.On some systems the efi_pci_io_protocol_64's romimage and romsize fields contain invalid data, which looks a bit like pointers pointing back into other EFI code or data. Interpreting these pointers as romsize leads to a very large value and if we then try to alloc this amount of memory to save a copy the alloc call fails.
This leads to a "Failed to alloc mem for rom" error being printed on the EFI console for each PCI device.
This commit avoids the printing of these errors, by checking romsize before doing the alloc and if it is larger then 256M silently ignore the ROM fields instead of trying to alloc mem and fail.
Quoted 6 lines
Quoted 2 lines
The UEFI spec limits the size of option ROMs to 16 MiB, so I'd prefer we use that as the upper bound instead.
Copying even 16MB of rom data into physical memory on the 'off chance' that the kernel might need it seems a waste of memory.
16 MB is the smallest number we can use that still guarantees that we will not misidentify a valid option ROM as bogus. But in reality, option ROMs are rarely that large.
Quoted 2 lines
I can't help feeling that some kind of caching would be more appropriate.
Could you elaborate?