Skip to content

Commit 7c08a26

Browse files
Merge patch series "RISC-V: Parse DT for Zkr to seed KASLR"
Jesse Taube <[email protected]> says: Add functions to pi/fdt_early.c to help parse the FDT to check if the isa string has the Zkr extension. Then use the Zkr extension to seed the KASLR base address. The first two patches fix the visibility of symbols. * b4-shazam-merge: RISC-V: Use Zkr to seed KASLR base address RISC-V: pi: Add kernel/pi/pi.h RISC-V: lib: Add pi aliases for string functions RISC-V: pi: Force hidden visibility for all symbol references Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
2 parents dc1c803 + 945302d commit 7c08a26

File tree

9 files changed

+224
-16
lines changed

9 files changed

+224
-16
lines changed

arch/riscv/kernel/pi/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
55
-Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
66
$(call cc-option,-mbranch-protection=none) \
77
-I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
8+
-include $(srctree)/include/linux/hidden.h \
89
-D__DISABLE_EXPORTS -ffreestanding \
910
-fno-asynchronous-unwind-tables -fno-unwind-tables \
1011
$(call cc-option,-fno-addrsig)
@@ -16,6 +17,7 @@ KBUILD_CFLAGS += -mcmodel=medany
1617

1718
CFLAGS_cmdline_early.o += -D__NO_FORTIFY
1819
CFLAGS_lib-fdt_ro.o += -D__NO_FORTIFY
20+
CFLAGS_fdt_early.o += -D__NO_FORTIFY
1921

2022
$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
2123
--remove-section=.note.gnu.property \
@@ -32,5 +34,5 @@ $(obj)/string.o: $(srctree)/lib/string.c FORCE
3234
$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
3335
$(call if_changed_rule,cc_o_c)
3436

35-
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
37+
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 archrandom_early.pi.o
3638
extra-y := $(patsubst %.pi.o,%.o,$(obj-y))
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+
3+
#include <asm/csr.h>
4+
#include <linux/processor.h>
5+
6+
#include "pi.h"
7+
8+
/*
9+
* To avoid rewriting code include asm/archrandom.h and create macros
10+
* for the functions that won't be included.
11+
*/
12+
#undef riscv_has_extension_unlikely
13+
#define riscv_has_extension_likely(...) false
14+
#undef pr_err_once
15+
#define pr_err_once(...)
16+
17+
#include <asm/archrandom.h>
18+
19+
u64 get_kaslr_seed_zkr(const uintptr_t dtb_pa)
20+
{
21+
unsigned long seed = 0;
22+
23+
if (!fdt_early_match_extension_isa((const void *)dtb_pa, "zkr"))
24+
return 0;
25+
26+
if (!csr_seed_long(&seed))
27+
return 0;
28+
29+
return seed;
30+
}

arch/riscv/kernel/pi/cmdline_early.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
#include <asm/pgtable.h>
77
#include <asm/setup.h>
88

9-
static char early_cmdline[COMMAND_LINE_SIZE];
9+
#include "pi.h"
1010

11-
/*
12-
* Declare the functions that are exported (but prefixed) here so that LLVM
13-
* does not complain it lacks the 'static' keyword (which, if added, makes
14-
* LLVM complain because the function is actually unused in this file).
15-
*/
16-
u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa);
17-
bool set_nokaslr_from_cmdline(uintptr_t dtb_pa);
11+
static char early_cmdline[COMMAND_LINE_SIZE];
1812

1913
static char *get_early_cmdline(uintptr_t dtb_pa)
2014
{

arch/riscv/kernel/pi/fdt_early.c

Lines changed: 161 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
#include <linux/types.h>
33
#include <linux/init.h>
44
#include <linux/libfdt.h>
5+
#include <linux/ctype.h>
56

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);
7+
#include "pi.h"
128

