Skip to contentSkip to conversation

x86/efi: Re-enable memory attributes table for kexec

PATCH Superseded Ard Biesheuvel 4 participants 11 messages Mar 26 – Mar 31

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/efi: Re-enable memory attributes table for kexec

From: Ard Biesheuvel <[email protected]>

The EFI memory attributes table augments the EFI memory map, and provides permission attributes for all runtime code and data regions that are otherwise mapped read-write-execute in their entirety.

Currently, this table is disregarded when doing kexec boot on x86, for two reasons:
- the boot services data region that holds the table is not reserved correctly, and may contain garbage at kexec time
- a misguided sanity check on the size of the table is likely to trigger on kexec, as the EFI memory map has been trimmed down by that time.

Fix both issues, so that the EFI memory attributes table can be taken into account again at kexec time. Note that this requires that the call to efi_memattr_init() is moved to a later point for x86.

Cc Dave Young <[email protected]>
Cc Gregory Price <[email protected]>
Cc Usama Arif <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Breno Leitao <[email protected]>

Ard Biesheuvel (5):
efi/memattr: Fix thinko in table size sanity check x86/efi: Gather initial memory reservation and table handling logic x86/efi: Defer the call to efi_memattr_init()
efi: Use efi_mem_reserve() to reserve the memory attribute table x86/efi: Drop kexec quirk for the EFI memory attributes table

 arch/x86/include/asm/efi.h     |  5 ++---
 arch/x86/kernel/setup.c        | 11 ++---------
 arch/x86/platform/efi/efi.c    | 15 +++++++++++++++
 arch/x86/platform/efi/quirks.c |  4 ----
 drivers/firmware/efi/efi.c     |  2 +-
 drivers/firmware/efi/memattr.c | 17 ++++++++++-------
 6 files changed, 30 insertions(+), 24 deletions(-)

base-commit: 217c0a5c177a3d4f7c8497950cbf5c36756e8bbb

Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#1 lore raw

PATCH 1/5 efi/memattr: Fix thinko in table size sanity check

From: Ard Biesheuvel <[email protected]>

While it is true that each PE/COFF runtime driver in memory can generally be split into 3 different regions (the header, the code/rodata region and the data/bss region), each with different permissions, it does not mean that 3x the size of the memory map is a suitable upper bound. This is due to the fact that all runtime drivers could be coalesced into a single EFI runtime code region by the firmware, and if the firmware does a good job of keeping the fragmentation down, it is conceivable that the memory attributes table has more entries than the EFI memory map itself.

So instead, base the sanity check on whether the descriptor size matches the EFI memory map's descriptor size (which is not mandated by the spec but extremely unlikely to differ in practice), and whether the size of the whole table does not exceed 2 MiB.

---
 drivers/firmware/efi/memattr.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/firmware/efi/memattr.c +9 -6
index e727cc5909cb..8d4004149297 100644
@@ -42,14 +42,17 @@ void __init efi_memattr_init(void)
 
 
 	/*
-	 * Sanity check: the Memory Attributes Table contains up to 3 entries
-	 * for each entry of type EfiRuntimeServicesCode in the EFI memory map.
-	 * So if the size of the table exceeds 3x the size of the entire EFI
-	 * memory map, there is clearly something wrong, and the table should
-	 * just be ignored altogether.
+	 * Sanity check: the Memory Attributes Table contains multiple entries
+	 * for each EFI runtime services code or data region in the EFI memory
+	 * map, each with the permission attributes that may be applied when
+	 * mapping the region.  There is no upper bound for the number of
+	 * entries, as it could conceivably contain more entries than the EFI
+	 * memory map itself. So pick an arbitrary limit of 2M for the size,
+	 * which translates to ~50k entries of 40 bytes in size. This prevents
+	 * a corrupted table from eating all system RAM.
 	 */
 	size = tbl->num_entries * tbl->desc_size;
