Skip to contentSkip to conversation

efi/tpm: add efi.tpm_log as a reserved region in 820_table_firmware

RFC In review Usama Arif 8 participants 38 messages Sep 11, 2024 – Oct 9, 2024

Subsystems (5)
lore
Share & tools
lore

Usama Arif

author
#0 lore raw

RFC 1/1 efi/tpm: add efi.tpm_log as a reserved region in 820_table_firmware

Looking at the TPM spec [1]

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform FirmwareTCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface.

It looks like the firmware should pass this as reserved in e820 memory map. However, it doesn't seem to. The firmware being tested on is:
dmidecode -s bios-version
edk2-20240214-2.el9

When this area is not reserved, it comes up as usable in /sys/firmware/memmap. This means that kexec, which uses that memmap to find usable memory regions, can select the region where efi.tpm_log is and overwrite it and relocate_kernel.

Having a fix in firmware can be difficult to get through. As a secondary fix, this patch marks that region as reserved in e820_table_firmware if it is currently E820_TYPE_RAM so that kexec doesn't use it for kernel segments.

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

---
 arch/x86/include/asm/e820/api.h | 2 ++
 arch/x86/kernel/e820.c          | 6 ++++++
 arch/x86/platform/efi/efi.c     | 9 +++++++++
 drivers/firmware/efi/tpm.c      | 2 +-
 include/linux/efi.h             | 7 +++++++
 5 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/e820/api.h +2 -0
index 2e74a7f0e935..4e9aa24f03bd 100644
@@ -16,6 +16,8 @@ extern bool e820__mapped_all(u64 start, u64 end, enum e820_type type);
 
 extern void e820__range_add   (u64 start, u64 size, enum e820_type type);
 extern u64  e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
+extern u64  e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+					enum e820_type new_type);
 extern u64  e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type);
 extern u64  e820__range_update_table(struct e820_table *t, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
 
arch/x86/kernel/e820.c +6 -0
index 4893d30ce438..912400161623 100644
@@ -538,6 +538,12 @@ u64 __init e820__range_update_table(struct e820_table *t, u64 start, u64 size,
 	return __e820__range_update(t, start, size, old_type, new_type);
 }
 
+u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+				       enum e820_type new_type)
+{
+	return __e820__range_update(e820_table_firmware, start, size, old_type, new_type);
+}
+
 /* Remove a range of memory from the E820 table: */
 u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
 {
arch/x86/platform/efi/efi.c +9 -0
index 88a96816de9a..aa95f77d7a30 100644
@@ -171,6 +171,15 @@ static void __init do_add_efi_memmap(void)
 	e820__update_table(e820_table);
 }
 
+/* Reserve firmware area if it was marked as RAM */
+void arch_update_firmware_area(u64 addr, u64 size)
+{
+	if (e820__get_entry_type(addr, addr + size) == E820_TYPE_RAM) {
+		e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
+		e820__update_table(e820_table_firmware);
+	}
+}
+
 /*
  * Given add_efi_memmap defaults to 0 and there is no alternative
  * e820 mechanism for soft-reserved memory, import the full EFI memory
drivers/firmware/efi/tpm.c +1 -1
index e8d69bd548f3..8e6e7131d718 100644
@@ -60,6 +60,7 @@ int __init efi_tpm_eventlog_init(void)
 	}
 
 	tbl_size = sizeof(*log_tbl) + log_tbl->size;
+	arch_update_firmware_area(efi.tpm_log, tbl_size);
 	memblock_reserve(efi.tpm_log, tbl_size);
 
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
@@ -107,4 +108,3 @@ int __init efi_tpm_eventlog_init(void)
 	early_memunmap(log_tbl, sizeof(*log_tbl));
 	return ret;
 }
-
include/linux/efi.h +7 -0
index 6bf3c4fe8511..9c239cdff771 100644
@@ -1371,4 +1371,11 @@ extern struct blocking_notifier_head efivar_ops_nh;
 void efivars_generic_ops_register(void);
 void efivars_generic_ops_unregister(void);
 
+#ifdef CONFIG_X86_64
+void __init arch_update_firmware_area(u64 addr, u64 size);
+#else
+static inline void __init arch_update_firmware_area(u64 addr, u64 size)
+{
+}
+#endif
 #endif /* _LINUX_EFI_H */
Signature · 1 line
2.43.5
maintainer
#1 lore raw
Quoted 18 lines · Usama Arif

Looking at the TPM spec [1]

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform FirmwareTCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface.

It looks like the firmware should pass this as reserved in e820 memory map. However, it doesn't seem to. The firmware being tested on is:
dmidecode -s bios-version
edk2-20240214-2.el9

When this area is not reserved, it comes up as usable in /sys/firmware/memmap. This means that kexec, which uses that memmap to find usable memory regions, can select the region where efi.tpm_log is and overwrite it and relocate_kernel.

Quoted 7 lines

Having a fix in firmware can be difficult to get through. As a secondary fix, this patch marks that region as reserved in e820_table_firmware if it is currently E820_TYPE_RAM so that kexec doesn't use it for kernel segments.

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

I would expect the EFI memory map to E820 conversion implemented in the EFI stub to take care of this.

If you are not booting via the EFI stub, the bootloader is performing this conversion, and so it should be done there instead.

Quoted 94 lines
---
 arch/x86/include/asm/e820/api.h | 2 ++
 arch/x86/kernel/e820.c          | 6 ++++++
 arch/x86/platform/efi/efi.c     | 9 +++++++++
 drivers/firmware/efi/tpm.c      | 2 +-
 include/linux/efi.h             | 7 +++++++
 5 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/e820/api.h +2 -0
index 2e74a7f0e935..4e9aa24f03bd 100644
@@ -16,6 +16,8 @@ extern bool e820__mapped_all(u64 start, u64 end, enum e820_type type);
 
 extern void e820__range_add   (u64 start, u64 size, enum e820_type type);
 extern u64  e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
+extern u64  e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+                                       enum e820_type new_type);
 extern u64  e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type);
 extern u64  e820__range_update_table(struct e820_table *t, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
 
arch/x86/kernel/e820.c +6 -0
index 4893d30ce438..912400161623 100644
@@ -538,6 +538,12 @@ u64 __init e820__range_update_table(struct e820_table *t, u64 start, u64 size,
        return __e820__range_update(t, start, size, old_type, new_type);
 }
 
+u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+                                      enum e820_type new_type)
+{
+       return __e820__range_update(e820_table_firmware, start, size, old_type, new_type);
+}
+
 /* Remove a range of memory from the E820 table: */
 u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
 {
arch/x86/platform/efi/efi.c +9 -0
index 88a96816de9a..aa95f77d7a30 100644
@@ -171,6 +171,15 @@ static void __init do_add_efi_memmap(void)
        e820__update_table(e820_table);
 }
 
+/* Reserve firmware area if it was marked as RAM */
+void arch_update_firmware_area(u64 addr, u64 size)
+{
+       if (e820__get_entry_type(addr, addr + size) == E820_TYPE_RAM) {
+               e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
+               e820__update_table(e820_table_firmware);
+       }
+}
+
 /*
  * Given add_efi_memmap defaults to 0 and there is no alternative
  * e820 mechanism for soft-reserved memory, import the full EFI memory
drivers/firmware/efi/tpm.c +1 -1
index e8d69bd548f3..8e6e7131d718 100644
@@ -60,6 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
+       arch_update_firmware_area(efi.tpm_log, tbl_size);
        memblock_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
@@ -107,4 +108,3 @@ int __init efi_tpm_eventlog_init(void)
        early_memunmap(log_tbl, sizeof(*log_tbl));
        return ret;
 }
-
include/linux/efi.h +7 -1
index 6bf3c4fe8511..9c239cdff771 100644
@@ -1371,4 +1371,11 @@ extern struct blocking_notifier_head efivar_ops_nh;
 void efivars_generic_ops_register(void);
 void efivars_generic_ops_unregister(void);
 
+#ifdef CONFIG_X86_64
+void __init arch_update_firmware_area(u64 addr, u64 size);
+#else
+static inline void __init arch_update_firmware_area(u64 addr, u64 size)
+{
+}
+#endif
 #endif /* _LINUX_EFI_H */
--

2.43.5

author
#2 lore raw
Quoted 19 lines · Ard Biesheuvel
Quoted 18 lines · Usama Arif

Looking at the TPM spec [1]

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform FirmwareTCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface.

It looks like the firmware should pass this as reserved in e820 memory map. However, it doesn't seem to. The firmware being tested on is:
dmidecode -s bios-version
edk2-20240214-2.el9

When this area is not reserved, it comes up as usable in /sys/firmware/memmap. This means that kexec, which uses that memmap to find usable memory regions, can select the region where efi.tpm_log is and overwrite it and relocate_kernel.

Quoted 7 lines
Quoted 7 lines

Having a fix in firmware can be difficult to get through. As a secondary fix, this patch marks that region as reserved in e820_table_firmware if it is currently E820_TYPE_RAM so that kexec doesn't use it for kernel segments.

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

Forgot to add:

Quoted 7 lines

I would expect the EFI memory map to E820 conversion implemented in the EFI stub to take care of this.

If you are not booting via the EFI stub, the bootloader is performing this conversion, and so it should be done there instead.

I will look into this and report back.
Thanks

Quoted 95 lines
Quoted 95 lines
---
 arch/x86/include/asm/e820/api.h | 2 ++
 arch/x86/kernel/e820.c          | 6 ++++++
 arch/x86/platform/efi/efi.c     | 9 +++++++++
 drivers/firmware/efi/tpm.c      | 2 +-
 include/linux/efi.h             | 7 +++++++
 5 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/e820/api.h +2 -0
