x86/KASLR: Build identity mappings on demand
cover x86/KASLR: Build identity mappings on demand
I'm going back to small steps. :)
This series lets the x86_64 compressed boot environment build page table identity mappings on demand. This will be used once we begin randomizing the physical memory position beyond the existing identity maps.
-Kees
PATCH 1/3 x86/boot: Clean up indenting for asm/boot.h
Before adding more defines to asm/boot.h, this cleans up the existing indenting for readability.
--- arch/x86/include/asm/boot.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
arch/x86/include/asm/boot.h +9 -11
@@ -12,29 +12,27 @@/* Minimum kernel alignment, as a power of two */ #ifdef CONFIG_X86_64 -#define MIN_KERNEL_ALIGN_LG2 PMD_SHIFT +# define MIN_KERNEL_ALIGN_LG2 PMD_SHIFT#else -#define MIN_KERNEL_ALIGN_LG2 (PAGE_SHIFT + THREAD_SIZE_ORDER) +# define MIN_KERNEL_ALIGN_LG2 (PAGE_SHIFT + THREAD_SIZE_ORDER)#endif #define MIN_KERNEL_ALIGN (_AC(1, UL) << MIN_KERNEL_ALIGN_LG2)#if (CONFIG_PHYSICAL_ALIGN & (CONFIG_PHYSICAL_ALIGN -1 )) || \ (CONFIG_PHYSICAL_ALIGN <MIN_KERNEL_ALIGN ) -#error "Invalid value for CONFIG_PHYSICAL_ALIGN" +# error "Invalid value for CONFIG_PHYSICAL_ALIGN"#endif #ifdef CONFIG_KERNEL_BZIP2 -#define BOOT_HEAP_SIZE 0x400000 +# define BOOT_HEAP_SIZE 0x400000#else /* !CONFIG_KERNEL_BZIP2 */ - -#define BOOT_HEAP_SIZE 0x10000 - -#endif /* !CONFIG_KERNEL_BZIP2 */ +# define BOOT_HEAP_SIZE 0x10000 +#endif #ifdef CONFIG_X86_64 -#define BOOT_STACK_SIZE 0x4000 -#else -#define BOOT_STACK_SIZE 0x1000 +# define BOOT_STACK_SIZE 0x4000 +#else /* !CONFIG_X86_64 */ +# define BOOT_STACK_SIZE 0x1000#endif #endif /* _ASM_X86_BOOT_H */
Signature · 1 line
2.6.3
PATCH 2/3 x86/boot: Split out kernel_ident_mapping_init
In reply to Kees CookFrom: Yinghai Lu <[email protected]>
In order to support on-demand page table creation when moving the kernel for KASLR, we need to use kernel_ident_mapping_init() in the decompression code. This splits it out into its own file for use outside of init_64.c. Additionally, checking for __pa/__va defines is added since they need to be overridden in the decompression code.
--- arch/x86/include/asm/page.h | 5 +++ arch/x86/mm/ident_map.c | 74 +++++++++++++++++++++++++++++++++++++++++++++ arch/x86/mm/init_64.c | 74 +-------------------------------------------- 3 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 arch/x86/mm/ident_map.c
arch/x86/include/asm/page.h +5 -0
@@ -37,7 +37,10 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr, alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)#define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE +#ifndef __pa#define __pa (x) __phys_addr((unsigned long)(x)) +#endif +#define __pa_nodebug (x) __phys_addr_nodebug((unsigned long)(x))/* __pa_symbol should be used for C visible symbols. This seems to be the official gcc blessed way to do such arithmetic. */
@@ -51,7 +54,9 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,#define __pa_symbol (x) \ __phys_addr_symbol(__phys_reloc_hide((unsigned long)(x))) +#ifndef __va#define __va (x) ((void *)((unsigned long)(x)+PAGE_OFFSET)) +#endif #define __boot_va (x) __va(x)#define __boot_pa (x) __pa(x)
arch/x86/mm/ident_map.c new file +74 -0
74 lines not on this page · raw message
arch/x86/mm/init_64.c +1 -73
80 lines not on this page · raw message
Signature · 1 line
2.6.3
PATCH 3/3 x86/KASLR: Build identity mappings on demand
In reply to Kees CookCurrently KASLR only supports relocation in a small physical range (from 16M to 1G), due to using the initial kernel page table identity mapping. To support ranges above this, we need to have an identity mapping for the desired memory range before we can decompress (and later run) the kernel.
32-bit kernels already have the needed identity mapping. This patch adds identity mappings for the needed memory ranges on 64-bit kernels. This happens in two possible boot paths:
If loaded via startup_32(), we need to set up the needed identity map.
If loaded from a 64-bit bootloader, the bootloader will have already set up an identity mapping, and we'll start via the compressed kernel's startup_64(). In this case, the bootloader's page tables need to be avoided while selecting the new uncompressed kernel location. If not, the decompressor could overwrite them during decompression.
To accomplish this, we could walk the pagetable and find every page that is used, and add them to mem_avoid, but this needs extra code and will require increasing the size of the mem_avoid array.
Instead, we can create a new set of page tables for our own identity mapping instead. The pages for the new page table will come from the _pagetable section of the compressed kernel, which means they are already contained by in mem_avoid array. To do this, we reuse the code from the uncompressed kernel's identity mapping routines.
The _pgtable will be shared by both the 32-bit and 64-bit paths to reduce init_size, as now the compressed kernel's _rodata to _end will contribute to init_size.
To handle the possible mappings, we need to increase the existing page table buffer size:
When booting via startup_64(), we need to cover the old VO, params, cmdline and uncompressed kernel. In an extreme case we could have them all beyond the 512G boundary, which needs (2+2)*4 pages with 2M mappings. And we'll need 2 for first 2M for VGA RAM. One more is needed for level4. This gets us to 19 pages total.
When booting via startup_32(), KASLR could move the uncompressed kernel above 4G, so we need to create extra identity mappings, which should only need (2+2) pages at most when it is beyond the 512G boundary. So 19 pages is sufficient for this case as well.
The resulting BOOT_*PGT_SIZE defines use the "_SIZE" suffix on their names to maintain logical consistency with the existing BOOT_HEAP_SIZE and BOOT_STACK_SIZE defines.
This patch is based on earlier patches from Yinghai Lu and Baoquan He.
--- arch/x86/boot/compressed/Makefile | 3 + arch/x86/boot/compressed/head_64.S | 4 +- arch/x86/boot/compressed/kaslr.c | 17 +++++ arch/x86/boot/compressed/misc.h | 11 +++ arch/x86/boot/compressed/pagetable.c | 135 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/boot.h | 19 +++++ 6 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 arch/x86/boot/compressed/pagetable.c
arch/x86/boot/compressed/Makefile +3 -0
@@ -75,6 +75,9 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \ vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o +ifdef CONFIG_X86_64 + vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/pagetable.o +endif $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
arch/x86/boot/compressed/head_64.S +2 -2
@@ -134,7 +134,7 @@ ENTRY(startup_32) /* Initialize Page tables to 0 */ leal pgtable(%ebx), %edi xorl %eax, %eax - movl $((4096*6)/4), %ecx + movl $(BOOT_INIT_PGT_SIZE/4), %ecx rep stosl /* Build Level 4 */
@@ -486,4 +486,4 @@ boot_stack_end: .section ".pgtable","a",@nobits .balign 4096 pgtable: - .fill 6*4096, 1, 0 + .fill BOOT_PGT_SIZE, 1, 0
arch/x86/boot/compressed/kaslr.c +17 -0
@@ -241,6 +241,8 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,*/ mem_avoid[MEM_AVOID_ZO_RANGE ].start = input; mem_avoid[MEM_AVOID_ZO_RANGE ].size = (output + init_size) - input; +add_identity_map (mem_avoid[MEM_AVOID_ZO_RANGE ].start, + mem_avoid[MEM_AVOID_ZO_RANGE ].size);/* Avoid initrd. */ initrd_start = (u64 )boot_params->ext_ramdisk_image <<32 ;
@@ -249,6 +251,7 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, initrd_size |= boot_params->hdr.ramdisk_size; mem_avoid[MEM_AVOID_INITRD ].start = initrd_start; mem_avoid[MEM_AVOID_INITRD ].size = initrd_size; +/* No need to set mapping for initrd, it will be handled in VO. */ /* Avoid kernel command line. */ cmd_line = (u64 )boot_params->ext_cmd_line_ptr <<32 ;
@@ -259,10 +262,21 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, ; mem_avoid[MEM_AVOID_CMDLINE ].start = cmd_line; mem_avoid[MEM_AVOID_CMDLINE ].size = cmd_line_size; +add_identity_map (mem_avoid[MEM_AVOID_CMDLINE ].start, + mem_avoid[MEM_AVOID_CMDLINE ].size);/* Avoid boot parameters. */ mem_avoid[MEM_AVOID_BOOTPARAMS ].start = (unsigned long )boot_params; mem_avoid[MEM_AVOID_BOOTPARAMS ].size =sizeof (*boot_params); +add_identity_map (mem_avoid[MEM_AVOID_BOOTPARAMS ].start, + mem_avoid[MEM_AVOID_BOOTPARAMS ].size); + +/* We don't need to set a mapping for setup_data. */ + +#ifdef CONFIG_X86_VERBOSE_BOOTUP +/* Make sure video RAM can be used. */ +add_identity_map (0 ,PMD_SIZE ); +#endif }/* Does this memory vector overlap a known avoided area? */
@@ -421,6 +435,9 @@ unsigned char *choose_random_location(unsigned long input, goto out; choice = random_addr; + +add_identity_map (choice, output_size); +finalize_identity_maps (); out:return (unsigned char *)choice; }
arch/x86/boot/compressed/misc.h +11 -0
@@ -84,6 +84,17 @@ unsigned char *choose_random_location(unsigned long input, }#endif +#ifdef CONFIG_X86_64 +void add_identity_map (unsigned long start,unsigned long size); +void finalize_identity_maps (void ); +extern unsigned char _pgtable[]; +#else +static inline void add_identity_map (unsigned long start,unsigned long size) +{ } +static inline void finalize_identity_maps (void ) +{ } +#endif +#ifdef CONFIG_EARLY_PRINTK /* early_serial_console.c */ extern int early_serial_base;
arch/x86/boot/compressed/pagetable.c new file +135 -0
135 lines not on this page · raw message
arch/x86/include/asm/boot.h +19 -0
@@ -31,6 +31,25 @@#ifdef CONFIG_X86_64 # define BOOT_STACK_SIZE 0x4000 + +# define BOOT_INIT_PGT_SIZE (6*4096) +# ifdef CONFIG_RANDOMIZE_BASE +/* +* Assuming all cross the 512GB boundary: +* 1 page for level4 +* (2+2)*4 pages for kernel, param, cmd_line, and randomized kernel +* 2 pages for first 2M (video RAM: CONFIG_X86_VERBOSE_BOOTUP). +* Total is 19 pages. +*/ +# ifdef CONFIG_X86_VERBOSE_BOOTUP +# define BOOT_PGT_SIZE (19*4096) +# else /* !CONFIG_X86_VERBOSE_BOOTUP */ +# define BOOT_PGT_SIZE (17*4096) +# endif +# else /* !CONFIG_RANDOMIZE_BASE */ +# define BOOT_PGT_SIZE BOOT_INIT_PGT_SIZE +# endif +#else /* !CONFIG_X86_64 */ # define BOOT_STACK_SIZE 0x1000#endif
Signature · 1 line
2.6.3
Quoted 6 lines · Kees Cook
I'm going back to small steps. :)
This series lets the
x86_64compressed boot environment build page table identity mappings on demand. This will be used once we begin randomizing the physical memory position beyond the existing identity maps.
This series was much better, thanks! I've applied them to tip:x86/boot and will push them out if they pass testing.
I also applied Boris's type casting cleanups and your comments improval patch:
ed09acde44e3 x86/KASLR: Improve comments around the mem_avoid[] logic
Thanks,
Ingo
tip-bot for Kees Cook
merge bot[tip:x86/boot] x86/boot: Clean up indenting for asm/boot.h
In reply to Kees Cookx86/boot: Clean up indenting for asm/boot.h
Commit-ID: 8665e6ff2107204f981ba8f9ee37085a003fc9e9 Gitweb: http://git.kernel.org/tip/8665e6ff2107204f981ba8f9ee37085a003fc9e9 Author: Kees Cook <[email protected]> AuthorDate: Fri, 6 May 2016 15:01:33 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 7 May 2016 07:38:39 +0200
x86/boot: Clean up indenting for asm/boot.h
Before adding more defines to asm/boot.h, this cleans up the existing indenting for readability.
--- arch/x86/include/asm/boot.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
arch/x86/include/asm/boot.h +9 -11
@@ -12,29 +12,27 @@/* Minimum kernel alignment, as a power of two */ #ifdef CONFIG_X86_64 -#define MIN_KERNEL_ALIGN_LG2 PMD_SHIFT +# define MIN_KERNEL_ALIGN_LG2 PMD_SHIFT#else -#define MIN_KERNEL_ALIGN_LG2 (PAGE_SHIFT + THREAD_SIZE_ORDER) +# define MIN_KERNEL_ALIGN_LG2 (PAGE_SHIFT + THREAD_SIZE_ORDER)#endif #define MIN_KERNEL_ALIGN (_AC(1, UL) << MIN_KERNEL_ALIGN_LG2)#if (CONFIG_PHYSICAL_ALIGN & (CONFIG_PHYSICAL_ALIGN -1 )) || \ (CONFIG_PHYSICAL_ALIGN <MIN_KERNEL_ALIGN ) -#error "Invalid value for CONFIG_PHYSICAL_ALIGN" +# error "Invalid value for CONFIG_PHYSICAL_ALIGN"#endif #ifdef CONFIG_KERNEL_BZIP2 -#define BOOT_HEAP_SIZE 0x400000 +# define BOOT_HEAP_SIZE 0x400000#else /* !CONFIG_KERNEL_BZIP2 */ - -#define BOOT_HEAP_SIZE 0x10000 - -#endif /* !CONFIG_KERNEL_BZIP2 */ +# define BOOT_HEAP_SIZE 0x10000 +#endif #ifdef CONFIG_X86_64 -#define BOOT_STACK_SIZE 0x4000 -#else -#define BOOT_STACK_SIZE 0x1000 +# define BOOT_STACK_SIZE 0x4000 +#else /* !CONFIG_X86_64 */ +# define BOOT_STACK_SIZE 0x1000#endif #endif /* _ASM_X86_BOOT_H */
tip-bot for Yinghai Lu
merge bot[tip:x86/boot] x86/boot: Split out kernel_ident_mapping_init()
In reply to Kees Cookx86/boot: Split out kernel_ident_mapping_init()
Commit-ID: cf4fb15b3110df070fe9829a1ef38fef8316fb90 Gitweb: http://git.kernel.org/tip/cf4fb15b3110df070fe9829a1ef38fef8316fb90 Author: Yinghai Lu <[email protected]> AuthorDate: Fri, 6 May 2016 15:01:34 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 7 May 2016 07:38:39 +0200
x86/boot: Split out kernel_ident_mapping_init()
In order to support on-demand page table creation when moving the kernel for KASLR, we need to use kernel_ident_mapping_init() in the decompression code.
This splits it out into its own file for use outside of init_64.c. Additionally, checking for __pa/__va defines is added since they need to be overridden in the decompression code.
--- arch/x86/include/asm/page.h | 5 +++ arch/x86/mm/ident_map.c | 74 +++++++++++++++++++++++++++++++++++++++++++++ arch/x86/mm/init_64.c | 74 +-------------------------------------------- 3 files changed, 80 insertions(+), 73 deletions(-)
arch/x86/include/asm/page.h +5 -0
@@ -37,7 +37,10 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr, alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)#define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE +#ifndef __pa#define __pa (x) __phys_addr((unsigned long)(x)) +#endif +#define __pa_nodebug (x) __phys_addr_nodebug((unsigned long)(x))/* __pa_symbol should be used for C visible symbols. This seems to be the official gcc blessed way to do such arithmetic. */
@@ -51,7 +54,9 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,#define __pa_symbol (x) \ __phys_addr_symbol(__phys_reloc_hide((unsigned long)(x))) +#ifndef __va#define __va (x) ((void *)((unsigned long)(x)+PAGE_OFFSET)) +#endif #define __boot_va (x) __va(x)#define __boot_pa (x) __pa(x)
arch/x86/mm/ident_map.c new file +74 -0
74 lines not on this page · raw message
arch/x86/mm/init_64.c +1 -73
80 lines not on this page · raw message
tip-bot for Kees Cook
merge botx86/KASLR: Build identity mappings on demand
Commit-ID: 3a94707d7a7bb1eb82acae5fbc035247dd1ba8a5 Gitweb: http://git.kernel.org/tip/3a94707d7a7bb1eb82acae5fbc035247dd1ba8a5 Author: Kees Cook <[email protected]> AuthorDate: Fri, 6 May 2016 15:01:35 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 7 May 2016 07:38:39 +0200
x86/KASLR: Build identity mappings on demand
Currently KASLR only supports relocation in a small physical range (from 16M to 1G), due to using the initial kernel page table identity mapping. To support ranges above this, we need to have an identity mapping for the desired memory range before we can decompress (and later run) the kernel.
32-bit kernels already have the needed identity mapping. This patch adds identity mappings for the needed memory ranges on 64-bit kernels. This happens in two possible boot paths:
If loaded via startup_32(), we need to set up the needed identity map.
If loaded from a 64-bit bootloader, the bootloader will have already set up an identity mapping, and we'll start via the compressed kernel's startup_64(). In this case, the bootloader's page tables need to be avoided while selecting the new uncompressed kernel location. If not, the decompressor could overwrite them during decompression.
To accomplish this, we could walk the pagetable and find every page that is used, and add them to mem_avoid, but this needs extra code and will require increasing the size of the mem_avoid array.
Instead, we can create a new set of page tables for our own identity mapping instead. The pages for the new page table will come from the _pagetable section of the compressed kernel, which means they are already contained by in mem_avoid array. To do this, we reuse the code from the uncompressed kernel's identity mapping routines.
The _pgtable will be shared by both the 32-bit and 64-bit paths to reduce init_size, as now the compressed kernel's _rodata to _end will contribute to init_size.
To handle the possible mappings, we need to increase the existing page table buffer size:
When booting via startup_64(), we need to cover the old VO, params, cmdline and uncompressed kernel. In an extreme case we could have them all beyond the 512G boundary, which needs (2+2)*4 pages with 2M mappings. And we'll need 2 for first 2M for VGA RAM. One more is needed for level4. This gets us to 19 pages total.
When booting via startup_32(), KASLR could move the uncompressed kernel above 4G, so we need to create extra identity mappings, which should only need (2+2) pages at most when it is beyond the 512G boundary. So 19 pages is sufficient for this case as well.
The resulting BOOT_*PGT_SIZE defines use the "_SIZE" suffix on their names to maintain logical consistency with the existing BOOT_HEAP_SIZE and BOOT_STACK_SIZE defines.
This patch is based on earlier patches from Yinghai Lu and Baoquan He.
--- arch/x86/boot/compressed/Makefile | 3 + arch/x86/boot/compressed/head_64.S | 4 +- arch/x86/boot/compressed/kaslr.c | 17 +++++ arch/x86/boot/compressed/misc.h | 11 +++ arch/x86/boot/compressed/pagetable.c | 135 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/boot.h | 19 +++++ 6 files changed, 187 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/Makefile +3 -0
@@ -75,6 +75,9 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \ vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o +ifdef CONFIG_X86_64 + vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/pagetable.o +endif $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
arch/x86/boot/compressed/head_64.S +2 -2
@@ -134,7 +134,7 @@ ENTRY(startup_32) /* Initialize Page tables to 0 */ leal pgtable(%ebx), %edi xorl %eax, %eax - movl $((4096*6)/4), %ecx + movl $(BOOT_INIT_PGT_SIZE/4), %ecx rep stosl /* Build Level 4 */
@@ -486,4 +486,4 @@ boot_stack_end: .section ".pgtable","a",@nobits .balign 4096 pgtable: - .fill 6*4096, 1, 0 + .fill BOOT_PGT_SIZE, 1, 0
arch/x86/boot/compressed/kaslr.c +17 -0
@@ -241,6 +241,8 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,*/ mem_avoid[MEM_AVOID_ZO_RANGE ].start = input; mem_avoid[MEM_AVOID_ZO_RANGE ].size = (output + init_size) - input; +add_identity_map (mem_avoid[MEM_AVOID_ZO_RANGE ].start, + mem_avoid[MEM_AVOID_ZO_RANGE ].size);/* Avoid initrd. */ initrd_start = (u64 )boot_params->ext_ramdisk_image <<32 ;
@@ -249,6 +251,7 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, initrd_size |= boot_params->hdr.ramdisk_size; mem_avoid[MEM_AVOID_INITRD ].start = initrd_start; mem_avoid[MEM_AVOID_INITRD ].size = initrd_size; +/* No need to set mapping for initrd, it will be handled in VO. */ /* Avoid kernel command line. */ cmd_line = (u64 )boot_params->ext_cmd_line_ptr <<32 ;
@@ -259,10 +262,21 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, ; mem_avoid[MEM_AVOID_CMDLINE ].start = cmd_line; mem_avoid[MEM_AVOID_CMDLINE ].size = cmd_line_size; +add_identity_map (mem_avoid[MEM_AVOID_CMDLINE ].start, + mem_avoid[MEM_AVOID_CMDLINE ].size);/* Avoid boot parameters. */ mem_avoid[MEM_AVOID_BOOTPARAMS ].start = (unsigned long )boot_params; mem_avoid[MEM_AVOID_BOOTPARAMS ].size =sizeof (*boot_params); +add_identity_map (mem_avoid[MEM_AVOID_BOOTPARAMS ].start, + mem_avoid[MEM_AVOID_BOOTPARAMS ].size); + +/* We don't need to set a mapping for setup_data. */ + +#ifdef CONFIG_X86_VERBOSE_BOOTUP +/* Make sure video RAM can be used. */ +add_identity_map (0 ,PMD_SIZE ); +#endif }/* Does this memory vector overlap a known avoided area? */
@@ -421,6 +435,9 @@ unsigned char *choose_random_location(unsigned long input, goto out; choice = random_addr; + +add_identity_map (choice, output_size); +finalize_identity_maps (); out:return (unsigned char *)choice; }
arch/x86/boot/compressed/misc.h +11 -0
@@ -84,6 +84,17 @@ unsigned char *choose_random_location(unsigned long input_ptr, }#endif +#ifdef CONFIG_X86_64 +void add_identity_map (unsigned long start,unsigned long size); +void finalize_identity_maps (void ); +extern unsigned char _pgtable[]; +#else +static inline void add_identity_map (unsigned long start,unsigned long size) +{ } +static inline void finalize_identity_maps (void ) +{ } +#endif +#ifdef CONFIG_EARLY_PRINTK /* early_serial_console.c */ extern int early_serial_base;
arch/x86/boot/compressed/pagetable.c new file +135 -0
135 lines not on this page · raw message
arch/x86/include/asm/boot.h +19 -0
@@ -31,6 +31,25 @@#ifdef CONFIG_X86_64 # define BOOT_STACK_SIZE 0x4000 + +# define BOOT_INIT_PGT_SIZE (6*4096) +# ifdef CONFIG_RANDOMIZE_BASE +/* +* Assuming all cross the 512GB boundary: +* 1 page for level4 +* (2+2)*4 pages for kernel, param, cmd_line, and randomized kernel +* 2 pages for first 2M (video RAM: CONFIG_X86_VERBOSE_BOOTUP). +* Total is 19 pages. +*/ +# ifdef CONFIG_X86_VERBOSE_BOOTUP +# define BOOT_PGT_SIZE (19*4096) +# else /* !CONFIG_X86_VERBOSE_BOOTUP */ +# define BOOT_PGT_SIZE (17*4096) +# endif +# else /* !CONFIG_RANDOMIZE_BASE */ +# define BOOT_PGT_SIZE BOOT_INIT_PGT_SIZE +# endif +#else /* !CONFIG_X86_64 */ # define BOOT_STACK_SIZE 0x1000#endif
Borislav Petkov
maintainerQuoted 13 lines · tip-bot for Kees Cook
Commit-ID: 3a94707d7a7bb1eb82acae5fbc035247dd1ba8a5 Gitweb: http://git.kernel.org/tip/3a94707d7a7bb1eb82acae5fbc035247dd1ba8a5 Author: Kees Cook <[email protected]> AuthorDate: Fri, 6 May 2016 15:01:35 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 7 May 2016 07:38:39 +0200x86/KASLR: Build identity mappings on demand
Currently KASLR only supports relocation in a small physical range (from 16M to 1G), due to using the initial kernel page table identity mapping. To support ranges above this, we need to have an identity mapping for the desired memory range before we can decompress (and later run) the kernel.
...
Quoted 44 lines
arch/x86/boot/compressed/kaslr.c +14
-0@@ -241,6 +241,8 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,*/ mem_avoid[MEM_AVOID_ZO_RANGE ].start = input; mem_avoid[MEM_AVOID_ZO_RANGE ].size = (output + init_size) - input; +add_identity_map (mem_avoid[MEM_AVOID_ZO_RANGE ].start, + mem_avoid[MEM_AVOID_ZO_RANGE ].size);/* Avoid initrd. */ initrd_start = (u64 )boot_params->ext_ramdisk_image <<32 ;@@ -249,6 +251,7 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, initrd_size |= boot_params->hdr.ramdisk_size; mem_avoid[MEM_AVOID_INITRD ].start = initrd_start; mem_avoid[MEM_AVOID_INITRD ].size = initrd_size; +/* No need to set mapping for initrd, it will be handled in VO. */ /* Avoid kernel command line. */ cmd_line = (u64 )boot_params->ext_cmd_line_ptr <<32 ;@@ -259,10 +262,21 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, ; mem_avoid[MEM_AVOID_CMDLINE ].start = cmd_line; mem_avoid[MEM_AVOID_CMDLINE ].size = cmd_line_size; +add_identity_map (mem_avoid[MEM_AVOID_CMDLINE ].start, + mem_avoid[MEM_AVOID_CMDLINE ].size);/* Avoid boot parameters. */ mem_avoid[MEM_AVOID_BOOTPARAMS ].start = (unsigned long )boot_params; mem_avoid[MEM_AVOID_BOOTPARAMS ].size =sizeof (*boot_params); +add_identity_map (mem_avoid[MEM_AVOID_BOOTPARAMS ].start, + mem_avoid[MEM_AVOID_BOOTPARAMS ].size); + +/* We don't need to set a mapping for setup_data. */ + +#ifdef CONFIG_X86_VERBOSE_BOOTUP +/* Make sure video RAM can be used. */ +add_identity_map (0 ,PMD_SIZE ); +#endif }/* Does this memory vector overlap a known avoided area? */
arch/x86/boot/compressed/kaslr.c unsigned char *choose_random_location(unsigned long input, · line 440
Quoted 6 lines
arch/x86/boot/compressed/kaslr.c +3
-0@@ -421,6 +435,9 @@ unsigned char *choose_random_location(unsigned long input, goto out; choice = random_addr; + +add_identity_map (choice, output_size); +finalize_identity_maps ();
Looks ok except that finalize_identity_maps()'s name is kinda not really telling me that we're writing CR3 inside and we're thus switching to the new pagetable. I thought about maybe having something like
write_cr3(get_identity_map());
to make it really explicit at the callsite that we're loading the pagetable but you have all that scheme of stubbed functions on 32-bit and functions doing something on 64-bit.
So how about at least commenting it?
---
From: Borislav Petkov <[email protected]>
Date: Sat, 7 May 2016 11:59:40 +0200
Subject: [PATCH] x86/boot: Comment what finalize_identity_maps() does
So it is not really obvious that finalize_identity_maps() doesn't do any finalization but it *actually* writes CR3 with the ident PGD. Comment that at the call site.
--- arch/x86/boot/compressed/kaslr.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/boot/compressed/kaslr.c +2 -0
@@ -437,6 +437,8 @@ unsigned char *choose_random_location(unsigned long input, 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;
Signature · 7 lines
2.7.3
--
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.tip-bot for Borislav Petkov
merge bot[tip:x86/boot] x86/boot: Comment what finalize_identity_maps() does
x86/boot: Comment what finalize_identity_maps() does
Commit-ID: 36a39ac967a548154a0fe44d71cb0063fa05010f Gitweb: http://git.kernel.org/tip/36a39ac967a548154a0fe44d71cb0063fa05010f Author: Borislav Petkov <[email protected]> AuthorDate: Sat, 7 May 2016 11:59:40 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 10 May 2016 10:12:02 +0200
x86/boot: Comment what finalize_identity_maps() does
So it is not really obvious that finalize_identity_maps() doesn't do any finalization but it *actually* writes CR3 with the ident PGD. Comment that at the call site.
--- arch/x86/boot/compressed/kaslr.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/boot/compressed/kaslr.c +2 -0
@@ -437,6 +437,8 @@ unsigned char *choose_random_location(unsigned long input, 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;