RISC-V: Parse DT for Zkr to seed KASLR
v4 cover RISC-V: Parse DT for Zkr to seed KASLR
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.
V1 -> V2:
- Add RISC-V: pi: Force hidden visibility for all symbol references
- Add RISC-V: pi: Add kernel/pi/pi.h
- Rewrite archrandom_early.c to parse DT over checking the csr
V2 -> V3:
- Add RISC-V: lib: Add pi aliases for string functions
- Rewrite isa_string_contains in third patch
Jesse Taube (4):
RISC-V: pi: Force hidden visibility for all symbol references
RISC-V: lib: Add pi aliases for string functions
RISC-V: pi: Add kernel/pi/pi.h
RISC-V: Use Zkr to seed KASLR base address
arch/riscv/kernel/pi/Makefile | 4 +- arch/riscv/kernel/pi/archrandom_early.c | 30 +++++ arch/riscv/kernel/pi/cmdline_early.c | 10 +- arch/riscv/kernel/pi/fdt_early.c | 167 +++++++++++++++++++++++- arch/riscv/kernel/pi/pi.h | 20 +++ arch/riscv/lib/memset.S | 2 + arch/riscv/lib/strcmp.S | 1 + arch/riscv/lib/strncmp.S | 1 + arch/riscv/mm/init.c | 5 +- 9 files changed, 224 insertions(+), 16 deletions(-) create mode 100644 arch/riscv/kernel/pi/archrandom_early.c create mode 100644 arch/riscv/kernel/pi/pi.h
Signature · 1 line
2.45.2
PATCH v4 1/4 RISC-V: pi: Force hidden visibility for all symbol references
Eliminate all GOT entries in the .pi section, by forcing hidden visibility for all symbol references, which informs the compiler that such references will be resolved at link time without the need for allocating GOT entries.
Include linux/hidden.h in Makefile, like arm64, for the hidden visibility attribute.
---
V1 -> V2:
- New patch
V2 -> V3:
- No changes
V3 -> V4:
- No changes
--- arch/riscv/kernel/pi/Makefile | 1 + 1 file changed, 1 insertion(+)
arch/riscv/kernel/pi/Makefile +1 -0
@@ -5,6 +5,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \ -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \ $(call cc-option,-mbranch-protection=none) \ -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \ + -include $(srctree)/include/linux/hidden.h \ -D__DISABLE_EXPORTS -ffreestanding \ -fno-asynchronous-unwind-tables -fno-unwind-tables \ $(call cc-option,-fno-addrsig)
Signature · 1 line
2.45.2
PATCH v4 2/4 RISC-V: lib: Add pi aliases for string functions
In reply to Jesse Taubememset, strcmp, and strncmp are all used in the __pi_ section, add SYM_FUNC_ALIAS for them.
When KASAN is enabled in <asm/string.h> __pi___memset is also needed.
---
V2 -> V3:
- New patch
V3 -> V4:
- Fix duplicate the in commit message
--- arch/riscv/lib/memset.S | 2 ++ arch/riscv/lib/strcmp.S | 1 + arch/riscv/lib/strncmp.S | 1 + 3 files changed, 4 insertions(+)
arch/riscv/lib/memset.S +2 -0
@@ -111,3 +111,5 @@ SYM_FUNC_START(__memset) ret SYM_FUNC_END(__memset) SYM_FUNC_ALIAS_WEAK(memset, __memset) +SYM_FUNC_ALIAS(__pi_memset, __memset) +SYM_FUNC_ALIAS(__pi___memset, __memset)
arch/riscv/lib/strcmp.S +1 -0
@@ -120,3 +120,4 @@ strcmp_zbb: .option pop #endif SYM_FUNC_END(strcmp) +SYM_FUNC_ALIAS(__pi_strcmp, strcmp)
arch/riscv/lib/strncmp.S +1 -0
@@ -136,3 +136,4 @@ strncmp_zbb: .option pop #endif SYM_FUNC_END(strncmp) +SYM_FUNC_ALIAS(__pi_strncmp, strncmp)
Signature · 1 line
2.45.2
PATCH v4 3/4 RISC-V: pi: Add kernel/pi/pi.h
In reply to Jesse TaubeAdd pi.h header for declarations of the kernel/pi prefixed functions and any other related declarations.
---
V1 -> V2:
- New patch
V2 -> V3:
- Spelling
V3 -> V4:
- No changes
--- arch/riscv/kernel/pi/cmdline_early.c | 10 ++-------- arch/riscv/kernel/pi/fdt_early.c | 7 +------ arch/riscv/kernel/pi/pi.h | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 arch/riscv/kernel/pi/pi.h
arch/riscv/kernel/pi/cmdline_early.c +2 -8
@@ -6,15 +6,9 @@#include <asm/pgtable.h> #include <asm/setup.h> -static char early_cmdline[COMMAND_LINE_SIZE ]; +#include "pi.h" -/* -* Declare the functions that are exported (but prefixed) here so that LLVM -* does not complain it lacks the 'static' keyword (which, if added, makes -* LLVM complain because the function is actually unused in this file). -*/ -u64 set_satp_mode_from_cmdline (uintptr_t dtb_pa); -bool set_nokaslr_from_cmdline (uintptr_t dtb_pa); +static char early_cmdline[COMMAND_LINE_SIZE ];static char *get_early_cmdline (uintptr_t dtb_pa) {
arch/riscv/kernel/pi/fdt_early.c +1 -6
@@ -3,12 +3,7 @@#include <linux/init.h> #include <linux/libfdt.h> -/* -* Declare the functions that are exported (but prefixed) here so that LLVM -* does not complain it lacks the 'static' keyword (which, if added, makes -* LLVM complain because the function is actually unused in this file). -*/ -u64 get_kaslr_seed (uintptr_t dtb_pa); +#include "pi.h" u64 get_kaslr_seed (uintptr_t dtb_pa) {
arch/riscv/kernel/pi/pi.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _RISCV_PI_H_ +#define _RISCV_PI_H_ + +#include <linux/types.h> + +/* +* The following functions are exported (but prefixed). Declare them here so +* that LLVM does not complain it lacks the 'static' keyword (which, if +* added, makes LLVM complain because the function is unused). +*/ + +u64 get_kaslr_seed (uintptr_t dtb_pa); +bool set_nokaslr_from_cmdline (uintptr_t dtb_pa); +u64 set_satp_mode_from_cmdline (uintptr_t dtb_pa); + +#endif /* _RISCV_PI_H_ */
Signature · 1 line
2.45.2
PATCH v4 4/4 RISC-V: Use Zkr to seed KASLR base address
In reply to Jesse TaubeParse the device tree for Zkr in the isa string. If Zkr is present, use it to seed the kernel base address.
On an ACPI system, as of this commit, there is no easy way to check if Zkr is present. Blindly running the instruction isn't an option as; we have to be able to trust the firmware.
---
V1 -> V2:
- Almost entire rewrite
V2 -> V3:
- Dont parse iscv,isa-base
- Move fdt_early_match_extension_isa in pi.h under comment
- Only check enabled cpus
- Rename early_isa_str to fdt_early_match_extension_isa
- Rename get_ext_named to early_cpu_isa_ext_available
- Rewrite isa_string_contains
- Update commit description
- Use fdt_stringlist_contains for riscv,isa-extensions
V3 -> V4:
- Add CFLAGS_fdt_early.o += -D__NO_FORTIFY to Makefile
- Remove isdigit
--- arch/riscv/kernel/pi/Makefile | 3 +- arch/riscv/kernel/pi/archrandom_early.c | 30 +++++ arch/riscv/kernel/pi/fdt_early.c | 160 ++++++++++++++++++++++++ arch/riscv/kernel/pi/pi.h | 3 + arch/riscv/mm/init.c | 5 +- 5 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/kernel/pi/archrandom_early.c
arch/riscv/kernel/pi/Makefile +2 -1
@@ -17,6 +17,7 @@ KBUILD_CFLAGS += -mcmodel=medany CFLAGS_cmdline_early.o += -D__NO_FORTIFY CFLAGS_lib-fdt_ro.o += -D__NO_FORTIFY +CFLAGS_fdt_early.o += -D__NO_FORTIFY $(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \ --remove-section=.note.gnu.property \
@@ -33,5 +34,5 @@ $(obj)/string.o: $(srctree)/lib/string.c FORCE $(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE $(call if_changed_rule,cc_o_c) -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 +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 extra-y := $(patsubst %.pi.o,%.o,$(obj-y))
arch/riscv/kernel/pi/archrandom_early.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <asm/csr.h> +#include <linux/processor.h> + +#include "pi.h" + +/* +* To avoid rewriting code include asm/archrandom.h and create macros +* for the functions that won't be included. +*/ +#undef riscv_has_extension_unlikely +#define riscv_has_extension_likely (...) false +#undef pr_err_once +#define pr_err_once (...) + +#include <asm/archrandom.h> + +u64 get_kaslr_seed_zkr (const uintptr_t dtb_pa) +{ +unsigned long seed =0 ; + +if (!fdt_early_match_extension_isa ((const void *)dtb_pa,"zkr" )) +return 0 ; + +if (!csr_seed_long (&seed)) +return 0 ; + +return seed; +}
arch/riscv/kernel/pi/fdt_early.c +160 -0
169 lines not on this page · raw message
arch/riscv/kernel/pi/pi.h +3 -0
@@ -11,7 +11,10 @@*/ u64 get_kaslr_seed (uintptr_t dtb_pa); +u64 get_kaslr_seed_zkr (const uintptr_t dtb_pa);bool set_nokaslr_from_cmdline (uintptr_t dtb_pa);u64 set_satp_mode_from_cmdline (uintptr_t dtb_pa); +bool fdt_early_match_extension_isa (const void *fdt,const char *ext_name); +#endif /* _RISCV_PI_H_ */
arch/riscv/mm/init.c +4 -1
@@ -1025,6 +1025,7 @@ static void __init pt_ops_set_late(void)#ifdef CONFIG_RANDOMIZE_BASE extern bool __init __pi_set_nokaslr_from_cmdline (uintptr_t dtb_pa);extern u64 __init __pi_get_kaslr_seed (uintptr_t dtb_pa); +extern u64 __init __pi_get_kaslr_seed_zkr (const uintptr_t dtb_pa);static int __init print_nokaslr (char *p) {
@@ -1045,10 +1046,12 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)#ifdef CONFIG_RANDOMIZE_BASE if (!__pi_set_nokaslr_from_cmdline (dtb_pa)) { -u64 kaslr_seed =__pi_get_kaslr_seed (dtb_pa); +u64 kaslr_seed =__pi_get_kaslr_seed_zkr (dtb_pa);u32 kernel_size = (uintptr_t )(&_end) - (uintptr_t )(&_start);u32 nr_pos; +if (kaslr_seed ==0 ) + kaslr_seed =__pi_get_kaslr_seed (dtb_pa);/* * Compute the number of positions available: we are limited * by the early page table that only has one PUD and we must
Signature · 1 line
2.45.2
Re: [PATCH v4 4/4] RISC-V: Use Zkr to seed KASLR base address
Quoted 11 lines · Jesse Taube
Parse the device tree for Zkr in the isa string. If Zkr is present, use it to seed the kernel base address.
On an ACPI system, as of this commit, there is no easy way to check if Zkr is present. Blindly running the instruction isn't an option as; we have to be able to trust the firmware.
Signed-off-by Jesse Taube <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Reviewed-by Alexandre Ghiti <[email protected]>Tested-by Zong Li <[email protected]>
My heads a bit addled from being sick still, but I think you've dealt with the things I had send on the previous version and I didn't spot any obvious problems with the parsing.
Thanks,
Conor.
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:
Hello:
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:
Quoted 14 lines · On Tue, 9 Jul 2024 13:39:33 -0400 you wrote:
Add functions to
pi/fdt_early.cto 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.
V1 -> V2:
- Add RISC-V: pi: Force hidden visibility for all symbol references
- Add RISC-V: pi: Addkernel/pi/pi.h
- Rewritearchrandom_early.cto parse DT over checking the csr
V2 -> V3:
- Add RISC-V: lib: Add pi aliases for string functions
- Rewriteisa_string_containsin third patch
Quoted 1 line
[...]
Here is the summary with links:
- [v4,1/4] RISC-V: pi: Force hidden visibility for all symbol references
https://git.kernel.org/riscv/c/14c3ec67236b - [v4,2/4] RISC-V: lib: Add pi aliases for string functions https://git.kernel.org/riscv/c/d57e19fcbf3f - [v4,3/4] RISC-V: pi: Add kernel/pi/pi.h https://git.kernel.org/riscv/c/b3311827155a - [v4,4/4] RISC-V: Use Zkr to seed KASLR base address https://git.kernel.org/riscv/c/945302df3de1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html