index 2e74a7f0e935..4e9aa24f03bd 100644
@@ -16,6 +16,8 @@ extern bool e820__mapped_all(u64 start, u64 end, enum e820_type type);
 
 extern void e820__range_add   (u64 start, u64 size, enum e820_type type);
 extern u64  e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
+extern u64  e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+                                       enum e820_type new_type);
 extern u64  e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type);
 extern u64  e820__range_update_table(struct e820_table *t, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
 
arch/x86/kernel/e820.c +6 -0
index 4893d30ce438..912400161623 100644
@@ -538,6 +538,12 @@ u64 __init e820__range_update_table(struct e820_table *t, u64 start, u64 size,
        return __e820__range_update(t, start, size, old_type, new_type);
 }
 
+u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+                                      enum e820_type new_type)
+{
+       return __e820__range_update(e820_table_firmware, start, size, old_type, new_type);
+}
+
 /* Remove a range of memory from the E820 table: */
 u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
 {
arch/x86/platform/efi/efi.c +9 -0
index 88a96816de9a..aa95f77d7a30 100644
@@ -171,6 +171,15 @@ static void __init do_add_efi_memmap(void)
        e820__update_table(e820_table);
 }
 
+/* Reserve firmware area if it was marked as RAM */
+void arch_update_firmware_area(u64 addr, u64 size)
+{
+       if (e820__get_entry_type(addr, addr + size) == E820_TYPE_RAM) {
+               e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
+               e820__update_table(e820_table_firmware);
+       }
+}
+
 /*
  * Given add_efi_memmap defaults to 0 and there is no alternative
  * e820 mechanism for soft-reserved memory, import the full EFI memory
drivers/firmware/efi/tpm.c +1 -1
index e8d69bd548f3..8e6e7131d718 100644
@@ -60,6 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
+       arch_update_firmware_area(efi.tpm_log, tbl_size);
        memblock_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
@@ -107,4 +108,3 @@ int __init efi_tpm_eventlog_init(void)
        early_memunmap(log_tbl, sizeof(*log_tbl));
        return ret;
 }
-
include/linux/efi.h +7 -1
index 6bf3c4fe8511..9c239cdff771 100644
@@ -1371,4 +1371,11 @@ extern struct blocking_notifier_head efivar_ops_nh;
 void efivars_generic_ops_register(void);
 void efivars_generic_ops_unregister(void);
 
+#ifdef CONFIG_X86_64
+void __init arch_update_firmware_area(u64 addr, u64 size);
+#else
+static inline void __init arch_update_firmware_area(u64 addr, u64 size)
+{
+}
+#endif
 #endif /* _LINUX_EFI_H */
--

2.43.5

author
#3 lore raw
In reply to Ard Biesheuvel
Quoted 23 lines · Ard Biesheuvel
Quoted 22 lines · Usama Arif

Looking at the TPM spec [1]

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform FirmwareTCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface.

It looks like the firmware should pass this as reserved in e820 memory map. However, it doesn't seem to. The firmware being tested on is:
dmidecode -s bios-version
edk2-20240214-2.el9

When this area is not reserved, it comes up as usable in /sys/firmware/memmap. This means that kexec, which uses that memmap to find usable memory regions, can select the region where efi.tpm_log is and overwrite it and relocate_kernel.

Having a fix in firmware can be difficult to get through. As a secondary fix, this patch marks that region as reserved in e820_table_firmware if it is currently E820_TYPE_RAM so that kexec doesn't use it for kernel segments.

Quoted 7 lines
Quoted 3 lines

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

I would expect the EFI memory map to E820 conversion implemented in the EFI stub to take care of this.

So I have been making a prototype with EFI stub, and the unfinished version is looking like a horrible hack.

The only way to do this in libstub is to pass log_tbl all the way from efi_retrieve_tcg2_eventlog to efi_stub_entry and from there to setup_e820.
While going through the efi memory map and converting it to e820 table in setup_e820, you have to check if log_tbl falls in any of the ranges and if the range is E820_TYPE_RAM. If that condition is satisfied, then you have to split that range into 3. i.e. the E820_TYPE_RAM range before tpm_log, the tpm_log E820_TYPE_RESERVED range, and the E820_TYPE_RAM range after. There are no helper functions, so this splitting involves playing with a lot of pointers, and it looks quite ugly. I believe doing this way is more likely to introduce bugs.

If we are having to compensate for an EFI bug, would it make sense to do it in the way done in RFC and do it in kernel rather than libstub? It is simple and very likely to be bug free.

Thanks,
Usama

Quoted 98 lines

If you are not booting via the EFI stub, the bootloader is performing this conversion, and so it should be done there instead.

Quoted 94 lines
---
 arch/x86/include/asm/e820/api.h | 2 ++
 arch/x86/kernel/e820.c          | 6 ++++++
 arch/x86/platform/efi/efi.c     | 9 +++++++++
 drivers/firmware/efi/tpm.c      | 2 +-
 include/linux/efi.h             | 7 +++++++
 5 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/e820/api.h +2 -0
index 2e74a7f0e935..4e9aa24f03bd 100644
@@ -16,6 +16,8 @@ extern bool e820__mapped_all(u64 start, u64 end, enum e820_type type);
 
 extern void e820__range_add   (u64 start, u64 size, enum e820_type type);
 extern u64  e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
+extern u64  e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+                                       enum e820_type new_type);
 extern u64  e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type);
 extern u64  e820__range_update_table(struct e820_table *t, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
 
arch/x86/kernel/e820.c +6 -0
index 4893d30ce438..912400161623 100644
@@ -538,6 +538,12 @@ u64 __init e820__range_update_table(struct e820_table *t, u64 start, u64 size,
        return __e820__range_update(t, start, size, old_type, new_type);
 }
 
+u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type,
+                                      enum e820_type new_type)
+{
+       return __e820__range_update(e820_table_firmware, start, size, old_type, new_type);
+}
+
 /* Remove a range of memory from the E820 table: */
 u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
 {
arch/x86/platform/efi/efi.c +9 -0
index 88a96816de9a..aa95f77d7a30 100644
@@ -171,6 +171,15 @@ static void __init do_add_efi_memmap(void)
        e820__update_table(e820_table);
 }
 
+/* Reserve firmware area if it was marked as RAM */
+void arch_update_firmware_area(u64 addr, u64 size)
+{
+       if (e820__get_entry_type(addr, addr + size) == E820_TYPE_RAM) {
+               e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
+               e820__update_table(e820_table_firmware);
+       }
+}
+
 /*
  * Given add_efi_memmap defaults to 0 and there is no alternative
  * e820 mechanism for soft-reserved memory, import the full EFI memory
drivers/firmware/efi/tpm.c +1 -1
index e8d69bd548f3..8e6e7131d718 100644
@@ -60,6 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
+       arch_update_firmware_area(efi.tpm_log, tbl_size);
        memblock_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
@@ -107,4 +108,3 @@ int __init efi_tpm_eventlog_init(void)
        early_memunmap(log_tbl, sizeof(*log_tbl));
        return ret;
 }
-
include/linux/efi.h +7 -1
index 6bf3c4fe8511..9c239cdff771 100644
@@ -1371,4 +1371,11 @@ extern struct blocking_notifier_head efivar_ops_nh;
 void efivars_generic_ops_register(void);
 void efivars_generic_ops_unregister(void);
 
+#ifdef CONFIG_X86_64
+void __init arch_update_firmware_area(u64 addr, u64 size);
+#else
+static inline void __init arch_update_firmware_area(u64 addr, u64 size)
+{
+}
+#endif
 #endif /* _LINUX_EFI_H */
--

2.43.5

maintainer
#4 lore raw
Quoted 47 lines · Usama Arif
Quoted 23 lines · Ard Biesheuvel
Quoted 22 lines · Usama Arif

Looking at the TPM spec [1]

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform FirmwareTCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface.

It looks like the firmware should pass this as reserved in e820 memory map. However, it doesn't seem to. The firmware being tested on is:
dmidecode -s bios-version
edk2-20240214-2.el9

When this area is not reserved, it comes up as usable in /sys/firmware/memmap. This means that kexec, which uses that memmap to find usable memory regions, can select the region where efi.tpm_log is and overwrite it and relocate_kernel.

Having a fix in firmware can be difficult to get through. As a secondary fix, this patch marks that region as reserved in e820_table_firmware if it is currently E820_TYPE_RAM so that kexec doesn't use it for kernel segments.

Quoted 7 lines
Quoted 3 lines

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

I would expect the EFI memory map to E820 conversion implemented in the EFI stub to take care of this.

So I have been making a prototype with EFI stub, and the unfinished version is looking like a horrible hack.

The only way to do this in libstub is to pass log_tbl all the way from efi_retrieve_tcg2_eventlog to efi_stub_entry and from there to setup_e820.
While going through the efi memory map and converting it to e820 table in setup_e820, you have to check if log_tbl falls in any of the ranges and if the range is E820_TYPE_RAM. If that condition is satisfied, then you have to split that range into 3. i.e. the E820_TYPE_RAM range before tpm_log, the tpm_log E820_TYPE_RESERVED range, and the E820_TYPE_RAM range after. There are no helper functions, so this splitting involves playing with a lot of pointers, and it looks quite ugly. I believe doing this way is more likely to introduce bugs.

Quoted 3 lines

If we are having to compensate for an EFI bug, would it make sense to do it in the way done in RFC and do it in kernel rather than libstub? It is simple and very likely to be bug free.

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

author
#5 lore raw
Quoted 48 lines · Ard Biesheuvel
Quoted 47 lines · Usama Arif
Quoted 23 lines · Ard Biesheuvel
Quoted 22 lines · Usama Arif

Looking at the TPM spec [1]

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform FirmwareTCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface.