139
u64 get_kaslr_seed(uintptr_t dtb_pa)
1410
{
@@ -28,3 +24,162 @@ u64 get_kaslr_seed(uintptr_t dtb_pa)
2824
*prop = 0;
2925
return ret;
3026
}
27+
28+
/**
29+
* fdt_device_is_available - check if a device is available for use
30+
*
31+
* @fdt: pointer to the device tree blob
32+
* @node: offset of the node whose property to find
33+
*
34+
* Returns true if the status property is absent or set to "okay" or "ok",
35+
* false otherwise
36+
*/
37+
static bool fdt_device_is_available(const void *fdt, int node)
38+
{
39+
const char *status;
40+
int statlen;
41+
42+
status = fdt_getprop(fdt, node, "status", &statlen);
43+
if (!status)
44+
return true;
45+
46+
if (statlen > 0) {
47+
if (!strcmp(status, "okay") || !strcmp(status, "ok"))
48+
return true;
49+
}
50+
51+
return false;
52+
}
53+
54+
/* Copy of fdt_nodename_eq_ */
55+
static int fdt_node_name_eq(const void *fdt, int offset,
56+
const char *s)
57+
{
58+
int olen;
59+
int len = strlen(s);
60+
const char *p = fdt_get_name(fdt, offset, &olen);
61+
62+
if (!p || olen < len)
63+
/* short match */
64+
return 0;
65+
66+
if (memcmp(p, s, len) != 0)
67+
return 0;
68+
69+
if (p[len] == '\0')
70+
return 1;
71+
else if (!memchr(s, '@', len) && (p[len] == '@'))
72+
return 1;
73+
else
74+
return 0;
75+
}
76+
77+
/**
78+
* isa_string_contains - check if isa string contains an extension
79+
*
80+
* @isa_str: isa string to search
81+
* @ext_name: the extension to search for
82+
*
83+
* Returns true if the extension is in the given isa string,
84+
* false otherwise
85+
*/
86+
static bool isa_string_contains(const char *isa_str, const char *ext_name)
87+
{
88+
size_t i, single_end, len = strlen(ext_name);
89+
char ext_end;
90+
91+
/* Error must contain rv32/64 */
92+
if (strlen(isa_str) < 4)
93+
return false;
94+
95+
if (len == 1) {
96+
single_end = strcspn(isa_str, "sSxXzZ");
97+
/* Search for single chars between rv32/64 and multi-letter extensions */
98+
for (i = 4; i < single_end; i++) {
99+
if (tolower(isa_str[i]) == ext_name[0])
100+
return true;
101+
}
102+
return false;
103+
}
104+
105+
/* Skip to start of multi-letter extensions */
106+
isa_str = strpbrk(isa_str, "sSxXzZ");
107+
while (isa_str) {
108+
if (strncasecmp(isa_str, ext_name, len) == 0) {
109+
ext_end = isa_str[len];
110+
/* Check if matches the whole extension. */
111+
if (ext_end == '\0' || ext_end == '_')
112+
return true;
113+
}
114+
/* Multi-letter extensions must be split from other multi-letter
115+
* extensions with an "_", the end of a multi-letter extension will
116+
* either be the null character or the "_" at the start of the next
117+
* multi-letter extension.
118+
*/
119+
isa_str = strchr(isa_str, '_');
120+
if (isa_str)
121+
isa_str++;
122+
}
123+
124+
return false;
125+
}
126+
127+
/**
128+
* early_cpu_isa_ext_available - check if cpu node has an extension
129+
*
130+
* @fdt: pointer to the device tree blob
131+
* @node: offset of the cpu node
132+
* @ext_name: the extension to search for
133+
*
134+
* Returns true if the cpu node has the extension,
135+
* false otherwise
136+
*/
137+
static bool early_cpu_isa_ext_available(const void *fdt, int node, const char *ext_name)
138+
{
139+
const void *prop;
140+
int len;
141+
142+
prop = fdt_getprop(fdt, node, "riscv,isa-extensions", &len);
143+
if (prop && fdt_stringlist_contains(prop, len, ext_name))
144+
return true;
145+
146+
prop = fdt_getprop(fdt, node, "riscv,isa", &len);
147+
if (prop && isa_string_contains(prop, ext_name))
148+
return true;
149+
150+
return false;
151+
}
152+
153+
/**
154+
* fdt_early_match_extension_isa - check if all cpu nodes have an extension
155+
*
156+
* @fdt: pointer to the device tree blob
157+
* @ext_name: the extension to search for
158+
*
159+
* Returns true if the all available the cpu nodes have the extension,
160+
* false otherwise
161+
*/
162+
bool fdt_early_match_extension_isa(const void *fdt, const char *ext_name)
163+
{
164+
int node, parent;
165+
bool ret = false;
166+
167+
parent = fdt_path_offset(fdt, "/cpus");
168+
if (parent < 0)
169+
return false;
170+
171+
fdt_for_each_subnode(node, fdt, parent) {
172+
if (!fdt_node_name_eq(fdt, node, "cpu"))
173+
continue;
174+
175+
if (!fdt_device_is_available(fdt, node))
176+
continue;
177+
178+
if (!early_cpu_isa_ext_available(fdt, node, ext_name))
179+
return false;
180+
181+
ret = true;
182+
}
183+
184+
return ret;
185+
}

