Skip to content

Commit 6b56beb

Browse files
Alexandre Ghitipalmer-dabbelt
authored andcommitted
arm64: libstub: Move KASLR handling functions to kaslr.c
This prepares for riscv to use the same functions to handle the pĥysical kernel move when KASLR is enabled. Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> Acked-by: Ard Biesheuvel <ardb@kernel.org> Tested-by: Conor Dooley <conor.dooley@microchip.com> Tested-by: Song Shuai <songshuaishuai@tinylab.org> Reviewed-by: Sami Tolvanen <samitolvanen@google.com> Tested-by: Sami Tolvanen <samitolvanen@google.com> Link: https://lore.kernel.org/r/20230722123850.634544-4-alexghiti@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 54a519e commit 6b56beb

File tree

5 files changed

+183
-105
lines changed

5 files changed

+183
-105
lines changed

arch/arm64/include/asm/efi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,6 @@ static inline void efi_capsule_flush_cache_range(void *addr, int size)
168168

169169
efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f);
170170

171+
void efi_icache_sync(unsigned long start, unsigned long end);
172+
171173
#endif /* _ASM_EFI_H */

drivers/firmware/efi/libstub/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \
8686
screen_info.o efi-stub-entry.o
8787

8888
lib-$(CONFIG_ARM) += arm32-stub.o
89-
lib-$(CONFIG_ARM64) += arm64.o arm64-stub.o smbios.o
89+
lib-$(CONFIG_ARM64) += kaslr.o arm64.o arm64-stub.o smbios.o
9090
lib-$(CONFIG_X86) += x86-stub.o
9191
lib-$(CONFIG_RISCV) += riscv.o riscv-stub.o
9292
lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o

drivers/firmware/efi/libstub/arm64-stub.c

Lines changed: 13 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,6 @@
1414

1515
#include "efistub.h"
1616

