4 #include <asm/archrandom.h>
7 #include <generated/compile.h>
8 #include <linux/module.h>
10 #include <linux/utsname.h>
11 #include <generated/utsrelease.h>
13 /* Simplified build-specific string for starting entropy. */
14 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
15 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
17 struct kaslr_setup_data {
24 #define I8254_PORT_CONTROL 0x43
25 #define I8254_PORT_COUNTER0 0x40
26 #define I8254_CMD_READBACK 0xC0
27 #define I8254_SELECT_COUNTER0 0x02
28 #define I8254_STATUS_NOTREADY 0x40
29 static inline u16 i8254(void)
34 outb(I8254_PORT_CONTROL,
35 I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
36 status = inb(I8254_PORT_COUNTER0);
37 timer = inb(I8254_PORT_COUNTER0);
38 timer |= inb(I8254_PORT_COUNTER0) << 8;
39 } while (status & I8254_STATUS_NOTREADY);
44 static unsigned long rotate_xor(unsigned long hash, const void *area,
48 unsigned long *ptr = (unsigned long *)area;
50 for (i = 0; i < size / sizeof(hash); i++) {
51 /* Rotate by odd number of bits and XOR. */
52 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
59 /* Attempt to create a simple but unpredictable starting entropy. */
60 static unsigned long get_random_boot(void)
62 unsigned long hash = 0;
64 hash = rotate_xor(hash, build_str, sizeof(build_str));
65 hash = rotate_xor(hash, real_mode, sizeof(*real_mode));
70 static unsigned long get_random_long(void)
73 const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
75 const unsigned long mix_const = 0x3f39e593UL;
77 unsigned long raw, random = get_random_boot();
78 bool use_i8254 = true;
80 debug_putstr("KASLR using");
82 if (has_cpuflag(X86_FEATURE_RDRAND)) {
83 debug_putstr(" RDRAND");
84 if (rdrand_long(&raw)) {
90 if (has_cpuflag(X86_FEATURE_TSC)) {
91 debug_putstr(" RDTSC");
99 debug_putstr(" i8254");
103 /* Circular multiply for better bit diffusion */
105 : "=a" (random), "=d" (raw)
106 : "a" (random), "rm" (mix_const));
109 debug_putstr("...\n");
119 #define MEM_AVOID_MAX 5
120 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
122 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
124 /* Item at least partially before region. */
125 if (item->start < region->start)
127 /* Item at least partially after region. */
128 if (item->start + item->size > region->start + region->size)
133 static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
135 /* Item one is entirely before item two. */
136 if (one->start + one->size <= two->start)
138 /* Item one is entirely after item two. */
139 if (one->start >= two->start + two->size)
144 static void mem_avoid_init(unsigned long input, unsigned long input_size,
145 unsigned long output, unsigned long output_size)
147 u64 initrd_start, initrd_size;
148 u64 cmd_line, cmd_line_size;
149 unsigned long unsafe, unsafe_len;
153 * Avoid the region that is unsafe to overlap during
154 * decompression (see calculations at top of misc.c).
156 unsafe_len = (output_size >> 12) + 32768 + 18;
157 unsafe = (unsigned long)input + input_size - unsafe_len;
158 mem_avoid[0].start = unsafe;
159 mem_avoid[0].size = unsafe_len;
162 initrd_start = (u64)real_mode->ext_ramdisk_image << 32;
163 initrd_start |= real_mode->hdr.ramdisk_image;
164 initrd_size = (u64)real_mode->ext_ramdisk_size << 32;
165 initrd_size |= real_mode->hdr.ramdisk_size;
166 mem_avoid[1].start = initrd_start;
167 mem_avoid[1].size = initrd_size;
169 /* Avoid kernel command line. */
170 cmd_line = (u64)real_mode->ext_cmd_line_ptr << 32;
171 cmd_line |= real_mode->hdr.cmd_line_ptr;
172 /* Calculate size of cmd_line. */
173 ptr = (char *)(unsigned long)cmd_line;
174 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
176 mem_avoid[2].start = cmd_line;
177 mem_avoid[2].size = cmd_line_size;
179 /* Avoid heap memory. */
180 mem_avoid[3].start = (unsigned long)free_mem_ptr;
181 mem_avoid[3].size = BOOT_HEAP_SIZE;
183 /* Avoid stack memory. */
184 mem_avoid[4].start = (unsigned long)free_mem_end_ptr;
185 mem_avoid[4].size = BOOT_STACK_SIZE;
188 /* Does this memory vector overlap a known avoided area? */
189 static bool mem_avoid_overlap(struct mem_vector *img)
192 struct setup_data *ptr;
194 for (i = 0; i < MEM_AVOID_MAX; i++) {
195 if (mem_overlaps(img, &mem_avoid[i]))
199 /* Avoid all entries in the setup_data linked list. */
200 ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data;
202 struct mem_vector avoid;
204 avoid.start = (unsigned long)ptr;
205 avoid.size = sizeof(*ptr) + ptr->len;
207 if (mem_overlaps(img, &avoid))
210 ptr = (struct setup_data *)(unsigned long)ptr->next;
216 static unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET /
217 CONFIG_PHYSICAL_ALIGN];
218 static unsigned long slot_max;
220 static void slots_append(unsigned long addr)
222 /* Overflowing the slots list should be impossible. */
223 if (slot_max >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET /
224 CONFIG_PHYSICAL_ALIGN)
227 slots[slot_max++] = addr;
230 static unsigned long slots_fetch_random(void)
232 /* Handle case of no slots stored. */
236 return slots[get_random_long() % slot_max];
239 static void process_e820_entry(struct e820entry *entry,
240 unsigned long minimum,
241 unsigned long image_size)
243 struct mem_vector region, img;
245 /* Skip non-RAM entries. */
246 if (entry->type != E820_RAM)
249 /* Ignore entries entirely above our maximum. */
250 if (entry->addr >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET)
253 /* Ignore entries entirely below our minimum. */
254 if (entry->addr + entry->size < minimum)
257 region.start = entry->addr;
258 region.size = entry->size;
260 /* Potentially raise address to minimum location. */
261 if (region.start < minimum)
262 region.start = minimum;
264 /* Potentially raise address to meet alignment requirements. */
265 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
267 /* Did we raise the address above the bounds of this e820 region? */
268 if (region.start > entry->addr + entry->size)
271 /* Reduce size by any delta from the original address. */
272 region.size -= region.start - entry->addr;
274 /* Reduce maximum size to fit end of image within maximum limit. */
275 if (region.start + region.size > CONFIG_RANDOMIZE_BASE_MAX_OFFSET)
276 region.size = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - region.start;
278 /* Walk each aligned slot and check for avoided areas. */
279 for (img.start = region.start, img.size = image_size ;
280 mem_contains(®ion, &img) ;
281 img.start += CONFIG_PHYSICAL_ALIGN) {
282 if (mem_avoid_overlap(&img))
284 slots_append(img.start);
288 static unsigned long find_random_addr(unsigned long minimum,
294 /* Make sure minimum is aligned. */
295 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
297 /* Verify potential e820 positions, appending to slots list. */
298 for (i = 0; i < real_mode->e820_entries; i++) {
299 process_e820_entry(&real_mode->e820_map[i], minimum, size);
302 return slots_fetch_random();
305 static void add_kaslr_setup_data(struct boot_params *params, __u8 enabled)
307 struct setup_data *data;
309 kaslr_setup_data.type = SETUP_KASLR;
310 kaslr_setup_data.len = 1;
311 kaslr_setup_data.next = 0;
312 kaslr_setup_data.data[0] = enabled;
314 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
316 while (data && data->next)
317 data = (struct setup_data *)(unsigned long)data->next;
320 data->next = (unsigned long)&kaslr_setup_data;
322 params->hdr.setup_data = (unsigned long)&kaslr_setup_data;
326 unsigned char *choose_kernel_location(struct boot_params *params,
327 unsigned char *input,
328 unsigned long input_size,
329 unsigned char *output,
330 unsigned long output_size)
332 unsigned long choice = (unsigned long)output;
333 unsigned long random;
335 #ifdef CONFIG_HIBERNATION
336 if (!cmdline_find_option_bool("kaslr")) {
337 debug_putstr("KASLR disabled by default...\n");
338 add_kaslr_setup_data(params, 0);
342 if (cmdline_find_option_bool("nokaslr")) {
343 debug_putstr("KASLR disabled by cmdline...\n");
344 add_kaslr_setup_data(params, 0);
348 add_kaslr_setup_data(params, 1);
350 /* Record the various known unsafe memory ranges. */
351 mem_avoid_init((unsigned long)input, input_size,
352 (unsigned long)output, output_size);
354 /* Walk e820 and find a random address. */
355 random = find_random_addr(choice, output_size);
357 debug_putstr("KASLR could not find suitable E820 region...\n");
361 /* Always enforce the minimum. */
367 return (unsigned char *)choice;