It looks like the firmware should pass this as reserved in e820 memory map. However, it doesn't seem to. The firmware being tested on is:
dmidecode -s bios-version
edk2-20240214-2.el9

When this area is not reserved, it comes up as usable in /sys/firmware/memmap. This means that kexec, which uses that memmap to find usable memory regions, can select the region where efi.tpm_log is and overwrite it and relocate_kernel.

Having a fix in firmware can be difficult to get through. As a secondary fix, this patch marks that region as reserved in e820_table_firmware if it is currently E820_TYPE_RAM so that kexec doesn't use it for kernel segments.

Quoted 7 lines
Quoted 3 lines

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

I would expect the EFI memory map to E820 conversion implemented in the EFI stub to take care of this.

So I have been making a prototype with EFI stub, and the unfinished version is looking like a horrible hack.

The only way to do this in libstub is to pass log_tbl all the way from efi_retrieve_tcg2_eventlog to efi_stub_entry and from there to setup_e820.
While going through the efi memory map and converting it to e820 table in setup_e820, you have to check if log_tbl falls in any of the ranges and if the range is E820_TYPE_RAM. If that condition is satisfied, then you have to split that range into 3. i.e. the E820_TYPE_RAM range before tpm_log, the tpm_log E820_TYPE_RESERVED range, and the E820_TYPE_RAM range after. There are no helper functions, so this splitting involves playing with a lot of pointers, and it looks quite ugly. I believe doing this way is more likely to introduce bugs.

Quoted 6 lines
Quoted 3 lines

If we are having to compensate for an EFI bug, would it make sense to do it in the way done in RFC and do it in kernel rather than libstub? It is simple and very likely to be bug free.

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

EFI passes memory descriptors to libstub, libstub converts it to e820.

I believe the right behaviour should be that EFI creates an EFI_RESERVED_TYPE region for that TPM log memory. Then libstub would automatically convert that EFI_RESERVED_TYPE to E820_TYPE_RESERVED in setup_e820 [1].

[1] https://elixir.bootlin.com/linux/v6.10.9/source/drivers/firmware/efi/libstub/x86-stub.c#L573

#6 lore raw
In reply to Ard Biesheuvel

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Once the e820 table does not have the memory range for TPM event log, then the kernel is free to overwrite that memory region, causing corruptions all across the board.

From what I understand from the thread discussion, there are three ways to "solve" it:

1) Fix the EFI to pass the TPM event log memory as reserved.

2) Workaround it in libstub, and considering the TPM event log memory range when populating the e820 table. (As proposed in
https://lore.kernel.org/all/[email protected]/)

3) Workaround in later in the kernel, as proposed in
https://lore.kernel.org/all/[email protected]/

Please let me know if my understanding is flawed here.

Thank you!
--breno

maintainer
#7 lore raw
Quoted 7 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

Quoted 8 lines

I want to back up a little bit and make sure I am following the discussion.

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE).

Why do you think EFI should use EFI_RESERVED_TYPE in this case?

The EFI spec is very clear that EFI_RESERVED_TYPE really shouldn't be used for anything by EFI itself. It is quite common for EFI configuration tables to be passed as EfiRuntimeServicesData (SMBIOS), EfiBootServicesData (ESRT) or EFiAcpiReclaim (ACPI tables).

Reserved memory is mostly for memory that even the firmware does not know what it is for, i.e., particular platform specific uses.

In general, it is up to the OS to ensure that EFI configuration tables that it cares about should be reserved in the correct way.

Quoted 9 lines

Not having an entry for the
event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Once the e820 table does not have the memory range for TPM event log, then the kernel is free to overwrite that memory region, causing corruptions all across the board.

We shouldn't be relying on the E820 table for this.

Quoted 12 lines

From what I understand from the thread discussion, there are three ways to "solve" it:

1) Fix the EFI to pass the TPM event log memory as reserved.

2) Workaround it in libstub, and considering the TPM event log memory range when populating the e820 table. (As proposed in
https://lore.kernel.org/all/[email protected]/)

3) Workaround in later in the kernel, as proposed in
https://lore.kernel.org/all/[email protected]/

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");
author
#8 lore raw

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

	e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
	e820__update_table(e820_table);

while arch_update_firmware_area does:

	e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
	e820__update_table(e820_table_firmware);

Thanks,
Usama

maintainer
#9 lore raw
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

maintainer
#10 lore raw

(cc Dave)

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

#11 lore raw
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 51 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

I have not read all the conversations, let me have a look and response later.

The first glance about the patch is that I think the kexec_file_load syscall (default of latest kexec-tools) will not use e820_table_firmware AFAIK. it will only use e820_table_kexec.

Usama, can you confirm how you tested this?
kexec -c -l will use kexec_load syscall
kexec [-s] -l will use kexec_file_load syscall

Thanks
Dave

author
#12 lore raw
Quoted 58 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 51 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

Quoted 5 lines

I have not read all the conversations, let me have a look and response later.

The first glance about the patch is that I think the kexec_file_load syscall (default of latest kexec-tools) will not use e820_table_firmware AFAIK. it will only use e820_table_kexec.

I initially thought that as well. But it looks like kexec just reads /sys/firmware/memmap

https://github.com/horms/kexec-tools/blob/main/kexec/firmware_memmap.h#L29

which is e820_table_firmware.

The patch that Ard sent in https://lore.kernel.org/all/[email protected]/
is the right approach to it I believe, and I dont see the issue anymore after applying that patch.

Quoted 3 lines

Usama, can you confirm how you tested this?
kexec -c -l will use kexec_load syscall

I am currently testing in my VM setup with kexec_load. But production is running kexec_file_load and has the same issue.

Thanks,
Usama

Quoted 5 lines

kexec [-s] -l will use kexec_file_load syscall

Thanks
Dave

#13 lore raw
Quoted 68 lines · Usama Arif
Quoted 58 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 51 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

Quoted 5 lines

I have not read all the conversations, let me have a look and response later.

The first glance about the patch is that I think the kexec_file_load syscall (default of latest kexec-tools) will not use e820_table_firmware AFAIK. it will only use e820_table_kexec.

Quoted 5 lines

I initially thought that as well. But it looks like kexec just reads /sys/firmware/memmap

https://github.com/horms/kexec-tools/blob/main/kexec/firmware_memmap.h#L29

which is e820_table_firmware.

That piece of code is only used by kexec_load

Quoted 10 lines

The patch that Ard sent in https://lore.kernel.org/all/[email protected]/
is the right approach to it I believe, and I dont see the issue anymore after applying that patch.

Quoted 3 lines

Usama, can you confirm how you tested this?
kexec -c -l will use kexec_load syscall

I am currently testing in my VM setup with kexec_load. But production is running kexec_file_load and has the same issue.

Ok, I mean efi_mem_reserve should be able to work if you retest with kexec_file_load.

Quoted 10 lines

Thanks,
Usama

Quoted 5 lines

kexec [-s] -l will use kexec_file_load syscall

Thanks
Dave

#14 lore raw
Quoted 83 lines · Dave Young
Quoted 68 lines · Usama Arif
Quoted 58 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 51 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

Quoted 5 lines

I have not read all the conversations, let me have a look and response later.

The first glance about the patch is that I think the kexec_file_load syscall (default of latest kexec-tools) will not use e820_table_firmware AFAIK. it will only use e820_table_kexec.

Quoted 5 lines

I initially thought that as well. But it looks like kexec just reads /sys/firmware/memmap

https://github.com/horms/kexec-tools/blob/main/kexec/firmware_memmap.h#L29

which is e820_table_firmware.

That piece of code is only used by kexec_load

Quoted 5 lines

The patch that Ard sent in https://lore.kernel.org/all/[email protected]/
is the right approach to it I believe, and I dont see the issue anymore after applying that patch.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Usama, can you confirm how you tested this?
kexec -c -l will use kexec_load syscall

I am currently testing in my VM setup with kexec_load. But production is running kexec_file_load and has the same issue.

Ok, I mean efi_mem_reserve should be able to work if you retest with kexec_file_load.

Hold on, I'm not sure about above :(

checking the efi_arch_mem_reserve(), currently it updates the e820 table, if you update the e820_table_kexec and e820_table_firmware then I think both kexec_load and kexec_file_load will work.

Anyway I was not aware very much about the firmware e820 tables and
kexec tables when they were created. I suspect that a cleanup and
revisit is needed. I will have a look at that.

For Ard's fix to allocate it as ACPI memory, I think it should be good and simpler.

Quoted 11 lines
Quoted 11 lines

Thanks,
Usama

Quoted 5 lines

kexec [-s] -l will use kexec_file_load syscall

Thanks
Dave

author
#15 lore raw
Quoted 95 lines · Dave Young
Quoted 83 lines · Dave Young
Quoted 68 lines · Usama Arif
Quoted 58 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 51 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

Quoted 5 lines

I have not read all the conversations, let me have a look and response later.

The first glance about the patch is that I think the kexec_file_load syscall (default of latest kexec-tools) will not use e820_table_firmware AFAIK. it will only use e820_table_kexec.

Quoted 5 lines

I initially thought that as well. But it looks like kexec just reads /sys/firmware/memmap

https://github.com/horms/kexec-tools/blob/main/kexec/firmware_memmap.h#L29

which is e820_table_firmware.

That piece of code is only used by kexec_load

Quoted 5 lines

The patch that Ard sent in https://lore.kernel.org/all/[email protected]/
is the right approach to it I believe, and I dont see the issue anymore after applying that patch.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Usama, can you confirm how you tested this?
kexec -c -l will use kexec_load syscall

I am currently testing in my VM setup with kexec_load. But production is running kexec_file_load and has the same issue.