17-
/*
18-
* Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
19-
* to provide space, and fail to zero it). Check for this condition by double
20-
* checking that the first and the last byte of the image are covered by the
21-
* same EFI memory map entry.
22-
*/
23-
static bool check_image_region(u64 base, u64 size)
24-
{
25-
struct efi_boot_memmap *map;
26-
efi_status_t status;
27-
bool ret = false;
28-
int map_offset;
29-
30-
status = efi_get_memory_map(&map, false);
31-
if (status != EFI_SUCCESS)
32-
return false;
33-
34-
for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
35-
efi_memory_desc_t *md = (void *)map->map + map_offset;
36-
u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
37-
38-
/*
39-
* Find the region that covers base, and return whether
40-
* it covers base+size bytes.
41-
*/
42-
if (base >= md->phys_addr && base < end) {
43-
ret = (base + size) <= end;
44-
break;
45-
}
46-
}
47-
48-
efi_bs_call(free_pool, map);
49-
50-
return ret;
51-
}
52-
5317
efi_status_t handle_kernel_image(unsigned long *image_addr,
5418
unsigned long *image_size,
5519
unsigned long *reserve_addr,
@@ -59,31 +23,6 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
5923
{
6024
efi_status_t status;
6125
unsigned long kernel_size, kernel_codesize, kernel_memsize;
62-
u32 phys_seed = 0;
63-
u64 min_kimg_align = efi_get_kimg_min_align();
64-
65-
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
66-
efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
67-
void *p;
68-
69-
if (efi_nokaslr) {
70-
efi_info("KASLR disabled on kernel command line\n");
71-
} else if (efi_bs_call(handle_protocol, image_handle,
72-
&li_fixed_proto, &p) == EFI_SUCCESS) {
73-
efi_info("Image placement fixed by loader\n");
74-
} else {
75-
status = efi_get_random_bytes(sizeof(phys_seed),
76-
(u8 *)&phys_seed);
77-
if (status == EFI_NOT_FOUND) {
78-
efi_info("EFI_RNG_PROTOCOL unavailable\n");
79-
efi_nokaslr = true;
80-
} else if (status != EFI_SUCCESS) {
81-
efi_err("efi_get_random_bytes() failed (0x%lx)\n",
82-
status);
83-
efi_nokaslr = true;
84-
}
85-
}
86-
}
8726

8827
if (image->image_base != _text) {
8928
efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
@@ -98,50 +37,15 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
9837
kernel_codesize = __inittext_end - _text;
9938
kernel_memsize = kernel_size + (_end - _edata);
10039
*reserve_size = kernel_memsize;
40+
*image_addr = (unsigned long)_text;
10141

102-
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
103-
/*
104-
* If KASLR is enabled, and we have some randomness available,
105-
* locate the kernel at a randomized offset in physical memory.
106-
*/
107-
status = efi_random_alloc(*reserve_size, min_kimg_align,
108-
reserve_addr, phys_seed,
109-
EFI_LOADER_CODE);
110-
if (status != EFI_SUCCESS)
111-
efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
112-
} else {
113-
status = EFI_OUT_OF_RESOURCES;
114-
}
115-
116-
if (status != EFI_SUCCESS) {
117-
if (!check_image_region((u64)_text, kernel_memsize)) {
118-
efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
119-
} else if (IS_ALIGNED((u64)_text, min_kimg_align) &&
120-
(u64)_end < EFI_ALLOC_LIMIT) {
121-
/*
122-
* Just execute from wherever we were loaded by the
123-
* UEFI PE/COFF loader if the placement is suitable.
124-
*/
125-
*image_addr = (u64)_text;
126-
*reserve_size = 0;
127-
return EFI_SUCCESS;
128-
}
129-
130-
status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
131-
ULONG_MAX, min_kimg_align,
132-
EFI_LOADER_CODE);
133-
134-
if (status != EFI_SUCCESS) {
135-
efi_err("Failed to relocate kernel\n");
136-
*reserve_size = 0;
137-
return status;
138-
}
139-
}
140-
141-
*image_addr = *reserve_addr;
142-
memcpy((void *)*image_addr, _text, kernel_size);
143-
caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize);
144-
efi_remap_image(*image_addr, *reserve_size, kernel_codesize);
42+
status = efi_kaslr_relocate_kernel(image_addr,
43+
reserve_addr, reserve_size,
44+
kernel_size, kernel_codesize,
45+
kernel_memsize,
46+
efi_kaslr_get_phys_seed(image_handle));
47+
if (status != EFI_SUCCESS)
48+
return status;
14549

14650
return EFI_SUCCESS;
14751
}
@@ -159,3 +63,8 @@ unsigned long primary_entry_offset(void)
15963
*/
16064
return (char *)primary_entry - _text;
16165
}
66+
67+
void efi_icache_sync(unsigned long start, unsigned long end)
68+
{
69+
caches_clean_inval_pou(start, end);
70+
}

drivers/firmware/efi/libstub/efistub.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,14 @@ const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,
11321132

11331133
void efi_remap_image(unsigned long image_base, unsigned alloc_size,
11341134
unsigned long code_size);
1135+
efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
1136+
unsigned long *reserve_addr,
1137+
unsigned long *reserve_size,
1138+
unsigned long kernel_size,
1139+
unsigned long kernel_codesize,
1140+
unsigned long kernel_memsize,
1141+
u32 phys_seed);
1142+
u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle);
11351143

11361144
asmlinkage efi_status_t __efiapi
11371145
efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab);

