efi/tpm: Preserve event log without changing x86 E820
cover efi/tpm: Preserve event log without changing x86 E820
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").
This patch series instead installs the Linux EFI memreserve table on the x86 stub path and then uses efi_mem_reserve_persistent() to preserve the TPM event log across kexec while keeping the allocation as EFI_LOADER_DATA.
The series was also backported to Linux 7.2 for validation on the affected system.
Results:
- stock 7.2:
TPM event log allocation changes the E820 map across boots; hibernation resume fails
- EFI_LOADER_DATA-only diagnostic build:
E820 map remains stable;
hibernation resume succeeds
- this series:
TPM range is persistently reserved;
hibernation resume succeeds with the normal device drivers; kexec_file_load() succeeds with the TPM range preserved; kexec_load() succeeds with the TPM range preserved
For kexec_file_load(), the event log had the same size and SHA256 digest before and after kexec. For kexec_load(), the before and after event log files compared byte-for-byte identical.
The original report and investigation are here:
https://lore.kernel.org/all/[email protected]/
Patch 1 makes the existing EFI memreserve table installer available to the x86 EFI stub path.
Patch 2 switches the TPM event log allocation back to EFI_LOADER_DATA and persistently reserves it after the normal TPM event log reservation has succeeded.
Jasmeet (Jazz) Bhatia (2):
efi/libstub: Install memreserve table on x86
efi/tpm: Persistently reserve the TPM event log
.../firmware/efi/libstub/efi-stub-helper.c | 23 ++++++++++++++++ drivers/firmware/efi/libstub/efi-stub.c | 23 ---------------- drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/tpm.c | 2 +- drivers/firmware/efi/libstub/x86-stub.c | 2 ++ drivers/firmware/efi/tpm.c | 27 +++++++++++++++++++ 6 files changed, 54 insertions(+), 24 deletions(-)
base-commit: 786262be6048deab760f68c8acc2c85607165894
Signature · 1 line
2.55.0
PATCH 1/2 efi/libstub: Install memreserve table on x86
The generic EFI stub installs the Linux EFI memreserve configuration table from efi_stub_common(), but x86 uses a separate EFI stub path. Consequently, efi_mem_reserve_persistent() cannot be used on x86 when no memreserve root has been installed.
Move install_memreserve_table() to the common stub helper code, expose it through efistub.h, and invoke it from the x86 stub before exiting boot services.
This allows x86 kernels to add persistent EFI memory reservations that are carried across kexec.
--- .../firmware/efi/libstub/efi-stub-helper.c | 23 +++++++++++++++++++ drivers/firmware/efi/libstub/efi-stub.c | 23 ------------------- drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/x86-stub.c | 2 ++ 4 files changed, 26 insertions(+), 23 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +23 -0
@@ -27,6 +27,29 @@ static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);int efi_mem_encrypt; +void install_memreserve_table (void ) +{ +struct linux_efi_memreserve *rsv; +efi_guid_t memreserve_table_guid =LINUX_EFI_MEMRESERVE_TABLE_GUID ; +efi_status_t status; + + status =efi_bs_call (allocate_pool,EFI_LOADER_DATA ,sizeof (*rsv), + (void **)&rsv); +if (status !=EFI_SUCCESS ) { +efi_err ("Failed to allocate memreserve entry!\n" ); +return ; + } + + rsv->next =0 ; + rsv->size =0 ; +atomic_set (&rsv->count,0 ); + + status =efi_bs_call (install_configuration_table, + &memreserve_table_guid, rsv); +if (status !=EFI_SUCCESS ) +efi_err ("Failed to install memreserve config table!\n" ); +} +bool __pure __efi_soft_reserve_enabled (void ) {return !efi_nosoftreserve;
drivers/firmware/efi/libstub/efi-stub.c +0 -23
@@ -77,29 +77,6 @@ static struct sysfb_display_info *setup_primary_display(void)return NULL ; } -static void install_memreserve_table (void ) -{ -struct linux_efi_memreserve *rsv; -efi_guid_t memreserve_table_guid =LINUX_EFI_MEMRESERVE_TABLE_GUID ; -efi_status_t status; - - status =efi_bs_call (allocate_pool,EFI_LOADER_DATA ,sizeof (*rsv), - (void **)&rsv); -if (status !=EFI_SUCCESS ) { -efi_err ("Failed to allocate memreserve entry!\n" ); -return ; - } - - rsv->next =0 ; - rsv->size =0 ; -atomic_set (&rsv->count,0 ); - - status =efi_bs_call (install_configuration_table, - &memreserve_table_guid, rsv); -if (status !=EFI_SUCCESS ) -efi_err ("Failed to install memreserve config table!\n" ); -} -static u32 get_supported_rt_services (void ) {const efi_rt_properties_table_t *rt_prop_table;
drivers/firmware/efi/libstub/efistub.h +1 -0
@@ -1168,6 +1168,7 @@ efi_enable_reset_attack_mitigation(void) { }#endif void efi_retrieve_eventlog (void ); +void install_memreserve_table (void );struct sysfb_display_info *alloc_primary_display (void );struct sysfb_display_info *__alloc_primary_display (void );
drivers/firmware/efi/libstub/x86-stub.c +2 -0
@@ -1015,6 +1015,8 @@ void __noreturn efi_stub_entry(efi_handle_t handle,efi_retrieve_eventlog (); +install_memreserve_table (); +setup_graphics (boot_params);setup_efi_pci (boot_params);
Signature · 1 line
2.55.0
PATCH 2/2 efi/tpm: Persistently reserve the TPM event log
In reply to Jasmeet (Jazz) BhatiaCommit 77d48d39e991 ("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption") changed the TPM event log allocation from EFI_LOADER_DATA to EFI_ACPI_RECLAIM_MEMORY so that the memory would remain reserved across kexec.
On x86, EFI_ACPI_RECLAIM_MEMORY is represented as ACPI data in the E820 map. If the EFI allocator places the event log at a different physical address on a subsequent boot, this changes the firmware E820 map.
x86 hibernation records architecture-specific information derived from that map and rejects the image when it differs on resume:
Hibernate inconsistent memory map detected!
PM: hibernation: Image mismatch: architecture specific data
This occurs on a Framework Laptop 16 (AMD Ryzen AI 300 Series), where the EFI allocation backing the TPM event log was observed at different addresses across otherwise ordinary boots.
Allocate the event log from EFI_LOADER_DATA again so that the Linux-created allocation does not appear as an E820 ACPI region. Preserve the kexec protection provided by commit 77d48d39e991 ("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption") by adding the event log range to the Linux EFI persistent memreserve table.
On Linux 7.2, the stock kernel failed hibernation resume because of the E820 mismatch, while both an EFI_LOADER_DATA-only diagnostic build and this series resumed successfully.
With this series, the TPM event log range remained reserved across both kexec_file_load() and kexec_load(). The contents exported through binary_bios_measurements were byte-for-byte identical before and after both kexec tests.
--- drivers/firmware/efi/libstub/tpm.c | 2 +- drivers/firmware/efi/tpm.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-)
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 ) {
drivers/firmware/efi/tpm.c +27 -0
@@ -15,6 +15,9 @@int efi_tpm_final_log_size;EXPORT_SYMBOL (efi_tpm_final_log_size); +#ifdef CONFIG_KEXEC_CORE +static unsigned int efi_tpm_eventlog_size__initdata ; +#endif static int __init tpm2_calc_event_log_size (void *data,int count,void *size_info) {
@@ -68,6 +71,10 @@ int __init efi_tpm_eventlog_init(void) goto out; } +#ifdef CONFIG_KEXEC_CORE + efi_tpm_eventlog_size = tbl_size; +#endif +if (efi.tpm_final_log ==EFI_INVALID_TABLE_ADDR ) {pr_info ("TPM Final Events table not present\n" ); goto out;
@@ -114,3 +121,23 @@ int __init efi_tpm_eventlog_init(void)return ret; } +#ifdef CONFIG_KEXEC_CORE +static int __init efi_tpm_eventlog_reserve_persistent (void ) +{ +int ret; + +if (efi.tpm_log ==EFI_INVALID_TABLE_ADDR || + !efi_tpm_eventlog_size) +return 0 ; + + ret =efi_mem_reserve_persistent (efi.tpm_log, + efi_tpm_eventlog_size); +if (ret) +pr_warn ("Failed to persistently reserve TPM Event Log: %d\n" , + ret); + +return 0 ; +} +late_initcall (efi_tpm_eventlog_reserve_persistent); +#endif +
Signature · 1 line
2.55.0
Ard Biesheuvel
maintainerHi Jazz,
Quoted 10 lines · Jasmeet (Jazz) Bhatia
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:
Quoted 8 lines
Hibernate inconsistent memory map detected!
PM: hibernation: Image mismatch: architecture specific dataAllocating the event log as
EFI_LOADER_DATAavoids changing the E820 map, but doing that alone would regress the kexec corruption issue fixed by commit77d48d39e991("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption").
As I replied in the other thread, I am not convinced preserving the TPM event log across a kexec makes sense to begin with. This is the firmware's view of the state of the TPM PCRs when it handed over the system to the first OS.
If the first OS boots, loads a kexec kernel and then boots it without measuring any of that into the TPM, the TPM event log will match the TPM state, but this is meaningless because of the missing measurements, and the attestation chain is broken.
If the first OS does perform TPM measurements, it would need to record them into a log and pass that on to the kexec'ed in some implementation specific way - it cannot use the existing TPM event log for that.
TL;DR perhaps we should just discard the TPM event log reference from the EFI config tables after consuming it. Or add a special case to the kexec code to disregard it.
Quoted 32 lines · Ard Biesheuvel
Hi Jazz,
Quoted 10 lines · Jasmeet (Jazz) Bhatia
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:
Quoted 8 lines
Hibernate inconsistent memory map detected!
PM: hibernation: Image mismatch: architecture specific dataAllocating the event log as
EFI_LOADER_DATAavoids changing the E820 map, but doing that alone would regress the kexec corruption issue fixed by commit77d48d39e991("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption").As I replied in the other thread, I am not convinced preserving the TPM event log across a kexec makes sense to begin with. This is the firmware's view of the state of the TPM PCRs when it handed over the system to the first OS.
If the first OS boots, loads a kexec kernel and then boots it without measuring any of that into the TPM, the TPM event log will match the TPM state, but this is meaningless because of the missing measurements, and the attestation chain is broken.
Quoted 7 lines
If the first OS does perform TPM measurements, it would need to record them into a log and pass that on to the kexec'ed in some implementation specific way - it cannot use the existing TPM event log for that.
TL;DR perhaps we should just discard the TPM event log reference from the EFI config tables after consuming it. Or add a special case to the kexec code to disregard it.
Ok, honestly that makes a lot of sense I see what you are saying and I tend to agree. I'm treating the copied event log at something that survives kexec in this patch because of the corruption fix so I'll look at dropping that LINUX_EFI_TPM_EVENT_LOG_GUID reference and rewrite this patch. Hopefully, that will avoid that x86 E820 instability as well.
Appreciate the guidance!
Ard Biesheuvel
maintainerQuoted 40 lines · Jasmeet (Jazz) Bhatia
Quoted 32 lines · Ard Biesheuvel
Hi Jazz,
Quoted 10 lines · Jasmeet (Jazz) Bhatia
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:
Quoted 8 lines
Hibernate inconsistent memory map detected!
PM: hibernation: Image mismatch: architecture specific dataAllocating the event log as
EFI_LOADER_DATAavoids changing the E820 map, but doing that alone would regress the kexec corruption issue fixed by commit77d48d39e991("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption").As I replied in the other thread, I am not convinced preserving the TPM event log across a kexec makes sense to begin with. This is the firmware's view of the state of the TPM PCRs when it handed over the system to the first OS.
If the first OS boots, loads a kexec kernel and then boots it without measuring any of that into the TPM, the TPM event log will match the TPM state, but this is meaningless because of the missing measurements, and the attestation chain is broken.
Quoted 7 lines
If the first OS does perform TPM measurements, it would need to record them into a log and pass that on to the kexec'ed in some implementation specific way - it cannot use the existing TPM event log for that.
TL;DR perhaps we should just discard the TPM event log reference from the EFI config tables after consuming it. Or add a special case to the kexec code to disregard it.
Quoted 7 lines
Ok, honestly that makes a lot of sense I see what you are saying and I tend to agree. I'm treating the copied event log at something that survives kexec in this patch because of the corruption fix so I'll look at dropping that
LINUX_EFI_TPM_EVENT_LOG_GUIDreference and rewrite this patch. Hopefully, that will avoid that x86 E820 instability as well.Appreciate the guidance!
Please don't send any patches yet - I'd like to get some input from other folks as well.
Ilias Apalodimas
reviewerHi Ard,
Quoted 27 lines · Ard Biesheuvel
Quoted 23 lines · Jasmeet (Jazz) Bhatia
Quoted 22 lines · Ard Biesheuvel
Hi Jazz,
Quoted 18 lines · Jasmeet (Jazz) Bhatia
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 dataAllocating the event log as
EFI_LOADER_DATAavoids changing the E820 map, but doing that alone would regress the kexec corruption issue fixed by commit77d48d39e991("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption").
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
As I replied in the other thread, I am not convinced preserving the TPM event log across a kexec makes sense to begin with. This is the firmware's view of the state of the TPM PCRs when it handed over the system to the first OS.
It doesn't make sense imho. The only thing you prove is that you kexeced from a 'previous good OS', but without knowledge of what that OS did before kexecing.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
If the first OS boots, loads a kexec kernel and then boots it without measuring any of that into the TPM, the TPM event log will match the TPM state, but this is meaningless because of the missing measurements, and the attestation chain is broken.
If the first OS does perform TPM measurements, it would need to record them into a log and pass that on to the kexec'ed in some implementation specific way - it cannot use the existing TPM event log for that.
Hrmm, that might be problematic. The EventLog needs to illustrate the state of the TPM PCRs. IOW replaying the EventLog in memory should match the current TPM PCRs. But that also means we'll have to teach kexec how to properly extend the PCRs and update the EventLog.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
TL;DR perhaps we should just discard the TPM event log reference from the EFI config tables after consuming it. Or add a special case to the kexec code to disregard it.
I can't decide what's best here. IIRC we don't expose the final event log config table in systab right? So we won't break any user-space app that depends on it. We just copy it in /sys/kernel/security/tpm0/binary_bios_measurements. In that case getting rid of it would work.
Cheers
/Ilias
Quoted 11 lines
Quoted 7 lines
Ok, honestly that makes a lot of sense I see what you are saying and I tend to agree. I'm treating the copied event log at something that survives kexec in this patch because of the corruption fix so I'll look at dropping that
LINUX_EFI_TPM_EVENT_LOG_GUIDreference and rewrite this patch. Hopefully, that will avoid that x86 E820 instability as well.Appreciate the guidance!
Please don't send any patches yet - I'd like to get some input from other folks as well.
Quoted 45 lines · Ilias Apalodimas
Hi Ard,
Quoted 27 lines · Ard Biesheuvel
Quoted 23 lines · Jasmeet (Jazz) Bhatia
Quoted 22 lines · Ard Biesheuvel
Hi Jazz,
Quoted 18 lines · Jasmeet (Jazz) Bhatia
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 dataAllocating the event log as
EFI_LOADER_DATAavoids changing the E820 map, but doing that alone would regress the kexec corruption issue fixed by commit77d48d39e991("efistub/tpm: Use ACPI reclaim memory for event log to avoid corruption").Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
As I replied in the other thread, I am not convinced preserving the TPM event log across a kexec makes sense to begin with. This is the firmware's view of the state of the TPM PCRs when it handed over the system to the first OS.
It doesn't make sense imho. The only thing you prove is that you kexeced from a 'previous good OS', but without knowledge of what that OS did before kexecing.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
If the first OS boots, loads a kexec kernel and then boots it without measuring any of that into the TPM, the TPM event log will match the TPM state, but this is meaningless because of the missing measurements, and the attestation chain is broken.
Quoted 13 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
If the first OS does perform TPM measurements, it would need to record them into a log and pass that on to the kexec'ed in some implementation specific way - it cannot use the existing TPM event log for that.
Hrmm, that might be problematic. The EventLog needs to illustrate the state of the TPM PCRs. IOW replaying the EventLog in memory should match the current TPM PCRs. But that also means we'll have to teach kexec how to properly extend the PCRs and update the EventLog.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
TL;DR perhaps we should just discard the TPM event log reference from the EFI config tables after consuming it. Or add a special case to the kexec code to disregard it.
Assuming we drop the LINUX_EFI_TPM_EVENT_LOG_GUID reference after the kernel consumes the log, is that going to impact any in-kernel kexec path that expects that efi.tpm_log to remain valid? Or is that reference only needed during the initial TPM event log import?
I'm trying to understand whether removing that reference is just for stopping the next kernel from re-importing the stale firmware log, or whether other there exists other users of that reference.
Quoted 21 lines
I can't decide what's best here. IIRC we don't expose the final event log config table in systab right? So we won't break any user-space app that depends on it. We just copy it in
/sys/kernel/security/tpm0/binary_bios_measurements. In that case getting rid of it would work.Cheers
/IliasQuoted 11 lines
Quoted 7 lines
Ok, honestly that makes a lot of sense I see what you are saying and I tend to agree. I'm treating the copied event log at something that survives kexec in this patch because of the corruption fix so I'll look at dropping that
LINUX_EFI_TPM_EVENT_LOG_GUIDreference and rewrite this patch. Hopefully, that will avoid that x86 E820 instability as well.Appreciate the guidance!
Please don't send any patches yet - I'd like to get some input from other folks as well.