Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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