Re: [PATCH v2 2/2] efi/libstub: Fix page table access in 5-level to 4-level paging transition
From: Borislav Petkov
Date: Fri Oct 31 2025 - 10:40:27 EST
On Tue, Oct 28, 2025 at 10:55:57AM +0000, Usama Arif wrote:
> 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 properly clearing SME encryption
> bit and extracting only the physical address portion.
> - mask pgd value with PTE_PFN_MASK instead of PAGE_MASK, accounting for
> flags above physical address (_PAGE_BIT_NOPTISHADOW in particular).
>
> Fixes: cb1c9e02b0c1 ("x86/efistub: Perform 4/5 level paging switch from the stub")
> Co-developed-by: Kiryl Shutsemau <kas@xxxxxxxxxx>
> Signed-off-by: Kiryl Shutsemau <kas@xxxxxxxxxx>
> Signed-off-by: Usama Arif <usamaarif642@xxxxxxxxx>
> Reported-by: Michael van der Westhuizen <rmikey@xxxxxxxx>
> Reported-by: Tobias Fleig <tfleig@xxxxxxxx>
> ---
> drivers/firmware/efi/libstub/x86-5lvl.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
allmodconfig build:
ld: error: unplaced orphan section `__bug_table' from `drivers/firmware/efi/libstub/x86-5lvl.stub.o'
ld: error: unplaced orphan section `.altinstructions' from `drivers/firmware/efi/libstub/x86-5lvl.stub.o'
ld: error: unplaced orphan section `.altinstr_replacement' from `drivers/firmware/efi/libstub/x86-5lvl.stub.o'
ld: Unexpected GOT/PLT entries detected!
ld: Unexpected run-time procedure linkages detected!
ld: Unexpected run-time relocations (.rela) detected!
ld: drivers/firmware/efi/libstub/x86-5lvl.stub.o: in function `efi_5level_switch':
x86-5lvl.c:(.text+0x13e): undefined reference to `pv_ops'
ld: x86-5lvl.c:(.text+0x14d): undefined reference to `pv_ops'
ld: drivers/firmware/efi/libstub/x86-5lvl.stub.o:(.altinstr_replacement+0x1): undefined reference to `BUG_func'
ld: arch/x86/boot/compressed/vmlinux: hidden symbol `pv_ops' isn't defined
ld: final link failed: bad value
make[3]: *** [arch/x86/boot/compressed/Makefile:116: arch/x86/boot/compressed/vmlinux] Error 1
make[2]: *** [arch/x86/boot/Makefile:96: arch/x86/boot/compressed/vmlinux] Error 2
make[1]: *** [arch/x86/Makefile:308: bzImage] Error 2
make: *** [Makefile:248: __sub-make] Error 2
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette