Skip to contentSkip to conversation

efi/libstub: Allocate the INITRD table in ACPI reclaim memory

PATCH New Sebastien WENSKE 3 participants 3 messages Oct 12 – Oct 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi/libstub: Allocate the INITRD table in ACPI reclaim memory

Commit 6e62d1c6f2c7 ("efi: Wipe INITRD config table from memory after consumption") makes the kernel clear the base and size of the LINUX_EFI_INITRD_MEDIA_GUID configuration table once it has read them, so that a kexec'd kernel no longer picks up a stale initrd.

However, the stub allocates that table from EFI_LOADER_DATA, which the kernel that consumes it treats as ordinary RAM, while the configuration table entry pointing at it survives kexec. A kexec'd kernel therefore finds the same address in the configuration table, and clears 16 bytes there again, in memory that may by then hold anything - including the kexec'd kernel's own image, which kexec_file_load() is free to place there.

Seen on x86_64 v7.2.10 under QEMU with Debian's OVMF 2025.02 Secure Boot capable build (OVMF_CODE_4M.secboot.fd, no keys enrolled), the initrd handed to the stub through the LINUX_EFI_INITRD_MEDIA_GUID device path (systemd-stub UKI). A kexec reboot (kexec_file_load(), bzImage64, with an initrd) loads the new kernel at 0x1ca00000; the stale table is at 0x1d4bcf98, in the middle of tcp_parse_options() in that image. The kexec'd kernel boots, then oopses on the first incoming SYN, 16 bytes of its text zeroed:

  BUG: kernel NULL pointer dereference, address: 0000000000000002
  RIP: 0010:tcp_parse_options+0xe7/0x2f0
  Code: a5 4c 29 e1 89 c8 0f b6 4d 12 83 e0 3f 83 e1 c0 09 c1 88 4d 12 eb de 41 83 fe 04 75 d8 41 f6 44 24 0d 02 74 d0 45 85 d2 75 cb <0f> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 66 39 d0 89 d1
  Call Trace:
   <IRQ>
   tcp_conn_request+0x270/0x9d0
   tcp_rcv_state_process+0x147/0x1560
   tcp_v4_do_rcv+0xc1/0x260
   tcp_v4_rcv+0x110e/0x1190

A debug print added before the wipe shows what the kexec'd kernel reads at the table's address - its own text:

efi: INITRD table at 0x1d4bcf98:

       base 0xc274c085660241b7 size 0x8c0c1661455b70f

Whether a given kernel crashes depends on what happens to land at that
address: v7.2.9 clears 16 bytes elsewhere in its image without any visible failure, and bisecting v7.2.9..v7.2.10 ends on an unrelated commit that only shifts the image's layout.

Allocate the table from EFI_ACPI_RECLAIM_MEMORY instead, as the stub already does for the RNG seed table so that it survives a kexec reboot. The table then stays where it is across kexec, and the wipe always lands on the table itself.

Fixes 6e62d1c6f2c7 ("efi: Wipe INITRD config table from memory after consumption")
Assisted-by LLM

---
Tested on x86_64 under QEMU (q35, KVM) with Debian's OVMF 2025.02 (OVMF_CODE_4M.secboot.fd and OVMF_CODE_4M.fd), booting a systemd-stub UKI, then rebooting with kexec_file_load() and an initrd:

- v7.2.10: the oops above in 6 out of 6 kexec reboots under the secboot build (none under the plain one). With this patch: 2 out of 2 come up, and two chained kexec'd kernels read the cleared table back (base 0x0 size 0x0), now in ACPI data memory.
- v7.3-rc7 without this patch: no visible failure, but the same debug print shows the kexec'd kernel reading its own image at the table's address - and so clearing 16 bytes of it:

    efi: INITRD table at 0x1d4bcf98:
         base 0xf413863894c40c4 size 0x415c74063c1246b6 - inside this
         kernel's image

With this patch: kexec reboots (chained too) come up under both OVMF builds. It builds without warnings, W=1 included for libstub.

Not tested: other architectures, real hardware.

Reserving the table with efi_mem_reserve() in efi_config_parse_tables() instead doesn't help on x86: efi_arch_mem_reserve() only handles EFI_BOOT_SERVICES_DATA ("Failed to lookup EFI memory descriptor"), so kexec still places the next kernel over the table. Note that a kernel kexec'd from one without this patch still finds the table in loader data memory.

 drivers/firmware/efi/libstub/efi-stub-helper.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +8 -2
