Skip to content

Commit 37f57bd

Browse files
cwshuPaul Walmsley
authored andcommitted
arch/riscv: compile vdso with landing pad and shadow stack note
User mode tasks compiled with Zicfilp may call indirectly into the vdso (like hwprobe indirect calls). Add support for compiling landing pads into the vdso. Landing pad instructions in the vdso will be no-ops for tasks which have not enabled landing pads. Furthermore, add support for the C sources of the vdso to be compiled with shadow stack and landing pads enabled as well. Landing pad and shadow stack instructions are emitted only when the VDSO_CFI cflags option is defined during compile. Signed-off-by: Jim Shu <jim.shu@sifive.com> Reviewed-by: Zong Li <zong.li@sifive.com> Signed-off-by: Deepak Gupta <debug@rivosinc.com> Tested-by: Andreas Korb <andreas.korb@aisec.fraunhofer.de> # QEMU, custom CVA6 Tested-by: Valentin Haudiquet <valentin.haudiquet@canonical.com> Link: https://patch.msgid.link/20251112-v5_user_cfi_series-v23-23-b55691eacf4f@rivosinc.com [pjw@kernel.org: cleaned up patch description, issues reported by checkpatch] Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent 41213bf commit 37f57bd

9 files changed

Lines changed: 81 additions & 3 deletions

File tree

arch/riscv/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) := $(riscv-march-y)_zacas
8181
# Check if the toolchain supports Zabha
8282
riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZABHA) := $(riscv-march-y)_zabha
8383

84+
KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/')
85+
export KBUILD_BASE_ISA
86+
8487
# Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by
8588
# matching non-v and non-multi-letter extensions out with the filter ([^v_]*)
86-
KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/')
89+
KBUILD_CFLAGS += $(KBUILD_BASE_ISA)
8790

8891
KBUILD_AFLAGS += -march=$(riscv-march-y)
8992

arch/riscv/include/asm/assembler.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,47 @@
8080
.endm
8181

8282
#endif /* __ASM_ASSEMBLER_H */
83+
84+
#if defined(VDSO_CFI) && (__riscv_xlen == 64)
85+
.macro vdso_lpad, label = 0
86+
lpad \label
87+
.endm
88+
#else
89+
.macro vdso_lpad, label = 0
90+
.endm
91+
#endif
92+
93+
/*
94+
* This macro emits a program property note section identifying
95+
* architecture features which require special handling, mainly for
96+
* use in assembly files included in the VDSO.
97+
*/
98+
#define NT_GNU_PROPERTY_TYPE_0 5
99+
#define GNU_PROPERTY_RISCV_FEATURE_1_AND 0xc0000000
100+
101+
#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP BIT(0)
102+
#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS BIT(1)
103+
104+
#if defined(VDSO_CFI) && (__riscv_xlen == 64)
105+
#define GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT \
106+
(GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP | GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS)
107+
#endif
108+
109+
#ifdef GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT
110+
.macro emit_riscv_feature_1_and, feat = GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT
111+
.pushsection .note.gnu.property, "a"
112+
.p2align 3
113+
.word 4
114+
.word 16
115+
.word NT_GNU_PROPERTY_TYPE_0
116+
.asciz "GNU"
117+
.word GNU_PROPERTY_RISCV_FEATURE_1_AND
118+
.word 4
119+
.word \feat
120+
.word 0
121+
.popsection
122+
.endm
123+
#else
124+
.macro emit_riscv_feature_1_and, feat = 0
125+
.endm
126+
#endif

arch/riscv/kernel/vdso/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ ifdef CONFIG_VDSO_GETRANDOM
1717
vdso-syms += getrandom
1818
endif
1919

20+
ifdef VDSO_CFI_BUILD
21+
CFI_MARCH = _zicfilp_zicfiss
22+
CFI_FULL = -fcf-protection=full
23+
endif
24+
2025
# Files to link into the vdso
2126
obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
2227

@@ -27,6 +32,10 @@ endif
2732
ccflags-y := -fno-stack-protector
2833
ccflags-y += -DDISABLE_BRANCH_PROFILING
2934
ccflags-y += -fno-builtin
35+
ccflags-y += $(KBUILD_BASE_ISA)$(CFI_MARCH)
36+
ccflags-y += $(CFI_FULL)
37+
asflags-y += $(KBUILD_BASE_ISA)$(CFI_MARCH)
38+
asflags-y += $(CFI_FULL)
3039

