fix function type mismatches in syscall wrappers
v2 cover fix function type mismatches in syscall wrappers
These patches fix type mismatches in arm64 syscall wrapper definitions, which trip indirect call checks with Control-Flow Integrity.
Changes in v2:
- more informative commit message for the syscall_fn_t change
- added a patch for fixing sys_ni_syscall
Sami Tolvanen (3):
arm64: fix syscall_fn_t type
arm64: use the correct function type in SYSCALL_DEFINE0
arm64: use the correct function type for __arm64_sys_ni_syscall
arch/arm64/include/asm/syscall.h | 2 +- arch/arm64/include/asm/syscall_wrapper.h | 18 +++++++++--------- arch/arm64/kernel/sys.c | 14 +++++++++----- arch/arm64/kernel/sys32.c | 12 ++++++++---- 4 files changed, 27 insertions(+), 19 deletions(-)
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v2 1/3 arm64: fix syscall_fn_t type
Syscall wrappers in <asm/syscall_wrapper.h> use const struct pt_regs * as the argument type. Use const in syscall_fn_t as well to fix indirect call type mismatches with Control-Flow Integrity checking.
--- arch/arm64/include/asm/syscall.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/syscall.h +1 -1
@@ -20,7 +20,7 @@#include <linux/compat.h> #include <linux/err.h> -typedef long (*syscall_fn_t )(struct pt_regs *regs); +typedef long (*syscall_fn_t )(const struct pt_regs *regs);extern const syscall_fn_t sys_call_table[];
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v2 2/3 arm64: use the correct function type in SYSCALL_DEFINE0
In reply to Sami TolvanenAlthough a syscall defined using SYSCALL_DEFINE0 doesn't accept parameters, use the correct function type to avoid indirect call type mismatches with Control-Flow Integrity checking.
--- arch/arm64/include/asm/syscall_wrapper.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
arch/arm64/include/asm/syscall_wrapper.h +9 -9
@@ -30,10 +30,10 @@ } \ static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) -#define COMPAT_SYSCALL_DEFINE0 (sname) \ - asmlinkage long __arm64_compat_sys_##sname(void); \ - ALLOW_ERROR_INJECTION(__arm64_compat_sys_##sname, ERRNO); \ - asmlinkage long __arm64_compat_sys_##sname(void) +#define COMPAT_SYSCALL_DEFINE0 (sname) \ + asmlinkage long __arm64_compat_sys_##sname(const struct pt_regs *__unused); \ + ALLOW_ERROR_INJECTION(__arm64_compat_sys_##sname, ERRNO); \ + asmlinkage long __arm64_compat_sys_##sname(const struct pt_regs *__unused)#define COND_SYSCALL_COMPAT (name) \ cond_syscall(__arm64_compat_sys_##name);
@@ -62,11 +62,11 @@ static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))#ifndef SYSCALL_DEFINE0 -#define SYSCALL_DEFINE0 (sname) \ - SYSCALL_METADATA(_##sname, 0); \ - asmlinkage long __arm64_sys_##sname(void); \ - ALLOW_ERROR_INJECTION(__arm64_sys_##sname, ERRNO); \ - asmlinkage long __arm64_sys_##sname(void) +#define SYSCALL_DEFINE0 (sname) \ + SYSCALL_METADATA(_##sname, 0); \ + asmlinkage long __arm64_sys_##sname(const struct pt_regs *__unused); \ + ALLOW_ERROR_INJECTION(__arm64_sys_##sname, ERRNO); \ + asmlinkage long __arm64_sys_##sname(const struct pt_regs *__unused)#endif #ifndef COND_SYSCALL
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v2 3/3 arm64: use the correct function type for __arm64_sys_ni_syscall
In reply to Sami TolvanenCalling sys_ni_syscall through a syscall_fn_t pointer trips indirect call Control-Flow Integrity checking due to a function type mismatch. Use SYSCALL_DEFINE0 for __arm64_sys_ni_syscall instead and remove the now unnecessary casts.
--- arch/arm64/kernel/sys.c | 14 +++++++++----- arch/arm64/kernel/sys32.c | 12 ++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-)
arch/arm64/kernel/sys.c +9 -5
@@ -47,22 +47,26 @@ SYSCALL_DEFINE1(arm64_personality, unsigned int, personality)return ksys_personality (personality); } +asmlinkage long sys_ni_syscall (void ); + +SYSCALL_DEFINE0 (ni_syscall) +{ +return sys_ni_syscall (); +} +/* * Wrappers to pass the pt_regs argument. */ #define sys_personality sys_arm64_personality -asmlinkage long sys_ni_syscall (const struct pt_regs *); -#define __arm64_sys_ni_syscall sys_ni_syscall -#undef __SYSCALL#define __SYSCALL (nr, sym) asmlinkage long __arm64_##sym(const struct pt_regs *);#include <asm/unistd.h> #undef __SYSCALL -#define __SYSCALL (nr, sym) [nr] = (syscall_fn_t)__arm64_##sym, +#define __SYSCALL (nr, sym) [nr] = __arm64_##sym,const syscall_fn_t sys_call_table[__NR_syscalls] = { - [0 ... __NR_syscalls -1 ] = (syscall_fn_t )sys_ni_syscall, + [0 ... __NR_syscalls -1 ] = __arm64_sys_ni_syscall, #include <asm/unistd.h> };
arch/arm64/kernel/sys32.c +8 -4
@@ -133,17 +133,21 @@ COMPAT_SYSCALL_DEFINE6(aarch32_fallocate, int, fd, int, mode,return ksys_fallocate (fd, mode,arg_u64 (offset),arg_u64 (len)); } -asmlinkage long sys_ni_syscall (const struct pt_regs *); -#define __arm64_sys_ni_syscall sys_ni_syscall +asmlinkage long sys_ni_syscall (void ); + +COMPAT_SYSCALL_DEFINE0 (ni_syscall) +{ +return sys_ni_syscall (); +}#undef __SYSCALL#define __SYSCALL (nr, sym) asmlinkage long __arm64_##sym(const struct pt_regs *);#include <asm/unistd32.h> #undef __SYSCALL -#define __SYSCALL (nr, sym) [nr] = (syscall_fn_t)__arm64_##sym, +#define __SYSCALL (nr, sym) [nr] = __arm64_##sym,const syscall_fn_t compat_sys_call_table[__NR_compat_syscalls] = { - [0 ... __NR_compat_syscalls -1 ] = (syscall_fn_t )sys_ni_syscall, + [0 ... __NR_compat_syscalls -1 ] = __arm64_sys_ni_syscall, #include <asm/unistd32.h> };
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Mark Rutland
reviewerRe: [PATCH v2 3/3] arm64: use the correct function type for __arm64_sys_ni_syscall
Quoted 16 lines · Sami Tolvanen
Calling
sys_ni_syscallthrough asyscall_fn_tpointer trips indirect call Control-Flow Integrity checking due to a function type mismatch. UseSYSCALL_DEFINE0for__arm64_sys_ni_syscallinstead and remove the now unnecessary casts.Signed-off-by Sami Tolvanen <[email protected]>--- arch/arm64/kernel/sys.c | 14 +++++++++----- arch/arm64/kernel/sys32.c | 12 ++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-)arch/arm64/kernel/sys.c +0
-0
arch/arm64/kernel/sys.c SYSCALL_DEFINE1(arm64_personality, unsigned int, personality) · line 55
Quoted 9 lines
arch/arm64/kernel/sys.c +6
-0@@ -47,22 +47,26 @@ SYSCALL_DEFINE1(arm64_personality, unsigned int, personality)return ksys_personality (personality); } +asmlinkage long sys_ni_syscall (void ); + +SYSCALL_DEFINE0 (ni_syscall) +{ +return sys_ni_syscall (); +}
I strongly think that we cant to fix up the common definition in kernel/sys_ni.c rather than having a point-hack in arm64. Other architectures (e.g. x86, s390) will want the same for CFI, and I'd like to ensure that our approached don't diverge.
I took a quick look, and it looks like it's messy but possible to fix up the core.
I also suspect that using SYSCALL_DEFINE0() as it currently stands isn't a great idea, since it'll allow fault injection for unimplemented syscalls, which sounds dubious to me.
Thanks,
Mark.
Quoted 55 lines
arch/arm64/kernel/sys.c +3
-5+/* * Wrappers to pass the pt_regs argument. */ #define sys_personality sys_arm64_personality -asmlinkage long sys_ni_syscall (const struct pt_regs *); -#define __arm64_sys_ni_syscall sys_ni_syscall -#undef __SYSCALL#define __SYSCALL (nr, sym) asmlinkage long __arm64_##sym(const struct pt_regs *);#include <asm/unistd.h> #undef __SYSCALL -#define __SYSCALL (nr, sym) [nr] = (syscall_fn_t)__arm64_##sym, +#define __SYSCALL (nr, sym) [nr] = __arm64_##sym,const syscall_fn_t sys_call_table[__NR_syscalls] = { - [0 ... __NR_syscalls -1 ] = (syscall_fn_t )sys_ni_syscall, + [0 ... __NR_syscalls -1 ] = __arm64_sys_ni_syscall, #include <asm/unistd.h> };arch/arm64/kernel/sys32.c +8
-4@@ -133,17 +133,21 @@ COMPAT_SYSCALL_DEFINE6(aarch32_fallocate, int, fd, int, mode,return ksys_fallocate (fd, mode,arg_u64 (offset),arg_u64 (len)); } -asmlinkage long sys_ni_syscall (const struct pt_regs *); -#define __arm64_sys_ni_syscall sys_ni_syscall +asmlinkage long sys_ni_syscall (void ); + +COMPAT_SYSCALL_DEFINE0 (ni_syscall) +{ +return sys_ni_syscall (); +}#undef __SYSCALL#define __SYSCALL (nr, sym) asmlinkage long __arm64_##sym(const struct pt_regs *);#include <asm/unistd32.h> #undef __SYSCALL -#define __SYSCALL (nr, sym) [nr] = (syscall_fn_t)__arm64_##sym, +#define __SYSCALL (nr, sym) [nr] = __arm64_##sym,const syscall_fn_t compat_sys_call_table[__NR_compat_syscalls] = { - [0 ... __NR_compat_syscalls -1 ] = (syscall_fn_t )sys_ni_syscall, + [0 ... __NR_compat_syscalls -1 ] = __arm64_sys_ni_syscall, #include <asm/unistd32.h> };Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Re: [PATCH v2 3/3] arm64: use the correct function type for __arm64_sys_ni_syscall
Quoted 4 lines · Mark Rutland
I strongly think that we cant to fix up the common definition in
kernel/sys_ni.crather than having a point-hack in arm64. Other architectures (e.g. x86, s390) will want the same for CFI, and I'd like to ensure that our approached don't diverge.
s390 already has the following in arch/s390/kernel/sys_s390.c:
SYSCALL_DEFINE0(ni_syscall)
{
return -ENOSYS;
}Which, I suppose, is cleaner than calling sys_ni_syscall.
Quoted 2 lines
I took a quick look, and it looks like it's messy but possible to fix up the core.
OK. How would you propose fixing this?
Sami
Re: [PATCH v2 3/3] arm64: use the correct function type for __arm64_sys_ni_syscall
Quoted 13 lines · Sami Tolvanen
Quoted 4 lines · Mark Rutland
I strongly think that we cant to fix up the common definition in
kernel/sys_ni.crather than having a point-hack in arm64. Other architectures (e.g. x86, s390) will want the same for CFI, and I'd like to ensure that our approached don't diverge.s390 already has the following in
arch/s390/kernel/sys_s390.c:SYSCALL_DEFINE0(ni_syscall) { return -ENOSYS; }
Quoted 6 lines
Which, I suppose, is cleaner than calling
sys_ni_syscall.Quoted 2 lines
I took a quick look, and it looks like it's messy but possible to fix up the core.
OK. How would you propose fixing this?
In the absence of a patch from Mark, I'd suggest just adding a SYS_NI macro to our asm/syscall_wrapper.h file which avoids the error injection stuff. It doesn't preclude moving this to the core later on, but it unblocks the CFI work.
Will
Re: [PATCH v2 3/3] arm64: use the correct function type for __arm64_sys_ni_syscall
Hi Sami,
Quoted 19 lines · Will Deacon
Quoted 18 lines · Sami Tolvanen
Quoted 4 lines · Mark Rutland
I strongly think that we cant to fix up the common definition in
kernel/sys_ni.crather than having a point-hack in arm64. Other architectures (e.g. x86, s390) will want the same for CFI, and I'd like to ensure that our approached don't diverge.s390 already has the following in
arch/s390/kernel/sys_s390.c:SYSCALL_DEFINE0(ni_syscall) { return -ENOSYS; }Which, I suppose, is cleaner than calling
sys_ni_syscall.Quoted 2 lines
I took a quick look, and it looks like it's messy but possible to fix up the core.
Quoted 6 lines
Quoted 1 line
OK. How would you propose fixing this?
In the absence of a patch from Mark, I'd suggest just adding a
SYS_NImacro to ourasm/syscall_wrapper.hfile which avoids the error injection stuff. It doesn't preclude moving this to the core later on, but it unblocks the CFI work.
Do you plan to repost this?
Will
Re: [PATCH v2 3/3] arm64: use the correct function type for __arm64_sys_ni_syscall
Quoted 2 lines · Will Deacon
Quoted 2 lines
In the absence of a patch from Mark, I'd suggest just adding a
SYS_NImacro to ourasm/syscall_wrapper.hfile which avoids the error injection stuff.
If we don't want to use SYSCALL_DEFINE0, I don't think we need a macro at all. I believe it's cleaner to just define __arm64_sys_ni_syscall with the correct type in sys.c.
Quoted 1 line
Do you plan to repost this?
Yes. Sorry for the delay. I'll post v3 shortly.
Sami