index f27f2e1f0..9293b8b31 100644
@@ -641,8 +641,14 @@ efi_status_t efi_load_initrd(efi_loaded_image_t *image,
 				     EFISTUB_EVT_INITRD) == EFI_SUCCESS)
 		efi_info("Measured initrd data into PCR 9\n");
 
-	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
-			     (void **)&tbl);
+	/*
+	 * Use EFI_ACPI_RECLAIM_MEMORY so the table survives a kexec reboot:
+	 * configuration tables do, and the kernel wipes this one once it has
+	 * consumed it - a kexec'd kernel wipes it again, at the same address,
+	 * which in EFI_LOADER_DATA memory may hold anything by then.
+	 */
+	status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
+			     sizeof(initrd), (void **)&tbl);
 	if (status != EFI_SUCCESS)
 		goto free_initrd;
 

base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/1 x86/efi: Drop stale INITRD table entry on kexec boot

From: Jaehee Park <[email protected]>

The EFI stub allocates the INITRD configuration table in EFI_LOADER_DATA memory. That memory can be reused after boot, but the configuration table entry pointing to it survives kexec.

When a kexec'd kernel consumes the stale entry, efi_config_parse_tables() clears the base and size fields at the old table address. If the new initramfs occupies that memory, this overwrites 16 bytes of compressed data and prevents the capture kernel from unpacking its initramfs.

Invalidate the INITRD entry when reusing the EFI configuration tables on x86. Kexec supplies the new initrd through boot_params, so the capture kernel does not need the old table.

Fixes 6e62d1c6f2c7 ("efi: Wipe INITRD config table from memory after consumption")

---
Hi Ard, Sebastien,

I independently diagnosed and locally fixed this issue on October 6 while debugging kdump on an x86_64 L40S system. I was preparing to submit my fix after internal review and found your October 12 proposal.

In my case, the stale INITRD table address overlapped the capture initramfs. The capture kernel reported:

Initramfs unpacking failed: ZSTD-compressed data is corrupt Failed to execute /init (error -2)

  Kernel panic - not syncing: No working init found.

My local fix invalidates the inherited INITRD entry in efi_reuse_config() instead of changing the stub allocation. With that fix, the capture initramfs checksum remained unchanged through successful unpacking and kdump saved a crash dump.

The tested version also removed the data->smbios early return. I have left that change out of this submission to keep the initial patch small. This reduced version has not been boot-tested separately. It retains the existing SMBIOS guard, so it does not cover SMBIOS3-only systems. Would you prefer handling that guard as part of this fix?

Would invalidating the inherited entry also be useful when kexec boots from a kernel whose stub allocated the table in EFI_LOADER_DATA? The allocation change appears to address the lifetime at its source, while this change prevents the incoming x86 kernel from consuming a stale entry created by an older kernel.

I have not tested the ACPI reclaim patch, so I am not offering a Tested-by for that patch.

Thanks,
Jaehee

 arch/x86/platform/efi/quirks.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/platform/efi/quirks.c +7 -0
@@ -615,6 +615,13 @@
 		if (!efi_guidcmp(guid, EFI_MEMORY_ATTRIBUTES_TABLE_GUID))
 			((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR;
 
+		/*
+		 * The EFI stub allocated this table in loader data, which may
+		 * have been reused. Kexec passes its initrd in boot_params.
+		 */
+		if (!efi_guidcmp(guid, LINUX_EFI_INITRD_MEDIA_GUID))
+			((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR;
+
 		p += sz;
 	}
 	early_memunmap(tablep, nr_tables * sz);

base-commit: 7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614

Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH] x86/efi: Drop stale INITRD table entry on kexec boot

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] x86/efi: efi_reuse_config() skips INITRD invalidation when data->smbios is 0 - [High] x86/efi…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] x86/efi: efi_reuse_config() skips INITRD invalidation when data->smbios is 0
- [High] x86/efi: incomplete stale INITRD table invalidation in efi_reuse_config() --

commit 475363733715c59cee77eadc0fb6905b395409e1
Author: Jaehee Park <[email protected]>

x86/efi: Drop stale INITRD table entry on kexec boot

This commit invalidates the LINUX_EFI_INITRD_MEDIA_GUID entry in efi_reuse_config() on x86 so that a kexec kernel does not consume and zero a stale EFI_LOADER_DATA table address that may now overlap the new initramfs.

Quoted 5 lines
arch/x86/platform/efi/quirks.c +0 -0
index 02c56a02eb9bc..d7f4b985b2794 100644

arch/x86/platform/efi/quirks.c int __init efi_reuse_config(u64 tables, int nr_tables) · line 624

Quoted 10 lines
arch/x86/platform/efi/quirks.c +7 -0
@@ -615,6 +615,13 @@ int __init efi_reuse_config(u64 tables, int nr_tables)
 		if (!efi_guidcmp(guid, EFI_MEMORY_ATTRIBUTES_TABLE_GUID))
 			((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR;
 
+		/*
+		 * The EFI stub allocated this table in loader data, which may
+		 * have been reused. Kexec passes its initrd in boot_params.
+		 */
+		if (!efi_guidcmp(guid, LINUX_EFI_INITRD_MEDIA_GUID))
+			((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR;
+

[Severity: High]
Will efi_reuse_config() in arch/x86/platform/efi/quirks.c still skip invalidating LINUX_EFI_INITRD_MEDIA_GUID when data->smbios is zero?

Before mapping the configuration table array and entering the loop, arch/x86/platform/efi/quirks.c:efi_reuse_config() checks:

	if (!efi_setup)
		return 0;

	if (!efi_enabled(EFI_64BIT))
		return 0;

	data = early_memremap(efi_setup, sizeof(*data));
	if (!data) {
		ret = -ENOMEM;
		goto out;
	}

	if (!data->smbios)
		goto out_memremap;

On UEFI systems that only provide a 64-bit SMBIOS 3.0 entry point (SMBIOS3_TABLE_GUID) and no legacy 32-bit SMBIOS 2.x entry point (SMBIOS_TABLE_GUID), efi.smbios remains EFI_INVALID_TABLE_ADDR. In drivers/firmware/efi/efi.c:systab_show(), /sys/firmware/efi/systab only emits SMBIOS3= and omits SMBIOS=:

	if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
		str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
	if (efi.smbios != EFI_INVALID_TABLE_ADDR)
		str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);

When userspace kexec-tools populates struct efi_setup_data from /sys/firmware/efi/systab on such a system, data->smbios is left as 0. When the kexec or kdump kernel boots, efi_reuse_config() takes the goto out_memremap path before reaching the configuration table loop.

On x86, the kexec initrd passed via boot_params is read through boot_params.hdr.ramdisk_size in arch/x86/kernel/setup.c:get_ramdisk_size() without setting phys_initrd_size:

	u64 ramdisk_size = boot_params.hdr.ramdisk_size;

	ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32;

	if (ramdisk_size == 0)
		ramdisk_size = phys_initrd_size;

As a result, phys_initrd_size is still 0 when drivers/firmware/efi/efi.c:efi_config_parse_tables() runs:

	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
	    initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) {
		struct linux_efi_initrd *tbl;

		tbl = early_memremap(initrd, sizeof(*tbl));
		if (tbl) {
			phys_initrd_start = tbl->base;
			phys_initrd_size = tbl->size;
			tbl->base = tbl->size = 0;
			early_memunmap(tbl, sizeof(*tbl));
		}
	}

Because drivers/firmware/efi/libstub/efi-stub-helper.c:efi_load_initrd() allocated tbl in EFI_LOADER_DATA memory during the initial boot:

	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
			     (void **)&tbl);

that physical address is reclaimed as normal RAM after ExitBootServices() and can be overwritten with 16 bytes of zeros when efi_reuse_config() returns early on !data->smbios (or when !efi_setup or !efi_enabled(EFI_64BIT)), corrupting whatever kexec or kdump segment now resides there.

Should data->smbios only guard the SMBIOS_TABLE_GUID assignment inside the loop rather than returning early from efi_reuse_config()?

Also, since non-x86 EFI architectures do not call efi_reuse_config(), could a kexec boot without an initrd (where phys_initrd_size == 0) on those architectures still dereference and zero the stale EFI_LOADER_DATA address in efi_config_parse_tables()?

Quoted 3 lines
arch/x86/platform/efi/quirks.c +0 -0
 		p += sz;
 	}
 	early_memunmap(tablep, nr_tables * sz);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1