Syscall User Dispatch
Subsystems (12)
v8 cover Syscall User Dispatch
Hi,
This is v8 of syscall user dispatch. Last version got some acks but there was one small documentation fix I wanted to do, as requested by Florian. This also addresses the commit message fixup Peter requested.
The only actual code change from v7 is solving a trivial merge conflict I myself created with the entry code fixup I made week and with something else in the TIP tree.
I also shared this with glibc and there wasn't any complaints other than the matter about user-notif vs. siginfo, which was discussed in v7 and the understanding is that it is not necessary now and can be added later, if needed, on the same infrastructure without a new api.
I'm not sure about TIP the rules, but is it too late to be queued for the next merge window? I'd love to have this in 5.11 if possible, since it has been flying for quite a while.
This is based on tip/master.
As usual, a working tree with this patchset is available at:
https://gitlab.collabora.com/krisman/linux -b syscall-user-dispatch-v8
Previous submissions are archived at:
RFC/v1: https://lkml.org/lkml/2020/7/8/96
v2: https://lkml.org/lkml/2020/7/9/17
v3: https://lkml.org/lkml/2020/7/12/4
v4: https://www.spinics.net/lists/linux-kselftest/msg16377.html
v5: https://lkml.org/lkml/2020/8/10/1320
v6: https://lkml.org/lkml/2020/9/4/1122
v7: https://lwn.net/Articles/837598/
Gabriel Krisman Bertazi (7):
x86: vdso: Expose sigreturn address on vdso to the kernel
signal: Expose SYS_USER_DISPATCH si_code type
kernel: Implement selective syscall userspace redirection
entry: Support Syscall User Dispatch on common syscall entry
selftests: Add kselftest for syscall user dispatch
selftests: Add benchmark for syscall user dispatch
docs: Document Syscall User Dispatch
.../admin-guide/syscall-user-dispatch.rst | 87 +++++ arch/x86/entry/vdso/vdso2c.c | 2 + arch/x86/entry/vdso/vdso32/sigreturn.S | 2 + arch/x86/entry/vdso/vma.c | 15 + arch/x86/include/asm/elf.h | 2 + arch/x86/include/asm/vdso.h | 2 + arch/x86/kernel/signal_compat.c | 2 +- fs/exec.c | 3 + include/linux/entry-common.h | 2 + include/linux/sched.h | 2 + include/linux/syscall_user_dispatch.h | 40 +++ include/linux/thread_info.h | 2 + include/uapi/asm-generic/siginfo.h | 3 +- include/uapi/linux/prctl.h | 5 + kernel/entry/Makefile | 2 +- kernel/entry/common.c | 17 + kernel/entry/common.h | 16 + kernel/entry/syscall_user_dispatch.c | 102 ++++++ kernel/fork.c | 1 + kernel/sys.c | 5 + tools/testing/selftests/Makefile | 1 + .../syscall_user_dispatch/.gitignore | 3 + .../selftests/syscall_user_dispatch/Makefile | 9 + .../selftests/syscall_user_dispatch/config | 1 + .../syscall_user_dispatch/sud_benchmark.c | 200 +++++++++++ .../syscall_user_dispatch/sud_test.c | 310 ++++++++++++++++++ 26 files changed, 833 insertions(+), 3 deletions(-) create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst create mode 100644 include/linux/syscall_user_dispatch.h create mode 100644 kernel/entry/common.h create mode 100644 kernel/entry/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/sud_benchmark.c create mode 100644 tools/testing/selftests/syscall_user_dispatch/sud_test.c
Signature · 1 line
2.29.2
PATCH v8 1/7 x86: vdso: Expose sigreturn address on vdso to the kernel
Syscall user redirection requires the signal trampoline code to not be captured, in order to support returning with a locked selector while avoiding recursion back into the signal handler. For ia-32, which has the trampoline in the vDSO, expose the entry points to the kernel, such that it can avoid dispatching syscalls from that region to userspace.
---
Changes since V5
- Change return address to bool (Andy)
--- arch/x86/entry/vdso/vdso2c.c | 2 ++ arch/x86/entry/vdso/vdso32/sigreturn.S | 2 ++ arch/x86/entry/vdso/vma.c | 15 +++++++++++++++ arch/x86/include/asm/elf.h | 2 ++ arch/x86/include/asm/vdso.h | 2 ++ 5 files changed, 23 insertions(+)
arch/x86/entry/vdso/vdso2c.c +2 -0
@@ -101,6 +101,8 @@ struct vdso_sym required_syms[] = { {"__kernel_sigreturn", true}, {"__kernel_rt_sigreturn", true}, {"int80_landing_pad", true}, + {"vdso32_rt_sigreturn_landing_pad", true}, + {"vdso32_sigreturn_landing_pad", true}, }; __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
arch/x86/entry/vdso/vdso32/sigreturn.S +2 -0
@@ -18,6 +18,7 @@ __kernel_sigreturn: movl $__NR_sigreturn, %eax SYSCALL_ENTER_KERNEL .LEND_sigreturn: +SYM_INNER_LABEL(vdso32_sigreturn_landing_pad, SYM_L_GLOBAL) nop .size __kernel_sigreturn,.-.LSTART_sigreturn
@@ -29,6 +30,7 @@ __kernel_rt_sigreturn: movl $__NR_rt_sigreturn, %eax SYSCALL_ENTER_KERNEL .LEND_rt_sigreturn: +SYM_INNER_LABEL(vdso32_rt_sigreturn_landing_pad, SYM_L_GLOBAL) nop .size __kernel_rt_sigreturn,.-.LSTART_rt_sigreturn .previous
arch/x86/entry/vdso/vma.c +15 -0
@@ -436,6 +436,21 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) }#endif +bool arch_syscall_is_vdso_sigreturn (struct pt_regs *regs) +{ +#if defined(CONFIG_X86_32 ) || defined(CONFIG_IA32_EMULATION ) +const struct vdso_image *image = current->mm->context.vdso_image; +unsigned long vdso = (unsigned long ) current->mm->context.vdso; + +if (in_ia32_syscall () && image == &vdso_image_32) { +if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad || + regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad) +return true; + } +#endif +return false; +} +#ifdef CONFIG_X86_64 static __init int vdso_setup (char *s) {
arch/x86/include/asm/elf.h +2 -0
@@ -388,6 +388,8 @@ extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,compat_arch_setup_additional_pages (bprm, interpreter, \ (ex->e_machine == EM_X86_64)) +extern bool arch_syscall_is_vdso_sigreturn (struct pt_regs *regs); +/* Do not change the values. See get_align_mask() */ enum align_flags {ALIGN_VA_32 =BIT (0 ),
arch/x86/include/asm/vdso.h +2 -0
@@ -29,6 +29,8 @@ struct vdso_image {long sym___kernel_rt_sigreturn;long sym___kernel_vsyscall;long sym_int80_landing_pad; +long sym_vdso32_sigreturn_landing_pad; +long sym_vdso32_rt_sigreturn_landing_pad; };#ifdef CONFIG_X86_64
Signature · 1 line
2.29.2
[tip: core/entry] x86: vdso: Expose sigreturn address on vdso to the kernel
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: c5c878125ad5aca199dfc10b1af4010165aaa596 Gitweb: https://git.kernel.org/tip/c5c878125ad5aca199dfc10b1af4010165aaa596 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:32 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:16 +01:00
x86: vdso: Expose sigreturn address on vdso to the kernel
Syscall user redirection requires the signal trampoline code to not be captured, in order to support returning with a locked selector while avoiding recursion back into the signal handler. For ia-32, which has the trampoline in the vDSO, expose the entry points to the kernel, such that it can avoid dispatching syscalls from that region to userspace.
--- arch/x86/entry/vdso/vdso2c.c | 2 ++ arch/x86/entry/vdso/vdso32/sigreturn.S | 2 ++ arch/x86/entry/vdso/vma.c | 15 +++++++++++++++ arch/x86/include/asm/elf.h | 2 ++ arch/x86/include/asm/vdso.h | 2 ++ 5 files changed, 23 insertions(+)
arch/x86/entry/vdso/vdso2c.c +2 -0
@@ -101,6 +101,8 @@ struct vdso_sym required_syms[] = { {"__kernel_sigreturn", true}, {"__kernel_rt_sigreturn", true}, {"int80_landing_pad", true}, + {"vdso32_rt_sigreturn_landing_pad", true}, + {"vdso32_sigreturn_landing_pad", true}, }; __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
arch/x86/entry/vdso/vdso32/sigreturn.S +2 -0
@@ -18,6 +18,7 @@ __kernel_sigreturn: movl $__NR_sigreturn, %eax SYSCALL_ENTER_KERNEL .LEND_sigreturn: +SYM_INNER_LABEL(vdso32_sigreturn_landing_pad, SYM_L_GLOBAL) nop .size __kernel_sigreturn,.-.LSTART_sigreturn
@@ -29,6 +30,7 @@ __kernel_rt_sigreturn: movl $__NR_rt_sigreturn, %eax SYSCALL_ENTER_KERNEL .LEND_rt_sigreturn: +SYM_INNER_LABEL(vdso32_rt_sigreturn_landing_pad, SYM_L_GLOBAL) nop .size __kernel_rt_sigreturn,.-.LSTART_rt_sigreturn .previous
arch/x86/entry/vdso/vma.c +15 -0
@@ -436,6 +436,21 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) }#endif +bool arch_syscall_is_vdso_sigreturn (struct pt_regs *regs) +{ +#if defined(CONFIG_X86_32 ) || defined(CONFIG_IA32_EMULATION ) +const struct vdso_image *image = current->mm->context.vdso_image; +unsigned long vdso = (unsigned long ) current->mm->context.vdso; + +if (in_ia32_syscall () && image == &vdso_image_32) { +if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad || + regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad) +return true; + } +#endif +return false; +} +#ifdef CONFIG_X86_64 static __init int vdso_setup (char *s) {
arch/x86/include/asm/elf.h +2 -0
@@ -388,6 +388,8 @@ extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,compat_arch_setup_additional_pages (bprm, interpreter, \ (ex->e_machine == EM_X86_64)) +extern bool arch_syscall_is_vdso_sigreturn (struct pt_regs *regs); +/* Do not change the values. See get_align_mask() */ enum align_flags {ALIGN_VA_32 =BIT (0 ),
arch/x86/include/asm/vdso.h +2 -0
@@ -27,6 +27,8 @@ struct vdso_image {long sym___kernel_rt_sigreturn;long sym___kernel_vsyscall;long sym_int80_landing_pad; +long sym_vdso32_sigreturn_landing_pad; +long sym_vdso32_rt_sigreturn_landing_pad; };#ifdef CONFIG_X86_64
PATCH v8 2/7 signal: Expose SYS_USER_DISPATCH si_code type
In reply to Gabriel Krisman BertaziSYS_USER_DISPATCH will be triggered when a syscall is sent to userspace by the Syscall User Dispatch mechanism. This adjusts eventual BUILD_BUG_ON around the tree.
--- arch/x86/kernel/signal_compat.c | 2 +- include/uapi/asm-generic/siginfo.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
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 );
include/uapi/asm-generic/siginfo.h +2 -1
@@ -286,7 +286,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
Signature · 1 line
2.29.2
[tip: core/entry] signal: Expose SYS_USER_DISPATCH si_code type
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 1d7637d89cfce54a4f4a41c2325288c2f47470e8 Gitweb: https://git.kernel.org/tip/1d7637d89cfce54a4f4a41c2325288c2f47470e8 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:33 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:16 +01:00
signal: Expose SYS_USER_DISPATCH si_code type
SYS_USER_DISPATCH will be triggered when a syscall is sent to userspace by the Syscall User Dispatch mechanism. This adjusts eventual BUILD_BUG_ON around the tree.
--- arch/x86/kernel/signal_compat.c | 2 +- include/uapi/asm-generic/siginfo.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
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 );
include/uapi/asm-generic/siginfo.h +2 -1
@@ -286,7 +286,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
PATCH v8 3/7 kernel: Implement selective syscall userspace redirection
In reply to Gabriel Krisman BertaziIntroduce 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>, <off>, <length>, [selector])
The range [<offset>,<offset>+<length>) 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.
---
Changes since v7:
- Correct half-open interval in commit message (PeterZ)
- Solve rebase conflicts
Changes since v6:
(Matthew Wilcox)
- Use unsigned long for mode
(peterZ)
- Change interface to {offset,len}
- Use SYSCALL_WORK interface instead of TIF flags
Changes since v4:
(Andy Lutomirski)
- Allow sigreturn coming from vDSO
- Exit with SIGSYS instead of SIGSEGV on bad selector (Thomas Gleixner)
- Use sizeof selector in access_ok
- Document usage of __get_user
- Use constant for state value
- Split out x86 parts
- Rebase on top of Gleixner's common entry code
- Don't expose do_syscall_user_dispatch
Changes since v3:
- NTR.
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
--- fs/exec.c | 3 + include/linux/sched.h | 2 + include/linux/syscall_user_dispatch.h | 40 ++++++++++ include/linux/thread_info.h | 2 + include/uapi/linux/prctl.h | 5 ++ kernel/entry/Makefile | 2 +- kernel/entry/common.h | 16 ++++ kernel/entry/syscall_user_dispatch.c | 102 ++++++++++++++++++++++++++ kernel/fork.c | 1 + kernel/sys.c | 5 ++ 10 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 include/linux/syscall_user_dispatch.h create mode 100644 kernel/entry/common.h create mode 100644 kernel/entry/syscall_user_dispatch.c
fs/exec.c +3 -0
@@ -64,6 +64,7 @@#include <linux/compat.h> #include <linux/vmalloc.h> #include <linux/io_uring.h> +#include <linux/syscall_user_dispatch.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -1302,6 +1303,8 @@ int begin_new_exec(struct linux_binprm * bprm)flush_thread (); me->personality &= ~bprm->per_clear; +clear_syscall_work_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 +2 -0
@@ -34,6 +34,7 @@#include <linux/rseq.h> #include <linux/seqlock.h> #include <linux/kcsan.h> +#include <linux/syscall_user_dispatch.h> #include <asm/kmap_size.h> /* task_struct member predeclarations (sorted alphabetically): */
@@ -1000,6 +1001,7 @@ struct task_struct {unsigned int sessionid;#endif struct seccomp seccomp; +struct syscall_user_dispatch syscall_dispatch;/* Thread group tracking: */ u64 parent_exec_id;
include/linux/syscall_user_dispatch.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020 Collabora Ltd. +*/ +#ifndef _SYSCALL_USER_DISPATCH_H +#define _SYSCALL_USER_DISPATCH_H + +#include <linux/thread_info.h> + +#ifdef CONFIG_GENERIC_ENTRY + +struct syscall_user_dispatch { +char __user *selector; +unsigned long offset; +unsigned long len; +bool on_dispatch; +}; + +int set_syscall_user_dispatch (unsigned long mode,unsigned long offset, +unsigned long len,char __user *selector); + +#define clear_syscall_work_syscall_user_dispatch (tsk) \ + clear_task_syscall_work(tsk, SYSCALL_USER_DISPATCH) + +#else +struct syscall_user_dispatch {}; + +static inline int set_syscall_user_dispatch (unsigned long mode,unsigned long offset, +unsigned long len,char __user *selector) +{ +return -EINVAL ; +} + +static inline void clear_syscall_work_syscall_user_dispatch (struct task_struct *tsk) +{ +} + +#endif /* CONFIG_GENERIC_ENTRY */ + +#endif /* _SYSCALL_USER_DISPATCH_H */
include/linux/thread_info.h +2 -0
@@ -42,6 +42,7 @@ enum syscall_work_bit {SYSCALL_WORK_BIT_SYSCALL_TRACE ,SYSCALL_WORK_BIT_SYSCALL_EMU ,SYSCALL_WORK_BIT_SYSCALL_AUDIT , + SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH, };#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -49,6 +50,7 @@ enum syscall_work_bit {#define SYSCALL_WORK_SYSCALL_TRACE BIT(SYSCALL_WORK_BIT_SYSCALL_TRACE)#define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)#define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT) +#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)#endif #include <asm/thread_info.h>
include/uapi/linux/prctl.h +5 -0
@@ -247,4 +247,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/entry/Makefile +1 -1
@@ -9,5 +9,5 @@ KCOV_INSTRUMENT := n CFLAGS_REMOVE_common.o = -fstack-protector -fstack-protector-strong CFLAGS_common.o += -fno-stack-protector -obj-$(CONFIG_GENERIC_ENTRY) += common.o +obj-$(CONFIG_GENERIC_ENTRY) += common.o syscall_user_dispatch.o obj-$(CONFIG_KVM_XFER_TO_GUEST_WORK) += kvm.o
kernel/entry/common.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _COMMON_H +#define _COMMON_H + +bool do_syscall_user_dispatch (struct pt_regs *regs); + +static inline bool on_syscall_dispatch (void ) +{ +if (unlikely (current->syscall_dispatch.on_dispatch)) { + current->syscall_dispatch.on_dispatch = false; +return true; + } +return false; +} + +#endif
kernel/entry/syscall_user_dispatch.c new file +102 -0
102 lines not on this page · raw message
kernel/fork.c +1 -0
@@ -906,6 +906,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_syscall_work_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>
@@ -2530,6 +2531,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 (arg2, arg3, arg4, + (char __user *) arg5); +break ;default : error = -EINVAL ;break ;
Signature · 1 line
2.29.2
Re: [PATCH v8 3/7] kernel: Implement selective syscall userspace redirection
Quoted 44 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>, <off>, <length>, [selector])The range [<offset>,<offset>+<length>) 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.
Quoted 6 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]>
Signature · 1 line
Kees Cook
[tip: core/entry] kernel: Implement selective syscall userspace redirection
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 7a2cc679e8bb5bfdeef28ec80dc3c34edbc099fd Gitweb: https://git.kernel.org/tip/7a2cc679e8bb5bfdeef28ec80dc3c34edbc099fd Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:34 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:17 +01:00
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>, <off>, <length>, [selector])
The range [<offset>,<offset>+<length>) 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.
--- fs/exec.c | 3 +- include/linux/sched.h | 2 +- include/linux/syscall_user_dispatch.h | 40 ++++++++++- include/linux/thread_info.h | 2 +- include/uapi/linux/prctl.h | 5 +- kernel/entry/Makefile | 2 +- kernel/entry/common.h | 7 ++- kernel/entry/syscall_user_dispatch.c | 102 +++++++++++++++++++++++++- kernel/fork.c | 1 +- kernel/sys.c | 5 +- 10 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 include/linux/syscall_user_dispatch.h create mode 100644 kernel/entry/common.h create mode 100644 kernel/entry/syscall_user_dispatch.c
fs/exec.c +3 -0
@@ -64,6 +64,7 @@#include <linux/compat.h> #include <linux/vmalloc.h> #include <linux/io_uring.h> +#include <linux/syscall_user_dispatch.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -1302,6 +1303,8 @@ int begin_new_exec(struct linux_binprm * bprm)flush_thread (); me->personality &= ~bprm->per_clear; +clear_syscall_work_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 +2 -0
@@ -28,6 +28,7 @@#include <linux/sched/prio.h> #include <linux/sched/types.h> #include <linux/signal_types.h> +#include <linux/syscall_user_dispatch.h> #include <linux/mm_types_task.h> #include <linux/task_io_accounting.h> #include <linux/posix-timers.h>
@@ -965,6 +966,7 @@ struct task_struct {unsigned int sessionid;#endif struct seccomp seccomp; +struct syscall_user_dispatch syscall_dispatch;/* Thread group tracking: */ u64 parent_exec_id;
include/linux/syscall_user_dispatch.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020 Collabora Ltd. +*/ +#ifndef _SYSCALL_USER_DISPATCH_H +#define _SYSCALL_USER_DISPATCH_H + +#include <linux/thread_info.h> + +#ifdef CONFIG_GENERIC_ENTRY + +struct syscall_user_dispatch { +char __user *selector; +unsigned long offset; +unsigned long len; +bool on_dispatch; +}; + +int set_syscall_user_dispatch (unsigned long mode,unsigned long offset, +unsigned long len,char __user *selector); + +#define clear_syscall_work_syscall_user_dispatch (tsk) \ + clear_task_syscall_work(tsk, SYSCALL_USER_DISPATCH) + +#else +struct syscall_user_dispatch {}; + +static inline int set_syscall_user_dispatch (unsigned long mode,unsigned long offset, +unsigned long len,char __user *selector) +{ +return -EINVAL ; +} + +static inline void clear_syscall_work_syscall_user_dispatch (struct task_struct *tsk) +{ +} + +#endif /* CONFIG_GENERIC_ENTRY */ + +#endif /* _SYSCALL_USER_DISPATCH_H */
include/linux/thread_info.h +2 -0
@@ -42,6 +42,7 @@ enum syscall_work_bit {SYSCALL_WORK_BIT_SYSCALL_TRACE ,SYSCALL_WORK_BIT_SYSCALL_EMU ,SYSCALL_WORK_BIT_SYSCALL_AUDIT , + SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH, };#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -49,6 +50,7 @@ enum syscall_work_bit {#define SYSCALL_WORK_SYSCALL_TRACE BIT(SYSCALL_WORK_BIT_SYSCALL_TRACE)#define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)#define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT) +#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)#endif #include <asm/thread_info.h>
include/uapi/linux/prctl.h +5 -0
@@ -247,4 +247,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/entry/Makefile +1 -1
@@ -9,5 +9,5 @@ KCOV_INSTRUMENT := n CFLAGS_REMOVE_common.o = -fstack-protector -fstack-protector-strong CFLAGS_common.o += -fno-stack-protector -obj-$(CONFIG_GENERIC_ENTRY) += common.o +obj-$(CONFIG_GENERIC_ENTRY) += common.o syscall_user_dispatch.o obj-$(CONFIG_KVM_XFER_TO_GUEST_WORK) += kvm.o
kernel/entry/common.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _COMMON_H +#define _COMMON_H + +bool syscall_user_dispatch (struct pt_regs *regs); + +#endif
kernel/entry/syscall_user_dispatch.c new file +102 -0
102 lines not on this page · raw message
kernel/fork.c +1 -0
@@ -906,6 +906,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_syscall_work_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>
@@ -2530,6 +2531,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 (arg2, arg3, arg4, + (char __user *) arg5); +break ;default : error = -EINVAL ;break ;
[tip: core/entry] kernel: Implement selective syscall userspace redirection
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 1446e1df9eb183fdf81c3f0715402f1d7595d4cb Gitweb: https://git.kernel.org/tip/1446e1df9eb183fdf81c3f0715402f1d7595d4cb Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:34 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 15:07:56 +01:00
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>, <off>, <length>, [selector])
The range [<offset>,<offset>+<length>) 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.
--- fs/exec.c | 3 +- include/linux/sched.h | 2 +- include/linux/syscall_user_dispatch.h | 40 ++++++++++- include/linux/thread_info.h | 2 +- include/uapi/linux/prctl.h | 5 +- kernel/entry/Makefile | 2 +- kernel/entry/common.h | 7 ++- kernel/entry/syscall_user_dispatch.c | 104 +++++++++++++++++++++++++- kernel/fork.c | 1 +- kernel/sys.c | 5 +- 10 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 include/linux/syscall_user_dispatch.h create mode 100644 kernel/entry/common.h create mode 100644 kernel/entry/syscall_user_dispatch.c
fs/exec.c +3 -0
@@ -64,6 +64,7 @@#include <linux/compat.h> #include <linux/vmalloc.h> #include <linux/io_uring.h> +#include <linux/syscall_user_dispatch.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -1302,6 +1303,8 @@ int begin_new_exec(struct linux_binprm * bprm)flush_thread (); me->personality &= ~bprm->per_clear; +clear_syscall_work_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 +2 -0
@@ -28,6 +28,7 @@#include <linux/sched/prio.h> #include <linux/sched/types.h> #include <linux/signal_types.h> +#include <linux/syscall_user_dispatch.h> #include <linux/mm_types_task.h> #include <linux/task_io_accounting.h> #include <linux/posix-timers.h>
@@ -965,6 +966,7 @@ struct task_struct {unsigned int sessionid;#endif struct seccomp seccomp; +struct syscall_user_dispatch syscall_dispatch;/* Thread group tracking: */ u64 parent_exec_id;
include/linux/syscall_user_dispatch.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020 Collabora Ltd. +*/ +#ifndef _SYSCALL_USER_DISPATCH_H +#define _SYSCALL_USER_DISPATCH_H + +#include <linux/thread_info.h> + +#ifdef CONFIG_GENERIC_ENTRY + +struct syscall_user_dispatch { +char __user *selector; +unsigned long offset; +unsigned long len; +bool on_dispatch; +}; + +int set_syscall_user_dispatch (unsigned long mode,unsigned long offset, +unsigned long len,char __user *selector); + +#define clear_syscall_work_syscall_user_dispatch (tsk) \ + clear_task_syscall_work(tsk, SYSCALL_USER_DISPATCH) + +#else +struct syscall_user_dispatch {}; + +static inline int set_syscall_user_dispatch (unsigned long mode,unsigned long offset, +unsigned long len,char __user *selector) +{ +return -EINVAL ; +} + +static inline void clear_syscall_work_syscall_user_dispatch (struct task_struct *tsk) +{ +} + +#endif /* CONFIG_GENERIC_ENTRY */ + +#endif /* _SYSCALL_USER_DISPATCH_H */
include/linux/thread_info.h +2 -0
@@ -42,6 +42,7 @@ enum syscall_work_bit {SYSCALL_WORK_BIT_SYSCALL_TRACE ,SYSCALL_WORK_BIT_SYSCALL_EMU ,SYSCALL_WORK_BIT_SYSCALL_AUDIT , + SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH, };#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -49,6 +50,7 @@ enum syscall_work_bit {#define SYSCALL_WORK_SYSCALL_TRACE BIT(SYSCALL_WORK_BIT_SYSCALL_TRACE)#define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)#define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT) +#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)#endif #include <asm/thread_info.h>
include/uapi/linux/prctl.h +5 -0
@@ -247,4 +247,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/entry/Makefile +1 -1
@@ -9,5 +9,5 @@ KCOV_INSTRUMENT := n CFLAGS_REMOVE_common.o = -fstack-protector -fstack-protector-strong CFLAGS_common.o += -fno-stack-protector -obj-$(CONFIG_GENERIC_ENTRY) += common.o +obj-$(CONFIG_GENERIC_ENTRY) += common.o syscall_user_dispatch.o obj-$(CONFIG_KVM_XFER_TO_GUEST_WORK) += kvm.o
kernel/entry/common.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _COMMON_H +#define _COMMON_H + +bool syscall_user_dispatch (struct pt_regs *regs); + +#endif
kernel/entry/syscall_user_dispatch.c new file +104 -0
104 lines not on this page · raw message
kernel/fork.c +1 -0
@@ -906,6 +906,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_syscall_work_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>
@@ -2530,6 +2531,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 (arg2, arg3, arg4, + (char __user *) arg5); +break ;default : error = -EINVAL ;break ;
Re: [PATCH v8 3/7] kernel: Implement selective syscall userspace redirection
In reply to Gabriel Krisman BertaziWhy does do_syscal_user_dispatch call do_exit(SIGSEGV) and do_exit(SIGSYS) instead of force_sig(SIGSEGV) and force_sig(SIGSYS)?
Looking at the code these cases are not expected to happen, so I would be surprised if userspace depends on any particular behaviour on the failure path so I think we can change this.
Is using do_exit in this way something you copied from seccomp?
The reason I am asking is that by using do_exit you deprive userspace of the change to catch the signal handler and try and fix things.
Also by using do_exit only a single thread of a multi-thread application is terminated which seems wrong.
I am asking because I am going through the callers of do_exit so I can refactor things and clean things up and this use just looks wrong.
Gabriel Krisman Bertazi <[email protected]> writes:
<snip>
Quoted 8 lines
kernel/entry/syscall_user_dispatch.c new file +8
-0+bool do_syscall_user_dispatch (struct pt_regs *regs) +{ +struct syscall_user_dispatch *sd = ¤t->syscall_dispatch; +char state; + +if (likely (instruction_pointer (regs) - sd->offset < sd->len)) +return false; +
kernel/entry/syscall_user_dispatch.c line 49
Quoted 10 lines
kernel/entry/syscall_user_dispatch.c new file +10
-0+if (unlikely (arch_syscall_is_vdso_sigreturn (regs))) +return false; + +if (likely (sd->selector)) { +/* +* access_ok() is performed once, at prctl time, when +* the selector is loaded by userspace. +*/ +if (unlikely (__get_user (state, sd->selector))) +do_exit (SIGSEGV );
^^^^^^^^^^^^^^^^
I think it makes more sense if the code does:
if (unlikely(__get_user(state, sd->selector))) {
force_sig(SIGSEGV);
return true;
}kernel/entry/syscall_user_dispatch.c line 55
Quoted 6 lines
kernel/entry/syscall_user_dispatch.c new file +6
-0+ +if (likely (state ==PR_SYS_DISPATCH_OFF )) +return false; + +if (state !=PR_SYS_DISPATCH_ON ) +do_exit (SIGSYS );
^^^^^^^^^^^^^^^
kernel/entry/syscall_user_dispatch.c line 63
Quoted 8 lines
kernel/entry/syscall_user_dispatch.c new file +8
-0+ } + + sd->on_dispatch = true; +syscall_rollback (current, regs); +trigger_sigsys (regs); + +return true; +}
Eric
Re: [PATCH v8 3/7] kernel: Implement selective syscall userspace redirection
Quoted 6 lines · [email protected] (Eric W. Biederman) writes:
Why does
do_syscal_user_dispatchcalldo_exit(SIGSEGV)anddo_exit(SIGSYS)instead offorce_sig(SIGSEGV)andforce_sig(SIGSYS)?Looking at the code these cases are not expected to happen, so I would be surprised if userspace depends on any particular behaviour on the failure path so I think we can change this.
Hi Eric,
There is not really a good reason, and the use case that originated the feature doesn't rely on it.
Unless I'm missing yet another problem and others correct me, I think it makes sense to change it as you described.
Quoted 1 line
Is using
do_exitin this way something you copied from seccomp?
I'm not sure, its been a while, but I think it might be just that. The first prototype of SUD was implemented as a seccomp mode.
Quoted 8 lines
The reason I am asking is that by using
do_exityou deprive userspace of the change to catch the signal handler and try and fix things.Also by using
do_exitonly a single thread of a multi-thread application is terminated which seems wrong.I am asking because I am going through the callers of
do_exitso I can refactor things and clean things up and this use just looks wrong.
Thanks,
Signature · 1 line
Gabriel Krisman Bertazi
PATCH v8 4/7 entry: Support Syscall User Dispatch on common syscall entry
In reply to Gabriel Krisman BertaziSyscall User Dispatch (SUD) must take precedence over seccomp and ptrace, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for to trace, or the syscall will be executed, and seccomp/ptrace will execute next.
Since SUD runs before tracepoints, it needs to be a SYSCALL_WORK_EXIT as well, just to prevent a trace exit event when dispatch was triggered. For that, the on_syscall_dispatch() examines context to skip the tracepoint, audit and other work.
---
Changes since v6:
- Update do_syscall_intercept signature (Christian Brauner)
- Move it to before tracepoints
- Use SYSCALL_WORK flags
--- include/linux/entry-common.h | 2 ++ kernel/entry/common.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+)
include/linux/entry-common.h +2 -0
@@ -44,10 +44,12 @@ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_EMU | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_ENTER)#define SYSCALL_WORK_EXIT (SYSCALL_WORK_SYSCALL_TRACEPOINT | \ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_EXIT)/*
kernel/entry/common.c +17 -0
@@ -6,6 +6,8 @@#include <linux/livepatch.h> #include <linux/audit.h> +#include "common.h" +#define CREATE_TRACE_POINTS #include <trace/events/syscalls.h>
@@ -47,6 +49,16 @@ static long syscall_trace_enter(struct pt_regs *regs, long syscall, {long ret =0 ; +/* +* Handle Syscall User Dispatch. This must comes first, since +* the ABI here can be something that doesn't make sense for +* other syscall_work features. +*/ +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (do_syscall_user_dispatch (regs)) +return -1L ; + } +/* Handle ptrace */ if (work & (SYSCALL_WORK_SYSCALL_TRACE |SYSCALL_WORK_SYSCALL_EMU )) { ret =arch_syscall_enter_tracehook (regs);
@@ -232,6 +244,11 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long work) {bool step; +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (on_syscall_dispatch ()) +return ; + } +audit_syscall_exit (regs);if (work &SYSCALL_WORK_SYSCALL_TRACEPOINT )
Signature · 1 line
2.29.2
Re: [PATCH v8 4/7] entry: Support Syscall User Dispatch on common syscall entry
Quoted 8 lines · Gabriel Krisman Bertazi
Syscall User Dispatch (SUD) must take precedence over seccomp and ptrace, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for to trace, or the syscall will be executed, and seccomp/ptrace will execute next.
Quoted 6 lines
Since SUD runs before tracepoints, it needs to be a
SYSCALL_WORK_EXITas well, just to prevent a trace exit event when dispatch was triggered. For that, theon_syscall_dispatch()examines context to skip the tracepoint, audit and other work.Signed-off-by Gabriel Krisman Bertazi <[email protected]>
Signature · 1 line
Kees Cook
Andy Lutomirski
maintainerRe: [PATCH v8 4/7] entry: Support Syscall User Dispatch on common syscall entry
In reply to Gabriel Krisman BertaziQuoted 74 lines · Gabriel Krisman Bertazi
Syscall User Dispatch (SUD) must take precedence over seccomp and ptrace, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for to trace, or the syscall will be executed, and seccomp/ptrace will execute next.
Since SUD runs before tracepoints, it needs to be a
SYSCALL_WORK_EXITas well, just to prevent a trace exit event when dispatch was triggered. For that, theon_syscall_dispatch()examines context to skip the tracepoint, audit and other work.Signed-off-by Gabriel Krisman Bertazi <[email protected]>---
Changes since v6:
- Updatedo_syscall_interceptsignature (Christian Brauner)
- Move it to before tracepoints
- UseSYSCALL_WORKflags--- include/linux/entry-common.h | 2 ++ kernel/entry/common.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+)include/linux/entry-common.h +2
-0@@ -44,10 +44,12 @@ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_EMU | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_ENTER)#define SYSCALL_WORK_EXIT (SYSCALL_WORK_SYSCALL_TRACEPOINT | \ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_EXIT)/* kernel/entry/common.c +12
-0@@ -6,6 +6,8 @@#include <linux/livepatch.h> #include <linux/audit.h> +#include "common.h" +#define CREATE_TRACE_POINTS #include <trace/events/syscalls.h> @@ -47,6 +49,16 @@ static long syscall_trace_enter(struct pt_regs *regs, long syscall, {long ret =0 ; +/* +* Handle Syscall User Dispatch. This must comes first, since +* the ABI here can be something that doesn't make sense for +* other syscall_work features. +*/ +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (do_syscall_user_dispatch (regs)) +return -1L ; + } +/* Handle ptrace */ if (work & (SYSCALL_WORK_SYSCALL_TRACE |SYSCALL_WORK_SYSCALL_EMU )) { ret =arch_syscall_enter_tracehook (regs);
kernel/entry/common.c static void syscall_exit_work(struct pt_regs *regs, unsigned long work) · line 250
Quoted 7 lines
kernel/entry/common.c +4
-0@@ -232,6 +244,11 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long work) {bool step; +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (on_syscall_dispatch ()) +return ; + }
I think this would be less confusing if you just open-coded the body of on_syscall_dispatch here and got rid of the helper.
--Andy
[tip: core/entry] entry: Support Syscall User Dispatch on common syscall entry
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 5a5c45c624b8851cbfd269d5b0a8856a2b728502 Gitweb: https://git.kernel.org/tip/5a5c45c624b8851cbfd269d5b0a8856a2b728502 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:35 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:17 +01:00
entry: Support Syscall User Dispatch on common syscall entry
Syscall User Dispatch (SUD) must take precedence over seccomp and ptrace, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for to trace, or the syscall will be executed, and seccomp/ptrace will execute next.
Since SUD runs before tracepoints, it needs to be a SYSCALL_WORK_EXIT as well, just to prevent a trace exit event when dispatch was triggered. For that, the on_syscall_dispatch() examines context to skip the tracepoint, audit and other work.
[ tglx: Add a comment on the exit side ]
--- include/linux/entry-common.h | 2 ++ kernel/entry/common.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/linux/entry-common.h +2 -0
@@ -44,10 +44,12 @@ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_EMU | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_ENTER)#define SYSCALL_WORK_EXIT (SYSCALL_WORK_SYSCALL_TRACEPOINT | \ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_EXIT)/*
kernel/entry/common.c +25 -0
@@ -5,6 +5,8 @@#include <linux/livepatch.h> #include <linux/audit.h> +#include "common.h" +#define CREATE_TRACE_POINTS #include <trace/events/syscalls.h>
@@ -46,6 +48,16 @@ static long syscall_trace_enter(struct pt_regs *regs, long syscall, {long ret =0 ; +/* +* Handle Syscall User Dispatch. This must comes first, since +* the ABI here can be something that doesn't make sense for +* other syscall_work features. +*/ +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (syscall_user_dispatch (regs)) +return -1L ; + } +/* Handle ptrace */ if (work & (SYSCALL_WORK_SYSCALL_TRACE |SYSCALL_WORK_SYSCALL_EMU )) { ret =arch_syscall_enter_tracehook (regs);
@@ -230,6 +242,19 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long work) {bool step; +/* +* If the syscall was rolled back due to syscall user dispatching, +* then the tracers below are not invoked for the same reason as +* the entry side was not invoked in syscall_trace_enter(): The ABI +* of these syscalls is unknown. +*/ +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (unlikely (current->syscall_dispatch.on_dispatch)) { + current->syscall_dispatch.on_dispatch = false; +return ; + } + } +audit_syscall_exit (regs);if (work &SYSCALL_WORK_SYSCALL_TRACEPOINT )
[tip: core/entry] entry: Support Syscall User Dispatch on common syscall entry
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 11894468e39def270199f845b76df6c36d4ed133 Gitweb: https://git.kernel.org/tip/11894468e39def270199f845b76df6c36d4ed133 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:35 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 15:07:56 +01:00
entry: Support Syscall User Dispatch on common syscall entry
Syscall User Dispatch (SUD) must take precedence over seccomp and ptrace, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for to trace, or the syscall will be executed, and seccomp/ptrace will execute next.
Since SUD runs before tracepoints, it needs to be a SYSCALL_WORK_EXIT as well, just to prevent a trace exit event when dispatch was triggered. For that, the on_syscall_dispatch() examines context to skip the tracepoint, audit and other work.
[ tglx: Add a comment on the exit side ]
--- include/linux/entry-common.h | 2 ++ kernel/entry/common.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/linux/entry-common.h +2 -0
@@ -44,10 +44,12 @@ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_EMU | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_ENTER)#define SYSCALL_WORK_EXIT (SYSCALL_WORK_SYSCALL_TRACEPOINT | \ SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_AUDIT | \ + SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ ARCH_SYSCALL_WORK_EXIT)/*
kernel/entry/common.c +25 -0
@@ -5,6 +5,8 @@#include <linux/livepatch.h> #include <linux/audit.h> +#include "common.h" +#define CREATE_TRACE_POINTS #include <trace/events/syscalls.h>
@@ -46,6 +48,16 @@ static long syscall_trace_enter(struct pt_regs *regs, long syscall, {long ret =0 ; +/* +* Handle Syscall User Dispatch. This must comes first, since +* the ABI here can be something that doesn't make sense for +* other syscall_work features. +*/ +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (syscall_user_dispatch (regs)) +return -1L ; + } +/* Handle ptrace */ if (work & (SYSCALL_WORK_SYSCALL_TRACE |SYSCALL_WORK_SYSCALL_EMU )) { ret =arch_syscall_enter_tracehook (regs);
@@ -230,6 +242,19 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long work) {bool step; +/* +* If the syscall was rolled back due to syscall user dispatching, +* then the tracers below are not invoked for the same reason as +* the entry side was not invoked in syscall_trace_enter(): The ABI +* of these syscalls is unknown. +*/ +if (work &SYSCALL_WORK_SYSCALL_USER_DISPATCH ) { +if (unlikely (current->syscall_dispatch.on_dispatch)) { + current->syscall_dispatch.on_dispatch = false; +return ; + } + } +audit_syscall_exit (regs);if (work &SYSCALL_WORK_SYSCALL_TRACEPOINT )
PATCH v8 5/7 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.
---
Changes since v6:
- Update selftests to reflect {offset,len} api change
Changes since v4:
- Update bad selector test to reflect change in API
Changes since v3:
- Sort entry in Makefile
- Add SPDX header
- Use __NR_syscalls if available
--- tools/testing/selftests/Makefile | 1 + .../syscall_user_dispatch/.gitignore | 3 + .../selftests/syscall_user_dispatch/Makefile | 9 + .../selftests/syscall_user_dispatch/config | 1 + .../syscall_user_dispatch/sud_test.c | 310 ++++++++++++++++++ 5 files changed, 324 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/sud_test.c
tools/testing/selftests/Makefile +1 -0
@@ -57,6 +57,7 @@ TARGETS += sparc64 TARGETS += splice TARGETS += static_keys TARGETS += sync +TARGETS += syscall_user_dispatch TARGETS += sysctl TARGETS += tc-testing TARGETS += timens
tools/testing/selftests/syscall_user_dispatch/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +sud_test +sud_benchmark
tools/testing/selftests/syscall_user_dispatch/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 +top_srcdir = ../../../.. +INSTALL_HDR_PATH = $(top_srcdir)/usr +LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ + +CFLAGS += -Wall -I$(LINUX_HDR_PATH) + +TEST_GEN_PROGS := sud_test +include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_GENERIC_ENTRY=y
tools/testing/selftests/syscall_user_dispatch/sud_test.c new file +310 -0
310 lines not on this page · raw message
Signature · 1 line
2.29.2
[tip: core/entry] selftests: Add kselftest for syscall user dispatch
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: c33fb498e133d8d059ad779d621d6a64315ca746 Gitweb: https://git.kernel.org/tip/c33fb498e133d8d059ad779d621d6a64315ca746 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:36 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:17 +01:00
selftests: Add kselftest for syscall user dispatch
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.
--- tools/testing/selftests/Makefile | 1 +- tools/testing/selftests/syscall_user_dispatch/.gitignore | 3 +- tools/testing/selftests/syscall_user_dispatch/Makefile | 9 +- tools/testing/selftests/syscall_user_dispatch/config | 1 +- tools/testing/selftests/syscall_user_dispatch/sud_test.c | 310 +++++++- 5 files changed, 324 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/sud_test.c
tools/testing/selftests/Makefile +1 -0
@@ -56,6 +56,7 @@ TARGETS += sparc64 TARGETS += splice TARGETS += static_keys TARGETS += sync +TARGETS += syscall_user_dispatch TARGETS += sysctl TARGETS += tc-testing TARGETS += timens
tools/testing/selftests/syscall_user_dispatch/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +sud_test +sud_benchmark
tools/testing/selftests/syscall_user_dispatch/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 +top_srcdir = ../../../.. +INSTALL_HDR_PATH = $(top_srcdir)/usr +LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ + +CFLAGS += -Wall -I$(LINUX_HDR_PATH) + +TEST_GEN_PROGS := sud_test +include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_GENERIC_ENTRY=y
tools/testing/selftests/syscall_user_dispatch/sud_test.c new file +310 -0
310 lines not on this page · raw message
[tip: core/entry] selftests: Add kselftest for syscall user dispatch
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 179ef035992e89646e17138b18b130bb874b86bb Gitweb: https://git.kernel.org/tip/179ef035992e89646e17138b18b130bb874b86bb Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:36 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 15:07:56 +01:00
selftests: Add kselftest for syscall user dispatch
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.
--- tools/testing/selftests/Makefile | 1 +- tools/testing/selftests/syscall_user_dispatch/.gitignore | 3 +- tools/testing/selftests/syscall_user_dispatch/Makefile | 9 +- tools/testing/selftests/syscall_user_dispatch/config | 1 +- tools/testing/selftests/syscall_user_dispatch/sud_test.c | 310 +++++++- 5 files changed, 324 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/sud_test.c
tools/testing/selftests/Makefile +1 -0
@@ -56,6 +56,7 @@ TARGETS += sparc64 TARGETS += splice TARGETS += static_keys TARGETS += sync +TARGETS += syscall_user_dispatch TARGETS += sysctl TARGETS += tc-testing TARGETS += timens
tools/testing/selftests/syscall_user_dispatch/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +sud_test +sud_benchmark
tools/testing/selftests/syscall_user_dispatch/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 +top_srcdir = ../../../.. +INSTALL_HDR_PATH = $(top_srcdir)/usr +LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ + +CFLAGS += -Wall -I$(LINUX_HDR_PATH) + +TEST_GEN_PROGS := sud_test +include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_GENERIC_ENTRY=y
tools/testing/selftests/syscall_user_dispatch/sud_test.c new file +310 -0
310 lines not on this page · raw message
PATCH v8 6/7 selftests: Add benchmark for syscall user dispatch
In reply to Gabriel Krisman BertaziThis is the patch I'm using to evaluate the impact syscall user dispatch has on native syscall (syscalls not redirected to userspace) when enabled for the process and submiting syscalls though the unblocked dispatch selector. It works by running a step to define a baseline of the cost of executing sysinfo, then enabling SUD, and rerunning that step.
On my test machine, an AMD Ryzen 5 1500X, I have the following results with the latest version of syscall user dispatch patches.
root@olga:~# syscall_user_dispatch/sud_benchmark Calibrating test set to last ~5 seconds... test iterations = 37500000 Avg syscall time 134ns. Caught sys_ff00 trapped_call_count 1, native_call_count 0. Avg syscall time 147ns. Interception overhead: 9.7% (+13ns).
--- .../selftests/syscall_user_dispatch/Makefile | 2 +- .../syscall_user_dispatch/sud_benchmark.c | 200 ++++++++++++++++++ 2 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c
tools/testing/selftests/syscall_user_dispatch/Makefile +1 -1
@@ -5,5 +5,5 @@ LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ CFLAGS += -Wall -I$(LINUX_HDR_PATH) -TEST_GEN_PROGS := sud_test +TEST_GEN_PROGS := sud_test sud_benchmark include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c new file +200 -0
200 lines not on this page · raw message
Signature · 1 line
2.29.2
Re: [PATCH v8 6/7] selftests: Add benchmark for syscall user dispatch
Quoted 19 lines · Gabriel Krisman Bertazi
This is the patch I'm using to evaluate the impact syscall user dispatch has on native syscall (syscalls not redirected to userspace) when enabled for the process and submiting syscalls though the unblocked dispatch selector. It works by running a step to define a baseline of the cost of executing sysinfo, then enabling SUD, and rerunning that step.
On my test machine, an AMD Ryzen 5 1500X, I have the following results with the latest version of syscall user dispatch patches.
root@olga:~# syscall_user_dispatch/sud_benchmark Calibrating test set to last ~5 seconds... test iterations = 37500000 Avg syscall time 134ns. Caught sys_ff00 trapped_call_count 1, native_call_count 0. Avg syscall time 147ns. Interception overhead: 9.7% (+13ns).
Quoted 1 line
Signed-off-by Gabriel Krisman Bertazi <[email protected]>
Signature · 1 line
Kees Cook
[tip: core/entry] selftests: Add benchmark for syscall user dispatch
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 3e8df777fbf4531a4ec740aae991090baed27856 Gitweb: https://git.kernel.org/tip/3e8df777fbf4531a4ec740aae991090baed27856 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:37 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:17 +01:00
selftests: Add benchmark for syscall user dispatch
This is the patch I'm using to evaluate the impact syscall user dispatch has on native syscall (syscalls not redirected to userspace) when enabled for the process and submiting syscalls though the unblocked dispatch selector. It works by running a step to define a baseline of the cost of executing sysinfo, then enabling SUD, and rerunning that step.
On my test machine, an AMD Ryzen 5 1500X, I have the following results with the latest version of syscall user dispatch patches.
root@olga:~# syscall_user_dispatch/sud_benchmark Calibrating test set to last ~5 seconds... test iterations = 37500000 Avg syscall time 134ns. Caught sys_ff00 trapped_call_count 1, native_call_count 0. Avg syscall time 147ns. Interception overhead: 9.7% (+13ns).
--- tools/testing/selftests/syscall_user_dispatch/Makefile | 2 +- tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c | 200 +++++++- 2 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c
tools/testing/selftests/syscall_user_dispatch/Makefile +1 -1
@@ -5,5 +5,5 @@ LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ CFLAGS += -Wall -I$(LINUX_HDR_PATH) -TEST_GEN_PROGS := sud_test +TEST_GEN_PROGS := sud_test sud_benchmark include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c new file +200 -0
200 lines not on this page · raw message
[tip: core/entry] selftests: Add benchmark for syscall user dispatch
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: d87ae0fa21c26db2d7c66f22dee9c27ecda48ce2 Gitweb: https://git.kernel.org/tip/d87ae0fa21c26db2d7c66f22dee9c27ecda48ce2 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:37 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 15:07:57 +01:00
selftests: Add benchmark for syscall user dispatch
This is the patch I'm using to evaluate the impact syscall user dispatch has on native syscall (syscalls not redirected to userspace) when enabled for the process and submiting syscalls though the unblocked dispatch selector. It works by running a step to define a baseline of the cost of executing sysinfo, then enabling SUD, and rerunning that step.
On my test machine, an AMD Ryzen 5 1500X, I have the following results with the latest version of syscall user dispatch patches.
root@olga:~# syscall_user_dispatch/sud_benchmark Calibrating test set to last ~5 seconds... test iterations = 37500000 Avg syscall time 134ns. Caught sys_ff00 trapped_call_count 1, native_call_count 0. Avg syscall time 147ns. Interception overhead: 9.7% (+13ns).
--- tools/testing/selftests/syscall_user_dispatch/Makefile | 2 +- tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c | 200 +++++++- 2 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c
tools/testing/selftests/syscall_user_dispatch/Makefile +1 -1
@@ -5,5 +5,5 @@ LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ CFLAGS += -Wall -I$(LINUX_HDR_PATH) -TEST_GEN_PROGS := sud_test +TEST_GEN_PROGS := sud_test sud_benchmark include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c new file +200 -0
200 lines not on this page · raw message
PATCH v8 7/7 docs: Document Syscall User Dispatch
In reply to Gabriel Krisman BertaziExplain the interface, provide some background and security notes.
---
Changes since v7:
- Change process -> thread (Florian Weimer)
- Drop bogus reference to CONFIG_SYSCALL_USER_DISPATCH (me)
- Document the interval as a half-open interval (me)
--- .../admin-guide/syscall-user-dispatch.rst | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
Documentation/admin-guide/syscall-user-dispatch.rst new file +87 -0
@@ -0,0 +1,87 @@ +.. SPDX-License-Identifier: GPL-2.0 + +===================== +Syscall User Dispatch +===================== + +Background +---------- + +Compatibility layers like Wine need a way to efficiently emulate system +calls of only a part of their process - the part that has the +incompatible code - while being able to execute native syscalls without +a high performance penalty on the native part of the process. Seccomp +falls short on this task, since it has limited support to efficiently +filter syscalls based on memory regions, and it doesn't support removing +filters. Therefore a new mechanism is necessary. + +Syscall User Dispatch brings the filtering of the syscall dispatcher +address back to userspace. The application is in control of a flip +switch, indicating the current personality of the process. A +multiple-personality application can then flip the switch without +invoking the kernel, when crossing the compatibility layer API +boundaries, to enable/disable the syscall redirection and execute +syscalls directly (disabled) or send them to be emulated in userspace +through a SIGSYS. + +The goal of this design is to provide very quick compatibility layer +boundary crosses, which is achieved by not executing a syscall to change +personality every time the compatibility layer executes. Instead, a +userspace memory region exposed to the kernel indicates the current +personality, and the application simply modifies that variable to +configure the mechanism. + +There is a relatively high cost associated with handling signals on most +architectures, like x86, but at least for Wine, syscalls issued by +native Windows code are currently not known to be a performance problem, +since they are quite rare, at least for modern gaming applications. + +Since this mechanism is designed to capture syscalls issued by +non-native applications, it must function on syscalls whose invocation +ABI is completely unexpected to Linux. Syscall User Dispatch, therefore +doesn't rely on any of the syscall ABI to make the filtering. It uses +only the syscall dispatcher address and the userspace key. + +Interface +--------- + +A thread can setup this mechanism on supported kernels by executing the +following prctl: + + prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <offset>, <length>, [selector]) + +<op> is either PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF, to enable and +disable the mechanism globally for that thread. When +PR_SYS_DISPATCH_OFF is used, the other fields must be zero. + +[<offset>, <offset>+<length>) delimit a memory region interval +from which syscalls are always executed directly, regardless of the +userspace selector. This provides a fast path for the C library, which +includes the most common syscall dispatchers in the native code +applications, and also provides a way for the signal handler to return +without triggering a nested SIGSYS on (rt_)sigreturn. Users of this +interface should make sure that at least the signal trampoline code is +included in this region. In addition, for syscalls that implement the +trampoline code on the vDSO, that trampoline is never intercepted. + +[selector] is a pointer to a char-sized region in the process memory +region, that provides a quick way to enable disable syscall redirection +thread-wide, without the need to invoke the kernel directly. selector +can be set to PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF. Any other +value should terminate the program with a SIGSYS. + +Security Notes +-------------- + +Syscall User Dispatch provides functionality for compatibility layers to +quickly capture system calls issued by a non-native part of the +application, while not impacting the Linux native regions of the +process. It is not a mechanism for sandboxing system calls, and it +should not be seen as a security mechanism, since it is trivial for a +malicious application to subvert the mechanism by jumping to an allowed +dispatcher region prior to executing the syscall, or to discover the +address and modify the selector value. If the use case requires any +kind of security sandboxing, Seccomp should be used instead. + +Any fork or exec of the existing process resets the mechanism to +PR_SYS_DISPATCH_OFF.
Signature · 1 line
2.29.2
Jonathan Corbet
maintainerRe: [PATCH v8 7/7] docs: Document Syscall User Dispatch
Quoted 5 lines · On Fri, 27 Nov 2020 14:32:38 -0500 Gabriel Krisman Bertazi…
Explain the interface, provide some background and security notes.
Signed-off-by Gabriel Krisman Bertazi <[email protected]>Reviewed-by Kees Cook <[email protected]>
Nice to see documentation included...:) One nit:
Quoted 9 lines
---
Changes since v7:
- Change process -> thread (Florian Weimer)
- Drop bogus reference toCONFIG_SYSCALL_USER_DISPATCH(me)
- Document the interval as a half-open interval (me)--- .../admin-guide/syscall-user-dispatch.rst | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
You need to add this file to index.rst in that directory as well so it gets included in the docs build.
Thanks,
jon
Re: [PATCH v8 7/7] docs: Document Syscall User Dispatch
Quoted 20 lines · Jonathan Corbet
Quoted 5 lines · On Fri, 27 Nov 2020 14:32:38 -0500 Gabriel Krisman Bertazi…
Explain the interface, provide some background and security notes.
Signed-off-by Gabriel Krisman Bertazi <[email protected]>Reviewed-by Kees Cook <[email protected]>Nice to see documentation included...:) One nit:
Quoted 9 lines
---
Changes since v7:
- Change process -> thread (Florian Weimer)
- Drop bogus reference toCONFIG_SYSCALL_USER_DISPATCH(me)
- Document the interval as a half-open interval (me)--- .../admin-guide/syscall-user-dispatch.rst | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
Quoted 2 lines
You need to add this file to
index.rstin that directory as well so it gets included in the docs build.
Fixed that already after trying to build it :)
Re: [PATCH v8 7/7] docs: Document Syscall User Dispatch
In reply to Gabriel Krisman BertaziQuoted 24 lines · Gabriel Krisman Bertazi
Documentation/admin-guide/syscall-user-dispatch.rst new file +24
-0+Compatibility layers like Wine need a way to efficiently emulate system +calls of only a part of their process - the part that has the +incompatible code - while being able to execute native syscalls without +a high performance penalty on the native part of the process. Seccomp +falls short on this task, since it has limited support to efficiently +filter syscalls based on memory regions, and it doesn't support removing +filters. Therefore a new mechanism is necessary. + +Syscall User Dispatch brings the filtering of the syscall dispatcher +address back to userspace. The application is in control of a flip +switch, indicating the current personality of the process. A +multiple-personality application can then flip the switch without +invoking the kernel, when crossing the compatibility layer API +boundaries, to enable/disable the syscall redirection and execute +syscalls directly (disabled) or send them to be emulated in userspace +through a SIGSYS. + +The goal of this design is to provide very quick compatibility layer +boundary crosses, which is achieved by not executing a syscall to change +personality every time the compatibility layer executes. Instead, a +userspace memory region exposed to the kernel indicates the current +personality, and the application simply modifies that variable to +configure the mechanism. +
Documentation/admin-guide/syscall-user-dispatch.rst line 43
Quoted 10 lines
Documentation/admin-guide/syscall-user-dispatch.rst new file +10
-0+There is a relatively high cost associated with handling signals on most +architectures, like x86, but at least for Wine, syscalls issued by +native Windows code are currently not known to be a performance problem, +since they are quite rare, at least for modern gaming applications. + +Since this mechanism is designed to capture syscalls issued by +non-native applications, it must function on syscalls whose invocation +ABI is completely unexpected to Linux. Syscall User Dispatch, therefore +doesn't rely on any of the syscall ABI to make the filtering. It uses +only the syscall dispatcher address and the userspace key.
I think this lacks information about the non-visiblity of these syscalls. Something like this:
As the ABI of these intercepted syscalls is unknown to Linux, these syscalls are not instrumentable via ptrace or the syscall tracepoints.
I'll add that unless someone objects or comes up with better wording before I apply the lot tomorrow morning.
Thanks,
tglx
[tip: core/entry] docs: Document Syscall User Dispatch
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: a4cff1161486c47a5d303f913d5d2fcba26cc553 Gitweb: https://git.kernel.org/tip/a4cff1161486c47a5d303f913d5d2fcba26cc553 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:38 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 10:32:17 +01:00
docs: Document Syscall User Dispatch
Explain the interface, provide some background and security notes.
[ tglx: Add note about non-visibility, add it to the index and fix the
kerneldoc warning ]
--- Documentation/admin-guide/index.rst | 1 +- Documentation/admin-guide/syscall-user-dispatch.rst | 90 ++++++++++++- 2 files changed, 91 insertions(+) create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
Documentation/admin-guide/index.rst +1 -0
@@ -111,6 +111,7 @@ configure specific aspects of kernel behavior to your liking. rtc serial-console svga + syscall-user-dispatch sysrq thunderbolt ufs
Documentation/admin-guide/syscall-user-dispatch.rst new file +90 -0
90 lines not on this page · raw message
[tip: core/entry] docs: Document Syscall User Dispatch
In reply to Gabriel Krisman BertaziThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: a4452e671c6770e1bb80764f39995934067f70a0 Gitweb: https://git.kernel.org/tip/a4452e671c6770e1bb80764f39995934067f70a0 Author: Gabriel Krisman Bertazi <[email protected]> AuthorDate: Fri, 27 Nov 2020 14:32:38 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 02 Dec 2020 15:07:57 +01:00
docs: Document Syscall User Dispatch
Explain the interface, provide some background and security notes.
[ tglx: Add note about non-visibility, add it to the index and fix the
kerneldoc warning ]
--- Documentation/admin-guide/index.rst | 1 +- Documentation/admin-guide/syscall-user-dispatch.rst | 90 ++++++++++++- 2 files changed, 91 insertions(+) create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
Documentation/admin-guide/index.rst +1 -0
@@ -111,6 +111,7 @@ configure specific aspects of kernel behavior to your liking. rtc serial-console svga + syscall-user-dispatch sysrq thunderbolt ufs
Documentation/admin-guide/syscall-user-dispatch.rst new file +90 -0
90 lines not on this page · raw message
Quoted 11 lines · Gabriel Krisman Bertazi
Hi,
This is v8 of syscall user dispatch. Last version got some acks but there was one small documentation fix I wanted to do, as requested by Florian. This also addresses the commit message fixup Peter requested.
The only actual code change from v7 is solving a trivial merge conflict I myself created with the entry code fixup I made week and with something else in the TIP tree.
Quoted 9 lines
I also shared this with glibc and there wasn't any complaints other than the matter about user-notif vs. siginfo, which was discussed in v7 and the understanding is that it is not necessary now and can be added later, if needed, on the same infrastructure without a new api.
I'm not sure about TIP the rules, but is it too late to be queued for the next merge window? I'd love to have this in 5.11 if possible, since it has been flying for quite a while.
Other than my little nitpick about on_syscall_dispatch(), the whole series is: