18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file implements KASLR memory randomization for x86_64. It randomizes
48c2ecf20Sopenharmony_ci * the virtual address space of kernel memory regions (physical memory
58c2ecf20Sopenharmony_ci * mapping, vmalloc & vmemmap) for x86_64. This security feature mitigates
68c2ecf20Sopenharmony_ci * exploits relying on predictable kernel addresses.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Entropy is generated using the KASLR early boot functions now shared in
98c2ecf20Sopenharmony_ci * the lib directory (originally written by Kees Cook). Randomization is
108c2ecf20Sopenharmony_ci * done on PGD & P4D/PUD page table levels to increase possible addresses.
118c2ecf20Sopenharmony_ci * The physical memory mapping code was adapted to support P4D/PUD level
128c2ecf20Sopenharmony_ci * virtual addresses. This implementation on the best configuration provides
138c2ecf20Sopenharmony_ci * 30,000 possible virtual addresses in average for each memory region.
148c2ecf20Sopenharmony_ci * An additional low memory page is used to ensure each CPU can start with
158c2ecf20Sopenharmony_ci * a PGD aligned virtual address (for realmode).
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * The order of each memory region is not changed. The feature looks at
188c2ecf20Sopenharmony_ci * the available space for the regions based on different configuration
198c2ecf20Sopenharmony_ci * options and randomizes the base and space between each. The size of the
208c2ecf20Sopenharmony_ci * physical memory mapping is the available physical memory.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci#include <linux/init.h>
258c2ecf20Sopenharmony_ci#include <linux/random.h>
268c2ecf20Sopenharmony_ci#include <linux/memblock.h>
278c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <asm/setup.h>
308c2ecf20Sopenharmony_ci#include <asm/kaslr.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "mm_internal.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define TB_SHIFT 40
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * The end address could depend on more configuration options to make the
388c2ecf20Sopenharmony_ci * highest amount of space for randomization available, but that's too hard
398c2ecf20Sopenharmony_ci * to keep straight and caused issues already.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cistatic const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * Memory regions randomized by KASLR (except modules that use a separate logic
458c2ecf20Sopenharmony_ci * earlier during boot). The list is ordered based on virtual addresses. This
468c2ecf20Sopenharmony_ci * order is kept after randomization.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_cistatic __initdata struct kaslr_memory_region {
498c2ecf20Sopenharmony_ci	unsigned long *base;
508c2ecf20Sopenharmony_ci	unsigned long size_tb;
518c2ecf20Sopenharmony_ci} kaslr_regions[] = {
528c2ecf20Sopenharmony_ci	{ &page_offset_base, 0 },
538c2ecf20Sopenharmony_ci	{ &vmalloc_base, 0 },
548c2ecf20Sopenharmony_ci	{ &vmemmap_base, 0 },
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* Get size in bytes used by the memory region */
588c2ecf20Sopenharmony_cistatic inline unsigned long get_padding(struct kaslr_memory_region *region)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return (region->size_tb << TB_SHIFT);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* Initialize base and padding for each memory region randomized with KASLR */
648c2ecf20Sopenharmony_civoid __init kernel_randomize_memory(void)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	size_t i;
678c2ecf20Sopenharmony_ci	unsigned long vaddr_start, vaddr;
688c2ecf20Sopenharmony_ci	unsigned long rand, memory_tb;
698c2ecf20Sopenharmony_ci	struct rnd_state rand_state;
708c2ecf20Sopenharmony_ci	unsigned long remain_entropy;
718c2ecf20Sopenharmony_ci	unsigned long vmemmap_size;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	vaddr_start = pgtable_l5_enabled() ? __PAGE_OFFSET_BASE_L5 : __PAGE_OFFSET_BASE_L4;
748c2ecf20Sopenharmony_ci	vaddr = vaddr_start;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/*
778c2ecf20Sopenharmony_ci	 * These BUILD_BUG_ON checks ensure the memory layout is consistent
788c2ecf20Sopenharmony_ci	 * with the vaddr_start/vaddr_end variables. These checks are very
798c2ecf20Sopenharmony_ci	 * limited....
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	BUILD_BUG_ON(vaddr_start >= vaddr_end);
828c2ecf20Sopenharmony_ci	BUILD_BUG_ON(vaddr_end != CPU_ENTRY_AREA_BASE);
838c2ecf20Sopenharmony_ci	BUILD_BUG_ON(vaddr_end > __START_KERNEL_map);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (!kaslr_memory_enabled())
868c2ecf20Sopenharmony_ci		return;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT);
898c2ecf20Sopenharmony_ci	kaslr_regions[1].size_tb = VMALLOC_SIZE_TB;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/*
928c2ecf20Sopenharmony_ci	 * Update Physical memory mapping to available and
938c2ecf20Sopenharmony_ci	 * add padding if needed (especially for memory hotplug support).
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	BUG_ON(kaslr_regions[0].base != &page_offset_base);
968c2ecf20Sopenharmony_ci	memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
978c2ecf20Sopenharmony_ci		CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* Adapt phyiscal memory region size based on available memory */
1008c2ecf20Sopenharmony_ci	if (memory_tb < kaslr_regions[0].size_tb)
1018c2ecf20Sopenharmony_ci		kaslr_regions[0].size_tb = memory_tb;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/*
1048c2ecf20Sopenharmony_ci	 * Calculate the vmemmap region size in TBs, aligned to a TB
1058c2ecf20Sopenharmony_ci	 * boundary.
1068c2ecf20Sopenharmony_ci	 */
1078c2ecf20Sopenharmony_ci	vmemmap_size = (kaslr_regions[0].size_tb << (TB_SHIFT - PAGE_SHIFT)) *
1088c2ecf20Sopenharmony_ci			sizeof(struct page);
1098c2ecf20Sopenharmony_ci	kaslr_regions[2].size_tb = DIV_ROUND_UP(vmemmap_size, 1UL << TB_SHIFT);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* Calculate entropy available between regions */
1128c2ecf20Sopenharmony_ci	remain_entropy = vaddr_end - vaddr_start;
1138c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++)
1148c2ecf20Sopenharmony_ci		remain_entropy -= get_padding(&kaslr_regions[i]);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	prandom_seed_state(&rand_state, kaslr_get_random_long("Memory"));
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) {
1198c2ecf20Sopenharmony_ci		unsigned long entropy;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci		/*
1228c2ecf20Sopenharmony_ci		 * Select a random virtual address using the extra entropy
1238c2ecf20Sopenharmony_ci		 * available.
1248c2ecf20Sopenharmony_ci		 */
1258c2ecf20Sopenharmony_ci		entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i);
1268c2ecf20Sopenharmony_ci		prandom_bytes_state(&rand_state, &rand, sizeof(rand));
1278c2ecf20Sopenharmony_ci		entropy = (rand % (entropy + 1)) & PUD_MASK;
1288c2ecf20Sopenharmony_ci		vaddr += entropy;
1298c2ecf20Sopenharmony_ci		*kaslr_regions[i].base = vaddr;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		/*
1328c2ecf20Sopenharmony_ci		 * Jump the region and add a minimum padding based on
1338c2ecf20Sopenharmony_ci		 * randomization alignment.
1348c2ecf20Sopenharmony_ci		 */
1358c2ecf20Sopenharmony_ci		vaddr += get_padding(&kaslr_regions[i]);
1368c2ecf20Sopenharmony_ci		vaddr = round_up(vaddr + 1, PUD_SIZE);
1378c2ecf20Sopenharmony_ci		remain_entropy -= entropy;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_civoid __meminit init_trampoline_kaslr(void)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	pud_t *pud_page_tramp, *pud, *pud_tramp;
1448c2ecf20Sopenharmony_ci	p4d_t *p4d_page_tramp, *p4d, *p4d_tramp;
1458c2ecf20Sopenharmony_ci	unsigned long paddr, vaddr;
1468c2ecf20Sopenharmony_ci	pgd_t *pgd;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	pud_page_tramp = alloc_low_page();
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/*
1518c2ecf20Sopenharmony_ci	 * There are two mappings for the low 1MB area, the direct mapping
1528c2ecf20Sopenharmony_ci	 * and the 1:1 mapping for the real mode trampoline:
1538c2ecf20Sopenharmony_ci	 *
1548c2ecf20Sopenharmony_ci	 * Direct mapping: virt_addr = phys_addr + PAGE_OFFSET
1558c2ecf20Sopenharmony_ci	 * 1:1 mapping:    virt_addr = phys_addr
1568c2ecf20Sopenharmony_ci	 */
1578c2ecf20Sopenharmony_ci	paddr = 0;
1588c2ecf20Sopenharmony_ci	vaddr = (unsigned long)__va(paddr);
1598c2ecf20Sopenharmony_ci	pgd = pgd_offset_k(vaddr);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	p4d = p4d_offset(pgd, vaddr);
1628c2ecf20Sopenharmony_ci	pud = pud_offset(p4d, vaddr);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	pud_tramp = pud_page_tramp + pud_index(paddr);
1658c2ecf20Sopenharmony_ci	*pud_tramp = *pud;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (pgtable_l5_enabled()) {
1688c2ecf20Sopenharmony_ci		p4d_page_tramp = alloc_low_page();
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		p4d_tramp = p4d_page_tramp + p4d_index(paddr);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		set_p4d(p4d_tramp,
1738c2ecf20Sopenharmony_ci			__p4d(_KERNPG_TABLE | __pa(pud_page_tramp)));
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		trampoline_pgd_entry =
1768c2ecf20Sopenharmony_ci			__pgd(_KERNPG_TABLE | __pa(p4d_page_tramp));
1778c2ecf20Sopenharmony_ci	} else {
1788c2ecf20Sopenharmony_ci		trampoline_pgd_entry =
1798c2ecf20Sopenharmony_ci			__pgd(_KERNPG_TABLE | __pa(pud_page_tramp));
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci}
182