Skip to content

LoongArch: Move KASLR to EFI stub to avoid initrd overlap

WANG Rui linux-efi lkml loongarch 5 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest3 patches 0 repliesNew
  2. v43 patches 1 replyApplied
  3. v33 patches 6 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v22 patches2 repliesSuperseded
v13 patches4 repliesSuperseded

Changes in v5 from the cover letter

* Add a comment above kaslr_disabled() introducing the terms "strictly-defined KASLR" and "broadly-defined KASLR", and explaining what kaslr_disabled() returns. (Huacai Chen)
* Set efi_nokaslr when "resume=<devname>" is present, since LoongArch hibernation does not support KASLR. (Huacai Chen)
* Adjust local variable declarations, comment wording, and function placement. (Huacai Chen)

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3efi/loongarch: Randomize kernel preferred address for KASLRarch/loongarch/include/asm/efi.h drivers/firmware/efi/libstub/efi-stub-helper.c drivers/firmware/efi/libstub/loongarch.c+23 -10 replies
2/3LoongArch: Skip relocation-time KASLR if already appliedarch/loongarch/kernel/relocate.c+12 -00 replies
3/3LoongArch: Avoid initrd overlap during kernel relocationarch/loongarch/kernel/relocate.c+38 -00 replies

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchArd BiesheuvelHuacai Chen
1/3A-b Co-authored-by
2/3A-b Co-authored-by
3/3A-b Co-authored-by

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v5 thread

Cover letter [PATCH v5 0/3] LoongArch: Move KASLR to EFI stub to avoid initrd overlap

Changes since [v4]:
* Add a comment above kaslr_disabled() introducing the terms "strictly-defined KASLR" and "broadly-defined KASLR", and explaining what kaslr_disabled() returns. (Huacai Chen)
* Set efi_nokaslr when "resume=<devname>" is present, since LoongArch hibernation does not support KASLR. (Huacai Chen)
* Adjust local variable declarations, comment wording, and function placement. (Huacai Chen)

Changes since [v3]:
* Minor cleanups based on review comments.

Changes since [v2]:
* Add a new patch to prevent initrd overlap during relocation.
* Revert changes to the CONFIG_RANDOMIZE_BASE_MAX_OFFSET range.

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.

[v4]: https://lore.kernel.org/loongarch/[email protected]
[v3]: https://lore.kernel.org/loongarch/[email protected]
[v2]: https://lore.kernel.org/loongarch/[email protected]
[v1]: https://lore.kernel.org/loongarch/[email protected]

WANG Rui (3):
efi/loongarch: Randomize kernel preferred address for KASLR
LoongArch: Skip relocation-time KASLR if already applied
LoongArch: Avoid initrd overlap during kernel relocation

 arch/loongarch/include/asm/efi.h              |  4 +-
 arch/loongarch/kernel/relocate.c              | 50 +++++++++++++++++++
 .../firmware/efi/libstub/efi-stub-helper.c    |  4 ++
 drivers/firmware/efi/libstub/loongarch.c      | 16 ++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
Signature · 1 line
2.54.0