drivers/firmware/efi/libstub/kaslr.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
efi_status_t status;
22+
u32 phys_seed;
23+
efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
24+
void *p;
25+
26+
if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
27+
return 0;
28+
29+
if (efi_nokaslr) {
30+
efi_info("KASLR disabled on kernel command line\n");
31+
} else if (efi_bs_call(handle_protocol, image_handle,
32+
&li_fixed_proto, &p) == EFI_SUCCESS) {
33+
efi_info("Image placement fixed by loader\n");
34+
} else {
35+
status = efi_get_random_bytes(sizeof(phys_seed),
36+
(u8 *)&phys_seed);
37+
if (status == EFI_SUCCESS) {
38+
return phys_seed;
39+
} else if (status == EFI_NOT_FOUND) {
40+
efi_info("EFI_RNG_PROTOCOL unavailable\n");
41+
efi_nokaslr = true;
42+
} else if (status != EFI_SUCCESS) {
43+
efi_err("efi_get_random_bytes() failed (0x%lx)\n",
44+
status);
45+
efi_nokaslr = true;
46+
}
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;
61+
efi_status_t status;
62+
bool ret = false;
63+
int map_offset;
64+
65+
status = efi_get_memory_map(&map, false);
66+
if (status != EFI_SUCCESS)
67+
return false;
68+
69+
for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
70+
efi_memory_desc_t *md = (void *)map->map + map_offset;
71+
u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
72+
73+
/*
74+
* Find the region that covers base, and return whether
75+
* it covers base+size bytes.
76+
*/
77+
if (base >= md->phys_addr && base < end) {
78+
ret = (base + size) <= end;
79+
break;
80+
}
81+
}
82+
83+
efi_bs_call(free_pool, map);
84+
85+
return ret;
86+
}
87+
88+
/**
89+
* efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled)
90+
* @image_addr: Pointer to the current kernel location
91+
* @reserve_addr: Pointer to the relocated kernel location
92+
* @reserve_size: Size of the relocated kernel
93+
* @kernel_size: Size of the text + data
94+
* @kernel_codesize: Size of the text
95+
* @kernel_memsize: Size of the text + data + bss
96+
* @phys_seed: Random seed used for the relocation
97+
*
98+
* If KASLR is not enabled, this function relocates the kernel to a fixed
99+
* address (or leave it as its current location). If KASLR is enabled, the
100+
* kernel physical location is randomized using the seed in parameter.
101+
*
102+
* Return: status code, EFI_SUCCESS if relocation is successful
103+
*/
104+
efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
105+
unsigned long *reserve_addr,
106+
unsigned long *reserve_size,
107+
unsigned long kernel_size,
108+
unsigned long kernel_codesize,
109+
unsigned long kernel_memsize,
110+
u32 phys_seed)
111+
{
112+
efi_status_t status;
113+
u64 min_kimg_align = efi_get_kimg_min_align();
114+
115+
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
116+
/*
117+
* If KASLR is enabled, and we have some randomness available,
118+
* locate the kernel at a randomized offset in physical memory.
119+
*/
120+
status = efi_random_alloc(*reserve_size, min_kimg_align,
121+
reserve_addr, phys_seed,
122+
EFI_LOADER_CODE);
123+
if (status != EFI_SUCCESS)
124+
efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
125+
} else {
126+
status = EFI_OUT_OF_RESOURCES;
127+
}
128+
129+
if (status != EFI_SUCCESS) {
130+
if (!check_image_region(*image_addr, kernel_memsize)) {
131+
efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
132+
} else if (IS_ALIGNED(*image_addr, min_kimg_align) &&
133+
(u64)_end < EFI_ALLOC_LIMIT) {
134+
/*
135+
* Just execute from wherever we were loaded by the
136+
* UEFI PE/COFF loader if the placement is suitable.
137+
*/
138+
*reserve_size = 0;
139+
return EFI_SUCCESS;
140+
}
141+
142+
status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
143+
ULONG_MAX, min_kimg_align,
144+
EFI_LOADER_CODE);
145+
146+
if (status != EFI_SUCCESS) {
147+
efi_err("Failed to relocate kernel\n");
148+
*reserve_size = 0;
149+
return status;
150+
}
151+
}
152+
153+
memcpy((void *)*reserve_addr, (void *)*image_addr, kernel_size);
154+
*image_addr = *reserve_addr;
155+
efi_icache_sync(*image_addr, *image_addr + kernel_codesize);
156+
efi_remap_image(*image_addr, *reserve_size, kernel_codesize);
157+
158+
return status;
159+
}

0 commit comments

Comments
 (0)