Skip to content

Commit 8391c73

Browse files
Baoquan HeIngo Molnar
authored andcommitted
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. [kees: rewrote changelog, limited virtual split to 64-bit only, update checks] [kees: fix CONFIG_RANDOMIZE_BASE=n boot failure] Signed-off-by: Baoquan He <bhe@redhat.com> Signed-off-by: Kees Cook <keescook@chromium.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: H.J. Lu <hjl.tools@gmail.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Yinghai Lu <yinghai@kernel.org> Link: http://lkml.kernel.org/r/1464216334-17200-4-git-send-email-keescook@chromium.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 11fdf97 commit 8391c73

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed

arch/x86/boot/compressed/kaslr.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -463,17 +463,20 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
463463
* Since this function examines addresses much more numerically,
464464
* it takes the input and output pointers as 'unsigned long'.
465465
*/
466-
unsigned char *choose_random_location(unsigned long input,
467-
unsigned long input_size,
468-
unsigned long output,
469-
unsigned long output_size)
466+
void choose_random_location(unsigned long input,
467+
unsigned long input_size,
468+
unsigned long *output,
469+
unsigned long output_size,
470+
unsigned long *virt_addr)
470471
{
471-
unsigned long choice = output;
472472
unsigned long random_addr;
473473

474+
/* By default, keep output position unchanged. */
475+
*virt_addr = *output;
476+
474477
if (cmdline_find_option_bool("nokaslr")) {
475478
warn("KASLR disabled: 'nokaslr' on cmdline.");
476-
goto out;
479+
return;
477480
}
478481

479482
boot_params->hdr.loadflags |= KASLR_FLAG;
@@ -482,25 +485,25 @@ unsigned char *choose_random_location(unsigned long input,
482485
initialize_identity_maps();
483486

484487
/* Record the various known unsafe memory ranges. */
485-
mem_avoid_init(input, input_size, output);
488+
mem_avoid_init(input, input_size, *output);
486489

487490
/* Walk e820 and find a random address. */
488-
random_addr = find_random_phys_addr(output, output_size);
491+
random_addr = find_random_phys_addr(*output, output_size);
489492
if (!random_addr) {
490493
warn("KASLR disabled: could not find suitable E820 region!");
491-
goto out;
494+
} else {
495+
/* Update the new physical address location. */
496+
if (*output != random_addr) {
497+
add_identity_map(random_addr, output_size);
498+
*output = random_addr;
499+
}
492500
}
493501

494-
/* Always enforce the minimum. */
495-
if (random_addr < choice)
496-
goto out;
497-
498-
choice = random_addr;
499-
500-
add_identity_map(choice, output_size);
501-
502502
/* This actually loads the identity pagetable on x86_64. */
503503
finalize_identity_maps();
504-
out:
505-
return (unsigned char *)choice;
504+
505+
/* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
506+
if (IS_ENABLED(CONFIG_X86_64))
507+
random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size);
508+
*virt_addr = random_addr;
506509
}

arch/x86/boot/compressed/misc.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ void __puthex(unsigned long value)
170170
}
171171

172172
#if CONFIG_X86_NEED_RELOCS
173-
static void handle_relocations(void *output, unsigned long output_len)
173+
static void handle_relocations(void *output, unsigned long output_len,
174+
unsigned long virt_addr)
174175
{
175176
int *reloc;
176177
unsigned long delta, map, ptr;
@@ -182,11 +183,6 @@ static void handle_relocations(void *output, unsigned long output_len)
182183
* and where it was actually loaded.
183184
*/
184185
delta = min_addr - LOAD_PHYSICAL_ADDR;
185-
if (!delta) {
186-
debug_putstr("No relocation needed... ");
187-
return;
188-
}
189-
debug_putstr("Performing relocations... ");
190186

191187
/*
192188
* The kernel contains a table of relocation addresses. Those
@@ -197,6 +193,20 @@ static void handle_relocations(void *output, unsigned long output_len)
197193
*/
198194
map = delta - __START_KERNEL_map;
199195

196+
/*
197+
* 32-bit always performs relocations. 64-bit relocations are only
198+
* needed if KASLR has chosen a different starting address offset
199+
* from __START_KERNEL_map.
200+
*/
201+
if (IS_ENABLED(CONFIG_X86_64))
202+
delta = virt_addr - LOAD_PHYSICAL_ADDR;
203+
204+
if (!delta) {
205+
debug_putstr("No relocation needed... ");
206+
return;
207+
}
208+
debug_putstr("Performing relocations... ");
209+
200210
/*
201211
* Process relocations: 32 bit relocations first then 64 bit after.
202212
* Three sets of binary relocations are added to the end of the kernel
@@ -250,7 +260,8 @@ static void handle_relocations(void *output, unsigned long output_len)
250260
#endif
251261
}
252262
#else
253-
static inline void handle_relocations(void *output, unsigned long output_len)
263+
static inline void handle_relocations(void *output, unsigned long output_len,
264+
unsigned long virt_addr)
254265
{ }
255266
#endif
256267

@@ -327,7 +338,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
327338
unsigned long output_len)
328339
{
329340
const unsigned long kernel_total_size = VO__end - VO__text;
330-
unsigned char *output_orig = output;
341+
unsigned long virt_addr = (unsigned long)output;
331342

332343
/* Retain x86 boot parameters pointer passed from startup_32/64. */
333344
boot_params = rmode;
@@ -366,13 +377,16 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
366377
* the entire decompressed kernel plus relocation table, or the
367378
* entire decompressed kernel plus .bss and .brk sections.
368379
*/
369-
output = choose_random_location((unsigned long)input_data, input_len,
370-
(unsigned long)output,
371-
max(output_len, kernel_total_size));
380+
choose_random_location((unsigned long)input_data, input_len,
381+
(unsigned long *)&output,
382+
max(output_len, kernel_total_size),
383+
&virt_addr);
372384

373385
/* Validate memory location choices. */
374386
if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
375-
error("Destination address inappropriately aligned");
387+
error("Destination physical address inappropriately aligned");
388+
if (virt_addr & (MIN_KERNEL_ALIGN - 1))
389+
error("Destination virtual address inappropriately aligned");
376390
#ifdef CONFIG_X86_64
377391
if (heap > 0x3fffffffffffUL)
378392
error("Destination address too large");
@@ -382,19 +396,16 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
382396
#endif
383397
#ifndef CONFIG_RELOCATABLE
384398
if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
385-
error("Wrong destination address");
399+
error("Destination address does not match LOAD_PHYSICAL_ADDR");
400+
if ((unsigned long)output != virt_addr)
401+
error("Destination virtual address changed when not relocatable");
386402
#endif
387403

388404
debug_putstr("\nDecompressing Linux... ");
389405
__decompress(input_data, input_len, NULL, NULL, output, output_len,
390406
NULL, error);
391407
parse_elf(output);
392-
/*
393-
* 32-bit always performs relocations. 64-bit relocations are only
394-
* needed if kASLR has chosen a different load address.
395-
*/
396-
if (!IS_ENABLED(CONFIG_X86_64) || output != output_orig)
397-
handle_relocations(output, output_len);
408+
handle_relocations(output, output_len, virt_addr);
398409
debug_putstr("done.\nBooting the kernel.\n");
399410
return output;
400411
}

arch/x86/boot/compressed/misc.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,22 @@ int cmdline_find_option_bool(const char *option);
6767

6868
#if CONFIG_RANDOMIZE_BASE
6969
/* kaslr.c */
70-
unsigned char *choose_random_location(unsigned long input_ptr,
71-
unsigned long input_size,
72-
unsigned long output_ptr,
73-
unsigned long output_size);
70+
void choose_random_location(unsigned long input,
71+
unsigned long input_size,
72+
unsigned long *output,
73+
unsigned long output_size,
74+
unsigned long *virt_addr);
7475
/* cpuflags.c */
7576
bool has_cpuflag(int flag);
7677
#else
77-
static inline
78-
unsigned char *choose_random_location(unsigned long input_ptr,
79-
unsigned long input_size,
80-
unsigned long output_ptr,
81-
unsigned long output_size)
78+
static inline void choose_random_location(unsigned long input,
79+
unsigned long input_size,
80+
unsigned long *output,
81+
unsigned long output_size,
82+
unsigned long *virt_addr)
8283
{
83-
return (unsigned char *)output_ptr;
84+
/* No change from existing output location. */
85+
*virt_addr = *output;
8486
}
8587
#endif
8688

0 commit comments

Comments
 (0)