Skip to content

Commit 43b1d3e

Browse files
hugelgupfhansendc
authored andcommitted
kexec: Allocate kernel above bzImage's pref_address
A relocatable kernel will relocate itself to pref_address if it is loaded below pref_address. This means a booted kernel may be relocating itself to an area with reserved memory on modern systems, potentially clobbering arbitrary data that may be important to the system. This is often the case, as the default value of PHYSICAL_START is 0x1000000 and kernels are typically loaded at 0x100000 or above by bootloaders like iPXE or kexec. GRUB behaves like the approach implemented here. Also fixes the documentation around pref_address and PHYSICAL_START to be accurate. [ dhansen: changelog tweak ] Co-developed-by: Cloud Hsu <[email protected]> Signed-off-by: Cloud Hsu <[email protected]> Signed-off-by: Chris Koch <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: H. Peter Anvin (Intel) <[email protected]> Link: https://lore.kernel.org/all/20231215190521.3796022-1-chrisko%40google.com
1 parent ac456ca commit 43b1d3e

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

Documentation/arch/x86/boot.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,8 @@ Protocol: 2.10+
878878
address if possible.
879879

880880
A non-relocatable kernel will unconditionally move itself and to run
881-
at this address.
881+
at this address. A relocatable kernel will move itself to this address if it
882+
loaded below this address.
882883

883884
============ =======
884885
Field name: init_size

arch/x86/Kconfig

+5-5
Original file line numberDiff line numberDiff line change
@@ -2114,11 +2114,11 @@ config PHYSICAL_START
21142114
help
21152115
This gives the physical address where the kernel is loaded.
21162116

2117-
If kernel is a not relocatable (CONFIG_RELOCATABLE=n) then
2118-
bzImage will decompress itself to above physical address and
2119-
run from there. Otherwise, bzImage will run from the address where
2120-
it has been loaded by the boot loader and will ignore above physical
2121-
address.
2117+
If the kernel is not relocatable (CONFIG_RELOCATABLE=n) then bzImage
2118+
will decompress itself to above physical address and run from there.
2119+
Otherwise, bzImage will run from the address where it has been loaded
2120+
by the boot loader. The only exception is if it is loaded below the
2121+
above physical address, in which case it will relocate itself there.
21222122

21232123
In normal kdump cases one does not have to set/change this option
21242124
as now bzImage can be compiled as a completely relocatable image

arch/x86/kernel/kexec-bzimage64.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,10 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
503503
kbuf.bufsz = kernel_len - kern16_size;
504504
kbuf.memsz = PAGE_ALIGN(header->init_size);
505505
kbuf.buf_align = header->kernel_alignment;
506-
kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
506+
if (header->pref_address < MIN_KERNEL_LOAD_ADDR)
507+
kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
508+
else
509+
kbuf.buf_min = header->pref_address;
507510
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
508511
ret = kexec_add_buffer(&kbuf);
509512
if (ret)

0 commit comments

Comments
 (0)