Skip to content

Commit 245561b

Browse files
samitolvanenpalmer-dabbelt
authored andcommitted
lkdtm: Fix CFI_BACKWARD on RISC-V
On RISC-V, the return address is before the current frame pointer, unlike on most other architectures. Use the correct offset on RISC-V to fix the CFI_BACKWARD test. Signed-off-by: Sami Tolvanen <samitolvanen@google.com> Acked-by: Kees Cook <keescook@chromium.org> Tested-by: Nathan Chancellor <nathan@kernel.org> Link: https://lore.kernel.org/r/20230927224757.1154247-14-samitolvanen@google.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent c40fef8 commit 245561b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

drivers/misc/lkdtm/cfi.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,20 @@ static void lkdtm_CFI_FORWARD_PROTO(void)
6868
#define no_pac_addr(addr) \
6969
((__force __typeof__(addr))((uintptr_t)(addr) | PAGE_OFFSET))
7070

71+
#ifdef CONFIG_RISCV
72+
/* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#frame-pointer-convention */
73+
#define FRAME_RA_OFFSET (-1)
74+
#else
75+
#define FRAME_RA_OFFSET 1
76+
#endif
77+
7178
/* The ultimate ROP gadget. */
7279
static noinline __no_ret_protection
7380
void set_return_addr_unchecked(unsigned long *expected, unsigned long *addr)
7481
{
7582
/* Use of volatile is to make sure final write isn't seen as a dead store. */
76-
unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
83+
unsigned long * volatile *ret_addr =
84+
(unsigned long **)__builtin_frame_address(0) + FRAME_RA_OFFSET;
7785

7886
/* Make sure we've found the right place on the stack before writing it. */
7987
if (no_pac_addr(*ret_addr) == expected)
@@ -88,7 +96,8 @@ static noinline
8896
void set_return_addr(unsigned long *expected, unsigned long *addr)
8997
{
9098
/* Use of volatile is to make sure final write isn't seen as a dead store. */
91-
unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
99+
unsigned long * volatile *ret_addr =
100+
(unsigned long **)__builtin_frame_address(0) + FRAME_RA_OFFSET;
92101

93102
/* Make sure we've found the right place on the stack before writing it. */
94103
if (no_pac_addr(*ret_addr) == expected)

0 commit comments

Comments
 (0)