kasan_init.c 9.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
A
Andrey Ryabinin 已提交
2 3 4 5 6 7 8 9 10 11
/*
 * This file contains kasan initialization code for ARM64.
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
 */

#define pr_fmt(fmt) "kasan: " fmt
#include <linux/kasan.h>
#include <linux/kernel.h>
12
#include <linux/sched/task.h>
A
Andrey Ryabinin 已提交
13 14
#include <linux/memblock.h>
#include <linux/start_kernel.h>
15
#include <linux/mm.h>
A
Andrey Ryabinin 已提交
16

M
Mark Rutland 已提交
17
#include <asm/mmu_context.h>
18
#include <asm/kernel-pgtable.h>
A
Andrey Ryabinin 已提交
19 20
#include <asm/page.h>
#include <asm/pgalloc.h>
21
#include <asm/sections.h>
A
Andrey Ryabinin 已提交
22 23
#include <asm/tlbflush.h>

24 25
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)

A
Andrey Ryabinin 已提交
26 27
static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);

28 29 30 31 32 33 34
/*
 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
 * directly on kernel symbols (bm_p*d). All the early functions are called too
 * early to use lm_alias so __p*d_populate functions must be used to populate
 * with the physical address from __pa_symbol.
 */

35
static phys_addr_t __init kasan_alloc_zeroed_page(int node)
A
Andrey Ryabinin 已提交
36
{
37
	void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
38
					      __pa(MAX_DMA_ADDRESS),
39
					      MEMBLOCK_ALLOC_KASAN, node);
40 41 42 43 44
	if (!p)
		panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
		      __func__, PAGE_SIZE, PAGE_SIZE, node,
		      __pa(MAX_DMA_ADDRESS));

45 46 47
	return __pa(p);
}

48 49 50 51 52
static phys_addr_t __init kasan_alloc_raw_page(int node)
{
	void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
						__pa(MAX_DMA_ADDRESS),
						MEMBLOCK_ALLOC_KASAN, node);
53 54 55 56 57
	if (!p)
		panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
		      __func__, PAGE_SIZE, PAGE_SIZE, node,
		      __pa(MAX_DMA_ADDRESS));

58 59 60
	return __pa(p);
}

61
static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
62 63
				      bool early)
{
64
	if (pmd_none(READ_ONCE(*pmdp))) {
65 66 67
		phys_addr_t pte_phys = early ?
				__pa_symbol(kasan_early_shadow_pte)
					: kasan_alloc_zeroed_page(node);
68
		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
69 70
	}

71 72
	return early ? pte_offset_kimg(pmdp, addr)
		     : pte_offset_kernel(pmdp, addr);
73
}
A
Andrey Ryabinin 已提交
74

75
static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
76 77
				      bool early)
{
78
	if (pud_none(READ_ONCE(*pudp))) {
79 80 81
		phys_addr_t pmd_phys = early ?
				__pa_symbol(kasan_early_shadow_pmd)
					: kasan_alloc_zeroed_page(node);
82
		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
83 84
	}

85
	return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
86 87
}

88
static pud_t *__init kasan_pud_offset(p4d_t *p4dp, unsigned long addr, int node,
89 90
				      bool early)
{
91
	if (p4d_none(READ_ONCE(*p4dp))) {
92 93 94
		phys_addr_t pud_phys = early ?
				__pa_symbol(kasan_early_shadow_pud)
					: kasan_alloc_zeroed_page(node);
95
		__p4d_populate(p4dp, pud_phys, P4D_TYPE_TABLE);
96 97
	}

98
	return early ? pud_offset_kimg(p4dp, addr) : pud_offset(p4dp, addr);
99 100
}

101
static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
102 103 104
				      unsigned long end, int node, bool early)
{
	unsigned long next;
105
	pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
A
Andrey Ryabinin 已提交
106 107

	do {
108 109
		phys_addr_t page_phys = early ?
				__pa_symbol(kasan_early_shadow_page)
110 111 112
					: kasan_alloc_raw_page(node);
		if (!early)
			memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
A
Andrey Ryabinin 已提交
113
		next = addr + PAGE_SIZE;
114 115
		set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
	} while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
A
Andrey Ryabinin 已提交
116 117
}

