2016-04-18 16:42:14 +00:00
|
|
|
/*
|
|
|
|
* kaslr.c
|
|
|
|
*
|
|
|
|
* This contains the routines needed to generate a reasonable level of
|
|
|
|
* entropy to choose a randomized kernel base address offset in support
|
|
|
|
* of Kernel Address Space Layout Randomization (KASLR). Additionally
|
|
|
|
* handles walking the physical memory maps (and tracking memory regions
|
|
|
|
* to avoid) in order to select a physical memory location that can
|
|
|
|
* contain the entire properly aligned running kernel image.
|
|
|
|
*
|
|
|
|
*/
|
2013-10-11 00:18:14 +00:00
|
|
|
#include "misc.h"
|
2016-05-02 22:51:00 +00:00
|
|
|
#include "error.h"
|
2017-01-11 23:20:01 +00:00
|
|
|
#include "../boot.h"
|
2013-10-11 00:18:14 +00:00
|
|
|
|
2013-11-11 22:28:39 +00:00
|
|
|
#include <generated/compile.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/uts.h>
|
|
|
|
#include <linux/utsname.h>
|
|
|
|
#include <generated/utsrelease.h>
|
|
|
|
|
|
|
|
/* Simplified build-specific string for starting entropy. */
|
2013-11-12 16:56:07 +00:00
|
|
|
static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
|
2013-11-11 22:28:39 +00:00
|
|
|
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
|
|
|
|
|
|
|
|
static unsigned long rotate_xor(unsigned long hash, const void *area,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned long *ptr = (unsigned long *)area;
|
|
|
|
|
|
|
|
for (i = 0; i < size / sizeof(hash); i++) {
|
|
|
|
/* Rotate by odd number of bits and XOR. */
|
|
|
|
hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
|
|
|
|
hash ^= ptr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to create a simple but unpredictable starting entropy. */
|
2016-06-22 00:46:58 +00:00
|
|
|
static unsigned long get_boot_seed(void)
|
2013-11-11 22:28:39 +00:00
|
|
|
{
|
|
|
|
unsigned long hash = 0;
|
|
|
|
|
|
|
|
hash = rotate_xor(hash, build_str, sizeof(build_str));
|
2016-04-18 16:42:12 +00:00
|
|
|
hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
|
2013-11-11 22:28:39 +00:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2016-06-22 00:46:58 +00:00
|
|
|
#define KASLR_COMPRESSED_BOOT
|
|
|
|
#include "../../lib/kaslr.c"
|
2013-10-11 00:18:14 +00:00
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
struct mem_vector {
|
2017-01-11 23:20:01 +00:00
|
|
|
unsigned long long start;
|
|
|
|
unsigned long long size;
|
2013-10-11 00:18:16 +00:00
|
|
|
};
|
|
|
|
|
2017-01-11 23:20:01 +00:00
|
|
|
/* Only supporting at most 4 unusable memmap regions with kaslr */
|
|
|
|
#define MAX_MEMMAP_REGIONS 4
|
|
|
|
|
|
|
|
static bool memmap_too_large;
|
|
|
|
|
2016-05-06 19:44:59 +00:00
|
|
|
enum mem_avoid_index {
|
|
|
|
MEM_AVOID_ZO_RANGE = 0,
|
|
|
|
MEM_AVOID_INITRD,
|
|
|
|
MEM_AVOID_CMDLINE,
|
|
|
|
MEM_AVOID_BOOTPARAMS,
|
2017-01-11 23:20:01 +00:00
|
|
|
MEM_AVOID_MEMMAP_BEGIN,
|
|
|
|
MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
|
2016-05-06 19:44:59 +00:00
|
|
|
MEM_AVOID_MAX,
|
|
|
|
};
|
|
|
|
|
2014-02-09 21:56:44 +00:00
|
|
|
static struct mem_vector mem_avoid[MEM_AVOID_MAX];
|
2013-10-11 00:18:16 +00:00
|
|
|
|
|
|
|
static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
|
|
|
|
{
|
|
|
|
/* Item one is entirely before item two. */
|
|
|
|
if (one->start + one->size <= two->start)
|
|
|
|
return false;
|
|
|
|
/* Item one is entirely after item two. */
|
|
|
|
if (one->start >= two->start + two->size)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:20:01 +00:00
|
|
|
/**
|
|
|
|
* _memparse - Parse a string with mem suffixes into a number
|
|
|
|
* @ptr: Where parse begins
|
|
|
|
* @retptr: (output) Optional pointer to next char after parse completes
|
|
|
|
*
|
|
|
|
* Parses a string into a number. The number stored at @ptr is
|
|
|
|
* potentially suffixed with K, M, G, T, P, E.
|
|
|
|
*/
|
|
|
|
static unsigned long long _memparse(const char *ptr, char **retptr)
|
|
|
|
{
|
|
|
|
char *endptr; /* Local pointer to end of parsed string */
|
|
|
|
|
|
|
|
unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
|
|
|
|
|
|
|
|
switch (*endptr) {
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
ret <<= 10;
|
|
|
|
case 'P':
|
|
|
|
case 'p':
|
|
|
|
ret <<= 10;
|
|
|
|
case 'T':
|
|
|
|
case 't':
|
|
|
|
ret <<= 10;
|
|
|
|
case 'G':
|
|
|
|
case 'g':
|
|
|
|
ret <<= 10;
|
|
|
|
case 'M':
|
|
|
|
case 'm':
|
|
|
|
ret <<= 10;
|
|
|
|
case 'K':
|
|
|
|
case 'k':
|
|
|
|
ret <<= 10;
|
|
|
|
endptr++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retptr)
|
|
|
|
*retptr = endptr;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_memmap(char *p, unsigned long long *start, unsigned long long *size)
|
|
|
|
{
|
|
|
|
char *oldp;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* We don't care about this option here */
|
|
|
|
if (!strncmp(p, "exactmap", 8))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
oldp = p;
|
|
|
|
*size = _memparse(p, &p);
|
|
|
|
if (p == oldp)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (*p) {
|
|
|
|
case '@':
|
|
|
|
/* Skip this region, usable */
|
|
|
|
*start = 0;
|
|
|
|
*size = 0;
|
|
|
|
return 0;
|
|
|
|
case '#':
|
|
|
|
case '$':
|
|
|
|
case '!':
|
|
|
|
*start = _memparse(p + 1, &p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mem_avoid_memmap(void)
|
|
|
|
{
|
|
|
|
char arg[128];
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* See if we have any memmap areas */
|
|
|
|
rc = cmdline_find_option("memmap", arg, sizeof(arg));
|
|
|
|
if (rc <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
str = arg;
|
|
|
|
while (str && (i < MAX_MEMMAP_REGIONS)) {
|
|
|
|
int rc;
|
|
|
|
unsigned long long start, size;
|
|
|
|
char *k = strchr(str, ',');
|
|
|
|
|
|
|
|
if (k)
|
|
|
|
*k++ = 0;
|
|
|
|
|
|
|
|
rc = parse_memmap(str, &start, &size);
|
|
|
|
if (rc < 0)
|
|
|
|
break;
|
|
|
|
str = k;
|
|
|
|
/* A usable region that should not be skipped */
|
|
|
|
if (size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].start = start;
|
|
|
|
mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].size = size;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* More than 4 memmaps, fail kaslr */
|
|
|
|
if ((i >= MAX_MEMMAP_REGIONS) && str)
|
|
|
|
memmap_too_large = true;
|
|
|
|
}
|
|
|
|
|
2016-05-05 22:13:47 +00:00
|
|
|
/*
|
2016-05-06 19:44:59 +00:00
|
|
|
* In theory, KASLR can put the kernel anywhere in the range of [16M, 64T).
|
|
|
|
* The mem_avoid array is used to store the ranges that need to be avoided
|
|
|
|
* when KASLR searches for an appropriate random address. We must avoid any
|
2016-05-05 22:13:47 +00:00
|
|
|
* regions that are unsafe to overlap with during decompression, and other
|
2016-05-06 19:44:59 +00:00
|
|
|
* things like the initrd, cmdline and boot_params. This comment seeks to
|
|
|
|
* explain mem_avoid as clearly as possible since incorrect mem_avoid
|
|
|
|
* memory ranges lead to really hard to debug boot failures.
|
|
|
|
*
|
|
|
|
* The initrd, cmdline, and boot_params are trivial to identify for
|
2016-05-09 20:22:05 +00:00
|
|
|
* avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and
|
2016-05-06 19:44:59 +00:00
|
|
|
* MEM_AVOID_BOOTPARAMS respectively below.
|
|
|
|
*
|
|
|
|
* What is not obvious how to avoid is the range of memory that is used
|
|
|
|
* during decompression (MEM_AVOID_ZO_RANGE below). This range must cover
|
|
|
|
* the compressed kernel (ZO) and its run space, which is used to extract
|
|
|
|
* the uncompressed kernel (VO) and relocs.
|
|
|
|
*
|
|
|
|
* ZO's full run size sits against the end of the decompression buffer, so
|
|
|
|
* we can calculate where text, data, bss, etc of ZO are positioned more
|
|
|
|
* easily.
|
|
|
|
*
|
|
|
|
* For additional background, the decompression calculations can be found
|
|
|
|
* in header.S, and the memory diagram is based on the one found in misc.c.
|
|
|
|
*
|
|
|
|
* The following conditions are already enforced by the image layouts and
|
|
|
|
* associated code:
|
|
|
|
* - input + input_size >= output + output_size
|
|
|
|
* - kernel_total_size <= init_size
|
|
|
|
* - kernel_total_size <= output_size (see Note below)
|
|
|
|
* - output + init_size >= output + output_size
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* (Note that kernel_total_size and output_size have no fundamental
|
|
|
|
* relationship, but output_size is passed to choose_random_location
|
|
|
|
* as a maximum of the two. The diagram is showing a case where
|
|
|
|
* kernel_total_size is larger than output_size, but this case is
|
|
|
|
* handled by bumping output_size.)
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* The above conditions can be illustrated by a diagram:
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* 0 output input input+input_size output+init_size
|
|
|
|
* | | | | |
|
|
|
|
* | | | | |
|
|
|
|
* |-----|--------|--------|--------------|-----------|--|-------------|
|
|
|
|
* | | |
|
|
|
|
* | | |
|
|
|
|
* output+init_size-ZO_INIT_SIZE output+output_size output+kernel_total_size
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* [output, output+init_size) is the entire memory range used for
|
|
|
|
* extracting the compressed image.
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* [output, output+kernel_total_size) is the range needed for the
|
|
|
|
* uncompressed kernel (VO) and its run size (bss, brk, etc).
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* [output, output+output_size) is VO plus relocs (i.e. the entire
|
|
|
|
* uncompressed payload contained by ZO). This is the area of the buffer
|
|
|
|
* written to during decompression.
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case
|
|
|
|
* range of the copied ZO and decompression code. (i.e. the range
|
|
|
|
* covered backwards of size ZO_INIT_SIZE, starting from output+init_size.)
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* [input, input+input_size) is the original copied compressed image (ZO)
|
|
|
|
* (i.e. it does not include its run size). This range must be avoided
|
|
|
|
* because it contains the data used for decompression.
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* [input+input_size, output+init_size) is [_text, _end) for ZO. This
|
|
|
|
* range includes ZO's heap and stack, and must be avoided since it
|
|
|
|
* performs the decompression.
|
2016-05-05 22:13:47 +00:00
|
|
|
*
|
2016-05-06 19:44:59 +00:00
|
|
|
* Since the above two ranges need to be avoided and they are adjacent,
|
|
|
|
* they can be merged, resulting in: [input, output+init_size) which
|
|
|
|
* becomes the MEM_AVOID_ZO_RANGE below.
|
2016-05-05 22:13:47 +00:00
|
|
|
*/
|
2013-10-11 00:18:16 +00:00
|
|
|
static void mem_avoid_init(unsigned long input, unsigned long input_size,
|
2016-05-05 22:13:47 +00:00
|
|
|
unsigned long output)
|
2013-10-11 00:18:16 +00:00
|
|
|
{
|
2016-05-05 22:13:47 +00:00
|
|
|
unsigned long init_size = boot_params->hdr.init_size;
|
2013-10-11 00:18:16 +00:00
|
|
|
u64 initrd_start, initrd_size;
|
|
|
|
u64 cmd_line, cmd_line_size;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid the region that is unsafe to overlap during
|
2016-05-05 22:13:47 +00:00
|
|
|
* decompression.
|
2013-10-11 00:18:16 +00:00
|
|
|
*/
|
2016-05-06 19:44:59 +00:00
|
|
|
mem_avoid[MEM_AVOID_ZO_RANGE].start = input;
|
|
|
|
mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input;
|
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.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: kernel-hardening@lists.openwall.com
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1462572095-11754-4-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-05-06 22:01:35 +00:00
|
|
|
add_identity_map(mem_avoid[MEM_AVOID_ZO_RANGE].start,
|
|
|
|
mem_avoid[MEM_AVOID_ZO_RANGE].size);
|
2013-10-11 00:18:16 +00:00
|
|
|
|
|
|
|
/* Avoid initrd. */
|
2016-04-18 16:42:12 +00:00
|
|
|
initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
|
|
|
|
initrd_start |= boot_params->hdr.ramdisk_image;
|
|
|
|
initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
|
|
|
|
initrd_size |= boot_params->hdr.ramdisk_size;
|
2016-05-06 19:44:59 +00:00
|
|
|
mem_avoid[MEM_AVOID_INITRD].start = initrd_start;
|
|
|
|
mem_avoid[MEM_AVOID_INITRD].size = initrd_size;
|
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.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: kernel-hardening@lists.openwall.com
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1462572095-11754-4-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-05-06 22:01:35 +00:00
|
|
|
/* No need to set mapping for initrd, it will be handled in VO. */
|
2013-10-11 00:18:16 +00:00
|
|
|
|
|
|
|
/* Avoid kernel command line. */
|
2016-04-18 16:42:12 +00:00
|
|
|
cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
|
|
|
|
cmd_line |= boot_params->hdr.cmd_line_ptr;
|
2013-10-11 00:18:16 +00:00
|
|
|
/* Calculate size of cmd_line. */
|
|
|
|
ptr = (char *)(unsigned long)cmd_line;
|
|
|
|
for (cmd_line_size = 0; ptr[cmd_line_size++]; )
|
|
|
|
;
|
2016-05-06 19:44:59 +00:00
|
|
|
mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
|
|
|
|
mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
|
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.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: kernel-hardening@lists.openwall.com
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1462572095-11754-4-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-05-06 22:01:35 +00:00
|
|
|
add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start,
|
|
|
|
mem_avoid[MEM_AVOID_CMDLINE].size);
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-06 19:44:59 +00:00
|
|
|
/* Avoid boot parameters. */
|
|
|
|
mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
|
|
|
|
mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params);
|
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.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: kernel-hardening@lists.openwall.com
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1462572095-11754-4-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-05-06 22:01:35 +00:00
|
|
|
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. */
|
|
|
|
|
2017-01-11 23:20:01 +00:00
|
|
|
/* Mark the memmap regions we need to avoid */
|
|
|
|
mem_avoid_memmap();
|
|
|
|
|
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.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: kernel-hardening@lists.openwall.com
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1462572095-11754-4-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-05-06 22:01:35 +00:00
|
|
|
#ifdef CONFIG_X86_VERBOSE_BOOTUP
|
|
|
|
/* Make sure video RAM can be used. */
|
|
|
|
add_identity_map(0, PMD_SIZE);
|
|
|
|
#endif
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:22:07 +00:00
|
|
|
/*
|
|
|
|
* Does this memory vector overlap a known avoided area? If so, record the
|
|
|
|
* overlap region with the lowest address.
|
|
|
|
*/
|
|
|
|
static bool mem_avoid_overlap(struct mem_vector *img,
|
|
|
|
struct mem_vector *overlap)
|
2013-10-11 00:18:16 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-09-11 16:19:31 +00:00
|
|
|
struct setup_data *ptr;
|
2016-05-09 20:22:07 +00:00
|
|
|
unsigned long earliest = img->start + img->size;
|
|
|
|
bool is_overlapping = false;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MEM_AVOID_MAX; i++) {
|
2016-05-09 20:22:07 +00:00
|
|
|
if (mem_overlaps(img, &mem_avoid[i]) &&
|
|
|
|
mem_avoid[i].start < earliest) {
|
|
|
|
*overlap = mem_avoid[i];
|
2016-07-01 07:34:40 +00:00
|
|
|
earliest = overlap->start;
|
2016-05-09 20:22:07 +00:00
|
|
|
is_overlapping = true;
|
|
|
|
}
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 16:19:31 +00:00
|
|
|
/* Avoid all entries in the setup_data linked list. */
|
2016-04-18 16:42:12 +00:00
|
|
|
ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
|
2014-09-11 16:19:31 +00:00
|
|
|
while (ptr) {
|
|
|
|
struct mem_vector avoid;
|
|
|
|
|
2014-10-01 18:36:32 +00:00
|
|
|
avoid.start = (unsigned long)ptr;
|
2014-09-11 16:19:31 +00:00
|
|
|
avoid.size = sizeof(*ptr) + ptr->len;
|
|
|
|
|
2016-05-09 20:22:07 +00:00
|
|
|
if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
|
|
|
|
*overlap = avoid;
|
2016-07-01 07:34:40 +00:00
|
|
|
earliest = overlap->start;
|
2016-05-09 20:22:07 +00:00
|
|
|
is_overlapping = true;
|
|
|
|
}
|
2014-09-11 16:19:31 +00:00
|
|
|
|
|
|
|
ptr = (struct setup_data *)(unsigned long)ptr->next;
|
|
|
|
}
|
|
|
|
|
2016-05-09 20:22:07 +00:00
|
|
|
return is_overlapping;
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:22:06 +00:00
|
|
|
struct slot_area {
|
|
|
|
unsigned long addr;
|
|
|
|
int num;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_SLOT_AREA 100
|
|
|
|
|
|
|
|
static struct slot_area slot_areas[MAX_SLOT_AREA];
|
|
|
|
|
2014-02-09 21:56:44 +00:00
|
|
|
static unsigned long slot_max;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-09 20:22:06 +00:00
|
|
|
static unsigned long slot_area_index;
|
|
|
|
|
|
|
|
static void store_slot_info(struct mem_vector *region, unsigned long image_size)
|
|
|
|
{
|
|
|
|
struct slot_area slot_area;
|
|
|
|
|
|
|
|
if (slot_area_index == MAX_SLOT_AREA)
|
|
|
|
return;
|
|
|
|
|
|
|
|
slot_area.addr = region->start;
|
|
|
|
slot_area.num = (region->size - image_size) /
|
|
|
|
CONFIG_PHYSICAL_ALIGN + 1;
|
|
|
|
|
|
|
|
if (slot_area.num > 0) {
|
|
|
|
slot_areas[slot_area_index++] = slot_area;
|
|
|
|
slot_max += slot_area.num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
static unsigned long slots_fetch_random(void)
|
|
|
|
{
|
2016-05-25 22:45:33 +00:00
|
|
|
unsigned long slot;
|
|
|
|
int i;
|
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
/* Handle case of no slots stored. */
|
|
|
|
if (slot_max == 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-06-22 00:46:58 +00:00
|
|
|
slot = kaslr_get_random_long("Physical") % slot_max;
|
2016-05-25 22:45:33 +00:00
|
|
|
|
|
|
|
for (i = 0; i < slot_area_index; i++) {
|
|
|
|
if (slot >= slot_areas[i].num) {
|
|
|
|
slot -= slot_areas[i].num;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return slot_areas[i].addr + slot * CONFIG_PHYSICAL_ALIGN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == slot_area_index)
|
|
|
|
debug_putstr("slots_fetch_random() failed!?\n");
|
|
|
|
return 0;
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 11:56:13 +00:00
|
|
|
static void process_e820_entry(struct boot_e820_entry *entry,
|
2013-10-11 00:18:16 +00:00
|
|
|
unsigned long minimum,
|
|
|
|
unsigned long image_size)
|
|
|
|
{
|
2016-05-25 22:45:33 +00:00
|
|
|
struct mem_vector region, overlap;
|
|
|
|
struct slot_area slot_area;
|
|
|
|
unsigned long start_orig;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
|
|
|
/* Skip non-RAM entries. */
|
2017-01-28 16:09:33 +00:00
|
|
|
if (entry->type != E820_TYPE_RAM)
|
2013-10-11 00:18:16 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* On 32-bit, ignore entries entirely above our maximum. */
|
|
|
|
if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= KERNEL_IMAGE_SIZE)
|
2013-10-11 00:18:16 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Ignore entries entirely below our minimum. */
|
|
|
|
if (entry->addr + entry->size < minimum)
|
|
|
|
return;
|
|
|
|
|
|
|
|
region.start = entry->addr;
|
|
|
|
region.size = entry->size;
|
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* Give up if slot area array is full. */
|
|
|
|
while (slot_area_index < MAX_SLOT_AREA) {
|
|
|
|
start_orig = region.start;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* Potentially raise address to minimum location. */
|
|
|
|
if (region.start < minimum)
|
|
|
|
region.start = minimum;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* Potentially raise address to meet alignment needs. */
|
|
|
|
region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* Did we raise the address above this e820 region? */
|
|
|
|
if (region.start > entry->addr + entry->size)
|
|
|
|
return;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* Reduce size by any delta from the original address. */
|
|
|
|
region.size -= region.start - start_orig;
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-25 22:45:33 +00:00
|
|
|
/* On 32-bit, reduce region size to fit within max size. */
|
|
|
|
if (IS_ENABLED(CONFIG_X86_32) &&
|
|
|
|
region.start + region.size > KERNEL_IMAGE_SIZE)
|
|
|
|
region.size = KERNEL_IMAGE_SIZE - region.start;
|
|
|
|
|
|
|
|
/* Return if region can't contain decompressed kernel */
|
|
|
|
if (region.size < image_size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If nothing overlaps, store the region and return. */
|
|
|
|
if (!mem_avoid_overlap(®ion, &overlap)) {
|
|
|
|
store_slot_info(®ion, image_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store beginning of region if holds at least image_size. */
|
|
|
|
if (overlap.start > region.start + image_size) {
|
|
|
|
struct mem_vector beginning;
|
|
|
|
|
|
|
|
beginning.start = region.start;
|
|
|
|
beginning.size = overlap.start - region.start;
|
|
|
|
store_slot_info(&beginning, image_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return if overlap extends to or past end of region. */
|
|
|
|
if (overlap.start + overlap.size >= region.start + region.size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Clip off the overlapping region and start over. */
|
|
|
|
region.size -= overlap.start - region.start + overlap.size;
|
|
|
|
region.start = overlap.start + overlap.size;
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 20:22:08 +00:00
|
|
|
static unsigned long find_random_phys_addr(unsigned long minimum,
|
|
|
|
unsigned long image_size)
|
2013-10-11 00:18:16 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long addr;
|
|
|
|
|
2017-01-11 23:20:01 +00:00
|
|
|
/* Check if we had too many memmaps. */
|
|
|
|
if (memmap_too_large) {
|
|
|
|
debug_putstr("Aborted e820 scan (more than 4 memmap= args)!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
/* Make sure minimum is aligned. */
|
|
|
|
minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
|
|
|
|
|
|
|
|
/* Verify potential e820 positions, appending to slots list. */
|
2016-04-18 16:42:12 +00:00
|
|
|
for (i = 0; i < boot_params->e820_entries; i++) {
|
2017-01-27 12:54:38 +00:00
|
|
|
process_e820_entry(&boot_params->e820_table[i], minimum,
|
2016-05-09 20:22:08 +00:00
|
|
|
image_size);
|
2016-05-25 22:45:33 +00:00
|
|
|
if (slot_area_index == MAX_SLOT_AREA) {
|
|
|
|
debug_putstr("Aborted e820 scan (slot_areas full)!\n");
|
|
|
|
break;
|
|
|
|
}
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return slots_fetch_random();
|
|
|
|
}
|
|
|
|
|
2016-05-09 20:22:08 +00:00
|
|
|
static unsigned long find_random_virt_addr(unsigned long minimum,
|
|
|
|
unsigned long image_size)
|
|
|
|
{
|
|
|
|
unsigned long slots, random_addr;
|
|
|
|
|
|
|
|
/* Make sure minimum is aligned. */
|
|
|
|
minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
|
|
|
|
/* Align image_size for easy slot calculations. */
|
|
|
|
image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There are how many CONFIG_PHYSICAL_ALIGN-sized slots
|
|
|
|
* that can hold image_size within the range of minimum to
|
|
|
|
* KERNEL_IMAGE_SIZE?
|
|
|
|
*/
|
|
|
|
slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
|
|
|
|
CONFIG_PHYSICAL_ALIGN + 1;
|
|
|
|
|
2016-06-22 00:46:58 +00:00
|
|
|
random_addr = kaslr_get_random_long("Virtual") % slots;
|
2016-05-09 20:22:08 +00:00
|
|
|
|
|
|
|
return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
|
|
|
|
}
|
|
|
|
|
2016-05-06 11:50:15 +00:00
|
|
|
/*
|
|
|
|
* Since this function examines addresses much more numerically,
|
|
|
|
* it takes the input and output pointers as 'unsigned long'.
|
|
|
|
*/
|
2016-05-25 22:45:32 +00:00
|
|
|
void choose_random_location(unsigned long input,
|
|
|
|
unsigned long input_size,
|
|
|
|
unsigned long *output,
|
|
|
|
unsigned long output_size,
|
|
|
|
unsigned long *virt_addr)
|
2013-10-11 00:18:14 +00:00
|
|
|
{
|
2016-05-25 22:45:34 +00:00
|
|
|
unsigned long random_addr, min_addr;
|
2013-10-11 00:18:14 +00:00
|
|
|
|
2016-05-25 22:45:32 +00:00
|
|
|
/* By default, keep output position unchanged. */
|
|
|
|
*virt_addr = *output;
|
|
|
|
|
2013-10-11 00:18:14 +00:00
|
|
|
if (cmdline_find_option_bool("nokaslr")) {
|
2016-04-20 20:55:46 +00:00
|
|
|
warn("KASLR disabled: 'nokaslr' on cmdline.");
|
2016-05-25 22:45:32 +00:00
|
|
|
return;
|
2013-10-11 00:18:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
boot_params->hdr.loadflags |= KASLR_FLAG;
|
2015-04-01 10:49:52 +00:00
|
|
|
|
2016-05-25 22:45:31 +00:00
|
|
|
/* Prepare to add new identity pagetables on demand. */
|
|
|
|
initialize_identity_maps();
|
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
/* Record the various known unsafe memory ranges. */
|
2016-05-25 22:45:32 +00:00
|
|
|
mem_avoid_init(input, input_size, *output);
|
2013-10-11 00:18:16 +00:00
|
|
|
|
2016-05-25 22:45:34 +00:00
|
|
|
/*
|
|
|
|
* Low end of the randomization range should be the
|
|
|
|
* smaller of 512M or the initial kernel image
|
|
|
|
* location:
|
|
|
|
*/
|
|
|
|
min_addr = min(*output, 512UL << 20);
|
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
/* Walk e820 and find a random address. */
|
2016-05-25 22:45:34 +00:00
|
|
|
random_addr = find_random_phys_addr(min_addr, output_size);
|
2016-04-18 16:42:15 +00:00
|
|
|
if (!random_addr) {
|
2017-01-11 23:20:01 +00:00
|
|
|
warn("Physical KASLR disabled: no suitable memory region!");
|
2016-05-25 22:45:32 +00:00
|
|
|
} else {
|
|
|
|
/* Update the new physical address location. */
|
|
|
|
if (*output != random_addr) {
|
|
|
|
add_identity_map(random_addr, output_size);
|
|
|
|
*output = random_addr;
|
|
|
|
}
|
2013-10-11 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 09:59:40 +00:00
|
|
|
/* This actually loads the identity pagetable on x86_64. */
|
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.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: kernel-hardening@lists.openwall.com
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1462572095-11754-4-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-05-06 22:01:35 +00:00
|
|
|
finalize_identity_maps();
|
2016-05-25 22:45:32 +00:00
|
|
|
|
|
|
|
/* 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;
|
2013-10-11 00:18:14 +00:00
|
|
|
}
|