3140
ifneq ($(c-gettimeofday-y),)
3241
CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y)
@@ -79,7 +88,7 @@ include/generated/vdso-offsets.h: $(obj)/vdso.so.dbg FORCE
7988
# The DSO images are built using a special linker script
8089
# Make sure only to export the intended __vdso_xxx symbol offsets.
8190
quiet_cmd_vdsold_and_check = VDSOLD $@
82-
cmd_vdsold_and_check = $(LD) $(ld_flags) -T $(filter-out FORCE,$^) -o $@.tmp && \
91+
cmd_vdsold_and_check = $(LD) $(CFI_FULL) $(ld_flags) -T $(filter-out FORCE,$^) -o $@.tmp && \
8392
$(OBJCOPY) $(patsubst %, -G __vdso_%, $(vdso-syms)) $@.tmp $@ && \
8493
rm $@.tmp && \
8594
$(cmd_vdso_check)

arch/riscv/kernel/vdso/flush_icache.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
#include <linux/linkage.h>
77
#include <asm/unistd.h>
8+
#include <asm/assembler.h>
89

910
.text
1011
/* int __vdso_flush_icache(void *start, void *end, unsigned long flags); */
1112
SYM_FUNC_START(__vdso_flush_icache)
1213
.cfi_startproc
14+
vdso_lpad
1315
#ifdef CONFIG_SMP
1416
li a7, __NR_riscv_flush_icache
1517
ecall
@@ -20,3 +22,5 @@ SYM_FUNC_START(__vdso_flush_icache)
2022
ret
2123
.cfi_endproc
2224
SYM_FUNC_END(__vdso_flush_icache)
25+
26+
emit_riscv_feature_1_and

arch/riscv/kernel/vdso/getcpu.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55

66
#include <linux/linkage.h>
77
#include <asm/unistd.h>
8+
#include <asm/assembler.h>
89

910
.text
1011
/* int __vdso_getcpu(unsigned *cpu, unsigned *node, void *unused); */
1112
SYM_FUNC_START(__vdso_getcpu)
1213
.cfi_startproc
14+
vdso_lpad
1315
/* For now, just do the syscall. */
1416
li a7, __NR_getcpu
1517
ecall
1618
ret
1719
.cfi_endproc
1820
SYM_FUNC_END(__vdso_getcpu)
21+
22+
emit_riscv_feature_1_and

arch/riscv/kernel/vdso/note.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
#include <linux/elfnote.h>
88
#include <linux/version.h>
9+
#include <asm/assembler.h>
910

1011
ELFNOTE_START(Linux, 0, "a")
1112
.long LINUX_VERSION_CODE
1213
ELFNOTE_END
14+
15+
emit_riscv_feature_1_and

arch/riscv/kernel/vdso/rt_sigreturn.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
#include <linux/linkage.h>
77
#include <asm/unistd.h>
8+
#include <asm/assembler.h>
89

910
.text
1011
SYM_FUNC_START(__vdso_rt_sigreturn)
1112
.cfi_startproc
1213
.cfi_signal_frame
14+
vdso_lpad
1315
li a7, __NR_rt_sigreturn
1416
ecall
1517
.cfi_endproc
1618
SYM_FUNC_END(__vdso_rt_sigreturn)
19+
20+
emit_riscv_feature_1_and

arch/riscv/kernel/vdso/sys_hwprobe.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
#include <linux/linkage.h>
55
#include <asm/unistd.h>
6+
#include <asm/assembler.h>
67

78
.text
89
SYM_FUNC_START(riscv_hwprobe)
910
.cfi_startproc
11+
vdso_lpad
1012
li a7, __NR_riscv_hwprobe
1113
ecall
1214
ret
1315

1416
.cfi_endproc
1517
SYM_FUNC_END(riscv_hwprobe)
18+
19+
emit_riscv_feature_1_and

arch/riscv/kernel/vdso/vgetrandom-chacha.S

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <asm/asm.h>
99
#include <linux/linkage.h>
10+
#include <asm/assembler.h>
1011

1112
.text
1213

@@ -74,7 +75,7 @@ SYM_FUNC_START(__arch_chacha20_blocks_nostack)
7475
#define _20 20, 20, 20, 20
7576
#define _24 24, 24, 24, 24
7677
#define _25 25, 25, 25, 25
77-
78+
vdso_lpad
7879
/*
7980
* The ABI requires s0-s9 saved.
8081
* This does not violate the stack-less requirement: no sensitive data
@@ -247,3 +248,5 @@ SYM_FUNC_START(__arch_chacha20_blocks_nostack)
247248

248249
ret
249250
SYM_FUNC_END(__arch_chacha20_blocks_nostack)
251+
252+
emit_riscv_feature_1_and

0 commit comments

Comments
 (0)