blob: 47388f0c0e59649ca3574d4e7c31b356dad7d247 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andrey Ryabininbe3606f2017-03-13 19:33:37 +03002#define DISABLE_BRANCH_PROFILING
Andrey Ryabinin85155222015-07-02 12:09:37 +03003#define pr_fmt(fmt) "kasan: " fmt
Andrey Ryabininef7f0d62015-02-13 14:39:25 -08004#include <linux/bootmem.h>
5#include <linux/kasan.h>
6#include <linux/kdebug.h>
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -08007#include <linux/memblock.h>
Andrey Ryabininef7f0d62015-02-13 14:39:25 -08008#include <linux/mm.h>
9#include <linux/sched.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010010#include <linux/sched/task.h>
Andrey Ryabininef7f0d62015-02-13 14:39:25 -080011#include <linux/vmalloc.h>
12
Ingo Molnar5520b7e2017-01-27 11:59:46 +010013#include <asm/e820/types.h>
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080014#include <asm/pgalloc.h>
Andrey Ryabininef7f0d62015-02-13 14:39:25 -080015#include <asm/tlbflush.h>
16#include <asm/sections.h>
Tom Lendackyb9d05202017-07-17 16:10:11 -050017#include <asm/pgtable.h>
Thomas Gleixner92a0f812017-12-20 18:51:31 +010018#include <asm/cpu_entry_area.h>
Andrey Ryabininef7f0d62015-02-13 14:39:25 -080019
Ingo Molnar08b46d52017-01-28 17:29:08 +010020extern struct range pfn_mapped[E820_MAX_ENTRIES];
Andrey Ryabininef7f0d62015-02-13 14:39:25 -080021
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +030022static p4d_t tmp_p4d_table[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
23
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080024static __init void *early_alloc(size_t size, int nid)
25{
26 return memblock_virt_alloc_try_nid_nopanic(size, size,
27 __pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid);
28}
29
30static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr,
31 unsigned long end, int nid)
32{
33 pte_t *pte;
34
35 if (pmd_none(*pmd)) {
36 void *p;
37
38 if (boot_cpu_has(X86_FEATURE_PSE) &&
39 ((end - addr) == PMD_SIZE) &&
40 IS_ALIGNED(addr, PMD_SIZE)) {
41 p = early_alloc(PMD_SIZE, nid);
42 if (p && pmd_set_huge(pmd, __pa(p), PAGE_KERNEL))
43 return;
44 else if (p)
45 memblock_free(__pa(p), PMD_SIZE);
46 }
47
48 p = early_alloc(PAGE_SIZE, nid);
49 pmd_populate_kernel(&init_mm, pmd, p);
50 }
51
52 pte = pte_offset_kernel(pmd, addr);
53 do {
54 pte_t entry;
55 void *p;
56
57 if (!pte_none(*pte))
58 continue;
59
60 p = early_alloc(PAGE_SIZE, nid);
61 entry = pfn_pte(PFN_DOWN(__pa(p)), PAGE_KERNEL);
62 set_pte_at(&init_mm, addr, pte, entry);
63 } while (pte++, addr += PAGE_SIZE, addr != end);
64}
65
66static void __init kasan_populate_pud(pud_t *pud, unsigned long addr,
67 unsigned long end, int nid)
68{
69 pmd_t *pmd;
70 unsigned long next;
71
72 if (pud_none(*pud)) {
73 void *p;
74
75 if (boot_cpu_has(X86_FEATURE_GBPAGES) &&
76 ((end - addr) == PUD_SIZE) &&
77 IS_ALIGNED(addr, PUD_SIZE)) {
78 p = early_alloc(PUD_SIZE, nid);
79 if (p && pud_set_huge(pud, __pa(p), PAGE_KERNEL))
80 return;
81 else if (p)
82 memblock_free(__pa(p), PUD_SIZE);
83 }
84
85 p = early_alloc(PAGE_SIZE, nid);
86 pud_populate(&init_mm, pud, p);
87 }
88
89 pmd = pmd_offset(pud, addr);
90 do {
91 next = pmd_addr_end(addr, end);
92 if (!pmd_large(*pmd))
93 kasan_populate_pmd(pmd, addr, next, nid);
94 } while (pmd++, addr = next, addr != end);
95}
96
97static void __init kasan_populate_p4d(p4d_t *p4d, unsigned long addr,
98 unsigned long end, int nid)
99{
100 pud_t *pud;
101 unsigned long next;
102
103 if (p4d_none(*p4d)) {
104 void *p = early_alloc(PAGE_SIZE, nid);
105
106 p4d_populate(&init_mm, p4d, p);
107 }
108
109 pud = pud_offset(p4d, addr);
110 do {
111 next = pud_addr_end(addr, end);
112 if (!pud_large(*pud))
113 kasan_populate_pud(pud, addr, next, nid);
114 } while (pud++, addr = next, addr != end);
115}
116
117static void __init kasan_populate_pgd(pgd_t *pgd, unsigned long addr,
118 unsigned long end, int nid)
119{
120 void *p;
121 p4d_t *p4d;
122 unsigned long next;
123
124 if (pgd_none(*pgd)) {
125 p = early_alloc(PAGE_SIZE, nid);
126 pgd_populate(&init_mm, pgd, p);
127 }
128
129 p4d = p4d_offset(pgd, addr);
130 do {
131 next = p4d_addr_end(addr, end);
132 kasan_populate_p4d(p4d, addr, next, nid);
133 } while (p4d++, addr = next, addr != end);
134}
135
136static void __init kasan_populate_shadow(unsigned long addr, unsigned long end,
137 int nid)
138{
139 pgd_t *pgd;
140 unsigned long next;
141
142 addr = addr & PAGE_MASK;
143 end = round_up(end, PAGE_SIZE);
144 pgd = pgd_offset_k(addr);
145 do {
146 next = pgd_addr_end(addr, end);
147 kasan_populate_pgd(pgd, addr, next, nid);
148 } while (pgd++, addr = next, addr != end);
149}
150
151static void __init map_range(struct range *range)
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800152{
153 unsigned long start;
154 unsigned long end;
155
156 start = (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range->start));
157 end = (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range->end));
158
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800159 kasan_populate_shadow(start, end, early_pfn_to_nid(range->start));
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800160}
161
162static void __init clear_pgds(unsigned long start,
163 unsigned long end)
164{
Kirill A. Shutemovd691a3c2017-03-17 21:55:13 +0300165 pgd_t *pgd;
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300166 /* See comment in kasan_init() */
167 unsigned long pgd_end = end & PGDIR_MASK;
Kirill A. Shutemovd691a3c2017-03-17 21:55:13 +0300168
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300169 for (; start < pgd_end; start += PGDIR_SIZE) {
Kirill A. Shutemovd691a3c2017-03-17 21:55:13 +0300170 pgd = pgd_offset_k(start);
171 /*
172 * With folded p4d, pgd_clear() is nop, use p4d_clear()
173 * instead.
174 */
175 if (CONFIG_PGTABLE_LEVELS < 5)
176 p4d_clear(p4d_offset(pgd, start));
177 else
178 pgd_clear(pgd);
179 }
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300180
181 pgd = pgd_offset_k(start);
182 for (; start < end; start += P4D_SIZE)
183 p4d_clear(p4d_offset(pgd, start));
184}
185
186static inline p4d_t *early_p4d_offset(pgd_t *pgd, unsigned long addr)
187{
188 unsigned long p4d;
189
190 if (!IS_ENABLED(CONFIG_X86_5LEVEL))
191 return (p4d_t *)pgd;
192
193 p4d = __pa_nodebug(pgd_val(*pgd)) & PTE_PFN_MASK;
194 p4d += __START_KERNEL_map - phys_base;
195 return (p4d_t *)p4d + p4d_index(addr);
196}
197
198static void __init kasan_early_p4d_populate(pgd_t *pgd,
199 unsigned long addr,
200 unsigned long end)
201{
202 pgd_t pgd_entry;
203 p4d_t *p4d, p4d_entry;
204 unsigned long next;
205
206 if (pgd_none(*pgd)) {
207 pgd_entry = __pgd(_KERNPG_TABLE | __pa_nodebug(kasan_zero_p4d));
208 set_pgd(pgd, pgd_entry);
209 }
210
211 p4d = early_p4d_offset(pgd, addr);
212 do {
213 next = p4d_addr_end(addr, end);
214
215 if (!p4d_none(*p4d))
216 continue;
217
218 p4d_entry = __p4d(_KERNPG_TABLE | __pa_nodebug(kasan_zero_pud));
219 set_p4d(p4d, p4d_entry);
220 } while (p4d++, addr = next, addr != end && p4d_none(*p4d));
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800221}
222
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300223static void __init kasan_map_early_shadow(pgd_t *pgd)
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800224{
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300225 /* See comment in kasan_init() */
226 unsigned long addr = KASAN_SHADOW_START & PGDIR_MASK;
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800227 unsigned long end = KASAN_SHADOW_END;
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300228 unsigned long next;
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800229
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300230 pgd += pgd_index(addr);
231 do {
232 next = pgd_addr_end(addr, end);
233 kasan_early_p4d_populate(pgd, addr, next);
234 } while (pgd++, addr = next, addr != end);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800235}
236
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800237#ifdef CONFIG_KASAN_INLINE
238static int kasan_die_handler(struct notifier_block *self,
239 unsigned long val,
240 void *data)
241{
242 if (val == DIE_GPF) {
Dmitry Vyukov2ba78052016-07-14 12:06:53 -0700243 pr_emerg("CONFIG_KASAN_INLINE enabled\n");
244 pr_emerg("GPF could be caused by NULL-ptr deref or user memory access\n");
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800245 }
246 return NOTIFY_OK;
247}
248
249static struct notifier_block kasan_die_notifier = {
250 .notifier_call = kasan_die_handler,
251};
252#endif
253
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300254void __init kasan_early_init(void)
255{
256 int i;
Tom Lendacky21729f82017-07-17 16:10:07 -0500257 pteval_t pte_val = __pa_nodebug(kasan_zero_page) | __PAGE_KERNEL | _PAGE_ENC;
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300258 pmdval_t pmd_val = __pa_nodebug(kasan_zero_pte) | _KERNPG_TABLE;
259 pudval_t pud_val = __pa_nodebug(kasan_zero_pmd) | _KERNPG_TABLE;
Kirill A. Shutemov5480bb62017-03-30 11:07:30 +0300260 p4dval_t p4d_val = __pa_nodebug(kasan_zero_pud) | _KERNPG_TABLE;
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300261
262 for (i = 0; i < PTRS_PER_PTE; i++)
263 kasan_zero_pte[i] = __pte(pte_val);
264
265 for (i = 0; i < PTRS_PER_PMD; i++)
266 kasan_zero_pmd[i] = __pmd(pmd_val);
267
268 for (i = 0; i < PTRS_PER_PUD; i++)
269 kasan_zero_pud[i] = __pud(pud_val);
270
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300271 for (i = 0; IS_ENABLED(CONFIG_X86_5LEVEL) && i < PTRS_PER_P4D; i++)
Kirill A. Shutemov5480bb62017-03-30 11:07:30 +0300272 kasan_zero_p4d[i] = __p4d(p4d_val);
273
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300274 kasan_map_early_shadow(early_top_pgt);
275 kasan_map_early_shadow(init_top_pgt);
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300276}
277
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800278void __init kasan_init(void)
279{
280 int i;
Andy Lutomirski21506522017-12-04 15:07:16 +0100281 void *shadow_cpu_entry_begin, *shadow_cpu_entry_end;
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800282
283#ifdef CONFIG_KASAN_INLINE
284 register_die_notifier(&kasan_die_notifier);
285#endif
286
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300287 memcpy(early_top_pgt, init_top_pgt, sizeof(early_top_pgt));
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300288
289 /*
290 * We use the same shadow offset for 4- and 5-level paging to
291 * facilitate boot-time switching between paging modes.
292 * As result in 5-level paging mode KASAN_SHADOW_START and
293 * KASAN_SHADOW_END are not aligned to PGD boundary.
294 *
295 * KASAN_SHADOW_START doesn't share PGD with anything else.
296 * We claim whole PGD entry to make things easier.
297 *
298 * KASAN_SHADOW_END lands in the last PGD entry and it collides with
299 * bunch of things like kernel code, modules, EFI mapping, etc.
300 * We need to take extra steps to not overwrite them.
301 */
302 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
303 void *ptr;
304
305 ptr = (void *)pgd_page_vaddr(*pgd_offset_k(KASAN_SHADOW_END));
306 memcpy(tmp_p4d_table, (void *)ptr, sizeof(tmp_p4d_table));
307 set_pgd(&early_top_pgt[pgd_index(KASAN_SHADOW_END)],
308 __pgd(__pa(tmp_p4d_table) | _KERNPG_TABLE));
309 }
310
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300311 load_cr3(early_top_pgt);
Andrey Ryabinin241d2c52015-07-02 12:09:35 +0300312 __flush_tlb_all();
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800313
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300314 clear_pgds(KASAN_SHADOW_START & PGDIR_MASK, KASAN_SHADOW_END);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800315
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300316 kasan_populate_zero_shadow((void *)(KASAN_SHADOW_START & PGDIR_MASK),
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800317 kasan_mem_to_shadow((void *)PAGE_OFFSET));
318
Ingo Molnar08b46d52017-01-28 17:29:08 +0100319 for (i = 0; i < E820_MAX_ENTRIES; i++) {
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800320 if (pfn_mapped[i].end == 0)
321 break;
322
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800323 map_range(&pfn_mapped[i]);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800324 }
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800325
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100326 shadow_cpu_entry_begin = (void *)CPU_ENTRY_AREA_BASE;
327 shadow_cpu_entry_begin = kasan_mem_to_shadow(shadow_cpu_entry_begin);
328 shadow_cpu_entry_begin = (void *)round_down((unsigned long)shadow_cpu_entry_begin,
329 PAGE_SIZE);
330
331 shadow_cpu_entry_end = (void *)(CPU_ENTRY_AREA_BASE +
332 CPU_ENTRY_AREA_MAP_SIZE);
333 shadow_cpu_entry_end = kasan_mem_to_shadow(shadow_cpu_entry_end);
334 shadow_cpu_entry_end = (void *)round_up((unsigned long)shadow_cpu_entry_end,
335 PAGE_SIZE);
336
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +0300337 kasan_populate_zero_shadow(
338 kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM),
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100339 shadow_cpu_entry_begin);
340
341 kasan_populate_shadow((unsigned long)shadow_cpu_entry_begin,
342 (unsigned long)shadow_cpu_entry_end, 0);
343
344 kasan_populate_zero_shadow(shadow_cpu_entry_end,
345 kasan_mem_to_shadow((void *)__START_KERNEL_map));
Andrey Ryabininc420f162015-02-13 14:39:59 -0800346
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800347 kasan_populate_shadow((unsigned long)kasan_mem_to_shadow(_stext),
348 (unsigned long)kasan_mem_to_shadow(_end),
349 early_pfn_to_nid(__pa(_stext)));
Andrey Ryabininc420f162015-02-13 14:39:59 -0800350
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +0300351 kasan_populate_zero_shadow(kasan_mem_to_shadow((void *)MODULES_END),
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100352 (void *)KASAN_SHADOW_END);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800353
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300354 load_cr3(init_top_pgt);
Andrey Ryabinin241d2c52015-07-02 12:09:35 +0300355 __flush_tlb_all();
Andrey Ryabinin85155222015-07-02 12:09:37 +0300356
Andrey Ryabinin69e02102016-01-11 15:51:18 +0300357 /*
358 * kasan_zero_page has been used as early shadow memory, thus it may
Andrey Ryabinin063fb3e2016-01-11 15:51:19 +0300359 * contain some garbage. Now we can clear and write protect it, since
360 * after the TLB flush no one should write to it.
Andrey Ryabinin69e02102016-01-11 15:51:18 +0300361 */
362 memset(kasan_zero_page, 0, PAGE_SIZE);
Andrey Ryabinin063fb3e2016-01-11 15:51:19 +0300363 for (i = 0; i < PTRS_PER_PTE; i++) {
Tom Lendacky21729f82017-07-17 16:10:07 -0500364 pte_t pte = __pte(__pa(kasan_zero_page) | __PAGE_KERNEL_RO | _PAGE_ENC);
Andrey Ryabinin063fb3e2016-01-11 15:51:19 +0300365 set_pte(&kasan_zero_pte[i], pte);
366 }
367 /* Flush TLBs again to be sure that write protection applied. */
368 __flush_tlb_all();
Andrey Ryabinin69e02102016-01-11 15:51:18 +0300369
370 init_task.kasan_depth = 0;
Andrey Konovalov25add7e2015-11-05 18:51:03 -0800371 pr_info("KernelAddressSanitizer initialized\n");
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800372}