xref: /linux/drivers/firmware/efi/libstub/kaslr.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Helper functions used by the EFI stub on multiple
4  * architectures to deal with physical address space randomization.
5  */
6 #include <linux/efi.h>
7 
8 #include "efistub.h"
9 
10 /**
11  * efi_kaslr_get_phys_seed() - Get random seed for physical kernel KASLR
12  * @image_handle:	Handle to the image
13  *
14  * If KASLR is not disabled, obtain a random seed using EFI_RNG_PROTOCOL
15  * that will be used to move the kernel physical mapping.
16  *
17  * Return:	the random seed
18  */
19 u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle)
20 {
21 	static efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
22 	void *p;
23 
24 	if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
25 		return 0;
26 
27 	if (efi_nokaslr) {
28 		efi_info("KASLR disabled on kernel command line\n");
29 	} else if (efi_bs_call(handle_protocol, image_handle,
30 			       &li_fixed_proto, &p) == EFI_SUCCESS) {
31 		efi_info("Image placement fixed by loader\n");
32 	} else {
33 		efi_status_t status;
34 		u32 phys_seed;
35 
36 		status = efi_get_random_bytes(sizeof(phys_seed),
37 					      (u8 *)&phys_seed);
38 		if (status == EFI_SUCCESS)
39 			return phys_seed;
40 
41 		if (status == EFI_NOT_FOUND)
42 			efi_info("EFI_RNG_PROTOCOL unavailable\n");
43 		else
44 			efi_err("efi_get_random_bytes() failed (0x%lx)\n", status);
45 
46 		efi_nokaslr = true;
47 	}
48 
49 	return 0;
50 }
51 
52 /*
53  * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
54  * to provide space, and fail to zero it). Check for this condition by double
55  * checking that the first and the last byte of the image are covered by the
56  * same EFI memory map entry.
57  */
58 static bool check_image_region(u64 base, u64 size)
59 {
60 	struct efi_boot_memmap *map __free(efi_pool) = NULL;
61 	efi_status_t status;
62 	unsigned long map_offset;
63 
64 	status = efi_get_memory_map(&map, false);
65 	if (status != EFI_SUCCESS)
66 		return false;
67 
68 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
69 		efi_memory_desc_t *md = (void *)map->map + map_offset;
70 		u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
71 
72 		/*
73 		 * Find the region that covers base, and return whether
74 		 * it covers base+size bytes.
75 		 */
76 		if (base >= md->phys_addr && base < end)
77 			return (base + size) <= end;
78 	}
79 
80 	return false;
81 }
82 
83 /**
84  * efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled)
85  * @image_addr:		Pointer to the current kernel location
86  * @reserve_addr:	Pointer to the relocated kernel location
87  * @reserve_size:	Size of the relocated kernel
88  * @kernel_size:	Size of the text + data
89  * @kernel_codesize:	Size of the text
90  * @kernel_memsize:	Size of the text + data + bss
91  * @phys_seed:		Random seed used for the relocation
92  *
93  * If KASLR is not enabled, this function relocates the kernel to a fixed
94  * address or leaves it at its current location. If KASLR is enabled, the
95  * kernel's physical location is randomized using the supplied seed.
96  *
97  * Return:	status code, EFI_SUCCESS if relocation is successful
98  */
99 efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
100 				       unsigned long *reserve_addr,
101 				       unsigned long *reserve_size,
102 				       unsigned long kernel_size,
103 				       unsigned long kernel_codesize,
104 				       unsigned long kernel_memsize,
105 				       u32 phys_seed)
106 {
107 	efi_status_t status;
108 	u64 min_kimg_align = efi_get_kimg_min_align();
109 
110 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
111 		/*
112 		 * If KASLR is enabled, and we have some randomness available,
113 		 * locate the kernel at a randomized offset in physical memory.
114 		 */
115 		status = efi_random_alloc(*reserve_size, min_kimg_align,
116 					  reserve_addr, phys_seed,
117 					  EFI_LOADER_CODE, 0, EFI_ALLOC_LIMIT);
118 		if (status != EFI_SUCCESS)
119 			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
120 	} else {
121 		status = EFI_OUT_OF_RESOURCES;
122 	}
123 
124 	if (status != EFI_SUCCESS) {
125 		if (!check_image_region(*image_addr, kernel_memsize)) {
126 			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
127 		} else if (IS_ALIGNED(*image_addr, min_kimg_align) &&
128 			   (unsigned long)_end < EFI_ALLOC_LIMIT) {
129 			/*
130 			 * Just execute from wherever we were loaded by the
131 			 * UEFI PE/COFF loader if the placement is suitable.
132 			 */
133 			*reserve_size = 0;
134 			return EFI_SUCCESS;
135 		}
136 
137 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
138 						    ULONG_MAX, min_kimg_align,
139 						    EFI_LOADER_CODE);
140 
141 		if (status != EFI_SUCCESS) {
142 			efi_err("Failed to relocate kernel\n");
143 			*reserve_size = 0;
144 			return status;
145 		}
146 	}
147 
148 	memcpy((void *)*reserve_addr, (void *)*image_addr, kernel_size);
149 	*image_addr = *reserve_addr;
150 	efi_icache_sync(*image_addr, *image_addr + kernel_codesize);
151 	efi_remap_image(*image_addr, *reserve_size, kernel_codesize);
152 
153 	return status;
154 }
155