Ok, I mean efi_mem_reserve should be able to work if you retest with kexec_file_load.

Hold on, I'm not sure about above :(

Quoted 7 lines

checking the efi_arch_mem_reserve(), currently it updates the e820 table, if you update the e820_table_kexec and e820_table_firmware then I think both kexec_load and kexec_file_load will work.

Anyway I was not aware very much about the firmware e820 tables and
kexec tables when they were created. I suspect that a cleanup and
revisit is needed. I will have a look at that.

Yes, I feel like there is one too many tables! From reading the code I understand that /sys/firmware/memmap should contain the untouched map at time of boot, i.e. e820_table_firmware. But I would be in favour of getting rid of e820_table_firmware, and just having e820_table_kexec. And /sys/firmware/memmap gets data from e820_table_kexec.

Quoted 4 lines

For Ard's fix to allocate it as ACPI memory, I think it should be good and simpler.

Agreed!

Quoted 12 lines
Quoted 11 lines
Quoted 11 lines

Thanks,
Usama

Quoted 5 lines

kexec [-s] -l will use kexec_file_load syscall

Thanks
Dave

#16 lore raw

Hi Usama,

Quoted 9 lines
Quoted 3 lines

Anyway I was not aware very much about the firmware e820 tables and
kexec tables when they were created. I suspect that a cleanup and
revisit is needed. I will have a look at that.

Yes, I feel like there is one too many tables! From reading the code I understand that /sys/firmware/memmap should contain the untouched map at time of boot, i.e. e820_table_firmware. But I would be in favour of getting rid of e820_table_firmware, and just having e820_table_kexec. And /sys/firmware/memmap gets data from e820_table_kexec.

Agreed, I have the same feelings.

Thanks
Dave

#17 lore raw
In reply to Dave Young
Quoted 53 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 46 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 41 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

I have not read all the conversations, let me have a look and response later.

I'm still confused after reading the code about why this issue can still happen with a efi_mem_reserve.
Usama, Breno, could any of you share the exact steps on how to reproduce this issue with a kvm guest?

Thanks
Daev

maintainer
#18 lore raw
Quoted 61 lines · Dave Young
Quoted 59 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 51 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 45 lines · Ard Biesheuvel
Quoted 24 lines · Usama Arif

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 13 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

Quoted 8 lines
Quoted 2 lines

I have not read all the conversations, let me have a look and response later.

I'm still confused after reading the code about why this issue can still happen with a efi_mem_reserve.
Usama, Breno, could any of you share the exact steps on how to reproduce this issue with a kvm guest?

The code does not use efi_mem_reserve() only memblock_reserve().

#19 lore raw
Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Dave Young
Quoted 29 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 22 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 17 lines · Ard Biesheuvel
Quoted 15 lines · Usama Arif
Quoted 11 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 2 lines
drivers/firmware/efi/tpm.c +0 -0
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

Updating e820_table should be good enough, it depends on where the corruption is happening.

kexec will find a suitable memory for the kernel via searching through
the system ram resources. So efi_mem_reserve will update e820_table,
then reserve in the resources list as E820_TYPE_RESERVED, thus it should not be a problem.
During the 2nd kernel boot phase, it is carried as EFI_LOADER_DATA with EFI_MEMORY_RUNTIME attribute, I think it is also fine, and later efi_mem_reserve will be called as what have been done in previous kernel.

So I think no need to update the e820_table_kexec and e820_table_firmware

Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 19 lines
Quoted 19 lines
Quoted 11 lines

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Quoted 5 lines
Quoted 4 lines

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

I have not read all the conversations, let me have a look and response later.

Quoted 7 lines
Quoted 5 lines

I'm still confused after reading the code about why this issue can still happen with a efi_mem_reserve.
Usama, Breno, could any of you share the exact steps on how to reproduce this issue with a kvm guest?

The code does not use efi_mem_reserve() only memblock_reserve().

Yes, I see this, I just thought that Usama tested with changes to efi_mem_reserve and it still did not work, this is what I'm confused about.

But maybe Usama did not test and only checked the code and assumed that we have to update the e820_table_kexec and e820_table_firmware. See my reply inline above.

Thanks
Dave

Quoted 1 line
#20 lore raw
Quoted 53 lines · Dave Young
Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Dave Young
Quoted 29 lines · Dave Young
Quoted 2 lines · Ard Biesheuvel

(cc Dave)

Thanks for ccing me.

Quoted 22 lines

Full thread here:
https://lore.kernel.org/all/CAMj1kXG1hbiafKRyC5qM1Vj5X7x-dmLndqqo2AYnHMRxDz-80w@mail.gmail.com/T/#u

Quoted 17 lines · Ard Biesheuvel
Quoted 15 lines · Usama Arif
Quoted 11 lines · Ard Biesheuvel

Does the below help at all?

drivers/firmware/efi/tpm.c +1 -1
@@ -60,7 +60,7 @@ int __init efi_tpm_eventlog_init(void)
        }
 
        tbl_size = sizeof(*log_tbl) + log_tbl->size;
-       memblock_reserve(efi.tpm_log, tbl_size);
+       efi_mem_reserve(efi.tpm_log, tbl_size);
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 66

Quoted 2 lines
drivers/firmware/efi/tpm.c +0 -0
        if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
                pr_info("TPM Final Events table not present\n");

Unfortunately not. efi_mem_reserve updates e820_table, while kexec looks at /sys/firmware/memmap which is e820_table_firmware.

Updating e820_table should be good enough, it depends on where the corruption is happening.

kexec will find a suitable memory for the kernel via searching through
the system ram resources. So efi_mem_reserve will update e820_table,
then reserve in the resources list as E820_TYPE_RESERVED, thus it should not be a problem.
During the 2nd kernel boot phase, it is carried as EFI_LOADER_DATA with EFI_MEMORY_RUNTIME attribute, I think it is also fine, and later efi_mem_reserve will be called as what have been done in previous kernel.

Quoted 1 line

So I think no need to update the e820_table_kexec and e820_table_firmware

Hmm, oops, I again forgot the kexec_load code in userspace kexec-tools. The kexec-tools code still searching for memory ranges from e820_table_firmware

Quoted 37 lines
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 20 lines
Quoted 20 lines
Quoted 5 lines

arch_update_firmware_area introduced in the RFC patch does the same thing as efi_mem_reserve does at its end, just with e820_table_firmware instead of e820_table.
i.e. efi_mem_reserve does:

Quoted 7 lines
        e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table);

while arch_update_firmware_area does:

        e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
        e820__update_table(e820_table_firmware);

Shame.

Using efi_mem_reserve() is appropriate here in any case, but I guess kexec on x86 needs to be fixed to juggle the EFI memory map, memblock table, and 3 (!) versions of the E820 table in the correct way (e820_table, e820_table_kexec and e820_table_firmware)

Quoted 5 lines
Quoted 4 lines

Perhaps we can put this additional logic in x86's implementation of efi_arch_mem_reserve()? AFAICT, all callers of efi_mem_reserve() deal with configuration tables produced by the firmware that may not be reserved correctly if kexec looks at e820_table_firmware[] only.

I have not read all the conversations, let me have a look and response later.

Quoted 7 lines
Quoted 5 lines

I'm still confused after reading the code about why this issue can still happen with a efi_mem_reserve.
Usama, Breno, could any of you share the exact steps on how to reproduce this issue with a kvm guest?

The code does not use efi_mem_reserve() only memblock_reserve().

Quoted 7 lines

Yes, I see this, I just thought that Usama tested with changes to efi_mem_reserve and it still did not work, this is what I'm confused about.

But maybe Usama did not test and only checked the code and assumed that we have to update the e820_table_kexec and e820_table_firmware. See my reply inline above.

Please ignore the above comment. The userspace code does need the
e820_table_firmware.
So the best way to make it easier is to clean up the e820 tables and maintain only one table then the kernel kexec_file_load behavior will
be the same as the userspace. But need a closer look about the
details, eg. if the hibernate (mentioned in code comment) is happy.

Or to change userspace to go through the /proc/iomem instead of checking the /sys/firmware/memmap

Quoted 4 lines

Thanks
Dave

Quoted 1 line
#21 lore raw
In reply to Ard Biesheuvel
Quoted 21 lines · Ard Biesheuvel
Quoted 4 lines · Breno Leitao
Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

Quoted 8 lines

I want to back up a little bit and make sure I am following the discussion.

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE).

Why do you think EFI should use EFI_RESERVED_TYPE in this case?

The EFI spec is very clear that EFI_RESERVED_TYPE really shouldn't be used for anything by EFI itself. It is quite common for EFI configuration tables to be passed as EfiRuntimeServicesData (SMBIOS), EfiBootServicesData (ESRT) or EFiAcpiReclaim (ACPI tables).

Quoted 5 lines

Reserved memory is mostly for memory that even the firmware does not know what it is for, i.e., particular platform specific uses.

In general, it is up to the OS to ensure that EFI configuration tables that it cares about should be reserved in the correct way.

Thanks for the explanation.

So, if I understand what you meant here, the TPM event log memory range shouldn't be listed as a memory region in EFI memory map (as passed by the firmware to the OS).

Hence, this is not a EFI firmware bug, but a OS/Kernel bug.

Am I correct with the statements above?

Another question, looking at the Spec[1] it says:

If the ACPI TPM2 table contains the address and size of the Platform Firmware TCG log, firmware “pins” the memory associated with the Platform Firmware TCG log, and reports this memory as “Reserved” memory via the INT 15h/E820 interface

What is the 'firmware' in the statement above, that says that reports the memory as reserved? (Is it libstub?!)

Thank you so much for guiding us here,
--breno

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-ClientPlatform_Profile_for_TPM_2p0_Systems_v49_161114_public-review.pdf

