blob: 010ea16e5f770f720ce7056f73438ae5ba2f21b2 [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"
Kees Cook8ab38202013-10-10 17:18:14 -070014
Kees Cook5bfce5e2013-10-10 17:18:15 -070015#include <asm/msr.h>
16#include <asm/archrandom.h>
Kees Cook82fa9632013-10-10 17:18:16 -070017#include <asm/e820.h>
Kees Cook5bfce5e2013-10-10 17:18:15 -070018
Kees Cooka653f352013-11-11 14:28:39 -080019#include <generated/compile.h>
20#include <linux/module.h>
21#include <linux/uts.h>
22#include <linux/utsname.h>
23#include <generated/utsrelease.h>
Kees Cooka653f352013-11-11 14:28:39 -080024
25/* Simplified build-specific string for starting entropy. */
Kees Cook327f7d72013-11-12 08:56:07 -080026static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
Kees Cooka653f352013-11-11 14:28:39 -080027 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
28
Kees Cook5bfce5e2013-10-10 17:18:15 -070029#define I8254_PORT_CONTROL 0x43
30#define I8254_PORT_COUNTER0 0x40
31#define I8254_CMD_READBACK 0xC0
32#define I8254_SELECT_COUNTER0 0x02
33#define I8254_STATUS_NOTREADY 0x40
34static inline u16 i8254(void)
35{
36 u16 status, timer;
37
38 do {
39 outb(I8254_PORT_CONTROL,
40 I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
41 status = inb(I8254_PORT_COUNTER0);
42 timer = inb(I8254_PORT_COUNTER0);
43 timer |= inb(I8254_PORT_COUNTER0) << 8;
44 } while (status & I8254_STATUS_NOTREADY);
45
46 return timer;
47}
48
Kees Cooka653f352013-11-11 14:28:39 -080049static unsigned long rotate_xor(unsigned long hash, const void *area,
50 size_t size)
51{
52 size_t i;
53 unsigned long *ptr = (unsigned long *)area;
54
55 for (i = 0; i < size / sizeof(hash); i++) {
56 /* Rotate by odd number of bits and XOR. */
57 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
58 hash ^= ptr[i];
59 }
60
61 return hash;
62}
63
64/* Attempt to create a simple but unpredictable starting entropy. */
65static unsigned long get_random_boot(void)
66{
67 unsigned long hash = 0;
68
69 hash = rotate_xor(hash, build_str, sizeof(build_str));
Kees Cook6655e0a2016-04-18 09:42:12 -070070 hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
Kees Cooka653f352013-11-11 14:28:39 -080071
72 return hash;
73}
74
Kees Cookd2d34622016-05-09 13:22:09 -070075static unsigned long get_random_long(const char *purpose)
Kees Cook5bfce5e2013-10-10 17:18:15 -070076{
H. Peter Anvine8236c42013-11-11 22:45:20 -080077#ifdef CONFIG_X86_64
78 const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
79#else
80 const unsigned long mix_const = 0x3f39e593UL;
81#endif
Kees Cooka653f352013-11-11 14:28:39 -080082 unsigned long raw, random = get_random_boot();
83 bool use_i8254 = true;
84
Kees Cookd2d34622016-05-09 13:22:09 -070085 debug_putstr(purpose);
86 debug_putstr(" KASLR using");
Kees Cook5bfce5e2013-10-10 17:18:15 -070087
88 if (has_cpuflag(X86_FEATURE_RDRAND)) {
Kees Cooka653f352013-11-11 14:28:39 -080089 debug_putstr(" RDRAND");
90 if (rdrand_long(&raw)) {
91 random ^= raw;
92 use_i8254 = false;
93 }
Kees Cook5bfce5e2013-10-10 17:18:15 -070094 }
95
96 if (has_cpuflag(X86_FEATURE_TSC)) {
Kees Cooka653f352013-11-11 14:28:39 -080097 debug_putstr(" RDTSC");
Andy Lutomirski4ea16362015-06-25 18:44:07 +020098 raw = rdtsc();
Kees Cook5bfce5e2013-10-10 17:18:15 -070099
Kees Cooka653f352013-11-11 14:28:39 -0800100 random ^= raw;
101 use_i8254 = false;
Kees Cook5bfce5e2013-10-10 17:18:15 -0700102 }
103
Kees Cooka653f352013-11-11 14:28:39 -0800104 if (use_i8254) {
105 debug_putstr(" i8254");
106 random ^= i8254();
107 }
108
H. Peter Anvine8236c42013-11-11 22:45:20 -0800109 /* Circular multiply for better bit diffusion */
110 asm("mul %3"
111 : "=a" (random), "=d" (raw)
112 : "a" (random), "rm" (mix_const));
113 random += raw;
114
Kees Cooka653f352013-11-11 14:28:39 -0800115 debug_putstr("...\n");
116
Kees Cook5bfce5e2013-10-10 17:18:15 -0700117 return random;
118}
Kees Cook8ab38202013-10-10 17:18:14 -0700119
Kees Cook82fa9632013-10-10 17:18:16 -0700120struct mem_vector {
121 unsigned long start;
122 unsigned long size;
123};
124
Kees Cooked09acd2016-05-06 12:44:59 -0700125enum mem_avoid_index {
126 MEM_AVOID_ZO_RANGE = 0,
127 MEM_AVOID_INITRD,
128 MEM_AVOID_CMDLINE,
129 MEM_AVOID_BOOTPARAMS,
130 MEM_AVOID_MAX,
131};
132
Kees Cooke290e8c2014-02-09 13:56:44 -0800133static struct mem_vector mem_avoid[MEM_AVOID_MAX];
Kees Cook82fa9632013-10-10 17:18:16 -0700134
Kees Cook82fa9632013-10-10 17:18:16 -0700135static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
136{
137 /* Item one is entirely before item two. */
138 if (one->start + one->size <= two->start)
139 return false;
140 /* Item one is entirely after item two. */
141 if (one->start >= two->start + two->size)
142 return false;
143 return true;
144}
145
Yinghai Lu9dc19692016-05-05 15:13:47 -0700146/*
Kees Cooked09acd2016-05-06 12:44:59 -0700147 * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T).
148 * The mem_avoid array is used to store the ranges that need to be avoided
149 * when KASLR searches for an appropriate random address. We must avoid any
Yinghai Lu9dc19692016-05-05 15:13:47 -0700150 * regions that are unsafe to overlap with during decompression, and other
Kees Cooked09acd2016-05-06 12:44:59 -0700151 * things like the initrd, cmdline and boot_params. This comment seeks to
152 * explain mem_avoid as clearly as possible since incorrect mem_avoid
153 * memory ranges lead to really hard to debug boot failures.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700154 *
Kees Cooked09acd2016-05-06 12:44:59 -0700155 * The initrd, cmdline, and boot_params are trivial to identify for
Kees Cookcb18ef02016-05-09 13:22:05 -0700156 * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and
Kees Cooked09acd2016-05-06 12:44:59 -0700157 * MEM_AVOID_BOOTPARAMS respectively below.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700158 *
Kees Cooked09acd2016-05-06 12:44:59 -0700159 * What is not obvious how to avoid is the range of memory that is used
160 * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover
161 * the compressed kernel (ZO) and its run space, which is used to extract
162 * the uncompressed kernel (VO) and relocs.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700163 *
Kees Cooked09acd2016-05-06 12:44:59 -0700164 * ZO's full run size sits against the end of the decompression buffer, so
165 * we can calculate where text, data, bss, etc of ZO are positioned more
166 * easily.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700167 *
Kees Cooked09acd2016-05-06 12:44:59 -0700168 * For additional background, the decompression calculations can be found
169 * in header.S, and the memory diagram is based on the one found in misc.c.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700170 *
Kees Cooked09acd2016-05-06 12:44:59 -0700171 * The following conditions are already enforced by the image layouts and
172 * associated code:
173 * - input + input_size >= output + output_size
174 * - kernel_total_size <= init_size
175 * - kernel_total_size <= output_size (see Note below)
176 * - output + init_size >= output + output_size
Yinghai Lu9dc19692016-05-05 15:13:47 -0700177 *
Kees Cooked09acd2016-05-06 12:44:59 -0700178 * (Note that kernel_total_size and output_size have no fundamental
179 * relationship, but output_size is passed to choose_random_location
180 * as a maximum of the two. The diagram is showing a case where
181 * kernel_total_size is larger than output_size, but this case is
182 * handled by bumping output_size.)
Yinghai Lu9dc19692016-05-05 15:13:47 -0700183 *
Kees Cooked09acd2016-05-06 12:44:59 -0700184 * The above conditions can be illustrated by a diagram:
Yinghai Lu9dc19692016-05-05 15:13:47 -0700185 *
Kees Cooked09acd2016-05-06 12:44:59 -0700186 * 0 output input input+input_size output+init_size
187 * | | | | |
188 * | | | | |
189 * |-----|--------|--------|--------------|-----------|--|-------------|
190 * | | |
191 * | | |
192 * output+init_size-ZO_INIT_SIZE output+output_size output+kernel_total_size
Yinghai Lu9dc19692016-05-05 15:13:47 -0700193 *
Kees Cooked09acd2016-05-06 12:44:59 -0700194 * [output, output+init_size) is the entire memory range used for
195 * extracting the compressed image.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700196 *
Kees Cooked09acd2016-05-06 12:44:59 -0700197 * [output, output+kernel_total_size) is the range needed for the
198 * uncompressed kernel (VO) and its run size (bss, brk, etc).
199 *
200 * [output, output+output_size) is VO plus relocs (i.e. the entire
201 * uncompressed payload contained by ZO). This is the area of the buffer
202 * written to during decompression.
203 *
204 * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case
205 * range of the copied ZO and decompression code. (i.e. the range
206 * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.)
207 *
208 * [input, input+input_size) is the original copied compressed image (ZO)
209 * (i.e. it does not include its run size). This range must be avoided
210 * because it contains the data used for decompression.
211 *
212 * [input+input_size, output+init_size) is [_text, _end) for ZO. This
213 * range includes ZO's heap and stack, and must be avoided since it
214 * performs the decompression.
215 *
216 * Since the above two ranges need to be avoided and they are adjacent,
217 * they can be merged, resulting in: [input, output+init_size) which
218 * becomes the MEM_AVOID_ZO_RANGE below.
Yinghai Lu9dc19692016-05-05 15:13:47 -0700219 */
Kees Cook82fa9632013-10-10 17:18:16 -0700220static void mem_avoid_init(unsigned long input, unsigned long input_size,
Yinghai Lu9dc19692016-05-05 15:13:47 -0700221 unsigned long output)
Kees Cook82fa9632013-10-10 17:18:16 -0700222{
Yinghai Lu9dc19692016-05-05 15:13:47 -0700223 unsigned long init_size = boot_params->hdr.init_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700224 u64 initrd_start, initrd_size;
225 u64 cmd_line, cmd_line_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700226 char *ptr;
227
228 /*
229 * Avoid the region that is unsafe to overlap during
Yinghai Lu9dc19692016-05-05 15:13:47 -0700230 * decompression.
Kees Cook82fa9632013-10-10 17:18:16 -0700231 */
Kees Cooked09acd2016-05-06 12:44:59 -0700232 mem_avoid[MEM_AVOID_ZO_RANGE].start = input;
233 mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input;
Kees Cook3a947072016-05-06 15:01:35 -0700234 add_identity_map(mem_avoid[MEM_AVOID_ZO_RANGE].start,
235 mem_avoid[MEM_AVOID_ZO_RANGE].size);
Kees Cook82fa9632013-10-10 17:18:16 -0700236
237 /* Avoid initrd. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700238 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
239 initrd_start |= boot_params->hdr.ramdisk_image;
240 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
241 initrd_size |= boot_params->hdr.ramdisk_size;
Kees Cooked09acd2016-05-06 12:44:59 -0700242 mem_avoid[MEM_AVOID_INITRD].start = initrd_start;
243 mem_avoid[MEM_AVOID_INITRD].size = initrd_size;
Kees Cook3a947072016-05-06 15:01:35 -0700244 /* No need to set mapping for initrd, it will be handled in VO. */
Kees Cook82fa9632013-10-10 17:18:16 -0700245
246 /* Avoid kernel command line. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700247 cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
248 cmd_line |= boot_params->hdr.cmd_line_ptr;
Kees Cook82fa9632013-10-10 17:18:16 -0700249 /* Calculate size of cmd_line. */
250 ptr = (char *)(unsigned long)cmd_line;
251 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
252 ;
Kees Cooked09acd2016-05-06 12:44:59 -0700253 mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
254 mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
Kees Cook3a947072016-05-06 15:01:35 -0700255 add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start,
256 mem_avoid[MEM_AVOID_CMDLINE].size);
Kees Cook82fa9632013-10-10 17:18:16 -0700257
Kees Cooked09acd2016-05-06 12:44:59 -0700258 /* Avoid boot parameters. */
259 mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
260 mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params);
Kees Cook3a947072016-05-06 15:01:35 -0700261 add_identity_map(mem_avoid[MEM_AVOID_BOOTPARAMS].start,
262 mem_avoid[MEM_AVOID_BOOTPARAMS].size);
263
264 /* We don't need to set a mapping for setup_data. */
265
266#ifdef CONFIG_X86_VERBOSE_BOOTUP
267 /* Make sure video RAM can be used. */
268 add_identity_map(0, PMD_SIZE);
269#endif
Kees Cook82fa9632013-10-10 17:18:16 -0700270}
271
Kees Cook06486d62016-05-09 13:22:07 -0700272/*
273 * Does this memory vector overlap a known avoided area? If so, record the
274 * overlap region with the lowest address.
275 */
276static bool mem_avoid_overlap(struct mem_vector *img,
277 struct mem_vector *overlap)
Kees Cook82fa9632013-10-10 17:18:16 -0700278{
279 int i;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700280 struct setup_data *ptr;
Kees Cook06486d62016-05-09 13:22:07 -0700281 unsigned long earliest = img->start + img->size;
282 bool is_overlapping = false;
Kees Cook82fa9632013-10-10 17:18:16 -0700283
284 for (i = 0; i < MEM_AVOID_MAX; i++) {
Kees Cook06486d62016-05-09 13:22:07 -0700285 if (mem_overlaps(img, &mem_avoid[i]) &&
286 mem_avoid[i].start < earliest) {
287 *overlap = mem_avoid[i];
Baoquan He6daa2ec2016-07-01 15:34:40 +0800288 earliest = overlap->start;
Kees Cook06486d62016-05-09 13:22:07 -0700289 is_overlapping = true;
290 }
Kees Cook82fa9632013-10-10 17:18:16 -0700291 }
292
Kees Cook0cacbfb2014-09-11 09:19:31 -0700293 /* Avoid all entries in the setup_data linked list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700294 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700295 while (ptr) {
296 struct mem_vector avoid;
297
Kees Cook20cc2882014-10-01 11:36:32 -0700298 avoid.start = (unsigned long)ptr;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700299 avoid.size = sizeof(*ptr) + ptr->len;
300
Kees Cook06486d62016-05-09 13:22:07 -0700301 if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
302 *overlap = avoid;
Baoquan He6daa2ec2016-07-01 15:34:40 +0800303 earliest = overlap->start;
Kees Cook06486d62016-05-09 13:22:07 -0700304 is_overlapping = true;
305 }
Kees Cook0cacbfb2014-09-11 09:19:31 -0700306
307 ptr = (struct setup_data *)(unsigned long)ptr->next;
308 }
309
Kees Cook06486d62016-05-09 13:22:07 -0700310 return is_overlapping;
Kees Cook82fa9632013-10-10 17:18:16 -0700311}
312
Baoquan Hec401cf12016-05-09 13:22:06 -0700313struct slot_area {
314 unsigned long addr;
315 int num;
316};
317
318#define MAX_SLOT_AREA 100
319
320static struct slot_area slot_areas[MAX_SLOT_AREA];
321
Kees Cooke290e8c2014-02-09 13:56:44 -0800322static unsigned long slot_max;
Kees Cook82fa9632013-10-10 17:18:16 -0700323
Baoquan Hec401cf12016-05-09 13:22:06 -0700324static unsigned long slot_area_index;
325
326static void store_slot_info(struct mem_vector *region, unsigned long image_size)
327{
328 struct slot_area slot_area;
329
330 if (slot_area_index == MAX_SLOT_AREA)
331 return;
332
333 slot_area.addr = region->start;
334 slot_area.num = (region->size - image_size) /
335 CONFIG_PHYSICAL_ALIGN + 1;
336
337 if (slot_area.num > 0) {
338 slot_areas[slot_area_index++] = slot_area;
339 slot_max += slot_area.num;
340 }
341}
342
Kees Cook82fa9632013-10-10 17:18:16 -0700343static unsigned long slots_fetch_random(void)
344{
Kees Cooked9f0072016-05-25 15:45:33 -0700345 unsigned long slot;
346 int i;
347
Kees Cook82fa9632013-10-10 17:18:16 -0700348 /* Handle case of no slots stored. */
349 if (slot_max == 0)
350 return 0;
351
Kees Cooked9f0072016-05-25 15:45:33 -0700352 slot = get_random_long("Physical") % slot_max;
353
354 for (i = 0; i < slot_area_index; i++) {
355 if (slot >= slot_areas[i].num) {
356 slot -= slot_areas[i].num;
357 continue;
358 }
359 return slot_areas[i].addr + slot * CONFIG_PHYSICAL_ALIGN;
360 }
361
362 if (i == slot_area_index)
363 debug_putstr("slots_fetch_random() failed!?\n");
364 return 0;
Kees Cook82fa9632013-10-10 17:18:16 -0700365}
366
367static void process_e820_entry(struct e820entry *entry,
368 unsigned long minimum,
369 unsigned long image_size)
370{
Kees Cooked9f0072016-05-25 15:45:33 -0700371 struct mem_vector region, overlap;
372 struct slot_area slot_area;
373 unsigned long start_orig;
Kees Cook82fa9632013-10-10 17:18:16 -0700374
375 /* Skip non-RAM entries. */
376 if (entry->type != E820_RAM)
377 return;
378
Kees Cooked9f0072016-05-25 15:45:33 -0700379 /* On 32-bit, ignore entries entirely above our maximum. */
380 if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= KERNEL_IMAGE_SIZE)
Kees Cook82fa9632013-10-10 17:18:16 -0700381 return;
382
383 /* Ignore entries entirely below our minimum. */
384 if (entry->addr + entry->size < minimum)
385 return;
386
387 region.start = entry->addr;
388 region.size = entry->size;
389
Kees Cooked9f0072016-05-25 15:45:33 -0700390 /* Give up if slot area array is full. */
391 while (slot_area_index < MAX_SLOT_AREA) {
392 start_orig = region.start;
Kees Cook82fa9632013-10-10 17:18:16 -0700393
Kees Cooked9f0072016-05-25 15:45:33 -0700394 /* Potentially raise address to minimum location. */
395 if (region.start < minimum)
396 region.start = minimum;
Kees Cook82fa9632013-10-10 17:18:16 -0700397
Kees Cooked9f0072016-05-25 15:45:33 -0700398 /* Potentially raise address to meet alignment needs. */
399 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
Kees Cook82fa9632013-10-10 17:18:16 -0700400
Kees Cooked9f0072016-05-25 15:45:33 -0700401 /* Did we raise the address above this e820 region? */
402 if (region.start > entry->addr + entry->size)
403 return;
Kees Cook82fa9632013-10-10 17:18:16 -0700404
Kees Cooked9f0072016-05-25 15:45:33 -0700405 /* Reduce size by any delta from the original address. */
406 region.size -= region.start - start_orig;
Kees Cook82fa9632013-10-10 17:18:16 -0700407
Kees Cooked9f0072016-05-25 15:45:33 -0700408 /* On 32-bit, reduce region size to fit within max size. */
409 if (IS_ENABLED(CONFIG_X86_32) &&
410 region.start + region.size > KERNEL_IMAGE_SIZE)
411 region.size = KERNEL_IMAGE_SIZE - region.start;
412
413 /* Return if region can't contain decompressed kernel */
414 if (region.size < image_size)
415 return;
416
417 /* If nothing overlaps, store the region and return. */
418 if (!mem_avoid_overlap(&region, &overlap)) {
419 store_slot_info(&region, image_size);
420 return;
421 }
422
423 /* Store beginning of region if holds at least image_size. */
424 if (overlap.start > region.start + image_size) {
425 struct mem_vector beginning;
426
427 beginning.start = region.start;
428 beginning.size = overlap.start - region.start;
429 store_slot_info(&beginning, image_size);
430 }
431
432 /* Return if overlap extends to or past end of region. */
433 if (overlap.start + overlap.size >= region.start + region.size)
434 return;
435
436 /* Clip off the overlapping region and start over. */
437 region.size -= overlap.start - region.start + overlap.size;
438 region.start = overlap.start + overlap.size;
Kees Cook82fa9632013-10-10 17:18:16 -0700439 }
440}
441
Baoquan He071a7492016-05-09 13:22:08 -0700442static unsigned long find_random_phys_addr(unsigned long minimum,
443 unsigned long image_size)
Kees Cook82fa9632013-10-10 17:18:16 -0700444{
445 int i;
446 unsigned long addr;
447
448 /* Make sure minimum is aligned. */
449 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
450
451 /* Verify potential e820 positions, appending to slots list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700452 for (i = 0; i < boot_params->e820_entries; i++) {
Baoquan He071a7492016-05-09 13:22:08 -0700453 process_e820_entry(&boot_params->e820_map[i], minimum,
454 image_size);
Kees Cooked9f0072016-05-25 15:45:33 -0700455 if (slot_area_index == MAX_SLOT_AREA) {
456 debug_putstr("Aborted e820 scan (slot_areas full)!\n");
457 break;
458 }
Kees Cook82fa9632013-10-10 17:18:16 -0700459 }
460
461 return slots_fetch_random();
462}
463
Baoquan He071a7492016-05-09 13:22:08 -0700464static unsigned long find_random_virt_addr(unsigned long minimum,
465 unsigned long image_size)
466{
467 unsigned long slots, random_addr;
468
469 /* Make sure minimum is aligned. */
470 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
471 /* Align image_size for easy slot calculations. */
472 image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
473
474 /*
475 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
476 * that can hold image_size within the range of minimum to
477 * KERNEL_IMAGE_SIZE?
478 */
479 slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
480 CONFIG_PHYSICAL_ALIGN + 1;
481
Kees Cookd2d34622016-05-09 13:22:09 -0700482 random_addr = get_random_long("Virtual") % slots;
Baoquan He071a7492016-05-09 13:22:08 -0700483
484 return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
485}
486
Borislav Petkov549f90d2016-05-06 13:50:15 +0200487/*
488 * Since this function examines addresses much more numerically,
489 * it takes the input and output pointers as 'unsigned long'.
490 */
Baoquan He8391c732016-05-25 15:45:32 -0700491void choose_random_location(unsigned long input,
492 unsigned long input_size,
493 unsigned long *output,
494 unsigned long output_size,
495 unsigned long *virt_addr)
Kees Cook8ab38202013-10-10 17:18:14 -0700496{
Yinghai Lue066cc42016-05-25 15:45:34 -0700497 unsigned long random_addr, min_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700498
Baoquan He8391c732016-05-25 15:45:32 -0700499 /* By default, keep output position unchanged. */
500 *virt_addr = *output;
501
Kees Cook24f2e022014-06-13 13:30:36 -0700502 if (cmdline_find_option_bool("nokaslr")) {
Kees Cook0f8ede1b2016-04-20 13:55:46 -0700503 warn("KASLR disabled: 'nokaslr' on cmdline.");
Baoquan He8391c732016-05-25 15:45:32 -0700504 return;
Kees Cook24f2e022014-06-13 13:30:36 -0700505 }
Kees Cook8ab38202013-10-10 17:18:14 -0700506
Kees Cook6655e0a2016-04-18 09:42:12 -0700507 boot_params->hdr.loadflags |= KASLR_FLAG;
Borislav Petkov78cac482015-04-01 12:49:52 +0200508
Kees Cook11fdf972016-05-25 15:45:31 -0700509 /* Prepare to add new identity pagetables on demand. */
510 initialize_identity_maps();
511
Kees Cook82fa9632013-10-10 17:18:16 -0700512 /* Record the various known unsafe memory ranges. */
Baoquan He8391c732016-05-25 15:45:32 -0700513 mem_avoid_init(input, input_size, *output);
Kees Cook8ab38202013-10-10 17:18:14 -0700514
Yinghai Lue066cc42016-05-25 15:45:34 -0700515 /*
516 * Low end of the randomization range should be the
517 * smaller of 512M or the initial kernel image
518 * location:
519 */
520 min_addr = min(*output, 512UL << 20);
521
Kees Cook82fa9632013-10-10 17:18:16 -0700522 /* Walk e820 and find a random address. */
Yinghai Lue066cc42016-05-25 15:45:34 -0700523 random_addr = find_random_phys_addr(min_addr, output_size);
Kees Cook90168752016-04-18 09:42:15 -0700524 if (!random_addr) {
Kees Cook0f8ede1b2016-04-20 13:55:46 -0700525 warn("KASLR disabled: could not find suitable E820 region!");
Baoquan He8391c732016-05-25 15:45:32 -0700526 } else {
527 /* Update the new physical address location. */
528 if (*output != random_addr) {
529 add_identity_map(random_addr, output_size);
530 *output = random_addr;
531 }
Kees Cook82fa9632013-10-10 17:18:16 -0700532 }
533
Borislav Petkov36a39ac2016-05-07 11:59:40 +0200534 /* This actually loads the identity pagetable on x86_64. */
Kees Cook3a947072016-05-06 15:01:35 -0700535 finalize_identity_maps();
Baoquan He8391c732016-05-25 15:45:32 -0700536
537 /* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
538 if (IS_ENABLED(CONFIG_X86_64))
539 random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size);
540 *virt_addr = random_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700541}