Syscall user redirection
v3 cover Syscall user redirection
Hi,
This is the v3 of the syscall user redirection patch, applying the suggestions from Matthew and Kees. In particular, it modifies the ABI to allow passing a range of allowed addresses and introduces kselftests for the feature.
RFC/v1: https://lkml.org/lkml/2020/7/8/96
v2: https://lkml.org/lkml/2020/7/9/17
Gabriel Krisman Bertazi (2):
kernel: Implement selective syscall userspace redirection
selftests: Add kselftest for syscall user dispatch
arch/Kconfig | 20 ++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 5 + arch/x86/include/asm/thread_info.h | 4 +- arch/x86/kernel/signal_compat.c | 2 +- fs/exec.c | 2 + include/linux/sched.h | 3 + include/linux/syscall_user_dispatch.h | 50 ++++ include/uapi/asm-generic/siginfo.h | 3 +- include/uapi/linux/prctl.h | 5 + kernel/Makefile | 1 + kernel/fork.c | 1 + kernel/sys.c | 5 + kernel/syscall_user_dispatch.c | 92 +++++++ tools/testing/selftests/Makefile | 1 + .../syscall_user_dispatch/.gitignore | 1 + .../selftests/syscall_user_dispatch/Makefile | 5 + .../selftests/syscall_user_dispatch/config | 1 + .../syscall_user_dispatch.c | 259 ++++++++++++++++++ 19 files changed, 458 insertions(+), 3 deletions(-) create mode 100644 include/linux/syscall_user_dispatch.h create mode 100644 kernel/syscall_user_dispatch.c create mode 100644 tools/testing/selftests/syscall_user_dispatch/.gitignore create mode 100644 tools/testing/selftests/syscall_user_dispatch/Makefile create mode 100644 tools/testing/selftests/syscall_user_dispatch/config create mode 100644 tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c
Signature · 1 line
2.27.0
PATCH v3 1/2 kernel: Implement selective syscall userspace redirection
Introduce a mechanism to quickly disable/enable syscall handling for a specific process and redirect to userspace via SIGSYS. This is useful for processes with parts that require syscall redirection and parts that don't, but who need to perform this boundary crossing really fast, without paying the cost of a system call to reconfigure syscall handling on each boundary transition. This is particularly important for Windows games running over Wine.
The proposed interface looks like this:
prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <start_addr>, <end_addr>, [selector])
The range [<start_addr>,<end_addr>] is a part of the process memory map that is allowed to by-pass the redirection code and dispatch syscalls directly, such that in fast paths a process doesn't need to disable the trap nor the kernel has to check the selector. This is essential to return from SIGSYS to a blocked area without triggering another SIGSYS from rt_sigreturn.
selector is an optional pointer to a char-sized userspace memory region that has a key switch for the mechanism. This key switch is set to either PR_SYS_DISPATCH_ON, PR_SYS_DISPATCH_OFF to enable and disable the redirection without calling the kernel.
The feature is meant to be set per-thread and it is disabled on fork/clone/execv.
Internally, this doesn't add overhead to the syscall hot path, and it requires very little per-architecture support. I avoided using seccomp, even though it duplicates some functionality, due to previous feedback that maybe it shouldn't mix with seccomp since it is not a security mechanism. And obviously, this should never be considered a security mechanism, since any part of the program can by-pass it by using the syscall dispatcher.
For the sysinfo benchmark, which measures the overhead added to executing a native syscall that doesn't require interception, the overhead using only the direct dispatcher region to issue syscalls is pretty much irrelevant. The overhead of using the selector goes around 40ns for a native (unredirected) syscall in my system, and it is (as expected) dominated by the supervisor-mode user-address access. In fact, with SMAP off, the overhead is consistently less than 5ns on my test box.
Right now, it is only supported by x86_64 and x86, but it should be easily enabled for other architectures.
An example code using this interface can be found at:
https://gitlab.collabora.com/krisman/syscall-disable-personality
Changes since v2:
(Matthew Wilcox suggestions)
- Drop __user on non-ptr type.
- Move #define closer to similar defs
- Allow a memory region that can dispatch directly (Kees Cook suggestions)
- Improve kconfig summary line
- Move flag cleanup on execve to begin_new_exec
- Hint branch predictor in the syscall path (Me)
- Convert selector to char
Changes since RFC:
(Kees Cook suggestions)
- Don't mention personality while explaining the feature
- Use syscall_get_nr
- Remove header guard on several places
- Convert WARN_ON to WARN_ON_ONCE
- Explicit check for state values
- Rename to syscall user dispatcher
--- arch/Kconfig | 20 ++++++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 5 ++ arch/x86/include/asm/thread_info.h | 4 +- arch/x86/kernel/signal_compat.c | 2 +- fs/exec.c | 2 + include/linux/sched.h | 3 + include/linux/syscall_user_dispatch.h | 50 +++++++++++++++ include/uapi/asm-generic/siginfo.h | 3 +- include/uapi/linux/prctl.h | 5 ++ kernel/Makefile | 1 + kernel/fork.c | 1 + kernel/sys.c | 5 ++ kernel/syscall_user_dispatch.c | 92 +++++++++++++++++++++++++++ 14 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 include/linux/syscall_user_dispatch.h create mode 100644 kernel/syscall_user_dispatch.c
arch/Kconfig +20 -0
@@ -465,6 +465,26 @@ config SECCOMP_FILTER See Documentation/userspace-api/seccomp_filter.rst for details. +config HAVE_ARCH_SYSCALL_USER_DISPATCH + bool + help + An arch should select this symbol if it provides all of these things: + - TIF_SYSCALL_USER_DISPATCH + - syscall_get_arch + - syscall_rollback + - syscall_get_nr + - SIGSYS siginfo_t support + +config SYSCALL_USER_DISPATCH + bool "Support syscall redirection to userspace dispatcher" + depends on HAVE_ARCH_SYSCALL_USER_DISPATCH + help + Enable tasks to ask the kernel to redirect syscalls not + issued from a predefined dispatcher back to userspace, + depending on a userspace memory selector. + + This option is useful to optimize games running over Wine. + config HAVE_ARCH_STACKLEAK bool help
arch/x86/Kconfig +1 -0
@@ -149,6 +149,7 @@ config X86 select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER + select HAVE_ARCH_SYSCALL_USER_DISPATCH select HAVE_ARCH_THREAD_STRUCT_WHITELIST select HAVE_ARCH_STACKLEAK select HAVE_ARCH_TRACEHOOK
arch/x86/entry/common.c +5 -0
@@ -138,6 +138,11 @@ static long syscall_trace_enter(struct pt_regs *regs)return -1L ; } +if (work & _TIF_SYSCALL_USER_DISPATCH) { +if (do_syscall_user_dispatch (regs)) +return -1L ; + } +#ifdef CONFIG_SECCOMP /* * Do seccomp after ptrace, to catch any tracer changes.
arch/x86/include/asm/thread_info.h +3 -1
@@ -93,6 +93,7 @@ struct thread_info {#define TIF_NOTSC 16/* TSC is not accessible in userland */ #define TIF_IA32 17/* IA32 compatibility process */ #define TIF_SLD 18/* Restore split lock detection on context switch */ +#define TIF_SYSCALL_USER_DISPATCH 19/* Redirect syscall for userspace handling */ #define TIF_MEMDIE 20/* is terminating due to OOM killer */ #define TIF_POLLING_NRFLAG 21/* idle is polling for TIF_NEED_RESCHED */ #define TIF_IO_BITMAP 22/* uses I/O bitmap */
@@ -123,6 +124,7 @@ struct thread_info {#define _TIF_NOTSC (1 << TIF_NOTSC)#define _TIF_IA32 (1 << TIF_IA32)#define _TIF_SLD (1 << TIF_SLD) +#define _TIF_SYSCALL_USER_DISPATCH (1 << TIF_SYSCALL_USER_DISPATCH)#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)#define _TIF_IO_BITMAP (1 << TIF_IO_BITMAP)#define _TIF_FORCED_TF (1 << TIF_FORCED_TF)
@@ -136,7 +138,7 @@ struct thread_info {/* Work to do before invoking the actual syscall. */ #define _TIF_WORK_SYSCALL_ENTRY \ (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU | _TIF_SYSCALL_AUDIT | \ - _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT) + _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_USER_DISPATCH)/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW_BASE \
arch/x86/kernel/signal_compat.c +1 -1
@@ -31,7 +31,7 @@ static inline void signal_compat_build_tests(void)BUILD_BUG_ON (NSIGBUS !=5 );BUILD_BUG_ON (NSIGTRAP !=5 );BUILD_BUG_ON (NSIGCHLD !=6 ); -BUILD_BUG_ON (NSIGSYS !=1 ); +BUILD_BUG_ON (NSIGSYS !=2 );/* This is part of the ABI and can never change in size: */ BUILD_BUG_ON (sizeof (compat_siginfo_t) !=128 );
fs/exec.c +2 -0
@@ -1386,6 +1386,8 @@ int begin_new_exec(struct linux_binprm * bprm)flush_thread (); me->personality &= ~bprm->per_clear; +clear_tsk_syscall_user_dispatch (me); +/* * We have to apply CLOEXEC before we change whether the process is * dumpable (in setup_new_exec) to avoid a race with a process in userspace
include/linux/sched.h +3 -0
@@ -32,6 +32,7 @@#include <linux/posix-timers.h> #include <linux/rseq.h> #include <linux/kcsan.h> +#include <linux/syscall_user_dispatch.h> /* task_struct member predeclarations (sorted alphabetically): */ struct audit_context ;
@@ -953,6 +954,8 @@ struct task_struct {#endif struct seccomp seccomp; +struct syscall_user_dispatch syscall_dispatch; +/* Thread group tracking: */ u64 parent_exec_id;u64 self_exec_id;
include/linux/syscall_user_dispatch.h new file +50 -0
50 lines not on this page · raw message
include/uapi/asm-generic/siginfo.h +2 -1
@@ -284,7 +284,8 @@ typedef struct siginfo {* SIGSYS si_codes */ #define SYS_SECCOMP 1/* seccomp triggered */ -#define NSIGSYS 1 +#define SYS_USER_DISPATCH 2/* syscall user dispatch triggered */ +#define NSIGSYS 2/* * SIGEMT si_codes
include/uapi/linux/prctl.h +5 -0
@@ -238,4 +238,9 @@ struct prctl_mm_map {#define PR_SET_IO_FLUSHER 57#define PR_GET_IO_FLUSHER 58 +/* Dispatch syscalls to a userspace handler */ +#define PR_SET_SYSCALL_USER_DISPATCH 59 +# define PR_SYS_DISPATCH_OFF 0 +# define PR_SYS_DISPATCH_ON 1 +#endif /* _LINUX_PRCTL_H */
kernel/Makefile +1 -0
@@ -92,6 +92,7 @@ obj-$(CONFIG_DETECT_HUNG_TASK) += hung_task.o obj-$(CONFIG_LOCKUP_DETECTOR) += watchdog.o obj-$(CONFIG_HARDLOCKUP_DETECTOR_PERF) += watchdog_hld.o obj-$(CONFIG_SECCOMP) += seccomp.o +obj-$(CONFIG_SYSCALL_USER_DISPATCH) += syscall_user_dispatch.o obj-$(CONFIG_RELAY) += relay.o obj-$(CONFIG_SYSCTL) += utsname_sysctl.o obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o
kernel/fork.c +1 -0
@@ -921,6 +921,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)clear_user_return_notifier (tsk);clear_tsk_need_resched (tsk);set_task_stack_end_magic (tsk); +clear_tsk_syscall_user_dispatch (tsk);#ifdef CONFIG_STACKPROTECTOR tsk->stack_canary =get_random_canary ();
kernel/sys.c +5 -0
@@ -42,6 +42,7 @@#include <linux/syscore_ops.h> #include <linux/version.h> #include <linux/ctype.h> +#include <linux/syscall_user_dispatch.h> #include <linux/compat.h> #include <linux/syscalls.h>
@@ -2527,6 +2528,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, error = (current->flags &PR_IO_FLUSHER ) ==PR_IO_FLUSHER ;break ; +case PR_SET_SYSCALL_USER_DISPATCH : + error =set_syscall_user_dispatch ((int ) arg2, arg3, arg4, + (char __user *) arg5); +break ;default : error = -EINVAL ;break ;
kernel/syscall_user_dispatch.c new file +92 -0
92 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v3 1/2] kernel: Implement selective syscall userspace redirection
Quoted 71 lines · Gabriel Krisman Bertazi
Introduce a mechanism to quickly disable/enable syscall handling for a specific process and redirect to userspace via SIGSYS. This is useful for processes with parts that require syscall redirection and parts that don't, but who need to perform this boundary crossing really fast, without paying the cost of a system call to reconfigure syscall handling on each boundary transition. This is particularly important for Windows games running over Wine.
The proposed interface looks like this:
prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <start_addr>, <end_addr>, [selector])The range [<start_addr>,<end_addr>] is a part of the process memory map that is allowed to by-pass the redirection code and dispatch syscalls directly, such that in fast paths a process doesn't need to disable the trap nor the kernel has to check the selector. This is essential to return from SIGSYS to a blocked area without triggering another SIGSYS from
rt_sigreturn.selector is an optional pointer to a char-sized userspace memory region that has a key switch for the mechanism. This key switch is set to either
PR_SYS_DISPATCH_ON,PR_SYS_DISPATCH_OFFto enable and disable the redirection without calling the kernel.The feature is meant to be set per-thread and it is disabled on fork/clone/execv.
Internally, this doesn't add overhead to the syscall hot path, and it requires very little per-architecture support. I avoided using seccomp, even though it duplicates some functionality, due to previous feedback that maybe it shouldn't mix with seccomp since it is not a security mechanism. And obviously, this should never be considered a security mechanism, since any part of the program can by-pass it by using the syscall dispatcher.
For the sysinfo benchmark, which measures the overhead added to executing a native syscall that doesn't require interception, the overhead using only the direct dispatcher region to issue syscalls is pretty much irrelevant. The overhead of using the selector goes around 40ns for a native (unredirected) syscall in my system, and it is (as expected) dominated by the supervisor-mode user-address access. In fact, with SMAP off, the overhead is consistently less than 5ns on my test box.
Right now, it is only supported by
x86_64and x86, but it should be easily enabled for other architectures.An example code using this interface can be found at:
https://gitlab.collabora.com/krisman/syscall-disable-personalityChanges since v2:
(Matthew Wilcox suggestions)
- Drop__useron non-ptr type.
- Move #define closer to similar defs
- Allow a memory region that can dispatch directly (Kees Cook suggestions)
- Improve kconfig summary line
- Move flag cleanup on execve tobegin_new_exec
- Hint branch predictor in the syscall path (Me)
- Convert selector to charChanges since RFC:
(Kees Cook suggestions)
- Don't mention personality while explaining the feature
- Usesyscall_get_nr
- Remove header guard on several places
- ConvertWARN_ONtoWARN_ON_ONCE
- Explicit check for state values
- Rename to syscall user dispatcher
Quoted 5 lines
Cc Matthew Wilcox <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Paul Gofman <[email protected]>Cc Kees Cook <[email protected]>Signed-off-by Gabriel Krisman Bertazi <[email protected]>
I think this looks great. :)
Any other folks able to look through it?
Signature · 1 line
Kees Cook
PATCH v3 2/2 selftests: Add kselftest for syscall user dispatch
In reply to Gabriel Krisman BertaziImplement functionality tests for syscall user dispatch. In order to make the test portable, refrain from open coding syscall dispatchers and calculating glibc memory ranges.
--- tools/testing/selftests/Makefile | 1 + .../syscall_user_dispatch/.gitignore | 1 + .../selftests/syscall_user_dispatch/Makefile | 5 + .../selftests/syscall_user_dispatch/config | 1 + .../syscall_user_dispatch.c | 259 ++++++++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 tools/testing/selftests/syscall_user_dispatch/.gitignore create mode 100644 tools/testing/selftests/syscall_user_dispatch/Makefile create mode 100644 tools/testing/selftests/syscall_user_dispatch/config create mode 100644 tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c
tools/testing/selftests/Makefile +1 -0
@@ -47,6 +47,7 @@ TARGETS += openat2 TARGETS += rseq TARGETS += rtc TARGETS += seccomp +TARGETS += syscall_user_dispatch TARGETS += sigaltstack TARGETS += size TARGETS += sparc64
tools/testing/selftests/syscall_user_dispatch/.gitignore new file +1 -0
@@ -0,0 +1 @@ +syscall_user_dispatch
tools/testing/selftests/syscall_user_dispatch/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wall + +TEST_GEN_PROGS := syscall_user_dispatch +include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_SYSCALL_USER_DISPATCH=y
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +259 -0
259 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v3 2/2] selftests: Add kselftest for syscall user dispatch
Quoted 22 lines · Gabriel Krisman Bertazi
Implement functionality tests for syscall user dispatch. In order to make the test portable, refrain from open coding syscall dispatchers and calculating glibc memory ranges.
Signed-off-by Gabriel Krisman Bertazi <[email protected]>--- tools/testing/selftests/Makefile | 1 + .../syscall_user_dispatch/.gitignore | 1 + .../selftests/syscall_user_dispatch/Makefile | 5 + .../selftests/syscall_user_dispatch/config | 1 + .../syscall_user_dispatch.c | 259 ++++++++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 tools/testing/selftests/syscall_user_dispatch/.gitignore create mode 100644 tools/testing/selftests/syscall_user_dispatch/Makefile create mode 100644 tools/testing/selftests/syscall_user_dispatch/config create mode 100644 tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.ctools/testing/selftests/Makefile +0
-0
tools/testing/selftests/Makefile TARGETS += openat2 · line 53
Quoted 7 lines
tools/testing/selftests/Makefile +1
-0@@ -47,6 +47,7 @@ TARGETS += openat2 TARGETS += rseq TARGETS += rtc TARGETS += seccomp +TARGETS += syscall_user_dispatch TARGETS += sigaltstack TARGETS += size TARGETS += sparc64
nit: moar alphabetical! :)
tools/testing/selftests/syscall_user_dispatch/.gitignore line 1
Quoted 7 lines
tools/testing/selftests/syscall_user_dispatch/.gitignore new file +1
-0@@ -0,0 +1 @@ +syscall_user_dispatch
nit: this needs as the first line:
# SPDX-License-Identifier: GPL-2.0-only
Quoted 32 lines
tools/testing/selftests/syscall_user_dispatch/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wall + +TEST_GEN_PROGS := syscall_user_dispatch +include ../lib.mktools/testing/selftests/syscall_user_dispatch/config new file +1
-0@@ -0,0 +1 @@ +CONFIG_SYSCALL_USER_DISPATCH=ytools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +8
-0@@ -0,0 +1,259 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2020 Collabora Ltd. +* +* Test code for syscall user dispatch +*/ + +#define _GNU_SOURCE
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c line 18
Quoted 10 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +10
-0+#include <sys/types.h> +#include <sys/prctl.h> +#include <sys/syscall.h> +#include <sys/sysinfo.h> +#include <signal.h> +#include <errno.h> + +#include "../kselftest_harness.h" + +#ifndef PR_SET_SYSCALL_USER_DISPATCH
style nit: I usually use the initial ifndef to wrap all those related to it. i.e.:
#ifndef PR_SET_SYSCALL_USER_DISPATCH
# define PR_SET_SYSCALL_USER_DISPATCH 59 # define PR_SYS_DISPATCH_OFF 0 # define PR_SYS_DISPATCH_ON 1 ... #endif
But either way is fine.
Quoted 89 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +89
-0+# define PR_SET_SYSCALL_USER_DISPATCH 59 +#endif + +#ifndef PR_SYS_DISPATCH_OFF +# define PR_SYS_DISPATCH_OFF 0 +#endif + +#ifndef PR_SYS_DISPATCH_ON +# define PR_SYS_DISPATCH_ON 1 +#endif + +#ifndef SYS_USER_DISPATCH +# define SYS_USER_DISPATCH 2 +#endif + +#define SYSCALL_DISPATCH_ON (x) ((x) = 1) +#define SYSCALL_DISPATCH_OFF (x) ((x) = 0) + +/* Test Summary: +* +* - dispatch_trigger_sigsys: Verify if PR_SET_SYSCALL_USER_DISPATCH is +* able to trigger SIGSYS on a syscall. +* +* - bad_selector: Test that a bad selector value triggers SIGSEGV. +* +* - bad_prctl_param: Test that the API correctly rejects invalid +* parameters on prctl +* +* - dispatch_and_return: Test that a syscall is selectively dispatched +* to userspace depending on the value of selector. +* +* - disable_dispatch: Test that the PR_SYS_DISPATCH_OFF correctly +* disables the dispatcher +* +* - direct_dispatch_range: Test that a syscall within the allowed range +* can bypass the dispatcher. +*/ + +TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS) +{ +char sel =0 ; +struct sysinfo info; +int ret; + + ret =sysinfo (&info); +ASSERT_EQ (0 , ret); + + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ASSERT_EQ(0, ret) { +TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); + } + +SYSCALL_DISPATCH_ON (sel); + +sysinfo (&info); + +EXPECT_FALSE (true) { +TH_LOG ("Unreachable!" ); + } +} + +TEST_SIGNAL(bad_selector, SIGSEGV) +{ +char sel =-1 ; +long ret; + + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ASSERT_EQ(0, ret) { +TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); + } +EXPECT_FALSE (true) { +TH_LOG ("Unreachable!" ); + } +} + +TEST (bad_prctl_param) +{ +char sel =0 ; +int op; + +/* Invalid op */ + op =-1 ; +prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0 ,0 , &sel); +ASSERT_EQ (EINVAL , errno); + +/* PR_SYS_DISPATCH_OFF */ + op =PR_SYS_DISPATCH_OFF ; + +/* start_addr != 0 */
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c line 117
Quoted 10 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +10
-0+prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x1 ,0xff ,0 ); +EXPECT_EQ (EINVAL , errno); + +/* end_addr != 0 */ +prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0xff ,0 ); +EXPECT_EQ (EINVAL , errno); + +/* sel != NULL */ +prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x0 , &sel); +EXPECT_EQ (EINVAL , errno);
I think here would be a good place to test PR_GET_... too?
Quoted 9 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +9
-0+ +/* Valid parameter */ + errno =0 ; +syscall (SYS_prctl,PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x0 ,0x0 ); +EXPECT_EQ (0 , errno); + +/* PR_SYS_DISPATCH_ON */ + op =PR_SYS_DISPATCH_ON ; +
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c line 136
Quoted 10 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +10
-0+/* start_addr > end_addr */ +syscall (SYS_prctl,PR_SET_SYSCALL_USER_DISPATCH , op,0x1 ,0x0 , &sel); +EXPECT_EQ (EINVAL , errno); + +/* Invalid selector */ +syscall (SYS_prctl,PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x1 , (void *)-1 ); +ASSERT_EQ (EFAULT , errno); +} + +#define MAGIC_SYSCALL_1 0xff00/* Bad Linux syscall number */
Some archs do weird things with syscalls. Maybe use __NR_syscalls + 1? For x86, this should be fine, though.
Quoted 113 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +113
-0+ +/* +* Use global selector for handle_sigsys tests, to avoid passing +* selector to signal handler +*/ +char glob_sel; +int nr_syscalls_emulated; +int si_code; + +static void handle_sigsys (int sig,siginfo_t *info,void *ucontext) +{ + si_code = info->si_code; + +if (info->si_syscall ==MAGIC_SYSCALL_1 ) + nr_syscalls_emulated++; + +/* In preparation for sigreturn. */ +SYSCALL_DISPATCH_OFF (glob_sel); +} + +TEST (dispatch_and_return) +{ +long ret; +struct sigaction act; +sigset_t mask; + + glob_sel =0 ; + nr_syscalls_emulated =0 ; + si_code =0 ; + +memset (&act,0 ,sizeof (act)); +sigemptyset (&mask); + + act.sa_sigaction = handle_sigsys; + act.sa_flags =SA_SIGINFO ; + act.sa_mask = mask; + + ret =sigaction (SIGSYS , &act,NULL ); +ASSERT_EQ (0 , ret); + +/* Make sure selector is good prior to prctl. */ +SYSCALL_DISPATCH_OFF (glob_sel); + + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &glob_sel); + ASSERT_EQ(0, ret) { +TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); + } + +/* MAGIC_SYSCALL_1 doesn't exist. */ +SYSCALL_DISPATCH_OFF (glob_sel); + ret =syscall (MAGIC_SYSCALL_1 ); + EXPECT_EQ(-1, ret) { +TH_LOG ("Dispatch triggered unexpectedly" ); + } + +/* MAGIC_SYSCALL_1 should be emulated. */ + nr_syscalls_emulated =0 ; +SYSCALL_DISPATCH_ON (glob_sel); + + ret =syscall (MAGIC_SYSCALL_1 ); + EXPECT_EQ(MAGIC_SYSCALL_1, ret) { +TH_LOG ("Failed to intercept syscall" ); + } + EXPECT_EQ(1, nr_syscalls_emulated) { +TH_LOG ("Failed to emulate syscall" ); + } + ASSERT_EQ(SYS_USER_DISPATCH, si_code) { +TH_LOG ("Bad si_code in SIGSYS" ); + } +} + +TEST (disable_dispatch) +{ +int ret; +struct sysinfo info; +char sel =0 ; + + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ASSERT_EQ(0, ret) { +TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); + } + +/* MAGIC_SYSCALL_1 doesn't exist. */ +SYSCALL_DISPATCH_OFF (glob_sel); + + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_OFF ,0 ,0 ,0 ); + EXPECT_EQ(0, ret) { +TH_LOG ("Failed to unset syscall user dispatch" ); + } + +/* Shouldn't have any effect... */ +SYSCALL_DISPATCH_ON (glob_sel); + + ret =syscall (SYS_sysinfo, &info); + EXPECT_EQ(0, ret) { +TH_LOG ("Dispatch triggered unexpectedly" ); + } +} + +TEST (direct_dispatch_range) +{ +int ret =0 ; +struct sysinfo info; +char sel =0 ; + +/* +* Instead of calculating libc addresses; allow the entire +* memory map and lock the selector. +*/ + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,-1L , &sel); + ASSERT_EQ(0, ret) { +TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); + }
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c line 259
Quoted 10 lines
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +10
-0+ +SYSCALL_DISPATCH_ON (sel); + + ret =syscall (SYS_sysinfo, &info); + ASSERT_EQ(0, ret) { +TH_LOG ("Dispatch triggered unexpectedly" ); + } +} + +TEST_HARNESS_MAIN
Yay tests! :) Thank you! :)
Signature · 1 line
Kees Cook