Commit | Line | Data |
---|---|---|
0483e1fa TG |
1 | /* |
2 | * This file implements KASLR memory randomization for x86_64. It randomizes | |
3 | * the virtual address space of kernel memory regions (physical memory | |
4 | * mapping, vmalloc & vmemmap) for x86_64. This security feature mitigates | |
5 | * exploits relying on predictable kernel addresses. | |
6 | * | |
7 | * Entropy is generated using the KASLR early boot functions now shared in | |
8 | * the lib directory (originally written by Kees Cook). Randomization is | |
9 | * done on PGD & PUD page table levels to increase possible addresses. The | |
10 | * physical memory mapping code was adapted to support PUD level virtual | |
11 | * addresses. This implementation on the best configuration provides 30,000 | |
12 | * possible virtual addresses in average for each memory region. An additional | |
13 | * low memory page is used to ensure each CPU can start with a PGD aligned | |
14 | * virtual address (for realmode). | |
15 | * | |
16 | * The order of each memory region is not changed. The feature looks at | |
17 | * the available space for the regions based on different configuration | |
18 | * options and randomizes the base and space between each. The size of the | |
19 | * physical memory mapping is the available physical memory. | |
20 | */ | |
21 | ||
22 | #include <linux/kernel.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/random.h> | |
25 | ||
26 | #include <asm/pgalloc.h> | |
27 | #include <asm/pgtable.h> | |
28 | #include <asm/setup.h> | |
29 | #include <asm/kaslr.h> | |
30 | ||
31 | #include "mm_internal.h" | |
32 | ||
33 | #define TB_SHIFT 40 | |
34 | ||
35 | /* | |
36 | * Virtual address start and end range for randomization. The end changes base | |
37 | * on configuration to have the highest amount of space for randomization. | |
38 | * It increases the possible random position for each randomized region. | |
39 | * | |
40 | * You need to add an if/def entry if you introduce a new memory region | |
41 | * compatible with KASLR. Your entry must be in logical order with memory | |
42 | * layout. For example, ESPFIX is before EFI because its virtual address is | |
43 | * before. You also need to add a BUILD_BUG_ON in kernel_randomize_memory to | |
44 | * ensure that this order is correct and won't be changed. | |
45 | */ | |
021182e5 | 46 | static const unsigned long vaddr_start = __PAGE_OFFSET_BASE; |
a95ae27c | 47 | static const unsigned long vaddr_end = VMEMMAP_START; |
021182e5 TG |
48 | |
49 | /* Default values */ | |
50 | unsigned long page_offset_base = __PAGE_OFFSET_BASE; | |
51 | EXPORT_SYMBOL(page_offset_base); | |
a95ae27c TG |
52 | unsigned long vmalloc_base = __VMALLOC_BASE; |
53 | EXPORT_SYMBOL(vmalloc_base); | |
0483e1fa TG |
54 | |
55 | /* | |
56 | * Memory regions randomized by KASLR (except modules that use a separate logic | |
57 | * earlier during boot). The list is ordered based on virtual addresses. This | |
58 | * order is kept after randomization. | |
59 | */ | |
60 | static __initdata struct kaslr_memory_region { | |
61 | unsigned long *base; | |
62 | unsigned long size_tb; | |
63 | } kaslr_regions[] = { | |
021182e5 | 64 | { &page_offset_base, 64/* Maximum */ }, |
a95ae27c | 65 | { &vmalloc_base, VMALLOC_SIZE_TB }, |
0483e1fa TG |
66 | }; |
67 | ||
68 | /* Get size in bytes used by the memory region */ | |
69 | static inline unsigned long get_padding(struct kaslr_memory_region *region) | |
70 | { | |
71 | return (region->size_tb << TB_SHIFT); | |
72 | } | |
73 | ||
74 | /* | |
75 | * Apply no randomization if KASLR was disabled at boot or if KASAN | |
76 | * is enabled. KASAN shadow mappings rely on regions being PGD aligned. | |
77 | */ | |
78 | static inline bool kaslr_memory_enabled(void) | |
79 | { | |
80 | return kaslr_enabled() && !config_enabled(CONFIG_KASAN); | |
81 | } | |
82 | ||
83 | /* Initialize base and padding for each memory region randomized with KASLR */ | |
84 | void __init kernel_randomize_memory(void) | |
85 | { | |
86 | size_t i; | |
87 | unsigned long vaddr = vaddr_start; | |
021182e5 | 88 | unsigned long rand, memory_tb; |
0483e1fa TG |
89 | struct rnd_state rand_state; |
90 | unsigned long remain_entropy; | |
91 | ||
92 | if (!kaslr_memory_enabled()) | |
93 | return; | |
94 | ||
021182e5 TG |
95 | BUG_ON(kaslr_regions[0].base != &page_offset_base); |
96 | memory_tb = ((max_pfn << PAGE_SHIFT) >> TB_SHIFT); | |
97 | ||
98 | /* Adapt phyiscal memory region size based on available memory */ | |
99 | if (memory_tb < kaslr_regions[0].size_tb) | |
100 | kaslr_regions[0].size_tb = memory_tb; | |
101 | ||
0483e1fa TG |
102 | /* Calculate entropy available between regions */ |
103 | remain_entropy = vaddr_end - vaddr_start; | |
104 | for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) | |
105 | remain_entropy -= get_padding(&kaslr_regions[i]); | |
106 | ||
107 | prandom_seed_state(&rand_state, kaslr_get_random_long("Memory")); | |
108 | ||
109 | for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) { | |
110 | unsigned long entropy; | |
111 | ||
112 | /* | |
113 | * Select a random virtual address using the extra entropy | |
114 | * available. | |
115 | */ | |
116 | entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i); | |
117 | prandom_bytes_state(&rand_state, &rand, sizeof(rand)); | |
118 | entropy = (rand % (entropy + 1)) & PUD_MASK; | |
119 | vaddr += entropy; | |
120 | *kaslr_regions[i].base = vaddr; | |
121 | ||
122 | /* | |
123 | * Jump the region and add a minimum padding based on | |
124 | * randomization alignment. | |
125 | */ | |
126 | vaddr += get_padding(&kaslr_regions[i]); | |
127 | vaddr = round_up(vaddr + 1, PUD_SIZE); | |
128 | remain_entropy -= entropy; | |
129 | } | |
130 | } | |
131 | ||
132 | /* | |
133 | * Create PGD aligned trampoline table to allow real mode initialization | |
134 | * of additional CPUs. Consume only 1 low memory page. | |
135 | */ | |
136 | void __meminit init_trampoline(void) | |
137 | { | |
138 | unsigned long paddr, paddr_next; | |
139 | pgd_t *pgd; | |
140 | pud_t *pud_page, *pud_page_tramp; | |
141 | int i; | |
142 | ||
143 | if (!kaslr_memory_enabled()) { | |
144 | init_trampoline_default(); | |
145 | return; | |
146 | } | |
147 | ||
148 | pud_page_tramp = alloc_low_page(); | |
149 | ||
150 | paddr = 0; | |
151 | pgd = pgd_offset_k((unsigned long)__va(paddr)); | |
152 | pud_page = (pud_t *) pgd_page_vaddr(*pgd); | |
153 | ||
154 | for (i = pud_index(paddr); i < PTRS_PER_PUD; i++, paddr = paddr_next) { | |
155 | pud_t *pud, *pud_tramp; | |
156 | unsigned long vaddr = (unsigned long)__va(paddr); | |
157 | ||
158 | pud_tramp = pud_page_tramp + pud_index(paddr); | |
159 | pud = pud_page + pud_index(vaddr); | |
160 | paddr_next = (paddr & PUD_MASK) + PUD_SIZE; | |
161 | ||
162 | *pud_tramp = *pud; | |
163 | } | |
164 | ||
165 | set_pgd(&trampoline_pgd_entry, | |
166 | __pgd(_KERNPG_TABLE | __pa(pud_page_tramp))); | |
167 | } |