maintainer
#22 lore raw
Quoted 31 lines · Breno Leitao
Quoted 21 lines · Ard Biesheuvel
Quoted 4 lines · Breno Leitao
Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

Quoted 8 lines

I want to back up a little bit and make sure I am following the discussion.

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE).

Why do you think EFI should use EFI_RESERVED_TYPE in this case?

The EFI spec is very clear that EFI_RESERVED_TYPE really shouldn't be used for anything by EFI itself. It is quite common for EFI configuration tables to be passed as EfiRuntimeServicesData (SMBIOS), EfiBootServicesData (ESRT) or EFiAcpiReclaim (ACPI tables).

Quoted 5 lines

Reserved memory is mostly for memory that even the firmware does not know what it is for, i.e., particular platform specific uses.

In general, it is up to the OS to ensure that EFI configuration tables that it cares about should be reserved in the correct way.

Thanks for the explanation.

Quoted 8 lines

So, if I understand what you meant here, the TPM event log memory range shouldn't be listed as a memory region in EFI memory map (as passed by the firmware to the OS).

Hence, this is not a EFI firmware bug, but a OS/Kernel bug.

Am I correct with the statements above?

No, not entirely. But I also missed the face that this table is actually created by the EFI stub in Linux, not the firmware. It is *not* the same memory region that the TPM2 ACPI table describes, and so what the various specs say about that is entirely irrelevant.

The TPM event log configuration table is created by the EFI stub, which uses the TCG2::GetEventLog () protocol method to obtain it. This must be done by the EFI stub because these protocols will no longer be available once the OS boots. But the data is not used by the EFI stub, only by the OS, which is why it is passed in memory like this.

The memory in question is allocated as EFI_LOADER_DATA, and so we are relying on the OS to know that this memory is special, and needs to be preserved.

I think the solution here is to use a different memory type:

drivers/firmware/efi/libstub/tpm.c +0 -0
@@ -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_LOADER_DATA,
+       status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
                             sizeof(*log_tbl) + log_size, (void **)&log_tbl);

        if (status != EFI_SUCCESS) {

which will be treated appropriately by the existing EFI-to-E820 conversion logic.

author
#23 lore raw
Quoted 71 lines · Ard Biesheuvel
Quoted 31 lines · Breno Leitao
Quoted 21 lines · Ard Biesheuvel
Quoted 4 lines · Breno Leitao
Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

Quoted 8 lines

I want to back up a little bit and make sure I am following the discussion.

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE).

Why do you think EFI should use EFI_RESERVED_TYPE in this case?

The EFI spec is very clear that EFI_RESERVED_TYPE really shouldn't be used for anything by EFI itself. It is quite common for EFI configuration tables to be passed as EfiRuntimeServicesData (SMBIOS), EfiBootServicesData (ESRT) or EFiAcpiReclaim (ACPI tables).

Quoted 5 lines

Reserved memory is mostly for memory that even the firmware does not know what it is for, i.e., particular platform specific uses.

In general, it is up to the OS to ensure that EFI configuration tables that it cares about should be reserved in the correct way.

Thanks for the explanation.

Quoted 8 lines

So, if I understand what you meant here, the TPM event log memory range shouldn't be listed as a memory region in EFI memory map (as passed by the firmware to the OS).

Hence, this is not a EFI firmware bug, but a OS/Kernel bug.

Am I correct with the statements above?

No, not entirely. But I also missed the face that this table is actually created by the EFI stub in Linux, not the firmware. It is *not* the same memory region that the TPM2 ACPI table describes, and so what the various specs say about that is entirely irrelevant.

The TPM event log configuration table is created by the EFI stub, which uses the TCG2::GetEventLog () protocol method to obtain it. This must be done by the EFI stub because these protocols will no longer be available once the OS boots. But the data is not used by the EFI stub, only by the OS, which is why it is passed in memory like this.

The memory in question is allocated as EFI_LOADER_DATA, and so we are relying on the OS to know that this memory is special, and needs to be preserved.

I think the solution here is to use a different memory type:

drivers/firmware/efi/libstub/tpm.c +0 -0
@@ -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. */
drivers/firmware/efi/libstub/tpm.c +1 -1
-       status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
+       status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
                             sizeof(*log_tbl) + log_size, (void **)&log_tbl);
 
        if (status != EFI_SUCCESS) {
Quoted 2 lines

which will be treated appropriately by the existing EFI-to-E820 conversion logic.

I have tested above diff, and it works! No memory corruption.

The region comes up as ACPI data:

[    0.000000] BIOS-e820: [mem 0x000000007fb6d000-0x000000007fb7efff] ACPI data                                                                                                                               

and kexec doesnt interfere with it.

Thanks,
Usama

maintainer
#24 lore raw
Quoted 80 lines · Usama Arif
Quoted 71 lines · Ard Biesheuvel
Quoted 31 lines · Breno Leitao
Quoted 21 lines · Ard Biesheuvel
Quoted 4 lines · Breno Leitao
Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

Quoted 8 lines

I want to back up a little bit and make sure I am following the discussion.

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE).

Why do you think EFI should use EFI_RESERVED_TYPE in this case?

The EFI spec is very clear that EFI_RESERVED_TYPE really shouldn't be used for anything by EFI itself. It is quite common for EFI configuration tables to be passed as EfiRuntimeServicesData (SMBIOS), EfiBootServicesData (ESRT) or EFiAcpiReclaim (ACPI tables).

Quoted 5 lines

Reserved memory is mostly for memory that even the firmware does not know what it is for, i.e., particular platform specific uses.

In general, it is up to the OS to ensure that EFI configuration tables that it cares about should be reserved in the correct way.

Thanks for the explanation.

Quoted 8 lines

So, if I understand what you meant here, the TPM event log memory range shouldn't be listed as a memory region in EFI memory map (as passed by the firmware to the OS).

Hence, this is not a EFI firmware bug, but a OS/Kernel bug.

Am I correct with the statements above?

No, not entirely. But I also missed the face that this table is actually created by the EFI stub in Linux, not the firmware. It is *not* the same memory region that the TPM2 ACPI table describes, and so what the various specs say about that is entirely irrelevant.

The TPM event log configuration table is created by the EFI stub, which uses the TCG2::GetEventLog () protocol method to obtain it. This must be done by the EFI stub because these protocols will no longer be available once the OS boots. But the data is not used by the EFI stub, only by the OS, which is why it is passed in memory like this.

The memory in question is allocated as EFI_LOADER_DATA, and so we are relying on the OS to know that this memory is special, and needs to be preserved.

I think the solution here is to use a different memory type:

