Skip to content

Commit b299217

Browse files
uarif1gregkh
authored andcommitted
x86/boot: Fix page table access in 5-level to 4-level paging transition
[ Upstream commit eb22663 ] 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 entry 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: e9d0e63 ("x86/boot/compressed/64: Prepare new top-level page table for trampoline") 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> Acked-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lore.kernel.org/r/a482fd68-ce54-472d-8df1-33d6ac9f6bb5@intel.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2e1132b commit b299217

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

arch/x86/boot/compressed/pgtable_64.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <asm/bootparam.h>
44
#include <asm/bootparam_utils.h>
55
#include <asm/e820/types.h>
6+
#include <asm/pgtable.h>
67
#include <asm/processor.h>
78
#include "pgtable.h"
89
#include "../string.h"
@@ -176,9 +177,10 @@ asmlinkage void configure_5level_paging(struct boot_params *bp, void *pgtable)
176177
* For 4- to 5-level paging transition, set up current CR3 as
177178
* the first and the only entry in a new top-level page table.
178179
*/
179-
*trampoline_32bit = __native_read_cr3() | _PAGE_TABLE_NOENC;
180+
*trampoline_32bit = native_read_cr3_pa() | _PAGE_TABLE_NOENC;
180181
} else {
181-
unsigned long src;
182+
u64 *new_cr3;
183+
pgd_t *pgdp;
182184

183185
/*
184186
* For 5- to 4-level paging transition, copy page table pointed
@@ -188,8 +190,9 @@ asmlinkage void configure_5level_paging(struct boot_params *bp, void *pgtable)
188190
* We cannot just point to the page table from trampoline as it
189191
* may be above 4G.
190192
*/
191-
src = *(unsigned long *)__native_read_cr3() & PAGE_MASK;
192-
memcpy(trampoline_32bit, (void *)src, PAGE_SIZE);
193+
pgdp = (pgd_t *)native_read_cr3_pa();
194+
new_cr3 = (u64 *)(native_pgd_val(pgdp[0]) & PTE_PFN_MASK);
195+
memcpy(trampoline_32bit, new_cr3, PAGE_SIZE);
193196
}
194197

195198
toggle_la57(trampoline_32bit);

0 commit comments

Comments
 (0)