118
static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
119
				      unsigned long end, int node, bool early)
A
Andrey Ryabinin 已提交
120 121
{
	unsigned long next;
122
	pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
A
Andrey Ryabinin 已提交
123 124 125

	do {
		next = pmd_addr_end(addr, end);
126 127
		kasan_pte_populate(pmdp, addr, next, node, early);
	} while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
A
Andrey Ryabinin 已提交
128 129
}

130
static void __init kasan_pud_populate(p4d_t *p4dp, unsigned long addr,
131
				      unsigned long end, int node, bool early)
A
Andrey Ryabinin 已提交
132 133
{
	unsigned long next;
134
	pud_t *pudp = kasan_pud_offset(p4dp, addr, node, early);
A
Andrey Ryabinin 已提交
135 136 137

	do {
		next = pud_addr_end(addr, end);
138 139
		kasan_pmd_populate(pudp, addr, next, node, early);
	} while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
A
Andrey Ryabinin 已提交
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153
static void __init kasan_p4d_populate(pgd_t *pgdp, unsigned long addr,
				      unsigned long end, int node, bool early)
{
	unsigned long next;
	p4d_t *p4dp = p4d_offset(pgdp, addr);

	do {
		next = p4d_addr_end(addr, end);
		kasan_pud_populate(p4dp, addr, next, node, early);
	} while (p4dp++, addr = next, addr != end);
}

154 155
static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
				      int node, bool early)
A
Andrey Ryabinin 已提交
156 157
{
	unsigned long next;
158
	pgd_t *pgdp;
A
Andrey Ryabinin 已提交
159

160
	pgdp = pgd_offset_k(addr);
A
Andrey Ryabinin 已提交
161 162
	do {
		next = pgd_addr_end(addr, end);
163
		kasan_p4d_populate(pgdp, addr, next, node, early);
164
	} while (pgdp++, addr = next, addr != end);
A
Andrey Ryabinin 已提交
165 166
}

167
/* The early shadow maps everything to a single page of zeroes */
168
asmlinkage void __init kasan_early_init(void)
A
Andrey Ryabinin 已提交
169
{
170 171
	BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
		KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
S
Steve Capper 已提交
172 173
	BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), PGDIR_SIZE));
	BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), PGDIR_SIZE));
A
Andrey Ryabinin 已提交
174
	BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
175 176 177 178 179 180 181 182 183
	kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
			   true);
}

/* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
static void __init kasan_map_populate(unsigned long start, unsigned long end,
				      int node)
{
	kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
A
Andrey Ryabinin 已提交
184 185
}

186 187 188 189 190
/*
 * Copy the current shadow region into a new pgdir.
 */
void __init kasan_copy_shadow(pgd_t *pgdir)
{
191
	pgd_t *pgdp, *pgdp_new, *pgdp_end;
192

193 194
	pgdp = pgd_offset_k(KASAN_SHADOW_START);
	pgdp_end = pgd_offset_k(KASAN_SHADOW_END);
195
	pgdp_new = pgd_offset_pgd(pgdir, KASAN_SHADOW_START);
196
	do {
197 198
		set_pgd(pgdp_new, READ_ONCE(*pgdp));
	} while (pgdp++, pgdp_new++, pgdp != pgdp_end);
199 200
}

A
Andrey Ryabinin 已提交
201 202 203 204 205 206 207 208 209 210 211 212
static void __init clear_pgds(unsigned long start,
			unsigned long end)
{
	/*
	 * Remove references to kasan page tables from
	 * swapper_pg_dir. pgd_clear() can't be used
	 * here because it's nop on 2,3-level pagetable setups
	 */
	for (; start < end; start += PGDIR_SIZE)
		set_pgd(pgd_offset_k(start), __pgd(0));
}