drivers/firmware/efi/libstub/tpm.c +0 -0
@@ -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. */
drivers/firmware/efi/libstub/tpm.c +1 -1
-       status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
+       status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
                             sizeof(*log_tbl) + log_size, (void **)&log_tbl);
 
        if (status != EFI_SUCCESS) {
Quoted 2 lines

which will be treated appropriately by the existing EFI-to-E820 conversion logic.

I have tested above diff, and it works! No memory corruption.

Quoted 5 lines

The region comes up as ACPI data:

[    0.000000] BIOS-e820: [mem 0x000000007fb6d000-0x000000007fb7efff] ACPI data

and kexec doesnt interfere with it.

Thanks, I'll take that as a tested-by

maintainer
#25 lore raw
In reply to Breno Leitao
Quoted 9 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

Quoted 8 lines

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Wait, that's not correct. The TPM log is in memory that doesn't survive ExitBootServices (by design in case the OS doesn't care about it). So the EFI stub actually copies it over to a new configuration table that is in reserved memory before it calls ExitBootServices. This new copy should be in kernel reserved memory regardless of its e820 map status.

Regards,

James

#26 lore raw

Hello James,

Quoted 19 lines · James Bottomley
Quoted 9 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

Quoted 8 lines

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Quoted 6 lines

Wait, that's not correct. The TPM log is in memory that doesn't survive ExitBootServices (by design in case the OS doesn't care about it). So the EFI stub actually copies it over to a new configuration table that is in reserved memory before it calls ExitBootServices. This new copy should be in kernel reserved memory regardless of its e820 map status.

First of all, thanks for clarifying some points here.

How should the TPM log table be passed to the next kernel when kexecing() since it didn't surive ExitBootServices?

maintainer
#27 lore raw
Quoted 29 lines · Breno Leitao

Hello James,

Quoted 19 lines · James Bottomley
Quoted 9 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

Quoted 8 lines

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Quoted 6 lines

Wait, that's not correct.  The TPM log is in memory that doesn't survive ExitBootServices (by design in case the OS doesn't care about it).  So the EFI stub actually copies it over to a new configuration table that is in reserved memory before it calls ExitBootServices. This new copy should be in kernel reserved memory regardless of its e820 map status.

Quoted 4 lines

First of all, thanks for clarifying some points here.

How should the TPM log table be passed to the next kernel when kexecing() since it didn't surive ExitBootServices?

I've no idea. I'm assuming you don't elaborately reconstruct the EFI boot services, so you can't enter the EFI boot stub before ExitBootServices is called? So I'd guess you want to preserve the EFI table that copied the TPM data in to kernel memory.

James

#28 lore raw
Quoted 32 lines · James Bottomley
Quoted 31 lines · Breno Leitao

Hello James,

Quoted 19 lines · James Bottomley
Quoted 9 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

Quoted 8 lines

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Quoted 6 lines

Wait, that's not correct.  The TPM log is in memory that doesn't survive ExitBootServices (by design in case the OS doesn't care about it).  So the EFI stub actually copies it over to a new configuration table that is in reserved memory before it calls ExitBootServices. This new copy should be in kernel reserved memory regardless of its e820 map status.

First of all, thanks for clarifying some points here.

Quoted 7 lines
Quoted 2 lines

How should the TPM log table be passed to the next kernel when kexecing() since it didn't surive ExitBootServices?

I've no idea. I'm assuming you don't elaborately reconstruct the EFI boot services, so you can't enter the EFI boot stub before ExitBootServices is called? So I'd guess you want to preserve the EFI table that copied the TPM data in to kernel memory.

This leaves two practical questions if I have been following everything correctly.

1) How to get kexec to avoid picking that memory for the new kernel to run in before it initializes itself. (AKA the getting stomped by relocate kernel problem).

2) How to point the new kernel to preserved tpm_log.

This recommendation is from memory so it may be a bit off but the general structure should work. The idea is as follows.

- Pass the information between kernels.

It is probably simplest for the kernel to have a command line option that tells the kernel the address and size of the tpm_log.

We have a couple of mechanisms here. Assuming you are loading a bzImage with kexec_file_load you should be able to have the in kernel loader to add those arguments to the kernel command line.

- Ensure that when the loader is finding an address to load the new kernel it treats the address of the tpm_log as unavailable.

Does that sound like a doable plan?

Eric

maintainer
#29 lore raw

Hi Eric,

Thanks for chiming in.

Quoted 56 lines · Eric W. Biederman
Quoted 32 lines · James Bottomley
Quoted 31 lines · Breno Leitao

Hello James,

Quoted 19 lines · James Bottomley
Quoted 9 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

Quoted 8 lines

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Quoted 6 lines

Wait, that's not correct. The TPM log is in memory that doesn't survive ExitBootServices (by design in case the OS doesn't care about it). So the EFI stub actually copies it over to a new configuration table that is in reserved memory before it calls ExitBootServices. This new copy should be in kernel reserved memory regardless of its e820 map status.

First of all, thanks for clarifying some points here.

Quoted 7 lines
Quoted 2 lines

How should the TPM log table be passed to the next kernel when kexecing() since it didn't surive ExitBootServices?

I've no idea. I'm assuming you don't elaborately reconstruct the EFI boot services, so you can't enter the EFI boot stub before ExitBootServices is called? So I'd guess you want to preserve the EFI table that copied the TPM data in to kernel memory.

This leaves two practical questions if I have been following everything correctly.

1) How to get kexec to avoid picking that memory for the new kernel to run in before it initializes itself. (AKA the getting stomped by relocate kernel problem).

2) How to point the new kernel to preserved tpm_log.

This recommendation is from memory so it may be a bit off but the general structure should work. The idea is as follows.

Quoted 9 lines

- Pass the information between kernels.

It is probably simplest for the kernel to have a command line option that tells the kernel the address and size of the tpm_log.

We have a couple of mechanisms here. Assuming you are loading a bzImage with kexec_file_load you should be able to have the in kernel loader to add those arguments to the kernel command line.

This shouldn't be necessary, and I think it is actively harmful to keep inventing special ways for the kexec kernel to learn about these things that deviate from the methods used by the first kernel. This is how we ended up with 5 sources of truth for the physical memory map (EFI memory map, memblock and 3 different versions of the e820 memory map).

We should try very hard to make kexec idempotent, and reuse the existing methods where possible. In this case, the EFI configuration table is already being exposed to the kexec kernel, which describes the base of the allocation. The size of the allocation can be derived from the table header.

Quoted 3 lines

- Ensure that when the loader is finding an address to load the new kernel it treats the address of the tpm_log as unavailable.

The TPM log is a table created by the EFI stub loader, which is part of the kernel. So if we need to tweak this for kexec's benefit, I'd prefer changing it in a way that can accommodate the first kernel too. However, I think the current method already has that property so I don't think we need to do anything (modulo fixing the bug)

That said, I am doubtful that the kexec kernel can make meaningful use of the TPM log to begin with, given that the TPM will be out of sync at this point. But it is still better to keep it for symmetry, letting the higher level kexec/kdump logic running in user space reason about whether the TPM log has any value to it.

Signature · 1 line
Ard.
#30 lore raw
Quoted 3 lines · Ard Biesheuvel

Hi Eric,

Thanks for chiming in.

It just looked like after James gave some expert input the conversation got stuck, so I am just trying to move it along.

I don't think anyone knows what this whole elephant looks like, which makes solving the problem tricky.

Quoted 84 lines · Eric W. Biederman
Quoted 56 lines
Quoted 32 lines · James Bottomley
Quoted 31 lines · Breno Leitao

Hello James,

Quoted 19 lines · James Bottomley
Quoted 9 lines · Breno Leitao

Hello Ard,

Quoted 2 lines · Ard Biesheuvel

I don't see how this could be an EFI bug, given that it does not deal with E820 tables at all.

I want to back up a little bit and make sure I am following the discussion.

Quoted 8 lines

From what I understand from previous discussion, we have an EFI bug as the root cause of this issue.

This happens because the EFI does NOT mark the EFI TPM event log memory region as reserved (EFI_RESERVED_TYPE). Not having an entry for the event table memory in EFI memory mapped, then libstub will ignore it completely (the TPM event log memory range) and not populate e820 table with it.

Quoted 6 lines

Wait, that's not correct. The TPM log is in memory that doesn't survive ExitBootServices (by design in case the OS doesn't care about it). So the EFI stub actually copies it over to a new configuration table that is in reserved memory before it calls ExitBootServices. This new copy should be in kernel reserved memory regardless of its e820 map status.

First of all, thanks for clarifying some points here.

Quoted 7 lines
Quoted 2 lines

How should the TPM log table be passed to the next kernel when kexecing() since it didn't surive ExitBootServices?

I've no idea. I'm assuming you don't elaborately reconstruct the EFI boot services, so you can't enter the EFI boot stub before ExitBootServices is called? So I'd guess you want to preserve the EFI table that copied the TPM data in to kernel memory.

This leaves two practical questions if I have been following everything correctly.

1) How to get kexec to avoid picking that memory for the new kernel to run in before it initializes itself. (AKA the getting stomped by relocate kernel problem).

2) How to point the new kernel to preserved tpm_log.

This recommendation is from memory so it may be a bit off but the general structure should work. The idea is as follows.

Quoted 9 lines

- Pass the information between kernels.

It is probably simplest for the kernel to have a command line option that tells the kernel the address and size of the tpm_log.

We have a couple of mechanisms here. Assuming you are loading a bzImage with kexec_file_load you should be able to have the in kernel loader to add those arguments to the kernel command line.

This shouldn't be necessary, and I think it is actively harmful to keep inventing special ways for the kexec kernel to learn about these things that deviate from the methods used by the first kernel. This is how we ended up with 5 sources of truth for the physical memory map (EFI memory map, memblock and 3 different versions of the e820 memory map).

We should try very hard to make kexec idempotent, and reuse the existing methods where possible. In this case, the EFI configuration table is already being exposed to the kexec kernel, which describes the base of the allocation. The size of the allocation can be derived from the table header.

Quoted 3 lines

- Ensure that when the loader is finding an address to load the new kernel it treats the address of the tpm_log as unavailable.

Quoted 5 lines

The TPM log is a table created by the EFI stub loader, which is part of the kernel. So if we need to tweak this for kexec's benefit, I'd prefer changing it in a way that can accommodate the first kernel too. However, I think the current method already has that property so I don't think we need to do anything (modulo fixing the bug)

I am fine with not inventing a new mechanism, but I think we need to reuse whatever mechanism the stub loader uses to pass it's table to the kernel. Not the EFI table that disappears at ExitBootServices().

Quoted 5 lines

That said, I am doubtful that the kexec kernel can make meaningful use of the TPM log to begin with, given that the TPM will be out of sync at this point. But it is still better to keep it for symmetry, letting the higher level kexec/kdump logic running in user space reason about whether the TPM log has any value to it.

Someone seems to think so or there would not be a complaint that it is getting corrupted.

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

Eric

maintainer
#31 lore raw
Quoted 15 lines · Eric W. Biederman
Quoted 3 lines · Ard Biesheuvel

Hi Eric,

Thanks for chiming in.

It just looked like after James gave some expert input the conversation got stuck, so I am just trying to move it along.

I don't think anyone knows what this whole elephant looks like, which makes solving the problem tricky.

Quoted 2 lines · Eric W. Biederman
Quoted 1 line

...

Quoted 47 lines
Quoted 41 lines
Quoted 14 lines

This leaves two practical questions if I have been following everything correctly.

1) How to get kexec to avoid picking that memory for the new kernel to run in before it initializes itself. (AKA the getting stomped by relocate kernel problem).

2) How to point the new kernel to preserved tpm_log.

This recommendation is from memory so it may be a bit off but the general structure should work. The idea is as follows.

Quoted 9 lines

- Pass the information between kernels.

It is probably simplest for the kernel to have a command line option that tells the kernel the address and size of the tpm_log.

We have a couple of mechanisms here. Assuming you are loading a bzImage with kexec_file_load you should be able to have the in kernel loader to add those arguments to the kernel command line.

This shouldn't be necessary, and I think it is actively harmful to keep inventing special ways for the kexec kernel to learn about these things that deviate from the methods used by the first kernel. This is how we ended up with 5 sources of truth for the physical memory map (EFI memory map, memblock and 3 different versions of the e820 memory map).

We should try very hard to make kexec idempotent, and reuse the existing methods where possible. In this case, the EFI configuration table is already being exposed to the kexec kernel, which describes the base of the allocation. The size of the allocation can be derived from the table header.

Quoted 3 lines

- Ensure that when the loader is finding an address to load the new kernel it treats the address of the tpm_log as unavailable.

Quoted 5 lines

