Skip to content

Commit 15aa8fb

Browse files
committed
x86/efistub: Omit physical KASLR when memory reservations exist
The legacy decompressor has elaborate logic to ensure that the randomized physical placement of the decompressed kernel image does not conflict with any memory reservations, including ones specified on the command line using mem=, memmap=, efi_fake_mem= or hugepages=, which are taken into account by the kernel proper at a later stage. When booting in EFI mode, it is the firmware's job to ensure that the chosen range does not conflict with any memory reservations that it knows about, and this is trivially achieved by using the firmware's memory allocation APIs. That leaves reservations specified on the command line, though, which the firmware knows nothing about, as these regions have no other special significance to the platform. Since commit a1b87d5 ("x86/efistub: Avoid legacy decompressor when doing EFI boot") these reservations are not taken into account when randomizing the physical placement, which may result in conflicts where the memory cannot be reserved by the kernel proper because its own executable image resides there. To avoid having to duplicate or reuse the existing complicated logic, disable physical KASLR entirely when such overrides are specified. These are mostly diagnostic tools or niche features, and physical KASLR (as opposed to virtual KASLR, which is much more important as it affects the memory addresses observed by code executing in the kernel) is something we can live without. Closes: https://lkml.kernel.org/r/FA5F6719-8824-4B04-803E-82990E65E627%40akamai.com Reported-by: Ben Chaney <bchaney@akamai.com> Fixes: a1b87d5 ("x86/efistub: Avoid legacy decompressor when doing EFI boot") Cc: <stable@vger.kernel.org> # v6.1+ Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
1 parent 4b2543f commit 15aa8fb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

drivers/firmware/efi/libstub/x86-stub.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,26 @@ static void error(char *str)
775775
efi_warn("Decompression failed: %s\n", str);
776776
}
777777

778+
static const char *cmdline_memmap_override;
779+
780+
static efi_status_t parse_options(const char *cmdline)
781+
{
782+
static const char opts[][14] = {
783+
"mem=", "memmap=", "efi_fake_mem=", "hugepages="
784+
};
785+
786+
for (int i = 0; i < ARRAY_SIZE(opts); i++) {
787+
const char *p = strstr(cmdline, opts[i]);
788+
789+
if (p == cmdline || (p > cmdline && isspace(p[-1]))) {
790+
cmdline_memmap_override = opts[i];
791+
break;
792+
}
793+
}
794+
795+
return efi_parse_options(cmdline);
796+
}
797+
778798
static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
779799
{
780800
unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
@@ -806,6 +826,10 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
806826
!memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
807827
efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
808828
seed[0] = 0;
829+
} else if (cmdline_memmap_override) {
830+
efi_info("%s detected on the kernel command line - disabling physical KASLR\n",
831+
cmdline_memmap_override);
832+
seed[0] = 0;
809833
}
810834

811835
boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
@@ -882,7 +906,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle,
882906
}
883907

884908
#ifdef CONFIG_CMDLINE_BOOL
885-
status = efi_parse_options(CONFIG_CMDLINE);
909+
status = parse_options(CONFIG_CMDLINE);
886910
if (status != EFI_SUCCESS) {
887911
efi_err("Failed to parse options\n");
888912
goto fail;
@@ -891,7 +915,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle,
891915
if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
892916
unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
893917
((u64)boot_params->ext_cmd_line_ptr << 32));
894-
status = efi_parse_options((char *)cmdline_paddr);
918+
status = parse_options((char *)cmdline_paddr);
895919
if (status != EFI_SUCCESS) {
896920
efi_err("Failed to parse options\n");
897921
goto fail;

0 commit comments

Comments
 (0)