-	if (size > 3 * efi.memmap.nr_map * efi.memmap.desc_size) {
+	if (tbl->desc_size != efi.memmap.desc_size || size > SZ_2M) {
 		pr_warn(FW_BUG "Corrupted EFI Memory Attributes Table detected! (version == %u, desc_size == %u, num_entries == %u)\n",
 			tbl->version, tbl->desc_size, tbl->num_entries);
 		goto unmap;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] efi/memattr: Fix thinko in table size sanity check

Quoted 12 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

While it is true that each PE/COFF runtime driver in memory can generally be split into 3 different regions (the header, the code/rodata region and the data/bss region), each with different permissions, it does not mean that 3x the size of the memory map is a suitable upper bound. This is due to the fact that all runtime drivers could be coalesced into a single EFI runtime code region by the firmware, and if the firmware does a good job of keeping the fragmentation down, it is conceivable that the memory attributes table has more entries than the EFI memory map itself.

Quoted 6 lines

So instead, base the sanity check on whether the descriptor size matches the EFI memory map's descriptor size (which is not mandated by the spec but extremely unlikely to differ in practice), and whether the size of the whole table does not exceed 2 MiB.

The 2MB limit is a bit odd to me - but then i don't see a legitimate reason to need 50k+ entries here unless the system is doing something absolutely nutty - it would mean a wildly fragmented system and most likely an indicator of a bug rather than a legitimately intended configuration.

Otherwise, this does seem like a better check regardless.

maintainer
#3 lore raw

Re: [PATCH 1/5] efi/memattr: Fix thinko in table size sanity check

Quoted 18 lines · Gregory Price
Quoted 17 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

While it is true that each PE/COFF runtime driver in memory can generally be split into 3 different regions (the header, the code/rodata region and the data/bss region), each with different permissions, it does not mean that 3x the size of the memory map is a suitable upper bound. This is due to the fact that all runtime drivers could be coalesced into a single EFI runtime code region by the firmware, and if the firmware does a good job of keeping the fragmentation down, it is conceivable that the memory attributes table has more entries than the EFI memory map itself.

So instead, base the sanity check on whether the descriptor size matches the EFI memory map's descriptor size (which is not mandated by the spec but extremely unlikely to differ in practice), and whether the size of the whole table does not exceed 2 MiB.

Quoted 8 lines
Quoted 1 line

The 2MB limit is a bit odd to me - but then i don't see a legitimate reason to need 50k+ entries here unless the system is doing something absolutely nutty - it would mean a wildly fragmented system and most likely an indicator of a bug rather than a legitimately intended configuration.

The 2MB is completely arbitrary. We could use 1M or 32M for all I care. The original report was about a bogus table eating up all the memory.

Quoted 3 lines

Otherwise, this does seem like a better check regardless.

Thanks.

#4 lore raw

PATCH 2/5 x86/efi: Gather initial memory reservation and table handling logic

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Move the back-to-back calls to various EFI routines related to processing of firmware tables and reserving the associated memory into a helper function. This is tidier, and will avoid the need to add yet another function call there in a subsequent patch.

---
 arch/x86/include/asm/efi.h  |  5 ++---
 arch/x86/kernel/setup.c     | 11 ++---------
 arch/x86/platform/efi/efi.c | 13 +++++++++++++
 3 files changed, 17 insertions(+), 12 deletions(-)
arch/x86/include/asm/efi.h +2 -3
index 51b4cdbea061..3dcb137a49ed 100644
@@ -387,11 +387,10 @@ static inline  bool efi_is_table_address(unsigned long phys_addr)
 {
 	return false;
 }
-static inline void efi_reserve_boot_services(void)
-{
-}
 #endif /* CONFIG_EFI */
 
+void efi_init_reservations(void);
+
 extern int __init efi_memmap_alloc(unsigned int num_entries,
 				   struct efi_memory_map_data *data);
 
arch/x86/kernel/setup.c +2 -9
index eebcc9db1a1b..9f5f50bff16d 100644
@@ -1083,15 +1083,8 @@ void __init setup_arch(char **cmdline_p)
 	mem_encrypt_setup_arch();
 	cc_random_init();
 
-	efi_find_mirror();
-	efi_esrt_init();
-	efi_mokvar_table_init();
-
-	/*
-	 * The EFI specification says that boot service code won't be
-	 * called after ExitBootServices(). This is, in fact, a lie.
-	 */
-	efi_reserve_boot_services();
+	if (efi_enabled(EFI_BOOT))
+		efi_init_reservations();
 
 	/* preallocate 4k for mptable mpc */
 	e820__memblock_alloc_reserved_mpc_new();
arch/x86/platform/efi/efi.c +13 -0
index d84c6020dda1..f32276bd8d4e 100644
@@ -165,6 +165,19 @@ static void __init do_add_efi_memmap(void)
 	e820__update_table(e820_table);
 }
 
