forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit f0bddf5 ("riscv: entry: Convert to generic entry") moved syscall handling to C code, which exposed function pointer type mismatches that trip fine-grained forward-edge Control-Flow Integrity (CFI) checks as syscall handlers are all called through the same syscall_t pointer type. To fix the type mismatches, implement pt_regs based syscall wrappers similarly to x86 and arm64. This patch is based on arm64 syscall wrappers added in commit 4378a7d ("arm64: implement syscall wrappers"), where the main goal was to minimize the risk of userspace-controlled values being used under speculation. This may be a concern for riscv in future as well. Following other architectures, the syscall wrappers generate three functions for each syscall; __riscv_<compat_>sys_<name> takes a pt_regs pointer and extracts arguments from registers, __se_<compat_>sys_<name> is a sign-extension wrapper that casts the long arguments to the correct types for the real syscall implementation, which is named __do_<compat_>sys_<name>. Reviewed-by: Kees Cook <keescook@chromium.org> Tested-by: Nathan Chancellor <nathan@kernel.org> Signed-off-by: Sami Tolvanen <samitolvanen@google.com> Link: https://lore.kernel.org/r/20230710183544.999540-9-samitolvanen@google.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
- Loading branch information
1 parent
06c2afb
commit 08d0ce3
Showing
6 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* | ||
* syscall_wrapper.h - riscv specific wrappers to syscall definitions | ||
* | ||
* Based on arch/arm64/include/syscall_wrapper.h | ||
*/ | ||
|
||
#ifndef __ASM_SYSCALL_WRAPPER_H | ||
#define __ASM_SYSCALL_WRAPPER_H | ||
|
||
#include <asm/ptrace.h> | ||
|
||
asmlinkage long __riscv_sys_ni_syscall(const struct pt_regs *); | ||
|
||
#define SC_RISCV_REGS_TO_ARGS(x, ...) \ | ||
__MAP(x,__SC_ARGS \ | ||
,,regs->orig_a0,,regs->a1,,regs->a2 \ | ||
,,regs->a3,,regs->a4,,regs->a5,,regs->a6) | ||
|
||
#ifdef CONFIG_COMPAT | ||
|
||
#define COMPAT_SYSCALL_DEFINEx(x, name, ...) \ | ||
asmlinkage long __riscv_compat_sys##name(const struct pt_regs *regs); \ | ||
ALLOW_ERROR_INJECTION(__riscv_compat_sys##name, ERRNO); \ | ||
static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ | ||
static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)); \ | ||
asmlinkage long __riscv_compat_sys##name(const struct pt_regs *regs) \ | ||
{ \ | ||
return __se_compat_sys##name(SC_RISCV_REGS_TO_ARGS(x,__VA_ARGS__)); \ | ||
} \ | ||
static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \ | ||
{ \ | ||
return __do_compat_sys##name(__MAP(x,__SC_DELOUSE,__VA_ARGS__)); \ | ||
} \ | ||
static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) | ||
|
||
#define COMPAT_SYSCALL_DEFINE0(sname) \ | ||
asmlinkage long __riscv_compat_sys_##sname(const struct pt_regs *__unused); \ | ||
ALLOW_ERROR_INJECTION(__riscv_compat_sys_##sname, ERRNO); \ | ||
asmlinkage long __riscv_compat_sys_##sname(const struct pt_regs *__unused) | ||
|
||
#define COND_SYSCALL_COMPAT(name) \ | ||
asmlinkage long __weak __riscv_compat_sys_##name(const struct pt_regs *regs); \ | ||
asmlinkage long __weak __riscv_compat_sys_##name(const struct pt_regs *regs) \ | ||
{ \ | ||
return sys_ni_syscall(); \ | ||
} | ||
|
||
#define COMPAT_SYS_NI(name) \ | ||
SYSCALL_ALIAS(__riscv_compat_sys_##name, sys_ni_posix_timers); | ||
|
||
#endif /* CONFIG_COMPAT */ | ||
|
||
#define __SYSCALL_DEFINEx(x, name, ...) \ | ||
asmlinkage long __riscv_sys##name(const struct pt_regs *regs); \ | ||
ALLOW_ERROR_INJECTION(__riscv_sys##name, ERRNO); \ | ||
static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ | ||
static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)); \ | ||
asmlinkage long __riscv_sys##name(const struct pt_regs *regs) \ | ||
{ \ | ||
return __se_sys##name(SC_RISCV_REGS_TO_ARGS(x,__VA_ARGS__)); \ | ||
} \ | ||
static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \ | ||
{ \ | ||
long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__)); \ | ||
__MAP(x,__SC_TEST,__VA_ARGS__); \ | ||
__PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \ | ||
return ret; \ | ||
} \ | ||
static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) | ||
|
||
#define SYSCALL_DEFINE0(sname) \ | ||
SYSCALL_METADATA(_##sname, 0); \ | ||
asmlinkage long __riscv_sys_##sname(const struct pt_regs *__unused); \ | ||
ALLOW_ERROR_INJECTION(__riscv_sys_##sname, ERRNO); \ | ||
asmlinkage long __riscv_sys_##sname(const struct pt_regs *__unused) | ||
|
||
#define COND_SYSCALL(name) \ | ||
asmlinkage long __weak __riscv_sys_##name(const struct pt_regs *regs); \ | ||
asmlinkage long __weak __riscv_sys_##name(const struct pt_regs *regs) \ | ||
{ \ | ||
return sys_ni_syscall(); \ | ||
} | ||
|
||
#define SYS_NI(name) SYSCALL_ALIAS(__riscv_sys_##name, sys_ni_posix_timers); | ||
|
||
#endif /* __ASM_SYSCALL_WRAPPER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters