1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/cache.h>
7*4882a593Smuzhiyun #include <linux/crc32.h>
8*4882a593Smuzhiyun #include <linux/init.h>
9*4882a593Smuzhiyun #include <linux/libfdt.h>
10*4882a593Smuzhiyun #include <linux/mm_types.h>
11*4882a593Smuzhiyun #include <linux/sched.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/pgtable.h>
14*4882a593Smuzhiyun #include <linux/random.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <asm/cacheflush.h>
17*4882a593Smuzhiyun #include <asm/fixmap.h>
18*4882a593Smuzhiyun #include <asm/kernel-pgtable.h>
19*4882a593Smuzhiyun #include <asm/memory.h>
20*4882a593Smuzhiyun #include <asm/mmu.h>
21*4882a593Smuzhiyun #include <asm/sections.h>
22*4882a593Smuzhiyun #include <asm/setup.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun enum kaslr_status {
25*4882a593Smuzhiyun KASLR_ENABLED,
26*4882a593Smuzhiyun KASLR_DISABLED_CMDLINE,
27*4882a593Smuzhiyun KASLR_DISABLED_NO_SEED,
28*4882a593Smuzhiyun KASLR_DISABLED_FDT_REMAP,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static enum kaslr_status __initdata kaslr_status;
32*4882a593Smuzhiyun u64 __ro_after_init module_alloc_base;
33*4882a593Smuzhiyun u16 __initdata memstart_offset_seed;
34*4882a593Smuzhiyun
get_kaslr_seed(void * fdt)35*4882a593Smuzhiyun static __init u64 get_kaslr_seed(void *fdt)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun int node, len;
38*4882a593Smuzhiyun fdt64_t *prop;
39*4882a593Smuzhiyun u64 ret;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun node = fdt_path_offset(fdt, "/chosen");
42*4882a593Smuzhiyun if (node < 0)
43*4882a593Smuzhiyun return 0;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun prop = fdt_getprop_w(fdt, node, "kaslr-seed", &len);
46*4882a593Smuzhiyun if (!prop || len != sizeof(u64))
47*4882a593Smuzhiyun return 0;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun ret = fdt64_to_cpu(*prop);
50*4882a593Smuzhiyun *prop = 0;
51*4882a593Smuzhiyun return ret;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun struct arm64_ftr_override kaslr_feature_override __initdata;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * This routine will be executed with the kernel mapped at its default virtual
58*4882a593Smuzhiyun * address, and if it returns successfully, the kernel will be remapped, and
59*4882a593Smuzhiyun * start_kernel() will be executed from a randomized virtual offset. The
60*4882a593Smuzhiyun * relocation will result in all absolute references (e.g., static variables
61*4882a593Smuzhiyun * containing function pointers) to be reinitialized, and zero-initialized
62*4882a593Smuzhiyun * .bss variables will be reset to 0.
63*4882a593Smuzhiyun */
kaslr_early_init(void)64*4882a593Smuzhiyun u64 __init kaslr_early_init(void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun void *fdt;
67*4882a593Smuzhiyun u64 seed, offset, mask, module_range;
68*4882a593Smuzhiyun unsigned long raw;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * Set a reasonable default for module_alloc_base in case
72*4882a593Smuzhiyun * we end up running with module randomization disabled.
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun module_alloc_base = (u64)_etext - MODULES_VSIZE;
75*4882a593Smuzhiyun __flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * Try to map the FDT early. If this fails, we simply bail,
79*4882a593Smuzhiyun * and proceed with KASLR disabled. We will make another
80*4882a593Smuzhiyun * attempt at mapping the FDT in setup_machine()
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun fdt = get_early_fdt_ptr();
83*4882a593Smuzhiyun if (!fdt) {
84*4882a593Smuzhiyun kaslr_status = KASLR_DISABLED_FDT_REMAP;
85*4882a593Smuzhiyun return 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * Retrieve (and wipe) the seed from the FDT
90*4882a593Smuzhiyun */
91*4882a593Smuzhiyun seed = get_kaslr_seed(fdt);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * Check if 'nokaslr' appears on the command line, and
95*4882a593Smuzhiyun * return 0 if that is the case.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun if (kaslr_feature_override.val & kaslr_feature_override.mask & 0xf) {
98*4882a593Smuzhiyun kaslr_status = KASLR_DISABLED_CMDLINE;
99*4882a593Smuzhiyun return 0;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Mix in any entropy obtainable architecturally if enabled
104*4882a593Smuzhiyun * and supported.
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (arch_get_random_seed_long_early(&raw))
108*4882a593Smuzhiyun seed ^= raw;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun if (!seed) {
111*4882a593Smuzhiyun kaslr_status = KASLR_DISABLED_NO_SEED;
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun * OK, so we are proceeding with KASLR enabled. Calculate a suitable
117*4882a593Smuzhiyun * kernel image offset from the seed. Let's place the kernel in the
118*4882a593Smuzhiyun * middle half of the VMALLOC area (VA_BITS_MIN - 2), and stay clear of
119*4882a593Smuzhiyun * the lower and upper quarters to avoid colliding with other
120*4882a593Smuzhiyun * allocations.
121*4882a593Smuzhiyun * Even if we could randomize at page granularity for 16k and 64k pages,
122*4882a593Smuzhiyun * let's always round to 2 MB so we don't interfere with the ability to
123*4882a593Smuzhiyun * map using contiguous PTEs
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun mask = ((1UL << (VA_BITS_MIN - 2)) - 1) & ~(SZ_2M - 1);
126*4882a593Smuzhiyun offset = BIT(VA_BITS_MIN - 3) + (seed & mask);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* use the top 16 bits to randomize the linear region */
129*4882a593Smuzhiyun memstart_offset_seed = seed >> 48;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) &&
132*4882a593Smuzhiyun (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
133*4882a593Smuzhiyun IS_ENABLED(CONFIG_KASAN_SW_TAGS)))
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * KASAN without KASAN_VMALLOC does not expect the module region
136*4882a593Smuzhiyun * to intersect the vmalloc region, since shadow memory is
137*4882a593Smuzhiyun * allocated for each module at load time, whereas the vmalloc
138*4882a593Smuzhiyun * region is shadowed by KASAN zero pages. So keep modules
139*4882a593Smuzhiyun * out of the vmalloc region if KASAN is enabled without
140*4882a593Smuzhiyun * KASAN_VMALLOC, and put the kernel well within 4 GB of the
141*4882a593Smuzhiyun * module region.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun return offset % SZ_2G;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) {
146*4882a593Smuzhiyun /*
147*4882a593Smuzhiyun * Randomize the module region over a 2 GB window covering the
148*4882a593Smuzhiyun * kernel. This reduces the risk of modules leaking information
149*4882a593Smuzhiyun * about the address of the kernel itself, but results in
150*4882a593Smuzhiyun * branches between modules and the core kernel that are
151*4882a593Smuzhiyun * resolved via PLTs. (Branches between modules will be
152*4882a593Smuzhiyun * resolved normally.)
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun module_range = SZ_2G - (u64)(_end - _stext);
155*4882a593Smuzhiyun module_alloc_base = max((u64)_end + offset - SZ_2G,
156*4882a593Smuzhiyun (u64)MODULES_VADDR);
157*4882a593Smuzhiyun } else {
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * Randomize the module region by setting module_alloc_base to
160*4882a593Smuzhiyun * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE,
161*4882a593Smuzhiyun * _stext) . This guarantees that the resulting region still
162*4882a593Smuzhiyun * covers [_stext, _etext], and that all relative branches can
163*4882a593Smuzhiyun * be resolved without veneers.
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun module_range = MODULES_VSIZE - (u64)(_etext - _stext);
166*4882a593Smuzhiyun module_alloc_base = (u64)_etext + offset - MODULES_VSIZE;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* use the lower 21 bits to randomize the base of the module region */
170*4882a593Smuzhiyun module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21;
171*4882a593Smuzhiyun module_alloc_base &= PAGE_MASK;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun __flush_dcache_area(&module_alloc_base, sizeof(module_alloc_base));
174*4882a593Smuzhiyun __flush_dcache_area(&memstart_offset_seed, sizeof(memstart_offset_seed));
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun return offset;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
kaslr_init(void)179*4882a593Smuzhiyun static int __init kaslr_init(void)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun switch (kaslr_status) {
182*4882a593Smuzhiyun case KASLR_ENABLED:
183*4882a593Smuzhiyun pr_info("KASLR enabled\n");
184*4882a593Smuzhiyun break;
185*4882a593Smuzhiyun case KASLR_DISABLED_CMDLINE:
186*4882a593Smuzhiyun pr_info("KASLR disabled on command line\n");
187*4882a593Smuzhiyun break;
188*4882a593Smuzhiyun case KASLR_DISABLED_NO_SEED:
189*4882a593Smuzhiyun pr_warn("KASLR disabled due to lack of seed\n");
190*4882a593Smuzhiyun break;
191*4882a593Smuzhiyun case KASLR_DISABLED_FDT_REMAP:
192*4882a593Smuzhiyun pr_warn("KASLR disabled due to FDT remapping failure\n");
193*4882a593Smuzhiyun break;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun return 0;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun core_initcall(kaslr_init)
199