From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=45.153.89.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795907871; b=RhF38U646OErU2hJN9bcOR+JXLUkL8Y46jxxrheMKwZZuX7Tbi6Em6S3953Jxpt9lgAF +h3DVt5/iwXvxC2tRHbhcuILOUUV/5ZeAyVaxviQAwU8sD3FUyfDa3VDTYen+Dnljl9Kf ocWLMOHXXHawHAJ94u07kYSoTf2Aemn2lCFMx1bzBk5/bsZiRJ/rAt37saH+/ZFBmy3Iy FEKcdF2O71xVWR3zyZGrrR7/hL517ZdYs6anksXV9mZI88uAGoseGslEsAVo94oCKBiIY RSFx+KUTcTYLXOOGjqOJu9GT7pvxZABYEElx0C+9oVATYf4Woz1mEX5D77Aql09k4hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795907871; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:Received:From:To:Cc: Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=ElKdXgy1lJ/8cntgfZ/saD53Q4BIScGrIXeesnZ4EcA=; b=C5aY2uu11gbuwztJbEPHIK4ddi9bibme0FFmsl/3IUJ2Q8PK/Mt+1liOmQASGFri6yXm zulhZKAfRNdnXm2IJ7H+J6Ao0gIdB7f+yo5KfgoH8bBI20aL4Gq8klpxMaeyEkrK6Kn+H ii5saotz/EEg3J6G+TmL+St+EEBa3HKq9NC/Pu6ee4iH56gP+hU78WShQVHym8HhKvs+6 iw7Lwu/6+AzOf0EqFT0JGPgOd+D0WS+JLfIBSWPY0hzXX1VdDMkwBa1tIQqdm9j/NST5J CmgMBnSUNvwUKOThUVc2V+LNRjKXn1YfFPU+ibc7QypdThG2yfIQ5bhQEu0ItYDlQ1g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=smayr.name; spf=pass smtp.mailfrom=smayr.name; dkim=pass header.d=smayr.name header.i=@smayr.name header.a=rsa-sha256 header.s=gm1 header.b=LEbz+uZ3; arc=none smtp.remote-ip=45.153.89.172 Received: from o1.gomailify.com (rac.o1.gomailify.com [45.153.89.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20BC7606A31C for ; Sat, 28 Nov 2026 23:17:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 20BC7606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=smayr.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=smayr.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=smayr.name header.i=@smayr.name header.a=rsa-sha256 header.s=gm1 header.b=LEbz+uZ3 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=LpSnZz2T8XuTjxhwiCx8eqqp8sA38fqLPGtPe+dQKX8=; c=simple/simple; d=smayr.name; h=from:to:subject:date:message-id:mime-version:content-type; s=gm1; t=1795907862; v=1; x=1796512662; b=LEbz+uZ3UQGDfdnxFTidtyYFQQnpJ5B8Yo5NvJhq7dJKYX/0FEcca++RzmYM0DFqWh8ku3V2 QI8TPOkGlh9mI2YyGuLAGRiCh8AIVPkb1s6bCdRmZ+UYQKZurPkmS8OOBEVvoAEoamFiaRxH+aD xNMw8HItXGVRSVyvziitT2aFv7VETA9o01scxLXpdimIDZaF96lQlf+91rzUn++F8ZwyBBcRpRI Fjv+I1LzMOz565khTIrB5V2xvq0Q4l6DqmAtcXnAaWC+ig2HFKlZ4v+0/czWt79fk1/WNpNWDoC UG5ZAW6Sex0NBFTh5xkEi7kABoMHkcooXGJM+aeIe+Ahg== Received: from localhost.localdomain (2600:4041:550f:5f00:ad76:d270:54d:5da2) by mx.gomailify.com with ESMTPS id ea7d3deb10b7b636; Sat, 28 Nov 2026 23:17:39 +0000 From: Sebastian Mayr To: Ard Biesheuvel Cc: Ilias Apalodimas , x86@kernel.org, Kees Cook , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, Sebastian Mayr Subject: [PATCH] x86/efistub: Avoid placing the kernel in MTRR-uncached RAM Date: Sat, 28 Nov 2026 18:16:50 -0500 Message-Id: <20261128231650.20941-1-me@smayr.name> X-Mailer: git-send-email 2.39.3 (Apple Git-145) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On systems whose firmware leaves part of RAM uncovered by variable MTRRs (with a UC default type), physical KASLR in the EFI stub can decompress the kernel into that uncached range, causing a silent early-boot hang when mtrr_trim_uncached_memory() later trims it out of the direct map. For example, on a Dell Wyse 5070 with 32 GiB, 0x800000000-0x87fffffff (the RAM remapped from under the PCI hole) is left uncovered (/proc/mtrr): reg00: base=0x000000000 ( 0MB), size= 2048MB, count=1: write-back reg01: base=0x07a000000 ( 1952MB), size= 32MB, count=1: uncachable reg02: base=0x07c000000 ( 1984MB), size= 64MB, count=1: uncachable reg03: base=0x100000000 ( 4096MB), size= 4096MB, count=1: write-back reg04: base=0x200000000 ( 8192MB), size= 8192MB, count=1: write-back reg05: base=0x400000000 (16384MB), size=16384MB, count=1: write-back WARNING: BIOS bug: CPU MTRRs don't cover all of memory, losing 2048MB of RAM. The EFI stub's physical KASLR considers every EFI_CONVENTIONAL_MEMORY slot, so it sometimes decompresses the kernel into that range (about 6% of boots on that machine). mtrr_trim_uncached_memory() then turns the range the kernel is running from into E820_TYPE_RESERVED, so init_mem_mapping() doesn't direct-map the kernel image, and the first __va() of a kernel page after that faults. In practice that is set_pte_vaddr() while early_acpi_boot_init() sets the APIC fixmap: BUG: unable to handle page fault for address: ff4896d3e8e32ff8 RIP: 0010:fill_pud+0x6/0xc0 set_pte_vaddr_p4d+0x28/0x50 native_set_fixmap+0x46/0xa0 apic_set_fixmap+0x35/0x80 early_acpi_boot_init+0xbf/0x150 setup_arch+0x7c3/0xcc0 This happens before any console is up, so the machine just hangs. Setting mem= allows working around this issue by implicitly disabling physical KASLR. This ensures the kernel can never end up in an area which is trimmed later and also silences the BIOS bug warning. Apply the same test as mtrr_trim_uncached_memory() before randomizing: if MTRRs are enabled, the default type is UC and the variable ranges are WB/UC only, cap the allocation at the highest WB-covered address. If nothing fits below that cap, fall back to the old limit, so no system that booted before can fail to boot now. Known limitations: - AMD/Hygon are skipped, since TOP_MEM2 can make RAM WB without any variable MTRR. - Only the top of RAM is handled; holes, or a UC range carved out of the top of a WB range, are not. - This does not fix the same issue in the legacy boot path (arch/x86/boot/compressed/kaslr.c). Fixes: a1b87d54f4e4 ("x86/efistub: Avoid legacy decompressor when doing EFI boot") Signed-off-by: Sebastian Mayr --- Not tagged for stable; happy to add Cc: stable if you think it qualifies. Testing: I started this off by creating a QEMU-based repro. No changes on QEMU/kernel, just a small EFI app which sets up the UC-default MTRR layout and leaves the top 2 GiB of a 6 GiB guest uncovered, then chainloads bzImage. It's available at https://gist.github.com/Sebmaster/fd494b4cf65eec9e1fd27dc419112e3f. forced placement into the uncovered range: hangs every time normal KASLR, unpatched: 4 of 12 boots hang normal KASLR, patched: all boots OK forced placement, patched: falls back to the old limit (no WB RAM left free), so hangs I also applied this patch to the Fedora kernel on the Dell Wyse 5070 and ran 20 consecutive reboots without hangs (vs. ~6% hang rate unpatched). Open questions: - I'd love to keep this initial patch small, but this same issue looks to be present in the boot/compressed x86 codepath as well. If you think this should be done in a single series, I can give that a shot, but would love some guidance on where to put shared helpers. - I've tried to keep this codepath very defensive (Intel-only, TDX check), but are there other environments which could cause any issues here? This is my first kernel submission; would appreciate any feedback on the approach and process. Disclosure: The repros and an initial draft of the patch were developed with Opus/Neon. I refined the code, ran the repros myself and verified the fixes on real hardware. drivers/firmware/efi/libstub/x86-stub.c | 144 ++++++++++++++++++++---- 1 file changed, 123 insertions(+), 21 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 253dd3ba8fab..705f75e7ffe6 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -16,7 +16,11 @@ #include #include #include +#include #include +#include +#include +#include #include #include @@ -835,13 +839,117 @@ static efi_status_t parse_options(const char *cmdline) return efi_parse_options(cmdline); } +static bool early_is_tdx_guest(void) +{ + static bool once; + static bool is_tdx; + + if (!IS_ENABLED(CONFIG_INTEL_TDX_GUEST)) + return false; + + if (!once) { + u32 eax = TDX_CPUID_LEAF_ID, sig[3] = {}; + + native_cpuid(&eax, &sig[0], &sig[2], &sig[1]); + is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig)); + once = true; + } + + return is_tdx; +} + +/* + * If the variable MTRRs leave the top of RAM uncached (default type UC and + * no WB range covering it), mtrr_trim_uncached_memory() will later turn that + * memory into E820_TYPE_RESERVED. A kernel image placed there loses its + * direct mapping and dies in early boot, so keep the randomized placement + * below the highest address covered by a WB variable MTRR, applying the + * same preconditions as the trim code. + */ +static u64 efi_mtrr_wb_limit(void) +{ + u32 eax = 0, vendor[3] = {}; + u64 highest = 0; + struct msr m; + int i, nvar; + + if (!IS_ENABLED(CONFIG_MTRR)) + return U64_MAX; + + /* + * Only handle Intel: on AMD/Hygon, TOP_MEM2 may make RAM above 4 GiB + * WB regardless of the variable MTRRs, and that cannot be checked + * safely from here. + */ + native_cpuid(&eax, &vendor[0], &vendor[2], &vendor[1]); + if (memcmp(vendor, "GenuineIntel", 12)) + return U64_MAX; + + if (!(native_cpuid_edx(1) & BIT(X86_FEATURE_MTRR & 31))) + return U64_MAX; + + /* + * TDX guests read the MTRRs from the untrusted VMM (via #VE), and + * the kernel replaces them with WB and never trims, so skip them. + */ + if (early_is_tdx_guest()) + return U64_MAX; + + raw_rdmsr(MSR_MTRRdefType, &m); + if (!(m.q & MTRR_DEF_TYPE_E) || + (m.q & MTRR_DEF_TYPE_TYPE) != MTRR_TYPE_UNCACHABLE) + return U64_MAX; + + raw_rdmsr(MSR_MTRRcap, &m); + nvar = m.q & MTRR_CAP_VCNT; + + for (i = 0; i < nvar; i++) { + struct msr base, mask; + u64 addr_mask, size, top; + + raw_rdmsr(MTRRphysMask_MSR(i), &mask); + if (!(mask.q & MTRR_PHYSMASK_V)) + continue; + raw_rdmsr(MTRRphysBase_MSR(i), &base); + + switch (base.q & MTRR_PHYSBASE_TYPE) { + case MTRR_TYPE_WRBACK: + /* size is the lowest set bit of the (contiguous) mask */ + addr_mask = mask.q & PAGE_MASK; + size = addr_mask & -addr_mask; + /* + * Empty or non-contiguous mask: don't guess. A contiguous + * mask plus its lowest bit is a single bit (open-coded, + * as is_power_of_2() takes an unsigned long). + */ + top = addr_mask + size; + if (!top || (top & (top - 1))) + return U64_MAX; + highest = max(highest, (base.q & addr_mask) + size); + break; + case MTRR_TYPE_UNCACHABLE: + break; + default: + /* the trim code only acts on WB/UC-only layouts */ + return U64_MAX; + } + } + + if (!highest) + return U64_MAX; + + return highest - 1; +} + static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry, struct boot_params *boot_params) { unsigned long virt_addr = LOAD_PHYSICAL_ADDR; unsigned long addr, alloc_size, entry; efi_status_t status; + unsigned long alloc_max = EFI_X86_KERNEL_ALLOC_LIMIT; u32 seed[2] = {}; + u64 wb_limit; boot_params_ptr = boot_params; @@ -878,10 +986,23 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry, boot_params->hdr.loadflags |= KASLR_FLAG; } + wb_limit = efi_mtrr_wb_limit(); + if (wb_limit < alloc_max) { + efi_debug("Limiting kernel placement to the MTRR WB range below 0x%llx\n", + wb_limit + 1); + alloc_max = wb_limit; + } + status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr, seed[0], EFI_LOADER_CODE, - LOAD_PHYSICAL_ADDR, - EFI_X86_KERNEL_ALLOC_LIMIT); + LOAD_PHYSICAL_ADDR, alloc_max); + if (status != EFI_SUCCESS && alloc_max != EFI_X86_KERNEL_ALLOC_LIMIT) { + efi_warn("No room below the MTRR WB limit - ignoring it\n"); + status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr, + seed[0], EFI_LOADER_CODE, + LOAD_PHYSICAL_ADDR, + EFI_X86_KERNEL_ALLOC_LIMIT); + } if (status != EFI_SUCCESS) return status; @@ -1082,25 +1203,6 @@ void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, * presence of TDX or SEV-SNP, or emit any kind of output if any error * conditions are detected. */ -static bool early_is_tdx_guest(void) -{ - static bool once; - static bool is_tdx; - - if (!IS_ENABLED(CONFIG_INTEL_TDX_GUEST)) - return false; - - if (!once) { - u32 eax = TDX_CPUID_LEAF_ID, sig[3] = {}; - - native_cpuid(&eax, &sig[0], &sig[2], &sig[1]); - is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig)); - once = true; - } - - return is_tdx; -} - void arch_accept_memory(phys_addr_t start, phys_addr_t end) { if (early_is_tdx_guest()) { base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f