1 // SPDX-License-Identifier: GPL-2.0
5 * This contains the routines needed to generate a reasonable level of
6 * entropy to choose a randomized kernel base address offset in support
7 * of Kernel Address Space Layout Randomization (KASLR). Additionally
8 * handles walking the physical memory maps (and tracking memory regions
9 * to avoid) in order to select a physical memory location that can
10 * contain the entire properly aligned running kernel image.
15 * isspace() in linux/ctype.h is expected by next_args() to filter
16 * out "space/lf/tab". While boot/ctype.h conflicts with linux/ctype.h,
17 * since isdigit() is implemented in both of them. Hence disable it
24 #include "../string.h"
27 #include <generated/compile.h>
28 #include <linux/module.h>
29 #include <linux/uts.h>
30 #include <linux/utsname.h>
31 #include <linux/ctype.h>
32 #include <generated/utsrelease.h>
35 #include <asm/setup.h> /* For COMMAND_LINE_SIZE */
38 extern unsigned long get_cmd_line_ptr(void);
40 /* Simplified build-specific string for starting entropy. */
41 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
42 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
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_boot_seed(void)
62 unsigned long hash = 0;
64 hash = rotate_xor(hash, build_str, sizeof(build_str));
65 hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
70 #define KASLR_COMPRESSED_BOOT
71 #include "../../lib/kaslr.c"
74 /* Only supporting at most 4 unusable memmap regions with kaslr */
75 #define MAX_MEMMAP_REGIONS 4
77 static bool memmap_too_large;
81 * Store memory limit: MAXMEM on 64-bit and KERNEL_IMAGE_SIZE on 32-bit.
82 * It may be reduced by "mem=nn[KMG]" or "memmap=nn[KMG]" command line options.
86 /* Number of immovable memory regions */
87 static int num_immovable_mem;
89 enum mem_avoid_index {
90 MEM_AVOID_ZO_RANGE = 0,
94 MEM_AVOID_MEMMAP_BEGIN,
95 MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
99 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
101 static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
103 /* Item one is entirely before item two. */
104 if (one->start + one->size <= two->start)
106 /* Item one is entirely after item two. */
107 if (one->start >= two->start + two->size)
112 char *skip_spaces(const char *str)
114 while (isspace(*str))
118 #include "../../../../lib/ctype.c"
119 #include "../../../../lib/cmdline.c"
127 parse_memmap(char *p, u64 *start, u64 *size, enum parse_mode mode)
134 /* We don't care about this option here */
135 if (!strncmp(p, "exactmap", 8))
139 *size = memparse(p, &p);
147 *start = memparse(p + 1, &p);
150 if (mode == PARSE_MEMMAP) {
152 * memmap=nn@ss specifies usable region, should
160 * efi_fake_mem=nn@ss:attr the attr specifies
161 * flags that might imply a soft-reservation.
163 *start = memparse(p + 1, &p);
164 if (p && *p == ':') {
166 if (kstrtoull(p, 0, &flags) < 0)
168 else if (flags & EFI_MEMORY_SP)
176 * If w/o offset, only size specified, memmap=nn[KMG] has the
177 * same behaviour as mem=nn[KMG]. It limits the max address
178 * system can use. Region above the limit should be avoided.
187 static void mem_avoid_memmap(enum parse_mode mode, char *str)
191 if (i >= MAX_MEMMAP_REGIONS)
194 while (str && (i < MAX_MEMMAP_REGIONS)) {
197 char *k = strchr(str, ',');
202 rc = parse_memmap(str, &start, &size, mode);
208 /* Store the specified memory limit if size > 0 */
209 if (size > 0 && size < mem_limit)
215 mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].start = start;
216 mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].size = size;
220 /* More than 4 memmaps, fail kaslr */
221 if ((i >= MAX_MEMMAP_REGIONS) && str)
222 memmap_too_large = true;
225 /* Store the number of 1GB huge pages which users specified: */
226 static unsigned long max_gb_huge_pages;
228 static void parse_gb_huge_pages(char *param, char *val)
230 static bool gbpage_sz;
233 if (!strcmp(param, "hugepagesz")) {
235 if (memparse(p, &p) != PUD_SIZE) {
241 warn("Repeatedly set hugeTLB page size of 1G!\n");
246 if (!strcmp(param, "hugepages") && gbpage_sz) {
248 max_gb_huge_pages = simple_strtoull(p, &p, 0);
253 static void handle_mem_options(void)
255 char *args = (char *)get_cmd_line_ptr();
264 len = strnlen(args, COMMAND_LINE_SIZE-1);
265 tmp_cmdline = malloc(len + 1);
267 error("Failed to allocate space for tmp_cmdline");
269 memcpy(tmp_cmdline, args, len);
270 tmp_cmdline[len] = 0;
273 /* Chew leading spaces */
274 args = skip_spaces(args);
277 args = next_arg(args, ¶m, &val);
279 if (!val && strcmp(param, "--") == 0)
282 if (!strcmp(param, "memmap")) {
283 mem_avoid_memmap(PARSE_MEMMAP, val);
284 } else if (IS_ENABLED(CONFIG_X86_64) && strstr(param, "hugepages")) {
285 parse_gb_huge_pages(param, val);
286 } else if (!strcmp(param, "mem")) {
289 if (!strcmp(p, "nopentium"))
291 mem_size = memparse(p, &p);
295 if (mem_size < mem_limit)
296 mem_limit = mem_size;
297 } else if (!strcmp(param, "efi_fake_mem")) {
298 mem_avoid_memmap(PARSE_EFI, val);
307 * In theory, KASLR can put the kernel anywhere in the range of [16M, MAXMEM)
308 * on 64-bit, and [16M, KERNEL_IMAGE_SIZE) on 32-bit.
310 * The mem_avoid array is used to store the ranges that need to be avoided
311 * when KASLR searches for an appropriate random address. We must avoid any
312 * regions that are unsafe to overlap with during decompression, and other
313 * things like the initrd, cmdline and boot_params. This comment seeks to
314 * explain mem_avoid as clearly as possible since incorrect mem_avoid
315 * memory ranges lead to really hard to debug boot failures.
317 * The initrd, cmdline, and boot_params are trivial to identify for
318 * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and
319 * MEM_AVOID_BOOTPARAMS respectively below.
321 * What is not obvious how to avoid is the range of memory that is used
322 * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover
323 * the compressed kernel (ZO) and its run space, which is used to extract
324 * the uncompressed kernel (VO) and relocs.
326 * ZO's full run size sits against the end of the decompression buffer, so
327 * we can calculate where text, data, bss, etc of ZO are positioned more
330 * For additional background, the decompression calculations can be found
331 * in header.S, and the memory diagram is based on the one found in misc.c.
333 * The following conditions are already enforced by the image layouts and
335 * - input + input_size >= output + output_size
336 * - kernel_total_size <= init_size
337 * - kernel_total_size <= output_size (see Note below)
338 * - output + init_size >= output + output_size
340 * (Note that kernel_total_size and output_size have no fundamental
341 * relationship, but output_size is passed to choose_random_location
342 * as a maximum of the two. The diagram is showing a case where
343 * kernel_total_size is larger than output_size, but this case is
344 * handled by bumping output_size.)
346 * The above conditions can be illustrated by a diagram:
348 * 0 output input input+input_size output+init_size
351 * |-----|--------|--------|--------------|-----------|--|-------------|
354 * output+init_size-ZO_INIT_SIZE output+output_size output+kernel_total_size
356 * [output, output+init_size) is the entire memory range used for
357 * extracting the compressed image.
359 * [output, output+kernel_total_size) is the range needed for the
360 * uncompressed kernel (VO) and its run size (bss, brk, etc).
362 * [output, output+output_size) is VO plus relocs (i.e. the entire
363 * uncompressed payload contained by ZO). This is the area of the buffer
364 * written to during decompression.
366 * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case
367 * range of the copied ZO and decompression code. (i.e. the range
368 * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.)
370 * [input, input+input_size) is the original copied compressed image (ZO)
371 * (i.e. it does not include its run size). This range must be avoided
372 * because it contains the data used for decompression.
374 * [input+input_size, output+init_size) is [_text, _end) for ZO. This
375 * range includes ZO's heap and stack, and must be avoided since it
376 * performs the decompression.
378 * Since the above two ranges need to be avoided and they are adjacent,
379 * they can be merged, resulting in: [input, output+init_size) which
380 * becomes the MEM_AVOID_ZO_RANGE below.
382 static void mem_avoid_init(unsigned long input, unsigned long input_size,
383 unsigned long output)
385 unsigned long init_size = boot_params->hdr.init_size;
386 u64 initrd_start, initrd_size;
387 unsigned long cmd_line, cmd_line_size;
390 * Avoid the region that is unsafe to overlap during
393 mem_avoid[MEM_AVOID_ZO_RANGE].start = input;
394 mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input;
397 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
398 initrd_start |= boot_params->hdr.ramdisk_image;
399 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
400 initrd_size |= boot_params->hdr.ramdisk_size;
401 mem_avoid[MEM_AVOID_INITRD].start = initrd_start;
402 mem_avoid[MEM_AVOID_INITRD].size = initrd_size;
403 /* No need to set mapping for initrd, it will be handled in VO. */
405 /* Avoid kernel command line. */
406 cmd_line = get_cmd_line_ptr();
407 /* Calculate size of cmd_line. */
409 cmd_line_size = strnlen((char *)cmd_line, COMMAND_LINE_SIZE-1) + 1;
410 mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
411 mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
414 /* Avoid boot parameters. */
415 mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
416 mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params);
418 /* We don't need to set a mapping for setup_data. */
420 /* Mark the memmap regions we need to avoid */
421 handle_mem_options();
423 /* Enumerate the immovable memory regions */
424 num_immovable_mem = count_immovable_mem_regions();
428 * Does this memory vector overlap a known avoided area? If so, record the
429 * overlap region with the lowest address.
431 static bool mem_avoid_overlap(struct mem_vector *img,
432 struct mem_vector *overlap)
435 struct setup_data *ptr;
436 u64 earliest = img->start + img->size;
437 bool is_overlapping = false;
439 for (i = 0; i < MEM_AVOID_MAX; i++) {
440 if (mem_overlaps(img, &mem_avoid[i]) &&
441 mem_avoid[i].start < earliest) {
442 *overlap = mem_avoid[i];
443 earliest = overlap->start;
444 is_overlapping = true;
448 /* Avoid all entries in the setup_data linked list. */
449 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
451 struct mem_vector avoid;
453 avoid.start = (unsigned long)ptr;
454 avoid.size = sizeof(*ptr) + ptr->len;
456 if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
458 earliest = overlap->start;
459 is_overlapping = true;
462 if (ptr->type == SETUP_INDIRECT &&
463 ((struct setup_indirect *)ptr->data)->type != SETUP_INDIRECT) {
464 avoid.start = ((struct setup_indirect *)ptr->data)->addr;
465 avoid.size = ((struct setup_indirect *)ptr->data)->len;
467 if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
469 earliest = overlap->start;
470 is_overlapping = true;
474 ptr = (struct setup_data *)(unsigned long)ptr->next;
477 return is_overlapping;
485 #define MAX_SLOT_AREA 100
487 static struct slot_area slot_areas[MAX_SLOT_AREA];
488 static unsigned int slot_area_index;
489 static unsigned long slot_max;
491 static void store_slot_info(struct mem_vector *region, unsigned long image_size)
493 struct slot_area slot_area;
495 if (slot_area_index == MAX_SLOT_AREA)
498 slot_area.addr = region->start;
499 slot_area.num = 1 + (region->size - image_size) / CONFIG_PHYSICAL_ALIGN;
501 slot_areas[slot_area_index++] = slot_area;
502 slot_max += slot_area.num;
506 * Skip as many 1GB huge pages as possible in the passed region
507 * according to the number which users specified:
510 process_gb_huge_pages(struct mem_vector *region, unsigned long image_size)
512 u64 pud_start, pud_end;
513 unsigned long gb_huge_pages;
514 struct mem_vector tmp;
516 if (!IS_ENABLED(CONFIG_X86_64) || !max_gb_huge_pages) {
517 store_slot_info(region, image_size);
521 /* Are there any 1GB pages in the region? */
522 pud_start = ALIGN(region->start, PUD_SIZE);
523 pud_end = ALIGN_DOWN(region->start + region->size, PUD_SIZE);
525 /* No good 1GB huge pages found: */
526 if (pud_start >= pud_end) {
527 store_slot_info(region, image_size);
531 /* Check if the head part of the region is usable. */
532 if (pud_start >= region->start + image_size) {
533 tmp.start = region->start;
534 tmp.size = pud_start - region->start;
535 store_slot_info(&tmp, image_size);
538 /* Skip the good 1GB pages. */
539 gb_huge_pages = (pud_end - pud_start) >> PUD_SHIFT;
540 if (gb_huge_pages > max_gb_huge_pages) {
541 pud_end = pud_start + (max_gb_huge_pages << PUD_SHIFT);
542 max_gb_huge_pages = 0;
544 max_gb_huge_pages -= gb_huge_pages;
547 /* Check if the tail part of the region is usable. */
548 if (region->start + region->size >= pud_end + image_size) {
550 tmp.size = region->start + region->size - pud_end;
551 store_slot_info(&tmp, image_size);
555 static u64 slots_fetch_random(void)
560 /* Handle case of no slots stored. */
564 slot = kaslr_get_random_long("Physical") % slot_max;
566 for (i = 0; i < slot_area_index; i++) {
567 if (slot >= slot_areas[i].num) {
568 slot -= slot_areas[i].num;
571 return slot_areas[i].addr + ((u64)slot * CONFIG_PHYSICAL_ALIGN);
574 if (i == slot_area_index)
575 debug_putstr("slots_fetch_random() failed!?\n");
579 static void __process_mem_region(struct mem_vector *entry,
580 unsigned long minimum,
581 unsigned long image_size)
583 struct mem_vector region, overlap;
586 /* Enforce minimum and memory limit. */
587 region.start = max_t(u64, entry->start, minimum);
588 region_end = min(entry->start + entry->size, mem_limit);
590 /* Give up if slot area array is full. */
591 while (slot_area_index < MAX_SLOT_AREA) {
592 /* Potentially raise address to meet alignment needs. */
593 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
595 /* Did we raise the address above the passed in memory entry? */
596 if (region.start > region_end)
599 /* Reduce size by any delta from the original address. */
600 region.size = region_end - region.start;
602 /* Return if region can't contain decompressed kernel */
603 if (region.size < image_size)
606 /* If nothing overlaps, store the region and return. */
607 if (!mem_avoid_overlap(®ion, &overlap)) {
608 process_gb_huge_pages(®ion, image_size);
612 /* Store beginning of region if holds at least image_size. */
613 if (overlap.start >= region.start + image_size) {
614 region.size = overlap.start - region.start;
615 process_gb_huge_pages(®ion, image_size);
618 /* Clip off the overlapping region and start over. */
619 region.start = overlap.start + overlap.size;
623 static bool process_mem_region(struct mem_vector *region,
624 unsigned long minimum,
625 unsigned long image_size)
629 * If no immovable memory found, or MEMORY_HOTREMOVE disabled,
630 * use @region directly.
632 if (!num_immovable_mem) {
633 __process_mem_region(region, minimum, image_size);
635 if (slot_area_index == MAX_SLOT_AREA) {
636 debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n");
642 #if defined(CONFIG_MEMORY_HOTREMOVE) && defined(CONFIG_ACPI)
644 * If immovable memory found, filter the intersection between
645 * immovable memory and @region.
647 for (i = 0; i < num_immovable_mem; i++) {
648 u64 start, end, entry_end, region_end;
649 struct mem_vector entry;
651 if (!mem_overlaps(region, &immovable_mem[i]))
654 start = immovable_mem[i].start;
655 end = start + immovable_mem[i].size;
656 region_end = region->start + region->size;
658 entry.start = clamp(region->start, start, end);
659 entry_end = clamp(region_end, start, end);
660 entry.size = entry_end - entry.start;
662 __process_mem_region(&entry, minimum, image_size);
664 if (slot_area_index == MAX_SLOT_AREA) {
665 debug_putstr("Aborted e820/efi memmap scan when walking immovable regions(slot_areas full)!\n");
675 * Returns true if we processed the EFI memmap, which we prefer over the E820
676 * table if it is available.
679 process_efi_entries(unsigned long minimum, unsigned long image_size)
681 struct efi_info *e = &boot_params->efi_info;
682 bool efi_mirror_found = false;
683 struct mem_vector region;
684 efi_memory_desc_t *md;
690 signature = (char *)&e->efi_loader_signature;
691 if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
692 strncmp(signature, EFI64_LOADER_SIGNATURE, 4))
696 /* Can't handle data above 4GB at this time */
697 if (e->efi_memmap_hi) {
698 warn("EFI memmap is above 4GB, can't be handled now on x86_32. EFI should be disabled.\n");
701 pmap = e->efi_memmap;
703 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
706 nr_desc = e->efi_memmap_size / e->efi_memdesc_size;
707 for (i = 0; i < nr_desc; i++) {
708 md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
709 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
710 efi_mirror_found = true;
715 for (i = 0; i < nr_desc; i++) {
716 md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
719 * Here we are more conservative in picking free memory than
720 * the EFI spec allows:
722 * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also
723 * free memory and thus available to place the kernel image into,
724 * but in practice there's firmware where using that memory leads
727 * Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free.
729 if (md->type != EFI_CONVENTIONAL_MEMORY)
732 if (efi_soft_reserve_enabled() &&
733 (md->attribute & EFI_MEMORY_SP))
736 if (efi_mirror_found &&
737 !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
740 region.start = md->phys_addr;
741 region.size = md->num_pages << EFI_PAGE_SHIFT;
742 if (process_mem_region(®ion, minimum, image_size))
749 process_efi_entries(unsigned long minimum, unsigned long image_size)
755 static void process_e820_entries(unsigned long minimum,
756 unsigned long image_size)
759 struct mem_vector region;
760 struct boot_e820_entry *entry;
762 /* Verify potential e820 positions, appending to slots list. */
763 for (i = 0; i < boot_params->e820_entries; i++) {
764 entry = &boot_params->e820_table[i];
765 /* Skip non-RAM entries. */
766 if (entry->type != E820_TYPE_RAM)
768 region.start = entry->addr;
769 region.size = entry->size;
770 if (process_mem_region(®ion, minimum, image_size))
775 static unsigned long find_random_phys_addr(unsigned long minimum,
776 unsigned long image_size)
780 /* Bail out early if it's impossible to succeed. */
781 if (minimum + image_size > mem_limit)
784 /* Check if we had too many memmaps. */
785 if (memmap_too_large) {
786 debug_putstr("Aborted memory entries scan (more than 4 memmap= args)!\n");
790 if (!process_efi_entries(minimum, image_size))
791 process_e820_entries(minimum, image_size);
793 phys_addr = slots_fetch_random();
795 /* Perform a final check to make sure the address is in range. */
796 if (phys_addr < minimum || phys_addr + image_size > mem_limit) {
797 warn("Invalid physical address chosen!\n");
801 return (unsigned long)phys_addr;
804 static unsigned long find_random_virt_addr(unsigned long minimum,
805 unsigned long image_size)
807 unsigned long slots, random_addr;
810 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
811 * that can hold image_size within the range of minimum to
814 slots = 1 + (KERNEL_IMAGE_SIZE - minimum - image_size) / CONFIG_PHYSICAL_ALIGN;
816 random_addr = kaslr_get_random_long("Virtual") % slots;
818 return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
822 * Since this function examines addresses much more numerically,
823 * it takes the input and output pointers as 'unsigned long'.
825 void choose_random_location(unsigned long input,
826 unsigned long input_size,
827 unsigned long *output,
828 unsigned long output_size,
829 unsigned long *virt_addr)
831 unsigned long random_addr, min_addr;
833 if (cmdline_find_option_bool("nokaslr")) {
834 warn("KASLR disabled: 'nokaslr' on cmdline.");
838 boot_params->hdr.loadflags |= KASLR_FLAG;
840 if (IS_ENABLED(CONFIG_X86_32))
841 mem_limit = KERNEL_IMAGE_SIZE;
845 /* Record the various known unsafe memory ranges. */
846 mem_avoid_init(input, input_size, *output);
849 * Low end of the randomization range should be the
850 * smaller of 512M or the initial kernel image
853 min_addr = min(*output, 512UL << 20);
854 /* Make sure minimum is aligned. */
855 min_addr = ALIGN(min_addr, CONFIG_PHYSICAL_ALIGN);
857 /* Walk available memory entries to find a random address. */
858 random_addr = find_random_phys_addr(min_addr, output_size);
860 warn("Physical KASLR disabled: no suitable memory region!");
862 /* Update the new physical address location. */
863 if (*output != random_addr)
864 *output = random_addr;
868 /* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
869 if (IS_ENABLED(CONFIG_X86_64))
870 random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size);
871 *virt_addr = random_addr;