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.
15 #include <asm/archrandom.h>
18 #include <generated/compile.h>
19 #include <linux/module.h>
20 #include <linux/uts.h>
21 #include <linux/utsname.h>
22 #include <generated/utsrelease.h>
24 /* Simplified build-specific string for starting entropy. */
25 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
26 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
28 #define I8254_PORT_CONTROL 0x43
29 #define I8254_PORT_COUNTER0 0x40
30 #define I8254_CMD_READBACK 0xC0
31 #define I8254_SELECT_COUNTER0 0x02
32 #define I8254_STATUS_NOTREADY 0x40
33 static inline u16 i8254(void)
38 outb(I8254_PORT_CONTROL,
39 I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
40 status = inb(I8254_PORT_COUNTER0);
41 timer = inb(I8254_PORT_COUNTER0);
42 timer |= inb(I8254_PORT_COUNTER0) << 8;
43 } while (status & I8254_STATUS_NOTREADY);
48 static unsigned long rotate_xor(unsigned long hash, const void *area,
52 unsigned long *ptr = (unsigned long *)area;
54 for (i = 0; i < size / sizeof(hash); i++) {
55 /* Rotate by odd number of bits and XOR. */
56 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
63 /* Attempt to create a simple but unpredictable starting entropy. */
64 static unsigned long get_random_boot(void)
66 unsigned long hash = 0;
68 hash = rotate_xor(hash, build_str, sizeof(build_str));
69 hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
74 static unsigned long get_random_long(void)
77 const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
79 const unsigned long mix_const = 0x3f39e593UL;
81 unsigned long raw, random = get_random_boot();
82 bool use_i8254 = true;
84 debug_putstr("KASLR using");
86 if (has_cpuflag(X86_FEATURE_RDRAND)) {
87 debug_putstr(" RDRAND");
88 if (rdrand_long(&raw)) {
94 if (has_cpuflag(X86_FEATURE_TSC)) {
95 debug_putstr(" RDTSC");
103 debug_putstr(" i8254");
107 /* Circular multiply for better bit diffusion */
109 : "=a" (random), "=d" (raw)
110 : "a" (random), "rm" (mix_const));
113 debug_putstr("...\n");
123 #define MEM_AVOID_MAX 5
124 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
126 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
128 /* Item at least partially before region. */
129 if (item->start < region->start)
131 /* Item at least partially after region. */
132 if (item->start + item->size > region->start + region->size)
137 static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
139 /* Item one is entirely before item two. */
140 if (one->start + one->size <= two->start)
142 /* Item one is entirely after item two. */
143 if (one->start >= two->start + two->size)
148 static void mem_avoid_init(unsigned long input, unsigned long input_size,
149 unsigned long output, unsigned long output_size)
151 u64 initrd_start, initrd_size;
152 u64 cmd_line, cmd_line_size;
153 unsigned long unsafe, unsafe_len;
157 * Avoid the region that is unsafe to overlap during
158 * decompression (see calculations in ../header.S).
160 unsafe_len = (output_size >> 12) + 32768 + 18;
161 unsafe = (unsigned long)input + input_size - unsafe_len;
162 mem_avoid[0].start = unsafe;
163 mem_avoid[0].size = unsafe_len;
166 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
167 initrd_start |= boot_params->hdr.ramdisk_image;
168 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
169 initrd_size |= boot_params->hdr.ramdisk_size;
170 mem_avoid[1].start = initrd_start;
171 mem_avoid[1].size = initrd_size;
173 /* Avoid kernel command line. */
174 cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
175 cmd_line |= boot_params->hdr.cmd_line_ptr;
176 /* Calculate size of cmd_line. */
177 ptr = (char *)(unsigned long)cmd_line;
178 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
180 mem_avoid[2].start = cmd_line;
181 mem_avoid[2].size = cmd_line_size;
183 /* Avoid heap memory. */
184 mem_avoid[3].start = (unsigned long)free_mem_ptr;
185 mem_avoid[3].size = BOOT_HEAP_SIZE;
187 /* Avoid stack memory. */
188 mem_avoid[4].start = (unsigned long)free_mem_end_ptr;
189 mem_avoid[4].size = BOOT_STACK_SIZE;
192 /* Does this memory vector overlap a known avoided area? */
193 static bool mem_avoid_overlap(struct mem_vector *img)
196 struct setup_data *ptr;
198 for (i = 0; i < MEM_AVOID_MAX; i++) {
199 if (mem_overlaps(img, &mem_avoid[i]))
203 /* Avoid all entries in the setup_data linked list. */
204 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
206 struct mem_vector avoid;
208 avoid.start = (unsigned long)ptr;
209 avoid.size = sizeof(*ptr) + ptr->len;
211 if (mem_overlaps(img, &avoid))
214 ptr = (struct setup_data *)(unsigned long)ptr->next;
220 static unsigned long slots[KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN];
221 static unsigned long slot_max;
223 static void slots_append(unsigned long addr)
225 /* Overflowing the slots list should be impossible. */
226 if (slot_max >= KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN)
229 slots[slot_max++] = addr;
232 static unsigned long slots_fetch_random(void)
234 /* Handle case of no slots stored. */
238 return slots[get_random_long() % slot_max];
241 static void process_e820_entry(struct e820entry *entry,
242 unsigned long minimum,
243 unsigned long image_size)
245 struct mem_vector region, img;
247 /* Skip non-RAM entries. */
248 if (entry->type != E820_RAM)
251 /* Ignore entries entirely above our maximum. */
252 if (entry->addr >= KERNEL_IMAGE_SIZE)
255 /* Ignore entries entirely below our minimum. */
256 if (entry->addr + entry->size < minimum)
259 region.start = entry->addr;
260 region.size = entry->size;
262 /* Potentially raise address to minimum location. */
263 if (region.start < minimum)
264 region.start = minimum;
266 /* Potentially raise address to meet alignment requirements. */
267 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
269 /* Did we raise the address above the bounds of this e820 region? */
270 if (region.start > entry->addr + entry->size)
273 /* Reduce size by any delta from the original address. */
274 region.size -= region.start - entry->addr;
276 /* Reduce maximum size to fit end of image within maximum limit. */
277 if (region.start + region.size > KERNEL_IMAGE_SIZE)
278 region.size = KERNEL_IMAGE_SIZE - region.start;
280 /* Walk each aligned slot and check for avoided areas. */
281 for (img.start = region.start, img.size = image_size ;
282 mem_contains(®ion, &img) ;
283 img.start += CONFIG_PHYSICAL_ALIGN) {
284 if (mem_avoid_overlap(&img))
286 slots_append(img.start);
290 static unsigned long find_random_addr(unsigned long minimum,
296 /* Make sure minimum is aligned. */
297 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
299 /* Verify potential e820 positions, appending to slots list. */
300 for (i = 0; i < boot_params->e820_entries; i++) {
301 process_e820_entry(&boot_params->e820_map[i], minimum, size);
304 return slots_fetch_random();
307 unsigned char *choose_random_location(unsigned char *input,
308 unsigned long input_size,
309 unsigned char *output,
310 unsigned long output_size)
312 unsigned long choice = (unsigned long)output;
313 unsigned long random_addr;
315 #ifdef CONFIG_HIBERNATION
316 if (!cmdline_find_option_bool("kaslr")) {
317 warn("KASLR disabled: 'kaslr' not on cmdline (hibernation selected).");
321 if (cmdline_find_option_bool("nokaslr")) {
322 warn("KASLR disabled: 'nokaslr' on cmdline.");
327 boot_params->hdr.loadflags |= KASLR_FLAG;
329 /* Record the various known unsafe memory ranges. */
330 mem_avoid_init((unsigned long)input, input_size,
331 (unsigned long)output, output_size);
333 /* Walk e820 and find a random address. */
334 random_addr = find_random_addr(choice, output_size);
336 warn("KASLR disabled: could not find suitable E820 region!");
340 /* Always enforce the minimum. */
341 if (random_addr < choice)
344 choice = random_addr;
346 return (unsigned char *)choice;