x86: Make the 64-bit bzImage always physically relocatable
PATCH 1/1 x86: Make the 64-bit bzImage always physically relocatable
From: Ard Biesheuvel <[email protected]>
On x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.
On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in Kconfig and in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whether CONFIG_RELOCATABLE is set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite for EFI_STUB and RANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.
--- arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)
arch/x86/Kconfig +24 -16
@@ -1898,7 +1898,7 @@ config EFI config EFI_STUB bool "EFI stub support" depends on EFI - select RELOCATABLE + select RELOCATABLE if X86_32 help This kernel feature allows a bzImage to be loaded directly by EFI firmware without the use of a bootloader.
@@ -1995,8 +1995,9 @@ config PHYSICAL_START help This gives the physical address where the kernel is loaded. - If the kernel is not relocatable (CONFIG_RELOCATABLE=n) then bzImage - will decompress itself to above physical address and run from there. + If the kernel is not relocatable (CONFIG_RELOCATABLE=n) and built for + i386, then the bzImage will decompress itself to the above physical + address and run from there. Otherwise, bzImage will run from the address where it has been loaded by the boot loader. The only exception is if it is loaded below the above physical address, in which case it will relocate itself there.
@@ -2031,16 +2032,22 @@ config PHYSICAL_START Don't change this unless you know what you are doing. config RELOCATABLE - bool "Build a relocatable kernel" - default y + bool "Build a relocatable kernel" if X86_32 + default X86_32 help - This builds a kernel image that retains relocation information - so it can be loaded someplace besides the default 1MB. + This builds a kernel image that retains relocation information so it + can be placed someplace besides the default PAGE_OFFSET + 1MB. This + is a prerequisite for KASLR. The relocations tend to make the kernel binary about 10% larger, but are discarded at runtime. - One use is for the kexec on panic case where the recovery kernel - must live at a different physical address than the primary + On i386, where the virtual and physical load offset of the kernel + must be equal, this also allows the kernel image to be placed at a + physical load address that differs from the compile time default. On + x86_64, this is always permitted. + + One use is for the kexec on panic case on i386, where the recovery + kernel must live at a different physical address than the primary kernel. Note: If CONFIG_RELOCATABLE=y, then the kernel runs from the address
@@ -2049,7 +2056,7 @@ config RELOCATABLE config RANDOMIZE_BASE bool "Randomize the address of the kernel image (KASLR)" - depends on RELOCATABLE + select RELOCATABLE default y help In support of Kernel Address Space Layout Randomization (KASLR),
@@ -2085,7 +2092,7 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build support config X86_NEED_RELOCS def_bool y - depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on RELOCATABLE select ARCH_VMLINUX_NEEDS_RELOCS config PHYSICAL_ALIGN
@@ -2098,12 +2105,13 @@ config PHYSICAL_ALIGN where kernel is loaded and run from. Kernel is compiled for an address which meets above alignment restriction. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is set, kernel will move itself to nearest - address aligned to above value and run from there. + If the bootloader loads the kernel at a non-aligned address and it + is built for x86_64 or CONFIG_RELOCATABLE is set, the kernel will + move itself to the nearest address aligned to above value and run + from there. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is not set, kernel will ignore the run time + If the bootloader loads the i386 kernel at a non-aligned address and + CONFIG_RELOCATABLE is not set, the kernel will ignore the run time load address and decompress itself to the address it has been compiled for and run from there. The address for which kernel is compiled already meets above alignment restrictions. Hence the
arch/x86/boot/compressed/head_64.S +0 -4
@@ -143,7 +143,6 @@ SYM_FUNC_START(startup_32) * for safe in-place decompression. */ -#ifdef CONFIG_RELOCATABLE movl %ebp, %ebx movl BP_kernel_alignment(%esi), %eax decl %eax
@@ -152,7 +151,6 @@ SYM_FUNC_START(startup_32) andl %eax, %ebx cmpl $LOAD_PHYSICAL_ADDR, %ebx jae 1f -#endif movl $LOAD_PHYSICAL_ADDR, %ebx 1:
@@ -316,7 +314,6 @@ SYM_CODE_START(startup_64) */ /* Start with the delta to where the kernel will run at. */ -#ifdef CONFIG_RELOCATABLE leaq startup_32(%rip) /* - $startup_32 */, %rbp movl BP_kernel_alignment(%rsi), %eax decl %eax
@@ -325,7 +322,6 @@ SYM_CODE_START(startup_64) andq %rax, %rbp cmpq $LOAD_PHYSICAL_ADDR, %rbp jae 1f -#endif movq $LOAD_PHYSICAL_ADDR, %rbp 1:
arch/x86/boot/compressed/misc.c +3 -5
@@ -314,12 +314,10 @@ static size_t parse_elf(void *output)if ((phdr->p_align %0x200000 ) !=0 )error ("Alignment of LOAD segment isn't multiple of 2MB" );#endif -#ifdef CONFIG_RELOCATABLE - dest = output; - dest += (phdr->p_paddr -LOAD_PHYSICAL_ADDR ); -#else dest = (void *)(phdr->p_paddr); -#endif +if (IS_ENABLED (CONFIG_X86_64 ) || +IS_ENABLED (CONFIG_RELOCATABLE )) + dest += (unsigned long )output -LOAD_PHYSICAL_ADDR ;memmove (dest, output + phdr->p_offset, phdr->p_filesz);break ;default :/* Ignore other PT_* */ break ;
arch/x86/boot/header.S +2 -6
@@ -332,7 +332,7 @@ initrd_addr_max: .long 0x7fffffff kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment #required for protected mode #kernel -#ifdef CONFIG_RELOCATABLE +#if defined(CONFIG_RELOCATABLE) || defined(CONFIG_X86_64) relocatable_kernel: .byte 1 #else relocatable_kernel: .byte 0
@@ -342,14 +342,10 @@ min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment xloadflags: #ifdef CONFIG_X86_64 # define XLF0 XLF_KERNEL_64 /* 64-bit kernel */ -#else -# define XLF0 0 -#endif - -#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) /* kernel/boot_param/ramdisk could be loaded above 4g */ # define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G #else +# define XLF0 0 # define XLF1 0 #endif
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Ard Biesheuvel
maintainerQuoted 20 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.
Quoted 8 lines
Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)
Ping?
H. Peter Anvin
reviewerQuoted 198 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)arch/x86/Kconfig +0
-0@@ -1898,7 +1898,7 @@ config EFIconfig
EFI_STUB
bool "EFI stub support"
depends on EFIarch/x86/Kconfig +1
-1- select RELOCATABLE + select RELOCATABLE if X86_32help
This kernel feature allows a bzImage to be loaded directly by EFI firmware without the use of a bootloader.arch/x86/Kconfig +3
-2@@ -1995,8 +1995,9 @@ config PHYSICAL_START help This gives the physical address where the kernel is loaded. - If the kernel is not relocatable (CONFIG_RELOCATABLE=n) then bzImage - will decompress itself to above physical address and run from there. + If the kernel is not relocatable (CONFIG_RELOCATABLE=n) and built for + i386, then the bzImage will decompress itself to the above physical + address and run from there. Otherwise, bzImage will run from the address where it has been loaded by the boot loader. The only exception is if it is loaded below the above physical address, in which case it will relocate itself there.@@ -2031,16 +2032,22 @@ config PHYSICAL_START Don't change this unless you know what you are doing.config RELOCATABLE
arch/x86/Kconfig +2
-2- bool "Build a relocatable kernel" - default y + bool "Build a relocatable kernel" if X86_32 + default X86_32help
arch/x86/Kconfig +3
-2- This builds a kernel image that retains relocation information - so it can be loaded someplace besides the default 1MB. + This builds a kernel image that retains relocation information so it + can be placed someplace besides the default PAGE_OFFSET + 1MB. This + is a prerequisite for KASLR.The relocations tend to make the kernel binary about 10% larger, but are discarded at runtime.
arch/x86/Kconfig +7
-2- One use is for the kexec on panic case where the recovery kernel - must live at a different physical address than the primary + On i386, where the virtual and physical load offset of the kernel + must be equal, this also allows the kernel image to be placed at a + physical load address that differs from the compile time default. On + x86_64, this is always permitted. + + One use is for the kexec on panic case on i386, where the recovery + kernel must live at a different physical address than the primarykernel.
Note: If
CONFIG_RELOCATABLE=y, then the kernel runs from the addressarch/x86/Kconfig +0
-0@@ -2049,7 +2056,7 @@ config RELOCATABLEconfig
RANDOMIZE_BASE
bool "Randomize the address of the kernel image (KASLR)"arch/x86/Kconfig +1
-1- depends on RELOCATABLE + select RELOCATABLEdefault y
help
In support of Kernel Address Space Layout Randomization (KASLR),arch/x86/Kconfig +0
-0@@ -2085,7 +2092,7 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build supportconfig
X86_NEED_RELOCS
def_boolyarch/x86/Kconfig +1
-1- depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on RELOCATABLEselect
ARCH_VMLINUX_NEEDS_RELOCSconfig
PHYSICAL_ALIGNarch/x86/Kconfig +6
-5@@ -2098,12 +2105,13 @@ config PHYSICAL_ALIGN where kernel is loaded and run from. Kernel is compiled for an address which meets above alignment restriction. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is set, kernel will move itself to nearest - address aligned to above value and run from there. + If the bootloader loads the kernel at a non-aligned address and it + is built for x86_64 or CONFIG_RELOCATABLE is set, the kernel will + move itself to the nearest address aligned to above value and run + from there. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is not set, kernel will ignore the run time + If the bootloader loads the i386 kernel at a non-aligned address and + CONFIG_RELOCATABLE is not set, the kernel will ignore the run time load address and decompress itself to the address it has been compiled for and run from there. The address for which kernel is compiled already meets above alignment restrictions. Hence thearch/x86/boot/compressed/head_64.S +0
-2@@ -143,7 +143,6 @@ SYM_FUNC_START(startup_32) * for safe in-place decompression. */ -#ifdef CONFIG_RELOCATABLE movl %ebp, %ebx movl BP_kernel_alignment(%esi), %eax decl %eax@@ -152,7 +151,6 @@ SYM_FUNC_START(startup_32) andl %eax, %ebx cmpl $LOAD_PHYSICAL_ADDR, %ebx jae 1f -#endif movl $LOAD_PHYSICAL_ADDR, %ebx1:
arch/x86/boot/compressed/head_64.S +0
-2@@ -316,7 +314,6 @@ SYM_CODE_START(startup_64) */ /* Start with the delta to where the kernel will run at. */ -#ifdef CONFIG_RELOCATABLE leaq startup_32(%rip) /* - $startup_32 */, %rbp movl BP_kernel_alignment(%rsi), %eax decl %eax@@ -325,7 +322,6 @@ SYM_CODE_START(startup_64) andq %rax, %rbp cmpq $LOAD_PHYSICAL_ADDR, %rbp jae 1f -#endif movq $LOAD_PHYSICAL_ADDR, %rbp1:
arch/x86/boot/compressed/misc.c +3
-5@@ -314,12 +314,10 @@ static size_t parse_elf(void *output)if ((phdr->p_align %0x200000 ) !=0 )error ("Alignment of LOAD segment isn't multiple of 2MB" );#endif -#ifdef CONFIG_RELOCATABLE - dest = output; - dest += (phdr->p_paddr -LOAD_PHYSICAL_ADDR ); -#else dest = (void *)(phdr->p_paddr); -#endif +if (IS_ENABLED (CONFIG_X86_64 ) || +IS_ENABLED (CONFIG_RELOCATABLE )) + dest += (unsigned long )output -LOAD_PHYSICAL_ADDR ;memmove (dest, output + phdr->p_offset, phdr->p_filesz);break ;default :/* Ignore other PT_* */ break ;arch/x86/boot/header.S +0
-0@@ -332,7 +332,7 @@ initrd_addr_max: .long 0x7fffffff
kernel_alignment: .longCONFIG_PHYSICAL_ALIGN#physical addr alignment
#required for protected mode #kernelarch/x86/boot/header.S +1
-1-#ifdef CONFIG_RELOCATABLE +#if defined(CONFIG_RELOCATABLE) || defined(CONFIG_X86_64)
relocatable_kernel: .byte 1
#elserelocatable_kernel: .byte 0arch/x86/boot/header.S +0
-0@@ -342,14 +342,10 @@ min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignmentxloadflags:
#ifdefCONFIG_X86_64# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */arch/x86/boot/header.S +0
-5-#else -# define XLF0 0 -#endif - -#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) /* kernel/boot_param/ramdisk could be loaded above 4g */
Quoted 6 lines
# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G #else +# define XLF0 0 # define XLF1 0 #endif
Can you clarify what these nebulous future improvements are?
Ard Biesheuvel
maintainerQuoted 199 lines · H. Peter Anvin
Quoted 198 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)arch/x86/Kconfig +0
-0@@ -1898,7 +1898,7 @@ config EFIconfig
EFI_STUB
bool "EFI stub support"
depends on EFIarch/x86/Kconfig +1
-1- select RELOCATABLE + select RELOCATABLE if X86_32help
This kernel feature allows a bzImage to be loaded directly by EFI firmware without the use of a bootloader.arch/x86/Kconfig +3
-2@@ -1995,8 +1995,9 @@ config PHYSICAL_START help This gives the physical address where the kernel is loaded. - If the kernel is not relocatable (CONFIG_RELOCATABLE=n) then bzImage - will decompress itself to above physical address and run from there. + If the kernel is not relocatable (CONFIG_RELOCATABLE=n) and built for + i386, then the bzImage will decompress itself to the above physical + address and run from there. Otherwise, bzImage will run from the address where it has been loaded by the boot loader. The only exception is if it is loaded below the above physical address, in which case it will relocate itself there.@@ -2031,16 +2032,22 @@ config PHYSICAL_START Don't change this unless you know what you are doing.config RELOCATABLE
arch/x86/Kconfig +2
-2- bool "Build a relocatable kernel" - default y + bool "Build a relocatable kernel" if X86_32 + default X86_32help
arch/x86/Kconfig +3
-2- This builds a kernel image that retains relocation information - so it can be loaded someplace besides the default 1MB. + This builds a kernel image that retains relocation information so it + can be placed someplace besides the default PAGE_OFFSET + 1MB. This + is a prerequisite for KASLR.The relocations tend to make the kernel binary about 10% larger, but are discarded at runtime.
arch/x86/Kconfig +7
-2- One use is for the kexec on panic case where the recovery kernel - must live at a different physical address than the primary + On i386, where the virtual and physical load offset of the kernel + must be equal, this also allows the kernel image to be placed at a + physical load address that differs from the compile time default. On + x86_64, this is always permitted. + + One use is for the kexec on panic case on i386, where the recovery + kernel must live at a different physical address than the primarykernel.
Note: If
CONFIG_RELOCATABLE=y, then the kernel runs from the addressarch/x86/Kconfig +0
-0@@ -2049,7 +2056,7 @@ config RELOCATABLEconfig
RANDOMIZE_BASE
bool "Randomize the address of the kernel image (KASLR)"arch/x86/Kconfig +1
-1- depends on RELOCATABLE + select RELOCATABLEdefault y
help
In support of Kernel Address Space Layout Randomization (KASLR),arch/x86/Kconfig +0
-0@@ -2085,7 +2092,7 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build supportconfig
X86_NEED_RELOCS
def_boolyarch/x86/Kconfig +1
-1- depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on RELOCATABLEselect
ARCH_VMLINUX_NEEDS_RELOCSconfig
PHYSICAL_ALIGNarch/x86/Kconfig +6
-5@@ -2098,12 +2105,13 @@ config PHYSICAL_ALIGN where kernel is loaded and run from. Kernel is compiled for an address which meets above alignment restriction. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is set, kernel will move itself to nearest - address aligned to above value and run from there. + If the bootloader loads the kernel at a non-aligned address and it + is built for x86_64 or CONFIG_RELOCATABLE is set, the kernel will + move itself to the nearest address aligned to above value and run + from there. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is not set, kernel will ignore the run time + If the bootloader loads the i386 kernel at a non-aligned address and + CONFIG_RELOCATABLE is not set, the kernel will ignore the run time load address and decompress itself to the address it has been compiled for and run from there. The address for which kernel is compiled already meets above alignment restrictions. Hence thearch/x86/boot/compressed/head_64.S +0
-2@@ -143,7 +143,6 @@ SYM_FUNC_START(startup_32) * for safe in-place decompression. */ -#ifdef CONFIG_RELOCATABLE movl %ebp, %ebx movl BP_kernel_alignment(%esi), %eax decl %eax@@ -152,7 +151,6 @@ SYM_FUNC_START(startup_32) andl %eax, %ebx cmpl $LOAD_PHYSICAL_ADDR, %ebx jae 1f -#endif movl $LOAD_PHYSICAL_ADDR, %ebx1:
arch/x86/boot/compressed/head_64.S +0
-2@@ -316,7 +314,6 @@ SYM_CODE_START(startup_64) */ /* Start with the delta to where the kernel will run at. */ -#ifdef CONFIG_RELOCATABLE leaq startup_32(%rip) /* - $startup_32 */, %rbp movl BP_kernel_alignment(%rsi), %eax decl %eax@@ -325,7 +322,6 @@ SYM_CODE_START(startup_64) andq %rax, %rbp cmpq $LOAD_PHYSICAL_ADDR, %rbp jae 1f -#endif movq $LOAD_PHYSICAL_ADDR, %rbp1:
arch/x86/boot/compressed/misc.c +3
-5@@ -314,12 +314,10 @@ static size_t parse_elf(void *output)if ((phdr->p_align %0x200000 ) !=0 )error ("Alignment of LOAD segment isn't multiple of 2MB" );#endif -#ifdef CONFIG_RELOCATABLE - dest = output; - dest += (phdr->p_paddr -LOAD_PHYSICAL_ADDR ); -#else dest = (void *)(phdr->p_paddr); -#endif +if (IS_ENABLED (CONFIG_X86_64 ) || +IS_ENABLED (CONFIG_RELOCATABLE )) + dest += (unsigned long )output -LOAD_PHYSICAL_ADDR ;memmove (dest, output + phdr->p_offset, phdr->p_filesz);break ;default :/* Ignore other PT_* */ break ;arch/x86/boot/header.S +0
-0@@ -332,7 +332,7 @@ initrd_addr_max: .long 0x7fffffff
kernel_alignment: .longCONFIG_PHYSICAL_ALIGN#physical addr alignment
#required for protected mode #kernelarch/x86/boot/header.S +1
-1-#ifdef CONFIG_RELOCATABLE +#if defined(CONFIG_RELOCATABLE) || defined(CONFIG_X86_64)
relocatable_kernel: .byte 1
#elserelocatable_kernel: .byte 0arch/x86/boot/header.S +0
-0@@ -342,14 +342,10 @@ min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignmentxloadflags:
#ifdefCONFIG_X86_64# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */arch/x86/boot/header.S +0
-5-#else -# define XLF0 0 -#endif - -#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) /* kernel/boot_param/ramdisk could be loaded above 4g */
Quoted 8 lines
Quoted 6 lines
# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G #else +# define XLF0 0 # define XLF1 0 #endifCan you clarify what these nebulous future improvements are?
The main improvement today is that CONFIG_RELOCATABLE will be only implied by CONFIG_RANDOMIZE_BASE, and so turning that off gets rid of the unnecessary relocation metadata.
The future improvement is still fgKASLR, which I am implementing in the ELF domain, making it arch-agnostic, and applicable to other static ELF executables such as hypervisors and statically linked user space executables. This will hopefully make the needed tooling changes more palatable to the wider community.
On x86, that will require the use of PIC code for kaslr capable kernels, [which surely you remember having discussed with me in the past], but only when opting in to kaslr/fgkaslr. Otherwise, the usual position dependent codegen will be preserved.
Note that I have extensive benchmarking data now that shows that the PIC change itself has negligible impact for performance, but having the option to turn that off if you don't need it is still appreciated, I assume. But let's leave that discussion for when I resend that particular series.
Signature · 1 line
Ard.
H. Peter Anvin
reviewerQuoted 226 lines · Ard Biesheuvel
Quoted 199 lines · H. Peter Anvin
Quoted 198 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)arch/x86/Kconfig +0
-0@@ -1898,7 +1898,7 @@ config EFIconfig
EFI_STUB
bool "EFI stub support"
depends on EFIarch/x86/Kconfig +1
-1- select RELOCATABLE + select RELOCATABLE if X86_32help
This kernel feature allows a bzImage to be loaded directly by EFI firmware without the use of a bootloader.arch/x86/Kconfig +3
-2@@ -1995,8 +1995,9 @@ config PHYSICAL_START help This gives the physical address where the kernel is loaded. - If the kernel is not relocatable (CONFIG_RELOCATABLE=n) then bzImage - will decompress itself to above physical address and run from there. + If the kernel is not relocatable (CONFIG_RELOCATABLE=n) and built for + i386, then the bzImage will decompress itself to the above physical + address and run from there. Otherwise, bzImage will run from the address where it has been loaded by the boot loader. The only exception is if it is loaded below the above physical address, in which case it will relocate itself there.@@ -2031,16 +2032,22 @@ config PHYSICAL_START Don't change this unless you know what you are doing.config RELOCATABLE
arch/x86/Kconfig +2
-2- bool "Build a relocatable kernel" - default y + bool "Build a relocatable kernel" if X86_32 + default X86_32help
arch/x86/Kconfig +3
-2- This builds a kernel image that retains relocation information - so it can be loaded someplace besides the default 1MB. + This builds a kernel image that retains relocation information so it + can be placed someplace besides the default PAGE_OFFSET + 1MB. This + is a prerequisite for KASLR.The relocations tend to make the kernel binary about 10% larger, but are discarded at runtime.
arch/x86/Kconfig +7
-2- One use is for the kexec on panic case where the recovery kernel - must live at a different physical address than the primary + On i386, where the virtual and physical load offset of the kernel + must be equal, this also allows the kernel image to be placed at a + physical load address that differs from the compile time default. On + x86_64, this is always permitted. + + One use is for the kexec on panic case on i386, where the recovery + kernel must live at a different physical address than the primarykernel.
Note: If
CONFIG_RELOCATABLE=y, then the kernel runs from the addressarch/x86/Kconfig +0
-0@@ -2049,7 +2056,7 @@ config RELOCATABLEconfig
RANDOMIZE_BASE
bool "Randomize the address of the kernel image (KASLR)"arch/x86/Kconfig +1
-1- depends on RELOCATABLE + select RELOCATABLEdefault y
help
In support of Kernel Address Space Layout Randomization (KASLR),arch/x86/Kconfig +0
-0@@ -2085,7 +2092,7 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build supportconfig
X86_NEED_RELOCS
def_boolyarch/x86/Kconfig +1
-1- depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on RELOCATABLEselect
ARCH_VMLINUX_NEEDS_RELOCSconfig
PHYSICAL_ALIGNarch/x86/Kconfig +6
-5@@ -2098,12 +2105,13 @@ config PHYSICAL_ALIGN where kernel is loaded and run from. Kernel is compiled for an address which meets above alignment restriction. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is set, kernel will move itself to nearest - address aligned to above value and run from there. + If the bootloader loads the kernel at a non-aligned address and it + is built for x86_64 or CONFIG_RELOCATABLE is set, the kernel will + move itself to the nearest address aligned to above value and run + from there. - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is not set, kernel will ignore the run time + If the bootloader loads the i386 kernel at a non-aligned address and + CONFIG_RELOCATABLE is not set, the kernel will ignore the run time load address and decompress itself to the address it has been compiled for and run from there. The address for which kernel is compiled already meets above alignment restrictions. Hence thearch/x86/boot/compressed/head_64.S +0
-2@@ -143,7 +143,6 @@ SYM_FUNC_START(startup_32) * for safe in-place decompression. */ -#ifdef CONFIG_RELOCATABLE movl %ebp, %ebx movl BP_kernel_alignment(%esi), %eax decl %eax@@ -152,7 +151,6 @@ SYM_FUNC_START(startup_32) andl %eax, %ebx cmpl $LOAD_PHYSICAL_ADDR, %ebx jae 1f -#endif movl $LOAD_PHYSICAL_ADDR, %ebx1:
arch/x86/boot/compressed/head_64.S +0
-2@@ -316,7 +314,6 @@ SYM_CODE_START(startup_64) */ /* Start with the delta to where the kernel will run at. */ -#ifdef CONFIG_RELOCATABLE leaq startup_32(%rip) /* - $startup_32 */, %rbp movl BP_kernel_alignment(%rsi), %eax decl %eax@@ -325,7 +322,6 @@ SYM_CODE_START(startup_64) andq %rax, %rbp cmpq $LOAD_PHYSICAL_ADDR, %rbp jae 1f -#endif movq $LOAD_PHYSICAL_ADDR, %rbp1:
arch/x86/boot/compressed/misc.c +3
-5@@ -314,12 +314,10 @@ static size_t parse_elf(void *output)if ((phdr->p_align %0x200000 ) !=0 )error ("Alignment of LOAD segment isn't multiple of 2MB" );#endif -#ifdef CONFIG_RELOCATABLE - dest = output; - dest += (phdr->p_paddr -LOAD_PHYSICAL_ADDR ); -#else dest = (void *)(phdr->p_paddr); -#endif +if (IS_ENABLED (CONFIG_X86_64 ) || +IS_ENABLED (CONFIG_RELOCATABLE )) + dest += (unsigned long )output -LOAD_PHYSICAL_ADDR ;memmove (dest, output + phdr->p_offset, phdr->p_filesz);break ;default :/* Ignore other PT_* */ break ;arch/x86/boot/header.S +0
-0@@ -332,7 +332,7 @@ initrd_addr_max: .long 0x7fffffff
kernel_alignment: .longCONFIG_PHYSICAL_ALIGN#physical addr alignment
#required for protected mode #kernelarch/x86/boot/header.S +1
-1-#ifdef CONFIG_RELOCATABLE +#if defined(CONFIG_RELOCATABLE) || defined(CONFIG_X86_64)
relocatable_kernel: .byte 1
#elserelocatable_kernel: .byte 0arch/x86/boot/header.S +0
-0@@ -342,14 +342,10 @@ min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignmentxloadflags:
#ifdefCONFIG_X86_64# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */arch/x86/boot/header.S +0
-5-#else -# define XLF0 0 -#endif - -#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) /* kernel/boot_param/ramdisk could be loaded above 4g */Quoted 8 lines
Quoted 6 lines
# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G #else +# define XLF0 0 # define XLF1 0 #endifCan you clarify what these nebulous future improvements are?
The main improvement today is that
CONFIG_RELOCATABLEwill be only implied byCONFIG_RANDOMIZE_BASE, and so turning that off gets rid of the unnecessary relocation metadata.The future improvement is still fgKASLR, which I am implementing in the ELF domain, making it arch-agnostic, and applicable to other static ELF executables such as hypervisors and statically linked user space executables. This will hopefully make the needed tooling changes more palatable to the wider community.
On x86, that will require the use of PIC code for kaslr capable kernels, [which surely you remember having discussed with me in the past], but only when opting in to kaslr/fgkaslr. Otherwise, the usual position dependent codegen will be preserved.
Quoted 6 lines
Note that I have extensive benchmarking data now that shows that the PIC change itself has negligible impact for performance, but having the option to turn that off if you don't need it is still appreciated, I assume. But let's leave that discussion for when I resend that particular series.
So since you're not going to get people to stop using kASLR, you're now going a different path to forcing everyone to build with PIC?
I'm going to vote NAK on this one... I was already thinking that it would mess up low-level debugging too much, but when *handwave handwave future improvement* turns into "the thing that has been NAKed several times already", then...
Ard Biesheuvel
maintainerQuoted 33 lines · H. Peter Anvin
Quoted 32 lines · Ard Biesheuvel
Quoted 29 lines · H. Peter Anvin
Quoted 28 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)
...
Quoted 2 lines
I'm going to vote NAK on this one... I was already thinking that it would mess up low-level debugging too much,
How so? The last time I proposed this, you replied that you were planning to do this yourself.
H. Peter Anvin
reviewerQuoted 36 lines · Ard Biesheuvel
Quoted 25 lines · H. Peter Anvin
Quoted 24 lines · Ard Biesheuvel
Quoted 21 lines · H. Peter Anvin
Quoted 20 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)
Quoted 9 lines
...
Quoted 2 lines
I'm going to vote NAK on this one... I was already thinking that it would mess up low-level debugging too much,
How so? The last time I proposed this, you replied that you were planning to do this yourself.
If we are going to get rid of it, we should get rid of it properly as in removing the conditional machinery, not use it as a backdoor for doing something explicitly NAKed (forcing PIC compilation.)
What you are proposing is literally the exact opposite.
Ard Biesheuvel
maintainerQuoted 41 lines · H. Peter Anvin
Quoted 40 lines · Ard Biesheuvel
Quoted 33 lines · H. Peter Anvin
Quoted 32 lines · Ard Biesheuvel
Quoted 29 lines · H. Peter Anvin
Quoted 28 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
On
x86_64, the physical placement of the kernel is independent from its mapping in the 'High Kernel Mapping' range. This means that even a position dependent kernel built without boot-time relocation support can run from any suitably aligned physical address, and there is no need to make this behavior dependent on whether or not the kernel is virtually relocatable.On i386, the situation is different, given that the physical and virtual load offsets must be equal, and so only a relocatable kernel can be loaded at a physical address that deviates from its build-time default.
Clarify this in
Kconfigand in the code, and advertise the 64-bit bzImage as loadable at any physical offset regardless of whetherCONFIG_RELOCATABLEis set. In practice, this makes little difference, given that it defaults to 'y' and is a prerequisite forEFI_STUBandRANDOMIZE_BASE, but it will help with some future refactoring of the relocation code.Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/Kconfig | 40 ++++++++++++++++++------------ arch/x86/boot/compressed/head_64.S | 4 --- arch/x86/boot/compressed/misc.c | 8 +++--- arch/x86/boot/header.S | 8 ++---- 4 files changed, 29 insertions(+), 31 deletions(-)...
Quoted 2 lines
I'm going to vote NAK on this one... I was already thinking that it would mess up low-level debugging too much,
Quoted 8 lines
Quoted 5 lines
How so? The last time I proposed this, you replied that you were planning to do this yourself.
If we are going to get rid of it, we should get rid of it properly as in removing the conditional machinery,
Which conditional machinery is that? All the code that reasons about the physical load address needs to stay, the only thing I am changing here is that you can no longer disable it on x86_64, as there is no upside to forcing the 64-bit kernel to run from a fixed physical address. This means the EFI stub no longer needs to depend on CONFIG_RELOCATABLE (which also controls the generation for relocation data for virtual displacement) for x86_64.
The i386 situation is different, but I left that alone on purpose. Would you prefer it if that always became relocatable as well?
Quoted 3 lines
not use it as a backdoor for
doing something explicitly NAKed (forcing PIC compilation.)
I know PIC compilation is a sensitive subject for some people, but would you mind not framing this as some backhanded action on my part?
Once I get to resending that series, you will have ample opportunity to voice your concerns and suggest workloads that will prove that it is infeasible. Or even provide one now so that I won't have to bother resending it in the first place.
Quoted 1 line
What you are proposing is literally the exact opposite.
I don't think so, but I'm still not sure what you meant by "it would mess up low-level debugging too much", so maybe you could clarify that?
H. Peter Anvin
reviewerQuoted 21 lines · Ard Biesheuvel
Quoted 4 lines
Quoted 4 lines
...
Quoted 2 lines
I'm going to vote NAK on this one... I was already thinking that it would mess up low-level debugging too much,
Quoted 8 lines
Quoted 5 lines
How so? The last time I proposed this, you replied that you were planning to do this yourself.
If we are going to get rid of it, we should get rid of it properly as in removing the conditional machinery,
Which conditional machinery is that? All the code that reasons about the physical load address needs to stay, the only thing I am changing here is that you can no longer disable it on
x86_64, as there is no upside to forcing the 64-bit kernel to run from a fixed physical address. This means the EFI stub no longer needs to depend onCONFIG_RELOCATABLE(which also controls the generation for relocation data for virtual displacement) forx86_64.
Quoted 3 lines
The i386 situation is different, but I left that alone on purpose. Would you prefer it if that always became relocatable as well?
Yes, if we're going to do it, it would make more sense to do exactly that.
Quoted 11 lines
Quoted 3 lines
not use it as a backdoor for
doing something explicitly NAKed (forcing PIC compilation.)I know PIC compilation is a sensitive subject for some people, but would you mind not framing this as some backhanded action on my part?
Once I get to resending that series, you will have ample opportunity to voice your concerns and suggest workloads that will prove that it is infeasible. Or even provide one now so that I won't have to bother resending it in the first place.
I do (genuinely) apologize; that was indeed *quite* uncalled for.
What I *do* think is a real problem -- and you are far from the only or worst offender -- is to put a vague "future improvements" as justification for a change. This is literally asking people to take on faith that the "future improvements" is actually an engineering direction we want to go in.
The following points are in no way specific to your proposals, so please don't take it that way; these are more general statements about "why x86 is special".
I do not believe in is pessimizing one of the most widely used architectures in the world in the name of "uniformity" or "compatibility", and there have been many, genuinely well-meaning, attempts to do so. If x86 were a niche architecture that would be a *very* different thing, but in the world we are living in right now, there is x86 and ARM; everything else is a rounding error (long tail). As such, for either of those architectures to carry more arch-specific stuff than other architectures is entirely fair.
Now, x86 and ARM are two very different beasts, which is entirely natural because they have evolved into their current market dominance through completely different routes. As such, ARM has a lot more *platform* divergences than x86 does, and that means it has has a lot of overlap with most other "platform rich" architectures. Here, ARM sits squarely with the long tail, and so it makes sense to strive for ARM to drive, cooperatively, where the other architectures go.
Here, the odd ones out are x86 and s390: they are architectures which have a dominant -- to the point of being universal -- *systems* architecture as well as a CPU architecture. This means that especially the early flows are heavily influenced by that systems architecture in ways other architectures aren't. This tight binding of systems architecture are a major aspect of those architectures which is very easy to forget.
Quoted 5 lines
Quoted 1 line
What you are proposing is literally the exact opposite.
I don't think so, but I'm still not sure what you meant by "it would mess up low-level debugging too much", so maybe you could clarify that?
Certainly.
When I debug the early (1:1-mapped) flows I often find it useful to pin down the kernel to a specific address so that I don't have to convert between ELF addresses and physical addresses (for this purpose, I don't care about significant hex digits larger than the size of the kernel, which is *one* reason why kernel alignment moved to 16 MiB -- another reason was the somewhat special nature of the lower 16 MiB of the address space on some x86 systems.) Once we are running under the kernel mappings, it is just a matter of of turning off kASLR.
Now, you also quite correctly pointed out that I already proposed turning on kernel relocation by default. In practice it doesn't become a problem, as the kernel generally ends up loaded at 16 MiB in a legacy environment, and in an EFI environment you have to deal with the memory space EFI assigns you no matter what you do.
PIC compilation is another matter entirely (as you also very correctly point out!), as x86 unlike most RISC architectures has enough direct addressing reach to encompass the entire kernel (in the "medium" model for user space and the "kernel" model for kernel space.) This means that x86 benefits from GOT/PLT elision in ways that many other architectures don't. On the flipside, because x86 is a CISC architecture once you give atup performance in the compiler stage it is very hard to recover it even in the linker stage. Furthermore, on i386 PIC is *extremely* painful since it involves giving up one of only 7 usable GPRs (6 if you are compiling with frame pointers!) and a relatively expensive sequence(*) to get EIP manifest.
As a result, I very much will continue to object to PIC compilation on x86 as opposed to boot time relocation. Whereas code patching in user space is expensive due to breaking sharing and thus demand paging of executables and libraries, none of that applies to the kernel -- and we do runtime code patching of the kernel anyway. The relocation is just a special case of that.
Now I do know you don't like the compressed kernel relocation format (which you are not trying to change with this patch) and it is *most certainly* insufficiently expressive for fgKASLR. For fgKASLR you need to know the relocation base rather than a simple adjustment factor, even on an architecture like x86 that doesn't have to deal with "upper part"-type relocations (for which the compressed format approach, carrying embedded addends, completely collapses.) The metadata required by fgKASLR is positively vast in comparison to the ordinary kernel boot, and legitimately so.
However, fgKALSR is time-consuming. On bare metal this is rarely a concern, but users want to be able to spin up a VM kernel in ~100 ms. In that case, the compressed relocation format is highly efficient, both since it is compact and since the processing of those relocations is extremely simple. Furthermore, since the kernel is also ready to go if loaded at its advertised preferred address (which is the reason why this address is explicitly advertised, even for a relocatable kernel: "if you can load me at this specific address, I can boot faster and your vmlinux symbols will match the runtime, too."), the performance-sensitive VM spinup the (virtual) environment can be set up so that the relocation can be skipped entirely.
-hpa
(*) That being said, the use of an out-of-line trampoline to do so is a thing that was only relevant on a small handful of cores that unfortunately fell right at the end of the true 32-bit era; on both older and newer cores CALL 0 is special cased to be a "PUSH xIP" instruction and does not mess up the call stack. It still costs a register, and if using PLT, requires that register to be %ebx, which further adds to the complexity; if %ebx was *permanently* assigned to this role like the gp register on (some) RISC architectures it might actually be less painful, but losing one register out of 7 is going to hurt like hell no matter how you slice it.
-hpa
Ard Biesheuvel
maintainerQuoted 22 lines · H. Peter Anvin
Quoted 21 lines · Ard Biesheuvel
Quoted 4 lines
Quoted 4 lines
...
Quoted 2 lines
I'm going to vote NAK on this one... I was already thinking that it would mess up low-level debugging too much,
Quoted 8 lines
Quoted 5 lines
How so? The last time I proposed this, you replied that you were planning to do this yourself.
If we are going to get rid of it, we should get rid of it properly as in removing the conditional machinery,
Which conditional machinery is that? All the code that reasons about the physical load address needs to stay, the only thing I am changing here is that you can no longer disable it on
x86_64, as there is no upside to forcing the 64-bit kernel to run from a fixed physical address. This means the EFI stub no longer needs to depend onCONFIG_RELOCATABLE(which also controls the generation for relocation data for virtual displacement) forx86_64.
Quoted 6 lines
Quoted 3 lines
The i386 situation is different, but I left that alone on purpose. Would you prefer it if that always became relocatable as well?
Yes, if we're going to do it, it would make more sense to do exactly that.
OK I'll go that way then.
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
not use it as a backdoor for
doing something explicitly NAKed (forcing PIC compilation.)I know PIC compilation is a sensitive subject for some people, but would you mind not framing this as some backhanded action on my part?
Quoted 7 lines
Quoted 4 lines
Once I get to resending that series, you will have ample opportunity to voice your concerns and suggest workloads that will prove that it is infeasible. Or even provide one now so that I won't have to bother resending it in the first place.
I do (genuinely) apologize; that was indeed *quite* uncalled for.
Thanks, much appreciated.
Quoted 6 lines
What I *do* think is a real problem -- and you are far from the only or worst offender -- is to put a vague "future improvements" as justification for a change. This is literally asking people to take on faith that the "future improvements" is actually an engineering direction we want to go in.
Point taken.
Since we are discussing the future respin of my PIC proposal here, let me address those points as well.
Quoted 12 lines
PIC compilation is another matter entirely (as you also very correctly point out!), as x86 unlike most RISC architectures has enough direct addressing reach to encompass the entire kernel (in the "medium" model for user space and the "kernel" model for kernel space.) This means that x86 benefits from GOT/PLT elision in ways that many other architectures don't. On the flipside, because x86 is a CISC architecture once you give atup performance in the compiler stage it is very hard to recover it even in the linker stage. Furthermore, on i386 PIC is *extremely* painful since it involves giving up one of only 7 usable GPRs (6 if you are compiling with frame pointers!) and a relatively expensive
sequence(*)to get EIP manifest.
Quoted 7 lines
As a result, I very much will continue to object to PIC compilation on x86 as opposed to boot time relocation. Whereas code patching in user space is expensive due to breaking sharing and thus demand paging of executables and libraries, none of that applies to the kernel -- and we do runtime code patching of the kernel anyway. The relocation is just a special case of that.
Please wait for me to make my proposal before shooting it down. You are making assumptions about the impact these changes will have without ever having looked at them.
I am not proposing any changes to i386.
I am not proposing the use of GOTs, PLTs, etc.
What I will be proposing is the use of RIP-relative references instead of sign-extended 32-bit references to global symbols. The reason for that is that this will allow use to run the linker in -pie mode, which then produces all metadata that traditional kaslr needs at boot time.
Quoted 7 lines
Now I do know you don't like the compressed kernel relocation format (which you are not trying to change with this patch) and it is *most certainly* insufficiently expressive for fgKASLR. For fgKASLR you need to know the relocation base rather than a simple adjustment factor, even on an architecture like x86 that doesn't have to deal with "upper part"-type relocations (for which the compressed format approach, carrying embedded addends, completely collapses.)
PIE linking allows the use of RELR packing, which reduces the size of these compressed kernel relocations by about 95%. This is the same format used by PIE in user space, as well as other architectures in Linux.
The size increase of .text is between 0.3% and 0.5%, and none of the benchmarks I've tried showed any significant performance impact on the micro-architectures I have access to. With this patch applied, you get those 0.5% back if you disable CONFIG_RANDOMIZE_BASE. This is the future work I was referring to.
I will be more than happy to try any benchmark you can throw at this.
Quoted 4 lines
The metadata required
by fgKASLR is positively vast in comparison to the ordinary kernel boot, and legitimately so.
Indeed.
The implementation I will be proposing is essentially layered on top of the ELF representation, using a seed to permute the contents of .text in a stable manner.
This means that a debugger that knows the seed can permute the image in the same way before loading it, and so all post-mortem and source level debug use cases are still supported. (Whether the seed is preserved on the system itself is a separate matter - on Android we will probably discard it after booting)
So the permutation performed by the loader and by the debugger is implemented by the same shared code, and the resulting ELF image remains a valid image as before. This allows permuted images to be packaged and shipped as well, without changes to the runtime loader (this might be useful for Android in some cases). It also means that traditional KASLR is still possible too - fgkaslr just fixes up the RELR/RELA metadata after doing the permutation, and the existing code works exactly the same as before.
The upshot of this is that relocations tacked onto the binary after linking will go out of sync, and fgkaslr boot would lose the ability to do traditional KASLR as a result, which is undesirable. This is why the existing x86 relocation format is incompatible.
Quoted 5 lines
However, fgKALSR is time-consuming. On bare metal this is rarely a concern, but users want to be able to spin up a VM kernel in ~100 ms. In that case, the compressed relocation format is highly efficient, both since it is compact and since the processing of those relocations is extremely simple.
I am not proposing to replace those relocations with fgkaslr, but with standard packed RELR relocations, which take up only 5% of the space used by the bespoke x86 relocation format, and can be applied by the following for loop:
for (const u64 *relr = relr_start; relr < relr_end; relr++) {
if ((*relr & 1) == 0) {
place = (u64 *)(*relr + offset);
*place++ += offset;
} else {
for (u64 *p = place, r = *relr >> 1; r; p++, r >>= 1)
if (r & 1)
*p += offset;
place += 63;
}
}Quoted 8 lines
Furthermore, since the kernel is also ready to go if
loaded at its advertised preferred address (which is the reason why this address is explicitly advertised, even for a relocatable kernel: "if you can load me at this specific address, I can boot faster and your vmlinux symbols will match the runtime, too."), the performance-sensitive VM spinup the (virtual) environment can be set up so that the relocation can be skipped entirely.
Both RELR packing and the fgkaslr I will be proposing have the same
properties: the metadata can just be ignored, and the kernel will boot in its default permutation from its default virtual load address.
So perhaps we can continue this discussion based on the actual code once I post it, rather than on your assumptions on how these things will be implemented? I promise you I will take your feedback very seriously.