x86/KASLR: Randomize virtual address separately
v8 cover x86/KASLR: Randomize virtual address separately
This is v8 of the last 3 patches from v7, with an additional clean-up for the pagetable.c code. The rest of the series has landed in -tip.
The patches are:
- 1: Further clean up on pagetable.c.
- 2: Last part of Baoquan's decoupling the physical address and virtual address randomization of kernel text.
- 3: Remove upper bound on physical address range.
- 4: Remove lower bound on physical address range.
Thanks for putting up with my attempts at this, and for all the review! Things really do look much cleaner. :)
-Kees
v7->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 v8 1/4 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
@@ -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
Re: [PATCH v8 1/4] x86/KASLR: Clarify identity map interface
Quoted 11 lines · Kees Cook
This extracts the call to
prepare_level4()into a top-level function that the user of thepagetable.cinterface must call to initialize the new page tables. For clarity and to match the "finalize" function, it has been renamed toinitialize_identity_maps(). This function also gains the initialization ofmapping_infoso we don't have to do it each time inadd_identity_map().Additionally add copyright notice to the top, to make it clear that the bulk of the
pagetable.ccode was written by Yinghai, and that I just added bugs later. :)
Quoted 6 lines
Signed-off-by Kees Cook <[email protected]>--- 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(-)
Exactly!
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Re: [PATCH v8 1/4] x86/KASLR: Clarify identity map interface
In reply to Kees Cookarch/x86/boot/compressed/pagetable.c static struct alloc_pgt_data pgt_data; · line 78
Quoted 13 lines · Kees Cook
arch/x86/boot/compressed/pagetable.c +11
-0+/* +* 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, +}; +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;
Could you please outline the precise failure mode? What gets executed when, which pointer gets relocated and which not, and exactly when does it pose a problem, etc.
Thanks,
Ingo
Re: [PATCH v8 1/4] x86/KASLR: Clarify identity map interface
Quoted 17 lines · Ingo Molnar
arch/x86/boot/compressed/pagetable.c static struct alloc_pgt_data pgt_data; · line 78
Quoted 13 lines · Kees Cook
arch/x86/boot/compressed/pagetable.c +11
-0+/* +* 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, +}; +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;
Quoted 3 lines
Could you please outline the precise failure mode? What gets executed when, which pointer gets relocated and which not, and exactly when does it pose a problem, etc.
It's the issue described at the top of misc.c:
/*
* WARNING!!
* This code is compiled with -fPIC and it is relocated dynamically at
* run time, but no relocation processing is performed. This means that
* it is not safe to place pointers in static structures.
*/
Should this be repeated in each .c file maybe as a reminder?
If the rest of the patches look good, we could clean this up as a following patch? What do you think?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v8 1/4] x86/KASLR: Clarify identity map interface
Quoted 22 lines · Kees Cook
Quoted 17 lines · Ingo Molnar
arch/x86/boot/compressed/pagetable.c static struct alloc_pgt_data pgt_data; · line 78
Quoted 13 lines · Kees Cook
arch/x86/boot/compressed/pagetable.c +11
-0+/* +* 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, +}; +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;Quoted 3 lines
Could you please outline the precise failure mode? What gets executed when, which pointer gets relocated and which not, and exactly when does it pose a problem, etc.
Quoted 7 lines
It's the issue described at the top of
misc.c:/*
* WARNING!!
* This code is compiled with -fPIC and it is relocated dynamically at
* run time, but no relocation processing is performed. This means that
* it is not safe to place pointers in static structures.
So I think this is still a bit confusing: what's the difference between 'relocated dynamically at run time' and 'relocation processing'?
So 'to relocate code' usually means the whole deal: 'copy code and fix up'.
The problem here is that we copy the code to an address not known at build time, but don't fix it up (because this is the fixup code), right?
Thanks,
Ingo
Re: [PATCH v8 1/4] x86/KASLR: Clarify identity map interface
In reply to Kees CookQuoted 22 lines · Kees Cook
Quoted 17 lines · Ingo Molnar
arch/x86/boot/compressed/pagetable.c static struct alloc_pgt_data pgt_data; · line 78
Quoted 13 lines · Kees Cook
arch/x86/boot/compressed/pagetable.c +11
-0+/* +* 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, +}; +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;Quoted 3 lines
Could you please outline the precise failure mode? What gets executed when, which pointer gets relocated and which not, and exactly when does it pose a problem, etc.
Quoted 8 lines
It's the issue described at the top of
misc.c:/*
* WARNING!!
* This code is compiled with -fPIC and it is relocated dynamically at
* run time, but no relocation processing is performed. This means that
* it is not safe to place pointers in static structures.
*/
Btw., is there any way to detect such invalid pointers during build-time somehow? This is not the first such bug and I suspect it won't be the last one either.
Thanks,
Ingo
PATCH v8 2/4 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
PATCH v8 3/4 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
@@ -1943,21 +1943,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
PATCH v8 4/4 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