arch/riscv/kernel/pi/pi.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _RISCV_PI_H_
3+
#define _RISCV_PI_H_
4+
5+
#include <linux/types.h>
6+
7+
/*
8+
* The following functions are exported (but prefixed). Declare them here so
9+
* that LLVM does not complain it lacks the 'static' keyword (which, if
10+
* added, makes LLVM complain because the function is unused).
11+
*/
12+
13+
u64 get_kaslr_seed(uintptr_t dtb_pa);
14+
u64 get_kaslr_seed_zkr(const uintptr_t dtb_pa);
15+
bool set_nokaslr_from_cmdline(uintptr_t dtb_pa);
16+
u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa);
17+
18+
bool fdt_early_match_extension_isa(const void *fdt, const char *ext_name);
19+
20+
#endif /* _RISCV_PI_H_ */

arch/riscv/lib/memset.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,5 @@ SYM_FUNC_START(__memset)
111111
ret
112112
SYM_FUNC_END(__memset)
113113
SYM_FUNC_ALIAS_WEAK(memset, __memset)
114+
SYM_FUNC_ALIAS(__pi_memset, __memset)
115+
SYM_FUNC_ALIAS(__pi___memset, __memset)

arch/riscv/lib/strcmp.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@ strcmp_zbb:
120120
.option pop
121121
#endif
122122
SYM_FUNC_END(strcmp)
123+
SYM_FUNC_ALIAS(__pi_strcmp, strcmp)

arch/riscv/lib/strncmp.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,4 @@ strncmp_zbb:
136136
.option pop
137137
#endif
138138
SYM_FUNC_END(strncmp)
139+
SYM_FUNC_ALIAS(__pi_strncmp, strncmp)

arch/riscv/mm/init.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ static void __init pt_ops_set_late(void)
10391039
#ifdef CONFIG_RANDOMIZE_BASE
10401040
extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa);
10411041
extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa);
1042+
extern u64 __init __pi_get_kaslr_seed_zkr(const uintptr_t dtb_pa);
10421043

10431044
static int __init print_nokaslr(char *p)
10441045
{
@@ -1059,10 +1060,12 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
10591060

10601061
#ifdef CONFIG_RANDOMIZE_BASE
10611062
if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) {
1062-
u64 kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
1063+
u64 kaslr_seed = __pi_get_kaslr_seed_zkr(dtb_pa);
10631064
u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
10641065
u32 nr_pos;
10651066

1067+
if (kaslr_seed == 0)
1068+
kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
10661069
/*
10671070
* Compute the number of positions available: we are limited
10681071
* by the early page table that only has one PUD and we must

0 commit comments

Comments
 (0)