xref: /openbmc/linux/arch/arm64/kernel/kaslr.c (revision 31d02e7a)
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>
22f6f0c436SMarc Zyngier #include <asm/setup.h>
23f80fb3a3SArd Biesheuvel 
24294a9dddSMark Brown enum kaslr_status {
25294a9dddSMark Brown 	KASLR_ENABLED,
26294a9dddSMark Brown 	KASLR_DISABLED_CMDLINE,
27294a9dddSMark Brown 	KASLR_DISABLED_NO_SEED,
28294a9dddSMark Brown 	KASLR_DISABLED_FDT_REMAP,
29294a9dddSMark Brown };
30294a9dddSMark Brown 
312203e1adSMark Brown static enum kaslr_status __initdata kaslr_status;
325a9e3e15SJisheng Zhang u64 __ro_after_init module_alloc_base;
33c031a421SArd Biesheuvel u16 __initdata memstart_offset_seed;
34f80fb3a3SArd Biesheuvel 
35f80fb3a3SArd Biesheuvel static __init u64 get_kaslr_seed(void *fdt)
36f80fb3a3SArd Biesheuvel {
37f80fb3a3SArd Biesheuvel 	int node, len;
3867831edfSLuc Van Oostenryck 	fdt64_t *prop;
39f80fb3a3SArd Biesheuvel 	u64 ret;
40f80fb3a3SArd Biesheuvel 
41f80fb3a3SArd Biesheuvel 	node = fdt_path_offset(fdt, "/chosen");
42f80fb3a3SArd Biesheuvel 	if (node < 0)
43f80fb3a3SArd Biesheuvel 		return 0;
44f80fb3a3SArd Biesheuvel 
45f80fb3a3SArd Biesheuvel 	prop = fdt_getprop_w(fdt, node, "kaslr-seed", &len);
46f80fb3a3SArd Biesheuvel 	if (!prop || len != sizeof(u64))
47f80fb3a3SArd Biesheuvel 		return 0;
48f80fb3a3SArd Biesheuvel 
49f80fb3a3SArd Biesheuvel 	ret = fdt64_to_cpu(*prop);
50f80fb3a3SArd Biesheuvel 	*prop = 0;
51f80fb3a3SArd Biesheuvel 	return ret;
52f80fb3a3SArd Biesheuvel }
53f80fb3a3SArd Biesheuvel 
54a762f4ffSMarc Zyngier struct arm64_ftr_override kaslr_feature_override __initdata;
55f80fb3a3SArd Biesheuvel 
56f80fb3a3SArd Biesheuvel /*
57f80fb3a3SArd Biesheuvel  * This routine will be executed with the kernel mapped at its default virtual
58f80fb3a3SArd Biesheuvel  * address, and if it returns successfully, the kernel will be remapped, and
59f80fb3a3SArd Biesheuvel  * start_kernel() will be executed from a randomized virtual offset. The
60f80fb3a3SArd Biesheuvel  * relocation will result in all absolute references (e.g., static variables
61f80fb3a3SArd Biesheuvel  * containing function pointers) to be reinitialized, and zero-initialized
62f80fb3a3SArd Biesheuvel  * .bss variables will be reset to 0.
63f80fb3a3SArd Biesheuvel  */
64f6f0c436SMarc Zyngier u64 __init kaslr_early_init(void)
65f80fb3a3SArd Biesheuvel {
66f80fb3a3SArd Biesheuvel 	void *fdt;
67f80fb3a3SArd Biesheuvel 	u64 seed, offset, mask, module_range;
689bceb80bSGuenter Roeck 	unsigned long raw;
69f80fb3a3SArd Biesheuvel 
70f80fb3a3SArd Biesheuvel 	/*
71f80fb3a3SArd Biesheuvel 	 * Set a reasonable default for module_alloc_base in case
72f80fb3a3SArd Biesheuvel 	 * we end up running with module randomization disabled.
73f80fb3a3SArd Biesheuvel 	 */
74f80fb3a3SArd Biesheuvel 	module_alloc_base = (u64)_etext - MODULES_VSIZE;
758ea23593SArd Biesheuvel 	__flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
76f80fb3a3SArd Biesheuvel 
77f80fb3a3SArd Biesheuvel 	/*
78f80fb3a3SArd Biesheuvel 	 * Try to map the FDT early. If this fails, we simply bail,
79f80fb3a3SArd Biesheuvel 	 * and proceed with KASLR disabled. We will make another
80f80fb3a3SArd Biesheuvel 	 * attempt at mapping the FDT in setup_machine()
81f80fb3a3SArd Biesheuvel 	 */
82f6f0c436SMarc Zyngier 	fdt = get_early_fdt_ptr();
83294a9dddSMark Brown 	if (!fdt) {
84294a9dddSMark Brown 		kaslr_status = KASLR_DISABLED_FDT_REMAP;
85f80fb3a3SArd Biesheuvel 		return 0;
86294a9dddSMark Brown 	}
87f80fb3a3SArd Biesheuvel 
88f80fb3a3SArd Biesheuvel 	/*
89f80fb3a3SArd Biesheuvel 	 * Retrieve (and wipe) the seed from the FDT
90f80fb3a3SArd Biesheuvel 	 */
91f80fb3a3SArd Biesheuvel 	seed = get_kaslr_seed(fdt);
92f80fb3a3SArd Biesheuvel 
93f80fb3a3SArd Biesheuvel 	/*
94f80fb3a3SArd Biesheuvel 	 * Check if 'nokaslr' appears on the command line, and
95f80fb3a3SArd Biesheuvel 	 * return 0 if that is the case.
96f80fb3a3SArd Biesheuvel 	 */
97a762f4ffSMarc Zyngier 	if (kaslr_feature_override.val & kaslr_feature_override.mask & 0xf) {
98294a9dddSMark Brown 		kaslr_status = KASLR_DISABLED_CMDLINE;
99f80fb3a3SArd Biesheuvel 		return 0;
100294a9dddSMark Brown 	}
101f80fb3a3SArd Biesheuvel 
1022e8e1ea8SMark Brown 	/*
1039bceb80bSGuenter Roeck 	 * Mix in any entropy obtainable architecturally if enabled
1049bceb80bSGuenter Roeck 	 * and supported.
1052e8e1ea8SMark Brown 	 */
1062e8e1ea8SMark Brown 
1079bceb80bSGuenter Roeck 	if (arch_get_random_seed_long_early(&raw))
1082e8e1ea8SMark Brown 		seed ^= raw;
1092e8e1ea8SMark Brown 
1102203e1adSMark Brown 	if (!seed) {
1112203e1adSMark Brown 		kaslr_status = KASLR_DISABLED_NO_SEED;
1122203e1adSMark Brown 		return 0;
1132203e1adSMark Brown 	}
1142203e1adSMark Brown 
115f80fb3a3SArd Biesheuvel 	/*
116f80fb3a3SArd Biesheuvel 	 * OK, so we are proceeding with KASLR enabled. Calculate a suitable
117f80fb3a3SArd Biesheuvel 	 * kernel image offset from the seed. Let's place the kernel in the
11890ec95cdSSteve Capper 	 * middle half of the VMALLOC area (VA_BITS_MIN - 2), and stay clear of
119f2b9ba87SArd Biesheuvel 	 * the lower and upper quarters to avoid colliding with other
120f2b9ba87SArd Biesheuvel 	 * allocations.
121f80fb3a3SArd Biesheuvel 	 * Even if we could randomize at page granularity for 16k and 64k pages,
122f80fb3a3SArd Biesheuvel 	 * let's always round to 2 MB so we don't interfere with the ability to
123f80fb3a3SArd Biesheuvel 	 * map using contiguous PTEs
124f80fb3a3SArd Biesheuvel 	 */
12590ec95cdSSteve Capper 	mask = ((1UL << (VA_BITS_MIN - 2)) - 1) & ~(SZ_2M - 1);
12690ec95cdSSteve Capper 	offset = BIT(VA_BITS_MIN - 3) + (seed & mask);
127f80fb3a3SArd Biesheuvel 
128c031a421SArd Biesheuvel 	/* use the top 16 bits to randomize the linear region */
129c031a421SArd Biesheuvel 	memstart_offset_seed = seed >> 48;
130c031a421SArd Biesheuvel 
131*31d02e7aSLecopzer Chen 	if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) &&
132*31d02e7aSLecopzer Chen 	    (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
133*31d02e7aSLecopzer Chen 	     IS_ENABLED(CONFIG_KASAN_SW_TAGS)))
134f80fb3a3SArd Biesheuvel 		/*
135*31d02e7aSLecopzer Chen 		 * KASAN without KASAN_VMALLOC does not expect the module region
136*31d02e7aSLecopzer Chen 		 * to intersect the vmalloc region, since shadow memory is
137*31d02e7aSLecopzer Chen 		 * allocated for each module at load time, whereas the vmalloc
138*31d02e7aSLecopzer Chen 		 * region is shadowed by KASAN zero pages. So keep modules
139*31d02e7aSLecopzer Chen 		 * out of the vmalloc region if KASAN is enabled without
140*31d02e7aSLecopzer Chen 		 * KASAN_VMALLOC, and put the kernel well within 4 GB of the
141*31d02e7aSLecopzer Chen 		 * module region.
142f80fb3a3SArd Biesheuvel 		 */
143f2b9ba87SArd Biesheuvel 		return offset % SZ_2G;
144f80fb3a3SArd Biesheuvel 
145f80fb3a3SArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) {
146f80fb3a3SArd Biesheuvel 		/*
147b2eed9b5SArd Biesheuvel 		 * Randomize the module region over a 2 GB window covering the
148f2b9ba87SArd Biesheuvel 		 * kernel. This reduces the risk of modules leaking information
149f80fb3a3SArd Biesheuvel 		 * about the address of the kernel itself, but results in
150f80fb3a3SArd Biesheuvel 		 * branches between modules and the core kernel that are
151f80fb3a3SArd Biesheuvel 		 * resolved via PLTs. (Branches between modules will be
152f80fb3a3SArd Biesheuvel 		 * resolved normally.)
153f80fb3a3SArd Biesheuvel 		 */
154b2eed9b5SArd Biesheuvel 		module_range = SZ_2G - (u64)(_end - _stext);
155b2eed9b5SArd Biesheuvel 		module_alloc_base = max((u64)_end + offset - SZ_2G,
156f2b9ba87SArd Biesheuvel 					(u64)MODULES_VADDR);
157f80fb3a3SArd Biesheuvel 	} else {
158f80fb3a3SArd Biesheuvel 		/*
159f80fb3a3SArd Biesheuvel 		 * Randomize the module region by setting module_alloc_base to
160f80fb3a3SArd Biesheuvel 		 * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE,
161f80fb3a3SArd Biesheuvel 		 * _stext) . This guarantees that the resulting region still
162f80fb3a3SArd Biesheuvel 		 * covers [_stext, _etext], and that all relative branches can
163f80fb3a3SArd Biesheuvel 		 * be resolved without veneers.
164f80fb3a3SArd Biesheuvel 		 */
165f80fb3a3SArd Biesheuvel 		module_range = MODULES_VSIZE - (u64)(_etext - _stext);
166f80fb3a3SArd Biesheuvel 		module_alloc_base = (u64)_etext + offset - MODULES_VSIZE;
167f80fb3a3SArd Biesheuvel 	}
168f80fb3a3SArd Biesheuvel 
169f80fb3a3SArd Biesheuvel 	/* use the lower 21 bits to randomize the base of the module region */
170f80fb3a3SArd Biesheuvel 	module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21;
171f80fb3a3SArd Biesheuvel 	module_alloc_base &= PAGE_MASK;
172f80fb3a3SArd Biesheuvel 
1731598ecdaSArd Biesheuvel 	__flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
1741598ecdaSArd Biesheuvel 	__flush_dcache_area(&memstart_offset_seed, sizeof(memstart_offset_seed));
1751598ecdaSArd Biesheuvel 
176f80fb3a3SArd Biesheuvel 	return offset;
177f80fb3a3SArd Biesheuvel }
178294a9dddSMark Brown 
179294a9dddSMark Brown static int __init kaslr_init(void)
180294a9dddSMark Brown {
181294a9dddSMark Brown 	switch (kaslr_status) {
182294a9dddSMark Brown 	case KASLR_ENABLED:
183294a9dddSMark Brown 		pr_info("KASLR enabled\n");
184294a9dddSMark Brown 		break;
185294a9dddSMark Brown 	case KASLR_DISABLED_CMDLINE:
186294a9dddSMark Brown 		pr_info("KASLR disabled on command line\n");
187294a9dddSMark Brown 		break;
188294a9dddSMark Brown 	case KASLR_DISABLED_NO_SEED:
189294a9dddSMark Brown 		pr_warn("KASLR disabled due to lack of seed\n");
190294a9dddSMark Brown 		break;
191294a9dddSMark Brown 	case KASLR_DISABLED_FDT_REMAP:
192294a9dddSMark Brown 		pr_warn("KASLR disabled due to FDT remapping failure\n");
193294a9dddSMark Brown 		break;
194294a9dddSMark Brown 	}
195294a9dddSMark Brown 
196294a9dddSMark Brown 	return 0;
197294a9dddSMark Brown }
198294a9dddSMark Brown core_initcall(kaslr_init)
199