Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 1 | #include "misc.h" |
| 2 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 3 | #include <asm/msr.h> |
| 4 | #include <asm/archrandom.h> |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 5 | #include <asm/e820.h> |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 6 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 7 | #include <generated/compile.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/uts.h> |
| 10 | #include <linux/utsname.h> |
| 11 | #include <generated/utsrelease.h> |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 12 | |
| 13 | /* Simplified build-specific string for starting entropy. */ |
Kees Cook | 327f7d7 | 2013-11-12 08:56:07 -0800 | [diff] [blame] | 14 | static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@" |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 15 | LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION; |
| 16 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 17 | #define I8254_PORT_CONTROL 0x43 |
| 18 | #define I8254_PORT_COUNTER0 0x40 |
| 19 | #define I8254_CMD_READBACK 0xC0 |
| 20 | #define I8254_SELECT_COUNTER0 0x02 |
| 21 | #define I8254_STATUS_NOTREADY 0x40 |
| 22 | static inline u16 i8254(void) |
| 23 | { |
| 24 | u16 status, timer; |
| 25 | |
| 26 | do { |
| 27 | outb(I8254_PORT_CONTROL, |
| 28 | I8254_CMD_READBACK | I8254_SELECT_COUNTER0); |
| 29 | status = inb(I8254_PORT_COUNTER0); |
| 30 | timer = inb(I8254_PORT_COUNTER0); |
| 31 | timer |= inb(I8254_PORT_COUNTER0) << 8; |
| 32 | } while (status & I8254_STATUS_NOTREADY); |
| 33 | |
| 34 | return timer; |
| 35 | } |
| 36 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 37 | static unsigned long rotate_xor(unsigned long hash, const void *area, |
| 38 | size_t size) |
| 39 | { |
| 40 | size_t i; |
| 41 | unsigned long *ptr = (unsigned long *)area; |
| 42 | |
| 43 | for (i = 0; i < size / sizeof(hash); i++) { |
| 44 | /* Rotate by odd number of bits and XOR. */ |
| 45 | hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7); |
| 46 | hash ^= ptr[i]; |
| 47 | } |
| 48 | |
| 49 | return hash; |
| 50 | } |
| 51 | |
| 52 | /* Attempt to create a simple but unpredictable starting entropy. */ |
| 53 | static unsigned long get_random_boot(void) |
| 54 | { |
| 55 | unsigned long hash = 0; |
| 56 | |
| 57 | hash = rotate_xor(hash, build_str, sizeof(build_str)); |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 58 | hash = rotate_xor(hash, boot_params, sizeof(*boot_params)); |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 59 | |
| 60 | return hash; |
| 61 | } |
| 62 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 63 | static unsigned long get_random_long(void) |
| 64 | { |
H. Peter Anvin | e8236c4 | 2013-11-11 22:45:20 -0800 | [diff] [blame] | 65 | #ifdef CONFIG_X86_64 |
| 66 | const unsigned long mix_const = 0x5d6008cbf3848dd3UL; |
| 67 | #else |
| 68 | const unsigned long mix_const = 0x3f39e593UL; |
| 69 | #endif |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 70 | unsigned long raw, random = get_random_boot(); |
| 71 | bool use_i8254 = true; |
| 72 | |
| 73 | debug_putstr("KASLR using"); |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 74 | |
| 75 | if (has_cpuflag(X86_FEATURE_RDRAND)) { |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 76 | debug_putstr(" RDRAND"); |
| 77 | if (rdrand_long(&raw)) { |
| 78 | random ^= raw; |
| 79 | use_i8254 = false; |
| 80 | } |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (has_cpuflag(X86_FEATURE_TSC)) { |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 84 | debug_putstr(" RDTSC"); |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 85 | raw = rdtsc(); |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 86 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 87 | random ^= raw; |
| 88 | use_i8254 = false; |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 91 | if (use_i8254) { |
| 92 | debug_putstr(" i8254"); |
| 93 | random ^= i8254(); |
| 94 | } |
| 95 | |
H. Peter Anvin | e8236c4 | 2013-11-11 22:45:20 -0800 | [diff] [blame] | 96 | /* Circular multiply for better bit diffusion */ |
| 97 | asm("mul %3" |
| 98 | : "=a" (random), "=d" (raw) |
| 99 | : "a" (random), "rm" (mix_const)); |
| 100 | random += raw; |
| 101 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 102 | debug_putstr("...\n"); |
| 103 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 104 | return random; |
| 105 | } |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 106 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 107 | struct mem_vector { |
| 108 | unsigned long start; |
| 109 | unsigned long size; |
| 110 | }; |
| 111 | |
| 112 | #define MEM_AVOID_MAX 5 |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 113 | static struct mem_vector mem_avoid[MEM_AVOID_MAX]; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 114 | |
| 115 | static bool mem_contains(struct mem_vector *region, struct mem_vector *item) |
| 116 | { |
| 117 | /* Item at least partially before region. */ |
| 118 | if (item->start < region->start) |
| 119 | return false; |
| 120 | /* Item at least partially after region. */ |
| 121 | if (item->start + item->size > region->start + region->size) |
| 122 | return false; |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two) |
| 127 | { |
| 128 | /* Item one is entirely before item two. */ |
| 129 | if (one->start + one->size <= two->start) |
| 130 | return false; |
| 131 | /* Item one is entirely after item two. */ |
| 132 | if (one->start >= two->start + two->size) |
| 133 | return false; |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | static void mem_avoid_init(unsigned long input, unsigned long input_size, |
| 138 | unsigned long output, unsigned long output_size) |
| 139 | { |
| 140 | u64 initrd_start, initrd_size; |
| 141 | u64 cmd_line, cmd_line_size; |
| 142 | unsigned long unsafe, unsafe_len; |
| 143 | char *ptr; |
| 144 | |
| 145 | /* |
| 146 | * Avoid the region that is unsafe to overlap during |
| 147 | * decompression (see calculations at top of misc.c). |
| 148 | */ |
| 149 | unsafe_len = (output_size >> 12) + 32768 + 18; |
| 150 | unsafe = (unsigned long)input + input_size - unsafe_len; |
| 151 | mem_avoid[0].start = unsafe; |
| 152 | mem_avoid[0].size = unsafe_len; |
| 153 | |
| 154 | /* Avoid initrd. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 155 | initrd_start = (u64)boot_params->ext_ramdisk_image << 32; |
| 156 | initrd_start |= boot_params->hdr.ramdisk_image; |
| 157 | initrd_size = (u64)boot_params->ext_ramdisk_size << 32; |
| 158 | initrd_size |= boot_params->hdr.ramdisk_size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 159 | mem_avoid[1].start = initrd_start; |
| 160 | mem_avoid[1].size = initrd_size; |
| 161 | |
| 162 | /* Avoid kernel command line. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 163 | cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32; |
| 164 | cmd_line |= boot_params->hdr.cmd_line_ptr; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 165 | /* Calculate size of cmd_line. */ |
| 166 | ptr = (char *)(unsigned long)cmd_line; |
| 167 | for (cmd_line_size = 0; ptr[cmd_line_size++]; ) |
| 168 | ; |
| 169 | mem_avoid[2].start = cmd_line; |
| 170 | mem_avoid[2].size = cmd_line_size; |
| 171 | |
| 172 | /* Avoid heap memory. */ |
| 173 | mem_avoid[3].start = (unsigned long)free_mem_ptr; |
| 174 | mem_avoid[3].size = BOOT_HEAP_SIZE; |
| 175 | |
| 176 | /* Avoid stack memory. */ |
| 177 | mem_avoid[4].start = (unsigned long)free_mem_end_ptr; |
| 178 | mem_avoid[4].size = BOOT_STACK_SIZE; |
| 179 | } |
| 180 | |
| 181 | /* Does this memory vector overlap a known avoided area? */ |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 182 | static bool mem_avoid_overlap(struct mem_vector *img) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 183 | { |
| 184 | int i; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 185 | struct setup_data *ptr; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 186 | |
| 187 | for (i = 0; i < MEM_AVOID_MAX; i++) { |
| 188 | if (mem_overlaps(img, &mem_avoid[i])) |
| 189 | return true; |
| 190 | } |
| 191 | |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 192 | /* Avoid all entries in the setup_data linked list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 193 | ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 194 | while (ptr) { |
| 195 | struct mem_vector avoid; |
| 196 | |
Kees Cook | 20cc288 | 2014-10-01 11:36:32 -0700 | [diff] [blame] | 197 | avoid.start = (unsigned long)ptr; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 198 | avoid.size = sizeof(*ptr) + ptr->len; |
| 199 | |
| 200 | if (mem_overlaps(img, &avoid)) |
| 201 | return true; |
| 202 | |
| 203 | ptr = (struct setup_data *)(unsigned long)ptr->next; |
| 204 | } |
| 205 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 209 | static unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET / |
| 210 | CONFIG_PHYSICAL_ALIGN]; |
| 211 | static unsigned long slot_max; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 212 | |
| 213 | static void slots_append(unsigned long addr) |
| 214 | { |
| 215 | /* Overflowing the slots list should be impossible. */ |
| 216 | if (slot_max >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET / |
| 217 | CONFIG_PHYSICAL_ALIGN) |
| 218 | return; |
| 219 | |
| 220 | slots[slot_max++] = addr; |
| 221 | } |
| 222 | |
| 223 | static unsigned long slots_fetch_random(void) |
| 224 | { |
| 225 | /* Handle case of no slots stored. */ |
| 226 | if (slot_max == 0) |
| 227 | return 0; |
| 228 | |
| 229 | return slots[get_random_long() % slot_max]; |
| 230 | } |
| 231 | |
| 232 | static void process_e820_entry(struct e820entry *entry, |
| 233 | unsigned long minimum, |
| 234 | unsigned long image_size) |
| 235 | { |
| 236 | struct mem_vector region, img; |
| 237 | |
| 238 | /* Skip non-RAM entries. */ |
| 239 | if (entry->type != E820_RAM) |
| 240 | return; |
| 241 | |
| 242 | /* Ignore entries entirely above our maximum. */ |
| 243 | if (entry->addr >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET) |
| 244 | return; |
| 245 | |
| 246 | /* Ignore entries entirely below our minimum. */ |
| 247 | if (entry->addr + entry->size < minimum) |
| 248 | return; |
| 249 | |
| 250 | region.start = entry->addr; |
| 251 | region.size = entry->size; |
| 252 | |
| 253 | /* Potentially raise address to minimum location. */ |
| 254 | if (region.start < minimum) |
| 255 | region.start = minimum; |
| 256 | |
| 257 | /* Potentially raise address to meet alignment requirements. */ |
| 258 | region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); |
| 259 | |
| 260 | /* Did we raise the address above the bounds of this e820 region? */ |
| 261 | if (region.start > entry->addr + entry->size) |
| 262 | return; |
| 263 | |
| 264 | /* Reduce size by any delta from the original address. */ |
| 265 | region.size -= region.start - entry->addr; |
| 266 | |
| 267 | /* Reduce maximum size to fit end of image within maximum limit. */ |
| 268 | if (region.start + region.size > CONFIG_RANDOMIZE_BASE_MAX_OFFSET) |
| 269 | region.size = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - region.start; |
| 270 | |
| 271 | /* Walk each aligned slot and check for avoided areas. */ |
| 272 | for (img.start = region.start, img.size = image_size ; |
| 273 | mem_contains(®ion, &img) ; |
| 274 | img.start += CONFIG_PHYSICAL_ALIGN) { |
| 275 | if (mem_avoid_overlap(&img)) |
| 276 | continue; |
| 277 | slots_append(img.start); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static unsigned long find_random_addr(unsigned long minimum, |
| 282 | unsigned long size) |
| 283 | { |
| 284 | int i; |
| 285 | unsigned long addr; |
| 286 | |
| 287 | /* Make sure minimum is aligned. */ |
| 288 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); |
| 289 | |
| 290 | /* Verify potential e820 positions, appending to slots list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 291 | for (i = 0; i < boot_params->e820_entries; i++) { |
| 292 | process_e820_entry(&boot_params->e820_map[i], minimum, size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return slots_fetch_random(); |
| 296 | } |
| 297 | |
Yinghai Lu | 206f25a | 2016-04-18 09:42:11 -0700 | [diff] [blame] | 298 | unsigned char *choose_kernel_location(unsigned char *input, |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 299 | unsigned long input_size, |
| 300 | unsigned char *output, |
| 301 | unsigned long output_size) |
| 302 | { |
| 303 | unsigned long choice = (unsigned long)output; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 304 | unsigned long random; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 305 | |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 306 | #ifdef CONFIG_HIBERNATION |
| 307 | if (!cmdline_find_option_bool("kaslr")) { |
| 308 | debug_putstr("KASLR disabled by default...\n"); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 309 | goto out; |
| 310 | } |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 311 | #else |
| 312 | if (cmdline_find_option_bool("nokaslr")) { |
| 313 | debug_putstr("KASLR disabled by cmdline...\n"); |
| 314 | goto out; |
| 315 | } |
| 316 | #endif |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 317 | |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 318 | boot_params->hdr.loadflags |= KASLR_FLAG; |
Borislav Petkov | 78cac48 | 2015-04-01 12:49:52 +0200 | [diff] [blame] | 319 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 320 | /* Record the various known unsafe memory ranges. */ |
| 321 | mem_avoid_init((unsigned long)input, input_size, |
| 322 | (unsigned long)output, output_size); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 323 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 324 | /* Walk e820 and find a random address. */ |
| 325 | random = find_random_addr(choice, output_size); |
| 326 | if (!random) { |
| 327 | debug_putstr("KASLR could not find suitable E820 region...\n"); |
| 328 | goto out; |
| 329 | } |
| 330 | |
| 331 | /* Always enforce the minimum. */ |
| 332 | if (random < choice) |
| 333 | goto out; |
| 334 | |
| 335 | choice = random; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 336 | out: |
| 337 | return (unsigned char *)choice; |
| 338 | } |