1f80fb3a3SArd Biesheuvel /* 2f80fb3a3SArd Biesheuvel * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org> 3f80fb3a3SArd Biesheuvel * 4f80fb3a3SArd Biesheuvel * This program is free software; you can redistribute it and/or modify 5f80fb3a3SArd Biesheuvel * it under the terms of the GNU General Public License version 2 as 6f80fb3a3SArd Biesheuvel * published by the Free Software Foundation. 7f80fb3a3SArd Biesheuvel */ 8f80fb3a3SArd Biesheuvel 95a9e3e15SJisheng Zhang #include <linux/cache.h> 10f80fb3a3SArd Biesheuvel #include <linux/crc32.h> 11f80fb3a3SArd Biesheuvel #include <linux/init.h> 12f80fb3a3SArd Biesheuvel #include <linux/libfdt.h> 13f80fb3a3SArd Biesheuvel #include <linux/mm_types.h> 14f80fb3a3SArd Biesheuvel #include <linux/sched.h> 15f80fb3a3SArd Biesheuvel #include <linux/types.h> 16f80fb3a3SArd Biesheuvel 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/pgtable.h> 22f80fb3a3SArd Biesheuvel #include <asm/sections.h> 23f80fb3a3SArd Biesheuvel 245a9e3e15SJisheng Zhang u64 __ro_after_init module_alloc_base; 25c031a421SArd Biesheuvel u16 __initdata memstart_offset_seed; 26f80fb3a3SArd Biesheuvel 27f80fb3a3SArd Biesheuvel static __init u64 get_kaslr_seed(void *fdt) 28f80fb3a3SArd Biesheuvel { 29f80fb3a3SArd Biesheuvel int node, len; 3067831edfSLuc Van Oostenryck fdt64_t *prop; 31f80fb3a3SArd Biesheuvel u64 ret; 32f80fb3a3SArd Biesheuvel 33f80fb3a3SArd Biesheuvel node = fdt_path_offset(fdt, "/chosen"); 34f80fb3a3SArd Biesheuvel if (node < 0) 35f80fb3a3SArd Biesheuvel return 0; 36f80fb3a3SArd Biesheuvel 37f80fb3a3SArd Biesheuvel prop = fdt_getprop_w(fdt, node, "kaslr-seed", &len); 38f80fb3a3SArd Biesheuvel if (!prop || len != sizeof(u64)) 39f80fb3a3SArd Biesheuvel return 0; 40f80fb3a3SArd Biesheuvel 41f80fb3a3SArd Biesheuvel ret = fdt64_to_cpu(*prop); 42f80fb3a3SArd Biesheuvel *prop = 0; 43f80fb3a3SArd Biesheuvel return ret; 44f80fb3a3SArd Biesheuvel } 45f80fb3a3SArd Biesheuvel 46f80fb3a3SArd Biesheuvel static __init const u8 *get_cmdline(void *fdt) 47f80fb3a3SArd Biesheuvel { 48f80fb3a3SArd Biesheuvel static __initconst const u8 default_cmdline[] = CONFIG_CMDLINE; 49f80fb3a3SArd Biesheuvel 50f80fb3a3SArd Biesheuvel if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) { 51f80fb3a3SArd Biesheuvel int node; 52f80fb3a3SArd Biesheuvel const u8 *prop; 53f80fb3a3SArd Biesheuvel 54f80fb3a3SArd Biesheuvel node = fdt_path_offset(fdt, "/chosen"); 55f80fb3a3SArd Biesheuvel if (node < 0) 56f80fb3a3SArd Biesheuvel goto out; 57f80fb3a3SArd Biesheuvel 58f80fb3a3SArd Biesheuvel prop = fdt_getprop(fdt, node, "bootargs", NULL); 59f80fb3a3SArd Biesheuvel if (!prop) 60f80fb3a3SArd Biesheuvel goto out; 61f80fb3a3SArd Biesheuvel return prop; 62f80fb3a3SArd Biesheuvel } 63f80fb3a3SArd Biesheuvel out: 64f80fb3a3SArd Biesheuvel return default_cmdline; 65f80fb3a3SArd Biesheuvel } 66f80fb3a3SArd Biesheuvel 67f80fb3a3SArd Biesheuvel extern void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, 68f80fb3a3SArd Biesheuvel pgprot_t prot); 69f80fb3a3SArd Biesheuvel 70f80fb3a3SArd Biesheuvel /* 71f80fb3a3SArd Biesheuvel * This routine will be executed with the kernel mapped at its default virtual 72f80fb3a3SArd Biesheuvel * address, and if it returns successfully, the kernel will be remapped, and 73f80fb3a3SArd Biesheuvel * start_kernel() will be executed from a randomized virtual offset. The 74f80fb3a3SArd Biesheuvel * relocation will result in all absolute references (e.g., static variables 75f80fb3a3SArd Biesheuvel * containing function pointers) to be reinitialized, and zero-initialized 76f80fb3a3SArd Biesheuvel * .bss variables will be reset to 0. 77f80fb3a3SArd Biesheuvel */ 784a23e56aSArd Biesheuvel u64 __init kaslr_early_init(u64 dt_phys) 79f80fb3a3SArd Biesheuvel { 80f80fb3a3SArd Biesheuvel void *fdt; 81f80fb3a3SArd Biesheuvel u64 seed, offset, mask, module_range; 82f80fb3a3SArd Biesheuvel const u8 *cmdline, *str; 83f80fb3a3SArd Biesheuvel int size; 84f80fb3a3SArd Biesheuvel 85f80fb3a3SArd Biesheuvel /* 86f80fb3a3SArd Biesheuvel * Set a reasonable default for module_alloc_base in case 87f80fb3a3SArd Biesheuvel * we end up running with module randomization disabled. 88f80fb3a3SArd Biesheuvel */ 89f80fb3a3SArd Biesheuvel module_alloc_base = (u64)_etext - MODULES_VSIZE; 90f80fb3a3SArd Biesheuvel 91f80fb3a3SArd Biesheuvel /* 92f80fb3a3SArd Biesheuvel * Try to map the FDT early. If this fails, we simply bail, 93f80fb3a3SArd Biesheuvel * and proceed with KASLR disabled. We will make another 94f80fb3a3SArd Biesheuvel * attempt at mapping the FDT in setup_machine() 95f80fb3a3SArd Biesheuvel */ 96f80fb3a3SArd Biesheuvel early_fixmap_init(); 97f80fb3a3SArd Biesheuvel fdt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL); 98f80fb3a3SArd Biesheuvel if (!fdt) 99f80fb3a3SArd Biesheuvel return 0; 100f80fb3a3SArd Biesheuvel 101f80fb3a3SArd Biesheuvel /* 102f80fb3a3SArd Biesheuvel * Retrieve (and wipe) the seed from the FDT 103f80fb3a3SArd Biesheuvel */ 104f80fb3a3SArd Biesheuvel seed = get_kaslr_seed(fdt); 105f80fb3a3SArd Biesheuvel if (!seed) 106f80fb3a3SArd Biesheuvel return 0; 107f80fb3a3SArd Biesheuvel 108f80fb3a3SArd Biesheuvel /* 109f80fb3a3SArd Biesheuvel * Check if 'nokaslr' appears on the command line, and 110f80fb3a3SArd Biesheuvel * return 0 if that is the case. 111f80fb3a3SArd Biesheuvel */ 112f80fb3a3SArd Biesheuvel cmdline = get_cmdline(fdt); 113f80fb3a3SArd Biesheuvel str = strstr(cmdline, "nokaslr"); 114f80fb3a3SArd Biesheuvel if (str == cmdline || (str > cmdline && *(str - 1) == ' ')) 115f80fb3a3SArd Biesheuvel return 0; 116f80fb3a3SArd Biesheuvel 117f80fb3a3SArd Biesheuvel /* 118f80fb3a3SArd Biesheuvel * OK, so we are proceeding with KASLR enabled. Calculate a suitable 119f80fb3a3SArd Biesheuvel * kernel image offset from the seed. Let's place the kernel in the 120f80fb3a3SArd Biesheuvel * lower half of the VMALLOC area (VA_BITS - 2). 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 */ 125f80fb3a3SArd Biesheuvel mask = ((1UL << (VA_BITS - 2)) - 1) & ~(SZ_2M - 1); 126f80fb3a3SArd Biesheuvel offset = 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 131f80fb3a3SArd Biesheuvel /* 132f80fb3a3SArd Biesheuvel * The kernel Image should not extend across a 1GB/32MB/512MB alignment 133f80fb3a3SArd Biesheuvel * boundary (for 4KB/16KB/64KB granule kernels, respectively). If this 134afd0e5a8SNeeraj Upadhyay * happens, increase the KASLR offset by the size of the kernel image 135afd0e5a8SNeeraj Upadhyay * rounded up by SWAPPER_BLOCK_SIZE. 1364a23e56aSArd Biesheuvel * 1374a23e56aSArd Biesheuvel * NOTE: The references to _text and _end below will already take the 1384a23e56aSArd Biesheuvel * modulo offset (the physical displacement modulo 2 MB) into 1394a23e56aSArd Biesheuvel * account, given that the physical placement is controlled by 1404a23e56aSArd Biesheuvel * the loader, and will not change as a result of the virtual 1414a23e56aSArd Biesheuvel * mapping we choose. 142f80fb3a3SArd Biesheuvel */ 1434a23e56aSArd Biesheuvel if ((((u64)_text + offset) >> SWAPPER_TABLE_SHIFT) != 1444a23e56aSArd Biesheuvel (((u64)_end + offset) >> SWAPPER_TABLE_SHIFT)) { 145afd0e5a8SNeeraj Upadhyay u64 kimg_sz = _end - _text; 146afd0e5a8SNeeraj Upadhyay offset = (offset + round_up(kimg_sz, SWAPPER_BLOCK_SIZE)) 147afd0e5a8SNeeraj Upadhyay & mask; 148afd0e5a8SNeeraj Upadhyay } 149f80fb3a3SArd Biesheuvel 150f80fb3a3SArd Biesheuvel if (IS_ENABLED(CONFIG_KASAN)) 151f80fb3a3SArd Biesheuvel /* 152f80fb3a3SArd Biesheuvel * KASAN does not expect the module region to intersect the 153f80fb3a3SArd Biesheuvel * vmalloc region, since shadow memory is allocated for each 154f80fb3a3SArd Biesheuvel * module at load time, whereas the vmalloc region is shadowed 155f80fb3a3SArd Biesheuvel * by KASAN zero pages. So keep modules out of the vmalloc 156f80fb3a3SArd Biesheuvel * region if KASAN is enabled. 157f80fb3a3SArd Biesheuvel */ 158f80fb3a3SArd Biesheuvel return offset; 159f80fb3a3SArd Biesheuvel 160f80fb3a3SArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) { 161f80fb3a3SArd Biesheuvel /* 162f80fb3a3SArd Biesheuvel * Randomize the module region independently from the core 163f80fb3a3SArd Biesheuvel * kernel. This prevents modules from leaking any information 164f80fb3a3SArd Biesheuvel * about the address of the kernel itself, but results in 165f80fb3a3SArd Biesheuvel * branches between modules and the core kernel that are 166f80fb3a3SArd Biesheuvel * resolved via PLTs. (Branches between modules will be 167f80fb3a3SArd Biesheuvel * resolved normally.) 168f80fb3a3SArd Biesheuvel */ 169f80fb3a3SArd Biesheuvel module_range = VMALLOC_END - VMALLOC_START - MODULES_VSIZE; 170f80fb3a3SArd Biesheuvel module_alloc_base = VMALLOC_START; 171f80fb3a3SArd Biesheuvel } else { 172f80fb3a3SArd Biesheuvel /* 173f80fb3a3SArd Biesheuvel * Randomize the module region by setting module_alloc_base to 174f80fb3a3SArd Biesheuvel * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE, 175f80fb3a3SArd Biesheuvel * _stext) . This guarantees that the resulting region still 176f80fb3a3SArd Biesheuvel * covers [_stext, _etext], and that all relative branches can 177f80fb3a3SArd Biesheuvel * be resolved without veneers. 178f80fb3a3SArd Biesheuvel */ 179f80fb3a3SArd Biesheuvel module_range = MODULES_VSIZE - (u64)(_etext - _stext); 180f80fb3a3SArd Biesheuvel module_alloc_base = (u64)_etext + offset - MODULES_VSIZE; 181f80fb3a3SArd Biesheuvel } 182f80fb3a3SArd Biesheuvel 183f80fb3a3SArd Biesheuvel /* use the lower 21 bits to randomize the base of the module region */ 184f80fb3a3SArd Biesheuvel module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21; 185f80fb3a3SArd Biesheuvel module_alloc_base &= PAGE_MASK; 186f80fb3a3SArd Biesheuvel 187f80fb3a3SArd Biesheuvel return offset; 188f80fb3a3SArd Biesheuvel } 189