blob: 3ad71a0afa242946b9aea4aace1318a978cee765 [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"
13
Kees Cook5bfce5e2013-10-10 17:18:15 -070014#include <asm/msr.h>
15#include <asm/archrandom.h>
Kees Cook82fa9632013-10-10 17:18:16 -070016#include <asm/e820.h>
Kees Cook5bfce5e2013-10-10 17:18:15 -070017
Kees Cooka653f352013-11-11 14:28:39 -080018#include <generated/compile.h>
19#include <linux/module.h>
20#include <linux/uts.h>
21#include <linux/utsname.h>
22#include <generated/utsrelease.h>
Kees Cooka653f352013-11-11 14:28:39 -080023
24/* Simplified build-specific string for starting entropy. */
Kees Cook327f7d72013-11-12 08:56:07 -080025static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
Kees Cooka653f352013-11-11 14:28:39 -080026 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
27
Kees Cook5bfce5e2013-10-10 17:18:15 -070028#define I8254_PORT_CONTROL 0x43
29#define I8254_PORT_COUNTER0 0x40
30#define I8254_CMD_READBACK 0xC0
31#define I8254_SELECT_COUNTER0 0x02
32#define I8254_STATUS_NOTREADY 0x40
33static inline u16 i8254(void)
34{
35 u16 status, timer;
36
37 do {
38 outb(I8254_PORT_CONTROL,
39 I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
40 status = inb(I8254_PORT_COUNTER0);
41 timer = inb(I8254_PORT_COUNTER0);
42 timer |= inb(I8254_PORT_COUNTER0) << 8;
43 } while (status & I8254_STATUS_NOTREADY);
44
45 return timer;
46}
47
Kees Cooka653f352013-11-11 14:28:39 -080048static unsigned long rotate_xor(unsigned long hash, const void *area,
49 size_t size)
50{
51 size_t i;
52 unsigned long *ptr = (unsigned long *)area;
53
54 for (i = 0; i < size / sizeof(hash); i++) {
55 /* Rotate by odd number of bits and XOR. */
56 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
57 hash ^= ptr[i];
58 }
59
60 return hash;
61}
62
63/* Attempt to create a simple but unpredictable starting entropy. */
64static unsigned long get_random_boot(void)
65{
66 unsigned long hash = 0;
67
68 hash = rotate_xor(hash, build_str, sizeof(build_str));
Kees Cook6655e0a2016-04-18 09:42:12 -070069 hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
Kees Cooka653f352013-11-11 14:28:39 -080070
71 return hash;
72}
73
Kees Cook5bfce5e2013-10-10 17:18:15 -070074static unsigned long get_random_long(void)
75{
H. Peter Anvine8236c42013-11-11 22:45:20 -080076#ifdef CONFIG_X86_64
77 const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
78#else
79 const unsigned long mix_const = 0x3f39e593UL;
80#endif
Kees Cooka653f352013-11-11 14:28:39 -080081 unsigned long raw, random = get_random_boot();
82 bool use_i8254 = true;
83
84 debug_putstr("KASLR using");
Kees Cook5bfce5e2013-10-10 17:18:15 -070085
86 if (has_cpuflag(X86_FEATURE_RDRAND)) {
Kees Cooka653f352013-11-11 14:28:39 -080087 debug_putstr(" RDRAND");
88 if (rdrand_long(&raw)) {
89 random ^= raw;
90 use_i8254 = false;
91 }
Kees Cook5bfce5e2013-10-10 17:18:15 -070092 }
93
94 if (has_cpuflag(X86_FEATURE_TSC)) {
Kees Cooka653f352013-11-11 14:28:39 -080095 debug_putstr(" RDTSC");
Andy Lutomirski4ea16362015-06-25 18:44:07 +020096 raw = rdtsc();
Kees Cook5bfce5e2013-10-10 17:18:15 -070097
Kees Cooka653f352013-11-11 14:28:39 -080098 random ^= raw;
99 use_i8254 = false;
Kees Cook5bfce5e2013-10-10 17:18:15 -0700100 }
101
Kees Cooka653f352013-11-11 14:28:39 -0800102 if (use_i8254) {
103 debug_putstr(" i8254");
104 random ^= i8254();
105 }
106
H. Peter Anvine8236c42013-11-11 22:45:20 -0800107 /* Circular multiply for better bit diffusion */
108 asm("mul %3"
109 : "=a" (random), "=d" (raw)
110 : "a" (random), "rm" (mix_const));
111 random += raw;
112
Kees Cooka653f352013-11-11 14:28:39 -0800113 debug_putstr("...\n");
114
Kees Cook5bfce5e2013-10-10 17:18:15 -0700115 return random;
116}
Kees Cook8ab38202013-10-10 17:18:14 -0700117
Kees Cook82fa9632013-10-10 17:18:16 -0700118struct mem_vector {
119 unsigned long start;
120 unsigned long size;
121};
122
123#define MEM_AVOID_MAX 5
Kees Cooke290e8c2014-02-09 13:56:44 -0800124static struct mem_vector mem_avoid[MEM_AVOID_MAX];
Kees Cook82fa9632013-10-10 17:18:16 -0700125
126static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
127{
128 /* Item at least partially before region. */
129 if (item->start < region->start)
130 return false;
131 /* Item at least partially after region. */
132 if (item->start + item->size > region->start + region->size)
133 return false;
134 return true;
135}
136
137static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
138{
139 /* Item one is entirely before item two. */
140 if (one->start + one->size <= two->start)
141 return false;
142 /* Item one is entirely after item two. */
143 if (one->start >= two->start + two->size)
144 return false;
145 return true;
146}
147
148static void mem_avoid_init(unsigned long input, unsigned long input_size,
149 unsigned long output, unsigned long output_size)
150{
151 u64 initrd_start, initrd_size;
152 u64 cmd_line, cmd_line_size;
153 unsigned long unsafe, unsafe_len;
154 char *ptr;
155
156 /*
157 * Avoid the region that is unsafe to overlap during
Baoquan He4252db12016-04-20 13:55:42 -0700158 * decompression (see calculations in ../header.S).
Kees Cook82fa9632013-10-10 17:18:16 -0700159 */
160 unsafe_len = (output_size >> 12) + 32768 + 18;
161 unsafe = (unsigned long)input + input_size - unsafe_len;
162 mem_avoid[0].start = unsafe;
163 mem_avoid[0].size = unsafe_len;
164
165 /* Avoid initrd. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700166 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
167 initrd_start |= boot_params->hdr.ramdisk_image;
168 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
169 initrd_size |= boot_params->hdr.ramdisk_size;
Kees Cook82fa9632013-10-10 17:18:16 -0700170 mem_avoid[1].start = initrd_start;
171 mem_avoid[1].size = initrd_size;
172
173 /* Avoid kernel command line. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700174 cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
175 cmd_line |= boot_params->hdr.cmd_line_ptr;
Kees Cook82fa9632013-10-10 17:18:16 -0700176 /* Calculate size of cmd_line. */
177 ptr = (char *)(unsigned long)cmd_line;
178 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
179 ;
180 mem_avoid[2].start = cmd_line;
181 mem_avoid[2].size = cmd_line_size;
182
183 /* Avoid heap memory. */
184 mem_avoid[3].start = (unsigned long)free_mem_ptr;
185 mem_avoid[3].size = BOOT_HEAP_SIZE;
186
187 /* Avoid stack memory. */
188 mem_avoid[4].start = (unsigned long)free_mem_end_ptr;
189 mem_avoid[4].size = BOOT_STACK_SIZE;
190}
191
192/* Does this memory vector overlap a known avoided area? */
Kees Cooke290e8c2014-02-09 13:56:44 -0800193static bool mem_avoid_overlap(struct mem_vector *img)
Kees Cook82fa9632013-10-10 17:18:16 -0700194{
195 int i;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700196 struct setup_data *ptr;
Kees Cook82fa9632013-10-10 17:18:16 -0700197
198 for (i = 0; i < MEM_AVOID_MAX; i++) {
199 if (mem_overlaps(img, &mem_avoid[i]))
200 return true;
201 }
202
Kees Cook0cacbfb2014-09-11 09:19:31 -0700203 /* Avoid all entries in the setup_data linked list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700204 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700205 while (ptr) {
206 struct mem_vector avoid;
207
Kees Cook20cc2882014-10-01 11:36:32 -0700208 avoid.start = (unsigned long)ptr;
Kees Cook0cacbfb2014-09-11 09:19:31 -0700209 avoid.size = sizeof(*ptr) + ptr->len;
210
211 if (mem_overlaps(img, &avoid))
212 return true;
213
214 ptr = (struct setup_data *)(unsigned long)ptr->next;
215 }
216
Kees Cook82fa9632013-10-10 17:18:16 -0700217 return false;
218}
219
Baoquan Hee8581e32016-04-20 13:55:43 -0700220static unsigned long slots[KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN];
Kees Cooke290e8c2014-02-09 13:56:44 -0800221static unsigned long slot_max;
Kees Cook82fa9632013-10-10 17:18:16 -0700222
223static void slots_append(unsigned long addr)
224{
225 /* Overflowing the slots list should be impossible. */
Baoquan Hee8581e32016-04-20 13:55:43 -0700226 if (slot_max >= KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN)
Kees Cook82fa9632013-10-10 17:18:16 -0700227 return;
228
229 slots[slot_max++] = addr;
230}
231
232static unsigned long slots_fetch_random(void)
233{
234 /* Handle case of no slots stored. */
235 if (slot_max == 0)
236 return 0;
237
238 return slots[get_random_long() % slot_max];
239}
240
241static void process_e820_entry(struct e820entry *entry,
242 unsigned long minimum,
243 unsigned long image_size)
244{
245 struct mem_vector region, img;
246
247 /* Skip non-RAM entries. */
248 if (entry->type != E820_RAM)
249 return;
250
251 /* Ignore entries entirely above our maximum. */
Baoquan Hee8581e32016-04-20 13:55:43 -0700252 if (entry->addr >= KERNEL_IMAGE_SIZE)
Kees Cook82fa9632013-10-10 17:18:16 -0700253 return;
254
255 /* Ignore entries entirely below our minimum. */
256 if (entry->addr + entry->size < minimum)
257 return;
258
259 region.start = entry->addr;
260 region.size = entry->size;
261
262 /* Potentially raise address to minimum location. */
263 if (region.start < minimum)
264 region.start = minimum;
265
266 /* Potentially raise address to meet alignment requirements. */
267 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
268
269 /* Did we raise the address above the bounds of this e820 region? */
270 if (region.start > entry->addr + entry->size)
271 return;
272
273 /* Reduce size by any delta from the original address. */
274 region.size -= region.start - entry->addr;
275
276 /* Reduce maximum size to fit end of image within maximum limit. */
Baoquan Hee8581e32016-04-20 13:55:43 -0700277 if (region.start + region.size > KERNEL_IMAGE_SIZE)
278 region.size = KERNEL_IMAGE_SIZE - region.start;
Kees Cook82fa9632013-10-10 17:18:16 -0700279
280 /* Walk each aligned slot and check for avoided areas. */
281 for (img.start = region.start, img.size = image_size ;
282 mem_contains(&region, &img) ;
283 img.start += CONFIG_PHYSICAL_ALIGN) {
284 if (mem_avoid_overlap(&img))
285 continue;
286 slots_append(img.start);
287 }
288}
289
290static unsigned long find_random_addr(unsigned long minimum,
291 unsigned long size)
292{
293 int i;
294 unsigned long addr;
295
296 /* Make sure minimum is aligned. */
297 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
298
299 /* Verify potential e820 positions, appending to slots list. */
Kees Cook6655e0a2016-04-18 09:42:12 -0700300 for (i = 0; i < boot_params->e820_entries; i++) {
301 process_e820_entry(&boot_params->e820_map[i], minimum, size);
Kees Cook82fa9632013-10-10 17:18:16 -0700302 }
303
304 return slots_fetch_random();
305}
306
Kees Cook7de828d2016-04-18 09:42:14 -0700307unsigned char *choose_random_location(unsigned char *input,
Kees Cook8ab38202013-10-10 17:18:14 -0700308 unsigned long input_size,
309 unsigned char *output,
310 unsigned long output_size)
311{
312 unsigned long choice = (unsigned long)output;
Kees Cook90168752016-04-18 09:42:15 -0700313 unsigned long random_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700314
Kees Cook24f2e022014-06-13 13:30:36 -0700315#ifdef CONFIG_HIBERNATION
316 if (!cmdline_find_option_bool("kaslr")) {
317 debug_putstr("KASLR disabled by default...\n");
Kees Cook8ab38202013-10-10 17:18:14 -0700318 goto out;
319 }
Kees Cook24f2e022014-06-13 13:30:36 -0700320#else
321 if (cmdline_find_option_bool("nokaslr")) {
322 debug_putstr("KASLR disabled by cmdline...\n");
323 goto out;
324 }
325#endif
Kees Cook8ab38202013-10-10 17:18:14 -0700326
Kees Cook6655e0a2016-04-18 09:42:12 -0700327 boot_params->hdr.loadflags |= KASLR_FLAG;
Borislav Petkov78cac482015-04-01 12:49:52 +0200328
Kees Cook82fa9632013-10-10 17:18:16 -0700329 /* Record the various known unsafe memory ranges. */
330 mem_avoid_init((unsigned long)input, input_size,
331 (unsigned long)output, output_size);
Kees Cook8ab38202013-10-10 17:18:14 -0700332
Kees Cook82fa9632013-10-10 17:18:16 -0700333 /* Walk e820 and find a random address. */
Kees Cook90168752016-04-18 09:42:15 -0700334 random_addr = find_random_addr(choice, output_size);
335 if (!random_addr) {
Kees Cook82fa9632013-10-10 17:18:16 -0700336 debug_putstr("KASLR could not find suitable E820 region...\n");
337 goto out;
338 }
339
340 /* Always enforce the minimum. */
Kees Cook90168752016-04-18 09:42:15 -0700341 if (random_addr < choice)
Kees Cook82fa9632013-10-10 17:18:16 -0700342 goto out;
343
Kees Cook90168752016-04-18 09:42:15 -0700344 choice = random_addr;
Kees Cook8ab38202013-10-10 17:18:14 -0700345out:
346 return (unsigned char *)choice;
347}