x86/KASLR: Randomize virtual address separately
v9 cover x86/KASLR: Randomize virtual address separately
This is v9 of the remaining patches needed to support separate phys/virt KASLR of the text base address. The rest of the series has landed in -tip.
The patches are:
- 1: Best-effort data relocation detection for the build.
- 2: Further clean up on pagetable.c.
- 3: Last part of Baoquan's decoupling the physical address and virtual address randomization of kernel text.
- 4: Remove upper bound on physical address range.
- 5: Remove lower bound on physical address range.
Thanks!
-Kees
v9:
- added data relocation detection
v8:
- extracted identity map initialization function to be part of the called interface, renamed appropriately to initialize_identity_maps().
- added copyright to pagetable.c for clarity.
- shuffled initialization of mapping_info around again for good measure.
- refactored remaining patches to include call to initialize_identity_maps().
PATCH v9 1/5 x86/boot: Refuse to build with data relocations
The compressed kernel is built with -fPIC/-fPIE so that it can run in any location a bootloader happens to put it. However, since ELF relocation processing is not happening (and all the relocation information has already been stripped at link time), none of the code can use data relocations (e.g. static assignments of pointers). This is already noted in a warning comment at the top of misc.c, but this adds an explicit check for the condition during the linking stage to block any such bugs from appearing.
If this was in place with the earlier bug in pagetable.c, the build would fail like this:
...
CC arch/x86/boot/compressed/pagetable.o
DATAREL arch/x86/boot/compressed/vmlinux
error: arch/x86/boot/compressed/pagetable.o has data relocations! make[2]: *** [arch/x86/boot/compressed/vmlinux] Error 1
...
A clean build shows:
...
CC arch/x86/boot/compressed/pagetable.o
DATAREL arch/x86/boot/compressed/vmlinux
LD arch/x86/boot/compressed/vmlinux
...
--- arch/x86/boot/compressed/Makefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/boot/compressed/Makefile +18 -0
@@ -85,7 +85,25 @@ vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \ $(objtree)/drivers/firmware/efi/libstub/lib.a vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o +# The compressed kernel is built with -fPIC/-fPIE so that a boot loader +# can place it anywhere in memory and it will still run. However, since +# it is executed as-is without any ELF relocation processing performed +# (and has already had all relocation sections stripped from the binary), +# none of the code can use data relocations (e.g. static assignments of +# pointer values), since they will be meaningless at runtime. This check +# will refuse to link the vmlinux if any of these relocations are found. +quiet_cmd_check_data_rel = DATAREL $@ +define cmd_check_data_rel + for obj in $(filter %.o,$^); do \ + readelf -S $$obj | grep -qF .rel.local && { \ + echo "error: $$obj has data relocations!" >&2; \ + exit 1; \ + } || true; \ + done +endef + $(obj)/vmlinux: $(vmlinux-objs-y) FORCE + $(call if_changed,check_data_rel) $(call if_changed,ld) @:
Signature · 1 line
2.6.3
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/boot: Refuse to build with data relocations
x86/boot: Refuse to build with data relocations
Commit-ID: f26973633bfe01b67dbfc351aff8aec355811583 Gitweb: http://git.kernel.org/tip/f26973633bfe01b67dbfc351aff8aec355811583 Author: Kees Cook <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:30 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 17 Jun 2016 11:03:45 +0200
x86/boot: Refuse to build with data relocations
The compressed kernel is built with -fPIC/-fPIE so that it can run in any location a bootloader happens to put it. However, since ELF relocation processing is not happening (and all the relocation information has already been stripped at link time), none of the code can use data relocations (e.g. static assignments of pointers). This is already noted in a warning comment at the top of misc.c, but this adds an explicit check for the condition during the linking stage to block any such bugs from appearing.
If this was in place with the earlier bug in pagetable.c, the build would fail like this:
...
CC arch/x86/boot/compressed/pagetable.o
DATAREL arch/x86/boot/compressed/vmlinux
error: arch/x86/boot/compressed/pagetable.o has data relocations! make[2]: *** [arch/x86/boot/compressed/vmlinux] Error 1
...
A clean build shows:
...
CC arch/x86/boot/compressed/pagetable.o
DATAREL arch/x86/boot/compressed/vmlinux
LD arch/x86/boot/compressed/vmlinux
...
--- arch/x86/boot/compressed/Makefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/boot/compressed/Makefile +18 -0
@@ -85,7 +85,25 @@ vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \ $(objtree)/drivers/firmware/efi/libstub/lib.a vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o +# The compressed kernel is built with -fPIC/-fPIE so that a boot loader +# can place it anywhere in memory and it will still run. However, since +# it is executed as-is without any ELF relocation processing performed +# (and has already had all relocation sections stripped from the binary), +# none of the code can use data relocations (e.g. static assignments of +# pointer values), since they will be meaningless at runtime. This check +# will refuse to link the vmlinux if any of these relocations are found. +quiet_cmd_check_data_rel = DATAREL $@ +define cmd_check_data_rel + for obj in $(filter %.o,$^); do \ + readelf -S $$obj | grep -qF .rel.local && { \ + echo "error: $$obj has data relocations!" >&2; \ + exit 1; \ + } || true; \ + done +endef + $(obj)/vmlinux: $(vmlinux-objs-y) FORCE + $(call if_changed,check_data_rel) $(call if_changed,ld) OBJCOPYFLAGS_vmlinux.bin := -R .comment -S
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/boot: Refuse to build with data relocations
In reply to Kees Cookx86/boot: Refuse to build with data relocations
Commit-ID: 98f78525371b55ccd1c480207ce10296c72fa340 Gitweb: http://git.kernel.org/tip/98f78525371b55ccd1c480207ce10296c72fa340 Author: Kees Cook <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:30 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sun, 26 Jun 2016 12:32:03 +0200
x86/boot: Refuse to build with data relocations
The compressed kernel is built with -fPIC/-fPIE so that it can run in any location a bootloader happens to put it. However, since ELF relocation processing is not happening (and all the relocation information has already been stripped at link time), none of the code can use data relocations (e.g. static assignments of pointers). This is already noted in a warning comment at the top of misc.c, but this adds an explicit check for the condition during the linking stage to block any such bugs from appearing.
If this was in place with the earlier bug in pagetable.c, the build would fail like this:
...
CC arch/x86/boot/compressed/pagetable.o
DATAREL arch/x86/boot/compressed/vmlinux
error: arch/x86/boot/compressed/pagetable.o has data relocations! make[2]: *** [arch/x86/boot/compressed/vmlinux] Error 1
...
A clean build shows:
...
CC arch/x86/boot/compressed/pagetable.o
DATAREL arch/x86/boot/compressed/vmlinux
LD arch/x86/boot/compressed/vmlinux
...
--- arch/x86/boot/compressed/Makefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/boot/compressed/Makefile +18 -0
@@ -85,7 +85,25 @@ vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \ $(objtree)/drivers/firmware/efi/libstub/lib.a vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o +# The compressed kernel is built with -fPIC/-fPIE so that a boot loader +# can place it anywhere in memory and it will still run. However, since +# it is executed as-is without any ELF relocation processing performed +# (and has already had all relocation sections stripped from the binary), +# none of the code can use data relocations (e.g. static assignments of +# pointer values), since they will be meaningless at runtime. This check +# will refuse to link the vmlinux if any of these relocations are found. +quiet_cmd_check_data_rel = DATAREL $@ +define cmd_check_data_rel + for obj in $(filter %.o,$^); do \ + readelf -S $$obj | grep -qF .rel.local && { \ + echo "error: $$obj has data relocations!" >&2; \ + exit 1; \ + } || true; \ + done +endef + $(obj)/vmlinux: $(vmlinux-objs-y) FORCE + $(call if_changed,check_data_rel) $(call if_changed,ld) OBJCOPYFLAGS_vmlinux.bin := -R .comment -S
PATCH v9 2/5 x86/KASLR: Clarify identity map interface
In reply to Kees CookThis extracts the call to prepare_level4() into a top-level function that the user of the pagetable.c interface must call to initialize the new page tables. For clarity and to match the "finalize" function, it has been renamed to initialize_identity_maps(). This function also gains the initialization of mapping_info so we don't have to do it each time in add_identity_map().
Additionally add copyright notice to the top, to make it clear that the bulk of the pagetable.c code was written by Yinghai, and that I just added bugs later. :)
--- arch/x86/boot/compressed/kaslr.c | 3 +++ arch/x86/boot/compressed/misc.h | 3 +++ arch/x86/boot/compressed/pagetable.c | 26 ++++++++++++++++---------- 3 files changed, 22 insertions(+), 10 deletions(-)
arch/x86/boot/compressed/kaslr.c +3 -0
@@ -485,6 +485,9 @@ unsigned char *choose_random_location(unsigned long input, boot_params->hdr.loadflags |=KASLR_FLAG ; +/* Prepare to add new identity pagetables on demand. */ +initialize_identity_maps (); +/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, output);
arch/x86/boot/compressed/misc.h +3 -0
@@ -85,10 +85,13 @@ unsigned char *choose_random_location(unsigned long input_ptr,#endif #ifdef CONFIG_X86_64 +void initialize_identity_maps (void );void add_identity_map (unsigned long start,unsigned long size);void finalize_identity_maps (void );extern unsigned char _pgtable[];#else +static inline void initialize_identity_maps (void ) +{ }static inline void add_identity_map (unsigned long start,unsigned long size) { }static inline void finalize_identity_maps (void )
arch/x86/boot/compressed/pagetable.c +16 -10
@@ -2,6 +2,9 @@* This code is used on x86_64 to create page table identity mappings on * demand by building up a new set of page tables (or appending to the * existing ones), and then switching over to them when ready. +* +* Copyright (C) 2015-2016 Yinghai Lu +* Copyright (C) 2016 Kees Cook */ /*
@@ -59,9 +62,21 @@ static struct alloc_pgt_data pgt_data;/* The top level page table entry pointer. */ static unsigned long level4p; +/* +* Mapping information structure passed to kernel_ident_mapping_init(). +* Due to relocation, pointers must be assigned at run time not build time. +*/ +static struct x86_mapping_info mapping_info = { + .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, +}; +/* Locates and clears a region for a new top level page table. */ -static void prepare_level4 (void ) +void initialize_identity_maps (void ) { +/* Init mapping_info with run-time function/buffer pointers. */ + mapping_info.alloc_pgt_page = alloc_pgt_page; + mapping_info.context = &pgt_data; +/* * It should be impossible for this not to already be true, * but since calling this a second time would rewind the other
@@ -96,17 +111,8 @@ static void prepare_level4(void)*/ void add_identity_map (unsigned long start,unsigned long size) { -struct x86_mapping_info mapping_info = { - .alloc_pgt_page = alloc_pgt_page, - .context = &pgt_data, - .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, - };unsigned long end = start + size; -/* Make sure we have a top level page table ready to use. */ -if (!level4p) -prepare_level4 (); -/* Align boundary to 2M. */ start =round_down (start,PMD_SIZE ); end =round_up (end,PMD_SIZE );
Signature · 1 line
2.6.3
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/KASLR: Clarify identity map interface
x86/KASLR: Clarify identity map interface
Commit-ID: 00860f47c2ac6706060656f423b119af1f454bcd Gitweb: http://git.kernel.org/tip/00860f47c2ac6706060656f423b119af1f454bcd Author: Kees Cook <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:31 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 17 Jun 2016 11:03:46 +0200
x86/KASLR: Clarify identity map interface
This extracts the call to prepare_level4() into a top-level function that the user of the pagetable.c interface must call to initialize the new page tables. For clarity and to match the "finalize" function, it has been renamed to initialize_identity_maps(). This function also gains the initialization of mapping_info so we don't have to do it each time in add_identity_map().
Additionally add copyright notice to the top, to make it clear that the bulk of the pagetable.c code was written by Yinghai, and that I just added bugs later. :)
--- arch/x86/boot/compressed/kaslr.c | 3 +++ arch/x86/boot/compressed/misc.h | 3 +++ arch/x86/boot/compressed/pagetable.c | 26 ++++++++++++++++---------- 3 files changed, 22 insertions(+), 10 deletions(-)
arch/x86/boot/compressed/kaslr.c +3 -0
@@ -478,6 +478,9 @@ unsigned char *choose_random_location(unsigned long input, boot_params->hdr.loadflags |=KASLR_FLAG ; +/* Prepare to add new identity pagetables on demand. */ +initialize_identity_maps (); +/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, output);
arch/x86/boot/compressed/misc.h +3 -0
@@ -85,10 +85,13 @@ unsigned char *choose_random_location(unsigned long input_ptr,#endif #ifdef CONFIG_X86_64 +void initialize_identity_maps (void );void add_identity_map (unsigned long start,unsigned long size);void finalize_identity_maps (void );extern unsigned char _pgtable[];#else +static inline void initialize_identity_maps (void ) +{ }static inline void add_identity_map (unsigned long start,unsigned long size) { }static inline void finalize_identity_maps (void )
arch/x86/boot/compressed/pagetable.c +16 -10
@@ -2,6 +2,9 @@* This code is used on x86_64 to create page table identity mappings on * demand by building up a new set of page tables (or appending to the * existing ones), and then switching over to them when ready. +* +* Copyright (C) 2015-2016 Yinghai Lu +* Copyright (C) 2016 Kees Cook */ /*
@@ -59,9 +62,21 @@ static struct alloc_pgt_data pgt_data;/* The top level page table entry pointer. */ static unsigned long level4p; +/* +* Mapping information structure passed to kernel_ident_mapping_init(). +* Due to relocation, pointers must be assigned at run time not build time. +*/ +static struct x86_mapping_info mapping_info = { + .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, +}; +/* Locates and clears a region for a new top level page table. */ -static void prepare_level4 (void ) +void initialize_identity_maps (void ) { +/* Init mapping_info with run-time function/buffer pointers. */ + mapping_info.alloc_pgt_page = alloc_pgt_page; + mapping_info.context = &pgt_data; +/* * It should be impossible for this not to already be true, * but since calling this a second time would rewind the other
@@ -96,17 +111,8 @@ static void prepare_level4(void)*/ void add_identity_map (unsigned long start,unsigned long size) { -struct x86_mapping_info mapping_info = { - .alloc_pgt_page = alloc_pgt_page, - .context = &pgt_data, - .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, - };unsigned long end = start + size; -/* Make sure we have a top level page table ready to use. */ -if (!level4p) -prepare_level4 (); -/* Align boundary to 2M. */ start =round_down (start,PMD_SIZE ); end =round_up (end,PMD_SIZE );
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/KASLR: Clarify identity map interface
In reply to Kees Cookx86/KASLR: Clarify identity map interface
Commit-ID: 11fdf97a3cd1a5a27625f820ceb74e1caba4fd26 Gitweb: http://git.kernel.org/tip/11fdf97a3cd1a5a27625f820ceb74e1caba4fd26 Author: Kees Cook <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:31 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sun, 26 Jun 2016 12:32:04 +0200
x86/KASLR: Clarify identity map interface
This extracts the call to prepare_level4() into a top-level function that the user of the pagetable.c interface must call to initialize the new page tables. For clarity and to match the "finalize" function, it has been renamed to initialize_identity_maps(). This function also gains the initialization of mapping_info so we don't have to do it each time in add_identity_map().
Additionally add copyright notice to the top, to make it clear that the bulk of the pagetable.c code was written by Yinghai, and that I just added bugs later. :)
--- arch/x86/boot/compressed/kaslr.c | 3 +++ arch/x86/boot/compressed/misc.h | 3 +++ arch/x86/boot/compressed/pagetable.c | 26 ++++++++++++++++---------- 3 files changed, 22 insertions(+), 10 deletions(-)
arch/x86/boot/compressed/kaslr.c +3 -0
@@ -478,6 +478,9 @@ unsigned char *choose_random_location(unsigned long input, boot_params->hdr.loadflags |=KASLR_FLAG ; +/* Prepare to add new identity pagetables on demand. */ +initialize_identity_maps (); +/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, output);
arch/x86/boot/compressed/misc.h +3 -0
@@ -85,10 +85,13 @@ unsigned char *choose_random_location(unsigned long input_ptr,#endif #ifdef CONFIG_X86_64 +void initialize_identity_maps (void );void add_identity_map (unsigned long start,unsigned long size);void finalize_identity_maps (void );extern unsigned char _pgtable[];#else +static inline void initialize_identity_maps (void ) +{ }static inline void add_identity_map (unsigned long start,unsigned long size) { }static inline void finalize_identity_maps (void )
arch/x86/boot/compressed/pagetable.c +16 -10
@@ -2,6 +2,9 @@* This code is used on x86_64 to create page table identity mappings on * demand by building up a new set of page tables (or appending to the * existing ones), and then switching over to them when ready. +* +* Copyright (C) 2015-2016 Yinghai Lu +* Copyright (C) 2016 Kees Cook */ /*
@@ -59,9 +62,21 @@ static struct alloc_pgt_data pgt_data;/* The top level page table entry pointer. */ static unsigned long level4p; +/* +* Mapping information structure passed to kernel_ident_mapping_init(). +* Due to relocation, pointers must be assigned at run time not build time. +*/ +static struct x86_mapping_info mapping_info = { + .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, +}; +/* Locates and clears a region for a new top level page table. */ -static void prepare_level4 (void ) +void initialize_identity_maps (void ) { +/* Init mapping_info with run-time function/buffer pointers. */ + mapping_info.alloc_pgt_page = alloc_pgt_page; + mapping_info.context = &pgt_data; +/* * It should be impossible for this not to already be true, * but since calling this a second time would rewind the other
@@ -96,17 +111,8 @@ static void prepare_level4(void)*/ void add_identity_map (unsigned long start,unsigned long size) { -struct x86_mapping_info mapping_info = { - .alloc_pgt_page = alloc_pgt_page, - .context = &pgt_data, - .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, - };unsigned long end = start + size; -/* Make sure we have a top level page table ready to use. */ -if (!level4p) -prepare_level4 (); -/* Align boundary to 2M. */ start =round_down (start,PMD_SIZE ); end =round_up (end,PMD_SIZE );
PATCH v9 3/5 x86/KASLR: Randomize virtual address separately
In reply to Kees CookFrom: Baoquan He <[email protected]>
The current KASLR implementation randomizes the physical and virtual addresses of the kernel together (both are offset by the same amount). It calculates the delta of the physical address where vmlinux was linked to load and where it is finally loaded. If the delta is not equal to 0 (i.e. the kernel was relocated), relocation handling needs be done.
On 64-bit, this patch randomizes both the physical address where kernel is decompressed and the virtual address where kernel text is mapped and will execute from. We now have two values being chosen, so the function arguments are reorganized to pass by pointer so they can be directly updated. Since relocation handling only depends on the virtual address, we must check the virtual delta, not the physical delta for processing kernel relocations. This also populates the page table for the new virtual address range. 32-bit does not support a separate virtual address, so it continues to use the physical offset for its virtual offset.
Additionally updates the sanity checks done on the resulting kernel addresses since they are potentially separate now.
--- arch/x86/boot/compressed/kaslr.c | 44 ++++++++++++++++++++---------------- arch/x86/boot/compressed/misc.c | 49 ++++++++++++++++++++++++---------------- arch/x86/boot/compressed/misc.h | 22 ++++++++++-------- 3 files changed, 66 insertions(+), 49 deletions(-)
arch/x86/boot/compressed/kaslr.c +24 -20
@@ -463,23 +463,26 @@ static unsigned long find_random_virt_addr(unsigned long minimum,* Since this function examines addresses much more numerically, * it takes the input and output pointers as 'unsigned long'. */ -unsigned char *choose_random_location (unsigned long input, -unsigned long input_size, -unsigned long output, -unsigned long output_size) +void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr) { -unsigned long choice = output;unsigned long random_addr; +/* By default, keep output position unchanged. */ + *virt_addr = *output; +#ifdef CONFIG_HIBERNATION if (!cmdline_find_option_bool ("kaslr" )) {warn ("KASLR disabled: 'kaslr' not on cmdline (hibernation selected)." ); - goto out; +return ; }#else if (cmdline_find_option_bool ("nokaslr" )) {warn ("KASLR disabled: 'nokaslr' on cmdline." ); - goto out; +return ; }#endif
@@ -489,25 +492,26 @@ unsigned char *choose_random_location(unsigned long input,initialize_identity_maps ();/* Record the various known unsafe memory ranges. */ -mem_avoid_init (input, input_size, output); +mem_avoid_init (input, input_size, *output);/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (output, output_size); + random_addr =find_random_phys_addr (*output, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); - goto out; + }else { +/* Update the new physical address location. */ +if (*output != random_addr) { +add_identity_map (random_addr, output_size); + *output = random_addr; + } } -/* Always enforce the minimum. */ -if (random_addr < choice) - goto out; - - choice = random_addr; - -add_identity_map (choice, output_size); -/* This actually loads the identity pagetable on x86_64. */ finalize_identity_maps (); -out: -return (unsigned char *)choice; + +/* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */ +if (IS_ENABLED (CONFIG_X86_64 )) + random_addr =find_random_virt_addr (LOAD_PHYSICAL_ADDR , + output_size); + *virt_addr = random_addr; }
arch/x86/boot/compressed/misc.c +30 -19
100 lines not on this page · raw message
arch/x86/boot/compressed/misc.h +12 -10
@@ -67,20 +67,22 @@ int cmdline_find_option_bool(const char *option);#if CONFIG_RANDOMIZE_BASE /* kaslr.c */ -unsigned char *choose_random_location (unsigned long input_ptr, -unsigned long input_size, -unsigned long output_ptr, -unsigned long output_size); +void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr);/* cpuflags.c */ bool has_cpuflag (int flag);#else -static inline -unsigned char *choose_random_location (unsigned long input_ptr, -unsigned long input_size, -unsigned long output_ptr, -unsigned long output_size) +static inline void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr) { -return (unsigned char *)output_ptr; +/* No change from existing output location. */ + *virt_addr = *output; }#endif
Signature · 1 line
2.6.3
Quoted 5 lines · Kees Cook
arch/x86/boot/compressed/kaslr.c +1
-4-unsigned char *choose_random_location (unsigned long input, -unsigned long input_size, -unsigned long output, -unsigned long output_size) +void choose_random_location(unsigned long input,
arch/x86/boot/compressed/kaslr.c static unsigned long find_random_virt_addr(unsigned long minimum, · line 475
Quoted 10 lines
arch/x86/boot/compressed/kaslr.c +6
-1+unsigned long input_size, + unsigned long *output, + unsigned long output_size, + unsigned long *virt_addr) { -unsigned long choice = output; unsigned long random_addr; +/* By default, keep output position unchanged. */ + *virt_addr = *output;
So I applied this, after fixing a conflict with a recent hibernation related change, but it would be nice to further clean up the types in this file, in particular could we please propagate 'const' for all input-only pointers?
For example in the above function it would be obvious at a glance if it said something like:
void choose_random_location(unsigned long input,
unsigned long input_size, const unsigned long *output, unsigned long output_size, unsigned long *virt_addr)
when reading such a function prototype I can immediately tell: 'yeah, while it's named "output", it's in fact a read-only input parameter - the _real_ output of the function is 'virt_addr'.)
In addition to that it would also be useful to eliminate the 'virt_addr' parameter altogether, and use an 'unsigned long' return value to set virt_addr in misc.c.
Ok?
Thanks,
Ingo
Quoted 32 lines · Ingo Molnar
Quoted 5 lines · Kees Cook
arch/x86/boot/compressed/kaslr.c +1
-4-unsigned char *choose_random_location (unsigned long input, -unsigned long input_size, -unsigned long output, -unsigned long output_size) +void choose_random_location(unsigned long input,arch/x86/boot/compressed/kaslr.c static unsigned long find_random_virt_addr(unsigned long minimum, · line 475
Quoted 10 lines
arch/x86/boot/compressed/kaslr.c +6
-1+unsigned long input_size, + unsigned long *output, + unsigned long output_size, + unsigned long *virt_addr) { -unsigned long choice = output; unsigned long random_addr; +/* By default, keep output position unchanged. */ + *virt_addr = *output;So I applied this, after fixing a conflict with a recent hibernation related change, but it would be nice to further clean up the types in this file, in particular could we please propagate 'const' for all input-only pointers?
For example in the above function it would be obvious at a glance if it said something like:
void choose_random_location(unsigned long input,
unsigned longinput_size, const unsigned long *output, unsigned longoutput_size, unsigned long*virt_addr)
Quoted 3 lines
when reading such a function prototype I can immediately tell: 'yeah, while it's named "output", it's in fact a read-only input parameter - the _real_ output of the function is '
virt_addr'.)
Doh, so I managed to confuse myself by looking at the unpatched function only. This patch in fact starts writing to 'output':
+ /* Update the new physical address location. */
+ if (*output != random_addr) {
+ add_identity_map(random_addr, output_size);
+ *output = random_addr;
+ }
At which point 'output' cannot be const, and in fact it might be beneficial that 'virt_addr' is passed in by a pointer as well.
The comment of the function definitely needs to be updated:
* it takes the input and output pointers as 'unsigned long'.
... which is not true anymore.
I also find the type flow and naming for the 'output' pointer very confusing. We have:
asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
unsigned char *input_data, unsigned long input_len, unsigned char *output, unsigned long output_len)
...
choose_random_location((unsigned long)input_data, input_len,
(unsigned long *)&output, max(output_len, kernel_total_size), &virt_addr);
void choose_random_location(unsigned long input,
unsigned long input_size, unsigned long *output, unsigned long output_size, unsigned long *virt_addr)
...
*output = random_addr;
it is very easy to confuse 'unsigned long *output' with the 'char *output' pointer to the output stream! But in reality this is a double pointer and we want to use it to change the pointer.
So at minimum we should rename 'output' in choose_random_location() to something like 'output_ptr' - but even better would be to just preserve its natural type and use 'char * const *' and do a single type cast when setting it.
Same goes for 'virt_addr'.
Agreed?
Thanks,
Ingo
tip-bot for Baoquan He
merge botx86/KASLR: Randomize virtual address separately
Commit-ID: ad908dc080e2d8ab26391d0013d2c8157ca0e2da Gitweb: http://git.kernel.org/tip/ad908dc080e2d8ab26391d0013d2c8157ca0e2da Author: Baoquan He <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:32 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 17 Jun 2016 11:03:47 +0200
x86/KASLR: Randomize virtual address separately
The current KASLR implementation randomizes the physical and virtual addresses of the kernel together (both are offset by the same amount). It calculates the delta of the physical address where vmlinux was linked to load and where it is finally loaded. If the delta is not equal to 0 (i.e. the kernel was relocated), relocation handling needs be done.
On 64-bit, this patch randomizes both the physical address where kernel is decompressed and the virtual address where kernel text is mapped and will execute from. We now have two values being chosen, so the function arguments are reorganized to pass by pointer so they can be directly updated. Since relocation handling only depends on the virtual address, we must check the virtual delta, not the physical delta for processing kernel relocations. This also populates the page table for the new virtual address range. 32-bit does not support a separate virtual address, so it continues to use the physical offset for its virtual offset.
Additionally updates the sanity checks done on the resulting kernel addresses since they are potentially separate now.
--- arch/x86/boot/compressed/kaslr.c | 41 +++++++++++++++++---------------- arch/x86/boot/compressed/misc.c | 49 ++++++++++++++++++++++++---------------- arch/x86/boot/compressed/misc.h | 22 ++++++++++-------- 3 files changed, 64 insertions(+), 48 deletions(-)
arch/x86/boot/compressed/kaslr.c +22 -19
@@ -463,17 +463,20 @@ static unsigned long find_random_virt_addr(unsigned long minimum,* Since this function examines addresses much more numerically, * it takes the input and output pointers as 'unsigned long'. */ -unsigned char *choose_random_location (unsigned long input, -unsigned long input_size, -unsigned long output, -unsigned long output_size) +void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr) { -unsigned long choice = output;unsigned long random_addr; +/* By default, keep output position unchanged. */ + *virt_addr = *output; +if (cmdline_find_option_bool ("nokaslr" )) {warn ("KASLR disabled: 'nokaslr' on cmdline." ); - goto out; +return ; } boot_params->hdr.loadflags |=KASLR_FLAG ;
@@ -482,25 +485,25 @@ unsigned char *choose_random_location(unsigned long input,initialize_identity_maps ();/* Record the various known unsafe memory ranges. */ -mem_avoid_init (input, input_size, output); +mem_avoid_init (input, input_size, *output);/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (output, output_size); + random_addr =find_random_phys_addr (*output, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); - goto out; + }else { +/* Update the new physical address location. */ +if (*output != random_addr) { +add_identity_map (random_addr, output_size); + *output = random_addr; + } } -/* Always enforce the minimum. */ -if (random_addr < choice) - goto out; - - choice = random_addr; - -add_identity_map (choice, output_size); -/* This actually loads the identity pagetable on x86_64. */ finalize_identity_maps (); -out: -return (unsigned char *)choice; + +/* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */ +if (IS_ENABLED (CONFIG_X86_64 )) + random_addr =find_random_virt_addr (LOAD_PHYSICAL_ADDR , output_size); + *virt_addr = random_addr; }
arch/x86/boot/compressed/misc.c +30 -19
100 lines not on this page · raw message
arch/x86/boot/compressed/misc.h +12 -10
@@ -67,20 +67,22 @@ int cmdline_find_option_bool(const char *option);#if CONFIG_RANDOMIZE_BASE /* kaslr.c */ -unsigned char *choose_random_location (unsigned long input_ptr, -unsigned long input_size, -unsigned long output_ptr, -unsigned long output_size); +void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr);/* cpuflags.c */ bool has_cpuflag (int flag);#else -static inline -unsigned char *choose_random_location (unsigned long input_ptr, -unsigned long input_size, -unsigned long output_ptr, -unsigned long output_size) +static inline void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr) { -return (unsigned char *)output_ptr; +/* No change from existing output location. */ + *virt_addr = *output; }#endif
tip-bot for Baoquan He
merge botx86/KASLR: Randomize virtual address separately
Commit-ID: 8391c73c96f28d4e8c40fd401fd0c9c04391b44a Gitweb: http://git.kernel.org/tip/8391c73c96f28d4e8c40fd401fd0c9c04391b44a Author: Baoquan He <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:32 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sun, 26 Jun 2016 12:32:04 +0200
x86/KASLR: Randomize virtual address separately
The current KASLR implementation randomizes the physical and virtual addresses of the kernel together (both are offset by the same amount). It calculates the delta of the physical address where vmlinux was linked to load and where it is finally loaded. If the delta is not equal to 0 (i.e. the kernel was relocated), relocation handling needs be done.
On 64-bit, this patch randomizes both the physical address where kernel is decompressed and the virtual address where kernel text is mapped and will execute from. We now have two values being chosen, so the function arguments are reorganized to pass by pointer so they can be directly updated. Since relocation handling only depends on the virtual address, we must check the virtual delta, not the physical delta for processing kernel relocations. This also populates the page table for the new virtual address range. 32-bit does not support a separate virtual address, so it continues to use the physical offset for its virtual offset.
Additionally updates the sanity checks done on the resulting kernel addresses since they are potentially separate now.
--- arch/x86/boot/compressed/kaslr.c | 41 +++++++++++++++++---------------- arch/x86/boot/compressed/misc.c | 49 ++++++++++++++++++++++++---------------- arch/x86/boot/compressed/misc.h | 22 ++++++++++-------- 3 files changed, 64 insertions(+), 48 deletions(-)
arch/x86/boot/compressed/kaslr.c +22 -19
@@ -463,17 +463,20 @@ static unsigned long find_random_virt_addr(unsigned long minimum,* Since this function examines addresses much more numerically, * it takes the input and output pointers as 'unsigned long'. */ -unsigned char *choose_random_location (unsigned long input, -unsigned long input_size, -unsigned long output, -unsigned long output_size) +void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr) { -unsigned long choice = output;unsigned long random_addr; +/* By default, keep output position unchanged. */ + *virt_addr = *output; +if (cmdline_find_option_bool ("nokaslr" )) {warn ("KASLR disabled: 'nokaslr' on cmdline." ); - goto out; +return ; } boot_params->hdr.loadflags |=KASLR_FLAG ;
@@ -482,25 +485,25 @@ unsigned char *choose_random_location(unsigned long input,initialize_identity_maps ();/* Record the various known unsafe memory ranges. */ -mem_avoid_init (input, input_size, output); +mem_avoid_init (input, input_size, *output);/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (output, output_size); + random_addr =find_random_phys_addr (*output, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); - goto out; + }else { +/* Update the new physical address location. */ +if (*output != random_addr) { +add_identity_map (random_addr, output_size); + *output = random_addr; + } } -/* Always enforce the minimum. */ -if (random_addr < choice) - goto out; - - choice = random_addr; - -add_identity_map (choice, output_size); -/* This actually loads the identity pagetable on x86_64. */ finalize_identity_maps (); -out: -return (unsigned char *)choice; + +/* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */ +if (IS_ENABLED (CONFIG_X86_64 )) + random_addr =find_random_virt_addr (LOAD_PHYSICAL_ADDR , output_size); + *virt_addr = random_addr; }
arch/x86/boot/compressed/misc.c +30 -19
100 lines not on this page · raw message
arch/x86/boot/compressed/misc.h +12 -10
@@ -67,20 +67,22 @@ int cmdline_find_option_bool(const char *option);#if CONFIG_RANDOMIZE_BASE /* kaslr.c */ -unsigned char *choose_random_location (unsigned long input_ptr, -unsigned long input_size, -unsigned long output_ptr, -unsigned long output_size); +void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr);/* cpuflags.c */ bool has_cpuflag (int flag);#else -static inline -unsigned char *choose_random_location (unsigned long input_ptr, -unsigned long input_size, -unsigned long output_ptr, -unsigned long output_size) +static inline void choose_random_location (unsigned long input, +unsigned long input_size, +unsigned long *output, +unsigned long output_size, +unsigned long *virt_addr) { -return (unsigned char *)output_ptr; +/* No change from existing output location. */ + *virt_addr = *output; }#endif
PATCH v9 4/5 x86/KASLR: Add physical address randomization >4G
In reply to Kees CookThis patch exchanges the prior slots[] array for the new slot_areas[] array, and lifts the limitation of KERNEL_IMAGE_SIZE on the physical address offset for 64-bit. As before, process_e820_entry() walks memory and populates slot_areas[], splitting on any detected mem_avoid collisions.
Finally, since the slots[] array and its associated functions are not needed any more, so they are removed.
Based on earlier patches by Baoquan He.
---
This patch is pretty noisy due to the indentation change in the e820 walker. I couldn't find a cleaner way to do this that didn't make the final code LESS readable, unfortunately. So, the diff is ugly, but I think the results are clean.
--- arch/x86/Kconfig | 27 +++++---- arch/x86/boot/compressed/kaslr.c | 115 +++++++++++++++++++++++---------------- 2 files changed, 85 insertions(+), 57 deletions(-)
arch/x86/Kconfig +16 -11
@@ -1934,21 +1934,26 @@ config RANDOMIZE_BASE attempts relying on knowledge of the location of kernel code internals. - The kernel physical and virtual address can be randomized - from 16MB up to 1GB on 64-bit and 512MB on 32-bit. (Note that - using RANDOMIZE_BASE reduces the memory space available to - kernel modules from 1.5GB to 1GB.) + On 64-bit, the kernel physical and virtual addresses are + randomized separately. The physical address will be anywhere + between 16MB and the top of physical memory (up to 64TB). The + virtual address will be randomized from 16MB up to 1GB (9 bits + of entropy). Note that this also reduces the memory space + available to kernel modules from 1.5GB to 1GB. + + On 32-bit, the kernel physical and virtual addresses are + randomized together. They will be randomized from 16MB up to + 512MB (8 bits of entropy). Entropy is generated using the RDRAND instruction if it is supported. If RDTSC is supported, its value is mixed into the entropy pool as well. If neither RDRAND nor RDTSC are - supported, then entropy is read from the i8254 timer. - - Since the kernel is built using 2GB addressing, and - PHYSICAL_ALIGN must be at a minimum of 2MB, only 10 bits of - entropy is theoretically possible. Currently, with the - default value for PHYSICAL_ALIGN and due to page table - layouts, 64-bit uses 9 bits of entropy and 32-bit uses 8 bits. + supported, then entropy is read from the i8254 timer. The + usable entropy is limited by the kernel being built using + 2GB addressing, and that PHYSICAL_ALIGN must be at a + minimum of 2MB. As a result, only 10 bits of entropy are + theoretically possible, but the implementations are further + limited due to memory layouts. If CONFIG_HIBERNATE is also enabled, KASLR is disabled at boot time. To enable it, boot with "kaslr" on the kernel command
arch/x86/boot/compressed/kaslr.c +69 -46
167 lines not on this page · raw message
Signature · 1 line
2.6.3
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/KASLR: Extend kernel image physical address randomization to addresses larger than 4G
x86/KASLR: Extend kernel image physical address randomization to addresses larger than 4G
Commit-ID: 9099fab617cd0518cc537849623fb440315b3c91 Gitweb: http://git.kernel.org/tip/9099fab617cd0518cc537849623fb440315b3c91 Author: Kees Cook <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:33 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 17 Jun 2016 11:03:48 +0200
x86/KASLR: Extend kernel image physical address randomization to addresses larger than 4G
We want the physical address to be randomized anywhere between 16MB and the top of physical memory (up to 64TB).
This patch exchanges the prior slots[] array for the new slot_areas[] array, and lifts the limitation of KERNEL_IMAGE_SIZE on the physical address offset for 64-bit. As before, process_e820_entry() walks memory and populates slot_areas[], splitting on any detected mem_avoid collisions.
Finally, since the slots[] array and its associated functions are not needed any more, so they are removed.
Based on earlier patches by Baoquan He.
Originally-from: Baoquan He <[email protected]>
--- arch/x86/Kconfig | 27 +++++---- arch/x86/boot/compressed/kaslr.c | 115 +++++++++++++++++++++++---------------- 2 files changed, 85 insertions(+), 57 deletions(-)
arch/x86/Kconfig +16 -11
@@ -1934,21 +1934,26 @@ config RANDOMIZE_BASE attempts relying on knowledge of the location of kernel code internals. - The kernel physical and virtual address can be randomized - from 16MB up to 1GB on 64-bit and 512MB on 32-bit. (Note that - using RANDOMIZE_BASE reduces the memory space available to - kernel modules from 1.5GB to 1GB.) + On 64-bit, the kernel physical and virtual addresses are + randomized separately. The physical address will be anywhere + between 16MB and the top of physical memory (up to 64TB). The + virtual address will be randomized from 16MB up to 1GB (9 bits + of entropy). Note that this also reduces the memory space + available to kernel modules from 1.5GB to 1GB. + + On 32-bit, the kernel physical and virtual addresses are + randomized together. They will be randomized from 16MB up to + 512MB (8 bits of entropy). Entropy is generated using the RDRAND instruction if it is supported. If RDTSC is supported, its value is mixed into the entropy pool as well. If neither RDRAND nor RDTSC are - supported, then entropy is read from the i8254 timer. - - Since the kernel is built using 2GB addressing, and - PHYSICAL_ALIGN must be at a minimum of 2MB, only 10 bits of - entropy is theoretically possible. Currently, with the - default value for PHYSICAL_ALIGN and due to page table - layouts, 64-bit uses 9 bits of entropy and 32-bit uses 8 bits. + supported, then entropy is read from the i8254 timer. The + usable entropy is limited by the kernel being built using + 2GB addressing, and that PHYSICAL_ALIGN must be at a + minimum of 2MB. As a result, only 10 bits of entropy are + theoretically possible, but the implementations are further + limited due to memory layouts. If CONFIG_HIBERNATE is also enabled, KASLR is disabled at boot time. To enable it, boot with "kaslr" on the kernel command
arch/x86/boot/compressed/kaslr.c +69 -46
167 lines not on this page · raw message
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/KASLR: Extend kernel image physical address randomization to addresses larger than 4G
In reply to Kees Cookx86/KASLR: Extend kernel image physical address randomization to addresses larger than 4G
Commit-ID: ed9f007ee68478f6a50ec9971ade25a0129a5c0e Gitweb: http://git.kernel.org/tip/ed9f007ee68478f6a50ec9971ade25a0129a5c0e Author: Kees Cook <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:33 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sun, 26 Jun 2016 12:32:05 +0200
x86/KASLR: Extend kernel image physical address randomization to addresses larger than 4G
We want the physical address to be randomized anywhere between 16MB and the top of physical memory (up to 64TB).
This patch exchanges the prior slots[] array for the new slot_areas[] array, and lifts the limitation of KERNEL_IMAGE_SIZE on the physical address offset for 64-bit. As before, process_e820_entry() walks memory and populates slot_areas[], splitting on any detected mem_avoid collisions.
Finally, since the slots[] array and its associated functions are not needed any more, so they are removed.
Based on earlier patches by Baoquan He.
Originally-from: Baoquan He <[email protected]>
--- arch/x86/Kconfig | 27 +++++---- arch/x86/boot/compressed/kaslr.c | 115 +++++++++++++++++++++++---------------- 2 files changed, 85 insertions(+), 57 deletions(-)
arch/x86/Kconfig +16 -11
@@ -1934,21 +1934,26 @@ config RANDOMIZE_BASE attempts relying on knowledge of the location of kernel code internals. - The kernel physical and virtual address can be randomized - from 16MB up to 1GB on 64-bit and 512MB on 32-bit. (Note that - using RANDOMIZE_BASE reduces the memory space available to - kernel modules from 1.5GB to 1GB.) + On 64-bit, the kernel physical and virtual addresses are + randomized separately. The physical address will be anywhere + between 16MB and the top of physical memory (up to 64TB). The + virtual address will be randomized from 16MB up to 1GB (9 bits + of entropy). Note that this also reduces the memory space + available to kernel modules from 1.5GB to 1GB. + + On 32-bit, the kernel physical and virtual addresses are + randomized together. They will be randomized from 16MB up to + 512MB (8 bits of entropy). Entropy is generated using the RDRAND instruction if it is supported. If RDTSC is supported, its value is mixed into the entropy pool as well. If neither RDRAND nor RDTSC are - supported, then entropy is read from the i8254 timer. - - Since the kernel is built using 2GB addressing, and - PHYSICAL_ALIGN must be at a minimum of 2MB, only 10 bits of - entropy is theoretically possible. Currently, with the - default value for PHYSICAL_ALIGN and due to page table - layouts, 64-bit uses 9 bits of entropy and 32-bit uses 8 bits. + supported, then entropy is read from the i8254 timer. The + usable entropy is limited by the kernel being built using + 2GB addressing, and that PHYSICAL_ALIGN must be at a + minimum of 2MB. As a result, only 10 bits of entropy are + theoretically possible, but the implementations are further + limited due to memory layouts. If CONFIG_HIBERNATE is also enabled, KASLR is disabled at boot time. To enable it, boot with "kaslr" on the kernel command
arch/x86/boot/compressed/kaslr.c +69 -46
167 lines not on this page · raw message
PATCH v9 5/5 x86/KASLR: Allow randomization below load address
In reply to Kees CookFrom: Yinghai Lu <[email protected]>
Currently the physical randomization's lower boundary is the original kernel load address. For bootloaders that load kernels into very high memory (e.g. kexec), this means randomization takes place in a very small window at the top of memory, ignoring the large region of physical memory below the load address.
Since mem_avoid is already correctly tracking the regions that must be avoided, this patch changes the minimum address to whatever is less:
512M (to conservatively avoid unknown things in lower memory) or the load address. Now, for example, if the kernel is loaded at 8G, [512M, 8G) will be added into possible physical memory positions.
--- arch/x86/boot/compressed/kaslr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/kaslr.c +5 -2
@@ -492,7 +492,7 @@ void choose_random_location(unsigned long input,unsigned long output_size, unsigned long *virt_addr) { - unsigned long random_addr; + unsigned long random_addr, min_addr;/* By default, keep output position unchanged. */ *virt_addr = *output;
@@ -517,8 +517,11 @@ void choose_random_location(unsigned long input,/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, *output); +/* Low end should be the smaller of 512M or initial location. */ + min_addr =min (*output,512UL <<20 ); +/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (*output, output_size); + random_addr =find_random_phys_addr (min_addr, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); }else {
Signature · 1 line
2.6.3
Re: [PATCH v9 5/5] x86/KASLR: Allow randomization below load address
Quoted 37 lines · Kees Cook
From: Yinghai Lu <[email protected]>
Currently the physical randomization's lower boundary is the original kernel load address. For bootloaders that load kernels into very high memory (e.g. kexec), this means randomization takes place in a very small window at the top of memory, ignoring the large region of physical memory below the load address.
Since
mem_avoidis already correctly tracking the regions that must be avoided, this patch changes the minimum address to whatever is less:
512M (to conservatively avoid unknown things in lower memory) or the load address. Now, for example, if the kernel is loaded at 8G, [512M, 8G) will be added into possible physical memory positions.Signed-off-by Yinghai Lu <[email protected]>[kees: rewrote changelog, refactor to use min()]Signed-off-by Kees Cook <[email protected]>--- arch/x86/boot/compressed/kaslr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)arch/x86/boot/compressed/kaslr.c +1
-1@@ -492,7 +492,7 @@ void choose_random_location(unsigned long input,unsigned long output_size, unsigned long *virt_addr) { - unsigned long random_addr; + unsigned long random_addr, min_addr;/* By default, keep output position unchanged. */ *virt_addr = *output;@@ -517,8 +517,11 @@ void choose_random_location(unsigned long input,/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, *output);
arch/x86/boot/compressed/kaslr.c void choose_random_location(unsigned long input, · line 527
Quoted 10 lines
arch/x86/boot/compressed/kaslr.c +4
-1+/* Low end should be the smaller of 512M or initial location. */ + min_addr =min (*output,512UL <<20 ); +/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (*output, output_size); + random_addr =find_random_phys_addr (min_addr, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); }else {
There's no explanation in the code or in the changelog of why 512M was picked as the lower limit.
Thanks,
Ingo
Re: [PATCH v9 5/5] x86/KASLR: Allow randomization below load address
Quoted 41 lines · Ingo Molnar
Quoted 38 lines · Kees Cook
From: Yinghai Lu <[email protected]>
Currently the physical randomization's lower boundary is the original kernel load address. For bootloaders that load kernels into very high memory (e.g. kexec), this means randomization takes place in a very small window at the top of memory, ignoring the large region of physical memory below the load address.
Since
mem_avoidis already correctly tracking the regions that must be avoided, this patch changes the minimum address to whatever is less:
512M (to conservatively avoid unknown things in lower memory) or the load address. Now, for example, if the kernel is loaded at 8G, [512M, 8G) will be added into possible physical memory positions.Signed-off-by Yinghai Lu <[email protected]>[kees: rewrote changelog, refactor to use min()]Signed-off-by Kees Cook <[email protected]>--- arch/x86/boot/compressed/kaslr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)arch/x86/boot/compressed/kaslr.c +1
-1@@ -492,7 +492,7 @@ void choose_random_location(unsigned long input,unsigned long output_size, unsigned long *virt_addr) { - unsigned long random_addr; + unsigned long random_addr, min_addr;/* By default, keep output position unchanged. */ *virt_addr = *output;@@ -517,8 +517,11 @@ void choose_random_location(unsigned long input,/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, *output);
Quoted 12 lines
arch/x86/boot/compressed/kaslr.c void choose_random_location(unsigned long input, · line 527
Quoted 9 lines
arch/x86/boot/compressed/kaslr.c +4
-1+/* Low end should be the smaller of 512M or initial location. */ + min_addr =min (*output,512UL <<20 ); +/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (*output, output_size); + random_addr =find_random_phys_addr (min_addr, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); }else {There's no explanation in the code or in the changelog of why 512M was picked as the lower limit.
Yinghai, do you have a rationale for this selection? I understood it to just be a very conservative target to avoid anything in low physical memory, but perhaps there is a better reason?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v9 5/5] x86/KASLR: Allow randomization below load address
Quoted 7 lines · Kees Cook
Quoted 3 lines
There's no explanation in the code or in the changelog of why 512M was picked as the lower limit.
Yinghai, do you have a rationale for this selection? I understood it to just be a very conservative target to avoid anything in low physical memory, but perhaps there is a better reason?
when kernel is not loaded high at first, then *output should be 16M or so, so no change.
when kernel is loaded high to save the low address space, don't want to KASL to pull back to low address again.
If choose 4G, on 4G+512M config, when kernel is loaded high, kasl may not work to chose range from 4G.
so I choose 512M, just stay away range for KERNEL_IMAGE_SIZE.
Thanks
Yinghai
tip-bot for Yinghai Lu
merge bot[tip:x86/boot] x86/KASLR: Allow randomization below the load address
In reply to Kees Cookx86/KASLR: Allow randomization below the load address
Commit-ID: 00bdbb0a0d6e5c7235cb8faa298c9f494e088499 Gitweb: http://git.kernel.org/tip/00bdbb0a0d6e5c7235cb8faa298c9f494e088499 Author: Yinghai Lu <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:34 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 17 Jun 2016 11:03:49 +0200
x86/KASLR: Allow randomization below the load address
Currently the kernel image physical address randomization's lower boundary is the original kernel load address.
For bootloaders that load kernels into very high memory (e.g. kexec), this means randomization takes place in a very small window at the top of memory, ignoring the large region of physical memory below the load address.
Since mem_avoid[] is already correctly tracking the regions that must be avoided, this patch changes the minimum address to whatever is less:
512M (to conservatively avoid unknown things in lower memory) or the load address. Now, for example, if the kernel is loaded at 8G, [512M, 8G) will be added to the list of possible physical memory positions.
--- arch/x86/boot/compressed/kaslr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/kaslr.c +9 -2
@@ -492,7 +492,7 @@ void choose_random_location(unsigned long input,unsigned long output_size, unsigned long *virt_addr) { - unsigned long random_addr; + unsigned long random_addr, min_addr;/* By default, keep output position unchanged. */ *virt_addr = *output;
@@ -510,8 +510,15 @@ void choose_random_location(unsigned long input,/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, *output); +/* +* Low end of the randomization range should be the +* smaller of 512M or the initial kernel image +* location: +*/ + min_addr =min (*output,512UL <<20 ); +/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (*output, output_size); + random_addr =find_random_phys_addr (min_addr, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); }else {
tip-bot for Yinghai Lu
merge bot[tip:x86/boot] x86/KASLR: Allow randomization below the load address
In reply to Kees Cookx86/KASLR: Allow randomization below the load address
Commit-ID: e066cc47776a89bbdaf4184c0e75f7d389f9ab48 Gitweb: http://git.kernel.org/tip/e066cc47776a89bbdaf4184c0e75f7d389f9ab48 Author: Yinghai Lu <[email protected]> AuthorDate: Wed, 25 May 2016 15:45:34 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sun, 26 Jun 2016 12:32:05 +0200
x86/KASLR: Allow randomization below the load address
Currently the kernel image physical address randomization's lower boundary is the original kernel load address.
For bootloaders that load kernels into very high memory (e.g. kexec), this means randomization takes place in a very small window at the top of memory, ignoring the large region of physical memory below the load address.
Since mem_avoid[] is already correctly tracking the regions that must be avoided, this patch changes the minimum address to whatever is less:
512M (to conservatively avoid unknown things in lower memory) or the load address. Now, for example, if the kernel is loaded at 8G, [512M, 8G) will be added to the list of possible physical memory positions.
--- arch/x86/boot/compressed/kaslr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/kaslr.c +9 -2
@@ -492,7 +492,7 @@ void choose_random_location(unsigned long input,unsigned long output_size, unsigned long *virt_addr) { - unsigned long random_addr; + unsigned long random_addr, min_addr;/* By default, keep output position unchanged. */ *virt_addr = *output;
@@ -510,8 +510,15 @@ void choose_random_location(unsigned long input,/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, *output); +/* +* Low end of the randomization range should be the +* smaller of 512M or the initial kernel image +* location: +*/ + min_addr =min (*output,512UL <<20 ); +/* Walk e820 and find a random address. */ - random_addr =find_random_phys_addr (*output, output_size); + random_addr =find_random_phys_addr (min_addr, output_size);if (!random_addr) {warn ("KASLR disabled: could not find suitable E820 region!" ); }else {