xref: /openbmc/linux/arch/arm64/kernel/kaslr.c (revision 0fea6e9a)
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>
1365fddcfcSMike Rapoport #include <linux/pgtable.h>
1458552408SLinus Torvalds #include <linux/random.h>
15f80fb3a3SArd Biesheuvel 
161598ecdaSArd Biesheuvel #include <asm/cacheflush.h>
17f80fb3a3SArd Biesheuvel #include <asm/fixmap.h>
18f80fb3a3SArd Biesheuvel #include <asm/kernel-pgtable.h>
19f80fb3a3SArd Biesheuvel #include <asm/memory.h>
20f80fb3a3SArd Biesheuvel #include <asm/mmu.h>
21f80fb3a3SArd Biesheuvel #include <asm/sections.h>
22f80fb3a3SArd Biesheuvel 
23294a9dddSMark Brown enum kaslr_status {
24294a9dddSMark Brown 	KASLR_ENABLED,
25294a9dddSMark Brown 	KASLR_DISABLED_CMDLINE,
26294a9dddSMark Brown 	KASLR_DISABLED_NO_SEED,
27294a9dddSMark Brown 	KASLR_DISABLED_FDT_REMAP,
28294a9dddSMark Brown };
29294a9dddSMark Brown 
302203e1adSMark Brown static enum kaslr_status __initdata kaslr_status;
315a9e3e15SJisheng Zhang u64 __ro_after_init module_alloc_base;
32c031a421SArd Biesheuvel u16 __initdata memstart_offset_seed;
33f80fb3a3SArd Biesheuvel 
34f80fb3a3SArd Biesheuvel static __init u64 get_kaslr_seed(void *fdt)
35f80fb3a3SArd Biesheuvel {
36f80fb3a3SArd Biesheuvel 	int node, len;
3767831edfSLuc Van Oostenryck 	fdt64_t *prop;
38f80fb3a3SArd Biesheuvel 	u64 ret;
39f80fb3a3SArd Biesheuvel 
40f80fb3a3SArd Biesheuvel 	node = fdt_path_offset(fdt, "/chosen");
41f80fb3a3SArd Biesheuvel 	if (node < 0)
42f80fb3a3SArd Biesheuvel 		return 0;
43f80fb3a3SArd Biesheuvel 
44f80fb3a3SArd Biesheuvel 	prop = fdt_getprop_w(fdt, node, "kaslr-seed", &len);
45f80fb3a3SArd Biesheuvel 	if (!prop || len != sizeof(u64))
46f80fb3a3SArd Biesheuvel 		return 0;
47f80fb3a3SArd Biesheuvel 
48f80fb3a3SArd Biesheuvel 	ret = fdt64_to_cpu(*prop);
49f80fb3a3SArd Biesheuvel 	*prop = 0;
50f80fb3a3SArd Biesheuvel 	return ret;
51f80fb3a3SArd Biesheuvel }
52f80fb3a3SArd Biesheuvel 
5352ec03f7STyler Hicks static __init bool cmdline_contains_nokaslr(const u8 *cmdline)
54f80fb3a3SArd Biesheuvel {
5552ec03f7STyler Hicks 	const u8 *str;
56f80fb3a3SArd Biesheuvel 
5752ec03f7STyler Hicks 	str = strstr(cmdline, "nokaslr");
5852ec03f7STyler Hicks 	return str == cmdline || (str > cmdline && *(str - 1) == ' ');
5952ec03f7STyler Hicks }
6052ec03f7STyler Hicks 
6152ec03f7STyler Hicks static __init bool is_kaslr_disabled_cmdline(void *fdt)
6252ec03f7STyler Hicks {
63f80fb3a3SArd Biesheuvel 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
64f80fb3a3SArd Biesheuvel 		int node;
65f80fb3a3SArd Biesheuvel 		const u8 *prop;
66f80fb3a3SArd Biesheuvel 
67f80fb3a3SArd Biesheuvel 		node = fdt_path_offset(fdt, "/chosen");
68f80fb3a3SArd Biesheuvel 		if (node < 0)
69f80fb3a3SArd Biesheuvel 			goto out;
70f80fb3a3SArd Biesheuvel 
71f80fb3a3SArd Biesheuvel 		prop = fdt_getprop(fdt, node, "bootargs", NULL);
72f80fb3a3SArd Biesheuvel 		if (!prop)
73f80fb3a3SArd Biesheuvel 			goto out;
741e40d105STyler Hicks 
751e40d105STyler Hicks 		if (cmdline_contains_nokaslr(prop))
761e40d105STyler Hicks 			return true;
771e40d105STyler Hicks 
781e40d105STyler Hicks 		if (IS_ENABLED(CONFIG_CMDLINE_EXTEND))
791e40d105STyler Hicks 			goto out;
801e40d105STyler Hicks 
811e40d105STyler Hicks 		return false;
82f80fb3a3SArd Biesheuvel 	}
83f80fb3a3SArd Biesheuvel out:
8452ec03f7STyler Hicks 	return cmdline_contains_nokaslr(CONFIG_CMDLINE);
85f80fb3a3SArd Biesheuvel }
86f80fb3a3SArd Biesheuvel 
87f80fb3a3SArd Biesheuvel /*
88f80fb3a3SArd Biesheuvel  * This routine will be executed with the kernel mapped at its default virtual
89f80fb3a3SArd Biesheuvel  * address, and if it returns successfully, the kernel will be remapped, and
90f80fb3a3SArd Biesheuvel  * start_kernel() will be executed from a randomized virtual offset. The
91f80fb3a3SArd Biesheuvel  * relocation will result in all absolute references (e.g., static variables
92f80fb3a3SArd Biesheuvel  * containing function pointers) to be reinitialized, and zero-initialized
93f80fb3a3SArd Biesheuvel  * .bss variables will be reset to 0.
94f80fb3a3SArd Biesheuvel  */
954a23e56aSArd Biesheuvel u64 __init kaslr_early_init(u64 dt_phys)
96f80fb3a3SArd Biesheuvel {
97f80fb3a3SArd Biesheuvel 	void *fdt;
98f80fb3a3SArd Biesheuvel 	u64 seed, offset, mask, module_range;
999bceb80bSGuenter Roeck 	unsigned long raw;
100f80fb3a3SArd Biesheuvel 	int size;
101f80fb3a3SArd Biesheuvel 
102f80fb3a3SArd Biesheuvel 	/*
103f80fb3a3SArd Biesheuvel 	 * Set a reasonable default for module_alloc_base in case
104f80fb3a3SArd Biesheuvel 	 * we end up running with module randomization disabled.
105f80fb3a3SArd Biesheuvel 	 */
106f80fb3a3SArd Biesheuvel 	module_alloc_base = (u64)_etext - MODULES_VSIZE;
1078ea23593SArd Biesheuvel 	__flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
108f80fb3a3SArd Biesheuvel 
109f80fb3a3SArd Biesheuvel 	/*
110f80fb3a3SArd Biesheuvel 	 * Try to map the FDT early. If this fails, we simply bail,
111f80fb3a3SArd Biesheuvel 	 * and proceed with KASLR disabled. We will make another
112f80fb3a3SArd Biesheuvel 	 * attempt at mapping the FDT in setup_machine()
113f80fb3a3SArd Biesheuvel 	 */
114f80fb3a3SArd Biesheuvel 	early_fixmap_init();
115e112b032SHsin-Yi Wang 	fdt = fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL);
116294a9dddSMark Brown 	if (!fdt) {
117294a9dddSMark Brown 		kaslr_status = KASLR_DISABLED_FDT_REMAP;
118f80fb3a3SArd Biesheuvel 		return 0;
119294a9dddSMark Brown 	}
120f80fb3a3SArd Biesheuvel 
121f80fb3a3SArd Biesheuvel 	/*
122f80fb3a3SArd Biesheuvel 	 * Retrieve (and wipe) the seed from the FDT
123f80fb3a3SArd Biesheuvel 	 */
124f80fb3a3SArd Biesheuvel 	seed = get_kaslr_seed(fdt);
125f80fb3a3SArd Biesheuvel 
126f80fb3a3SArd Biesheuvel 	/*
127f80fb3a3SArd Biesheuvel 	 * Check if 'nokaslr' appears on the command line, and
128f80fb3a3SArd Biesheuvel 	 * return 0 if that is the case.
129f80fb3a3SArd Biesheuvel 	 */
13052ec03f7STyler Hicks 	if (is_kaslr_disabled_cmdline(fdt)) {
131294a9dddSMark Brown 		kaslr_status = KASLR_DISABLED_CMDLINE;
132f80fb3a3SArd Biesheuvel 		return 0;
133294a9dddSMark Brown 	}
134f80fb3a3SArd Biesheuvel 
1352e8e1ea8SMark Brown 	/*
1369bceb80bSGuenter Roeck 	 * Mix in any entropy obtainable architecturally if enabled
1379bceb80bSGuenter Roeck 	 * and supported.
1382e8e1ea8SMark Brown 	 */
1392e8e1ea8SMark Brown 
1409bceb80bSGuenter Roeck 	if (arch_get_random_seed_long_early(&raw))
1412e8e1ea8SMark Brown 		seed ^= raw;
1422e8e1ea8SMark Brown 
1432203e1adSMark Brown 	if (!seed) {
1442203e1adSMark Brown 		kaslr_status = KASLR_DISABLED_NO_SEED;
1452203e1adSMark Brown 		return 0;
1462203e1adSMark Brown 	}
1472203e1adSMark Brown 
148f80fb3a3SArd Biesheuvel 	/*
149f80fb3a3SArd Biesheuvel 	 * OK, so we are proceeding with KASLR enabled. Calculate a suitable
150f80fb3a3SArd Biesheuvel 	 * kernel image offset from the seed. Let's place the kernel in the
15190ec95cdSSteve Capper 	 * middle half of the VMALLOC area (VA_BITS_MIN - 2), and stay clear of
152f2b9ba87SArd Biesheuvel 	 * the lower and upper quarters to avoid colliding with other
153f2b9ba87SArd Biesheuvel 	 * allocations.
154f80fb3a3SArd Biesheuvel 	 * Even if we could randomize at page granularity for 16k and 64k pages,
155f80fb3a3SArd Biesheuvel 	 * let's always round to 2 MB so we don't interfere with the ability to
156f80fb3a3SArd Biesheuvel 	 * map using contiguous PTEs
157f80fb3a3SArd Biesheuvel 	 */
15890ec95cdSSteve Capper 	mask = ((1UL << (VA_BITS_MIN - 2)) - 1) & ~(SZ_2M - 1);
15990ec95cdSSteve Capper 	offset = BIT(VA_BITS_MIN - 3) + (seed & mask);
160f80fb3a3SArd Biesheuvel 
161c031a421SArd Biesheuvel 	/* use the top 16 bits to randomize the linear region */
162c031a421SArd Biesheuvel 	memstart_offset_seed = seed >> 48;
163c031a421SArd Biesheuvel 
164*0fea6e9aSAndrey Konovalov 	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
165*0fea6e9aSAndrey Konovalov 	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
166f80fb3a3SArd Biesheuvel 		/*
167f80fb3a3SArd Biesheuvel 		 * KASAN does not expect the module region to intersect the
168f80fb3a3SArd Biesheuvel 		 * vmalloc region, since shadow memory is allocated for each
169f80fb3a3SArd Biesheuvel 		 * module at load time, whereas the vmalloc region is shadowed
170f80fb3a3SArd Biesheuvel 		 * by KASAN zero pages. So keep modules out of the vmalloc
171f2b9ba87SArd Biesheuvel 		 * region if KASAN is enabled, and put the kernel well within
172f2b9ba87SArd Biesheuvel 		 * 4 GB of the module region.
173f80fb3a3SArd Biesheuvel 		 */
174f2b9ba87SArd Biesheuvel 		return offset % SZ_2G;
175f80fb3a3SArd Biesheuvel 
176f80fb3a3SArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) {
177f80fb3a3SArd Biesheuvel 		/*
178b2eed9b5SArd Biesheuvel 		 * Randomize the module region over a 2 GB window covering the
179f2b9ba87SArd Biesheuvel 		 * kernel. This reduces the risk of modules leaking information
180f80fb3a3SArd Biesheuvel 		 * about the address of the kernel itself, but results in
181f80fb3a3SArd Biesheuvel 		 * branches between modules and the core kernel that are
182f80fb3a3SArd Biesheuvel 		 * resolved via PLTs. (Branches between modules will be
183f80fb3a3SArd Biesheuvel 		 * resolved normally.)
184f80fb3a3SArd Biesheuvel 		 */
185b2eed9b5SArd Biesheuvel 		module_range = SZ_2G - (u64)(_end - _stext);
186b2eed9b5SArd Biesheuvel 		module_alloc_base = max((u64)_end + offset - SZ_2G,
187f2b9ba87SArd Biesheuvel 					(u64)MODULES_VADDR);
188f80fb3a3SArd Biesheuvel 	} else {
189f80fb3a3SArd Biesheuvel 		/*
190f80fb3a3SArd Biesheuvel 		 * Randomize the module region by setting module_alloc_base to
191f80fb3a3SArd Biesheuvel 		 * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE,
192f80fb3a3SArd Biesheuvel 		 * _stext) . This guarantees that the resulting region still
193f80fb3a3SArd Biesheuvel 		 * covers [_stext, _etext], and that all relative branches can
194f80fb3a3SArd Biesheuvel 		 * be resolved without veneers.
195f80fb3a3SArd Biesheuvel 		 */
196f80fb3a3SArd Biesheuvel 		module_range = MODULES_VSIZE - (u64)(_etext - _stext);
197f80fb3a3SArd Biesheuvel 		module_alloc_base = (u64)_etext + offset - MODULES_VSIZE;
198f80fb3a3SArd Biesheuvel 	}
199f80fb3a3SArd Biesheuvel 
200f80fb3a3SArd Biesheuvel 	/* use the lower 21 bits to randomize the base of the module region */
201f80fb3a3SArd Biesheuvel 	module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21;
202f80fb3a3SArd Biesheuvel 	module_alloc_base &= PAGE_MASK;
203f80fb3a3SArd Biesheuvel 
2041598ecdaSArd Biesheuvel 	__flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
2051598ecdaSArd Biesheuvel 	__flush_dcache_area(&memstart_offset_seed, sizeof(memstart_offset_seed));
2061598ecdaSArd Biesheuvel 
207f80fb3a3SArd Biesheuvel 	return offset;
208f80fb3a3SArd Biesheuvel }
209294a9dddSMark Brown 
210294a9dddSMark Brown static int __init kaslr_init(void)
211294a9dddSMark Brown {
212294a9dddSMark Brown 	switch (kaslr_status) {
213294a9dddSMark Brown 	case KASLR_ENABLED:
214294a9dddSMark Brown 		pr_info("KASLR enabled\n");
215294a9dddSMark Brown 		break;
216294a9dddSMark Brown 	case KASLR_DISABLED_CMDLINE:
217294a9dddSMark Brown 		pr_info("KASLR disabled on command line\n");
218294a9dddSMark Brown 		break;
219294a9dddSMark Brown 	case KASLR_DISABLED_NO_SEED:
220294a9dddSMark Brown 		pr_warn("KASLR disabled due to lack of seed\n");
221294a9dddSMark Brown 		break;
222294a9dddSMark Brown 	case KASLR_DISABLED_FDT_REMAP:
223294a9dddSMark Brown 		pr_warn("KASLR disabled due to FDT remapping failure\n");
224294a9dddSMark Brown 		break;
225294a9dddSMark Brown 	}
226294a9dddSMark Brown 
227294a9dddSMark Brown 	return 0;
228294a9dddSMark Brown }
229294a9dddSMark Brown core_initcall(kaslr_init)
230