efi/x86: Avoid corrupted config tables under Xen
Subsystems (3)
Ard Biesheuvel
maintainercover efi/x86: Avoid corrupted config tables under Xen
This is an alternate approach to addressing the issue that Demi Marie is attempting to fix in [0] (i.e., ESRT config table exposed to a x86 dom0 is corrupted because it resides in boot services memory as per the EFI spec, where it gets corrupted by Xen). My main objection to that approach is that it needs Xen-specific fixes in multiple different places, but we still end up only fixing the ESRT case specifically.
So instead, I am proposing this series as a more generic way to handle configuration tables that reside in boot services memory, and confining the Xen specific logic to the Xen EFI glue code.
Given that EFI boot without a memory map is only permitted on x86 and only when doing Xen boot, let's clear up some inconsistencies there first so we can set the EFI_PARAVIRT flag on all architectures that do pseudo-EFI boot straight into the core kernel (i.e., without going through the stub). This moves a good chunk of EFI memory map manipulation code into the x86 arch tree, where it arguably belongs as no other architectures rely on it. This is implemented in patches 1 - 3.
Patch #4 updates the configuration table iteration logic so that only ACPI and SMBIOS tables are exposed automatically when EFI_PARAVIRT is set, and other config tables only if they reside in a memory region of a EFI memory type that is guaranteed to be preserved. This effectively hides the ESRT, but also the memory attributes table and the runtime properties (and potentially others) when doing Xen dom0 boot unless they have been moved out of EFI boot services memory.
The final patch relaxes the ESRT sanity check so that the ESRT is parsed and exposed even if EFI_MEMMAP is not set, which is the case with Xen dom0 on x86. If additional memory map checks are required in this code path, the best way to achieve this is for Xen to expose a EFI memory map on x86 just like it does on other architectures that support Xen (ARM and arm64)
[0] https://lore.kernel.org/all/[email protected]/
Ard Biesheuvel (5):
efi: Move EFI fake memmap support into x86 arch tree
efi: memmap: Move manipulation routines into x86 arch tree
efi: xen: Set EFI_PARAVIRT for Xen dom0 boot on all architectures
efi: Apply allowlist to EFI configuration tables when running under
Xen
efi: esrt: Omit region sanity check when no memory map is available
arch/x86/Kconfig | 20 ++
arch/x86/include/asm/efi.h | 16 ++
arch/x86/kernel/setup.c | 1 +
arch/x86/platform/efi/Makefile | 4 +-
arch/x86/platform/efi/efi.c | 8 +-
{drivers/firmware => arch/x86/platform}/efi/fake_mem.c | 79 ++++++-
arch/x86/platform/efi/memmap.c | 238 ++++++++++++++++++++
arch/x86/platform/efi/quirks.c | 3 +
drivers/firmware/efi/Kconfig | 22 --
drivers/firmware/efi/Makefile | 4 -
drivers/firmware/efi/efi.c | 7 +
drivers/firmware/efi/esrt.c | 61 ++---
drivers/firmware/efi/fake_mem.h | 10 -
drivers/firmware/efi/fdtparams.c | 4 +
drivers/firmware/efi/memmap.c | 224 +-----------------
drivers/firmware/efi/x86_fake_mem.c | 75 ------
drivers/xen/efi.c | 69 ++++++
include/linux/efi.h | 18 +-
18 files changed, 481 insertions(+), 382 deletions(-)
rename {drivers/firmware => arch/x86/platform}/efi/fake_mem.c (58%)
create mode 100644 arch/x86/platform/efi/memmap.c
delete mode 100644 drivers/firmware/efi/fake_mem.h
delete mode 100644 drivers/firmware/efi/x86_fake_mem.cSignature · 1 line
2.35.1
Ard Biesheuvel
maintainerRFC PATCH 1/5 efi: Move EFI fake memmap support into x86 arch tree
The EFI fake memmap support is specific to x86, which manipulates the EFI memory map in various different ways after receiving it from the EFI stub. On other architectures, we have manages to push back on this, and the EFI memory map is kept pristine.
So let's move the fake memmap code into the x86 arch tree, where it arguably belongs.
---
arch/x86/Kconfig | 20 +++++
arch/x86/include/asm/efi.h | 5 ++
arch/x86/kernel/setup.c | 1 +
arch/x86/platform/efi/Makefile | 1 +
{drivers/firmware => arch/x86/platform}/efi/fake_mem.c | 79 +++++++++++++++++++-
drivers/firmware/efi/Kconfig | 22 ------
drivers/firmware/efi/Makefile | 4 -
drivers/firmware/efi/fake_mem.h | 10 ---
drivers/firmware/efi/x86_fake_mem.c | 75 -------------------
include/linux/efi.h | 6 --
10 files changed, 103 insertions(+), 120 deletions(-)arch/x86/Kconfig +20 -0
@@ -1978,6 +1978,26 @@ config EFI_MIXED If unsure, say N. +config EFI_FAKE_MEMMAP + bool "Enable EFI fake memory map" + depends on EFI + help + Saying Y here will enable "efi_fake_mem" boot option. By specifying + this parameter, you can add arbitrary attribute to specific memory + range by updating original (firmware provided) EFI memmap. This is + useful for debugging of EFI memmap related feature, e.g., Address + Range Mirroring feature. + +config EFI_MAX_FAKE_MEM + int "maximum allowable number of ranges in efi_fake_mem boot option" + depends on EFI_FAKE_MEMMAP + range 1 128 + default 8 + help + Maximum allowable number of ranges in efi_fake_mem boot option. + Ranges can be set up to this value using comma-separated list. + The default value is 8. + source "kernel/Kconfig.hz" config KEXEC
arch/x86/include/asm/efi.h +5 -0
@@ -404,10 +404,15 @@ static inline void efi_reserve_boot_services(void)#ifdef CONFIG_EFI_FAKE_MEMMAP extern void __init efi_fake_memmap_early (void ); +extern void __init efi_fake_memmap (void );#else static inline void efi_fake_memmap_early (void ) { } + +static inline void efi_fake_memmap (void ) +{ +}#endif #define arch_ima_efi_boot_mode \
arch/x86/kernel/setup.c +1 -0
@@ -31,6 +31,7 @@#include <xen/xen.h> #include <asm/apic.h> +#include <asm/efi.h> #include <asm/numa.h> #include <asm/bios_ebda.h> #include <asm/bugs.h>
arch/x86/platform/efi/Makefile +1 -0
@@ -4,3 +4,4 @@ GCOV_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o +obj-$(CONFIG_EFI_FAKE_MEMMAP) += fake_mem.o
arch/x86/platform/efi/fake_mem.c renamed from drivers/firmware/efi/fake_mem.c +76 -3
89 lines not on this page · raw message
drivers/firmware/efi/Kconfig +0 -22
@@ -37,28 +37,6 @@ config EFI_RUNTIME_MAP See also Documentation/ABI/testing/sysfs-firmware-efi-runtime-map. -config EFI_FAKE_MEMMAP - bool "Enable EFI fake memory map" - depends on EFI && X86 - default n - help - Saying Y here will enable "efi_fake_mem" boot option. - By specifying this parameter, you can add arbitrary attribute - to specific memory range by updating original (firmware provided) - EFI memmap. - This is useful for debugging of EFI memmap related feature. - e.g. Address Range Mirroring feature. - -config EFI_MAX_FAKE_MEM - int "maximum allowable number of ranges in efi_fake_mem boot option" - depends on EFI_FAKE_MEMMAP - range 1 128 - default 8 - help - Maximum allowable number of ranges in efi_fake_mem boot option. - Ranges can be set up to this value using comma-separated list. - The default value is 8. - config EFI_SOFT_RESERVE bool "Reserve EFI Specific Purpose Memory" depends on EFI && EFI_STUB && ACPI_HMAT
drivers/firmware/efi/Makefile +0 -4
@@ -23,7 +23,6 @@ obj-$(CONFIG_UEFI_CPER) += cper.o obj-$(CONFIG_EFI_RUNTIME_MAP) += runtime-map.o obj-$(CONFIG_EFI_RUNTIME_WRAPPERS) += runtime-wrappers.o subdir-$(CONFIG_EFI_STUB) += libstub -obj-$(CONFIG_EFI_FAKE_MEMMAP) += fake_map.o obj-$(CONFIG_EFI_BOOTLOADER_CONTROL) += efibc.o obj-$(CONFIG_EFI_TEST) += test/ obj-$(CONFIG_EFI_DEV_PATH_PARSER) += dev-path-parser.o
@@ -32,9 +31,6 @@ obj-$(CONFIG_EFI_RCI2_TABLE) += rci2-table.o obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += embedded-firmware.o obj-$(CONFIG_LOAD_UEFI_KEYS) += mokvar-table.o -fake_map-y += fake_mem.o -fake_map-$(CONFIG_X86) += x86_fake_mem.o - obj-$(CONFIG_SYSFB) += sysfb_efi.o arm-obj-$(CONFIG_EFI) := efi-init.o arm-runtime.o
drivers/firmware/efi/fake_mem.h deleted +0 -10
@@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __EFI_FAKE_MEM_H__ -#define __EFI_FAKE_MEM_H__ -#include <asm/efi.h> - -#define EFI_MAX_FAKEMEM CONFIG_EFI_MAX_FAKE_MEM - -extern struct efi_mem_range efi_fake_mems[EFI_MAX_FAKEMEM ]; -extern int nr_fake_mem; -#endif /* __EFI_FAKE_MEM_H__ */
drivers/firmware/efi/x86_fake_mem.c deleted +0 -75
75 lines not on this page · raw message
include/linux/efi.h +0 -6
@@ -749,12 +749,6 @@ extern struct kobject *efi_kobj;extern int efi_reboot_quirk_mode;extern bool efi_poweroff_required (void ); -#ifdef CONFIG_EFI_FAKE_MEMMAP -extern void __init efi_fake_memmap (void ); -#else -static inline void efi_fake_memmap (void ) { } -#endif -extern unsigned long efi_mem_attr_table;/*
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerRFC PATCH 2/5 efi: memmap: Move manipulation routines into x86 arch tree
In reply to Ard BiesheuvelThe EFI memory map is a description of the memory layout as provided by the firmware, and only x86 manipulates it in various different ways for its own memory bookkeeping. So let's move the memmap routines that are only used by x86 into the x86 arch tree.
--- arch/x86/include/asm/efi.h | 11 + arch/x86/platform/efi/Makefile | 3 +- arch/x86/platform/efi/memmap.c | 235 ++++++++++++++++++++ drivers/firmware/efi/memmap.c | 221 +----------------- include/linux/efi.h | 10 +- 5 files changed, 251 insertions(+), 229 deletions(-)
arch/x86/include/asm/efi.h +11 -0
@@ -415,6 +415,17 @@ static inline void efi_fake_memmap(void) }#endif +extern int __init efi_memmap_alloc (unsigned int num_entries, +struct efi_memory_map_data *data); +extern void __efi_memmap_free (u64 phys,unsigned long size, +unsigned long flags); + +extern int __init efi_memmap_install (struct efi_memory_map_data *data); +extern int __init efi_memmap_split_count (efi_memory_desc_t *md, +struct range *range); +extern void __init efi_memmap_insert (struct efi_memory_map *old_memmap, +void *buf,struct efi_mem_range *mem); +#define arch_ima_efi_boot_mode \ ({ extern struct boot_params boot_params; boot_params.secure_boot; })
arch/x86/platform/efi/Makefile +2 -1
@@ -2,6 +2,7 @@ KASAN_SANITIZE := n GCOV_PROFILE := n -obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o +obj-$(CONFIG_EFI) += memmap.o quirks.o efi.o efi_$(BITS).o \ + efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o obj-$(CONFIG_EFI_FAKE_MEMMAP) += fake_mem.o
arch/x86/platform/efi/memmap.c new file +235 -0
235 lines not on this page · raw message
drivers/firmware/efi/memmap.c +2 -219
243 lines not on this page · raw message
include/linux/efi.h +1 -9
@@ -707,18 +707,10 @@ static inline efi_status_t efi_query_variable_store(u32 attributes,#endif extern void __iomem *efi_lookup_mapped_addr (u64 phys_addr); -extern int __init efi_memmap_alloc (unsigned int num_entries, -struct efi_memory_map_data *data); -extern void __efi_memmap_free (u64 phys,unsigned long size, -unsigned long flags); +extern int __init __efi_memmap_init (struct efi_memory_map_data *data);extern int __init efi_memmap_init_early (struct efi_memory_map_data *data);extern int __init efi_memmap_init_late (phys_addr_t addr,unsigned long size);extern void __init efi_memmap_unmap (void ); -extern int __init efi_memmap_install (struct efi_memory_map_data *data); -extern int __init efi_memmap_split_count (efi_memory_desc_t *md, -struct range *range); -extern void __init efi_memmap_insert (struct efi_memory_map *old_memmap, -void *buf,struct efi_mem_range *mem);#ifdef CONFIG_EFI_ESRT extern void __init efi_esrt_init (void );
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerRFC PATCH 3/5 efi: xen: Set EFI_PARAVIRT for Xen dom0 boot on all architectures
In reply to Ard BiesheuvelCurrently, the EFI_PARAVIRT flag is only used by x86, even though other architectures also support pseudo-EFI boot, where the core kernel is invoked directly and provided with a set of data tables that resemble the ones constructed by the EFI stub, which never actually runs in that case.
Let's fix this inconsistency, and always set this flag when booting dom0 via the EFI boot path. Note that Xen on x86 does not provide the EFI memory map in this case, whereas other architectures do, so move the associated EFI_PARAVIRT check into the x86 platform code.
--- arch/x86/platform/efi/efi.c | 8 +++++--- arch/x86/platform/efi/memmap.c | 3 +++ drivers/firmware/efi/fdtparams.c | 4 ++++ drivers/firmware/efi/memmap.c | 3 --- 4 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/platform/efi/efi.c +5 -3
@@ -214,9 +214,11 @@ int __init efi_memblock_x86_reserve_range(void) data.desc_size = e->efi_memdesc_size; data.desc_version = e->efi_memdesc_version; - rv =efi_memmap_init_early (&data); -if (rv) -return rv; +if (!efi_enabled (EFI_PARAVIRT )) { + rv =efi_memmap_init_early (&data); +if (rv) +return rv; + }if (add_efi_memmap ||do_efi_soft_reserve ())do_add_efi_memmap ();
arch/x86/platform/efi/memmap.c +3 -0
@@ -93,6 +93,9 @@ int __init efi_memmap_install(struct efi_memory_map_data *data) {efi_memmap_unmap (); +if (efi_enabled (EFI_PARAVIRT )) +return 0 ; +return __efi_memmap_init (data); }
drivers/firmware/efi/fdtparams.c +4 -0
@@ -30,11 +30,13 @@ static __initconst const char name[][22] = {static __initconst const struct {const char path[17 ]; +u8 paravirt;const char params[PARAMCOUNT ][26 ]; } dt_params[] = { { #ifdef CONFIG_XEN // <-------17------> .path ="/hypervisor/uefi" , + .paravirt =1 , .params = { [SYSTAB ] ="xen,uefi-system-table" , [MMBASE ] ="xen,uefi-mmap-start" ,
@@ -121,6 +123,8 @@ u64 __init efi_get_fdt_params(struct efi_memory_map_data *mm)pr_err ("Can't find property '%s' in DT!\n" , pname);return 0 ; } +if (dt_params[i].paravirt) +set_bit (EFI_PARAVIRT , &efi.flags);return systab; } notfound:
drivers/firmware/efi/memmap.c +0 -3
@@ -44,9 +44,6 @@ int __init __efi_memmap_init(struct efi_memory_map_data *data)struct efi_memory_map map;phys_addr_t phys_map; -if (efi_enabled (EFI_PARAVIRT )) -return 0 ; - phys_map = data->phys_map;if (data->flags &EFI_MEMMAP_LATE )
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerRFC PATCH 4/5 efi: Apply allowlist to EFI configuration tables when running under Xen
In reply to Ard BiesheuvelAs it turns out, Xen does not guarantee that EFI bootservices data regions in memory are preserved, which means that EFI configuration tables pointing into such memory regions may be corrupted before the dom0 OS has had a chance to inspect them.
Demi Marie reports that this is causing problems for Qubes OS when it attempts to perform system firmware updates, which requires that the contents of the ESRT configuration table are valid when the fwupd user space program runs.
However, other configuration tables such as the memory attributes table or the runtime properties table are equally affected, and so we need a comprehensive workaround that works for any table type.
So let's first disregard all EFI configuration tables except the ones that are known (or can be expected) to reside in memory regions of a type that Xen preserves, i.e., ACPI tables (which are passed in EfiAcpiReclaimMemory regions) and SMBIOS tables (which are usually passed in EfiRuntimeServicesData regions, even though the UEFI spec only mentions this as a recommendation). Then, cross reference unknown tables against either the EFI memory map (if available) or do a Xen hypercall to determine the memory type, and allow the config table if the type is one that is guaranteed to be preserved.
Future patches can augment the logic in this routine to allow other table types based on the size of the allocation, or based on a table specific header size field.
--- drivers/firmware/efi/efi.c | 7 ++ drivers/xen/efi.c | 69 ++++++++++++++++++++ include/linux/efi.h | 2 + 3 files changed, 78 insertions(+)
drivers/firmware/efi/efi.c +7 -0
@@ -556,6 +556,13 @@ static __init int match_config_table(const efi_guid_t *guid,for (i =0 ;efi_guidcmp (table_types[i].guid,NULL_GUID ); i++) {if (!efi_guidcmp (*guid, table_types[i].guid)) { +if (IS_ENABLED (CONFIG_XEN_EFI ) && + !xen_efi_config_table_is_usable (guid, table)) { +if (table_types[i].name[0 ]) +pr_cont ("(%s=0x%lx) " , + table_types[i].name, table); +return 1 ; + } *(table_types[i].ptr) = table;if (table_types[i].name[0 ]) pr_cont("%s=0x%lx ",
drivers/xen/efi.c +69 -0
72 lines not on this page · raw message
include/linux/efi.h +2 -0
@@ -1351,4 +1351,6 @@ struct linux_efi_initrd {/* Header of a populated EFI secret area */ #define EFI_SECRET_TABLE_HEADER_GUID EFI_GUID(0x1e74f542, 0x71dd, 0x4d66, 0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b) +bool xen_efi_config_table_is_usable (const efi_guid_t *,unsigned long table); +#endif /* _LINUX_EFI_H */
Signature · 1 line
2.35.1
Re: [RFC PATCH 4/5] efi: Apply allowlist to EFI configuration tables when running under Xen
Quoted 60 lines · Ard Biesheuvel
As it turns out, Xen does not guarantee that EFI bootservices data regions in memory are preserved, which means that EFI configuration tables pointing into such memory regions may be corrupted before the dom0 OS has had a chance to inspect them.
Demi Marie reports that this is causing problems for Qubes OS when it attempts to perform system firmware updates, which requires that the contents of the ESRT configuration table are valid when the fwupd user space program runs.
However, other configuration tables such as the memory attributes table or the runtime properties table are equally affected, and so we need a comprehensive workaround that works for any table type.
So let's first disregard all EFI configuration tables except the ones that are known (or can be expected) to reside in memory regions of a type that Xen preserves, i.e., ACPI tables (which are passed in EfiAcpiReclaimMemory regions) and SMBIOS tables (which are usually passed in EfiRuntimeServicesData regions, even though the UEFI spec only mentions this as a recommendation). Then, cross reference unknown tables against either the EFI memory map (if available) or do a Xen hypercall to determine the memory type, and allow the config table if the type is one that is guaranteed to be preserved.
Future patches can augment the logic in this routine to allow other table types based on the size of the allocation, or based on a table specific header size field.
Co-developed-by Demi Marie Obenour <[email protected]>Signed-off-by Demi Marie Obenour <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/efi.c | 7 ++ drivers/xen/efi.c | 69 ++++++++++++++++++++ include/linux/efi.h | 2 + 3 files changed, 78 insertions(+)drivers/firmware/efi/efi.c +7
-0@@ -556,6 +556,13 @@ static __init int match_config_table(const efi_guid_t *guid,for (i =0 ;efi_guidcmp (table_types[i].guid,NULL_GUID ); i++) {if (!efi_guidcmp (*guid, table_types[i].guid)) { +if (IS_ENABLED (CONFIG_XEN_EFI ) && + !xen_efi_config_table_is_usable (guid, table)) { +if (table_types[i].name[0 ]) +pr_cont ("(%s=0x%lx) " , + table_types[i].name, table); +return 1 ; + } *(table_types[i].ptr) = table;if (table_types[i].name[0 ]) pr_cont("%s=0x%lx ",drivers/xen/efi.c +0
-0
drivers/xen/efi.c void __init xen_efi_runtime_setup(void) · line 301
Quoted 10 lines
drivers/xen/efi.c +7
-0@@ -292,3 +292,72 @@ void __init xen_efi_runtime_setup(void) efi.get_next_high_mono_count = xen_efi_get_next_high_mono_count; efi.reset_system = xen_efi_reset_system; } + +static const efi_guid_t cfg_table_allow_list[]__initconst = { +ACPI_20_TABLE_GUID , +ACPI_TABLE_GUID , +SMBIOS_TABLE_GUID , +SMBIOS3_TABLE_GUID , +};
This allowlist seems redundant. Either the tables are already in memory that Xen will preserve or they aren’t. In both cases the subsequent code will do the right thing.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Ard Biesheuvel
maintainerRe: [RFC PATCH 4/5] efi: Apply allowlist to EFI configuration tables when running under Xen
Quoted 68 lines · Demi Marie Obenour
Quoted 66 lines · Ard Biesheuvel
As it turns out, Xen does not guarantee that EFI bootservices data regions in memory are preserved, which means that EFI configuration tables pointing into such memory regions may be corrupted before the dom0 OS has had a chance to inspect them.
Demi Marie reports that this is causing problems for Qubes OS when it attempts to perform system firmware updates, which requires that the contents of the ESRT configuration table are valid when the fwupd user space program runs.
However, other configuration tables such as the memory attributes table or the runtime properties table are equally affected, and so we need a comprehensive workaround that works for any table type.
So let's first disregard all EFI configuration tables except the ones that are known (or can be expected) to reside in memory regions of a type that Xen preserves, i.e., ACPI tables (which are passed in EfiAcpiReclaimMemory regions) and SMBIOS tables (which are usually passed in EfiRuntimeServicesData regions, even though the UEFI spec only mentions this as a recommendation). Then, cross reference unknown tables against either the EFI memory map (if available) or do a Xen hypercall to determine the memory type, and allow the config table if the type is one that is guaranteed to be preserved.
Future patches can augment the logic in this routine to allow other table types based on the size of the allocation, or based on a table specific header size field.
Co-developed-by Demi Marie Obenour <[email protected]>Signed-off-by Demi Marie Obenour <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/efi.c | 7 ++ drivers/xen/efi.c | 69 ++++++++++++++++++++ include/linux/efi.h | 2 + 3 files changed, 78 insertions(+)drivers/firmware/efi/efi.c +7
-0@@ -556,6 +556,13 @@ static __init int match_config_table(const efi_guid_t *guid,for (i =0 ;efi_guidcmp (table_types[i].guid,NULL_GUID ); i++) {if (!efi_guidcmp (*guid, table_types[i].guid)) { +if (IS_ENABLED (CONFIG_XEN_EFI ) && + !xen_efi_config_table_is_usable (guid, table)) { +if (table_types[i].name[0 ]) +pr_cont ("(%s=0x%lx) " , + table_types[i].name, table); +return 1 ; + } *(table_types[i].ptr) = table;if (table_types[i].name[0 ]) pr_cont("%s=0x%lx ",drivers/xen/efi.c +3
-0@@ -292,3 +292,72 @@ void __init xen_efi_runtime_setup(void) efi.get_next_high_mono_count = xen_efi_get_next_high_mono_count; efi.reset_system = xen_efi_reset_system; } + +static const efi_guid_t cfg_table_allow_list[]__initconst = { +ACPI_20_TABLE_GUID ,
Quoted 8 lines
drivers/xen/efi.c void __init xen_efi_runtime_setup(void) · line 301
Quoted 4 lines
drivers/xen/efi.c +4
-0+ACPI_TABLE_GUID , +SMBIOS_TABLE_GUID , + SMBIOS3_TABLE_GUID, +};This allowlist seems redundant. Either the tables are already in memory that Xen will preserve or they aren’t. In both cases the subsequent code will do the right thing.
Will it? Currently, Xen simply accepts all ACPI and SMBIOS tables, regardless of what type of memory region they reside in (if any).
So what will happen with buggy firmware where the ACPI or SMBIOS tables are not covered by the memory map at all? Currently, this works fine but now, it will be rejected. And without ACPI tables, the boot will not get far enough to even inform the user what is wrong. And SMBIOS tables are used for platform quirks, which means they might be essential for a platform to boot as well.
Re: [RFC PATCH 4/5] efi: Apply allowlist to EFI configuration tables when running under Xen
Quoted 82 lines · Ard Biesheuvel
Quoted 68 lines · Demi Marie Obenour
Quoted 66 lines · Ard Biesheuvel
As it turns out, Xen does not guarantee that EFI bootservices data regions in memory are preserved, which means that EFI configuration tables pointing into such memory regions may be corrupted before the dom0 OS has had a chance to inspect them.
Demi Marie reports that this is causing problems for Qubes OS when it attempts to perform system firmware updates, which requires that the contents of the ESRT configuration table are valid when the fwupd user space program runs.
However, other configuration tables such as the memory attributes table or the runtime properties table are equally affected, and so we need a comprehensive workaround that works for any table type.
So let's first disregard all EFI configuration tables except the ones that are known (or can be expected) to reside in memory regions of a type that Xen preserves, i.e., ACPI tables (which are passed in EfiAcpiReclaimMemory regions) and SMBIOS tables (which are usually passed in EfiRuntimeServicesData regions, even though the UEFI spec only mentions this as a recommendation). Then, cross reference unknown tables against either the EFI memory map (if available) or do a Xen hypercall to determine the memory type, and allow the config table if the type is one that is guaranteed to be preserved.
Future patches can augment the logic in this routine to allow other table types based on the size of the allocation, or based on a table specific header size field.
Co-developed-by Demi Marie Obenour <[email protected]>Signed-off-by Demi Marie Obenour <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/efi.c | 7 ++ drivers/xen/efi.c | 69 ++++++++++++++++++++ include/linux/efi.h | 2 + 3 files changed, 78 insertions(+)drivers/firmware/efi/efi.c +7
-0@@ -556,6 +556,13 @@ static __init int match_config_table(const efi_guid_t *guid,for (i =0 ;efi_guidcmp (table_types[i].guid,NULL_GUID ); i++) {if (!efi_guidcmp (*guid, table_types[i].guid)) { +if (IS_ENABLED (CONFIG_XEN_EFI ) && + !xen_efi_config_table_is_usable (guid, table)) { +if (table_types[i].name[0 ]) +pr_cont ("(%s=0x%lx) " , + table_types[i].name, table); +return 1 ; + } *(table_types[i].ptr) = table;if (table_types[i].name[0 ]) pr_cont("%s=0x%lx ",drivers/xen/efi.c +3
-0@@ -292,3 +292,72 @@ void __init xen_efi_runtime_setup(void) efi.get_next_high_mono_count = xen_efi_get_next_high_mono_count; efi.reset_system = xen_efi_reset_system; } + +static const efi_guid_t cfg_table_allow_list[]__initconst = { +ACPI_20_TABLE_GUID ,Quoted 8 lines
drivers/xen/efi.c void __init xen_efi_runtime_setup(void) · line 301
Quoted 4 lines
drivers/xen/efi.c +4
-0+ACPI_TABLE_GUID , +SMBIOS_TABLE_GUID , + SMBIOS3_TABLE_GUID, +};This allowlist seems redundant. Either the tables are already in memory that Xen will preserve or they aren’t. In both cases the subsequent code will do the right thing.
Will it? Currently, Xen simply accepts all ACPI and SMBIOS tables, regardless of what type of memory region they reside in (if any).
Quoted 6 lines
So what will happen with buggy firmware where the ACPI or SMBIOS tables are not covered by the memory map at all? Currently, this works fine but now, it will be rejected. And without ACPI tables, the boot will not get far enough to even inform the user what is wrong. And SMBIOS tables are used for platform quirks, which means they might be essential for a platform to boot as well.
If the tables are not in the memory map at all, I recommend add_taint(TAINT_FIRMWARE_WORKAROUND) but otherwise continuing to boot. If the tables are somewhere nonsensical, then the platform is FUBAR anyway. Linux alone might be able to work by reserving the memory, but under Xen that does not work.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Ard Biesheuvel
maintainerRFC PATCH 5/5 efi: esrt: Omit region sanity check when no memory map is available
In reply to Ard BiesheuvelIn order to permit the ESRT to be used when doing pseudo-EFI boot without a EFI memory map, e.g., when booting inside a Xen dom0 on x86, make the sanity checks optional based on whether the memory map is available.
If additional validation is needed, it is up to the Xen EFI glue code to implement this in its xen_efi_config_table_is_valid() helper, or provide a EFI memory map like it does on other architectures.
--- arch/x86/platform/efi/quirks.c | 3 + drivers/firmware/efi/esrt.c | 61 +++++++++++--------- 2 files changed, 37 insertions(+), 27 deletions(-)
arch/x86/platform/efi/quirks.c +3 -0
@@ -250,6 +250,9 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)int num_entries;void *new; +if (!efi_enabled (EFI_MEMMAP )) +return ; +if (efi_mem_desc_lookup (addr, &md) || md.type !=EFI_BOOT_SERVICES_DATA ) {pr_err ("Failed to lookup EFI memory descriptor for %pa\n" , &addr);
drivers/firmware/efi/esrt.c +34 -27
83 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [RFC PATCH 5/5] efi: esrt: Omit region sanity check when no memory map is available
Quoted 8 lines · Ard Biesheuvel
In order to permit the ESRT to be used when doing pseudo-EFI boot without a EFI memory map, e.g., when booting inside a Xen dom0 on x86, make the sanity checks optional based on whether the memory map is available.
If additional validation is needed, it is up to the Xen EFI glue code to implement this in its
xen_efi_config_table_is_valid()helper, or provide a EFI memory map like it does on other architectures.
I don’t like this. It is easy to use a hypercall to get the end of the memory region containing the config table, which is what my one of my previous patches actually does. Skipping all of the validation could easily lead to a regression. I understand wanting to get Xen-specific code out of esrt.c, but this isn’t the answer. Some sort of abstraction over both cases would be a much better solution.
Quoted 13 lines
Co-developed-by Demi Marie Obenour <[email protected]>Signed-off-by Demi Marie Obenour <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/platform/efi/quirks.c | 3 + drivers/firmware/efi/esrt.c | 61 +++++++++++--------- 2 files changed, 37 insertions(+), 27 deletions(-)arch/x86/platform/efi/quirks.c +0
-0
arch/x86/platform/efi/quirks.c void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) · line 255
Quoted 6 lines
arch/x86/platform/efi/quirks.c +3
-0@@ -250,6 +250,9 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)int num_entries;void *new; +if (!efi_enabled (EFI_MEMMAP )) +return ; +
This function does not actually work under Xen, even if EFI_MEMMAP is set. When running under Xen, either this function must never be called (in which case there should be at least a WARN()), or it should return an error that callers must check for.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Ard Biesheuvel
maintainerRe: [RFC PATCH 5/5] efi: esrt: Omit region sanity check when no memory map is available
Quoted 7 lines · Demi Marie Obenour
Quoted 5 lines · Ard Biesheuvel
In order to permit the ESRT to be used when doing pseudo-EFI boot without a EFI memory map, e.g., when booting inside a Xen dom0 on x86, make the sanity checks optional based on whether the memory map is available.
Quoted 8 lines
Quoted 3 lines
If additional validation is needed, it is up to the Xen EFI glue code to implement this in its
xen_efi_config_table_is_valid()helper, or provide a EFI memory map like it does on other architectures.I don’t like this. It is easy to use a hypercall to get the end of the memory region containing the config table, which is what my one of my previous patches actually does. Skipping all of the validation could easily lead to a regression.
I don't like putting Xen specific hacks left and right because Xen on x86 cannot be bothered to provide an EFI memory map. And as for regressions, ESRT does not work at all under Xen (given the lack of a memory map) and so I fail to see how this could break a currently working case.
Quoted 4 lines
I understand wanting to get Xen-specific
code out ofesrt.c, but this isn’t the answer. Some sort of abstraction over both cases would be a much better solution.
We have such an abstraction already, it is called the EFI memory map.
So there are two options here:
- expose a EFI memory map
- add a ESRT specific check to xen_efi_config_table_is_valid() so that the ESRT is withheld from dom0 if there is something wrong with it.
And frankly, the validation itself could use some attention as well:
"""
rc = efi_mem_desc_lookup(efi.esrt, &md);
...
max = efi_mem_desc_end(&md);
if (max < efi.esrt) {
"""
Unless I am missing something, this can never occur so the check is pointless and the pr_err() that follows is unreachable.
Then we have
"""
size = sizeof(*esrt);
max -= efi.esrt;
if (max < size) {
"""
'size' is 16 bytes here, so the only way this can become true is if the memory descriptor describes a region of 0 pages in length, which is explicitly forbidden by the EFI spec. If such a descriptor exists in spite of that, this is a memory map problem not a ESRT problem.
So actually, instead of making these checks conditional on EFI_MEMMAP being set, I might just rip them out entirely and be done with it.
Quoted 28 lines
Quoted 13 lines
Co-developed-by Demi Marie Obenour <[email protected]>Signed-off-by Demi Marie Obenour <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/platform/efi/quirks.c | 3 + drivers/firmware/efi/esrt.c | 61 +++++++++++--------- 2 files changed, 37 insertions(+), 27 deletions(-)arch/x86/platform/efi/quirks.c +0
-0arch/x86/platform/efi/quirks.c void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) · line 255
Quoted 6 lines
arch/x86/platform/efi/quirks.c +3
-0@@ -250,6 +250,9 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)int num_entries;void *new; +if (!efi_enabled (EFI_MEMMAP )) +return ; +This function does not actually work under Xen, even if
EFI_MEMMAPis set. When running under Xen, either this function must never be called (in which case there should be at least aWARN()), or it should return an error that callers must check for.
--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
Ard Biesheuvel
maintainerRe: [RFC PATCH 5/5] efi: esrt: Omit region sanity check when no memory map is available
Quoted 30 lines · Ard Biesheuvel
Quoted 7 lines · Demi Marie Obenour
Quoted 5 lines · Ard Biesheuvel
In order to permit the ESRT to be used when doing pseudo-EFI boot without a EFI memory map, e.g., when booting inside a Xen dom0 on x86, make the sanity checks optional based on whether the memory map is available.
Quoted 8 lines
Quoted 3 lines
If additional validation is needed, it is up to the Xen EFI glue code to implement this in its
xen_efi_config_table_is_valid()helper, or provide a EFI memory map like it does on other architectures.I don’t like this. It is easy to use a hypercall to get the end of the memory region containing the config table, which is what my one of my previous patches actually does. Skipping all of the validation could easily lead to a regression.
I don't like putting Xen specific hacks left and right because Xen on x86 cannot be bothered to provide an EFI memory map. And as for regressions, ESRT does not work at all under Xen (given the lack of a memory map) and so I fail to see how this could break a currently working case.
Quoted 4 lines
I understand wanting to get Xen-specific
code out ofesrt.c, but this isn’t the answer. Some sort of abstraction over both cases would be a much better solution.
Quoted 7 lines
We have such an abstraction already, it is called the EFI memory map.
So there are two options here:
- expose a EFI memory map
- add a ESRT specific check toxen_efi_config_table_is_valid()so that the ESRT is withheld from dom0 if there is something wrong with it.
Actually, the obvious answer here is to implement efi_mem_desc_lookup() for the EFI_PARAVIRT / !EFI_MEMMAP case. I'll have a go at that and send a v2 shortly.