aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunjie Mao <eternal.n08@gmail.com>2014-10-31 09:40:38 -0400
committerThomas Gleixner <tglx@linutronix.de>2014-11-01 17:20:50 -0400
commite6023367d779060fddc9a52d1f474085b2b36298 (patch)
treeef35781f63032842e6cf8b3e1869d54f312ce17f
parent4750a0d112cbfcc744929f1530ffe3193436766c (diff)
x86, kaslr: Prevent .bss from overlaping initrd
When choosing a random address, the current implementation does not take into account the reversed space for .bss and .brk sections. Thus the relocated kernel may overlap other components in memory. Here is an example of the overlap from a x86_64 kernel in qemu (the ranges of physical addresses are presented): Physical Address 0x0fe00000 --+--------------------+ <-- randomized base / | relocated kernel | vmlinux.bin | (from vmlinux.bin) | 0x1336d000 (an ELF file) +--------------------+-- \ | | \ 0x1376d870 --+--------------------+ | | relocs table | | 0x13c1c2a8 +--------------------+ .bss and .brk | | | 0x13ce6000 +--------------------+ | | | / 0x13f77000 | initrd |-- | | 0x13fef374 +--------------------+ The initrd image will then be overwritten by the memset during early initialization: [ 1.655204] Unpacking initramfs... [ 1.662831] Initramfs unpacking failed: junk in compressed archive This patch prevents the above situation by requiring a larger space when looking for a random kernel base, so that existing logic can effectively avoids the overlap. [kees: switched to perl to avoid hex translation pain in mawk vs gawk] [kees: calculated overlap without relocs table] Fixes: 82fa9637a2 ("x86, kaslr: Select random position from e820 maps") Reported-by: Fengguang Wu <fengguang.wu@intel.com> Signed-off-by: Junjie Mao <eternal.n08@gmail.com> Signed-off-by: Kees Cook <keescook@chromium.org> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Matt Fleming <matt.fleming@intel.com> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Vivek Goyal <vgoyal@redhat.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: stable@vger.kernel.org Link: http://lkml.kernel.org/r/1414762838-13067-1-git-send-email-eternal.n08@gmail.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
-rw-r--r--arch/x86/boot/compressed/Makefile4
-rw-r--r--arch/x86/boot/compressed/head_32.S5
-rw-r--r--arch/x86/boot/compressed/head_64.S5
-rw-r--r--arch/x86/boot/compressed/misc.c13
-rw-r--r--arch/x86/boot/compressed/mkpiggy.c9
-rw-r--r--arch/x86/tools/calc_run_size.pl30
6 files changed, 57 insertions, 9 deletions
diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index 704f58aa79cd..be1e07d4b596 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -76,8 +76,10 @@ suffix-$(CONFIG_KERNEL_XZ) := xz
76suffix-$(CONFIG_KERNEL_LZO) := lzo 76suffix-$(CONFIG_KERNEL_LZO) := lzo
77suffix-$(CONFIG_KERNEL_LZ4) := lz4 77suffix-$(CONFIG_KERNEL_LZ4) := lz4
78 78
79RUN_SIZE = $(shell objdump -h vmlinux | \
80 perl $(srctree)/arch/x86/tools/calc_run_size.pl)
79quiet_cmd_mkpiggy = MKPIGGY $@ 81quiet_cmd_mkpiggy = MKPIGGY $@
80 cmd_mkpiggy = $(obj)/mkpiggy $< > $@ || ( rm -f $@ ; false ) 82 cmd_mkpiggy = $(obj)/mkpiggy $< $(RUN_SIZE) > $@ || ( rm -f $@ ; false )
81 83
82targets += piggy.S 84targets += piggy.S
83$(obj)/piggy.S: $(obj)/vmlinux.bin.$(suffix-y) $(obj)/mkpiggy FORCE 85$(obj)/piggy.S: $(obj)/vmlinux.bin.$(suffix-y) $(obj)/mkpiggy FORCE
diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S
index cbed1407a5cd..1d7fbbcc196d 100644
--- a/arch/x86/boot/compressed/head_32.S
+++ b/arch/x86/boot/compressed/head_32.S
@@ -207,7 +207,8 @@ relocated:
207 * Do the decompression, and jump to the new kernel.. 207 * Do the decompression, and jump to the new kernel..
208 */ 208 */
209 /* push arguments for decompress_kernel: */ 209 /* push arguments for decompress_kernel: */
210 pushl $z_output_len /* decompressed length */ 210 pushl $z_run_size /* size of kernel with .bss and .brk */
211 pushl $z_output_len /* decompressed length, end of relocs */
211 leal z_extract_offset_negative(%ebx), %ebp 212 leal z_extract_offset_negative(%ebx), %ebp
212 pushl %ebp /* output address */ 213 pushl %ebp /* output address */
213 pushl $z_input_len /* input_len */ 214 pushl $z_input_len /* input_len */
@@ -217,7 +218,7 @@ relocated:
217 pushl %eax /* heap area */ 218 pushl %eax /* heap area */
218 pushl %esi /* real mode pointer */ 219 pushl %esi /* real mode pointer */
219 call decompress_kernel /* returns kernel location in %eax */ 220 call decompress_kernel /* returns kernel location in %eax */
220 addl $24, %esp 221 addl $28, %esp
221 222
222/* 223/*
223 * Jump to the decompressed kernel. 224 * Jump to the decompressed kernel.
diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S
index 2884e0c3e8a5..6b1766c6c082 100644
--- a/arch/x86/boot/compressed/head_64.S
+++ b/arch/x86/boot/compressed/head_64.S
@@ -402,13 +402,16 @@ relocated:
402 * Do the decompression, and jump to the new kernel.. 402 * Do the decompression, and jump to the new kernel..
403 */ 403 */
404 pushq %rsi /* Save the real mode argument */ 404 pushq %rsi /* Save the real mode argument */
405 movq $z_run_size, %r9 /* size of kernel with .bss and .brk */
406 pushq %r9
405 movq %rsi, %rdi /* real mode address */ 407 movq %rsi, %rdi /* real mode address */
406 leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ 408 leaq boot_heap(%rip), %rsi /* malloc area for uncompression */
407 leaq input_data(%rip), %rdx /* input_data */ 409 leaq input_data(%rip), %rdx /* input_data */
408 movl $z_input_len, %ecx /* input_len */ 410 movl $z_input_len, %ecx /* input_len */
409 movq %rbp, %r8 /* output target address */ 411 movq %rbp, %r8 /* output target address */
410 movq $z_output_len, %r9 /* decompressed length */ 412 movq $z_output_len, %r9 /* decompressed length, end of relocs */
411 call decompress_kernel /* returns kernel location in %rax */ 413 call decompress_kernel /* returns kernel location in %rax */
414 popq %r9
412 popq %rsi 415 popq %rsi
413 416
414/* 417/*
diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c
index 57ab74df7eea..30dd59a9f0b4 100644
--- a/arch/x86/boot/compressed/misc.c
+++ b/arch/x86/boot/compressed/misc.c
@@ -358,7 +358,8 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
358 unsigned char *input_data, 358 unsigned char *input_data,
359 unsigned long input_len, 359 unsigned long input_len,
360 unsigned char *output, 360 unsigned char *output,
361 unsigned long output_len) 361 unsigned long output_len,
362 unsigned long run_size)
362{ 363{
363 real_mode = rmode; 364 real_mode = rmode;
364 365
@@ -381,8 +382,14 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
381 free_mem_ptr = heap; /* Heap */ 382 free_mem_ptr = heap; /* Heap */
382 free_mem_end_ptr = heap + BOOT_HEAP_SIZE; 383 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
383 384
384 output = choose_kernel_location(input_data, input_len, 385 /*
385 output, output_len); 386 * The memory hole needed for the kernel is the larger of either
387 * the entire decompressed kernel plus relocation table, or the
388 * entire decompressed kernel plus .bss and .brk sections.
389 */
390 output = choose_kernel_location(input_data, input_len, output,
391 output_len > run_size ? output_len
392 : run_size);
386 393
387 /* Validate memory location choices. */ 394 /* Validate memory location choices. */
388 if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1)) 395 if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
diff --git a/arch/x86/boot/compressed/mkpiggy.c b/arch/x86/boot/compressed/mkpiggy.c
index b669ab65bf6c..d8222f213182 100644
--- a/arch/x86/boot/compressed/mkpiggy.c
+++ b/arch/x86/boot/compressed/mkpiggy.c
@@ -36,11 +36,13 @@ int main(int argc, char *argv[])
36 uint32_t olen; 36 uint32_t olen;
37 long ilen; 37 long ilen;
38 unsigned long offs; 38 unsigned long offs;
39 unsigned long run_size;
39 FILE *f = NULL; 40 FILE *f = NULL;
40 int retval = 1; 41 int retval = 1;
41 42
42 if (argc < 2) { 43 if (argc < 3) {
43 fprintf(stderr, "Usage: %s compressed_file\n", argv[0]); 44 fprintf(stderr, "Usage: %s compressed_file run_size\n",
45 argv[0]);
44 goto bail; 46 goto bail;
45 } 47 }
46 48
@@ -74,6 +76,7 @@ int main(int argc, char *argv[])
74 offs += olen >> 12; /* Add 8 bytes for each 32K block */ 76 offs += olen >> 12; /* Add 8 bytes for each 32K block */
75 offs += 64*1024 + 128; /* Add 64K + 128 bytes slack */ 77 offs += 64*1024 + 128; /* Add 64K + 128 bytes slack */
76 offs = (offs+4095) & ~4095; /* Round to a 4K boundary */ 78 offs = (offs+4095) & ~4095; /* Round to a 4K boundary */
79 run_size = atoi(argv[2]);
77 80
78 printf(".section \".rodata..compressed\",\"a\",@progbits\n"); 81 printf(".section \".rodata..compressed\",\"a\",@progbits\n");
79 printf(".globl z_input_len\n"); 82 printf(".globl z_input_len\n");
@@ -85,6 +88,8 @@ int main(int argc, char *argv[])
85 /* z_extract_offset_negative allows simplification of head_32.S */ 88 /* z_extract_offset_negative allows simplification of head_32.S */
86 printf(".globl z_extract_offset_negative\n"); 89 printf(".globl z_extract_offset_negative\n");
87 printf("z_extract_offset_negative = -0x%lx\n", offs); 90 printf("z_extract_offset_negative = -0x%lx\n", offs);
91 printf(".globl z_run_size\n");
92 printf("z_run_size = %lu\n", run_size);
88 93
89 printf(".globl input_data, input_data_end\n"); 94 printf(".globl input_data, input_data_end\n");
90 printf("input_data:\n"); 95 printf("input_data:\n");
diff --git a/arch/x86/tools/calc_run_size.pl b/arch/x86/tools/calc_run_size.pl
new file mode 100644
index 000000000000..0b0b124d3ece
--- /dev/null
+++ b/arch/x86/tools/calc_run_size.pl
@@ -0,0 +1,30 @@
1#!/usr/bin/perl
2#
3# Calculate the amount of space needed to run the kernel, including room for
4# the .bss and .brk sections.
5#
6# Usage:
7# objdump -h a.out | perl calc_run_size.pl
8use strict;
9
10my $mem_size = 0;
11my $file_offset = 0;
12
13my $sections=" *[0-9]+ \.(?:bss|brk) +";
14while (<>) {
15 if (/^$sections([0-9a-f]+) +(?:[0-9a-f]+ +){2}([0-9a-f]+)/) {
16 my $size = hex($1);
17 my $offset = hex($2);
18 $mem_size += $size;
19 if ($file_offset == 0) {
20 $file_offset = $offset;
21 } elsif ($file_offset != $offset) {
22 die ".bss and .brk lack common file offset\n";
23 }
24 }
25}
26
27if ($file_offset == 0) {
28 die "Never found .bss or .brk file offset\n";
29}
30printf("%d\n", $mem_size + $file_offset);