+void __init efi_init_reservations(void)
+{
+	efi_find_mirror();
+	efi_esrt_init();
+	efi_mokvar_table_init();
+
+	/*
+	 * The EFI specification says that boot service code won't be
+	 * called after ExitBootServices(). This is, in fact, a lie.
+	 */
+	efi_reserve_boot_services();
+}
+
 /*
  * Given add_efi_memmap defaults to 0 and there is no alternative
  * e820 mechanism for soft-reserved memory, import the full EFI memory
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] x86/efi: Gather initial memory reservation and table handling logic

Quoted 8 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Move the back-to-back calls to various EFI routines related to processing of firmware tables and reserving the associated memory into a helper function. This is tidier, and will avoid the need to add yet another function call there in a subsequent patch.

#6 lore raw

PATCH 3/5 x86/efi: Defer the call to efi_memattr_init()

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

efi_memattr_init() should call efi_mem_reserve() rather than memblock_reserve() to preserve the memory contents of the region when the EFI memory attributes table is loaded by the firmware.

However, efi_mem_reserve() can only be called later during the boot on x86, due to the fact that it may need to memblock_alloc() memory for the EFI memory map.

So move the call to efi_memattr_init() to a later stage when building for x86_64.

---
 arch/x86/platform/efi/efi.c | 2 ++
 drivers/firmware/efi/efi.c  | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +2 -0
index f32276bd8d4e..107dcdf354ba 100644
@@ -167,6 +167,8 @@ static void __init do_add_efi_memmap(void)
 
 void __init efi_init_reservations(void)
 {
+	if (IS_ENABLED(CONFIG_X86_64) && efi_enabled(EFI_MEMMAP))
+		efi_memattr_init();
 	efi_find_mirror();
 	efi_esrt_init();
 	efi_mokvar_table_init();
drivers/firmware/efi/efi.c +1 -1
index b2fb92a4bbd1..d28ed1ce9a33 100644
@@ -761,7 +761,7 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 		}
 	}
 
-	if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
+	if (!IS_ENABLED(CONFIG_X86) && efi_enabled(EFI_MEMMAP))
 		efi_memattr_init();
 
 	efi_tpm_eventlog_init();
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#7 lore raw

PATCH 4/5 efi: Use efi_mem_reserve() to reserve the memory attribute table

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Use efi_mem_reserve() rather than memblock_reserve() to reserve the EFI memory attributes table. This is needed on x86, where memblock_reserve() is not sufficient for assets that may be placed in EFI boot services data memory.

---
 drivers/firmware/efi/memattr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/memattr.c +1 -1
index 8d4004149297..f9cfdec1fb51 100644
@@ -59,7 +59,7 @@ void __init efi_memattr_init(void)
 	}
 
 	tbl_size = sizeof(*tbl) + size;
-	memblock_reserve(efi_mem_attr_table, tbl_size);
+	efi_mem_reserve(efi_mem_attr_table, tbl_size);
 	set_bit(EFI_MEM_ATTR, &efi.flags);
 
 unmap:
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#8 lore raw

PATCH 5/5 x86/efi: Drop kexec quirk for the EFI memory attributes table

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Now that the EFI memory attributes table is preserved properly, and the quirk to detect corrupted tables has been updated not to result in false positives when the number of EFI memory map entries is low compared to the number of EFI memory attributes table entries, there is no longer a need to ignore the latter when doing a kexec boot. So drop the workaround.

This reverts commit

64b45dd46e15 ("x86/efi: skip memattr table on kexec boot")

---
 arch/x86/platform/efi/quirks.c | 4 ----
 1 file changed, 4 deletions(-)
arch/x86/platform/efi/quirks.c +0 -4
index 79f0818131e8..98641c621e6c 100644
@@ -611,10 +611,6 @@ int __init efi_reuse_config(u64 tables, int nr_tables)
 		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
 			((efi_config_table_64_t *)p)->table = data->smbios;
 
-		/* Do not bother to play with mem attr table across kexec */
-		if (!efi_guidcmp(guid, EFI_MEMORY_ATTRIBUTES_TABLE_GUID))
-			((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR;
-
 		p += sz;
 	}
 	early_memunmap(tablep, nr_tables * sz);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#9 lore raw
In reply to Ard Biesheuvel
Quoted 13 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The EFI memory attributes table augments the EFI memory map, and provides permission attributes for all runtime code and data regions that are otherwise mapped read-write-execute in their entirety.

Currently, this table is disregarded when doing kexec boot on x86, for two reasons:
- the boot services data region that holds the table is not reserved correctly, and may contain garbage at kexec time
- a misguided sanity check on the size of the table is likely to trigger on kexec, as the EFI memory map has been trimmed down by that time.

Quoted 4 lines

Fix both issues, so that the EFI memory attributes table can be taken into account again at kexec time. Note that this requires that the call to efi_memattr_init() is moved to a later point for x86.

I appreciate this fixup, I'll try to give it a test in some known host configuration switch scenarios I know don't work across multiple kexecs and see if it is an improvement.

~Gregory

Quoted 26 lines
Cc Dave Young <[email protected]>
Cc Gregory Price <[email protected]>
Cc Usama Arif <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Breno Leitao <[email protected]>

Ard Biesheuvel (5):
efi/memattr: Fix thinko in table size sanity check x86/efi: Gather initial memory reservation and table handling logic x86/efi: Defer the call to efi_memattr_init()
efi: Use efi_mem_reserve() to reserve the memory attribute table x86/efi: Drop kexec quirk for the EFI memory attributes table

 arch/x86/include/asm/efi.h     |  5 ++---
 arch/x86/kernel/setup.c        | 11 ++---------
 arch/x86/platform/efi/efi.c    | 15 +++++++++++++++
 arch/x86/platform/efi/quirks.c |  4 ----
 drivers/firmware/efi/efi.c     |  2 +-
 drivers/firmware/efi/memattr.c | 17 ++++++++++-------
 6 files changed, 30 insertions(+), 24 deletions(-)

base-commit: 217c0a5c177a3d4f7c8497950cbf5c36756e8bbb

Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#10 lore raw
In reply to Ard Biesheuvel
Quoted 14 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The EFI memory attributes table augments the EFI memory map, and provides permission attributes for all runtime code and data regions that are otherwise mapped read-write-execute in their entirety.

Currently, this table is disregarded when doing kexec boot on x86, for two reasons:
- the boot services data region that holds the table is not reserved correctly, and may contain garbage at kexec time
- a misguided sanity check on the size of the table is likely to trigger on kexec, as the EFI memory map has been trimmed down by that time.

Quoted 3 lines

Fix both issues, so that the EFI memory attributes table can be taken into account again at kexec time. Note that this requires that the call to efi_memattr_init() is moved to a later point for x86.

I vaguely remember when we discussed this issue in old threads efi_mem_reserve can not work for kexec_load syscall, but I think after
the commit a2498e5c453b it should just work. So I think it looks
good, thanks Ard!

Thanks
Dave