// SPDX-License-Identifier: GPL-2.0 /* * This file implements KASLR memory randomization for x86_64. It randomizes * the virtual address space of kernel memory regions (physical memory * mapping, vmalloc & vmemmap) for x86_64. This security feature mitigates * exploits relying on predictable kernel addresses. * * Entropy is generated using the KASLR early boot functions now shared in * the lib directory (originally written by Kees Cook). Randomization is * done on PGD & P4D/PUD page table levels to increase possible addresses. * The physical memory mapping code was adapted to support P4D/PUD level * virtual addresses. This implementation on the best configuration provides * 30,000 possible virtual addresses in average for each memory region. * An additional low memory page is used to ensure each CPU can start with * a PGD aligned virtual address (for realmode). * * The order of each memory region is not changed. The feature looks at * the available space for the regions based on different configuration * options and randomizes the base and space between each. The size of the * physical memory mapping is the available physical memory. */ #include <linux/kernel.h> #include <linux/init.h> #include <linux/random.h> #include <linux/memblock.h> #include <linux/pgtable.h> #include <asm/setup.h> #include <asm/kaslr.h> #include "mm_internal.h" #define TB_SHIFT … /* * The end address could depend on more configuration options to make the * highest amount of space for randomization available, but that's too hard * to keep straight and caused issues already. */ static const unsigned long vaddr_end = …; /* * Memory regions randomized by KASLR (except modules that use a separate logic * earlier during boot). The list is ordered based on virtual addresses. This * order is kept after randomization. */ static __initdata struct kaslr_memory_region { … } kaslr_regions[] = …; /* Get size in bytes used by the memory region */ static inline unsigned long get_padding(struct kaslr_memory_region *region) { … } /* Initialize base and padding for each memory region randomized with KASLR */ void __init kernel_randomize_memory(void) { … } void __meminit init_trampoline_kaslr(void) { … }