|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | #include <linux/cache.h> |
7 | | -#include <linux/crc32.h> |
8 | 7 | #include <linux/init.h> |
9 | | -#include <linux/libfdt.h> |
10 | | -#include <linux/mm_types.h> |
11 | | -#include <linux/sched.h> |
12 | | -#include <linux/types.h> |
13 | | -#include <linux/pgtable.h> |
14 | | -#include <linux/random.h> |
| 8 | +#include <linux/printk.h> |
15 | 9 |
|
16 | | -#include <asm/fixmap.h> |
17 | | -#include <asm/kernel-pgtable.h> |
| 10 | +#include <asm/cpufeature.h> |
18 | 11 | #include <asm/memory.h> |
19 | | -#include <asm/mmu.h> |
20 | | -#include <asm/sections.h> |
21 | | -#include <asm/setup.h> |
22 | 12 |
|
23 | | -u64 __ro_after_init module_alloc_base; |
24 | 13 | u16 __initdata memstart_offset_seed; |
25 | 14 |
|
26 | | -static int __init kaslr_init(void) |
27 | | -{ |
28 | | - u64 module_range; |
29 | | - u32 seed; |
30 | | - |
31 | | - /* |
32 | | - * Set a reasonable default for module_alloc_base in case |
33 | | - * we end up running with module randomization disabled. |
34 | | - */ |
35 | | - module_alloc_base = (u64)_etext - MODULES_VSIZE; |
| 15 | +bool __ro_after_init __kaslr_is_enabled = false; |
36 | 16 |
|
| 17 | +void __init kaslr_init(void) |
| 18 | +{ |
37 | 19 | if (cpuid_feature_extract_unsigned_field(arm64_sw_feature_override.val & |
38 | 20 | arm64_sw_feature_override.mask, |
39 | 21 | ARM64_SW_FEATURE_OVERRIDE_NOKASLR)) { |
40 | 22 | pr_info("KASLR disabled on command line\n"); |
41 | | - return 0; |
42 | | - } |
43 | | - |
44 | | - if (!kaslr_enabled()) { |
45 | | - pr_warn("KASLR disabled due to lack of seed\n"); |
46 | | - return 0; |
| 23 | + return; |
47 | 24 | } |
48 | 25 |
|
49 | | - pr_info("KASLR enabled\n"); |
50 | | - |
51 | 26 | /* |
52 | | - * KASAN without KASAN_VMALLOC does not expect the module region to |
53 | | - * intersect the vmalloc region, since shadow memory is allocated for |
54 | | - * each module at load time, whereas the vmalloc region will already be |
55 | | - * shadowed by KASAN zero pages. |
| 27 | + * The KASLR offset modulo MIN_KIMG_ALIGN is taken from the physical |
| 28 | + * placement of the image rather than from the seed, so a displacement |
| 29 | + * of less than MIN_KIMG_ALIGN means that no seed was provided. |
56 | 30 | */ |
57 | | - BUILD_BUG_ON((IS_ENABLED(CONFIG_KASAN_GENERIC) || |
58 | | - IS_ENABLED(CONFIG_KASAN_SW_TAGS)) && |
59 | | - !IS_ENABLED(CONFIG_KASAN_VMALLOC)); |
60 | | - |
61 | | - seed = get_random_u32(); |
62 | | - |
63 | | - if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) { |
64 | | - /* |
65 | | - * Randomize the module region over a 2 GB window covering the |
66 | | - * kernel. This reduces the risk of modules leaking information |
67 | | - * about the address of the kernel itself, but results in |
68 | | - * branches between modules and the core kernel that are |
69 | | - * resolved via PLTs. (Branches between modules will be |
70 | | - * resolved normally.) |
71 | | - */ |
72 | | - module_range = SZ_2G - (u64)(_end - _stext); |
73 | | - module_alloc_base = max((u64)_end - SZ_2G, (u64)MODULES_VADDR); |
74 | | - } else { |
75 | | - /* |
76 | | - * Randomize the module region by setting module_alloc_base to |
77 | | - * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE, |
78 | | - * _stext) . This guarantees that the resulting region still |
79 | | - * covers [_stext, _etext], and that all relative branches can |
80 | | - * be resolved without veneers unless this region is exhausted |
81 | | - * and we fall back to a larger 2GB window in module_alloc() |
82 | | - * when ARM64_MODULE_PLTS is enabled. |
83 | | - */ |
84 | | - module_range = MODULES_VSIZE - (u64)(_etext - _stext); |
| 31 | + if (kaslr_offset() < MIN_KIMG_ALIGN) { |
| 32 | + pr_warn("KASLR disabled due to lack of seed\n"); |
| 33 | + return; |
85 | 34 | } |
86 | 35 |
|
87 | | - /* use the lower 21 bits to randomize the base of the module region */ |
88 | | - module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21; |
89 | | - module_alloc_base &= PAGE_MASK; |
90 | | - |
91 | | - return 0; |
| 36 | + pr_info("KASLR enabled\n"); |
| 37 | + __kaslr_is_enabled = true; |
92 | 38 | } |
93 | | -subsys_initcall(kaslr_init) |
0 commit comments