xref: /openbmc/linux/arch/arm64/kernel/kaslr.c (revision 2e8e1ea8)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f80fb3a3SArd Biesheuvel /*
3f80fb3a3SArd Biesheuvel  * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
4f80fb3a3SArd Biesheuvel  */
5f80fb3a3SArd Biesheuvel 
65a9e3e15SJisheng Zhang #include <linux/cache.h>
7f80fb3a3SArd Biesheuvel #include <linux/crc32.h>
8f80fb3a3SArd Biesheuvel #include <linux/init.h>
9f80fb3a3SArd Biesheuvel #include <linux/libfdt.h>
10f80fb3a3SArd Biesheuvel #include <linux/mm_types.h>
11f80fb3a3SArd Biesheuvel #include <linux/sched.h>
12f80fb3a3SArd Biesheuvel #include <linux/types.h>
13f80fb3a3SArd Biesheuvel 
141598ecdaSArd Biesheuvel #include <asm/cacheflush.h>
15f80fb3a3SArd Biesheuvel #include <asm/fixmap.h>
16f80fb3a3SArd Biesheuvel #include <asm/kernel-pgtable.h>
17f80fb3a3SArd Biesheuvel #include <asm/memory.h>
18f80fb3a3SArd Biesheuvel #include <asm/mmu.h>
19f80fb3a3SArd Biesheuvel #include <asm/pgtable.h>
20f80fb3a3SArd Biesheuvel #include <asm/sections.h>
21f80fb3a3SArd Biesheuvel 
22294a9dddSMark Brown enum kaslr_status {
23294a9dddSMark Brown 	KASLR_ENABLED,
24294a9dddSMark Brown 	KASLR_DISABLED_CMDLINE,
25294a9dddSMark Brown 	KASLR_DISABLED_NO_SEED,
26294a9dddSMark Brown 	KASLR_DISABLED_FDT_REMAP,
27294a9dddSMark Brown };
28294a9dddSMark Brown 
292203e1adSMark Brown static enum kaslr_status __initdata kaslr_status;
305a9e3e15SJisheng Zhang u64 __ro_after_init module_alloc_base;
31c031a421SArd Biesheuvel u16 __initdata memstart_offset_seed;
32f80fb3a3SArd Biesheuvel 
33f80fb3a3SArd Biesheuvel static __init u64 get_kaslr_seed(void *fdt)
34f80fb3a3SArd Biesheuvel {
35f80fb3a3SArd Biesheuvel 	int node, len;
3667831edfSLuc Van Oostenryck 	fdt64_t *prop;
37f80fb3a3SArd Biesheuvel 	u64 ret;
38f80fb3a3SArd Biesheuvel 
39f80fb3a3SArd Biesheuvel 	node = fdt_path_offset(fdt, "/chosen");
40f80fb3a3SArd Biesheuvel 	if (node < 0)
41f80fb3a3SArd Biesheuvel 		return 0;
42f80fb3a3SArd Biesheuvel 
43f80fb3a3SArd Biesheuvel 	prop = fdt_getprop_w(fdt, node, "kaslr-seed", &len);
44f80fb3a3SArd Biesheuvel 	if (!prop || len != sizeof(u64))
45f80fb3a3SArd Biesheuvel 		return 0;
46f80fb3a3SArd Biesheuvel 
47f80fb3a3SArd Biesheuvel 	ret = fdt64_to_cpu(*prop);
48f80fb3a3SArd Biesheuvel 	*prop = 0;
49f80fb3a3SArd Biesheuvel 	return ret;
50f80fb3a3SArd Biesheuvel }
51f80fb3a3SArd Biesheuvel 
521598ecdaSArd Biesheuvel static __init const u8 *kaslr_get_cmdline(void *fdt)
53f80fb3a3SArd Biesheuvel {
54f80fb3a3SArd Biesheuvel 	static __initconst const u8 default_cmdline[] = CONFIG_CMDLINE;
55f80fb3a3SArd Biesheuvel 
56f80fb3a3SArd Biesheuvel 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
57f80fb3a3SArd Biesheuvel 		int node;
58f80fb3a3SArd Biesheuvel 		const u8 *prop;
59f80fb3a3SArd Biesheuvel 
60f80fb3a3SArd Biesheuvel 		node = fdt_path_offset(fdt, "/chosen");
61f80fb3a3SArd Biesheuvel 		if (node < 0)
62f80fb3a3SArd Biesheuvel 			goto out;
63f80fb3a3SArd Biesheuvel 
64f80fb3a3SArd Biesheuvel 		prop = fdt_getprop(fdt, node, "bootargs", NULL);
65f80fb3a3SArd Biesheuvel 		if (!prop)
66f80fb3a3SArd Biesheuvel 			goto out;
67f80fb3a3SArd Biesheuvel 		return prop;
68f80fb3a3SArd Biesheuvel 	}
69f80fb3a3SArd Biesheuvel out:
70f80fb3a3SArd Biesheuvel 	return default_cmdline;
71f80fb3a3SArd Biesheuvel }
72f80fb3a3SArd Biesheuvel 
73f80fb3a3SArd Biesheuvel /*
74f80fb3a3SArd Biesheuvel  * This routine will be executed with the kernel mapped at its default virtual
75f80fb3a3SArd Biesheuvel  * address, and if it returns successfully, the kernel will be remapped, and
76f80fb3a3SArd Biesheuvel  * start_kernel() will be executed from a randomized virtual offset. The
77f80fb3a3SArd Biesheuvel  * relocation will result in all absolute references (e.g., static variables
78f80fb3a3SArd Biesheuvel  * containing function pointers) to be reinitialized, and zero-initialized
79f80fb3a3SArd Biesheuvel  * .bss variables will be reset to 0.
80f80fb3a3SArd Biesheuvel  */
814a23e56aSArd Biesheuvel u64 __init kaslr_early_init(u64 dt_phys)
82f80fb3a3SArd Biesheuvel {
83f80fb3a3SArd Biesheuvel 	void *fdt;
84f80fb3a3SArd Biesheuvel 	u64 seed, offset, mask, module_range;
85f80fb3a3SArd Biesheuvel 	const u8 *cmdline, *str;
86f80fb3a3SArd Biesheuvel 	int size;
87f80fb3a3SArd Biesheuvel 
88f80fb3a3SArd Biesheuvel 	/*
89f80fb3a3SArd Biesheuvel 	 * Set a reasonable default for module_alloc_base in case
90f80fb3a3SArd Biesheuvel 	 * we end up running with module randomization disabled.
91f80fb3a3SArd Biesheuvel 	 */
92f80fb3a3SArd Biesheuvel 	module_alloc_base = (u64)_etext - MODULES_VSIZE;
938ea23593SArd Biesheuvel 	__flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
94f80fb3a3SArd Biesheuvel 
95f80fb3a3SArd Biesheuvel 	/*
96f80fb3a3SArd Biesheuvel 	 * Try to map the FDT early. If this fails, we simply bail,
97f80fb3a3SArd Biesheuvel 	 * and proceed with KASLR disabled. We will make another
98f80fb3a3SArd Biesheuvel 	 * attempt at mapping the FDT in setup_machine()
99f80fb3a3SArd Biesheuvel 	 */
100f80fb3a3SArd Biesheuvel 	early_fixmap_init();
101e112b032SHsin-Yi Wang 	fdt = fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL);
102294a9dddSMark Brown 	if (!fdt) {
103294a9dddSMark Brown 		kaslr_status = KASLR_DISABLED_FDT_REMAP;
104f80fb3a3SArd Biesheuvel 		return 0;
105294a9dddSMark Brown 	}
106f80fb3a3SArd Biesheuvel 
107f80fb3a3SArd Biesheuvel 	/*
108f80fb3a3SArd Biesheuvel 	 * Retrieve (and wipe) the seed from the FDT
109f80fb3a3SArd Biesheuvel 	 */
110f80fb3a3SArd Biesheuvel 	seed = get_kaslr_seed(fdt);
111f80fb3a3SArd Biesheuvel 
112f80fb3a3SArd Biesheuvel 	/*
113f80fb3a3SArd Biesheuvel 	 * Check if 'nokaslr' appears on the command line, and
114f80fb3a3SArd Biesheuvel 	 * return 0 if that is the case.
115f80fb3a3SArd Biesheuvel 	 */
1161598ecdaSArd Biesheuvel 	cmdline = kaslr_get_cmdline(fdt);
117f80fb3a3SArd Biesheuvel 	str = strstr(cmdline, "nokaslr");
118294a9dddSMark Brown 	if (str == cmdline || (str > cmdline && *(str - 1) == ' ')) {
119294a9dddSMark Brown 		kaslr_status = KASLR_DISABLED_CMDLINE;
120f80fb3a3SArd Biesheuvel 		return 0;
121294a9dddSMark Brown 	}
122f80fb3a3SArd Biesheuvel 
1232e8e1ea8SMark Brown 	/*
1242e8e1ea8SMark Brown 	 * Mix in any entropy obtainable architecturally, open coded
1252e8e1ea8SMark Brown 	 * since this runs extremely early.
1262e8e1ea8SMark Brown 	 */
1272e8e1ea8SMark Brown 	if (__early_cpu_has_rndr()) {
1282e8e1ea8SMark Brown 		unsigned long raw;
1292e8e1ea8SMark Brown 
1302e8e1ea8SMark Brown 		if (__arm64_rndr(&raw))
1312e8e1ea8SMark Brown 			seed ^= raw;
1322e8e1ea8SMark Brown 	}
1332e8e1ea8SMark Brown 
1342203e1adSMark Brown 	if (!seed) {
1352203e1adSMark Brown 		kaslr_status = KASLR_DISABLED_NO_SEED;
1362203e1adSMark Brown 		return 0;
1372203e1adSMark Brown 	}
1382203e1adSMark Brown 
139f80fb3a3SArd Biesheuvel 	/*
140f80fb3a3SArd Biesheuvel 	 * OK, so we are proceeding with KASLR enabled. Calculate a suitable
141f80fb3a3SArd Biesheuvel 	 * kernel image offset from the seed. Let's place the kernel in the
14290ec95cdSSteve Capper 	 * middle half of the VMALLOC area (VA_BITS_MIN - 2), and stay clear of
143f2b9ba87SArd Biesheuvel 	 * the lower and upper quarters to avoid colliding with other
144f2b9ba87SArd Biesheuvel 	 * allocations.
145f80fb3a3SArd Biesheuvel 	 * Even if we could randomize at page granularity for 16k and 64k pages,
146f80fb3a3SArd Biesheuvel 	 * let's always round to 2 MB so we don't interfere with the ability to
147f80fb3a3SArd Biesheuvel 	 * map using contiguous PTEs
148f80fb3a3SArd Biesheuvel 	 */
14990ec95cdSSteve Capper 	mask = ((1UL << (VA_BITS_MIN - 2)) - 1) & ~(SZ_2M - 1);
15090ec95cdSSteve Capper 	offset = BIT(VA_BITS_MIN - 3) + (seed & mask);
151f80fb3a3SArd Biesheuvel 
152c031a421SArd Biesheuvel 	/* use the top 16 bits to randomize the linear region */
153c031a421SArd Biesheuvel 	memstart_offset_seed = seed >> 48;
154c031a421SArd Biesheuvel 
155f80fb3a3SArd Biesheuvel 	if (IS_ENABLED(CONFIG_KASAN))
156f80fb3a3SArd Biesheuvel 		/*
157f80fb3a3SArd Biesheuvel 		 * KASAN does not expect the module region to intersect the
158f80fb3a3SArd Biesheuvel 		 * vmalloc region, since shadow memory is allocated for each
159f80fb3a3SArd Biesheuvel 		 * module at load time, whereas the vmalloc region is shadowed
160f80fb3a3SArd Biesheuvel 		 * by KASAN zero pages. So keep modules out of the vmalloc
161f2b9ba87SArd Biesheuvel 		 * region if KASAN is enabled, and put the kernel well within
162f2b9ba87SArd Biesheuvel 		 * 4 GB of the module region.
163f80fb3a3SArd Biesheuvel 		 */
164f2b9ba87SArd Biesheuvel 		return offset % SZ_2G;
165f80fb3a3SArd Biesheuvel 
166f80fb3a3SArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) {
167f80fb3a3SArd Biesheuvel 		/*
168b2eed9b5SArd Biesheuvel 		 * Randomize the module region over a 2 GB window covering the
169f2b9ba87SArd Biesheuvel 		 * kernel. This reduces the risk of modules leaking information
170f80fb3a3SArd Biesheuvel 		 * about the address of the kernel itself, but results in
171f80fb3a3SArd Biesheuvel 		 * branches between modules and the core kernel that are
172f80fb3a3SArd Biesheuvel 		 * resolved via PLTs. (Branches between modules will be
173f80fb3a3SArd Biesheuvel 		 * resolved normally.)
174f80fb3a3SArd Biesheuvel 		 */
175b2eed9b5SArd Biesheuvel 		module_range = SZ_2G - (u64)(_end - _stext);
176b2eed9b5SArd Biesheuvel 		module_alloc_base = max((u64)_end + offset - SZ_2G,
177f2b9ba87SArd Biesheuvel 					(u64)MODULES_VADDR);
178f80fb3a3SArd Biesheuvel 	} else {
179f80fb3a3SArd Biesheuvel 		/*
180f80fb3a3SArd Biesheuvel 		 * Randomize the module region by setting module_alloc_base to
181f80fb3a3SArd Biesheuvel 		 * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE,
182f80fb3a3SArd Biesheuvel 		 * _stext) . This guarantees that the resulting region still
183f80fb3a3SArd Biesheuvel 		 * covers [_stext, _etext], and that all relative branches can
184f80fb3a3SArd Biesheuvel 		 * be resolved without veneers.
185f80fb3a3SArd Biesheuvel 		 */
186f80fb3a3SArd Biesheuvel 		module_range = MODULES_VSIZE - (u64)(_etext - _stext);
187f80fb3a3SArd Biesheuvel 		module_alloc_base = (u64)_etext + offset - MODULES_VSIZE;
188f80fb3a3SArd Biesheuvel 	}
189f80fb3a3SArd Biesheuvel 
190f80fb3a3SArd Biesheuvel 	/* use the lower 21 bits to randomize the base of the module region */
191f80fb3a3SArd Biesheuvel 	module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21;
192f80fb3a3SArd Biesheuvel 	module_alloc_base &= PAGE_MASK;
193f80fb3a3SArd Biesheuvel 
1941598ecdaSArd Biesheuvel 	__flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
1951598ecdaSArd Biesheuvel 	__flush_dcache_area(&memstart_offset_seed, sizeof(memstart_offset_seed));
1961598ecdaSArd Biesheuvel 
197f80fb3a3SArd Biesheuvel 	return offset;
198f80fb3a3SArd Biesheuvel }
199294a9dddSMark Brown 
200294a9dddSMark Brown static int __init kaslr_init(void)
201294a9dddSMark Brown {
202294a9dddSMark Brown 	switch (kaslr_status) {
203294a9dddSMark Brown 	case KASLR_ENABLED:
204294a9dddSMark Brown 		pr_info("KASLR enabled\n");
205294a9dddSMark Brown 		break;
206294a9dddSMark Brown 	case KASLR_DISABLED_CMDLINE:
207294a9dddSMark Brown 		pr_info("KASLR disabled on command line\n");
208294a9dddSMark Brown 		break;
209294a9dddSMark Brown 	case KASLR_DISABLED_NO_SEED:
210294a9dddSMark Brown 		pr_warn("KASLR disabled due to lack of seed\n");
211294a9dddSMark Brown 		break;
212294a9dddSMark Brown 	case KASLR_DISABLED_FDT_REMAP:
213294a9dddSMark Brown 		pr_warn("KASLR disabled due to FDT remapping failure\n");
214294a9dddSMark Brown 		break;
215294a9dddSMark Brown 	}
216294a9dddSMark Brown 
217294a9dddSMark Brown 	return 0;
218294a9dddSMark Brown }
219294a9dddSMark Brown core_initcall(kaslr_init)
220