The TPM log is a table created by the EFI stub loader, which is part of the kernel. So if we need to tweak this for kexec's benefit, I'd prefer changing it in a way that can accommodate the first kernel too. However, I think the current method already has that property so I don't think we need to do anything (modulo fixing the bug)

Quoted 5 lines

I am fine with not inventing a new mechanism, but I think we need to reuse whatever mechanism the stub loader uses to pass it's table to the kernel. Not the EFI table that disappears at ExitBootServices().

Not sure what you mean here - the EFI table that gets clobbered by kexec *is* the table that is created by the stub loader to pass the TPM log to the kernel. Not sure what alternative you have in mind here.

Quoted 9 lines
Quoted 5 lines

That said, I am doubtful that the kexec kernel can make meaningful use of the TPM log to begin with, given that the TPM will be out of sync at this point. But it is still better to keep it for symmetry, letting the higher level kexec/kdump logic running in user space reason about whether the TPM log has any value to it.

Someone seems to think so or there would not be a complaint that it is getting corrupted.

No. The problem is that the size of the table is *in* the table, and so if it gets corrupted, the code that attempts to memblock_reserve() it goes off into the weeds. But that does not imply there is a point to having access to this table from a kexec kernel in the first place.

Quoted 4 lines

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

kexec as a bootloader under TPM based measured boot will need to do a lot more than pass the firmware's event log to the next kernel. I'd expect a properly engineered kexec to replace this table entirely, and include the hashes of the assets it has loaded and measured into the respective PCRs.

But let's stick to solving the actual issue here, rather than philosophize on how kexec might work in this context.

#32 lore raw
Quoted 70 lines · Ard Biesheuvel
Quoted 15 lines · Eric W. Biederman
Quoted 3 lines · Ard Biesheuvel

Hi Eric,

Thanks for chiming in.

It just looked like after James gave some expert input the conversation got stuck, so I am just trying to move it along.

I don't think anyone knows what this whole elephant looks like, which makes solving the problem tricky.

Quoted 2 lines · Eric W. Biederman
Quoted 1 line

...

Quoted 47 lines
Quoted 41 lines
Quoted 14 lines

This leaves two practical questions if I have been following everything correctly.

1) How to get kexec to avoid picking that memory for the new kernel to run in before it initializes itself. (AKA the getting stomped by relocate kernel problem).

2) How to point the new kernel to preserved tpm_log.

This recommendation is from memory so it may be a bit off but the general structure should work. The idea is as follows.

Quoted 9 lines

- Pass the information between kernels.

It is probably simplest for the kernel to have a command line option that tells the kernel the address and size of the tpm_log.

We have a couple of mechanisms here. Assuming you are loading a bzImage with kexec_file_load you should be able to have the in kernel loader to add those arguments to the kernel command line.

This shouldn't be necessary, and I think it is actively harmful to keep inventing special ways for the kexec kernel to learn about these things that deviate from the methods used by the first kernel. This is how we ended up with 5 sources of truth for the physical memory map (EFI memory map, memblock and 3 different versions of the e820 memory map).

We should try very hard to make kexec idempotent, and reuse the existing methods where possible. In this case, the EFI configuration table is already being exposed to the kexec kernel, which describes the base of the allocation. The size of the allocation can be derived from the table header.

Quoted 3 lines

- Ensure that when the loader is finding an address to load the new kernel it treats the address of the tpm_log as unavailable.

Quoted 5 lines

The TPM log is a table created by the EFI stub loader, which is part of the kernel. So if we need to tweak this for kexec's benefit, I'd prefer changing it in a way that can accommodate the first kernel too. However, I think the current method already has that property so I don't think we need to do anything (modulo fixing the bug)

Quoted 5 lines

I am fine with not inventing a new mechanism, but I think we need to reuse whatever mechanism the stub loader uses to pass it's table to the kernel. Not the EFI table that disappears at ExitBootServices().

Quoted 4 lines

Not sure what you mean here - the EFI table that gets clobbered by kexec *is* the table that is created by the stub loader to pass the TPM log to the kernel. Not sure what alternative you have in mind here.

I was referring to whatever the EFI table that James Bottomley mentioned that I presume the stub loader reads from when the stub loader constructs the tpm_log that is passed to the kernel.

So I believe we are in agreement of everything except terminology.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

That said, I am doubtful that the kexec kernel can make meaningful use of the TPM log to begin with, given that the TPM will be out of sync at this point. But it is still better to keep it for symmetry, letting the higher level kexec/kdump logic running in user space reason about whether the TPM log has any value to it.

Quoted 8 lines
Quoted 3 lines

Someone seems to think so or there would not be a complaint that it is getting corrupted.

No. The problem is that the size of the table is *in* the table, and so if it gets corrupted, the code that attempts to memblock_reserve() it goes off into the weeds. But that does not imply there is a point to having access to this table from a kexec kernel in the first place.

If there is no point to having access to it then we should just not pass anything to the loaded kernel, so the kernel does not think there is anything there.

Quoted 5 lines
Quoted 4 lines

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

Quoted 8 lines

kexec as a bootloader under TPM based measured boot will need to do a lot more than pass the firmware's event log to the next kernel. I'd expect a properly engineered kexec to replace this table entirely, and include the hashes of the assets it has loaded and measured into the respective PCRs.

But let's stick to solving the actual issue here, rather than philosophize on how kexec might work in this context.

I am fine with that. The complaint I had seen was that the table was being corrupted and asking how to solve that. It seems I haven't read the part of the conversation where it was made clear that no one wants the tpm_log after kexec.

If someone wants the tpm_log then we need to solve this problem.

Eric

maintainer
#33 lore raw
Quoted 73 lines · Eric W. Biederman
Quoted 70 lines · Ard Biesheuvel
Quoted 15 lines · Eric W. Biederman
Quoted 3 lines · Ard Biesheuvel

Hi Eric,

Thanks for chiming in.

It just looked like after James gave some expert input the conversation got stuck, so I am just trying to move it along.

I don't think anyone knows what this whole elephant looks like, which makes solving the problem tricky.

Quoted 2 lines · Eric W. Biederman
Quoted 1 line

...

Quoted 52 lines
Quoted 41 lines
Quoted 14 lines

This leaves two practical questions if I have been following everything correctly.

1) How to get kexec to avoid picking that memory for the new kernel to run in before it initializes itself. (AKA the getting stomped by relocate kernel problem).

2) How to point the new kernel to preserved tpm_log.

This recommendation is from memory so it may be a bit off but the general structure should work. The idea is as follows.

Quoted 9 lines

- Pass the information between kernels.

It is probably simplest for the kernel to have a command line option that tells the kernel the address and size of the tpm_log.

We have a couple of mechanisms here. Assuming you are loading a bzImage with kexec_file_load you should be able to have the in kernel loader to add those arguments to the kernel command line.

This shouldn't be necessary, and I think it is actively harmful to keep inventing special ways for the kexec kernel to learn about these things that deviate from the methods used by the first kernel. This is how we ended up with 5 sources of truth for the physical memory map (EFI memory map, memblock and 3 different versions of the e820 memory map).

We should try very hard to make kexec idempotent, and reuse the existing methods where possible. In this case, the EFI configuration table is already being exposed to the kexec kernel, which describes the base of the allocation. The size of the allocation can be derived from the table header.

Quoted 3 lines

- Ensure that when the loader is finding an address to load the new kernel it treats the address of the tpm_log as unavailable.

Quoted 5 lines

The TPM log is a table created by the EFI stub loader, which is part of the kernel. So if we need to tweak this for kexec's benefit, I'd prefer changing it in a way that can accommodate the first kernel too. However, I think the current method already has that property so I don't think we need to do anything (modulo fixing the bug)

I am fine with not inventing a new mechanism, but I think we need to reuse whatever mechanism the stub loader uses to pass it's table to the kernel. Not the EFI table that disappears at ExitBootServices().

Quoted 9 lines
Quoted 4 lines

Not sure what you mean here - the EFI table that gets clobbered by kexec *is* the table that is created by the stub loader to pass the TPM log to the kernel. Not sure what alternative you have in mind here.

I was referring to whatever the EFI table that James Bottomley mentioned that I presume the stub loader reads from when the stub loader constructs the tpm_log that is passed to the kernel.

There is no such table. The event log is exposed by the firmware via a TCG2 protocol interface, which is no longer available after boot. So the stub loader (which is the last kernel component that has access to this interface) invokes this protocol and copies the output into a table in memory which is exposed to the kernel proper as a EFI configuration table.

So the main issue here is that EFI configuration tables are passed on to kexec kernels, and we have to ensure (in the general case) that the associated memory is not reused. The implication is that the stub loader should always use EFI_ACPI_RECLAIM_MEMORY for allocations that are referenced via EFI configuration tables, and doing so for this table makes the bug go away.

Quoted 2 lines

So I believe we are in agreement of everything except terminology.

Sure.

Quoted 30 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

That said, I am doubtful that the kexec kernel can make meaningful use of the TPM log to begin with, given that the TPM will be out of sync at this point. But it is still better to keep it for symmetry, letting the higher level kexec/kdump logic running in user space reason about whether the TPM log has any value to it.

Quoted 8 lines
Quoted 3 lines

Someone seems to think so or there would not be a complaint that it is getting corrupted.

No. The problem is that the size of the table is *in* the table, and so if it gets corrupted, the code that attempts to memblock_reserve() it goes off into the weeds. But that does not imply there is a point to having access to this table from a kexec kernel in the first place.

If there is no point to having access to it then we should just not pass anything to the loaded kernel, so the kernel does not think there is anything there.

Quoted 11 lines
Quoted 4 lines

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

kexec as a bootloader under TPM based measured boot will need to do a lot more than pass the firmware's event log to the next kernel. I'd expect a properly engineered kexec to replace this table entirely, and include the hashes of the assets it has loaded and measured into the respective PCRs.