213
static void __init kasan_init_shadow(void)
A
Andrey Ryabinin 已提交
214
{
215
	u64 kimg_shadow_start, kimg_shadow_end;
216
	u64 mod_shadow_start, mod_shadow_end;
217
	u64 vmalloc_shadow_end;
218 219
	phys_addr_t pa_start, pa_end;
	u64 i;
A
Andrey Ryabinin 已提交
220

221 222
	kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
223

224 225 226
	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);

227 228
	vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);

A
Andrey Ryabinin 已提交
229 230
	/*
	 * We are going to perform proper setup of shadow memory.
231
	 * At first we should unmap early shadow (clear_pgds() call below).
A
Andrey Ryabinin 已提交
232 233 234 235 236
	 * However, instrumented code couldn't execute without shadow memory.
	 * tmp_pg_dir used to keep early shadow mapped until full shadow
	 * setup will be finished.
	 */
	memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
M
Mark Rutland 已提交
237
	dsb(ishst);
238
	cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
A
Andrey Ryabinin 已提交
239 240 241

	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);

242
	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
243
			   early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));
244

245
	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
S
Steve Capper 已提交
246
				   (void *)mod_shadow_start);
247

248 249 250 251 252 253 254 255 256 257 258
	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
		BUILD_BUG_ON(VMALLOC_START != MODULES_END);
		kasan_populate_early_shadow((void *)vmalloc_shadow_end,
					    (void *)KASAN_SHADOW_END);
	} else {
		kasan_populate_early_shadow((void *)kimg_shadow_end,
					    (void *)KASAN_SHADOW_END);
		if (kimg_shadow_start > mod_shadow_end)
			kasan_populate_early_shadow((void *)mod_shadow_end,
						    (void *)kimg_shadow_start);
	}
A
Andrey Ryabinin 已提交
259

260 261 262
	for_each_mem_range(i, &pa_start, &pa_end) {
		void *start = (void *)__phys_to_virt(pa_start);
		void *end = (void *)__phys_to_virt(pa_end);
A
Andrey Ryabinin 已提交
263 264 265 266

		if (start >= end)
			break;

267 268
		kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
				   (unsigned long)kasan_mem_to_shadow(end),
269
				   early_pfn_to_nid(virt_to_pfn(start)));
A
Andrey Ryabinin 已提交
270 271
	}

272
	/*
273 274
	 * KAsan may reuse the contents of kasan_early_shadow_pte directly,
	 * so we should make sure that it maps the zero page read-only.
275 276
	 */
	for (i = 0; i < PTRS_PER_PTE; i++)
277 278 279
		set_pte(&kasan_early_shadow_pte[i],
			pfn_pte(sym_to_pfn(kasan_early_shadow_page),
				PAGE_KERNEL_RO));
280

281
	memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
282
	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
283 284
}

285 286 287 288 289
static void __init kasan_init_depth(void)
{
	init_task.kasan_depth = 0;
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
#ifdef CONFIG_KASAN_VMALLOC
void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size)
{
	unsigned long shadow_start, shadow_end;

	if (!is_vmalloc_or_module_addr(start))
		return;

	shadow_start = (unsigned long)kasan_mem_to_shadow(start);
	shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
	shadow_end = (unsigned long)kasan_mem_to_shadow(start + size);
	shadow_end = ALIGN(shadow_end, PAGE_SIZE);
	kasan_map_populate(shadow_start, shadow_end, NUMA_NO_NODE);
}
#endif

306 307 308
void __init kasan_init(void)
{
	kasan_init_shadow();
309
	kasan_init_depth();
310
#if defined(CONFIG_KASAN_GENERIC)
311
	/* CONFIG_KASAN_SW_TAGS also requires kasan_init_sw_tags(). */
A
Andrey Ryabinin 已提交
312
	pr_info("KernelAddressSanitizer initialized\n");
313
#endif
A
Andrey Ryabinin 已提交
314
}
315 316

#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */