blob: a47f832664f2bfa057d70600982420d2b18023f3 [file] [log] [blame]
Kees Cook7de828d2016-04-18 09:42:14 -07001/*
2 * kaslr.c
3 *
4 * This contains the routines needed to generate a reasonable level of
5 * entropy to choose a randomized kernel base address offset in support
6 * of Kernel Address Space Layout Randomization (KASLR). Additionally
7 * handles walking the physical memory maps (and tracking memory regions
8 * to avoid) in order to select a physical memory location that can
9 * contain the entire properly aligned running kernel image.
10 *
11 */
Kees Cook8ab38202013-10-10 17:18:14 -070012#include "misc.h"
Kees Cookdc425a62016-05-02 15:51:00 -070013#include "error.h"
Dave Jiangf2844242017-01-11 16:20:01 -070014#include "../boot.h"
Kees Cook8ab38202013-10-10 17:18:14 -070015
Kees Cooka653f352013-11-11 14:28:39 -080016#include <generated/compile.h>
17#include <linux/module.h>
18#include <linux/uts.h>
19#include <linux/utsname.h>
20#include <generated/utsrelease.h>
Kees Cooka653f352013-11-11 14:28:39 -080021
22/* Simplified build-specific string for starting entropy. */
Kees Cook327f7d72013-11-12 08:56:07 -080023static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
Kees Cooka653f352013-11-11 14:28:39 -080024 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
25
Kees Cooka653f352013-11-11 14:28:39 -080026static unsigned long rotate_xor(unsigned long hash, const void *area,
27 size_t size)
28{
29 size_t i;
30 unsigned long *ptr = (unsigned long *)area;
31
32 for (i = 0; i < size / sizeof(hash); i++) {
33 /* Rotate by odd number of bits and XOR. */
34 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
35 hash ^= ptr[i];
36 }
37
38 return hash;
39}
40
41/* Attempt to create a simple but unpredictable starting entropy. */
Thomas Garnierd899a7d2016-06-21 17:46:58 -070042static unsigned long get_boot_seed(void)
Kees Cooka653f352013-11-11 14:28:39 -080043{
44 unsigned long hash = 0;
45
46 hash = rotate_xor(hash, build_str, sizeof(build_str));
Kees Cook6655e0a2016-04-18 09:42:12 -070047 hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
Kees Cooka653f352013-11-11 14:28:39 -080048
49 return hash;
50}
51
Thomas Garnierd899a7d2016-06-21 17:46:58 -070052#define KASLR_COMPRESSED_BOOT
53#include "../../lib/kaslr.c"
Kees Cook8ab38202013-10-10 17:18:14 -070054
Kees Cook82fa9632013-10-10 17:18:16 -070055struct mem_vector {
Dave Jiangf2844242017-01-11 16:20:01 -070056 unsigned long long start;
57 unsigned long long size;
Kees Cook82fa9632013-10-10 17:18:16 -070058};
59
Dave Jiangf2844242017-01-11 16:20:01 -070060/* Only supporting at most 4 unusable memmap regions with kaslr */
61#define MAX_MEMMAP_REGIONS 4
62
63static bool memmap_too_large;
64
Kees Cooked09acd2016-05-06 12:44:59 -070065enum mem_avoid_index {
66 MEM_AVOID_ZO_RANGE = 0,
67 MEM_AVOID_INITRD,
68 MEM_AVOID_CMDLINE,
69 MEM_AVOID_BOOTPARAMS,
Dave Jiangf2844242017-01-11 16:20:01 -070070 MEM_AVOID_MEMMAP_BEGIN,
71 MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
Kees Cooked09acd2016-05-06 12:44:59 -070072 MEM_AVOID_MAX,
73};
74
Kees Cooke290e8c2014-02-09 13:56:44 -080075static struct mem_vector mem_avoid[MEM_AVOID_MAX];
Kees Cook82fa9632013-10-10 17:18:16 -070076
Kees Cook82fa9632013-10-10 17:18:16 -070077static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
78{
79 /* Item one is entirely before item two. */
80 if (one->start + one->size <= two->start)
81 return false;
82 /* Item one is entirely after item two. */
83 if (one->start >= two->start + two->size)
84 return false;
85 return true;
86}
87
Dave Jiangf2844242017-01-11 16:20:01 -070088/**
89 * _memparse - Parse a string with mem suffixes into a number
90 * @ptr: Where parse begins
91 * @retptr: (output) Optional pointer to next char after parse completes
92 *
93 * Parses a string into a number. The number stored at @ptr is
94 * potentially suffixed with K, M, G, T, P, E.
95 */
96static unsigned long long _memparse(const char *ptr, char **retptr)
97{
98 char *endptr; /* Local pointer to end of parsed string */
99
100 unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
101
102 switch (*endptr) {
103 case 'E':
104 case 'e':
105 ret <<= 10;
106 case 'P':
107 case 'p':
108 ret <<= 10;
109 case 'T':
110 case 't':
111 ret <<= 10;
112 case 'G':
113 case 'g':
114 ret <<= 10;
115 case 'M':
116 case 'm':
117 ret <<= 10;
118 case 'K':
119 case 'k':
120 ret <<= 10;
121 endptr++;
122 default:
123 break;
124 }
125
126 if (retptr)
127 *retptr = endptr;
128
129 return ret;
130}
131
132static int
133parse_memmap(char *p, unsigned long long *start, unsigned long long *size)
134{
135 char *oldp;
136
137 if (!p)
138 return -EINVAL;
139
140 /* We don't care about this option here */
141 if (!strncmp(p, "exactmap", 8))
142 return -EINVAL;
143
144 oldp = p;
145 *size = _memparse(p, &p);
146 if (p == oldp)
147 return -EINVAL;
148
149 switch (*p) {
150 case '@':
151 /* Skip this region, usable */
152 *start = 0;
153 *size = 0;
154 return 0;
155 case '#':
156 case '$':
157 case '!':
158 *start = _memparse(p + 1, &p);
159 return 0;
160 }
161
162 return -EINVAL;
163}
164
165static void mem_avoid_memmap(void)
166{
167 char arg[128];
168 int rc;
169 int i;
170 char *str;
171
172 /* See if we have any memmap areas */
173 rc = cmdline_find_option("memmap", arg, sizeof(arg));
174 if (rc <= 0)
175 return;
176
177 i = 0;
178 str = arg;
179 while (str && (i < MAX_MEMMAP_REGIONS)) {
180 int rc;
181 unsigned long long start, size;
182 char *k = strchr(str, ',');
183
184 if (k)
185 *k++ = 0;
186
187 rc = parse_memmap(str, &start, &size);
188 if (rc < 0)
189 break;
190 str = k;
191 /* A usable region that should not be skipped */
192 if (size == 0)
193 continue;
194
195 mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].start = start;
196 mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].size = size;
197 i++;
198 }
199
200 /* More than 4 memmaps, fail kaslr */
201 if ((i >= MAX_MEMMAP_REGIONS) && str)
202 memmap_too_large = true;
203}
204
Yinghai Lu9dc19692016-05-05 15:13:47 -0700205/*
Kees Cooked09acd2016-05-06 12:44:59 -0700206 * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T).
207 * The mem_avoid array is used to store the ranges that need to be avoided
208 * when KASLR searches for an appropriate random address. We must avoid any
Yinghai Lu9dc19692016-05-05 15:13:47 -0700209 * regions that are unsafe to overlap with during decompression, and other
Kees Cooked09acd2016-05-06 12:44:59 -0700210 * things like the initrd, cmdline and boot_params. This comment seeks to
211 * explain mem_avoid as clearly as possible since incorrect mem_avoid
212 * memory ranges lead to really hard to debug boot failures.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700213 *
Kees Cooked09acd2016-05-06 12:44:59 -0700214 * The initrd, cmdline, and boot_params are trivial to identify for
Kees Cookcb18ef02016-05-09 13:22:05 -0700215 * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and
Kees Cooked09acd2016-05-06 12:44:59 -0700216 * MEM_AVOID_BOOTPARAMS respectively below.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700217 *
Kees Cooked09acd2016-05-06 12:44:59 -0700218 * What is not obvious how to avoid is the range of memory that is used
219 * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover
220 * the compressed kernel (ZO) and its run space, which is used to extract
221 * the uncompressed kernel (VO) and relocs.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700222 *
Kees Cooked09acd2016-05-06 12:44:59 -0700223 * ZO's full run size sits against the end of the decompression buffer, so
224 * we can calculate where text, data, bss, etc of ZO are positioned more
225 * easily.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700226 *
Kees Cooked09acd2016-05-06 12:44:59 -0700227 * For additional background, the decompression calculations can be found
228 * in header.S, and the memory diagram is based on the one found in misc.c.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700229 *
Kees Cooked09acd2016-05-06 12:44:59 -0700230 * The following conditions are already enforced by the image layouts and
231 * associated code:
232 * - input + input_size >= output + output_size
233 * - kernel_total_size <= init_size
234 * - kernel_total_size <= output_size (see Note below)
235 * - output + init_size >= output + output_size
Yinghai Lu9dc19692016-05-05 15:13:47 -0700236 *
Kees Cooked09acd2016-05-06 12:44:59 -0700237 * (Note that kernel_total_size and output_size have no fundamental
238 * relationship, but output_size is passed to choose_random_location
239 * as a maximum of the two. The diagram is showing a case where
240 * kernel_total_size is larger than output_size, but this case is
241 * handled by bumping output_size.)
Yinghai Lu9dc19692016-05-05 15:13:47 -0700242 *
Kees Cooked09acd2016-05-06 12:44:59 -0700243 * The above conditions can be illustrated by a diagram:
Yinghai Lu9dc19692016-05-05 15:13:47 -0700244 *
Kees Cooked09acd2016-05-06 12:44:59 -0700245 * 0 output input input+input_size output+init_size
246 * | | | | |
247 * | | | | |
248 * |-----|--------|--------|--------------|-----------|--|-------------|
249 * | | |
250 * | | |
251 * output+init_size-ZO_INIT_SIZE output+output_size output+kernel_total_size
Yinghai Lu9dc19692016-05-05 15:13:47 -0700252 *
Kees Cooked09acd2016-05-06 12:44:59 -0700253 * [output, output+init_size) is the entire memory range used for
254 * extracting the compressed image.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700255 *
Kees Cooked09acd2016-05-06 12:44:59 -0700256 * [output, output+kernel_total_size) is the range needed for the
257 * uncompressed kernel (VO) and its run size (bss, brk, etc).
258 *
259 * [output, output+output_size) is VO plus relocs (i.e. the entire
260 * uncompressed payload contained by ZO). This is the area of the buffer
261 * written to during decompression.
262 *
263 * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case
264 * range of the copied ZO and decompression code. (i.e. the range
265 * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.)
266 *
267 * [input, input+input_size) is the original copied compressed image (ZO)
268 * (i.e. it does not include its run size). This range must be avoided
269 * because it contains the data used for decompression.
270 *
271 * [input+input_size, output+init_size) is [_text, _end) for ZO. This
272 * range includes ZO's heap and stack, and must be avoided since it
273 * performs the decompression.
274 *
275 * Since the above two ranges need to be avoided and they are adjacent,
276 * they can be merged, resulting in: [input, output+init_size) which
277 * becomes the MEM_AVOID_ZO_RANGE below.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700278 */
Kees Cook82fa9632013-10-10 17:18:16 -0700279static void mem_avoid_init(unsigned long input, unsigned long input_size,
Yinghai Lu9dc19692016-05-05 15:13:47 -0700280 unsigned long output)
Kees Cook82fa9632013-10-10 17:18:16 -0700281{
Yinghai Lu9dc19692016-05-05 15:13:47 -0700282 unsigned long init_size = boot_params->hdr.init_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700283 u64 initrd_start, initrd_size;
284 u64 cmd_line, cmd_line_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700285 char *ptr;
286
287 /*
288 * Avoid the region that is unsafe to overlap during
Yinghai Lu9dc19692016-05-05 15:13:47 -0700289 * decompression.
Kees Cook82fa9632013-10-10 17:18:16 -0700290 */
Kees Cooked09acd2016-05-06 12:44:59 -0700291 mem_avoid[MEM_AVOID_ZO_RANGE].start = input;
292 mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input;
Kees Cook3a947072016-05-06 15:01:35 -0700293 add_identity_map(mem_avoid[MEM_AVOID_ZO_RANGE].start,
294 mem_avoid[MEM_AVOID_ZO_RANGE].size);
Kees Cook82fa9632013-10-10 17:18:16 -0700295
296 /* Avoid initrd. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700297 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
298 initrd_start |= boot_params->hdr.ramdisk_image;
299 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
300 initrd_size |= boot_params->hdr.ramdisk_size;
Kees Cooked09acd2016-05-06 12:44:59 -0700301 mem_avoid[MEM_AVOID_INITRD].start = initrd_start;
302 mem_avoid[MEM_AVOID_INITRD].size = initrd_size;
Kees Cook3a947072016-05-06 15:01:35 -0700303 /* No need to set mapping for initrd, it will be handled in VO. */
Kees Cook82fa9632013-10-10 17:18:16 -0700304
305 /* Avoid kernel command line. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700306 cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
307 cmd_line |= boot_params->hdr.cmd_line_ptr;
Kees Cook82fa9632013-10-10 17:18:16 -0700308 /* Calculate size of cmd_line. */
309 ptr = (char *)(unsigned long)cmd_line;
310 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
311 ;
Kees Cooked09acd2016-05-06 12:44:59 -0700312 mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
313 mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
Kees Cook3a947072016-05-06 15:01:35 -0700314 add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start,
315 mem_avoid[MEM_AVOID_CMDLINE].size);
Kees Cook82fa9632013-10-10 17:18:16 -0700316
Kees Cooked09acd2016-05-06 12:44:59 -0700317 /* Avoid boot parameters. */
318 mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
319 mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params);
Kees Cook3a947072016-05-06 15:01:35 -0700320 add_identity_map(mem_avoid[MEM_AVOID_BOOTPARAMS].start,
321 mem_avoid[MEM_AVOID_BOOTPARAMS].size);
322
323 /* We don't need to set a mapping for setup_data. */
324
Dave Jiangf2844242017-01-11 16:20:01 -0700325 /* Mark the memmap regions we need to avoid */
326 mem_avoid_memmap();
327
Kees Cook3a947072016-05-06 15:01:35 -0700328#ifdef CONFIG_X86_VERBOSE_BOOTUP
329 /* Make sure video RAM can be used. */
330 add_identity_map(0, PMD_SIZE);
331#endif
Kees Cook82fa9632013-10-10 17:18:16 -0700332}
333
Kees Cook06486d62016-05-09 13:22:07 -0700334/*
335 * Does this memory vector overlap a known avoided area? If so, record the
336 * overlap region with the lowest address.
337 */
338static bool mem_avoid_overlap(struct mem_vector *img,
339 struct mem_vector *overlap)
Kees Cook82fa9632013-10-10 17:18:16 -0700340{
341 int i;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700342 struct setup_data *ptr;
Kees Cook06486d62016-05-09 13:22:07 -0700343 unsigned long earliest = img->start + img->size;
344 bool is_overlapping = false;
Kees Cook82fa9632013-10-10 17:18:16 -0700345
346 for (i = 0; i < MEM_AVOID_MAX; i++) {
Kees Cook06486d62016-05-09 13:22:07 -0700347 if (mem_overlaps(img, &mem_avoid[i]) &&
348 mem_avoid[i].start < earliest) {
349 *overlap = mem_avoid[i];
Baoquan He6daa2ec2016-07-01 15:34:40 +0800350 earliest = overlap->start;
Kees Cook06486d62016-05-09 13:22:07 -0700351 is_overlapping = true;
352 }
Kees Cook82fa9632013-10-10 17:18:16 -0700353 }
354
Kees Cook0cacbfb2014-09-11 09:19:31 -0700355 /* Avoid all entries in the setup_data linked list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700356 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700357 while (ptr) {
358 struct mem_vector avoid;
359
Kees Cook20cc2882014-10-01 11:36:32 -0700360 avoid.start = (unsigned long)ptr;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700361 avoid.size = sizeof(*ptr) + ptr->len;
362
Kees Cook06486d62016-05-09 13:22:07 -0700363 if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
364 *overlap = avoid;
Baoquan He6daa2ec2016-07-01 15:34:40 +0800365 earliest = overlap->start;
Kees Cook06486d62016-05-09 13:22:07 -0700366 is_overlapping = true;
367 }
Kees Cook0cacbfb2014-09-11 09:19:31 -0700368
369 ptr = (struct setup_data *)(unsigned long)ptr->next;
370 }
371
Kees Cook06486d62016-05-09 13:22:07 -0700372 return is_overlapping;
Kees Cook82fa9632013-10-10 17:18:16 -0700373}
374
Baoquan Hec401cf12016-05-09 13:22:06 -0700375struct slot_area {
376 unsigned long addr;
377 int num;
378};
379
380#define MAX_SLOT_AREA 100
381
382static struct slot_area slot_areas[MAX_SLOT_AREA];
383
Kees Cooke290e8c2014-02-09 13:56:44 -0800384static unsigned long slot_max;
Kees Cook82fa9632013-10-10 17:18:16 -0700385
Baoquan Hec401cf12016-05-09 13:22:06 -0700386static unsigned long slot_area_index;
387
388static void store_slot_info(struct mem_vector *region, unsigned long image_size)
389{
390 struct slot_area slot_area;
391
392 if (slot_area_index == MAX_SLOT_AREA)
393 return;
394
395 slot_area.addr = region->start;
396 slot_area.num = (region->size - image_size) /
397 CONFIG_PHYSICAL_ALIGN + 1;
398
399 if (slot_area.num > 0) {
400 slot_areas[slot_area_index++] = slot_area;
401 slot_max += slot_area.num;
402 }
403}
404
Kees Cook82fa9632013-10-10 17:18:16 -0700405static unsigned long slots_fetch_random(void)
406{
Kees Cooked9f0072016-05-25 15:45:33 -0700407 unsigned long slot;
408 int i;
409
Kees Cook82fa9632013-10-10 17:18:16 -0700410 /* Handle case of no slots stored. */
411 if (slot_max == 0)
412 return 0;
413
Thomas Garnierd899a7d2016-06-21 17:46:58 -0700414 slot = kaslr_get_random_long("Physical") % slot_max;
Kees Cooked9f0072016-05-25 15:45:33 -0700415
416 for (i = 0; i < slot_area_index; i++) {
417 if (slot >= slot_areas[i].num) {
418 slot -= slot_areas[i].num;
419 continue;
420 }
421 return slot_areas[i].addr + slot * CONFIG_PHYSICAL_ALIGN;
422 }
423
424 if (i == slot_area_index)
425 debug_putstr("slots_fetch_random() failed!?\n");
426 return 0;
Kees Cook82fa9632013-10-10 17:18:16 -0700427}
428
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100429static void process_e820_entry(struct e820_entry *entry,
Kees Cook82fa9632013-10-10 17:18:16 -0700430 unsigned long minimum,
431 unsigned long image_size)
432{
Kees Cooked9f0072016-05-25 15:45:33 -0700433 struct mem_vector region, overlap;
434 struct slot_area slot_area;
435 unsigned long start_orig;
Kees Cook82fa9632013-10-10 17:18:16 -0700436
437 /* Skip non-RAM entries. */
438 if (entry->type != E820_RAM)
439 return;
440
Kees Cooked9f0072016-05-25 15:45:33 -0700441 /* On 32-bit, ignore entries entirely above our maximum. */
442 if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= KERNEL_IMAGE_SIZE)
Kees Cook82fa9632013-10-10 17:18:16 -0700443 return;
444
445 /* Ignore entries entirely below our minimum. */
446 if (entry->addr + entry->size < minimum)
447 return;
448
449 region.start = entry->addr;
450 region.size = entry->size;
451
Kees Cooked9f0072016-05-25 15:45:33 -0700452 /* Give up if slot area array is full. */
453 while (slot_area_index < MAX_SLOT_AREA) {
454 start_orig = region.start;
Kees Cook82fa9632013-10-10 17:18:16 -0700455
Kees Cooked9f0072016-05-25 15:45:33 -0700456 /* Potentially raise address to minimum location. */
457 if (region.start < minimum)
458 region.start = minimum;
Kees Cook82fa9632013-10-10 17:18:16 -0700459
Kees Cooked9f0072016-05-25 15:45:33 -0700460 /* Potentially raise address to meet alignment needs. */
461 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
Kees Cook82fa9632013-10-10 17:18:16 -0700462
Kees Cooked9f0072016-05-25 15:45:33 -0700463 /* Did we raise the address above this e820 region? */
464 if (region.start > entry->addr + entry->size)
465 return;
Kees Cook82fa9632013-10-10 17:18:16 -0700466
Kees Cooked9f0072016-05-25 15:45:33 -0700467 /* Reduce size by any delta from the original address. */
468 region.size -= region.start - start_orig;
Kees Cook82fa9632013-10-10 17:18:16 -0700469
Kees Cooked9f0072016-05-25 15:45:33 -0700470 /* On 32-bit, reduce region size to fit within max size. */
471 if (IS_ENABLED(CONFIG_X86_32) &&
472 region.start + region.size > KERNEL_IMAGE_SIZE)
473 region.size = KERNEL_IMAGE_SIZE - region.start;
474
475 /* Return if region can't contain decompressed kernel */
476 if (region.size < image_size)
477 return;
478
479 /* If nothing overlaps, store the region and return. */
480 if (!mem_avoid_overlap(&region, &overlap)) {
481 store_slot_info(&region, image_size);
482 return;
483 }
484
485 /* Store beginning of region if holds at least image_size. */
486 if (overlap.start > region.start + image_size) {
487 struct mem_vector beginning;
488
489 beginning.start = region.start;
490 beginning.size = overlap.start - region.start;
491 store_slot_info(&beginning, image_size);
492 }
493
494 /* Return if overlap extends to or past end of region. */
495 if (overlap.start + overlap.size >= region.start + region.size)
496 return;
497
498 /* Clip off the overlapping region and start over. */
499 region.size -= overlap.start - region.start + overlap.size;
500 region.start = overlap.start + overlap.size;
Kees Cook82fa9632013-10-10 17:18:16 -0700501 }
502}
503
Baoquan He071a7492016-05-09 13:22:08 -0700504static unsigned long find_random_phys_addr(unsigned long minimum,
505 unsigned long image_size)
Kees Cook82fa9632013-10-10 17:18:16 -0700506{
507 int i;
508 unsigned long addr;
509
Dave Jiangf2844242017-01-11 16:20:01 -0700510 /* Check if we had too many memmaps. */
511 if (memmap_too_large) {
512 debug_putstr("Aborted e820 scan (more than 4 memmap= args)!\n");
513 return 0;
514 }
515
Kees Cook82fa9632013-10-10 17:18:16 -0700516 /* Make sure minimum is aligned. */
517 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
518
519 /* Verify potential e820 positions, appending to slots list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700520 for (i = 0; i < boot_params->e820_entries; i++) {
Ingo Molnar61a50102017-01-27 13:54:38 +0100521 process_e820_entry(&boot_params->e820_table[i], minimum,
Baoquan He071a7492016-05-09 13:22:08 -0700522 image_size);
Kees Cooked9f0072016-05-25 15:45:33 -0700523 if (slot_area_index == MAX_SLOT_AREA) {
524 debug_putstr("Aborted e820 scan (slot_areas full)!\n");
525 break;
526 }
Kees Cook82fa9632013-10-10 17:18:16 -0700527 }
528
529 return slots_fetch_random();
530}
531
Baoquan He071a7492016-05-09 13:22:08 -0700532static unsigned long find_random_virt_addr(unsigned long minimum,
533 unsigned long image_size)
534{
535 unsigned long slots, random_addr;
536
537 /* Make sure minimum is aligned. */
538 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
539 /* Align image_size for easy slot calculations. */
540 image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
541
542 /*
543 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
544 * that can hold image_size within the range of minimum to
545 * KERNEL_IMAGE_SIZE?
546 */
547 slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
548 CONFIG_PHYSICAL_ALIGN + 1;
549
Thomas Garnierd899a7d2016-06-21 17:46:58 -0700550 random_addr = kaslr_get_random_long("Virtual") % slots;
Baoquan He071a7492016-05-09 13:22:08 -0700551
552 return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
553}
554
Borislav Petkov549f90d2016-05-06 13:50:15 +0200555/*
556 * Since this function examines addresses much more numerically,
557 * it takes the input and output pointers as 'unsigned long'.
558 */
Baoquan He8391c732016-05-25 15:45:32 -0700559void choose_random_location(unsigned long input,
560 unsigned long input_size,
561 unsigned long *output,
562 unsigned long output_size,
563 unsigned long *virt_addr)
Kees Cook8ab38202013-10-10 17:18:14 -0700564{
Yinghai Lue066cc42016-05-25 15:45:34 -0700565 unsigned long random_addr, min_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700566
Baoquan He8391c732016-05-25 15:45:32 -0700567 /* By default, keep output position unchanged. */
568 *virt_addr = *output;
569
Kees Cook24f2e022014-06-13 13:30:36 -0700570 if (cmdline_find_option_bool("nokaslr")) {
Kees Cook0f8ede1b2016-04-20 13:55:46 -0700571 warn("KASLR disabled: 'nokaslr' on cmdline.");
Baoquan He8391c732016-05-25 15:45:32 -0700572 return;
Kees Cook24f2e022014-06-13 13:30:36 -0700573 }
Kees Cook8ab38202013-10-10 17:18:14 -0700574
Kees Cook6655e0a2016-04-18 09:42:12 -0700575 boot_params->hdr.loadflags |= KASLR_FLAG;
Borislav Petkov78cac482015-04-01 12:49:52 +0200576
Kees Cook11fdf972016-05-25 15:45:31 -0700577 /* Prepare to add new identity pagetables on demand. */
578 initialize_identity_maps();
579
Kees Cook82fa9632013-10-10 17:18:16 -0700580 /* Record the various known unsafe memory ranges. */
Baoquan He8391c732016-05-25 15:45:32 -0700581 mem_avoid_init(input, input_size, *output);
Kees Cook8ab38202013-10-10 17:18:14 -0700582
Yinghai Lue066cc42016-05-25 15:45:34 -0700583 /*
584 * Low end of the randomization range should be the
585 * smaller of 512M or the initial kernel image
586 * location:
587 */
588 min_addr = min(*output, 512UL << 20);
589
Kees Cook82fa9632013-10-10 17:18:16 -0700590 /* Walk e820 and find a random address. */
Yinghai Lue066cc42016-05-25 15:45:34 -0700591 random_addr = find_random_phys_addr(min_addr, output_size);
Kees Cook90168752016-04-18 09:42:15 -0700592 if (!random_addr) {
Dave Jiangf2844242017-01-11 16:20:01 -0700593 warn("Physical KASLR disabled: no suitable memory region!");
Baoquan He8391c732016-05-25 15:45:32 -0700594 } else {
595 /* Update the new physical address location. */
596 if (*output != random_addr) {
597 add_identity_map(random_addr, output_size);
598 *output = random_addr;
599 }
Kees Cook82fa9632013-10-10 17:18:16 -0700600 }
601
Borislav Petkov36a39ac2016-05-07 11:59:40 +0200602 /* This actually loads the identity pagetable on x86_64. */
Kees Cook3a947072016-05-06 15:01:35 -0700603 finalize_identity_maps();
Baoquan He8391c732016-05-25 15:45:32 -0700604
605 /* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
606 if (IS_ENABLED(CONFIG_X86_64))
607 random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size);
608 *virt_addr = random_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700609}