Quoted 9 lines
Quoted 2 lines

But let's stick to solving the actual issue here, rather than philosophize on how kexec might work in this context.

I am fine with that. The complaint I had seen was that the table was being corrupted and asking how to solve that. It seems I haven't read the part of the conversation where it was made clear that no one wants the tpm_log after kexec.

It was not made clear, that is why I raised the question. I argued that the TPM log has limited utility after a kexec, given that we will be in one of two situations:
- the kexec boot chain cares about the TPM and measured boot, and will therefore have extended the TPM's PCRs and the TPM log will be out of sync;
- the kexec boot chain does not care, and so there is no point in forwarding the TPM log.

Perhaps there is a third case where kdump wants to inspect the TPM log that the crashed kernel accessed? But this is rather speculative.

Quoted 2 lines

If someone wants the tpm_log then we need to solve this problem.

Agreed.

#34 lore raw
Quoted 32 lines · Ard Biesheuvel
Quoted 14 lines · Eric W. Biederman
Quoted 13 lines · Ard Biesheuvel
Quoted 5 lines · Eric W. Biederman

Ard Biesheuvel <[email protected]> writes:

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

kexec as a bootloader under TPM based measured boot will need to do a lot more than pass the firmware's event log to the next kernel. I'd expect a properly engineered kexec to replace this table entirely, and include the hashes of the assets it has loaded and measured into the respective PCRs.

Quoted 8 lines
Quoted 2 lines

But let's stick to solving the actual issue here, rather than philosophize on how kexec might work in this context.

I am fine with that. The complaint I had seen was that the table was being corrupted and asking how to solve that. It seems I haven't read the part of the conversation where it was made clear that no one wants the tpm_log after kexec.

It was not made clear, that is why I raised the question. I argued that the TPM log has limited utility after a kexec, given that we will be in one of two situations:
- the kexec boot chain cares about the TPM and measured boot, and will therefore have extended the TPM's PCRs and the TPM log will be out of sync;
- the kexec boot chain does not care, and so there is no point in forwarding the TPM log.

Quoted 2 lines

Perhaps there is a third case where kdump wants to inspect the TPM log that the crashed kernel accessed? But this is rather speculative.

Generally the kernel/host OS and the firmware are touching different PCRs in the TPM.

The firmware eventlog covers what the firmware/bootloader measured; itself, option ROMs, secure boot details, bootloader, initial kernel/initrd (if we're talking grub as the initial bootloader). These details are not changed by a kexec, and provide the anchor of the software trust chain.

A kexec'd kernel will generally not touch the same PCRs. The primary way I know to carry kexec measurements / logs over to new kernels is using IMA, which will be configured to use one of the later PCRs (default of 10).

That means that the firmware event log is still completely valid to subsequent kernels, and is still required to validate the firmware/bootloader trust chain. You then probably _also_ want to make use of the IMA log to validate the kexec'd kernel chain, but that's separate.

Quoted 2 lines
Quoted 1 line

If someone wants the tpm_log then we need to solve this problem.

Agreed.

There's a real requirement and use for kexec'd kernels to be able to continue to access the firmware TPM event log; to the extent that there are also patches floating around to have this carried over via device tree on non-UEFI platforms.

J.

Signature · 8 lines
Avoid temporary variables and strange women.
This .sig brought to you by the letter U and the number 37
Product of the Republic of HuggieTag

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#35 lore raw
Quoted 45 lines · Jonathan McDowell
Quoted 32 lines · Ard Biesheuvel
Quoted 14 lines · Eric W. Biederman
Quoted 13 lines · Ard Biesheuvel
Quoted 5 lines · Eric W. Biederman

Ard Biesheuvel <[email protected]> writes:

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

kexec as a bootloader under TPM based measured boot will need to do a lot more than pass the firmware's event log to the next kernel. I'd expect a properly engineered kexec to replace this table entirely, and include the hashes of the assets it has loaded and measured into the respective PCRs.

Quoted 8 lines
Quoted 2 lines

But let's stick to solving the actual issue here, rather than philosophize on how kexec might work in this context.

I am fine with that. The complaint I had seen was that the table was being corrupted and asking how to solve that. It seems I haven't read the part of the conversation where it was made clear that no one wants the tpm_log after kexec.

It was not made clear, that is why I raised the question. I argued that the TPM log has limited utility after a kexec, given that we will be in one of two situations:
- the kexec boot chain cares about the TPM and measured boot, and will therefore have extended the TPM's PCRs and the TPM log will be out of sync;
- the kexec boot chain does not care, and so there is no point in forwarding the TPM log.

Quoted 2 lines

Perhaps there is a third case where kdump wants to inspect the TPM log that the crashed kernel accessed? But this is rather speculative.

Generally the kernel/host OS and the firmware are touching different PCRs in the TPM.

The firmware eventlog covers what the firmware/bootloader measured; itself, option ROMs, secure boot details, bootloader, initial kernel/initrd (if we're talking grub as the initial bootloader). These details are not changed by a kexec, and provide the anchor of the software trust chain.

Quoted 4 lines

A kexec'd kernel will generally not touch the same PCRs. The primary way I know to carry kexec measurements / logs over to new kernels is using IMA, which will be configured to use one of the later PCRs (default of 10).

What about in the case where you don't have Grub, but, use the kernel as the bootloader, kexecing into the desired kernel?

Will the bootloader-kernel touch the same PCRs as GRUB, or it will only touch PCRs above 10?

Thanks
--breno

#36 lore raw
Quoted 51 lines · Breno Leitao
Quoted 45 lines · Jonathan McDowell
Quoted 32 lines · Ard Biesheuvel
Quoted 14 lines · Eric W. Biederman
Quoted 13 lines · Ard Biesheuvel
Quoted 5 lines · Eric W. Biederman

Ard Biesheuvel <[email protected]> writes:

This should not be the kexec-on-panic kernel as that runs in memory that is reserved solely for it's own use. So we are talking something like using kexec as a bootloader.

kexec as a bootloader under TPM based measured boot will need to do a lot more than pass the firmware's event log to the next kernel. I'd expect a properly engineered kexec to replace this table entirely, and include the hashes of the assets it has loaded and measured into the respective PCRs.

Quoted 8 lines
Quoted 2 lines

But let's stick to solving the actual issue here, rather than philosophize on how kexec might work in this context.

I am fine with that. The complaint I had seen was that the table was being corrupted and asking how to solve that. It seems I haven't read the part of the conversation where it was made clear that no one wants the tpm_log after kexec.

It was not made clear, that is why I raised the question. I argued that the TPM log has limited utility after a kexec, given that we will be in one of two situations:
- the kexec boot chain cares about the TPM and measured boot, and will therefore have extended the TPM's PCRs and the TPM log will be out of sync;
- the kexec boot chain does not care, and so there is no point in forwarding the TPM log.

Quoted 2 lines

Perhaps there is a third case where kdump wants to inspect the TPM log that the crashed kernel accessed? But this is rather speculative.

Generally the kernel/host OS and the firmware are touching different PCRs in the TPM.

The firmware eventlog covers what the firmware/bootloader measured; itself, option ROMs, secure boot details, bootloader, initial kernel/initrd (if we're talking grub as the initial bootloader). These details are not changed by a kexec, and provide the anchor of the software trust chain.

Quoted 4 lines

A kexec'd kernel will generally not touch the same PCRs. The primary way I know to carry kexec measurements / logs over to new kernels is using IMA, which will be configured to use one of the later PCRs (default of 10).

Quoted 5 lines

What about in the case where you don't have Grub, but, use the kernel as the bootloader, kexecing into the desired kernel?

Will the bootloader-kernel touch the same PCRs as GRUB, or it will only touch PCRs above 10?

A kernel kexecing into another will generally use IMA if it wants to measure into the TPM, which will use PCR 10 by default and not conflict with the firmware PCRs (and you then use the IMA integrity log, which is passed over a kexec, to work out the kexec side of things).

You still need the firmware event log in that case because the "bootloader" kernel combo you load is measured into the TPM by the firmware.

You _could_ technically configure things up to re-use some of the firmware PCRs, but it generally wouldn't make a lot of sense to do so and I've not seen any examples of that sort of configuration.

J.

Signature · 1 line
101 things you can't have too much of : 41 - Tea.
#37 lore raw
In reply to Usama Arif
kernel test robot noticed the following build errors:

Hi Usama,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on efi/next]
[also build test ERROR on tip/x86/core linus/master v6.11-rc7 next-20240913] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Usama-Arif/efi-tpm-add-efi-tpm_log-as-a-reserved-region-in-820_table_firmware/20240911-184309
base:   https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next
patch link:    https://lore.kernel.org/r/20240911104109.1831501-1-usamaarif642%40gmail.com
patch subject: [RFC] efi/tpm: add efi.tpm_log as a reserved region in 820_table_firmware
config: i386-defconfig (https://download.01.org/0day-ci/archive/20240914/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240914/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> arch/x86/platform/efi/efi.c:175:6: error: redefinition of 'arch_update_firmware_area'

175 | void arch_update_firmware_area(u64 addr, u64 size)
| ^
include/linux/efi.h:1375:27: note: previous definition is here
1375 | static inline void __init arch_update_firmware_area(u64 addr, u64 size)
| ^
1 error generated.

vim +/arch_update_firmware_area +175 arch/x86/platform/efi/efi.c

173
174 /* Reserve firmware area if it was marked as RAM */

 > 175	void arch_update_firmware_area(u64 addr, u64 size)

176 {
177 if (e820__get_entry_type(addr, addr + size) == E820_TYPE_RAM) {
178 e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
179 e820__update_table(e820_table_firmware);
180 }
181 }
182

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki