x86/efi: Disregard stale EFI config tables after kexec
cover x86/efi: Disregard stale EFI config tables after kexec
From: Ard Biesheuvel <[email protected]>
On x86, kexec passes the firmware's EFI configuration table array on to the next kernel as-is, including tables that reside in memory that has been released or repurposed since, and may therefore have been clobbered by the time the next kernel gets to parse them.
Patch #1 wires up efi_config_table_is_usable() for x86, and uses it to disregard the INITRD and Memory Attributes tables when booting via kexec. This fixes a panic when kexec'ing without an initrd, and supersedes the existing handling of the Memory Attributes table, which does not cover all cases.
Patch #2 does the same for the TPM event log table, so that the EFI stub can allocate it as EFI loader data again without regressing kexec. This fixes hibernation on systems where the ACPI reclaim allocation that is currently used for the event log ends up at a different address on each boot, resulting in an inconsistent E820 map.
Ard Biesheuvel (2):
x86/efi: Disregard the INITRD and MEMATTR config tables after kexec efi/tpm: Allocate the event log from EFI loader data again
arch/x86/platform/efi/efi.c | 32 ++++++++++++++++++++++++++++++ arch/x86/platform/efi/quirks.c | 4 ---- drivers/firmware/efi/libstub/tpm.c | 2 +- include/linux/efi.h | 3 +++ 4 files changed, 36 insertions(+), 5 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 1/2 x86/efi: Disregard the INITRD and MEMATTR config tables after kexec
From: Ard Biesheuvel <[email protected]>
When the EFI stub loads the initrd itself, it describes its location and size to the core kernel via a LINUX_EFI_INITRD_MEDIA_GUID configuration table, which is allocated from EFI loader data, just like the initrd. On x86, the stub passes the same information via boot_params as well, which takes precedence, but the core kernel will fall back to the INITRD config table if boot_params does not describe an initrd.
The EFI configuration table array is passed on to kexec'd kernels as-is, and so a kexec'd kernel that is booted without an initrd will pick up the INITRD config table that was installed during the original boot, and use the initrd it describes, which no longer exists at that point.
Commit 6e62d1c6f2c7 ("efi: Wipe INITRD config table from memory after consumption") mitigated this by wiping the base and size fields of the table once it has been consumed. However, the memory holding the table is released to the page allocator as well, and so it may have been reused by the time the kexec occurs, in which case the kexec'd kernel will interpret whatever happens to be stored there as the base and size of the initrd, e.g.,
[ 0.000000] efi: ... INITRD=0x7c41cc18 ... [ 0.000000] RAMDISK: [mem 0x9090909090909090-0xd1e7d1e68aaeafff] [ 0.000000] Kernel panic - not syncing: Cannot find place for new RAMDISK of size 4708303772963442675
Crash kernels will find the stale table as well, and wipe it again, i.e., write to the memory of the crashed kernel that they are supposed to capture.
Both kexec_file_load() and kexec-tools pass a SETUP_EFI setup_data entry to the next kernel when EFI is enabled, which the x86 EFI code already relies on to detect that it was booted via kexec. Such kernels are entered directly, bypassing the EFI stub, and so any INITRD config table they encounter is necessarily stale. So wire up efi_config_table_is_usable() for x86, and use it to disregard the INITRD config table when a SETUP_EFI setup_data entry is present.
While at it, use the same mechanism to disregard the Memory Attributes table after kexec, which is currently achieved by overwriting its address in the firmware's config table array with EFI_INVALID_TABLE_ADDR in efi_reuse_config(). This only happens on 64-bit firmware, and only if the SETUP_EFI entry carries the address of an SMBIOS table, which kexec-tools does not provide on systems that only expose an SMBIOS3 entry point. In that case, the kexec'd kernel will parse the table, which may be stale as well, given that EDK2 based firmware allocates it from EFI boot services data, which is released to the page allocator after boot too, e.g.,
[ 0.000000] efi: memattr: Unexpected EFI Memory Attributes table version -1958647317
--- arch/x86/platform/efi/efi.c | 26 ++++++++++++++++++++++++++ arch/x86/platform/efi/quirks.c | 4 ---- include/linux/efi.h | 3 +++ 3 files changed, 29 insertions(+), 4 deletions(-)
arch/x86/platform/efi/efi.c +26 -0
@@ -432,6 +432,32 @@ static int __init efi_systab_init(unsigned long phys)return 0 ; } +/* +* kexec passes the firmware's config table array on to the next kernel as-is, +* but some of the tables it describes must be disregarded when booting via +* kexec, which is indicated by the presence of a SETUP_EFI setup_data entry: +* +* - The INITRD table is installed by the EFI stub to describe the initrd that +* it loaded. Both the table and the initrd are allocated from EFI loader +* data, which is released to the page allocator once the kernel is up and +* running, and so the table is stale by the time kexec boots the next kernel. +* +* - The Memory Attributes table may reside in EFI boot services data, which is +* released to the page allocator as well. Even if it is still intact, the +* sanity check in efi_memattr_init() may fail spuriously, given that only +* the runtime regions of the EFI memory map are passed on via kexec. Applying +* the permissions it describes is not critical, so just ignore it. +*/ +bool __init x86_efi_config_table_is_usable (const efi_guid_t *guid, +unsigned long table) +{ +if (!efi_setup) +return true; + +return efi_guidcmp (*guid,LINUX_EFI_INITRD_MEDIA_GUID ) && +efi_guidcmp (*guid,EFI_MEMORY_ATTRIBUTES_TABLE_GUID ); +} +static int __init efi_config_init (const efi_config_table_type_t *arch_tables) {void *config_tables;
arch/x86/platform/efi/quirks.c +0 -4
@@ -611,10 +611,6 @@ int __init efi_reuse_config(u64 tables, int nr_tables)if (!efi_guidcmp (guid,SMBIOS_TABLE_GUID )) ((efi_config_table_64_t *)p)->table = data->smbios; -/* Do not bother to play with mem attr table across kexec */ -if (!efi_guidcmp (guid,EFI_MEMORY_ATTRIBUTES_TABLE_GUID )) - ((efi_config_table_64_t *)p)->table =EFI_INVALID_TABLE_ADDR ; - p += sz; }early_memunmap (tablep, nr_tables * sz);
include/linux/efi.h +3 -0
@@ -1355,10 +1355,13 @@ struct linux_efi_initrd {#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 *guid,unsigned long table); +bool x86_efi_config_table_is_usable (const efi_guid_t *guid,unsigned long table);static __always_inline bool efi_config_table_is_usable(const efi_guid_t *guid, unsigned long table) { +if (IS_ENABLED (CONFIG_X86 ) && !x86_efi_config_table_is_usable (guid, table)) +return false;if (!IS_ENABLED (CONFIG_XEN_EFI ))return true;return xen_efi_config_table_is_usable (guid, table);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 2/2 efi/tpm: Allocate the event log from EFI loader data again
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The EFI stub currently allocates the TPM event log as EFI_ACPI_RECLAIM_MEMORY. On x86, this becomes an ACPI data entry in the E820 map.
On a Framework Laptop 16 (AMD Ryzen AI 300 Series), the EFI allocator can place this allocation at different physical addresses across boots. Since x86 hibernation validates architecture-specific data from the firmware E820 map, this causes an otherwise valid hibernation image to be rejected on resume with the following error:
Hibernate inconsistent memory map detected!
PM: hibernation: Image mismatch: architecture specific data
Allocating the event log as EFI_LOADER_DATA avoids changing the E820 map, but doing that alone would regress the kexec corruption issue fixed by commit 77d48d39e991 ("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption"): the E820 map that kexec passes on to the next kernel describes EFI loader data as ordinary RAM, and so the event log may get clobbered before the next kernel gets around to reserving it.
So use the x86 implementation of efi_config_table_is_usable() to disregard the TPM event log table after kexec, just like the INITRD table, and go back to allocating it as EFI_LOADER_DATA. This is simpler than preserving the event log across kexec, e.g., by installing the memreserve table on x86 and using efi_mem_reserve_persistent(), and the utility of the event log after kexec is questionable to begin with.
This also protects kexec'd kernels from a clobbered event log if the previous kernel predates the change above, and allocated the table from EFI loader data as well. The kexec issue was specific to x86, so other architectures can use EFI loader data as before.
--- arch/x86/platform/efi/efi.c | 6 ++++++ drivers/firmware/efi/libstub/tpm.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +6 -0
@@ -442,6 +442,11 @@ static int __init efi_systab_init(unsigned long phys)* data, which is released to the page allocator once the kernel is up and * running, and so the table is stale by the time kexec boots the next kernel. * +* - The TPM event log table is installed by the EFI stub too, and allocated +* from EFI loader data as well. It is reserved by the kernel, but the E820 +* memory map that kexec passes on describes it as ordinary RAM, and so it +* may get clobbered before the next kernel gets around to reserving it. +* * - The Memory Attributes table may reside in EFI boot services data, which is * released to the page allocator as well. Even if it is still intact, the * sanity check in efi_memattr_init() may fail spuriously, given that only
@@ -455,6 +460,7 @@ bool __init x86_efi_config_table_is_usable(const efi_guid_t *guid,return true;return efi_guidcmp (*guid,LINUX_EFI_INITRD_MEDIA_GUID ) && +efi_guidcmp (*guid,LINUX_EFI_TPM_EVENT_LOG_GUID ) &&efi_guidcmp (*guid,EFI_MEMORY_ATTRIBUTES_TABLE_GUID ); }
drivers/firmware/efi/libstub/tpm.c +1 -1
@@ -96,7 +96,7 @@ static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca }/* Allocate space for the logs and copy them. */ - status =efi_bs_call (allocate_pool,EFI_ACPI_RECLAIM_MEMORY , + status =efi_bs_call (allocate_pool,EFI_LOADER_DATA ,sizeof (*log_tbl) + log_size, (void **)&log_tbl);if (status !=EFI_SUCCESS ) {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Hi Ard,
Quoted 10 lines · Ard Biesheuvel
[...]
Patch #1 wires upefi_config_table_is_usable()for x86, and uses it to disregard the INITRD and Memory Attributes tables when booting via kexec. This fixes a panic when kexec'ing without an initrd, and supersedes the existing handling of the Memory Attributes table, which does not cover all cases.Patch #2 does the same for the TPM event log table, so that the EFI stub can allocate it as EFI loader data again without regressing kexec. [...]
I've tested both patches on my Framework Laptop 16 (AMD Ryzen AI 300) running Linux 7.4-rc4, and the results look good!
- Hibernation: 4/4 successful platform hibernation/resume cycles with both EFI patches applied. The original inconsistent E820 memory map error no longer occurs, and the saved sessions restore correctly.
- kexec: Successfully booted into the patched kernel using systemctl kexec. The kernel correctly marked the inherited MEMATTR, INITRD, and TPMEventLog EFI configuration tables as unusable and booted normally. The results suggest that both patches are working as intended on my hardware.
Thanks again for preparing these fixes!
Jazz