Skip to contentSkip to conversation

x86/efistub: fix the missing KASLR_FLAG bit in boot_params->hdr.loadflags

PATCH In review Yuntao Wang 3 participants 4 messages Dec 26, 2023 – Jan 2, 2024 EFI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/efistub: fix the missing KASLR_FLAG bit in boot_params->hdr.loadflags

When KASLR is enabled, the KASLR_FLAG bit in boot_params->hdr.loadflags should be set to 1 to propagate KASLR status from compressed kernel to kernel, just as the choose_random_location() function does.

Currently, when kernel is booted via efi stub, the KASLR_FLAG bit in boot_params->hdr.loadflags is not set, even though it should be. This causes some functions, such as kernel_randomize_memory(), not to execute as expected. Fix it.

---
 drivers/firmware/efi/libstub/x86-stub.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/x86-stub.c +4 -1
index da9b7b8d0716..b0c68593ad40 100644
@@ -787,7 +787,10 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
 			efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
 			seed[0] = 0;
 		}
-	}
+
+		boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
+	} else
+		boot_params_ptr->hdr.loadflags &= ~KASLR_FLAG;
 
 	status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
 				  seed[0], EFI_LOADER_CODE,
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 20 lines · Yuntao Wang

When KASLR is enabled, the KASLR_FLAG bit in boot_params->hdr.loadflags should be set to 1 to propagate KASLR status from compressed kernel to kernel, just as the choose_random_location() function does.

Currently, when kernel is booted via efi stub, the KASLR_FLAG bit in boot_params->hdr.loadflags is not set, even though it should be. This causes some functions, such as kernel_randomize_memory(), not to execute as expected. Fix it.

---
 drivers/firmware/efi/libstub/x86-stub.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/x86-stub.c +0 -0
index da9b7b8d0716..b0c68593ad40 100644

drivers/firmware/efi/libstub/x86-stub.c static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry) · line 794

Quoted 9 lines
drivers/firmware/efi/libstub/x86-stub.c +4 -1
@@ -787,7 +787,10 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
                        efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
                        seed[0] = 0;
                }
-       }
+
+               boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
+       } else
+               boot_params_ptr->hdr.loadflags &= ~KASLR_FLAG;
 

Thanks for the fix.

I'll queue this up right away, but I am going to drop the 'else' part, given that the KASLR flag is never set by the EFI stub so clearing it should never be needed.

Quoted 5 lines
drivers/firmware/efi/libstub/x86-stub.c +0 -1
        status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
                                  seed[0], EFI_LOADER_CODE,
--

2.43.0

#2 lore raw
Quoted 16 lines · Ard Biesheuvel
Quoted 15 lines · Yuntao Wang

When KASLR is enabled, the KASLR_FLAG bit in boot_params->hdr.loadflags should be set to 1 to propagate KASLR status from compressed kernel to kernel, just as the choose_random_location() function does.

Currently, when kernel is booted via efi stub, the KASLR_FLAG bit in boot_params->hdr.loadflags is not set, even though it should be. This causes some functions, such as kernel_randomize_memory(), not to execute as expected. Fix it.

---
  drivers/firmware/efi/libstub/x86-stub.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
drivers/firmware/efi/libstub/x86-stub.c +0 -0
index da9b7b8d0716..b0c68593ad40 100644

drivers/firmware/efi/libstub/x86-stub.c static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry) · line 794

Quoted 9 lines
drivers/firmware/efi/libstub/x86-stub.c +4 -1
@@ -787,7 +787,10 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
                         efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
                         seed[0] = 0;
                 }
-       }
+
+               boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
+       } else
+               boot_params_ptr->hdr.loadflags &= ~KASLR_FLAG;
 

Thanks for the fix.

Does it need a Fixes: tag?

Thanks,
Tom

Quoted 11 lines

I'll queue this up right away, but I am going to drop the 'else' part, given that the KASLR flag is never set by the EFI stub so clearing it should never be needed.

Quoted 5 lines
drivers/firmware/efi/libstub/x86-stub.c +0 -1
         status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
                                   seed[0], EFI_LOADER_CODE,
--

2.43.0

maintainer
#3 lore raw
Quoted 35 lines · Tom Lendacky
Quoted 16 lines · Ard Biesheuvel
Quoted 15 lines · Yuntao Wang

When KASLR is enabled, the KASLR_FLAG bit in boot_params->hdr.loadflags should be set to 1 to propagate KASLR status from compressed kernel to kernel, just as the choose_random_location() function does.

Currently, when kernel is booted via efi stub, the KASLR_FLAG bit in boot_params->hdr.loadflags is not set, even though it should be. This causes some functions, such as kernel_randomize_memory(), not to execute as expected. Fix it.

---
  drivers/firmware/efi/libstub/x86-stub.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
drivers/firmware/efi/libstub/x86-stub.c +0 -0
index da9b7b8d0716..b0c68593ad40 100644

drivers/firmware/efi/libstub/x86-stub.c static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry) · line 794

Quoted 9 lines
drivers/firmware/efi/libstub/x86-stub.c +4 -1
@@ -787,7 +787,10 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
                         efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
                         seed[0] = 0;
                 }
-       }
+
+               boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
+       } else
+               boot_params_ptr->hdr.loadflags &= ~KASLR_FLAG;
 

Thanks for the fix.

Quoted 2 lines

Does it need a Fixes: tag?

Yes, I've added that.