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 | d2d3462 | 2016-05-09 13:22:09 -0700 | [diff] [blame] | 75 | static unsigned long get_random_long(const char *purpose) |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 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 | |
Kees Cook | d2d3462 | 2016-05-09 13:22:09 -0700 | [diff] [blame] | 85 | debug_putstr(purpose); |
| 86 | debug_putstr(" KASLR using"); |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 87 | |
| 88 | if (has_cpuflag(X86_FEATURE_RDRAND)) { |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 89 | debug_putstr(" RDRAND"); |
| 90 | if (rdrand_long(&raw)) { |
| 91 | random ^= raw; |
| 92 | use_i8254 = false; |
| 93 | } |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (has_cpuflag(X86_FEATURE_TSC)) { |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 97 | debug_putstr(" RDTSC"); |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 98 | raw = rdtsc(); |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 99 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 100 | random ^= raw; |
| 101 | use_i8254 = false; |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 104 | if (use_i8254) { |
| 105 | debug_putstr(" i8254"); |
| 106 | random ^= i8254(); |
| 107 | } |
| 108 | |
H. Peter Anvin | e8236c4 | 2013-11-11 22:45:20 -0800 | [diff] [blame] | 109 | /* Circular multiply for better bit diffusion */ |
| 110 | asm("mul %3" |
| 111 | : "=a" (random), "=d" (raw) |
| 112 | : "a" (random), "rm" (mix_const)); |
| 113 | random += raw; |
| 114 | |
Kees Cook | a653f35 | 2013-11-11 14:28:39 -0800 | [diff] [blame] | 115 | debug_putstr("...\n"); |
| 116 | |
Kees Cook | 5bfce5e | 2013-10-10 17:18:15 -0700 | [diff] [blame] | 117 | return random; |
| 118 | } |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 119 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 120 | struct mem_vector { |
| 121 | unsigned long start; |
| 122 | unsigned long size; |
| 123 | }; |
| 124 | |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 125 | enum mem_avoid_index { |
| 126 | MEM_AVOID_ZO_RANGE = 0, |
| 127 | MEM_AVOID_INITRD, |
| 128 | MEM_AVOID_CMDLINE, |
| 129 | MEM_AVOID_BOOTPARAMS, |
| 130 | MEM_AVOID_MAX, |
| 131 | }; |
| 132 | |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 133 | static struct mem_vector mem_avoid[MEM_AVOID_MAX]; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 134 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 135 | static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two) |
| 136 | { |
| 137 | /* Item one is entirely before item two. */ |
| 138 | if (one->start + one->size <= two->start) |
| 139 | return false; |
| 140 | /* Item one is entirely after item two. */ |
| 141 | if (one->start >= two->start + two->size) |
| 142 | return false; |
| 143 | return true; |
| 144 | } |
| 145 | |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 146 | /* |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 147 | * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T). |
| 148 | * The mem_avoid array is used to store the ranges that need to be avoided |
| 149 | * when KASLR searches for an appropriate random address. We must avoid any |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 150 | * regions that are unsafe to overlap with during decompression, and other |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 151 | * things like the initrd, cmdline and boot_params. This comment seeks to |
| 152 | * explain mem_avoid as clearly as possible since incorrect mem_avoid |
| 153 | * memory ranges lead to really hard to debug boot failures. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 154 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 155 | * The initrd, cmdline, and boot_params are trivial to identify for |
Kees Cook | cb18ef0 | 2016-05-09 13:22:05 -0700 | [diff] [blame] | 156 | * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 157 | * MEM_AVOID_BOOTPARAMS respectively below. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 158 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 159 | * What is not obvious how to avoid is the range of memory that is used |
| 160 | * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover |
| 161 | * the compressed kernel (ZO) and its run space, which is used to extract |
| 162 | * the uncompressed kernel (VO) and relocs. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 163 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 164 | * ZO's full run size sits against the end of the decompression buffer, so |
| 165 | * we can calculate where text, data, bss, etc of ZO are positioned more |
| 166 | * easily. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 167 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 168 | * For additional background, the decompression calculations can be found |
| 169 | * 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] | 170 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 171 | * The following conditions are already enforced by the image layouts and |
| 172 | * associated code: |
| 173 | * - input + input_size >= output + output_size |
| 174 | * - kernel_total_size <= init_size |
| 175 | * - kernel_total_size <= output_size (see Note below) |
| 176 | * - output + init_size >= output + output_size |
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 | * (Note that kernel_total_size and output_size have no fundamental |
| 179 | * relationship, but output_size is passed to choose_random_location |
| 180 | * as a maximum of the two. The diagram is showing a case where |
| 181 | * kernel_total_size is larger than output_size, but this case is |
| 182 | * handled by bumping output_size.) |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 183 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 184 | * The above conditions can be illustrated by a diagram: |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 185 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 186 | * 0 output input input+input_size output+init_size |
| 187 | * | | | | | |
| 188 | * | | | | | |
| 189 | * |-----|--------|--------|--------------|-----------|--|-------------| |
| 190 | * | | | |
| 191 | * | | | |
| 192 | * 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] | 193 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 194 | * [output, output+init_size) is the entire memory range used for |
| 195 | * extracting the compressed image. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 196 | * |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 197 | * [output, output+kernel_total_size) is the range needed for the |
| 198 | * uncompressed kernel (VO) and its run size (bss, brk, etc). |
| 199 | * |
| 200 | * [output, output+output_size) is VO plus relocs (i.e. the entire |
| 201 | * uncompressed payload contained by ZO). This is the area of the buffer |
| 202 | * written to during decompression. |
| 203 | * |
| 204 | * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case |
| 205 | * range of the copied ZO and decompression code. (i.e. the range |
| 206 | * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.) |
| 207 | * |
| 208 | * [input, input+input_size) is the original copied compressed image (ZO) |
| 209 | * (i.e. it does not include its run size). This range must be avoided |
| 210 | * because it contains the data used for decompression. |
| 211 | * |
| 212 | * [input+input_size, output+init_size) is [_text, _end) for ZO. This |
| 213 | * range includes ZO's heap and stack, and must be avoided since it |
| 214 | * performs the decompression. |
| 215 | * |
| 216 | * Since the above two ranges need to be avoided and they are adjacent, |
| 217 | * they can be merged, resulting in: [input, output+init_size) which |
| 218 | * becomes the MEM_AVOID_ZO_RANGE below. |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 219 | */ |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 220 | static void mem_avoid_init(unsigned long input, unsigned long input_size, |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 221 | unsigned long output) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 222 | { |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 223 | unsigned long init_size = boot_params->hdr.init_size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 224 | u64 initrd_start, initrd_size; |
| 225 | u64 cmd_line, cmd_line_size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 226 | char *ptr; |
| 227 | |
| 228 | /* |
| 229 | * Avoid the region that is unsafe to overlap during |
Yinghai Lu | 9dc1969 | 2016-05-05 15:13:47 -0700 | [diff] [blame] | 230 | * decompression. |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 231 | */ |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 232 | mem_avoid[MEM_AVOID_ZO_RANGE].start = input; |
| 233 | mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 234 | add_identity_map(mem_avoid[MEM_AVOID_ZO_RANGE].start, |
| 235 | mem_avoid[MEM_AVOID_ZO_RANGE].size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 236 | |
| 237 | /* Avoid initrd. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 238 | initrd_start = (u64)boot_params->ext_ramdisk_image << 32; |
| 239 | initrd_start |= boot_params->hdr.ramdisk_image; |
| 240 | initrd_size = (u64)boot_params->ext_ramdisk_size << 32; |
| 241 | initrd_size |= boot_params->hdr.ramdisk_size; |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 242 | mem_avoid[MEM_AVOID_INITRD].start = initrd_start; |
| 243 | mem_avoid[MEM_AVOID_INITRD].size = initrd_size; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 244 | /* 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] | 245 | |
| 246 | /* Avoid kernel command line. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 247 | cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32; |
| 248 | cmd_line |= boot_params->hdr.cmd_line_ptr; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 249 | /* Calculate size of cmd_line. */ |
| 250 | ptr = (char *)(unsigned long)cmd_line; |
| 251 | for (cmd_line_size = 0; ptr[cmd_line_size++]; ) |
| 252 | ; |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 253 | mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line; |
| 254 | mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size; |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 255 | add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start, |
| 256 | mem_avoid[MEM_AVOID_CMDLINE].size); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 257 | |
Kees Cook | ed09acd | 2016-05-06 12:44:59 -0700 | [diff] [blame] | 258 | /* Avoid boot parameters. */ |
| 259 | mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params; |
| 260 | mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params); |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 261 | add_identity_map(mem_avoid[MEM_AVOID_BOOTPARAMS].start, |
| 262 | mem_avoid[MEM_AVOID_BOOTPARAMS].size); |
| 263 | |
| 264 | /* We don't need to set a mapping for setup_data. */ |
| 265 | |
| 266 | #ifdef CONFIG_X86_VERBOSE_BOOTUP |
| 267 | /* Make sure video RAM can be used. */ |
| 268 | add_identity_map(0, PMD_SIZE); |
| 269 | #endif |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 272 | /* |
| 273 | * Does this memory vector overlap a known avoided area? If so, record the |
| 274 | * overlap region with the lowest address. |
| 275 | */ |
| 276 | static bool mem_avoid_overlap(struct mem_vector *img, |
| 277 | struct mem_vector *overlap) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 278 | { |
| 279 | int i; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 280 | struct setup_data *ptr; |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 281 | unsigned long earliest = img->start + img->size; |
| 282 | bool is_overlapping = false; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 283 | |
| 284 | for (i = 0; i < MEM_AVOID_MAX; i++) { |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 285 | if (mem_overlaps(img, &mem_avoid[i]) && |
| 286 | mem_avoid[i].start < earliest) { |
| 287 | *overlap = mem_avoid[i]; |
Baoquan He | 6daa2ec | 2016-07-01 15:34:40 +0800 | [diff] [blame^] | 288 | earliest = overlap->start; |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 289 | is_overlapping = true; |
| 290 | } |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 293 | /* Avoid all entries in the setup_data linked list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 294 | ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 295 | while (ptr) { |
| 296 | struct mem_vector avoid; |
| 297 | |
Kees Cook | 20cc288 | 2014-10-01 11:36:32 -0700 | [diff] [blame] | 298 | avoid.start = (unsigned long)ptr; |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 299 | avoid.size = sizeof(*ptr) + ptr->len; |
| 300 | |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 301 | if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) { |
| 302 | *overlap = avoid; |
Baoquan He | 6daa2ec | 2016-07-01 15:34:40 +0800 | [diff] [blame^] | 303 | earliest = overlap->start; |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 304 | is_overlapping = true; |
| 305 | } |
Kees Cook | 0cacbfb | 2014-09-11 09:19:31 -0700 | [diff] [blame] | 306 | |
| 307 | ptr = (struct setup_data *)(unsigned long)ptr->next; |
| 308 | } |
| 309 | |
Kees Cook | 06486d6 | 2016-05-09 13:22:07 -0700 | [diff] [blame] | 310 | return is_overlapping; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Baoquan He | c401cf1 | 2016-05-09 13:22:06 -0700 | [diff] [blame] | 313 | struct slot_area { |
| 314 | unsigned long addr; |
| 315 | int num; |
| 316 | }; |
| 317 | |
| 318 | #define MAX_SLOT_AREA 100 |
| 319 | |
| 320 | static struct slot_area slot_areas[MAX_SLOT_AREA]; |
| 321 | |
Kees Cook | e290e8c | 2014-02-09 13:56:44 -0800 | [diff] [blame] | 322 | static unsigned long slot_max; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 323 | |
Baoquan He | c401cf1 | 2016-05-09 13:22:06 -0700 | [diff] [blame] | 324 | static unsigned long slot_area_index; |
| 325 | |
| 326 | static void store_slot_info(struct mem_vector *region, unsigned long image_size) |
| 327 | { |
| 328 | struct slot_area slot_area; |
| 329 | |
| 330 | if (slot_area_index == MAX_SLOT_AREA) |
| 331 | return; |
| 332 | |
| 333 | slot_area.addr = region->start; |
| 334 | slot_area.num = (region->size - image_size) / |
| 335 | CONFIG_PHYSICAL_ALIGN + 1; |
| 336 | |
| 337 | if (slot_area.num > 0) { |
| 338 | slot_areas[slot_area_index++] = slot_area; |
| 339 | slot_max += slot_area.num; |
| 340 | } |
| 341 | } |
| 342 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 343 | static unsigned long slots_fetch_random(void) |
| 344 | { |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 345 | unsigned long slot; |
| 346 | int i; |
| 347 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 348 | /* Handle case of no slots stored. */ |
| 349 | if (slot_max == 0) |
| 350 | return 0; |
| 351 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 352 | slot = get_random_long("Physical") % slot_max; |
| 353 | |
| 354 | for (i = 0; i < slot_area_index; i++) { |
| 355 | if (slot >= slot_areas[i].num) { |
| 356 | slot -= slot_areas[i].num; |
| 357 | continue; |
| 358 | } |
| 359 | return slot_areas[i].addr + slot * CONFIG_PHYSICAL_ALIGN; |
| 360 | } |
| 361 | |
| 362 | if (i == slot_area_index) |
| 363 | debug_putstr("slots_fetch_random() failed!?\n"); |
| 364 | return 0; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static void process_e820_entry(struct e820entry *entry, |
| 368 | unsigned long minimum, |
| 369 | unsigned long image_size) |
| 370 | { |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 371 | struct mem_vector region, overlap; |
| 372 | struct slot_area slot_area; |
| 373 | unsigned long start_orig; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 374 | |
| 375 | /* Skip non-RAM entries. */ |
| 376 | if (entry->type != E820_RAM) |
| 377 | return; |
| 378 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 379 | /* On 32-bit, ignore entries entirely above our maximum. */ |
| 380 | if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= KERNEL_IMAGE_SIZE) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 381 | return; |
| 382 | |
| 383 | /* Ignore entries entirely below our minimum. */ |
| 384 | if (entry->addr + entry->size < minimum) |
| 385 | return; |
| 386 | |
| 387 | region.start = entry->addr; |
| 388 | region.size = entry->size; |
| 389 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 390 | /* Give up if slot area array is full. */ |
| 391 | while (slot_area_index < MAX_SLOT_AREA) { |
| 392 | start_orig = region.start; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 393 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 394 | /* Potentially raise address to minimum location. */ |
| 395 | if (region.start < minimum) |
| 396 | region.start = minimum; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 397 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 398 | /* Potentially raise address to meet alignment needs. */ |
| 399 | region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 400 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 401 | /* Did we raise the address above this e820 region? */ |
| 402 | if (region.start > entry->addr + entry->size) |
| 403 | return; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 404 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 405 | /* Reduce size by any delta from the original address. */ |
| 406 | region.size -= region.start - start_orig; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 407 | |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 408 | /* On 32-bit, reduce region size to fit within max size. */ |
| 409 | if (IS_ENABLED(CONFIG_X86_32) && |
| 410 | region.start + region.size > KERNEL_IMAGE_SIZE) |
| 411 | region.size = KERNEL_IMAGE_SIZE - region.start; |
| 412 | |
| 413 | /* Return if region can't contain decompressed kernel */ |
| 414 | if (region.size < image_size) |
| 415 | return; |
| 416 | |
| 417 | /* If nothing overlaps, store the region and return. */ |
| 418 | if (!mem_avoid_overlap(®ion, &overlap)) { |
| 419 | store_slot_info(®ion, image_size); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | /* Store beginning of region if holds at least image_size. */ |
| 424 | if (overlap.start > region.start + image_size) { |
| 425 | struct mem_vector beginning; |
| 426 | |
| 427 | beginning.start = region.start; |
| 428 | beginning.size = overlap.start - region.start; |
| 429 | store_slot_info(&beginning, image_size); |
| 430 | } |
| 431 | |
| 432 | /* Return if overlap extends to or past end of region. */ |
| 433 | if (overlap.start + overlap.size >= region.start + region.size) |
| 434 | return; |
| 435 | |
| 436 | /* Clip off the overlapping region and start over. */ |
| 437 | region.size -= overlap.start - region.start + overlap.size; |
| 438 | region.start = overlap.start + overlap.size; |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Baoquan He | 071a749 | 2016-05-09 13:22:08 -0700 | [diff] [blame] | 442 | static unsigned long find_random_phys_addr(unsigned long minimum, |
| 443 | unsigned long image_size) |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 444 | { |
| 445 | int i; |
| 446 | unsigned long addr; |
| 447 | |
| 448 | /* Make sure minimum is aligned. */ |
| 449 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); |
| 450 | |
| 451 | /* Verify potential e820 positions, appending to slots list. */ |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 452 | for (i = 0; i < boot_params->e820_entries; i++) { |
Baoquan He | 071a749 | 2016-05-09 13:22:08 -0700 | [diff] [blame] | 453 | process_e820_entry(&boot_params->e820_map[i], minimum, |
| 454 | image_size); |
Kees Cook | ed9f007 | 2016-05-25 15:45:33 -0700 | [diff] [blame] | 455 | if (slot_area_index == MAX_SLOT_AREA) { |
| 456 | debug_putstr("Aborted e820 scan (slot_areas full)!\n"); |
| 457 | break; |
| 458 | } |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return slots_fetch_random(); |
| 462 | } |
| 463 | |
Baoquan He | 071a749 | 2016-05-09 13:22:08 -0700 | [diff] [blame] | 464 | static unsigned long find_random_virt_addr(unsigned long minimum, |
| 465 | unsigned long image_size) |
| 466 | { |
| 467 | unsigned long slots, random_addr; |
| 468 | |
| 469 | /* Make sure minimum is aligned. */ |
| 470 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); |
| 471 | /* Align image_size for easy slot calculations. */ |
| 472 | image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN); |
| 473 | |
| 474 | /* |
| 475 | * There are how many CONFIG_PHYSICAL_ALIGN-sized slots |
| 476 | * that can hold image_size within the range of minimum to |
| 477 | * KERNEL_IMAGE_SIZE? |
| 478 | */ |
| 479 | slots = (KERNEL_IMAGE_SIZE - minimum - image_size) / |
| 480 | CONFIG_PHYSICAL_ALIGN + 1; |
| 481 | |
Kees Cook | d2d3462 | 2016-05-09 13:22:09 -0700 | [diff] [blame] | 482 | random_addr = get_random_long("Virtual") % slots; |
Baoquan He | 071a749 | 2016-05-09 13:22:08 -0700 | [diff] [blame] | 483 | |
| 484 | return random_addr * CONFIG_PHYSICAL_ALIGN + minimum; |
| 485 | } |
| 486 | |
Borislav Petkov | 549f90d | 2016-05-06 13:50:15 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Since this function examines addresses much more numerically, |
| 489 | * it takes the input and output pointers as 'unsigned long'. |
| 490 | */ |
Baoquan He | 8391c73 | 2016-05-25 15:45:32 -0700 | [diff] [blame] | 491 | void choose_random_location(unsigned long input, |
| 492 | unsigned long input_size, |
| 493 | unsigned long *output, |
| 494 | unsigned long output_size, |
| 495 | unsigned long *virt_addr) |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 496 | { |
Yinghai Lu | e066cc4 | 2016-05-25 15:45:34 -0700 | [diff] [blame] | 497 | unsigned long random_addr, min_addr; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 498 | |
Baoquan He | 8391c73 | 2016-05-25 15:45:32 -0700 | [diff] [blame] | 499 | /* By default, keep output position unchanged. */ |
| 500 | *virt_addr = *output; |
| 501 | |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 502 | if (cmdline_find_option_bool("nokaslr")) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 503 | warn("KASLR disabled: 'nokaslr' on cmdline."); |
Baoquan He | 8391c73 | 2016-05-25 15:45:32 -0700 | [diff] [blame] | 504 | return; |
Kees Cook | 24f2e02 | 2014-06-13 13:30:36 -0700 | [diff] [blame] | 505 | } |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 506 | |
Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 507 | boot_params->hdr.loadflags |= KASLR_FLAG; |
Borislav Petkov | 78cac48 | 2015-04-01 12:49:52 +0200 | [diff] [blame] | 508 | |
Kees Cook | 11fdf97 | 2016-05-25 15:45:31 -0700 | [diff] [blame] | 509 | /* Prepare to add new identity pagetables on demand. */ |
| 510 | initialize_identity_maps(); |
| 511 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 512 | /* Record the various known unsafe memory ranges. */ |
Baoquan He | 8391c73 | 2016-05-25 15:45:32 -0700 | [diff] [blame] | 513 | mem_avoid_init(input, input_size, *output); |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 514 | |
Yinghai Lu | e066cc4 | 2016-05-25 15:45:34 -0700 | [diff] [blame] | 515 | /* |
| 516 | * Low end of the randomization range should be the |
| 517 | * smaller of 512M or the initial kernel image |
| 518 | * location: |
| 519 | */ |
| 520 | min_addr = min(*output, 512UL << 20); |
| 521 | |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 522 | /* Walk e820 and find a random address. */ |
Yinghai Lu | e066cc4 | 2016-05-25 15:45:34 -0700 | [diff] [blame] | 523 | random_addr = find_random_phys_addr(min_addr, output_size); |
Kees Cook | 9016875 | 2016-04-18 09:42:15 -0700 | [diff] [blame] | 524 | if (!random_addr) { |
Kees Cook | 0f8ede1b | 2016-04-20 13:55:46 -0700 | [diff] [blame] | 525 | warn("KASLR disabled: could not find suitable E820 region!"); |
Baoquan He | 8391c73 | 2016-05-25 15:45:32 -0700 | [diff] [blame] | 526 | } else { |
| 527 | /* Update the new physical address location. */ |
| 528 | if (*output != random_addr) { |
| 529 | add_identity_map(random_addr, output_size); |
| 530 | *output = random_addr; |
| 531 | } |
Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Borislav Petkov | 36a39ac | 2016-05-07 11:59:40 +0200 | [diff] [blame] | 534 | /* This actually loads the identity pagetable on x86_64. */ |
Kees Cook | 3a94707 | 2016-05-06 15:01:35 -0700 | [diff] [blame] | 535 | finalize_identity_maps(); |
Baoquan He | 8391c73 | 2016-05-25 15:45:32 -0700 | [diff] [blame] | 536 | |
| 537 | /* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */ |
| 538 | if (IS_ENABLED(CONFIG_X86_64)) |
| 539 | random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size); |
| 540 | *virt_addr = random_addr; |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 541 | } |