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 | |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 124 | enum mem_avoid_index { |
| 125 | MEM_AVOID_ZO_RANGE = 0, |
| 126 | MEM_AVOID_INITRD, |
| 127 | MEM_AVOID_CMDLINE, |
| 128 | MEM_AVOID_BOOTPARAMS, |
| 129 | MEM_AVOID_MAX, |
| 130 | }; |
| 131 | |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 132 | static struct mem_vector mem_avoid[MEM_AVOID_MAX]; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 133 | |
| 134 | static bool mem_contains(struct mem_vector *region, struct mem_vector *item) |
| 135 | { |
| 136 | /* Item at least partially before region. */ |
| 137 | if (item->start < region->start) |
| 138 | return false; |
| 139 | /* Item at least partially after region. */ |
| 140 | if (item->start + item->size > region->start + region->size) |
| 141 | return false; |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two) |
| 146 | { |
| 147 | /* Item one is entirely before item two. */ |
| 148 | if (one->start + one->size <= two->start) |
| 149 | return false; |
| 150 | /* Item one is entirely after item two. */ |
| 151 | if (one->start >= two->start + two->size) |
| 152 | return false; |
| 153 | return true; |
| 154 | } |
| 155 | |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 156 | /* |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 157 | * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T). |
| 158 | * The mem_avoid array is used to store the ranges that need to be avoided |
| 159 | * when KASLR searches for an appropriate random address. We must avoid any |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 160 | * regions that are unsafe to overlap with during decompression, and other |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 161 | * things like the initrd, cmdline and boot_params. This comment seeks to |
| 162 | * explain mem_avoid as clearly as possible since incorrect mem_avoid |
| 163 | * memory ranges lead to really hard to debug boot failures. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 164 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 165 | * The initrd, cmdline, and boot_params are trivial to identify for |
Kees Cook | cb18ef0 | 2016-05-09 13:22:05 -0700 | [diff] [blame] | 166 | * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 167 | * MEM_AVOID_BOOTPARAMS respectively below. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 168 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 169 | * What is not obvious how to avoid is the range of memory that is used |
| 170 | * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover |
| 171 | * the compressed kernel (ZO) and its run space, which is used to extract |
| 172 | * the uncompressed kernel (VO) and relocs. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 173 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 174 | * ZO's full run size sits against the end of the decompression buffer, so |
| 175 | * we can calculate where text, data, bss, etc of ZO are positioned more |
| 176 | * easily. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 177 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 178 | * For additional background, the decompression calculations can be found |
| 179 | * in header.S, and the memory diagram is based on the one found in misc.c. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 180 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 181 | * The following conditions are already enforced by the image layouts and |
| 182 | * associated code: |
| 183 | * - input + input_size >= output + output_size |
| 184 | * - kernel_total_size <= init_size |
| 185 | * - kernel_total_size <= output_size (see Note below) |
| 186 | * - output + init_size >= output + output_size |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 187 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 188 | * (Note that kernel_total_size and output_size have no fundamental |
| 189 | * relationship, but output_size is passed to choose_random_location |
| 190 | * as a maximum of the two. The diagram is showing a case where |
| 191 | * kernel_total_size is larger than output_size, but this case is |
| 192 | * handled by bumping output_size.) |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 193 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 194 | * The above conditions can be illustrated by a diagram: |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 195 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 196 | * 0 output input input+input_size output+init_size |
| 197 | * | | | | | |
| 198 | * | | | | | |
| 199 | * |-----|--------|--------|--------------|-----------|--|-------------| |
| 200 | * | | | |
| 201 | * | | | |
| 202 | * output+init_size-ZO_INIT_SIZE output+output_size output+kernel_total_size |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 203 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 204 | * [output, output+init_size) is the entire memory range used for |
| 205 | * extracting the compressed image. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 206 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 207 | * [output, output+kernel_total_size) is the range needed for the |
| 208 | * uncompressed kernel (VO) and its run size (bss, brk, etc). |
| 209 | * |
| 210 | * [output, output+output_size) is VO plus relocs (i.e. the entire |
| 211 | * uncompressed payload contained by ZO). This is the area of the buffer |
| 212 | * written to during decompression. |
| 213 | * |
| 214 | * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case |
| 215 | * range of the copied ZO and decompression code. (i.e. the range |
| 216 | * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.) |
| 217 | * |
| 218 | * [input, input+input_size) is the original copied compressed image (ZO) |
| 219 | * (i.e. it does not include its run size). This range must be avoided |
| 220 | * because it contains the data used for decompression. |
| 221 | * |
| 222 | * [input+input_size, output+init_size) is [_text, _end) for ZO. This |
| 223 | * range includes ZO's heap and stack, and must be avoided since it |
| 224 | * performs the decompression. |
| 225 | * |
| 226 | * Since the above two ranges need to be avoided and they are adjacent, |
| 227 | * they can be merged, resulting in: [input, output+init_size) which |
| 228 | * becomes the MEM_AVOID_ZO_RANGE below. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 229 | */ |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 230 | static void mem_avoid_init(unsigned long input, unsigned long input_size, |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 231 | unsigned long output) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 232 | { |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 233 | unsigned long init_size = boot_params->hdr.init_size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 234 | u64 initrd_start, initrd_size; |
| 235 | u64 cmd_line, cmd_line_size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 236 | char *ptr; |
| 237 | |
| 238 | /* |
| 239 | * Avoid the region that is unsafe to overlap during |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 240 | * decompression. |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 241 | */ |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 242 | mem_avoid[MEM_AVOID_ZO_RANGE].start = input; |
| 243 | mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 244 | add_identity_map(mem_avoid[MEM_AVOID_ZO_RANGE].start, |
| 245 | mem_avoid[MEM_AVOID_ZO_RANGE].size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 246 | |
| 247 | /* Avoid initrd. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 248 | initrd_start = (u64)boot_params->ext_ramdisk_image << 32; |
| 249 | initrd_start |= boot_params->hdr.ramdisk_image; |
| 250 | initrd_size = (u64)boot_params->ext_ramdisk_size << 32; |
| 251 | initrd_size |= boot_params->hdr.ramdisk_size; |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 252 | mem_avoid[MEM_AVOID_INITRD].start = initrd_start; |
| 253 | mem_avoid[MEM_AVOID_INITRD].size = initrd_size; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 254 | /* No need to set mapping for initrd, it will be handled in VO. */ |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 255 | |
| 256 | /* Avoid kernel command line. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 257 | cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32; |
| 258 | cmd_line |= boot_params->hdr.cmd_line_ptr; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 259 | /* Calculate size of cmd_line. */ |
| 260 | ptr = (char *)(unsigned long)cmd_line; |
| 261 | for (cmd_line_size = 0; ptr[cmd_line_size++]; ) |
| 262 | ; |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 263 | mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line; |
| 264 | mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 265 | add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start, |
| 266 | mem_avoid[MEM_AVOID_CMDLINE].size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 267 | |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 268 | /* Avoid boot parameters. */ |
| 269 | mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params; |
| 270 | mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params); |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 271 | add_identity_map(mem_avoid[MEM_AVOID_BOOTPARAMS].start, |
| 272 | mem_avoid[MEM_AVOID_BOOTPARAMS].size); |
| 273 | |
| 274 | /* We don't need to set a mapping for setup_data. */ |
| 275 | |
| 276 | #ifdef CONFIG_X86_VERBOSE_BOOTUP |
| 277 | /* Make sure video RAM can be used. */ |
| 278 | add_identity_map(0, PMD_SIZE); |
| 279 | #endif |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 282 | /* |
| 283 | * Does this memory vector overlap a known avoided area? If so, record the |
| 284 | * overlap region with the lowest address. |
| 285 | */ |
| 286 | static bool mem_avoid_overlap(struct mem_vector *img, |
| 287 | struct mem_vector *overlap) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 288 | { |
| 289 | int i; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 290 | struct setup_data *ptr; |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 291 | unsigned long earliest = img->start + img->size; |
| 292 | bool is_overlapping = false; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 293 | |
| 294 | for (i = 0; i < MEM_AVOID_MAX; i++) { |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 295 | if (mem_overlaps(img, &mem_avoid[i]) && |
| 296 | mem_avoid[i].start < earliest) { |
| 297 | *overlap = mem_avoid[i]; |
| 298 | is_overlapping = true; |
| 299 | } |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 302 | /* Avoid all entries in the setup_data linked list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 303 | ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 304 | while (ptr) { |
| 305 | struct mem_vector avoid; |
| 306 | |
Kees Cook | 20cc288 | 2014-10-01 11:36:32 -0700 | [diff] [blame] | 307 | avoid.start = (unsigned long)ptr; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 308 | avoid.size = sizeof(*ptr) + ptr->len; |
| 309 | |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 310 | if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) { |
| 311 | *overlap = avoid; |
| 312 | is_overlapping = true; |
| 313 | } |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 314 | |
| 315 | ptr = (struct setup_data *)(unsigned long)ptr->next; |
| 316 | } |
| 317 | |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 318 | return is_overlapping; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 321 | static unsigned long slots[KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN]; |
Baoquan He | c401cf1 | 2016-05-09 13:22:06 -0700 | [diff] [blame] | 322 | |
| 323 | struct slot_area { |
| 324 | unsigned long addr; |
| 325 | int num; |
| 326 | }; |
| 327 | |
| 328 | #define MAX_SLOT_AREA 100 |
| 329 | |
| 330 | static struct slot_area slot_areas[MAX_SLOT_AREA]; |
| 331 | |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 332 | static unsigned long slot_max; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 333 | |
Baoquan He | c401cf1 | 2016-05-09 13:22:06 -0700 | [diff] [blame] | 334 | static unsigned long slot_area_index; |
| 335 | |
| 336 | static void store_slot_info(struct mem_vector *region, unsigned long image_size) |
| 337 | { |
| 338 | struct slot_area slot_area; |
| 339 | |
| 340 | if (slot_area_index == MAX_SLOT_AREA) |
| 341 | return; |
| 342 | |
| 343 | slot_area.addr = region->start; |
| 344 | slot_area.num = (region->size - image_size) / |
| 345 | CONFIG_PHYSICAL_ALIGN + 1; |
| 346 | |
| 347 | if (slot_area.num > 0) { |
| 348 | slot_areas[slot_area_index++] = slot_area; |
| 349 | slot_max += slot_area.num; |
| 350 | } |
| 351 | } |
| 352 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 353 | static void slots_append(unsigned long addr) |
| 354 | { |
| 355 | /* Overflowing the slots list should be impossible. */ |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 356 | if (slot_max >= KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 357 | return; |
| 358 | |
| 359 | slots[slot_max++] = addr; |
| 360 | } |
| 361 | |
| 362 | static unsigned long slots_fetch_random(void) |
| 363 | { |
| 364 | /* Handle case of no slots stored. */ |
| 365 | if (slot_max == 0) |
| 366 | return 0; |
| 367 | |
| 368 | return slots[get_random_long() % slot_max]; |
| 369 | } |
| 370 | |
| 371 | static void process_e820_entry(struct e820entry *entry, |
| 372 | unsigned long minimum, |
| 373 | unsigned long image_size) |
| 374 | { |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 375 | struct mem_vector region, img, overlap; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 376 | |
| 377 | /* Skip non-RAM entries. */ |
| 378 | if (entry->type != E820_RAM) |
| 379 | return; |
| 380 | |
| 381 | /* Ignore entries entirely above our maximum. */ |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 382 | if (entry->addr >= KERNEL_IMAGE_SIZE) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 383 | return; |
| 384 | |
| 385 | /* Ignore entries entirely below our minimum. */ |
| 386 | if (entry->addr + entry->size < minimum) |
| 387 | return; |
| 388 | |
| 389 | region.start = entry->addr; |
| 390 | region.size = entry->size; |
| 391 | |
| 392 | /* Potentially raise address to minimum location. */ |
| 393 | if (region.start < minimum) |
| 394 | region.start = minimum; |
| 395 | |
| 396 | /* Potentially raise address to meet alignment requirements. */ |
| 397 | region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); |
| 398 | |
| 399 | /* Did we raise the address above the bounds of this e820 region? */ |
| 400 | if (region.start > entry->addr + entry->size) |
| 401 | return; |
| 402 | |
| 403 | /* Reduce size by any delta from the original address. */ |
| 404 | region.size -= region.start - entry->addr; |
| 405 | |
| 406 | /* Reduce maximum size to fit end of image within maximum limit. */ |
Baoquan He | e8581e3 | 2016-04-20 13:55:43 -0700 | [diff] [blame] | 407 | if (region.start + region.size > KERNEL_IMAGE_SIZE) |
| 408 | region.size = KERNEL_IMAGE_SIZE - region.start; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 409 | |
| 410 | /* Walk each aligned slot and check for avoided areas. */ |
| 411 | for (img.start = region.start, img.size = image_size ; |
| 412 | mem_contains(®ion, &img) ; |
| 413 | img.start += CONFIG_PHYSICAL_ALIGN) { |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame^] | 414 | if (mem_avoid_overlap(&img, &overlap)) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 415 | continue; |
| 416 | slots_append(img.start); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | static unsigned long find_random_addr(unsigned long minimum, |
| 421 | unsigned long size) |
| 422 | { |
| 423 | int i; |
| 424 | unsigned long addr; |
| 425 | |
| 426 | /* Make sure minimum is aligned. */ |
| 427 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); |
| 428 | |
| 429 | /* Verify potential e820 positions, appending to slots list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 430 | for (i = 0; i < boot_params->e820_entries; i++) { |
| 431 | process_e820_entry(&boot_params->e820_map[i], minimum, size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return slots_fetch_random(); |
| 435 | } |
| 436 | |
Borislav Petkov | 549f90d | 2016-05-06 13:50:15 +0200 | [diff] [blame] | 437 | /* |
| 438 | * Since this function examines addresses much more numerically, |
| 439 | * it takes the input and output pointers as 'unsigned long'. |
| 440 | */ |
| 441 | unsigned char *choose_random_location(unsigned long input, |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 442 | unsigned long input_size, |
Borislav Petkov | 549f90d | 2016-05-06 13:50:15 +0200 | [diff] [blame] | 443 | unsigned long output, |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 444 | unsigned long output_size) |
| 445 | { |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame] | 446 | unsigned long choice = output; |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 447 | unsigned long random_addr; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 448 | |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 449 | #ifdef CONFIG_HIBERNATION |
| 450 | if (!cmdline_find_option_bool("kaslr")) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 451 | warn("KASLR disabled: 'kaslr' not on cmdline (hibernation selected)."); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 452 | goto out; |
| 453 | } |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 454 | #else |
| 455 | if (cmdline_find_option_bool("nokaslr")) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 456 | warn("KASLR disabled: 'nokaslr' on cmdline."); |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 457 | goto out; |
| 458 | } |
| 459 | #endif |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 460 | |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 461 | boot_params->hdr.loadflags |= KASLR_FLAG; |
Borislav Petkov | 78cac48 | 2015-04-01 12:49:52 +0200 | [diff] [blame] | 462 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 463 | /* Record the various known unsafe memory ranges. */ |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 464 | mem_avoid_init(input, input_size, output); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 465 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 466 | /* Walk e820 and find a random address. */ |
Kees Cook | 2bc1cd3 | 2016-05-05 15:13:46 -0700 | [diff] [blame] | 467 | random_addr = find_random_addr(output, output_size); |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 468 | if (!random_addr) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 469 | warn("KASLR disabled: could not find suitable E820 region!"); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 470 | goto out; |
| 471 | } |
| 472 | |
| 473 | /* Always enforce the minimum. */ |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 474 | if (random_addr < choice) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 475 | goto out; |
| 476 | |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 477 | choice = random_addr; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 478 | |
| 479 | add_identity_map(choice, output_size); |
Borislav Petkov | 36a39ac | 2016-05-07 11:59:40 +0200 | [diff] [blame] | 480 | |
| 481 | /* This actually loads the identity pagetable on x86_64. */ |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 482 | finalize_identity_maps(); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 483 | out: |
| 484 | return (unsigned char *)choice; |
| 485 | } |