Skip to content

Commit 84fe419

Browse files
Alexandre Ghitipalmer-dabbelt
authored andcommitted
riscv: Introduce virtual kernel mapping KASLR
KASLR implementation relies on a relocatable kernel so that we can move the kernel mapping. The seed needed to virtually move the kernel is taken from the device tree, so we rely on the bootloader to provide a correct seed. Zkr could be used unconditionnally instead if implemented, but that's for another patch. Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> 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-2-alexghiti@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 06c2afb commit 84fe419

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

arch/riscv/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,25 @@ config RELOCATABLE
719719

720720
If unsure, say N.
721721

722+
config RANDOMIZE_BASE
723+
bool "Randomize the address of the kernel image"
724+
select RELOCATABLE
725+
depends on MMU && 64BIT && !XIP_KERNEL
726+
help
727+
Randomizes the virtual address at which the kernel image is
728+
loaded, as a security feature that deters exploit attempts
729+
relying on knowledge of the location of kernel internals.
730+
731+
It is the bootloader's job to provide entropy, by passing a
732+
random u64 value in /chosen/kaslr-seed at kernel entry.
733+
734+
When booting via the UEFI stub, it will invoke the firmware's
735+
EFI_RNG_PROTOCOL implementation (if available) to supply entropy
736+
to the kernel proper. In addition, it will randomise the physical
737+
location of the kernel Image as well.
738+
739+
If unsure, say N.
740+
722741
endmenu # "Kernel features"
723742

724743
menu "Boot options"

arch/riscv/include/asm/page.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ typedef struct page *pgtable_t;
106106
struct kernel_mapping {
107107
unsigned long page_offset;
108108
unsigned long virt_addr;
109+
unsigned long virt_offset;
109110
uintptr_t phys_addr;
110111
uintptr_t size;
111112
/* Offset between linear mapping virtual address and kernel load address */
@@ -185,6 +186,8 @@ extern phys_addr_t __phys_addr_symbol(unsigned long x);
185186

186187
#define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x))
187188

189+
unsigned long kaslr_offset(void);
190+
188191
#endif /* __ASSEMBLY__ */
189192

190193
#define virt_addr_valid(vaddr) ({ \

arch/riscv/kernel/pi/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ $(obj)/string.o: $(srctree)/lib/string.c FORCE
3535
$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
3636
$(call if_changed_rule,cc_o_c)
3737

38-
obj-y := cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
38+
obj-y := cmdline_early.pi.o fdt_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
3939
extra-y := $(patsubst %.pi.o,%.o,$(obj-y))

arch/riscv/kernel/pi/cmdline_early.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static char early_cmdline[COMMAND_LINE_SIZE];
1414
* LLVM complain because the function is actually unused in this file).
1515
*/
1616
u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa);
17+
bool set_nokaslr_from_cmdline(uintptr_t dtb_pa);
1718

1819
static char *get_early_cmdline(uintptr_t dtb_pa)
1920
{
@@ -60,3 +61,15 @@ u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa)
6061

6162
return match_noXlvl(cmdline);
6263
}
64+
65+
static bool match_nokaslr(char *cmdline)
66+
{
67+
return strstr(cmdline, "nokaslr");
68+
}
69+
70+
bool set_nokaslr_from_cmdline(uintptr_t dtb_pa)
71+
{
72+
char *cmdline = get_early_cmdline(dtb_pa);
73+
74+
return match_nokaslr(cmdline);
75+
}

arch/riscv/kernel/pi/fdt_early.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <linux/types.h>
3+
#include <linux/init.h>
4+
#include <linux/libfdt.h>
5+
6+
/*
7+
* Declare the functions that are exported (but prefixed) here so that LLVM
8+
* does not complain it lacks the 'static' keyword (which, if added, makes
9+
* LLVM complain because the function is actually unused in this file).
10+
*/
11+
u64 get_kaslr_seed(uintptr_t dtb_pa);
12+
13+
u64 get_kaslr_seed(uintptr_t dtb_pa)
14+
{
15+
int node, len;
16+
fdt64_t *prop;
17+
u64 ret;
18+
19+
node = fdt_path_offset((void *)dtb_pa, "/chosen");
20+
if (node < 0)
21+
return 0;
22+
23+
prop = fdt_getprop_w((void *)dtb_pa, node, "kaslr-seed", &len);
24+
if (!prop || len != sizeof(u64))
25+
return 0;
26+
27+
ret = fdt64_to_cpu(*prop);
28+
*prop = 0;
29+
return ret;
30+
}

arch/riscv/mm/init.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,11 +1012,45 @@ static void __init pt_ops_set_late(void)
10121012
#endif
10131013
}
10141014

1015+
#ifdef CONFIG_RANDOMIZE_BASE
1016+
extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa);
1017+
extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa);
1018+
1019+
static int __init print_nokaslr(char *p)
1020+
{
1021+
pr_info("Disabled KASLR");
1022+
return 0;
1023+
}
1024+
early_param("nokaslr", print_nokaslr);
1025+
1026+
unsigned long kaslr_offset(void)
1027+
{
1028+
return kernel_map.virt_offset;
1029+
}
1030+
#endif
1031+
10151032
asmlinkage void __init setup_vm(uintptr_t dtb_pa)
10161033
{
10171034
pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
10181035

1019-
kernel_map.virt_addr = KERNEL_LINK_ADDR;
1036+
#ifdef CONFIG_RANDOMIZE_BASE
1037+
if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) {
1038+
u64 kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
1039+
u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
1040+
u32 nr_pos;
1041+
1042+
/*
1043+
* Compute the number of positions available: we are limited
1044+
* by the early page table that only has one PUD and we must
1045+
* be aligned on PMD_SIZE.
1046+
*/
1047+
nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE;
1048+
1049+
kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE;
1050+
}
1051+
#endif
1052+
1053+
kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset;
10201054
kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
10211055

10221056
#ifdef CONFIG_XIP_KERNEL

0 commit comments

Comments
 (0)