Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This file implements KASLR memory randomization for x86_64. It randomizes
0004  * the virtual address space of kernel memory regions (physical memory
0005  * mapping, vmalloc & vmemmap) for x86_64. This security feature mitigates
0006  * exploits relying on predictable kernel addresses.
0007  *
0008  * Entropy is generated using the KASLR early boot functions now shared in
0009  * the lib directory (originally written by Kees Cook). Randomization is
0010  * done on PGD & P4D/PUD page table levels to increase possible addresses.
0011  * The physical memory mapping code was adapted to support P4D/PUD level
0012  * virtual addresses. This implementation on the best configuration provides
0013  * 30,000 possible virtual addresses in average for each memory region.
0014  * An additional low memory page is used to ensure each CPU can start with
0015  * a PGD aligned virtual address (for realmode).
0016  *
0017  * The order of each memory region is not changed. The feature looks at
0018  * the available space for the regions based on different configuration
0019  * options and randomizes the base and space between each. The size of the
0020  * physical memory mapping is the available physical memory.
0021  */
0022 
0023 #include <linux/kernel.h>
0024 #include <linux/init.h>
0025 #include <linux/random.h>
0026 #include <linux/memblock.h>
0027 #include <linux/pgtable.h>
0028 
0029 #include <asm/setup.h>
0030 #include <asm/kaslr.h>
0031 
0032 #include "mm_internal.h"
0033 
0034 #define TB_SHIFT 40
0035 
0036 /*
0037  * The end address could depend on more configuration options to make the
0038  * highest amount of space for randomization available, but that's too hard
0039  * to keep straight and caused issues already.
0040  */
0041 static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;
0042 
0043 /*
0044  * Memory regions randomized by KASLR (except modules that use a separate logic
0045  * earlier during boot). The list is ordered based on virtual addresses. This
0046  * order is kept after randomization.
0047  */
0048 static __initdata struct kaslr_memory_region {
0049     unsigned long *base;
0050     unsigned long size_tb;
0051 } kaslr_regions[] = {
0052     { &page_offset_base, 0 },
0053     { &vmalloc_base, 0 },
0054     { &vmemmap_base, 0 },
0055 };
0056 
0057 /* Get size in bytes used by the memory region */
0058 static inline unsigned long get_padding(struct kaslr_memory_region *region)
0059 {
0060     return (region->size_tb << TB_SHIFT);
0061 }
0062 
0063 /* Initialize base and padding for each memory region randomized with KASLR */
0064 void __init kernel_randomize_memory(void)
0065 {
0066     size_t i;
0067     unsigned long vaddr_start, vaddr;
0068     unsigned long rand, memory_tb;
0069     struct rnd_state rand_state;
0070     unsigned long remain_entropy;
0071     unsigned long vmemmap_size;
0072 
0073     vaddr_start = pgtable_l5_enabled() ? __PAGE_OFFSET_BASE_L5 : __PAGE_OFFSET_BASE_L4;
0074     vaddr = vaddr_start;
0075 
0076     /*
0077      * These BUILD_BUG_ON checks ensure the memory layout is consistent
0078      * with the vaddr_start/vaddr_end variables. These checks are very
0079      * limited....
0080      */
0081     BUILD_BUG_ON(vaddr_start >= vaddr_end);
0082     BUILD_BUG_ON(vaddr_end != CPU_ENTRY_AREA_BASE);
0083     BUILD_BUG_ON(vaddr_end > __START_KERNEL_map);
0084 
0085     if (!kaslr_memory_enabled())
0086         return;
0087 
0088     kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT);
0089     kaslr_regions[1].size_tb = VMALLOC_SIZE_TB;
0090 
0091     /*
0092      * Update Physical memory mapping to available and
0093      * add padding if needed (especially for memory hotplug support).
0094      */
0095     BUG_ON(kaslr_regions[0].base != &page_offset_base);
0096     memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
0097         CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
0098 
0099     /* Adapt physical memory region size based on available memory */
0100     if (memory_tb < kaslr_regions[0].size_tb)
0101         kaslr_regions[0].size_tb = memory_tb;
0102 
0103     /*
0104      * Calculate the vmemmap region size in TBs, aligned to a TB
0105      * boundary.
0106      */
0107     vmemmap_size = (kaslr_regions[0].size_tb << (TB_SHIFT - PAGE_SHIFT)) *
0108             sizeof(struct page);
0109     kaslr_regions[2].size_tb = DIV_ROUND_UP(vmemmap_size, 1UL << TB_SHIFT);
0110 
0111     /* Calculate entropy available between regions */
0112     remain_entropy = vaddr_end - vaddr_start;
0113     for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++)
0114         remain_entropy -= get_padding(&kaslr_regions[i]);
0115 
0116     prandom_seed_state(&rand_state, kaslr_get_random_long("Memory"));
0117 
0118     for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) {
0119         unsigned long entropy;
0120 
0121         /*
0122          * Select a random virtual address using the extra entropy
0123          * available.
0124          */
0125         entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i);
0126         prandom_bytes_state(&rand_state, &rand, sizeof(rand));
0127         entropy = (rand % (entropy + 1)) & PUD_MASK;
0128         vaddr += entropy;
0129         *kaslr_regions[i].base = vaddr;
0130 
0131         /*
0132          * Jump the region and add a minimum padding based on
0133          * randomization alignment.
0134          */
0135         vaddr += get_padding(&kaslr_regions[i]);
0136         vaddr = round_up(vaddr + 1, PUD_SIZE);
0137         remain_entropy -= entropy;
0138     }
0139 }
0140 
0141 void __meminit init_trampoline_kaslr(void)
0142 {
0143     pud_t *pud_page_tramp, *pud, *pud_tramp;
0144     p4d_t *p4d_page_tramp, *p4d, *p4d_tramp;
0145     unsigned long paddr, vaddr;
0146     pgd_t *pgd;
0147 
0148     pud_page_tramp = alloc_low_page();
0149 
0150     /*
0151      * There are two mappings for the low 1MB area, the direct mapping
0152      * and the 1:1 mapping for the real mode trampoline:
0153      *
0154      * Direct mapping: virt_addr = phys_addr + PAGE_OFFSET
0155      * 1:1 mapping:    virt_addr = phys_addr
0156      */
0157     paddr = 0;
0158     vaddr = (unsigned long)__va(paddr);
0159     pgd = pgd_offset_k(vaddr);
0160 
0161     p4d = p4d_offset(pgd, vaddr);
0162     pud = pud_offset(p4d, vaddr);
0163 
0164     pud_tramp = pud_page_tramp + pud_index(paddr);
0165     *pud_tramp = *pud;
0166 
0167     if (pgtable_l5_enabled()) {
0168         p4d_page_tramp = alloc_low_page();
0169 
0170         p4d_tramp = p4d_page_tramp + p4d_index(paddr);
0171 
0172         set_p4d(p4d_tramp,
0173             __p4d(_KERNPG_TABLE | __pa(pud_page_tramp)));
0174 
0175         set_pgd(&trampoline_pgd_entry,
0176             __pgd(_KERNPG_TABLE | __pa(p4d_page_tramp)));
0177     } else {
0178         set_pgd(&trampoline_pgd_entry,
0179             __pgd(_KERNPG_TABLE | __pa(pud_page_tramp)));
0180     }
0181 }