LoongArch: Move KASLR to EFI stub to avoid initrd overlap
v2 cover LoongArch: Move KASLR to EFI stub to avoid initrd overlap
Changes since [v1]:
* Drop the patch "LoongArch: Allow rdtime_h() and rdtime_l() in 64-bit builds".
* Use random_get_entropy() instead of rdtime_l().
This series addresses a potential overlap issue between the kernel image and the initrd when KASLR is enabled.
In the normal boot flow, the bootloader is responsible for loading both vmlinux and the initrd, and it can guarantee that the two do not overlap in memory. However, this assumption only holds as long as neither image changes its location afterwards.
The in-kernel KASLR implementation breaks that assumption. When the initrd is placed close to the kernel image, randomizing the kernel location at runtime may move it into the initrd region, leading to memory corruption early during boot.
To fix this, this series moves the KASLR logic out of the kernel proper and into the EFI stub. With this change, the final placement of both the kernel image and the initrd is determined by the EFI memory allocator. This ensures that the two allocations are coordinated and cannot overlap.
Functionally, the kernel still supports KASLR as before, but the randomization now happens before the kernel is entered, rather than during early kernel relocation.
[v1]: https://lore.kernel.org/loongarch/[email protected]
WANG Rui (2):
efi/loongarch: Randomize kernel preferred address for KASLR
LoongArch: Skip relocation-time KASLR if it has already been applied
arch/loongarch/Kconfig | 2 +- arch/loongarch/include/asm/efi.h | 4 +++- arch/loongarch/kernel/relocate.c | 4 ++++ drivers/firmware/efi/libstub/loongarch.c | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/2 efi/loongarch: Randomize kernel preferred address for KASLR
Introduce efi_get_kimg_kaslr_address() to compute the preferred kernel image address dynamically when CONFIG_RANDOMIZE_BASE is enabled. The function derives a random offset using EFI-provided randomness combined with the timer value, and constrains it within CONFIG_RANDOMIZE_BASE_MAX_OFFSET.
Update EFI_KIMG_PREFERRED_ADDRESS to call this helper so that the EFI stub can select a randomized load address when KASLR is active, while preserving the original base address behavior when KASLR is disabled or nokaslr is specified.
--- arch/loongarch/Kconfig | 2 +- arch/loongarch/include/asm/efi.h | 4 +++- drivers/firmware/efi/libstub/loongarch.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-)
arch/loongarch/Kconfig +1 -1
@@ -730,7 +730,7 @@ config RANDOMIZE_BASE config RANDOMIZE_BASE_MAX_OFFSET hex "Maximum KASLR offset" if EXPERT depends on RANDOMIZE_BASE - range 0x0 0x10000000 + range 0x20000 0x10000000 default "0x01000000" help When KASLR is active, this provides the maximum offset that will
arch/loongarch/include/asm/efi.h +3 -1
@@ -30,6 +30,8 @@ static inline unsigned long efi_get_kimg_min_align(void) return SZ_2M; } -#define EFI_KIMG_PREFERRED_ADDRESS PHYSADDR(VMLINUX_LOAD_ADDRESS) +unsigned long efi_get_kimg_kaslr_address (void ); + +#define EFI_KIMG_PREFERRED_ADDRESS efi_get_kimg_kaslr_address()#endif /* _ASM_LOONGARCH_EFI_H */
drivers/firmware/efi/libstub/loongarch.c +16 -0
@@ -38,6 +38,22 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)return EFI_SUCCESS ; } +unsigned long efi_get_kimg_kaslr_address (void ) +{ +unsigned int random_offset =0 ; + +#ifdef CONFIG_RANDOMIZE_BASE +if (!efi_nokaslr) { +efi_get_random_bytes (sizeof (random_offset), (u8 *)&random_offset); + random_offset ^= (random_get_entropy () <<16 ); + random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET -SZ_64K -1 ); + random_offset =ALIGN (random_offset,SZ_64K ) +SZ_64K ; + } +#endif + +return PHYSADDR (VMLINUX_LOAD_ADDRESS ) + random_offset; +} +unsigned long __weak kernel_entry_address (unsigned long kernel_addr,efi_loaded_image_t *image) {
Signature · 1 line
2.54.0
Huacai Chen
maintainerRe: [PATCH v2 1/2] efi/loongarch: Randomize kernel preferred address for KASLR
Hi, Rui,
Quoted 51 lines · WANG Rui
Introduce
efi_get_kimg_kaslr_address()to compute the preferred kernel image address dynamically whenCONFIG_RANDOMIZE_BASEis enabled. The function derives a random offset using EFI-provided randomness combined with the timer value, and constrains it withinCONFIG_RANDOMIZE_BASE_MAX_OFFSET.Update
EFI_KIMG_PREFERRED_ADDRESSto call this helper so that the EFI stub can select a randomized load address when KASLR is active, while preserving the original base address behavior when KASLR is disabled or nokaslr is specified.Signed-off-by WANG Rui <[email protected]>--- arch/loongarch/Kconfig | 2 +- arch/loongarch/include/asm/efi.h | 4 +++- drivers/firmware/efi/libstub/loongarch.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-)arch/loongarch/Kconfig +1
-1@@ -730,7 +730,7 @@ config RANDOMIZE_BASE config RANDOMIZE_BASE_MAX_OFFSET hex "Maximum KASLR offset" if EXPERT depends on RANDOMIZE_BASE - range 0x0 0x10000000 + range 0x20000 0x10000000 default "0x01000000" help When KASLR is active, this provides the maximum offset that willarch/loongarch/include/asm/efi.h +3
-1@@ -30,6 +30,8 @@ static inline unsigned long efi_get_kimg_min_align(void) return SZ_2M; } -#define EFI_KIMG_PREFERRED_ADDRESS PHYSADDR(VMLINUX_LOAD_ADDRESS) +unsigned long efi_get_kimg_kaslr_address (void ); + +#define EFI_KIMG_PREFERRED_ADDRESS efi_get_kimg_kaslr_address()#endif /* _ASM_LOONGARCH_EFI_H */ drivers/firmware/efi/libstub/loongarch.c +0
-0
drivers/firmware/efi/libstub/loongarch.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) · line 41
Quoted 4 lines
drivers/firmware/efi/libstub/loongarch.c +1
-0@@ -38,6 +38,22 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)return EFI_SUCCESS ; } +unsigned long efi_get_kimg_kaslr_address (void )
Move it into arch/loongarch/include/asm/efi.h?
drivers/firmware/efi/libstub/loongarch.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) · line 49
Quoted 8 lines
drivers/firmware/efi/libstub/loongarch.c +8
-0+{ +unsigned int random_offset =0 ; + +#ifdef CONFIG_RANDOMIZE_BASE +if (!efi_nokaslr) { +efi_get_random_bytes (sizeof (random_offset), (u8 *)&random_offset); + random_offset ^= (random_get_entropy () <<16 ); + random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET -SZ_64K -1 );
I still don't want to modify the range of RANDOMIZE_BASE_MAX_OFFSET, so use
random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1)
here?
drivers/firmware/efi/libstub/loongarch.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) · line 50
Quoted 1 line
drivers/firmware/efi/libstub/loongarch.c +1
-0+ random_offset =ALIGN (random_offset,SZ_64K ) +SZ_64K ;
It seems "random_offset = ALIGN(random_offset + SZ64K, SZ_64K)" is better.
Huacai
Quoted 12 lines
drivers/firmware/efi/libstub/loongarch.c +6
-1+ } +#endif + +return PHYSADDR (VMLINUX_LOAD_ADDRESS ) + random_offset; +} +unsigned long __weak kernel_entry_address (unsigned long kernel_addr,efi_loaded_image_t *image) { --2.54.0
Re: [PATCH v2 1/2] efi/loongarch: Randomize kernel preferred address for KASLR
Hi Huacai,
Quoted 58 lines · Huacai Chen
Hi, Rui,
Quoted 54 lines · WANG Rui
Introduce
efi_get_kimg_kaslr_address()to compute the preferred kernel image address dynamically whenCONFIG_RANDOMIZE_BASEis enabled. The function derives a random offset using EFI-provided randomness combined with the timer value, and constrains it withinCONFIG_RANDOMIZE_BASE_MAX_OFFSET.Update
EFI_KIMG_PREFERRED_ADDRESSto call this helper so that the EFI stub can select a randomized load address when KASLR is active, while preserving the original base address behavior when KASLR is disabled or nokaslr is specified.Signed-off-by WANG Rui <[email protected]>--- arch/loongarch/Kconfig | 2 +- arch/loongarch/include/asm/efi.h | 4 +++- drivers/firmware/efi/libstub/loongarch.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-)arch/loongarch/Kconfig +1
-1@@ -730,7 +730,7 @@ config RANDOMIZE_BASE config RANDOMIZE_BASE_MAX_OFFSET hex "Maximum KASLR offset" if EXPERT depends on RANDOMIZE_BASE - range 0x0 0x10000000 + range 0x20000 0x10000000 default "0x01000000" help When KASLR is active, this provides the maximum offset that willarch/loongarch/include/asm/efi.h +3
-1@@ -30,6 +30,8 @@ static inline unsigned long efi_get_kimg_min_align(void) return SZ_2M; } -#define EFI_KIMG_PREFERRED_ADDRESS PHYSADDR(VMLINUX_LOAD_ADDRESS) +unsigned long efi_get_kimg_kaslr_address (void ); + +#define EFI_KIMG_PREFERRED_ADDRESS efi_get_kimg_kaslr_address()#endif /* _ASM_LOONGARCH_EFI_H */ drivers/firmware/efi/libstub/loongarch.c +0
-0@@ -38,6 +38,22 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)return EFI_SUCCESS ; }
Quoted 2 lines
drivers/firmware/efi/libstub/loongarch.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) · line 41
Quoted 1 line
drivers/firmware/efi/libstub/loongarch.c +1
-0+unsigned long efi_get_kimg_kaslr_address (void )Move it into
arch/loongarch/include/asm/efi.h?
In that case, we'd need to declare efi_nokaslr and efi_get_random_bytes() in efi.h. Since including efistub.h in efi.h doesn't work, putting them here keeps things pretty clean.
Thanks,
Rui
Quoted 31 lines
drivers/firmware/efi/libstub/loongarch.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) · line 49
Quoted 9 lines
drivers/firmware/efi/libstub/loongarch.c +8
-0+{ +unsigned int random_offset =0 ; + +#ifdef CONFIG_RANDOMIZE_BASE +if (!efi_nokaslr) { +efi_get_random_bytes (sizeof (random_offset), (u8 *)&random_offset); + random_offset ^= (random_get_entropy () <<16 ); + random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET -SZ_64K -1 );I still don't want to modify the range of
RANDOMIZE_BASE_MAX_OFFSET, so use
random_offset&= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET- 1)
here?drivers/firmware/efi/libstub/loongarch.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) · line 50
Quoted 1 line
drivers/firmware/efi/libstub/loongarch.c +1
-0+ random_offset =ALIGN (random_offset,SZ_64K ) +SZ_64K ;It seems "
random_offset=ALIGN(random_offset + SZ64K, SZ_64K)" is better.Huacai
Quoted 12 lines
drivers/firmware/efi/libstub/loongarch.c +6
-1+ } +#endif + +return PHYSADDR (VMLINUX_LOAD_ADDRESS ) + random_offset; +} +unsigned long __weak kernel_entry_address (unsigned long kernel_addr,efi_loaded_image_t *image) { --2.54.0
PATCH v2 2/2 LoongArch: Skip relocation-time KASLR if it has already been applied
In reply to WANG RuiWhen the kernel is relocated during early boot, a randomized load address may already have been selected and applied. In this case, performing KASLR again in relocate.c is unnecessary.
--- arch/loongarch/kernel/relocate.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/loongarch/kernel/relocate.c +4 -0
@@ -139,6 +139,10 @@ static inline __init bool kaslr_disabled(void)char *str;const char *builtin_cmdline =CONFIG_CMDLINE ; +/* KASLR is performed during early boot. */ +if (kaslr_offset ()) +return true; + str =strstr (builtin_cmdline,"nokaslr" );if (str == builtin_cmdline || (str > builtin_cmdline && *(str -1 ) ==' ' )) {pr_info (KASLR_DISABLED_MESSAGE ,"\'nokaslr\'" ,"built-in" );
Signature · 1 line
2.54.0