blob: ff1227746c72acede9785f38afa0ad5ae7dbc321 [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 Cook5bfce5e2013-10-10 17:18:15 -070075static unsigned long get_random_long(void)
76{
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
85 debug_putstr("KASLR using");
Kees Cook5bfce5e2013-10-10 17:18:15 -070086
87 if (has_cpuflag(X86_FEATURE_RDRAND)) {
Kees Cooka653f352013-11-11 14:28:39 -080088 debug_putstr(" RDRAND");
89 if (rdrand_long(&raw)) {
90 random ^= raw;
91 use_i8254 = false;
92 }
Kees Cook5bfce5e2013-10-10 17:18:15 -070093 }
94
95 if (has_cpuflag(X86_FEATURE_TSC)) {
Kees Cooka653f352013-11-11 14:28:39 -080096 debug_putstr(" RDTSC");
Andy Lutomirski4ea16362015-06-25 18:44:07 +020097 raw = rdtsc();
Kees Cook5bfce5e2013-10-10 17:18:15 -070098
Kees Cooka653f352013-11-11 14:28:39 -080099 random ^= raw;
100 use_i8254 = false;
Kees Cook5bfce5e2013-10-10 17:18:15 -0700101 }
102
Kees Cooka653f352013-11-11 14:28:39 -0800103 if (use_i8254) {
104 debug_putstr(" i8254");
105 random ^= i8254();
106 }
107
H. Peter Anvine8236c42013-11-11 22:45:20 -0800108 /* Circular multiply for better bit diffusion */
109 asm("mul %3"
110 : "=a" (random), "=d" (raw)
111 : "a" (random), "rm" (mix_const));
112 random += raw;
113
Kees Cooka653f352013-11-11 14:28:39 -0800114 debug_putstr("...\n");
115
Kees Cook5bfce5e2013-10-10 17:18:15 -0700116 return random;
117}
Kees Cook8ab38202013-10-10 17:18:14 -0700118
Kees Cook82fa9632013-10-10 17:18:16 -0700119struct mem_vector {
120 unsigned long start;
121 unsigned long size;
122};
123
Yinghai Lu9dc19692016-05-05 15:13:47 -0700124#define MEM_AVOID_MAX 4
Kees Cooke290e8c2014-02-09 13:56:44 -0800125static struct mem_vector mem_avoid[MEM_AVOID_MAX];
Kees Cook82fa9632013-10-10 17:18:16 -0700126
127static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
128{
129 /* Item at least partially before region. */
130 if (item->start < region->start)
131 return false;
132 /* Item at least partially after region. */
133 if (item->start + item->size > region->start + region->size)
134 return false;
135 return true;
136}
137
138static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
139{
140 /* Item one is entirely before item two. */
141 if (one->start + one->size <= two->start)
142 return false;
143 /* Item one is entirely after item two. */
144 if (one->start >= two->start + two->size)
145 return false;
146 return true;
147}
148
Yinghai Lu9dc19692016-05-05 15:13:47 -0700149/*
150 * In theroy, KASLR can put the kernel anywhere in area of [16M, 64T). The
151 * mem_avoid array is used to store the ranges that need to be avoided when
152 * KASLR searches for a an appropriate random address. We must avoid any
153 * regions that are unsafe to overlap with during decompression, and other
154 * things like the initrd, cmdline and boot_params.
155 *
156 * How to calculate the unsafe areas is detailed here, and is informed by
157 * the decompression calculations in header.S, and the diagram in misc.c.
158 *
159 * The compressed vmlinux (ZO) plus relocs and the run space of ZO can't be
160 * overwritten by decompression output.
161 *
162 * ZO sits against the end of the decompression buffer, so we can calculate
163 * where text, data, bss, etc of ZO are positioned.
164 *
165 * The follow are already enforced by the code:
166 * - init_size >= kernel_total_size
167 * - input + input_len >= output + output_len
168 * - kernel_total_size could be >= or < output_len
169 *
170 * From this, we can make several observations, illustrated by a diagram:
171 * - init_size >= kernel_total_size
172 * - input + input_len > output + output_len
173 * - kernel_total_size >= output_len
174 *
175 * 0 output input input+input_len output+init_size
176 * | | | | |
177 * | | | | |
178 * |-----|--------|--------|------------------|----|------------|----------|
179 * | | |
180 * | | |
181 * output+init_size-ZO_INIT_SIZE output+output_len output+kernel_total_size
182 *
183 * [output, output+init_size) is for the buffer for decompressing the
184 * compressed kernel (ZO).
185 *
186 * [output, output+kernel_total_size) is for the uncompressed kernel (VO)
187 * and its bss, brk, etc.
188 * [output, output+output_len) is VO plus relocs
189 *
190 * [output+init_size-ZO_INIT_SIZE, output+init_size) is the copied ZO.
191 * [input, input+input_len) is the copied compressed (VO (vmlinux after
192 * objcopy) plus relocs), not the ZO.
193 *
194 * [input+input_len, output+init_size) is [_text, _end) for ZO. That was the
195 * first range in mem_avoid, which included ZO's heap and stack. Also
196 * [input, input+input_size) need be put in mem_avoid array, but since it
197 * is adjacent to the first entry, they can be merged. This is how we get
198 * the first entry in mem_avoid[].
199 */
Kees Cook82fa9632013-10-10 17:18:16 -0700200static void mem_avoid_init(unsigned long input, unsigned long input_size,
Yinghai Lu9dc19692016-05-05 15:13:47 -0700201 unsigned long output)
Kees Cook82fa9632013-10-10 17:18:16 -0700202{
Yinghai Lu9dc19692016-05-05 15:13:47 -0700203 unsigned long init_size = boot_params->hdr.init_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700204 u64 initrd_start, initrd_size;
205 u64 cmd_line, cmd_line_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700206 char *ptr;
207
208 /*
209 * Avoid the region that is unsafe to overlap during
Yinghai Lu9dc19692016-05-05 15:13:47 -0700210 * decompression.
Kees Cook82fa9632013-10-10 17:18:16 -0700211 */
Yinghai Lu9dc19692016-05-05 15:13:47 -0700212 mem_avoid[0].start = input;
213 mem_avoid[0].size = (output + init_size) - input;
Kees Cook82fa9632013-10-10 17:18:16 -0700214
215 /* Avoid initrd. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700216 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
217 initrd_start |= boot_params->hdr.ramdisk_image;
218 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
219 initrd_size |= boot_params->hdr.ramdisk_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700220 mem_avoid[1].start = initrd_start;
221 mem_avoid[1].size = initrd_size;
222
223 /* Avoid kernel command line. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700224 cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
225 cmd_line |= boot_params->hdr.cmd_line_ptr;
Kees Cook82fa9632013-10-10 17:18:16 -0700226 /* Calculate size of cmd_line. */
227 ptr = (char *)(unsigned long)cmd_line;
228 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
229 ;
230 mem_avoid[2].start = cmd_line;
231 mem_avoid[2].size = cmd_line_size;
232
Yinghai Lu9dc19692016-05-05 15:13:47 -0700233 /* Avoid params */
234 mem_avoid[3].start = (unsigned long)boot_params;
235 mem_avoid[3].size = sizeof(*boot_params);
Kees Cook82fa9632013-10-10 17:18:16 -0700236}
237
238/* Does this memory vector overlap a known avoided area? */
Kees Cooke290e8c2014-02-09 13:56:44 -0800239static bool mem_avoid_overlap(struct mem_vector *img)
Kees Cook82fa9632013-10-10 17:18:16 -0700240{
241 int i;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700242 struct setup_data *ptr;
Kees Cook82fa9632013-10-10 17:18:16 -0700243
244 for (i = 0; i < MEM_AVOID_MAX; i++) {
245 if (mem_overlaps(img, &mem_avoid[i]))
246 return true;
247 }
248
Kees Cook0cacbfb2014-09-11 09:19:31 -0700249 /* Avoid all entries in the setup_data linked list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700250 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700251 while (ptr) {
252 struct mem_vector avoid;
253
Kees Cook20cc2882014-10-01 11:36:32 -0700254 avoid.start = (unsigned long)ptr;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700255 avoid.size = sizeof(*ptr) + ptr->len;
256
257 if (mem_overlaps(img, &avoid))
258 return true;
259
260 ptr = (struct setup_data *)(unsigned long)ptr->next;
261 }
262
Kees Cook82fa9632013-10-10 17:18:16 -0700263 return false;
264}
265
Baoquan Hee8581e32016-04-20 13:55:43 -0700266static unsigned long slots[KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN];
Kees Cooke290e8c2014-02-09 13:56:44 -0800267static unsigned long slot_max;
Kees Cook82fa9632013-10-10 17:18:16 -0700268
269static void slots_append(unsigned long addr)
270{
271 /* Overflowing the slots list should be impossible. */
Baoquan Hee8581e32016-04-20 13:55:43 -0700272 if (slot_max >= KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN)
Kees Cook82fa9632013-10-10 17:18:16 -0700273 return;
274
275 slots[slot_max++] = addr;
276}
277
278static unsigned long slots_fetch_random(void)
279{
280 /* Handle case of no slots stored. */
281 if (slot_max == 0)
282 return 0;
283
284 return slots[get_random_long() % slot_max];
285}
286
287static void process_e820_entry(struct e820entry *entry,
288 unsigned long minimum,
289 unsigned long image_size)
290{
291 struct mem_vector region, img;
292
293 /* Skip non-RAM entries. */
294 if (entry->type != E820_RAM)
295 return;
296
297 /* Ignore entries entirely above our maximum. */
Baoquan Hee8581e32016-04-20 13:55:43 -0700298 if (entry->addr >= KERNEL_IMAGE_SIZE)
Kees Cook82fa9632013-10-10 17:18:16 -0700299 return;
300
301 /* Ignore entries entirely below our minimum. */
302 if (entry->addr + entry->size < minimum)
303 return;
304
305 region.start = entry->addr;
306 region.size = entry->size;
307
308 /* Potentially raise address to minimum location. */
309 if (region.start < minimum)
310 region.start = minimum;
311
312 /* Potentially raise address to meet alignment requirements. */
313 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
314
315 /* Did we raise the address above the bounds of this e820 region? */
316 if (region.start > entry->addr + entry->size)
317 return;
318
319 /* Reduce size by any delta from the original address. */
320 region.size -= region.start - entry->addr;
321
322 /* Reduce maximum size to fit end of image within maximum limit. */
Baoquan Hee8581e32016-04-20 13:55:43 -0700323 if (region.start + region.size > KERNEL_IMAGE_SIZE)
324 region.size = KERNEL_IMAGE_SIZE - region.start;
Kees Cook82fa9632013-10-10 17:18:16 -0700325
326 /* Walk each aligned slot and check for avoided areas. */
327 for (img.start = region.start, img.size = image_size ;
328 mem_contains(&region, &img) ;
329 img.start += CONFIG_PHYSICAL_ALIGN) {
330 if (mem_avoid_overlap(&img))
331 continue;
332 slots_append(img.start);
333 }
334}
335
336static unsigned long find_random_addr(unsigned long minimum,
337 unsigned long size)
338{
339 int i;
340 unsigned long addr;
341
342 /* Make sure minimum is aligned. */
343 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
344
345 /* Verify potential e820 positions, appending to slots list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700346 for (i = 0; i < boot_params->e820_entries; i++) {
347 process_e820_entry(&boot_params->e820_map[i], minimum, size);
Kees Cook82fa9632013-10-10 17:18:16 -0700348 }
349
350 return slots_fetch_random();
351}
352
Borislav Petkov549f90d2016-05-06 13:50:15 +0200353/*
354 * Since this function examines addresses much more numerically,
355 * it takes the input and output pointers as 'unsigned long'.
356 */
357unsigned char *choose_random_location(unsigned long input,
Kees Cook8ab38202013-10-10 17:18:14 -0700358 unsigned long input_size,
Borislav Petkov549f90d2016-05-06 13:50:15 +0200359 unsigned long output,
Kees Cook8ab38202013-10-10 17:18:14 -0700360 unsigned long output_size)
361{
Kees Cook2bc1cd32016-05-05 15:13:46 -0700362 unsigned long choice = output;
Kees Cook90168752016-04-18 09:42:15 -0700363 unsigned long random_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700364
Kees Cook24f2e022014-06-13 13:30:36 -0700365#ifdef CONFIG_HIBERNATION
366 if (!cmdline_find_option_bool("kaslr")) {
Kees Cook0f8ede1b2016-04-20 13:55:46 -0700367 warn("KASLR disabled: 'kaslr' not on cmdline (hibernation selected).");
Kees Cook8ab38202013-10-10 17:18:14 -0700368 goto out;
369 }
Kees Cook24f2e022014-06-13 13:30:36 -0700370#else
371 if (cmdline_find_option_bool("nokaslr")) {
Kees Cook0f8ede1b2016-04-20 13:55:46 -0700372 warn("KASLR disabled: 'nokaslr' on cmdline.");
Kees Cook24f2e022014-06-13 13:30:36 -0700373 goto out;
374 }
375#endif
Kees Cook8ab38202013-10-10 17:18:14 -0700376
Kees Cook6655e0a2016-04-18 09:42:12 -0700377 boot_params->hdr.loadflags |= KASLR_FLAG;
Borislav Petkov78cac482015-04-01 12:49:52 +0200378
Kees Cook82fa9632013-10-10 17:18:16 -0700379 /* Record the various known unsafe memory ranges. */
Yinghai Lu9dc19692016-05-05 15:13:47 -0700380 mem_avoid_init(input, input_size, output);
Kees Cook8ab38202013-10-10 17:18:14 -0700381
Kees Cook82fa9632013-10-10 17:18:16 -0700382 /* Walk e820 and find a random address. */
Kees Cook2bc1cd32016-05-05 15:13:46 -0700383 random_addr = find_random_addr(output, output_size);
Kees Cook90168752016-04-18 09:42:15 -0700384 if (!random_addr) {
Kees Cook0f8ede1b2016-04-20 13:55:46 -0700385 warn("KASLR disabled: could not find suitable E820 region!");
Kees Cook82fa9632013-10-10 17:18:16 -0700386 goto out;
387 }
388
389 /* Always enforce the minimum. */
Kees Cook90168752016-04-18 09:42:15 -0700390 if (random_addr < choice)
Kees Cook82fa9632013-10-10 17:18:16 -0700391 goto out;
392
Kees Cook90168752016-04-18 09:42:15 -0700393 choice = random_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700394out:
395 return (unsigned char *)choice;
396}