Skip to content

Commit 220dd76

Browse files
ryncsningomolnar
authored andcommitted
x86, efi: Never relocate kernel below lowest acceptable address
Currently, kernel fails to boot on some HyperV VMs when using EFI. And it's a potential issue on all x86 platforms. It's caused by broken kernel relocation on EFI systems, when below three conditions are met: 1. Kernel image is not loaded to the default address (LOAD_PHYSICAL_ADDR) by the loader. 2. There isn't enough room to contain the kernel, starting from the default load address (eg. something else occupied part the region). 3. In the memmap provided by EFI firmware, there is a memory region starts below LOAD_PHYSICAL_ADDR, and suitable for containing the kernel. EFI stub will perform a kernel relocation when condition 1 is met. But due to condition 2, EFI stub can't relocate kernel to the preferred address, so it fallback to ask EFI firmware to alloc lowest usable memory region, got the low region mentioned in condition 3, and relocated kernel there. It's incorrect to relocate the kernel below LOAD_PHYSICAL_ADDR. This is the lowest acceptable kernel relocation address. The first thing goes wrong is in arch/x86/boot/compressed/head_64.S. Kernel decompression will force use LOAD_PHYSICAL_ADDR as the output address if kernel is located below it. Then the relocation before decompression, which move kernel to the end of the decompression buffer, will overwrite other memory region, as there is no enough memory there. To fix it, just don't let EFI stub relocate the kernel to any address lower than lowest acceptable address. [ ardb: introduce efi_low_alloc_above() to reduce the scope of the change ] Signed-off-by: Kairui Song <kasong@redhat.com> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Acked-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-efi@vger.kernel.org Link: https://lkml.kernel.org/r/20191029173755.27149-6-ardb@kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 41cd96f commit 220dd76

4 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎arch/x86/boot/compressed/eboot.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <asm/e820/types.h>
1414
#include <asm/setup.h>
1515
#include <asm/desc.h>
16+
#include <asm/boot.h>
1617

1718
#include "../string.h"
1819
#include "eboot.h"
@@ -813,7 +814,8 @@ efi_main(struct efi_config *c, struct boot_params *boot_params)
813814
status = efi_relocate_kernel(sys_table, &bzimage_addr,
814815
hdr->init_size, hdr->init_size,
815816
hdr->pref_address,
816-
hdr->kernel_alignment);
817+
hdr->kernel_alignment,
818+
LOAD_PHYSICAL_ADDR);
817819
if (status != EFI_SUCCESS) {
818820
efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
819821
goto fail;

‎drivers/firmware/efi/libstub/arm32-stub.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
230230
*image_size = image->image_size;
231231
status = efi_relocate_kernel(sys_table, image_addr, *image_size,
232232
*image_size,
233-
kernel_base + MAX_UNCOMP_KERNEL_SIZE, 0);
233+
kernel_base + MAX_UNCOMP_KERNEL_SIZE, 0, 0);
234234
if (status != EFI_SUCCESS) {
235235
pr_efi_err(sys_table, "Failed to relocate kernel.\n");
236236
efi_free(sys_table, *reserve_size, *reserve_addr);

‎drivers/firmware/efi/libstub/efi-stub-helper.c‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
260260
}
261261

262262
/*
263-
* Allocate at the lowest possible address.
263+
* Allocate at the lowest possible address that is not below 'min'.
264264
*/
265-
efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
266-
unsigned long size, unsigned long align,
267-
unsigned long *addr)
265+
efi_status_t efi_low_alloc_above(efi_system_table_t *sys_table_arg,
266+
unsigned long size, unsigned long align,
267+
unsigned long *addr, unsigned long min)
268268
{
269269
unsigned long map_size, desc_size, buff_size;
270270
efi_memory_desc_t *map;
@@ -311,13 +311,8 @@ efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
311311
start = desc->phys_addr;
312312
end = start + desc->num_pages * EFI_PAGE_SIZE;
313313

314-
/*
315-
* Don't allocate at 0x0. It will confuse code that
316-
* checks pointers against NULL. Skip the first 8
317-
* bytes so we start at a nice even number.
318-
*/
319-
if (start == 0x0)
320-
start += 8;
314+
if (start < min)
315+
start = min;
321316

322317
start = round_up(start, align);
323318
if ((start + size) > end)
@@ -698,7 +693,8 @@ efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
698693
unsigned long image_size,
699694
unsigned long alloc_size,
700695
unsigned long preferred_addr,
701-
unsigned long alignment)
696+
unsigned long alignment,
697+
unsigned long min_addr)
702698
{
703699
unsigned long cur_image_addr;
704700
unsigned long new_addr = 0;
@@ -731,8 +727,8 @@ efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
731727
* possible.
732728
*/
733729
if (status != EFI_SUCCESS) {
734-
status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
735-
&new_addr);
730+
status = efi_low_alloc_above(sys_table_arg, alloc_size,
731+
alignment, &new_addr, min_addr);
736732
}
737733
if (status != EFI_SUCCESS) {
738734
pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");

‎include/linux/efi.h‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,9 +1579,22 @@ char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
15791579
efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
15801580
struct efi_boot_memmap *map);
15811581

1582+
efi_status_t efi_low_alloc_above(efi_system_table_t *sys_table_arg,
1583+
unsigned long size, unsigned long align,
1584+
unsigned long *addr, unsigned long min);
1585+
1586+
static inline
15821587
efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
15831588
unsigned long size, unsigned long align,
1584-
unsigned long *addr);
1589+
unsigned long *addr)
1590+
{
1591+
/*
1592+
* Don't allocate at 0x0. It will confuse code that
1593+
* checks pointers against NULL. Skip the first 8
1594+
* bytes so we start at a nice even number.
1595+
*/
1596+
return efi_low_alloc_above(sys_table_arg, size, align, addr, 0x8);
1597+
}
15851598

15861599
efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
15871600
unsigned long size, unsigned long align,
@@ -1592,7 +1605,8 @@ efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
15921605
unsigned long image_size,
15931606
unsigned long alloc_size,
15941607
unsigned long preferred_addr,
1595-
unsigned long alignment);
1608+
unsigned long alignment,
1609+
unsigned long min_addr);
15961610

15971611
efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
15981612
efi_loaded_image_t *image,

0 commit comments

Comments
 (0)