Skip to content

Commit 2390e90

Browse files
uarif1gregkh
authored andcommitted
efi/libstub: Fix page table access in 5-level to 4-level paging transition
[ Upstream commit 8436112 ] When transitioning from 5-level to 4-level paging, the existing code incorrectly accesses page table entries by directly dereferencing CR3 and applying PAGE_MASK. This approach has several issues: - __native_read_cr3() returns the raw CR3 register value, which on x86_64 includes not just the physical address but also flags Bits above the physical address width of the system (i.e. above __PHYSICAL_MASK_SHIFT) are also not masked. - The pgd value is masked by PAGE_SIZE which doesn't take into account the higher bits such as _PAGE_BIT_NOPTISHADOW. Replace this with proper accessor functions: - native_read_cr3_pa(): Uses CR3_ADDR_MASK to additionally mask metadata out of CR3 (like SME or LAM bits). All remaining bits are real address bits or reserved and must be 0. - mask pgd value with PTE_PFN_MASK instead of PAGE_MASK, accounting for flags above bit 51 (_PAGE_BIT_NOPTISHADOW in particular). Bits below 51, but above the max physical address are reserved and must be 0. Fixes: cb1c9e0 ("x86/efistub: Perform 4/5 level paging switch from the stub") Reported-by: Michael van der Westhuizen <rmikey@meta.com> Reported-by: Tobias Fleig <tfleig@meta.com> Co-developed-by: Kiryl Shutsemau <kas@kernel.org> Signed-off-by: Kiryl Shutsemau <kas@kernel.org> Signed-off-by: Usama Arif <usamaarif642@gmail.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Link: https://patch.msgid.link/20251103141002.2280812-3-usamaarif642@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b299217 commit 2390e90

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

drivers/firmware/efi/libstub/x86-5lvl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void efi_5level_switch(void)
6666
bool have_la57 = native_read_cr4() & X86_CR4_LA57;
6767
bool need_toggle = want_la57 ^ have_la57;
6868
u64 *pgt = (void *)la57_toggle + PAGE_SIZE;
69-
u64 *cr3 = (u64 *)__native_read_cr3();
69+
pgd_t *cr3 = (pgd_t *)native_read_cr3_pa();
7070
u64 *new_cr3;
7171

7272
if (!la57_toggle || !need_toggle)
@@ -82,7 +82,7 @@ void efi_5level_switch(void)
8282
new_cr3[0] = (u64)cr3 | _PAGE_TABLE_NOENC;
8383
} else {
8484
/* take the new root table pointer from the current entry #0 */
85-
new_cr3 = (u64 *)(cr3[0] & PAGE_MASK);
85+
new_cr3 = (u64 *)(native_pgd_val(cr3[0]) & PTE_PFN_MASK);
8686

8787
/* copy the new root table if it is not 32-bit addressable */
8888
if ((u64)new_cr3 > U32_MAX)

0 commit comments

Comments
 (0)