blob: af6f2f9c6a265d837f3882fcc45ef46da918fff0 [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 Ryabinin0d39e262018-01-10 18:36:02 +030024static __init void *early_alloc(size_t size, int nid, bool panic)
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080025{
Andrey Ryabinin0d39e262018-01-10 18:36:02 +030026 if (panic)
27 return memblock_virt_alloc_try_nid(size, size,
28 __pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid);
29 else
30 return memblock_virt_alloc_try_nid_nopanic(size, size,
31 __pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080032}
33
34static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr,
35 unsigned long end, int nid)
36{
37 pte_t *pte;
38
39 if (pmd_none(*pmd)) {
40 void *p;
41
42 if (boot_cpu_has(X86_FEATURE_PSE) &&
43 ((end - addr) == PMD_SIZE) &&
44 IS_ALIGNED(addr, PMD_SIZE)) {
Andrey Ryabinin0d39e262018-01-10 18:36:02 +030045 p = early_alloc(PMD_SIZE, nid, false);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080046 if (p && pmd_set_huge(pmd, __pa(p), PAGE_KERNEL))
47 return;
48 else if (p)
49 memblock_free(__pa(p), PMD_SIZE);
50 }
51
Andrey Ryabinin0d39e262018-01-10 18:36:02 +030052 p = early_alloc(PAGE_SIZE, nid, true);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080053 pmd_populate_kernel(&init_mm, pmd, p);
54 }
55
56 pte = pte_offset_kernel(pmd, addr);
57 do {
58 pte_t entry;
59 void *p;
60
61 if (!pte_none(*pte))
62 continue;
63
Andrey Ryabinin0d39e262018-01-10 18:36:02 +030064 p = early_alloc(PAGE_SIZE, nid, true);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080065 entry = pfn_pte(PFN_DOWN(__pa(p)), PAGE_KERNEL);
66 set_pte_at(&init_mm, addr, pte, entry);
67 } while (pte++, addr += PAGE_SIZE, addr != end);
68}
69
70static void __init kasan_populate_pud(pud_t *pud, unsigned long addr,
71 unsigned long end, int nid)
72{
73 pmd_t *pmd;
74 unsigned long next;
75
76 if (pud_none(*pud)) {
77 void *p;
78
79 if (boot_cpu_has(X86_FEATURE_GBPAGES) &&
80 ((end - addr) == PUD_SIZE) &&
81 IS_ALIGNED(addr, PUD_SIZE)) {
Andrey Ryabinin0d39e262018-01-10 18:36:02 +030082 p = early_alloc(PUD_SIZE, nid, false);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080083 if (p && pud_set_huge(pud, __pa(p), PAGE_KERNEL))
84 return;
85 else if (p)
86 memblock_free(__pa(p), PUD_SIZE);
87 }
88
Andrey Ryabinin0d39e262018-01-10 18:36:02 +030089 p = early_alloc(PAGE_SIZE, nid, true);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -080090 pud_populate(&init_mm, pud, p);
91 }
92
93 pmd = pmd_offset(pud, addr);
94 do {
95 next = pmd_addr_end(addr, end);
96 if (!pmd_large(*pmd))
97 kasan_populate_pmd(pmd, addr, next, nid);
98 } while (pmd++, addr = next, addr != end);
99}
100
101static void __init kasan_populate_p4d(p4d_t *p4d, unsigned long addr,
102 unsigned long end, int nid)
103{
104 pud_t *pud;
105 unsigned long next;
106
107 if (p4d_none(*p4d)) {
Andrey Ryabinin0d39e262018-01-10 18:36:02 +0300108 void *p = early_alloc(PAGE_SIZE, nid, true);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800109
110 p4d_populate(&init_mm, p4d, p);
111 }
112
113 pud = pud_offset(p4d, addr);
114 do {
115 next = pud_addr_end(addr, end);
116 if (!pud_large(*pud))
117 kasan_populate_pud(pud, addr, next, nid);
118 } while (pud++, addr = next, addr != end);
119}
120
121static void __init kasan_populate_pgd(pgd_t *pgd, unsigned long addr,
122 unsigned long end, int nid)
123{
124 void *p;
125 p4d_t *p4d;
126 unsigned long next;
127
128 if (pgd_none(*pgd)) {
Andrey Ryabinin0d39e262018-01-10 18:36:02 +0300129 p = early_alloc(PAGE_SIZE, nid, true);
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800130 pgd_populate(&init_mm, pgd, p);
131 }
132
133 p4d = p4d_offset(pgd, addr);
134 do {
135 next = p4d_addr_end(addr, end);
136 kasan_populate_p4d(p4d, addr, next, nid);
137 } while (p4d++, addr = next, addr != end);
138}
139
140static void __init kasan_populate_shadow(unsigned long addr, unsigned long end,
141 int nid)
142{
143 pgd_t *pgd;
144 unsigned long next;
145
146 addr = addr & PAGE_MASK;
147 end = round_up(end, PAGE_SIZE);
148 pgd = pgd_offset_k(addr);
149 do {
150 next = pgd_addr_end(addr, end);
151 kasan_populate_pgd(pgd, addr, next, nid);
152 } while (pgd++, addr = next, addr != end);
153}
154
155static void __init map_range(struct range *range)
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800156{
157 unsigned long start;
158 unsigned long end;
159
160 start = (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range->start));
161 end = (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range->end));
162
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800163 kasan_populate_shadow(start, end, early_pfn_to_nid(range->start));
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800164}
165
166static void __init clear_pgds(unsigned long start,
167 unsigned long end)
168{
Kirill A. Shutemovd691a3c2017-03-17 21:55:13 +0300169 pgd_t *pgd;
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300170 /* See comment in kasan_init() */
171 unsigned long pgd_end = end & PGDIR_MASK;
Kirill A. Shutemovd691a3c2017-03-17 21:55:13 +0300172
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300173 for (; start < pgd_end; start += PGDIR_SIZE) {
Kirill A. Shutemovd691a3c2017-03-17 21:55:13 +0300174 pgd = pgd_offset_k(start);
175 /*
176 * With folded p4d, pgd_clear() is nop, use p4d_clear()
177 * instead.
178 */
179 if (CONFIG_PGTABLE_LEVELS < 5)
180 p4d_clear(p4d_offset(pgd, start));
181 else
182 pgd_clear(pgd);
183 }
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300184
185 pgd = pgd_offset_k(start);
186 for (; start < end; start += P4D_SIZE)
187 p4d_clear(p4d_offset(pgd, start));
188}
189
190static inline p4d_t *early_p4d_offset(pgd_t *pgd, unsigned long addr)
191{
192 unsigned long p4d;
193
194 if (!IS_ENABLED(CONFIG_X86_5LEVEL))
195 return (p4d_t *)pgd;
196
197 p4d = __pa_nodebug(pgd_val(*pgd)) & PTE_PFN_MASK;
198 p4d += __START_KERNEL_map - phys_base;
199 return (p4d_t *)p4d + p4d_index(addr);
200}
201
202static void __init kasan_early_p4d_populate(pgd_t *pgd,
203 unsigned long addr,
204 unsigned long end)
205{
206 pgd_t pgd_entry;
207 p4d_t *p4d, p4d_entry;
208 unsigned long next;
209
210 if (pgd_none(*pgd)) {
211 pgd_entry = __pgd(_KERNPG_TABLE | __pa_nodebug(kasan_zero_p4d));
212 set_pgd(pgd, pgd_entry);
213 }
214
215 p4d = early_p4d_offset(pgd, addr);
216 do {
217 next = p4d_addr_end(addr, end);
218
219 if (!p4d_none(*p4d))
220 continue;
221
222 p4d_entry = __p4d(_KERNPG_TABLE | __pa_nodebug(kasan_zero_pud));
223 set_p4d(p4d, p4d_entry);
224 } while (p4d++, addr = next, addr != end && p4d_none(*p4d));
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800225}
226
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300227static void __init kasan_map_early_shadow(pgd_t *pgd)
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800228{
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300229 /* See comment in kasan_init() */
230 unsigned long addr = KASAN_SHADOW_START & PGDIR_MASK;
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800231 unsigned long end = KASAN_SHADOW_END;
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300232 unsigned long next;
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800233
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300234 pgd += pgd_index(addr);
235 do {
236 next = pgd_addr_end(addr, end);
237 kasan_early_p4d_populate(pgd, addr, next);
238 } while (pgd++, addr = next, addr != end);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800239}
240
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800241#ifdef CONFIG_KASAN_INLINE
242static int kasan_die_handler(struct notifier_block *self,
243 unsigned long val,
244 void *data)
245{
246 if (val == DIE_GPF) {
Dmitry Vyukov2ba78052016-07-14 12:06:53 -0700247 pr_emerg("CONFIG_KASAN_INLINE enabled\n");
248 pr_emerg("GPF could be caused by NULL-ptr deref or user memory access\n");
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800249 }
250 return NOTIFY_OK;
251}
252
253static struct notifier_block kasan_die_notifier = {
254 .notifier_call = kasan_die_handler,
255};
256#endif
257
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300258void __init kasan_early_init(void)
259{
260 int i;
Tom Lendacky21729f82017-07-17 16:10:07 -0500261 pteval_t pte_val = __pa_nodebug(kasan_zero_page) | __PAGE_KERNEL | _PAGE_ENC;
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300262 pmdval_t pmd_val = __pa_nodebug(kasan_zero_pte) | _KERNPG_TABLE;
263 pudval_t pud_val = __pa_nodebug(kasan_zero_pmd) | _KERNPG_TABLE;
Kirill A. Shutemov5480bb62017-03-30 11:07:30 +0300264 p4dval_t p4d_val = __pa_nodebug(kasan_zero_pud) | _KERNPG_TABLE;
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300265
266 for (i = 0; i < PTRS_PER_PTE; i++)
267 kasan_zero_pte[i] = __pte(pte_val);
268
269 for (i = 0; i < PTRS_PER_PMD; i++)
270 kasan_zero_pmd[i] = __pmd(pmd_val);
271
272 for (i = 0; i < PTRS_PER_PUD; i++)
273 kasan_zero_pud[i] = __pud(pud_val);
274
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300275 for (i = 0; IS_ENABLED(CONFIG_X86_5LEVEL) && i < PTRS_PER_P4D; i++)
Kirill A. Shutemov5480bb62017-03-30 11:07:30 +0300276 kasan_zero_p4d[i] = __p4d(p4d_val);
277
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300278 kasan_map_early_shadow(early_top_pgt);
279 kasan_map_early_shadow(init_top_pgt);
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300280}
281
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800282void __init kasan_init(void)
283{
284 int i;
Andy Lutomirski21506522017-12-04 15:07:16 +0100285 void *shadow_cpu_entry_begin, *shadow_cpu_entry_end;
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800286
287#ifdef CONFIG_KASAN_INLINE
288 register_die_notifier(&kasan_die_notifier);
289#endif
290
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300291 memcpy(early_top_pgt, init_top_pgt, sizeof(early_top_pgt));
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300292
293 /*
294 * We use the same shadow offset for 4- and 5-level paging to
295 * facilitate boot-time switching between paging modes.
296 * As result in 5-level paging mode KASAN_SHADOW_START and
297 * KASAN_SHADOW_END are not aligned to PGD boundary.
298 *
299 * KASAN_SHADOW_START doesn't share PGD with anything else.
300 * We claim whole PGD entry to make things easier.
301 *
302 * KASAN_SHADOW_END lands in the last PGD entry and it collides with
303 * bunch of things like kernel code, modules, EFI mapping, etc.
304 * We need to take extra steps to not overwrite them.
305 */
306 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
307 void *ptr;
308
309 ptr = (void *)pgd_page_vaddr(*pgd_offset_k(KASAN_SHADOW_END));
310 memcpy(tmp_p4d_table, (void *)ptr, sizeof(tmp_p4d_table));
311 set_pgd(&early_top_pgt[pgd_index(KASAN_SHADOW_END)],
312 __pgd(__pa(tmp_p4d_table) | _KERNPG_TABLE));
313 }
314
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300315 load_cr3(early_top_pgt);
Andrey Ryabinin241d2c52015-07-02 12:09:35 +0300316 __flush_tlb_all();
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800317
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300318 clear_pgds(KASAN_SHADOW_START & PGDIR_MASK, KASAN_SHADOW_END);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800319
Andrey Ryabinin12a8cc72017-09-29 17:08:18 +0300320 kasan_populate_zero_shadow((void *)(KASAN_SHADOW_START & PGDIR_MASK),
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800321 kasan_mem_to_shadow((void *)PAGE_OFFSET));
322
Ingo Molnar08b46d52017-01-28 17:29:08 +0100323 for (i = 0; i < E820_MAX_ENTRIES; i++) {
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800324 if (pfn_mapped[i].end == 0)
325 break;
326
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800327 map_range(&pfn_mapped[i]);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800328 }
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800329
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100330 shadow_cpu_entry_begin = (void *)CPU_ENTRY_AREA_BASE;
331 shadow_cpu_entry_begin = kasan_mem_to_shadow(shadow_cpu_entry_begin);
332 shadow_cpu_entry_begin = (void *)round_down((unsigned long)shadow_cpu_entry_begin,
333 PAGE_SIZE);
334
335 shadow_cpu_entry_end = (void *)(CPU_ENTRY_AREA_BASE +
336 CPU_ENTRY_AREA_MAP_SIZE);
337 shadow_cpu_entry_end = kasan_mem_to_shadow(shadow_cpu_entry_end);
338 shadow_cpu_entry_end = (void *)round_up((unsigned long)shadow_cpu_entry_end,
339 PAGE_SIZE);
340
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +0300341 kasan_populate_zero_shadow(
342 kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM),
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100343 shadow_cpu_entry_begin);
344
345 kasan_populate_shadow((unsigned long)shadow_cpu_entry_begin,
346 (unsigned long)shadow_cpu_entry_end, 0);
347
348 kasan_populate_zero_shadow(shadow_cpu_entry_end,
349 kasan_mem_to_shadow((void *)__START_KERNEL_map));
Andrey Ryabininc420f162015-02-13 14:39:59 -0800350
Andrey Ryabinin2aeb0732017-11-15 17:36:35 -0800351 kasan_populate_shadow((unsigned long)kasan_mem_to_shadow(_stext),
352 (unsigned long)kasan_mem_to_shadow(_end),
353 early_pfn_to_nid(__pa(_stext)));
Andrey Ryabininc420f162015-02-13 14:39:59 -0800354
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +0300355 kasan_populate_zero_shadow(kasan_mem_to_shadow((void *)MODULES_END),
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100356 (void *)KASAN_SHADOW_END);
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800357
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300358 load_cr3(init_top_pgt);
Andrey Ryabinin241d2c52015-07-02 12:09:35 +0300359 __flush_tlb_all();
Andrey Ryabinin85155222015-07-02 12:09:37 +0300360
Andrey Ryabinin69e02102016-01-11 15:51:18 +0300361 /*
362 * kasan_zero_page has been used as early shadow memory, thus it may
Andrey Ryabinin063fb3e2016-01-11 15:51:19 +0300363 * contain some garbage. Now we can clear and write protect it, since
364 * after the TLB flush no one should write to it.
Andrey Ryabinin69e02102016-01-11 15:51:18 +0300365 */
366 memset(kasan_zero_page, 0, PAGE_SIZE);
Andrey Ryabinin063fb3e2016-01-11 15:51:19 +0300367 for (i = 0; i < PTRS_PER_PTE; i++) {
Tom Lendacky21729f82017-07-17 16:10:07 -0500368 pte_t pte = __pte(__pa(kasan_zero_page) | __PAGE_KERNEL_RO | _PAGE_ENC);
Andrey Ryabinin063fb3e2016-01-11 15:51:19 +0300369 set_pte(&kasan_zero_pte[i], pte);
370 }
371 /* Flush TLBs again to be sure that write protection applied. */
372 __flush_tlb_all();
Andrey Ryabinin69e02102016-01-11 15:51:18 +0300373
374 init_task.kasan_depth = 0;
Andrey Konovalov25add7e2015-11-05 18:51:03 -0800375 pr_info("KernelAddressSanitizer initialized\n");
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800376}