Kees Cook | 7de828d | 2016-04-18 09:42:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 12 | #include "misc.h" |
Kees Cook | dc425a6 | 2016-05-02 15:51:00 -0700 | [diff] [blame] | 13 | #include "error.h" |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 14 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 15 | #include <asm/msr.h> |
| 16 | #include <asm/archrandom.h> |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 17 | #include <asm/e820.h> |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 18 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 19 | #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 Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 24 | |
| 25 | /* Simplified build-specific string for starting entropy. */ |
Kees Cook | 327f7d7 | 2013-11-12 08:56:07 -0800 | [diff] [blame] | 26 | static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@" |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 27 | LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION; |
| 28 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 29 | #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 |
| 34 | static 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 Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 49 | static 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. */ |
| 65 | static unsigned long get_random_boot(void) |
| 66 | { |
| 67 | unsigned long hash = 0; |
| 68 | |
| 69 | hash = rotate_xor(hash, build_str, sizeof(build_str)); |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 70 | hash = rotate_xor(hash, boot_params, sizeof(*boot_params)); |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 71 | |
| 72 | return hash; |
| 73 | } |
| 74 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 75 | static unsigned long get_random_long(void) |
| 76 | { |
H. Peter Anvin | e8236c4 | 2013-11-11 22:45:20 -0800 | [diff] [blame] | 77 | #ifdef CONFIG_X86_64 |
| 78 | const unsigned long mix_const = 0x5d6008cbf3848dd3UL; |
| 79 | #else |
| 80 | const unsigned long mix_const = 0x3f39e593UL; |
| 81 | #endif |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 82 | unsigned long raw, random = get_random_boot(); |
| 83 | bool use_i8254 = true; |
| 84 | |
| 85 | debug_putstr("KASLR using"); |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 86 | |
| 87 | if (has_cpuflag(X86_FEATURE_RDRAND)) { |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 88 | debug_putstr(" RDRAND"); |
| 89 | if (rdrand_long(&raw)) { |
| 90 | random ^= raw; |
| 91 | use_i8254 = false; |
| 92 | } |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (has_cpuflag(X86_FEATURE_TSC)) { |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 96 | debug_putstr(" RDTSC"); |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 97 | raw = rdtsc(); |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 98 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 99 | random ^= raw; |
| 100 | use_i8254 = false; |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 103 | if (use_i8254) { |
| 104 | debug_putstr(" i8254"); |
| 105 | random ^= i8254(); |
| 106 | } |
| 107 | |
H. Peter Anvin | e8236c4 | 2013-11-11 22:45:20 -0800 | [diff] [blame] | 108 | /* 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 Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 114 | debug_putstr("...\n"); |
| 115 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 116 | return random; |
| 117 | } |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 118 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 119 | struct mem_vector { |
| 120 | unsigned long start; |
| 121 | unsigned long size; |
| 122 | }; |
| 123 | |
| 124 | #define MEM_AVOID_MAX 5 |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 125 | static struct mem_vector mem_avoid[MEM_AVOID_MAX]; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 126 | |
| 127 | static 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 | |
| 138 | static 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 | |
| 149 | static void mem_avoid_init(unsigned long input, unsigned long input_size, |
| 150 | unsigned long output, unsigned long output_size) |
| 151 | { |
| 152 | u64 initrd_start, initrd_size; |
| 153 | u64 cmd_line, cmd_line_size; |
| 154 | unsigned long unsafe, unsafe_len; |
| 155 | char *ptr; |
| 156 | |
| 157 | /* |
| 158 | * Avoid the region that is unsafe to overlap during |
Baoquan He | 4252db1 | 2016-04-20 13:55:42 -0700 | [diff] [blame] | 159 | * decompression (see calculations in ../header.S). |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 160 | */ |
| 161 | unsafe_len = (output_size >> 12) + 32768 + 18; |
| 162 | unsafe = (unsigned long)input + input_size - unsafe_len; |
| 163 | mem_avoid[0].start = unsafe; |
| 164 | mem_avoid[0].size = unsafe_len; |
| 165 | |
| 166 | /* Avoid initrd. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 167 | initrd_start = (u64)boot_params->ext_ramdisk_image << 32; |
| 168 | initrd_start |= boot_params->hdr.ramdisk_image; |
| 169 | initrd_size = (u64)boot_params->ext_ramdisk_size << 32; |
| 170 | initrd_size |= boot_params->hdr.ramdisk_size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 171 | mem_avoid[1].start = initrd_start; |
| 172 | mem_avoid[1].size = initrd_size; |
| 173 | |
| 174 | /* Avoid kernel command line. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 175 | cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32; |
| 176 | cmd_line |= boot_params->hdr.cmd_line_ptr; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 177 | /* Calculate size of cmd_line. */ |
| 178 | ptr = (char *)(unsigned long)cmd_line; |
| 179 | for (cmd_line_size = 0; ptr[cmd_line_size++]; ) |
| 180 | ; |
| 181 | mem_avoid[2].start = cmd_line; |
| 182 | mem_avoid[2].size = cmd_line_size; |
| 183 | |
| 184 | /* Avoid heap memory. */ |
| 185 | mem_avoid[3].start = (unsigned long)free_mem_ptr; |
| 186 | mem_avoid[3].size = BOOT_HEAP_SIZE; |
| 187 | |
| 188 | /* Avoid stack memory. */ |
| 189 | mem_avoid[4].start = (unsigned long)free_mem_end_ptr; |
| 190 | mem_avoid[4].size = BOOT_STACK_SIZE; |
| 191 | } |
| 192 | |
| 193 | /* Does this memory vector overlap a known avoided area? */ |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 194 | static bool mem_avoid_overlap(struct mem_vector *img) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 195 | { |
| 196 | int i; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 197 | struct setup_data *ptr; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 198 | |
| 199 | for (i = 0; i < MEM_AVOID_MAX; i++) { |
| 200 | if (mem_overlaps(img, &mem_avoid[i])) |
| 201 | return true; |
| 202 | } |
| 203 | |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 204 | /* Avoid all entries in the setup_data linked list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 205 | ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 206 | while (ptr) { |
| 207 | struct mem_vector avoid; |
| 208 | |
Kees Cook | 20cc288 | 2014-10-01 11:36:32 -0700 | [diff] [blame] | 209 | avoid.start = (unsigned long)ptr; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 210 | avoid.size = sizeof(*ptr) + ptr->len; |
| 211 | |
| 212 | if (mem_overlaps(img, &avoid)) |
| 213 | return true; |
| 214 | |
| 215 | ptr = (struct setup_data *)(unsigned long)ptr->next; |
| 216 | } |
| 217 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 221 | static unsigned long slots[KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN]; |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 222 | static unsigned long slot_max; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 223 | |
| 224 | static void slots_append(unsigned long addr) |
| 225 | { |
| 226 | /* Overflowing the slots list should be impossible. */ |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 227 | if (slot_max >= KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 228 | return; |
| 229 | |
| 230 | slots[slot_max++] = addr; |
| 231 | } |
| 232 | |
| 233 | static unsigned long slots_fetch_random(void) |
| 234 | { |
| 235 | /* Handle case of no slots stored. */ |
| 236 | if (slot_max == 0) |
| 237 | return 0; |
| 238 | |
| 239 | return slots[get_random_long() % slot_max]; |
| 240 | } |
| 241 | |
| 242 | static void process_e820_entry(struct e820entry *entry, |
| 243 | unsigned long minimum, |
| 244 | unsigned long image_size) |
| 245 | { |
| 246 | struct mem_vector region, img; |
| 247 | |
| 248 | /* Skip non-RAM entries. */ |
| 249 | if (entry->type != E820_RAM) |
| 250 | return; |
| 251 | |
| 252 | /* Ignore entries entirely above our maximum. */ |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 253 | if (entry->addr >= KERNEL_IMAGE_SIZE) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 254 | return; |
| 255 | |
| 256 | /* Ignore entries entirely below our minimum. */ |
| 257 | if (entry->addr + entry->size < minimum) |
| 258 | return; |
| 259 | |
| 260 | region.start = entry->addr; |
| 261 | region.size = entry->size; |
| 262 | |
| 263 | /* Potentially raise address to minimum location. */ |
| 264 | if (region.start < minimum) |
| 265 | region.start = minimum; |
| 266 | |
| 267 | /* Potentially raise address to meet alignment requirements. */ |
| 268 | region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); |
| 269 | |
| 270 | /* Did we raise the address above the bounds of this e820 region? */ |
| 271 | if (region.start > entry->addr + entry->size) |
| 272 | return; |
| 273 | |
| 274 | /* Reduce size by any delta from the original address. */ |
| 275 | region.size -= region.start - entry->addr; |
| 276 | |
| 277 | /* Reduce maximum size to fit end of image within maximum limit. */ |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 278 | if (region.start + region.size > KERNEL_IMAGE_SIZE) |
| 279 | region.size = KERNEL_IMAGE_SIZE - region.start; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 280 | |
| 281 | /* Walk each aligned slot and check for avoided areas. */ |
| 282 | for (img.start = region.start, img.size = image_size ; |
| 283 | mem_contains(®ion, &img) ; |
| 284 | img.start += CONFIG_PHYSICAL_ALIGN) { |
| 285 | if (mem_avoid_overlap(&img)) |
| 286 | continue; |
| 287 | slots_append(img.start); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static unsigned long find_random_addr(unsigned long minimum, |
| 292 | unsigned long size) |
| 293 | { |
| 294 | int i; |
| 295 | unsigned long addr; |
| 296 | |
| 297 | /* Make sure minimum is aligned. */ |
| 298 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); |
| 299 | |
| 300 | /* Verify potential e820 positions, appending to slots list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 301 | for (i = 0; i < boot_params->e820_entries; i++) { |
| 302 | process_e820_entry(&boot_params->e820_map[i], minimum, size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | return slots_fetch_random(); |
| 306 | } |
| 307 | |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame^] | 308 | unsigned char *choose_random_location(unsigned char *input_ptr, |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 309 | unsigned long input_size, |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame^] | 310 | unsigned char *output_ptr, |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 311 | unsigned long output_size) |
| 312 | { |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame^] | 313 | /* |
| 314 | * The caller of choose_random_location() uses unsigned char * for |
| 315 | * buffer pointers since it performs decompression, elf parsing, etc. |
| 316 | * Since this code examines addresses much more numerically, |
| 317 | * unsigned long is used internally here. Instead of sprinkling |
| 318 | * more casts into extract_kernel, do them here and at return. |
| 319 | */ |
| 320 | unsigned long input = (unsigned long)input_ptr; |
| 321 | unsigned long output = (unsigned long)output_ptr; |
| 322 | unsigned long choice = output; |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 323 | unsigned long random_addr; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 324 | |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 325 | #ifdef CONFIG_HIBERNATION |
| 326 | if (!cmdline_find_option_bool("kaslr")) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 327 | warn("KASLR disabled: 'kaslr' not on cmdline (hibernation selected)."); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 328 | goto out; |
| 329 | } |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 330 | #else |
| 331 | if (cmdline_find_option_bool("nokaslr")) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 332 | warn("KASLR disabled: 'nokaslr' on cmdline."); |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 333 | goto out; |
| 334 | } |
| 335 | #endif |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 336 | |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 337 | boot_params->hdr.loadflags |= KASLR_FLAG; |
Borislav Petkov | 78cac48 | 2015-04-01 12:49:52 +0200 | [diff] [blame] | 338 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 339 | /* Record the various known unsafe memory ranges. */ |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame^] | 340 | mem_avoid_init(input, input_size, output, output_size); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 341 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 342 | /* Walk e820 and find a random address. */ |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame^] | 343 | random_addr = find_random_addr(output, output_size); |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 344 | if (!random_addr) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 345 | warn("KASLR disabled: could not find suitable E820 region!"); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 346 | goto out; |
| 347 | } |
| 348 | |
| 349 | /* Always enforce the minimum. */ |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 350 | if (random_addr < choice) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 351 | goto out; |
| 352 | |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 353 | choice = random_addr; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 354 | out: |
| 355 | return (unsigned char *)choice; |
| 356 | } |