Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 1 | #include "misc.h" |
| 2 | |
| 3 | #ifdef CONFIG_RANDOMIZE_BASE |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 4 | #include <asm/msr.h> |
| 5 | #include <asm/archrandom.h> |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 6 | #include <asm/e820.h> |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 7 | |
| 8 | #define I8254_PORT_CONTROL 0x43 |
| 9 | #define I8254_PORT_COUNTER0 0x40 |
| 10 | #define I8254_CMD_READBACK 0xC0 |
| 11 | #define I8254_SELECT_COUNTER0 0x02 |
| 12 | #define I8254_STATUS_NOTREADY 0x40 |
| 13 | static inline u16 i8254(void) |
| 14 | { |
| 15 | u16 status, timer; |
| 16 | |
| 17 | do { |
| 18 | outb(I8254_PORT_CONTROL, |
| 19 | I8254_CMD_READBACK | I8254_SELECT_COUNTER0); |
| 20 | status = inb(I8254_PORT_COUNTER0); |
| 21 | timer = inb(I8254_PORT_COUNTER0); |
| 22 | timer |= inb(I8254_PORT_COUNTER0) << 8; |
| 23 | } while (status & I8254_STATUS_NOTREADY); |
| 24 | |
| 25 | return timer; |
| 26 | } |
| 27 | |
| 28 | static unsigned long get_random_long(void) |
| 29 | { |
| 30 | unsigned long random; |
| 31 | |
| 32 | if (has_cpuflag(X86_FEATURE_RDRAND)) { |
| 33 | debug_putstr("KASLR using RDRAND...\n"); |
| 34 | if (rdrand_long(&random)) |
| 35 | return random; |
| 36 | } |
| 37 | |
| 38 | if (has_cpuflag(X86_FEATURE_TSC)) { |
| 39 | uint32_t raw; |
| 40 | |
| 41 | debug_putstr("KASLR using RDTSC...\n"); |
| 42 | rdtscl(raw); |
| 43 | |
| 44 | /* Only use the low bits of rdtsc. */ |
| 45 | random = raw & 0xffff; |
| 46 | } else { |
| 47 | debug_putstr("KASLR using i8254...\n"); |
| 48 | random = i8254(); |
| 49 | } |
| 50 | |
| 51 | /* Extend timer bits poorly... */ |
| 52 | random |= (random << 16); |
| 53 | #ifdef CONFIG_X86_64 |
| 54 | random |= (random << 32); |
| 55 | #endif |
| 56 | return random; |
| 57 | } |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 58 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 59 | struct mem_vector { |
| 60 | unsigned long start; |
| 61 | unsigned long size; |
| 62 | }; |
| 63 | |
| 64 | #define MEM_AVOID_MAX 5 |
| 65 | struct mem_vector mem_avoid[MEM_AVOID_MAX]; |
| 66 | |
| 67 | static bool mem_contains(struct mem_vector *region, struct mem_vector *item) |
| 68 | { |
| 69 | /* Item at least partially before region. */ |
| 70 | if (item->start < region->start) |
| 71 | return false; |
| 72 | /* Item at least partially after region. */ |
| 73 | if (item->start + item->size > region->start + region->size) |
| 74 | return false; |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two) |
| 79 | { |
| 80 | /* Item one is entirely before item two. */ |
| 81 | if (one->start + one->size <= two->start) |
| 82 | return false; |
| 83 | /* Item one is entirely after item two. */ |
| 84 | if (one->start >= two->start + two->size) |
| 85 | return false; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | static void mem_avoid_init(unsigned long input, unsigned long input_size, |
| 90 | unsigned long output, unsigned long output_size) |
| 91 | { |
| 92 | u64 initrd_start, initrd_size; |
| 93 | u64 cmd_line, cmd_line_size; |
| 94 | unsigned long unsafe, unsafe_len; |
| 95 | char *ptr; |
| 96 | |
| 97 | /* |
| 98 | * Avoid the region that is unsafe to overlap during |
| 99 | * decompression (see calculations at top of misc.c). |
| 100 | */ |
| 101 | unsafe_len = (output_size >> 12) + 32768 + 18; |
| 102 | unsafe = (unsigned long)input + input_size - unsafe_len; |
| 103 | mem_avoid[0].start = unsafe; |
| 104 | mem_avoid[0].size = unsafe_len; |
| 105 | |
| 106 | /* Avoid initrd. */ |
| 107 | initrd_start = (u64)real_mode->ext_ramdisk_image << 32; |
| 108 | initrd_start |= real_mode->hdr.ramdisk_image; |
| 109 | initrd_size = (u64)real_mode->ext_ramdisk_size << 32; |
| 110 | initrd_size |= real_mode->hdr.ramdisk_size; |
| 111 | mem_avoid[1].start = initrd_start; |
| 112 | mem_avoid[1].size = initrd_size; |
| 113 | |
| 114 | /* Avoid kernel command line. */ |
| 115 | cmd_line = (u64)real_mode->ext_cmd_line_ptr << 32; |
| 116 | cmd_line |= real_mode->hdr.cmd_line_ptr; |
| 117 | /* Calculate size of cmd_line. */ |
| 118 | ptr = (char *)(unsigned long)cmd_line; |
| 119 | for (cmd_line_size = 0; ptr[cmd_line_size++]; ) |
| 120 | ; |
| 121 | mem_avoid[2].start = cmd_line; |
| 122 | mem_avoid[2].size = cmd_line_size; |
| 123 | |
| 124 | /* Avoid heap memory. */ |
| 125 | mem_avoid[3].start = (unsigned long)free_mem_ptr; |
| 126 | mem_avoid[3].size = BOOT_HEAP_SIZE; |
| 127 | |
| 128 | /* Avoid stack memory. */ |
| 129 | mem_avoid[4].start = (unsigned long)free_mem_end_ptr; |
| 130 | mem_avoid[4].size = BOOT_STACK_SIZE; |
| 131 | } |
| 132 | |
| 133 | /* Does this memory vector overlap a known avoided area? */ |
| 134 | bool mem_avoid_overlap(struct mem_vector *img) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | for (i = 0; i < MEM_AVOID_MAX; i++) { |
| 139 | if (mem_overlaps(img, &mem_avoid[i])) |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET / CONFIG_PHYSICAL_ALIGN]; |
| 147 | unsigned long slot_max = 0; |
| 148 | |
| 149 | static void slots_append(unsigned long addr) |
| 150 | { |
| 151 | /* Overflowing the slots list should be impossible. */ |
| 152 | if (slot_max >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET / |
| 153 | CONFIG_PHYSICAL_ALIGN) |
| 154 | return; |
| 155 | |
| 156 | slots[slot_max++] = addr; |
| 157 | } |
| 158 | |
| 159 | static unsigned long slots_fetch_random(void) |
| 160 | { |
| 161 | /* Handle case of no slots stored. */ |
| 162 | if (slot_max == 0) |
| 163 | return 0; |
| 164 | |
| 165 | return slots[get_random_long() % slot_max]; |
| 166 | } |
| 167 | |
| 168 | static void process_e820_entry(struct e820entry *entry, |
| 169 | unsigned long minimum, |
| 170 | unsigned long image_size) |
| 171 | { |
| 172 | struct mem_vector region, img; |
| 173 | |
| 174 | /* Skip non-RAM entries. */ |
| 175 | if (entry->type != E820_RAM) |
| 176 | return; |
| 177 | |
| 178 | /* Ignore entries entirely above our maximum. */ |
| 179 | if (entry->addr >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET) |
| 180 | return; |
| 181 | |
| 182 | /* Ignore entries entirely below our minimum. */ |
| 183 | if (entry->addr + entry->size < minimum) |
| 184 | return; |
| 185 | |
| 186 | region.start = entry->addr; |
| 187 | region.size = entry->size; |
| 188 | |
| 189 | /* Potentially raise address to minimum location. */ |
| 190 | if (region.start < minimum) |
| 191 | region.start = minimum; |
| 192 | |
| 193 | /* Potentially raise address to meet alignment requirements. */ |
| 194 | region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); |
| 195 | |
| 196 | /* Did we raise the address above the bounds of this e820 region? */ |
| 197 | if (region.start > entry->addr + entry->size) |
| 198 | return; |
| 199 | |
| 200 | /* Reduce size by any delta from the original address. */ |
| 201 | region.size -= region.start - entry->addr; |
| 202 | |
| 203 | /* Reduce maximum size to fit end of image within maximum limit. */ |
| 204 | if (region.start + region.size > CONFIG_RANDOMIZE_BASE_MAX_OFFSET) |
| 205 | region.size = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - region.start; |
| 206 | |
| 207 | /* Walk each aligned slot and check for avoided areas. */ |
| 208 | for (img.start = region.start, img.size = image_size ; |
| 209 | mem_contains(®ion, &img) ; |
| 210 | img.start += CONFIG_PHYSICAL_ALIGN) { |
| 211 | if (mem_avoid_overlap(&img)) |
| 212 | continue; |
| 213 | slots_append(img.start); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static unsigned long find_random_addr(unsigned long minimum, |
| 218 | unsigned long size) |
| 219 | { |
| 220 | int i; |
| 221 | unsigned long addr; |
| 222 | |
| 223 | /* Make sure minimum is aligned. */ |
| 224 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); |
| 225 | |
| 226 | /* Verify potential e820 positions, appending to slots list. */ |
| 227 | for (i = 0; i < real_mode->e820_entries; i++) { |
| 228 | process_e820_entry(&real_mode->e820_map[i], minimum, size); |
| 229 | } |
| 230 | |
| 231 | return slots_fetch_random(); |
| 232 | } |
| 233 | |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 234 | unsigned char *choose_kernel_location(unsigned char *input, |
| 235 | unsigned long input_size, |
| 236 | unsigned char *output, |
| 237 | unsigned long output_size) |
| 238 | { |
| 239 | unsigned long choice = (unsigned long)output; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 240 | unsigned long random; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 241 | |
| 242 | if (cmdline_find_option_bool("nokaslr")) { |
| 243 | debug_putstr("KASLR disabled...\n"); |
| 244 | goto out; |
| 245 | } |
| 246 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 247 | /* Record the various known unsafe memory ranges. */ |
| 248 | mem_avoid_init((unsigned long)input, input_size, |
| 249 | (unsigned long)output, output_size); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 250 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 251 | /* Walk e820 and find a random address. */ |
| 252 | random = find_random_addr(choice, output_size); |
| 253 | if (!random) { |
| 254 | debug_putstr("KASLR could not find suitable E820 region...\n"); |
| 255 | goto out; |
| 256 | } |
| 257 | |
| 258 | /* Always enforce the minimum. */ |
| 259 | if (random < choice) |
| 260 | goto out; |
| 261 | |
| 262 | choice = random; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 263 | out: |
| 264 | return (unsigned char *)choice; |
| 265 | } |
| 266 | |
| 267 | #endif /* CONFIG_RANDOMIZE_BASE */ |