Allow executing code and syscalls in another address space
Subsystems (11)
cover Allow executing code and syscalls in another address space
We already have process_vm_readv and process_vm_writev to read and write to a process memory faster than we can do this with ptrace. And now it is time for process_vm_exec that allows executing code in an address space of another process. We can do this with ptrace but it is much slower.
= Use-cases =
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use PTRACE_SYSEMU to trap system calls, but the process_vm_exec can significantly speed them up.
Another use-case is CRIU (Checkpoint/Restore in User-space). Several process properties can be received only from the process itself. Right now, we use a parasite code that is injected into the process. We do this with ptrace but it is slow, unsafe, and tricky. process_vm_exec can simplify the process of injecting a parasite code and it will allow pre-dump memory without stopping processes. The pre-dump here is when we enable a memory tracker and dump the memory while a process is continue running. On each interaction we dump memory that has been changed from the previous iteration. In the final step, we will stop processes and dump their full state. Right now the most effective way to dump process memory is to create a set of pipes and splice memory into these pipes from the parasite code. With process_vm_exec, we will be able to call vmsplice directly. It means that we will not need to stop a process to inject the parasite code.
= How it works =
process_vm_exec has two modes:
* Execute code in an address space of a target process and stop on any signal or system call.
* Execute a system call in an address space of a target process.
int process_vm_exec(pid_t pid, struct sigcontext uctx,
unsigned long flags, siginfo_t siginfo, sigset_t *sigmask, size_t sizemask)
PID - target process identification. We can consider to use pidfd instead of PID here.
sigcontext contains a process state with what the process will be resumed after switching the address space and then when a process will be stopped, its sate will be saved back to sigcontext.
siginfo is information about a signal that has interrupted the process. If a process is interrupted by a system call, signfo will contain a synthetic siginfo of the SIGSYS signal.
sigmask is a set of signals that process_vm_exec returns via signfo.
# How fast is it
In the fourth patch, you can find two benchmarks that execute a function that calls system calls in a loop. ptrace_vm_exe uses ptrace to trap system calls, proces_vm_exec uses the process_vm_exec syscall to do the same thing.
ptrace_vm_exec: 1446 ns/syscall ptrocess_vm_exec: 289 ns/syscall
PS: This version is just a prototype. Its goal is to collect the initial feedback, to discuss the interfaces, and maybe to get some advice on implementation..
Andrei Vagin (4):
signal: add a helper to restore a process state from sigcontex arch/x86: implement the process_vm_exec syscall arch/x86: allow to execute syscalls via process_vm_exec
selftests: add tests for process_vm_exec
arch/Kconfig | 15 ++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 19 +++ arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/include/asm/sigcontext.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/process_vm_exec.c | 160 ++++++++++++++++++ arch/x86/kernel/signal.c | 125 ++++++++++---- include/linux/entry-common.h | 2 + include/linux/process_vm_exec.h | 17 ++ include/linux/sched.h | 7 + include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 4 +- include/uapi/linux/process_vm_exec.h | 8 + kernel/entry/common.c | 2 +- kernel/fork.c | 9 + kernel/sys_ni.c | 2 + .../selftests/process_vm_exec/Makefile | 7 + tools/testing/selftests/process_vm_exec/log.h | 26 +++ .../process_vm_exec/process_vm_exec.c | 105 ++++++++++++ .../process_vm_exec/process_vm_exec_fault.c | 111 ++++++++++++ .../process_vm_exec/process_vm_exec_syscall.c | 81 +++++++++ .../process_vm_exec/ptrace_vm_exec.c | 111 ++++++++++++ 23 files changed, 785 insertions(+), 37 deletions(-) create mode 100644 arch/x86/kernel/process_vm_exec.c create mode 100644 include/linux/process_vm_exec.h create mode 100644 include/uapi/linux/process_vm_exec.h create mode 100644 tools/testing/selftests/process_vm_exec/Makefile create mode 100644 tools/testing/selftests/process_vm_exec/log.h create mode 100644 tools/testing/selftests/process_vm_exec/process_vm_exec.c create mode 100644 tools/testing/selftests/process_vm_exec/process_vm_exec_fault.c create mode 100644 tools/testing/selftests/process_vm_exec/process_vm_exec_syscall.c create mode 100644 tools/testing/selftests/process_vm_exec/ptrace_vm_exec.c
Signature · 7 lines
2.29.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 1/4 signal: add a helper to restore a process state from sigcontex
It will be used to implement process_vm_exec.
--- arch/x86/kernel/signal.c | 78 ++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 35 deletions(-)
arch/x86/kernel/signal.c +43 -35
@@ -79,51 +79,43 @@ static void force_valid_ss(struct pt_regs *regs)# define CONTEXT_COPY_SIZE sizeof(struct sigcontext)#endif -static int restore_sigcontext (struct pt_regs *regs, -struct sigcontext __user *usc, +static int __restore_sigcontext (struct pt_regs *regs, +struct sigcontext __user *sc,unsigned long uc_flags) { -struct sigcontext sc; - -/* Always make any pending restarted system calls return -EINTR */ - current->restart_block.fn = do_no_restart_syscall; - -if (copy_from_user (&sc, usc,CONTEXT_COPY_SIZE )) -return -EFAULT ; -#ifdef CONFIG_X86_32 -set_user_gs (regs, sc.gs); - regs->fs = sc.fs; - regs->es = sc.es; - regs->ds = sc.ds; +set_user_gs (regs, sc->gs); + regs->fs = sc->fs; + regs->es = sc->es; + regs->ds = sc->ds;#endif /* CONFIG_X86_32 */ - regs->bx = sc.bx; - regs->cx = sc.cx; - regs->dx = sc.dx; - regs->si = sc.si; - regs->di = sc.di; - regs->bp = sc.bp; - regs->ax = sc.ax; - regs->sp = sc.sp; - regs->ip = sc.ip; + regs->bx = sc->bx; + regs->cx = sc->cx; + regs->dx = sc->dx; + regs->si = sc->si; + regs->di = sc->di; + regs->bp = sc->bp; + regs->ax = sc->ax; + regs->sp = sc->sp; + regs->ip = sc->ip;#ifdef CONFIG_X86_64 - regs->r8 = sc.r8; - regs->r9 = sc.r9; - regs->r10 = sc.r10; - regs->r11 = sc.r11; - regs->r12 = sc.r12; - regs->r13 = sc.r13; - regs->r14 = sc.r14; - regs->r15 = sc.r15; + regs->r8 = sc->r8; + regs->r9 = sc->r9; + regs->r10 = sc->r10; + regs->r11 = sc->r11; + regs->r12 = sc->r12; + regs->r13 = sc->r13; + regs->r14 = sc->r14; + regs->r15 = sc->r15;#endif /* CONFIG_X86_64 */ /* Get CS/SS and force CPL3 */ - regs->cs = sc.cs |0x03 ; - regs->ss = sc.ss |0x03 ; + regs->cs = sc->cs |0x03 ; + regs->ss = sc->ss |0x03 ; - regs->flags = (regs->flags & ~FIX_EFLAGS ) | (sc.flags &FIX_EFLAGS ); + regs->flags = (regs->flags & ~FIX_EFLAGS ) | (sc->flags &FIX_EFLAGS );/* disable syscall checks */ regs->orig_ax =-1 ;
@@ -136,10 +128,26 @@ static int restore_sigcontext(struct pt_regs *regs,force_valid_ss (regs);#endif -return fpu__restore_sig ((void __user *)sc.fpstate, +return fpu__restore_sig ((void __user *)sc->fpstate,IS_ENABLED (CONFIG_X86_32 )); } +static int restore_sigcontext (struct pt_regs *regs, +struct sigcontext __user *usc, +unsigned long uc_flags) +{ +struct sigcontext sc; + +/* Always make any pending restarted system calls return -EINTR */ + current->restart_block.fn = do_no_restart_syscall; + +if (copy_from_user (&sc, usc,CONTEXT_COPY_SIZE )) +return -EFAULT ; + +return __restore_sigcontext (regs, &sc, uc_flags); +} + + static __always_inline int__unsafe_setup_sigcontext (struct sigcontext __user *sc,void __user *fpstate,struct pt_regs *regs,unsigned long mask)
Signature · 1 line
2.29.2
PATCH 2/4 arch/x86: implement the process_vm_exec syscall
In reply to Andrei VaginThis change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t * uinfo, sigset_t *sigmask, size_t sizemask)
process_vm_exec allows to execute the current process in an address space of another process.
process_vm_exec swaps the current address space with an address space of a specified process, sets a state from sigcontex and resumes the process. When a process receives a signal or calls a system call, process_vm_exec saves the process state back to sigcontext, restores the origin address space, restores the origin process state, and returns to userspace.
If it was interrupted by a signal and the signal is in the user_mask, the signal is dequeued and information about it is saved in uinfo. If process_vm_exec is interrupted by a system call, a synthetic siginfo for the SIGSYS signal is generated.
The behavior of this system call is similar to PTRACE_SYSEMU but everything is happing in the context of one process, so process_vm_exec shows a better performance.
PTRACE_SYSEMU is primarily used to implement sandboxes (application kernels) like User-mode Linux or gVisor. These type of sandboxes intercepts applications system calls and acts as the guest kernel. A simple benchmark, where a "tracee" process executes systems calls in a loop and a "tracer" process traps syscalls and handles them just incrementing the tracee instruction pointer to skip the syscall instruction shows that process_vm_exec works more than 5 times faster than PTRACE_SYSEMU.
--- arch/Kconfig | 15 +++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 16 +++ arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/include/asm/sigcontext.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/process_vm_exec.c | 133 +++++++++++++++++++++++++ arch/x86/kernel/signal.c | 47 +++++++++ include/linux/process_vm_exec.h | 15 +++ include/linux/sched.h | 7 ++ include/linux/syscalls.h | 6 ++ include/uapi/asm-generic/unistd.h | 4 +- kernel/fork.c | 9 ++ kernel/sys_ni.c | 2 + 14 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/process_vm_exec.c create mode 100644 include/linux/process_vm_exec.h
arch/Kconfig +15 -0
@@ -514,6 +514,21 @@ config SECCOMP_FILTER See Documentation/userspace-api/seccomp_filter.rst for details. +config HAVE_ARCH_PROCESS_VM_EXEC + bool + help + An arch should select this symbol to support the process_vm_exec system call. + +config PROCESS_VM_EXEC + prompt "Enable the process_vm_exec syscall" + def_bool y + depends on HAVE_ARCH_PROCESS_VM_EXEC + help + process_vm_exec allows executing code and system calls in a specified + address space. + + If unsure, say Y. + config HAVE_ARCH_STACKLEAK bool help
arch/x86/Kconfig +1 -0
@@ -27,6 +27,7 @@ config X86_64 select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 select ARCH_USE_CMPXCHG_LOCKREF select HAVE_ARCH_SOFT_DIRTY + select HAVE_ARCH_PROCESS_VM_EXEC select MODULES_USE_ELF_RELA select NEED_DMA_MAP_STATE select SWIOTLB
arch/x86/entry/common.c +16 -0
@@ -19,6 +19,7 @@#include <linux/nospec.h> #include <linux/syscalls.h> #include <linux/uaccess.h> +#include <linux/process_vm_exec.h> #ifdef CONFIG_XEN_PV #include <xen/xen-ops.h>
@@ -38,6 +39,21 @@#ifdef CONFIG_X86_64 __visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs) { +#ifdef CONFIG_PROCESS_VM_EXEC +if (current->exec_mm && current->exec_mm->ctx) { +kernel_siginfo_t info = { + .si_signo =SIGSYS , + .si_call_addr = (void __user *)KSTK_EIP (current), + .si_arch =syscall_get_arch (current), + .si_syscall = nr, + }; +restore_vm_exec_context (regs); + regs->ax =copy_siginfo_to_user (current->exec_mm->siginfo, &info); +syscall_exit_to_user_mode (regs); +return ; + } +#endif + nr =syscall_enter_from_user_mode (regs, nr);instrumentation_begin ();
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -362,6 +362,7 @@ 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise +441 64 process_vm_exec sys_process_vm_exec # # Due to a historical design error, certain syscalls are numbered differently
arch/x86/include/asm/sigcontext.h +2 -0
@@ -6,4 +6,6 @@#include <uapi/asm/sigcontext.h> +extern long swap_vm_exec_context (struct sigcontext __user *uctx); +#endif /* _ASM_X86_SIGCONTEXT_H */
arch/x86/kernel/Makefile +1 -0
@@ -163,3 +163,4 @@ ifeq ($(CONFIG_X86_64),y) endif obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT) += ima_arch.o +obj-$(CONFIG_PROCESS_VM_EXEC) += process_vm_exec.o
arch/x86/kernel/process_vm_exec.c new file +133 -0
133 lines not on this page · raw message
arch/x86/kernel/signal.c +47 -0
62 lines not on this page · raw message
include/linux/process_vm_exec.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PROCESS_VM_EXEC_H +#define _LINUX_PROCESS_VM_EXEC_H + +struct exec_mm { +struct sigcontext *ctx; +struct mm_struct *mm; +unsigned long flags; +sigset_t sigmask; +siginfo_t __user *siginfo; +}; + +void free_exec_mm_struct (struct task_struct *tsk); + +#endif
include/linux/sched.h +7 -0
@@ -64,6 +64,7 @@ struct signal_struct;struct task_delay_info ;struct task_group ;struct io_uring_task ; +struct exec_mm ;/* * Task state bitmask. NOTE! These bits are also
@@ -637,6 +638,8 @@ struct wake_q_node {struct wake_q_node *next; }; +struct exec_mm ; +struct task_struct {#ifdef CONFIG_THREAD_INFO_IN_TASK /*
@@ -757,6 +760,10 @@ struct task_struct {struct mm_struct *mm;struct mm_struct *active_mm; +#ifdef CONFIG_PROCESS_VM_EXEC +struct exec_mm *exec_mm; +#endif +/* Per-thread vma caching: */ struct vmacache vmacache;
include/linux/syscalls.h +6 -0
@@ -1347,4 +1347,10 @@ int __sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen);int __sys_setsockopt (int fd,int level,int optname,char __user *optval,int optlen); + +#ifdef CONFIG_PROCESS_VM_EXEC +void restore_vm_exec_context (struct pt_regs *regs); +#else +static inline void restore_vm_exec_context (struct pt_regs *regs) {} +#endif #endif
include/uapi/asm-generic/unistd.h +3 -1
@@ -859,9 +859,11 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) __SYSCALL(__NR_faccessat2, sys_faccessat2)#define __NR_process_madvise 440 __SYSCALL(__NR_process_madvise, sys_process_madvise) +#define __NR_process_madvise 441 +__SYSCALL(__NR_process_vm_exec, sys_process_vm_exec)#undef __NR_syscalls -#define __NR_syscalls 441 +#define __NR_syscalls 442/* * 32 bit systems traditionally used different
kernel/fork.c +9 -0
@@ -96,6 +96,7 @@#include <linux/kasan.h> #include <linux/scs.h> #include <linux/io_uring.h> +#include <linux/process_vm_exec.h> #include <asm/pgalloc.h> #include <linux/uaccess.h>
@@ -461,6 +462,9 @@ void free_task(struct task_struct *tsk)arch_release_task_struct (tsk);if (tsk->flags &PF_KTHREAD )free_kthread_struct (tsk); +#ifdef CONFIG_PROCESS_VM_EXEC +free_exec_mm_struct (tsk); +#endif free_task_struct (tsk); }EXPORT_SYMBOL (free_task);
@@ -943,6 +947,11 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)#ifdef CONFIG_MEMCG tsk->active_memcg =NULL ;#endif + +#ifdef CONFIG_PROCESS_VM_EXEC + tsk->exec_mm =NULL ; +#endif +return tsk; free_stack:
kernel/sys_ni.c +2 -0
@@ -350,6 +350,8 @@ COND_SYSCALL(pkey_mprotect);COND_SYSCALL (pkey_alloc);COND_SYSCALL (pkey_free); +/* execute in another address space */ +COND_SYSCALL (process_vm_exec);/* * Architecture specific weak syscall entries.
Signature · 7 lines
2.29.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 20 lines · Andrei Vagin
arch/x86/kernel/process_vm_exec.c new file +19
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); + +sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;
arch/x86/kernel/process_vm_exec.c line 44
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+switch_mm_irqs_off (active_mm, target_mm, tsk); +local_irq_enable (); +task_unlock (tsk); +#ifdef finish_arch_post_lock_switch +finish_arch_post_lock_switch (); +#endif + +if (active_mm != target_mm) +mmdrop (active_mm); +}
I think this should be unified with kthread_use_mm() somehow...
And does it really need the "prev_mm" argument? It must be tsk->mm, no?
Oleg.
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 25 lines · Oleg Nesterov
Quoted 24 lines · Andrei Vagin
arch/x86/kernel/process_vm_exec.c new file +23
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); + +sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm; +switch_mm_irqs_off (active_mm, target_mm, tsk); +local_irq_enable (); +task_unlock (tsk); +#ifdef finish_arch_post_lock_switch
Quoted 8 lines
arch/x86/kernel/process_vm_exec.c line 44
Quoted 6 lines
arch/x86/kernel/process_vm_exec.c new file +6
-0+finish_arch_post_lock_switch (); +#endif + +if (active_mm != target_mm) +mmdrop (active_mm); +}I think this should be unified with
kthread_use_mm()somehow...
I agree.
Quoted 2 lines
And does it really need the "
prev_mm" argument? It must betsk->mm, no?
No, it doesn't. It is leftover of unuse_mm. BTW why do we pass mm to kthread_unuse_mm?
Thanks,
Andrei.
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
In reply to Andrei VaginQuoted 6 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.
[...]
I still think that this whole API is fundamentally the wrong approach because it tries to shoehorn multiple usecases with different requirements into a single API. But that aside:
Quoted 9 lines
arch/x86/kernel/process_vm_exec.c new file +9
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); +
arch/x86/kernel/process_vm_exec.c line 34
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;
I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example, zap_threads() assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)
As far as I understand, only kthreads are allowed to do this (as implemented in kthread_use_mm()).
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+switch_mm_irqs_off (active_mm, target_mm, tsk); +local_irq_enable (); +task_unlock (tsk); +#ifdef finish_arch_post_lock_switch +finish_arch_post_lock_switch (); +#endif + +if (active_mm != target_mm) +mmdrop (active_mm); +}
Andy Lutomirski
maintainerRe: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 29 lines · Jann Horn
Quoted 6 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.[...]
I still think that this whole API is fundamentally the wrong approach because it tries to shoehorn multiple usecases with different requirements into a single API. But that aside:
Quoted 16 lines
arch/x86/kernel/process_vm_exec.c new file +16
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); + +sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm);
Quoted 8 lines
arch/x86/kernel/process_vm_exec.c line 34
Quoted 3 lines
arch/x86/kernel/process_vm_exec.c new file +3
-0+ tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)
exec_mmap() does it, so it can’t be entirely impossible.
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 39 lines · Andy Lutomirski
Quoted 29 lines · Jann Horn
Quoted 6 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.[...]
I still think that this whole API is fundamentally the wrong approach because it tries to shoehorn multiple usecases with different requirements into a single API. But that aside:
Quoted 16 lines
arch/x86/kernel/process_vm_exec.c new file +16
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); + +sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm);Quoted 8 lines
arch/x86/kernel/process_vm_exec.c line 34
Quoted 3 lines
arch/x86/kernel/process_vm_exec.c new file +3
-0+ tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)
Quoted 1 line
exec_mmap()does it, so it can’t be entirely impossible.
Yeah, true, execve can do it - I guess the thing that makes that special is that it's running after de_thread(), so it's guaranteed to be single-threaded?
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 40 lines · Jann Horn
Quoted 39 lines · Andy Lutomirski
Quoted 37 lines · Jann Horn
Quoted 6 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.[...]
I still think that this whole API is fundamentally the wrong approach because it tries to shoehorn multiple usecases with different requirements into a single API. But that aside:
Quoted 9 lines
arch/x86/kernel/process_vm_exec.c new file +9
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); +arch/x86/kernel/process_vm_exec.c line 34
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)
Quoted 5 lines
Quoted 1 line
exec_mmap()does it, so it can’t be entirely impossible.Yeah, true, execve can do it - I guess the thing that makes that special is that it's running after
de_thread(), so it's guaranteed to be single-threaded?
Even the implementation detail of swapping the mm aside. Even the idea of swaping the mm is completely broken, as an endless system calls depend upon the state held in task_struct. io_uring just tried running system calls of a process in a different context and we ultimately had to make the threads part of the original process to make enough things work to keep the problem tractable.
System calls deeply and fundamentally depend on task_struct and signal_struct.
I can think of two possibilities.
1) Hijack and existing process thread.
2) Inject a new thread into an existing process.
Anything else is just an exercise in trouble. Of this I think Hijacking an existing thread is the only one that won't require lots of tracking down of special cases. I seem to remember audit is still struggling with how to properly audit io_uring threads.
Eric
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 55 lines · Eric W. Biederman
Quoted 40 lines · Jann Horn
Quoted 39 lines · Andy Lutomirski
Quoted 37 lines · Jann Horn
Quoted 6 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.[...]
I still think that this whole API is fundamentally the wrong approach because it tries to shoehorn multiple usecases with different requirements into a single API. But that aside:
Quoted 9 lines
arch/x86/kernel/process_vm_exec.c new file +9
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); +arch/x86/kernel/process_vm_exec.c line 34
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)Quoted 5 lines
Quoted 1 line
exec_mmap()does it, so it can’t be entirely impossible.Yeah, true, execve can do it - I guess the thing that makes that special is that it's running after
de_thread(), so it's guaranteed to be single-threaded?Even the implementation detail of swapping the mm aside. Even the idea of swaping the mm is completely broken, as an endless system calls depend upon the state held in
task_struct.io_uringjust tried running system calls of a process in a different context and we ultimately had to make the threads part of the original process to make enough things work to keep the problem tractable.
Quoted 2 lines
System calls deeply and fundamentally depend on
task_structandsignal_struct.
In opposite to io_uring, process_vm_exec doesn't intend to run system calls in the context of the target process. We initially declare that system calls are executed in the context of the current process with just another mm. If we are talking about user-mode kernels, they will need just two system calls: mmap and munmap. In case of CRIU, vmsplice will be used too.
Quoted 4 lines
I can think of two possibilities.
1) Hijack and existing process thread.
2) Inject a new thread into an existing process.
I am not sure that I understand what you mean here, but it sounds like we will need to do a context switch to execute anything in a context of a hijacked thread. If I am right, it kills the main idea of process_vm_exec. If I misunderstand your idea, maybe you can describe it with more details.
Thanks,
Andrei
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
In reply to Jann HornQuoted 5 lines · Jann Horn
Quoted 4 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
Quoted 7 lines
Quoted 2 lines
process_vm_execallows to execute the current process in an address space of another process.[...]
I still think that this whole API is fundamentally the wrong approach because it tries to shoehorn multiple usecases with different requirements into a single API. But that aside:
Here, I can't agree with you, but this is discussed in the parallel thread.
Quoted 26 lines
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +9
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); +arch/x86/kernel/process_vm_exec.c line 34
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)
Quoted 2 lines
As far as I understand, only kthreads are allowed to do this (as implemented in
kthread_use_mm()).
kthread_use_mm() was renamed from use_mm in the v5.8 kernel. Before that, it wasn't used for user processes in the kernel, but it was exported for modules, and we used it without any visible problems. We understood that there could be some issues like zap_threads and it was one of reasons why we decided to introduce this system call.
I understand that there are no places in the kernel where we change mm of user threads back and forth, but are there any real concerns why we should not do that? I agree that zap_threads should be fixed, but it will the easy one.
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+switch_mm_irqs_off (active_mm, target_mm, tsk); +local_irq_enable (); +task_unlock (tsk); +#ifdef finish_arch_post_lock_switch +finish_arch_post_lock_switch (); +#endif + +if (active_mm != target_mm) +mmdrop (active_mm); +}
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 36 lines · Andrei Vagin
Quoted 26 lines · Jann Horn
Quoted 9 lines · Andrei Vagin
arch/x86/kernel/process_vm_exec.c new file +9
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); +arch/x86/kernel/process_vm_exec.c line 34
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)Quoted 2 lines
As far as I understand, only kthreads are allowed to do this (as implemented in
kthread_use_mm()).
kthread_use_mm()was renamed fromuse_mmin the v5.8 kernel. Before that, it wasn't used for user processes in the kernel, but it was exported for modules, and we used it without any visible problems. We understood that there could be some issues likezap_threadsand it was one of reasons why we decided to introduce this system call.
Quoted 4 lines
I understand that there are no places in the kernel where we change mm of user threads back and forth, but are there any real concerns why we should not do that? I agree that
zap_threadsshould be fixed, but it will the easy one.
My point is that if you break a preexisting assumption like this, you'll have to go through the kernel and search for places that rely on this assumption, and fix them up, which may potentially require thinking about what kinds of semantics would actually be appropriate there. Like the MCE killing logic (collect_procs_anon() and such). And current_is_single_threaded(), in which the current patch probably leads to logic security bugs. And __uprobe_perf_filter(). Before my refactoring of the ELF coredump logic in kernel 5.10 (commit b2767d97f5ff75 and the ones before it), you'd have also probably created memory corruption bugs in races between elf_core_dump() and syscalls like mmap()/munmap(). (Note that this is not necessarily an exhaustive list.)
Andy Lutomirski
maintainerRe: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 42 lines · Jann Horn
Quoted 36 lines · Andrei Vagin
Quoted 26 lines · Jann Horn
Quoted 9 lines · Andrei Vagin
arch/x86/kernel/process_vm_exec.c new file +9
-0+static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm) +{ +struct task_struct *tsk = current; +struct mm_struct *active_mm; + +task_lock (tsk); +/* Hold off tlb flush IPIs while switching mm's */ +local_irq_disable (); +arch/x86/kernel/process_vm_exec.c line 34
Quoted 10 lines
arch/x86/kernel/process_vm_exec.c new file +10
-0+sync_mm_rss (prev_mm); + +vmacache_flush (tsk); + + active_mm = tsk->active_mm; +if (active_mm != target_mm) { +mmgrab (target_mm); + tsk->active_mm = target_mm; + } + tsk->mm = target_mm;I'm pretty sure you're not currently allowed to overwrite the ->mm pointer of a userspace thread. For example,
zap_threads()assumes that all threads running under a process have the same ->mm. (And if you're fiddling with ->mm stuff, you should probably CC linux-mm@.)Quoted 2 lines
As far as I understand, only kthreads are allowed to do this (as implemented in
kthread_use_mm()).
kthread_use_mm()was renamed fromuse_mmin the v5.8 kernel. Before that, it wasn't used for user processes in the kernel, but it was exported for modules, and we used it without any visible problems. We understood that there could be some issues likezap_threadsand it was one of reasons why we decided to introduce this system call.Quoted 4 lines
I understand that there are no places in the kernel where we change mm of user threads back and forth, but are there any real concerns why we should not do that? I agree that
zap_threadsshould be fixed, but it will the easy one.
Quoted 13 lines
My point is that if you break a preexisting assumption like this, you'll have to go through the kernel and search for places that rely on this assumption, and fix them up, which may potentially require thinking about what kinds of semantics would actually be appropriate there. Like the MCE killing logic (
collect_procs_anon()and such). Andcurrent_is_single_threaded(), in which the current patch probably leads to logic security bugs. And__uprobe_perf_filter(). Before my refactoring of the ELF coredump logic in kernel 5.10 (commitb2767d97f5ff75and the ones before it), you'd have also probably created memory corruption bugs in races betweenelf_core_dump()and syscalls like mmap()/munmap(). (Note that this is not necessarily an exhaustive list.)
There’s nmi_uaccess_okay(), and its callers assume that, when a task is perf tracing itself, that an event on that task with nmi_uaccess_okay() means that uaccess will access that task’s memory.
Core dump code probably expects that dumping memory will access the correct mm.
I cannot fathom why any kind of remote vm access touched FPU state at all.
What PKRU value is supposed to be used when doing mm swap shenanigans? How about PASID?
What happens if one task attempts to issue a KVM ioctl while its mm is swapped?
Peter Zijlstra
maintainerRe: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
In reply to Andrei VaginI'm terrified of all of this...
Quoted 5 lines · Andrei Vagin
arch/x86/kernel/signal.c +5
-0+long swap_vm_exec_context (struct sigcontext __user *uctx) +{ +struct sigcontext ctx = {}; +sigset_t set = {}; +
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 933
Quoted 10 lines
arch/x86/kernel/signal.c +10
-0+ +if (copy_from_user (&ctx, uctx,CONTEXT_COPY_SIZE )) +return -EFAULT ; +/* A floating point state is managed from user-space. */ +if (ctx.fpstate !=0 ) +return -EINVAL ; +if (!user_access_begin (uctx,sizeof (*uctx))) +return -EFAULT ; +unsafe_put_sigcontext (uctx,NULL ,current_pt_regs (), (&set), Efault); +user_access_end ();
But here you save the sigcontext without FPU state.
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 936
Quoted 3 lines
arch/x86/kernel/signal.c +3
-0+ +if (__restore_sigcontext (current_pt_regs (), &ctx,0 )) + goto badframe;
And here you restore sigcontext, *with* FPU state. At which point your FPU state is irrecoverably lost.
Also, I'm not at all convinced this can ever do the right thing when the tasks don't agree on what the FPU state is. I suppose in the best case the save will EFAULT.
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 944
Quoted 8 lines
arch/x86/kernel/signal.c +8
-0+ +return 0 ; +Efault: +user_access_end (); +badframe: +signal_fault (current_pt_regs (), uctx,"swap_vm_exec_context" ); +return -EFAULT ; +}
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 5 lines · Peter Zijlstra
I'm terrified of all of this...
On Tue, Apr 13, 2021 at 10:52:15PM -0700, Andrei Vagin wrote:
Quoted 11 lines
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 929
Quoted 11 lines
arch/x86/kernel/signal.c +11
-0+long swap_vm_exec_context (struct sigcontext __user *uctx) +{ +struct sigcontext ctx = {}; +sigset_t set = {}; + + +if (copy_from_user (&ctx, uctx,CONTEXT_COPY_SIZE )) +return -EFAULT ; +/* A floating point state is managed from user-space. */ +if (ctx.fpstate !=0 ) +return -EINVAL ;
Here, we check that ctx doesn't have an FPU state.
Quoted 7 lines
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 933
Quoted 4 lines
arch/x86/kernel/signal.c +4
-0+if (!user_access_begin (uctx,sizeof (*uctx))) +return -EFAULT ; +unsafe_put_sigcontext (uctx,NULL ,current_pt_regs (), (&set), Efault); +user_access_end ();But here you save the sigcontext without FPU state.
Quoted 6 lines
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 936
Quoted 3 lines
arch/x86/kernel/signal.c +3
-0+ +if (__restore_sigcontext (current_pt_regs (), &ctx,0 )) + goto badframe;And here you restore sigcontext, *with* FPU state. At which point your FPU state is irrecoverably lost.
process_vm_exec doesn't change a process FPU state. Unlike signals, here we can control it from a user-space. A process can set an FPU state before process_vm_exec and then retore its FPU state after the call.
This version of patches has a bug that I fixed in my tree when I implemented the user-space part for gVisor. I didn't take into account that restore_sigcontext(ctx) clears a process fpu state if ctx->fpstate is zero. I moved fpu__restore_sig out from __restore_sigcontext to fix this issue:
https://github.com/avagin/linux-task-diag/commit/55b7194d00ff
Quoted 13 lines
Also, I'm not at all convinced this can ever do the right thing when the tasks don't agree on what the FPU state is. I suppose in the best case the save will EFAULT.
Quoted 8 lines
arch/x86/kernel/signal.c +8
-0+ +return 0 ; +Efault: +user_access_end (); +badframe: +signal_fault (current_pt_regs (), uctx,"swap_vm_exec_context" ); +return -EFAULT ; +}
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
In reply to Andrei VaginQuoted 31 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.
process_vm_execswaps the current address space with an address space of a specified process, sets a state from sigcontex and resumes the process. When a process receives a signal or calls a system call,process_vm_execsaves the process state back to sigcontext, restores the origin address space, restores the origin process state, and returns to userspace.If it was interrupted by a signal and the signal is in the
user_mask, the signal is dequeued and information about it is saved in uinfo. Ifprocess_vm_execis interrupted by a system call, a synthetic siginfo for the SIGSYS signal is generated.The behavior of this system call is similar to
PTRACE_SYSEMUbut everything is happing in the context of one process, soprocess_vm_execshows a better performance.
PTRACE_SYSEMUis primarily used to implement sandboxes (application kernels) like User-mode Linux or gVisor. These type of sandboxes intercepts applications system calls and acts as the guest kernel. A simple benchmark, where a "tracee" process executes systems calls in a loop and a "tracer" process traps syscalls and handles them just incrementing the tracee instruction pointer to skip the syscall instruction shows thatprocess_vm_execworks more than 5 times faster thanPTRACE_SYSEMU.
[...]
Quoted 16 lines
arch/x86/kernel/signal.c +16
-0+long swap_vm_exec_context (struct sigcontext __user *uctx) +{ +struct sigcontext ctx = {}; +sigset_t set = {}; + + +if (copy_from_user (&ctx, uctx,CONTEXT_COPY_SIZE )) +return -EFAULT ; +/* A floating point state is managed from user-space. */ +if (ctx.fpstate !=0 ) +return -EINVAL ; +if (!user_access_begin (uctx,sizeof (*uctx))) +return -EFAULT ; +unsafe_put_sigcontext (uctx,NULL ,current_pt_regs (), (&set), Efault); +user_access_end (); +
arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 944
Quoted 10 lines
arch/x86/kernel/signal.c +10
-0+if (__restore_sigcontext (current_pt_regs (), &ctx,0 )) + goto badframe; + +return 0 ; +Efault: +user_access_end (); +badframe: +signal_fault (current_pt_regs (), uctx,"swap_vm_exec_context" ); +return -EFAULT ; +}
Comparing the pieces of context that restore_sigcontext() restores with what a normal task switch does (see __switch_to() and callees), I
noticed: On CPUs with FSGSBASE support, I think sandboxed code could overwrite FSBASE/GSBASE using the WRFSBASE/WRGSBASE instructions, causing the supervisor to access attacker-controlled addresses when it tries to access a thread-local variable like "errno"? Signal handling saves the segment registers, but not the FS/GS base addresses.
jannh@laptop:~/test$ cat signal_gsbase.c // compile with -mfsgsbase #include <stdio.h> #include <signal.h> #include <immintrin.h>
void signal_handler(int sig, siginfo_t *info, void *ucontext_) {
puts("signal handler"); _writegsbase_u64(0x12345678);
}
int main(void) {
struct sigaction new_act = {
.sa_sigaction = signal_handler,
.sa_flags = SA_SIGINFO
};
sigaction(SIGUSR1, &new_act, NULL);
printf("original gsbase is 0x%lx\n", _readgsbase_u64()); raise(SIGUSR1);
printf("post-signal gsbase is 0x%lx\n", _readgsbase_u64());
}
jannh@laptop:~/test$ gcc -o signal_gsbase signal_gsbase.c -mfsgsbase jannh@laptop:~/test$ ./signal_gsbase original gsbase is 0x0 signal handler post-signal gsbase is 0x12345678 jannh@laptop:~/test$
So to make this usable for a sandboxing usecase, you'd also have to save and restore FSBASE/GSBASE, just like __switch_to().
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall
Quoted 99 lines · Jann Horn
Quoted 31 lines · Andrei Vagin
This change introduces the new system call:
process_vm_exec(pid_t pid, struct sigcontext *uctx, unsigned long flags,
siginfo_t* uinfo,sigset_t*sigmask,size_tsizemask)
process_vm_execallows to execute the current process in an address space of another process.
process_vm_execswaps the current address space with an address space of a specified process, sets a state from sigcontex and resumes the process. When a process receives a signal or calls a system call,process_vm_execsaves the process state back to sigcontext, restores the origin address space, restores the origin process state, and returns to userspace.If it was interrupted by a signal and the signal is in the
user_mask, the signal is dequeued and information about it is saved in uinfo. Ifprocess_vm_execis interrupted by a system call, a synthetic siginfo for the SIGSYS signal is generated.The behavior of this system call is similar to
PTRACE_SYSEMUbut everything is happing in the context of one process, soprocess_vm_execshows a better performance.
PTRACE_SYSEMUis primarily used to implement sandboxes (application kernels) like User-mode Linux or gVisor. These type of sandboxes intercepts applications system calls and acts as the guest kernel. A simple benchmark, where a "tracee" process executes systems calls in a loop and a "tracer" process traps syscalls and handles them just incrementing the tracee instruction pointer to skip the syscall instruction shows thatprocess_vm_execworks more than 5 times faster thanPTRACE_SYSEMU.[...]
Quoted 16 lines
arch/x86/kernel/signal.c +16
-0+long swap_vm_exec_context (struct sigcontext __user *uctx) +{ +struct sigcontext ctx = {}; +sigset_t set = {}; + + +if (copy_from_user (&ctx, uctx,CONTEXT_COPY_SIZE )) +return -EFAULT ; +/* A floating point state is managed from user-space. */ +if (ctx.fpstate !=0 ) +return -EINVAL ; +if (!user_access_begin (uctx,sizeof (*uctx))) +return -EFAULT ; +unsafe_put_sigcontext (uctx,NULL ,current_pt_regs (), (&set), Efault); +user_access_end (); +arch/x86/kernel/signal.c COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) · line 944
Quoted 10 lines
arch/x86/kernel/signal.c +10
-0+if (__restore_sigcontext (current_pt_regs (), &ctx,0 )) + goto badframe; + +return 0 ; +Efault: +user_access_end (); +badframe: +signal_fault (current_pt_regs (), uctx,"swap_vm_exec_context" ); +return -EFAULT ; +}Comparing the pieces of context that
restore_sigcontext()restores with what a normal task switch does (see__switch_to()and callees), I
noticed: On CPUs with FSGSBASE support, I think sandboxed code could overwrite FSBASE/GSBASE using the WRFSBASE/WRGSBASE instructions, causing the supervisor to access attacker-controlled addresses when it tries to access a thread-local variable like "errno"? Signal handling saves the segment registers, but not the FS/GS base addresses.jannh@laptop:~/test$ cat signal_gsbase.c // compile with -mfsgsbase #include <stdio.h> #include <signal.h> #include <immintrin.h>void
signal_handler(int sig, siginfo_t *info, void *ucontext_){
puts("signal handler");_writegsbase_u64(0x12345678);
}int main(void) {
struct sigactionnew_act= {
.sa_sigaction =signal_handler,
.sa_flags =SA_SIGINFO
};
sigaction(SIGUSR1, &new_act, NULL);
printf("original gsbase is 0x%lx\n", _readgsbase_u64()); raise(SIGUSR1);
printf("post-signal gsbase is 0x%lx\n", _readgsbase_u64());
}jannh@laptop:~/test$ gcc -o signal_gsbase signal_gsbase.c -mfsgsbase jannh@laptop:~/test$ ./signal_gsbase original gsbase is 0x0 signal handler post-signal gsbase is 0x12345678 jannh@laptop:~/test$
Quoted 2 lines
So to make this usable for a sandboxing usecase, you'd also have to save and restore FSBASE/GSBASE, just like
__switch_to().
You are right. I've found this too when I implemented the gviosr user-space part.
Here is the tree whether this problem has been fixed:
https://github.com/avagin/linux-task-diag/commits/wip/gvisor-5.10
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 3/4 arch/x86: allow to execute syscalls via process_vm_exec
In reply to Andrei Vaginprocess_vm_exec allows to execute code in an address space of another process. It changes the current address space to the target address space and resume the current process with registers from sigcontex that is passed in the arguments.
This changes adds the PROCESS_VM_EXEC_SYSCALL flag and if it is set process_vm_exec will execute a system call with arguments from sigcontext.
process_vm_exec retuns 0 if the system call has been executed and an error code in other cases.
A return code of the system call can be found in a proper register in sigcontext.
--- arch/x86/entry/common.c | 5 ++++- arch/x86/kernel/process_vm_exec.c | 29 +++++++++++++++++++++++++++- include/linux/entry-common.h | 2 ++ include/linux/process_vm_exec.h | 2 ++ include/uapi/linux/process_vm_exec.h | 8 ++++++++ kernel/entry/common.c | 2 +- 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 include/uapi/linux/process_vm_exec.h
arch/x86/entry/common.c +4 -1
@@ -40,7 +40,10 @@ __visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs) {#ifdef CONFIG_PROCESS_VM_EXEC -if (current->exec_mm && current->exec_mm->ctx) { +struct exec_mm *exec_mm = current->exec_mm; + +if (exec_mm && exec_mm->ctx && + !(exec_mm->flags &PROCESS_VM_EXEC_SYSCALL )) {kernel_siginfo_t info = { .si_signo =SIGSYS , .si_call_addr = (void __user *)KSTK_EIP(current),
arch/x86/kernel/process_vm_exec.c +28 -1
@@ -11,6 +11,7 @@#include <linux/sched/mm.h> #include <linux/syscalls.h> #include <linux/vmacache.h> +#include <linux/entry-common.h> #include <linux/process_vm_exec.h> static void swap_mm (struct mm_struct *prev_mm,struct mm_struct *target_mm)
@@ -73,7 +74,7 @@ SYSCALL_DEFINE6(process_vm_exec, pid_t, pid, struct sigcontext __user *, uctx,sigset_t mask; -if (flags) +if (flags & ~PROCESS_VM_EXEC_SYSCALL )return -EINVAL ;if (sizemask !=sizeof (sigset_t))
@@ -97,6 +98,9 @@ SYSCALL_DEFINE6(process_vm_exec, pid_t, pid, struct sigcontext __user *, uctx, }current_pt_regs ()->ax =0 ; +if (flags &PROCESS_VM_EXEC_SYSCALL ) +syscall_exit_to_user_mode_prepare (current_pt_regs ()); + ret =swap_vm_exec_context (uctx);if (ret <0 ) goto err_mm_put;
@@ -117,6 +121,29 @@ SYSCALL_DEFINE6(process_vm_exec, pid_t, pid, struct sigcontext __user *, uctx,mmgrab (prev_mm);swap_mm (prev_mm, mm); +if (flags &PROCESS_VM_EXEC_SYSCALL ) { +struct pt_regs *regs =current_pt_regs (); +kernel_siginfo_t info; +int sysno; + + regs->orig_ax = regs->ax; + regs->ax = -ENOSYS ; + sysno =syscall_get_nr (current, regs); + +do_syscall_64 (sysno, regs); + +restore_vm_exec_context (regs); + info.si_signo =SIGSYS ; + info.si_call_addr = (void __user *)KSTK_EIP (current); + info.si_arch =syscall_get_arch (current); + info.si_syscall = sysno; + ret =copy_siginfo_to_user (current->exec_mm->siginfo, &info); +current_pt_regs ()->orig_ax = __NR_process_vm_exec; +current_pt_regs ()->ax = -ENOSYS ; +syscall_enter_from_user_mode_work (current_pt_regs (),current_pt_regs ()->orig_ax); +return ret; + } + ret =current_pt_regs ()->ax;return ret;
include/linux/entry-common.h +2 -0
@@ -285,6 +285,8 @@ static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step) }#endif +void syscall_exit_to_user_mode_prepare (struct pt_regs *regs); +/** * syscall_exit_to_user_mode - Handle work before returning to user mode * @regs: Pointer to currents pt_regs
include/linux/process_vm_exec.h +2 -0
@@ -2,6 +2,8 @@ #ifndef _LINUX_PROCESS_VM_EXEC_H #define _LINUX_PROCESS_VM_EXEC_H +#include <uapi/linux/process_vm_exec.h> + struct exec_mm { struct sigcontext *ctx; struct mm_struct *mm;
include/uapi/linux/process_vm_exec.h new file +8 -0
@@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ + +#ifndef _UAPI_LINUX_PROCESS_VM_EXEC_H +#define _UAPI_LINUX_PROCESS_VM_EXEC_H + +#define PROCESS_VM_EXEC_SYSCALL 0x1UL + +#endif
kernel/entry/common.c +1 -1
@@ -235,7 +235,7 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long ti_work)* Syscall specific exit to user mode preparation. Runs with interrupts * enabled. */ -static void syscall_exit_to_user_mode_prepare (struct pt_regs *regs) +void syscall_exit_to_user_mode_prepare (struct pt_regs *regs) {u32 cached_flags =READ_ONCE (current_thread_info ()->flags);unsigned long nr =syscall_get_nr (current, regs);
Signature · 7 lines
2.29.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 4/4 selftests: add tests for process_vm_exec
In reply to Andrei VaginOutput:
$ make run_tests TAP version 13 1..4 # selftests: process_vm_exec: process_vm_exec # 1..1 # ok 1 275 ns/syscall # # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 ok 1 selftests: process_vm_exec: process_vm_exec # selftests: process_vm_exec: process_vm_exec_fault # 1..1 # ok 1 789 ns/signal # # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 ok 2 selftests: process_vm_exec: process_vm_exec_fault # selftests: process_vm_exec: ptrace_vm_exec # 1..1 # ok 1 1378 ns/syscall# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 ok 3 selftests: process_vm_exec: ptrace_vm_exec # selftests: process_vm_exec: process_vm_exec_syscall # 1..1 # ok 1 write works as expectd # # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 ok 4 selftests: process_vm_exec: process_vm_exec_syscall
--- .../selftests/process_vm_exec/Makefile | 7 ++ tools/testing/selftests/process_vm_exec/log.h | 26 ++++ .../process_vm_exec/process_vm_exec.c | 105 +++++++++++++++++ .../process_vm_exec/process_vm_exec_fault.c | 111 ++++++++++++++++++ .../process_vm_exec/process_vm_exec_syscall.c | 81 +++++++++++++ .../process_vm_exec/ptrace_vm_exec.c | 111 ++++++++++++++++++ 6 files changed, 441 insertions(+) create mode 100644 tools/testing/selftests/process_vm_exec/Makefile create mode 100644 tools/testing/selftests/process_vm_exec/log.h create mode 100644 tools/testing/selftests/process_vm_exec/process_vm_exec.c create mode 100644 tools/testing/selftests/process_vm_exec/process_vm_exec_fault.c create mode 100644 tools/testing/selftests/process_vm_exec/process_vm_exec_syscall.c create mode 100644 tools/testing/selftests/process_vm_exec/ptrace_vm_exec.c
tools/testing/selftests/process_vm_exec/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +UNAME_M := $(shell uname -m) +TEST_GEN_PROGS_x86_64 := process_vm_exec process_vm_exec_fault ptrace_vm_exec process_vm_exec_syscall +TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M)) + +include ../lib.mk
tools/testing/selftests/process_vm_exec/log.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __SELFTEST_PROCESS_VM_EXEC_LOG_H__ +#define __SELFTEST_PROCESS_VM_EXEC_LOG_H__ + +#define pr_msg (fmt, lvl, ...) \ + ksft_print_msg("[%s] (%s:%d)\t" fmt "\n", \ + lvl, __FILE__, __LINE__, ##__VA_ARGS__) + +#define pr_p (func, fmt, ...) func(fmt ": %m", ##__VA_ARGS__) + +#define pr_err (fmt, ...) \ + ({ \ + ksft_test_result_error(fmt "\n", ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_fail (fmt, ...) \ + ({ \ + ksft_test_result_fail(fmt "\n", ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_perror (fmt, ...) pr_p(pr_err, fmt, ##__VA_ARGS__) + +#endif
tools/testing/selftests/process_vm_exec/process_vm_exec.c new file +105 -0
105 lines not on this page · raw message
tools/testing/selftests/process_vm_exec/process_vm_exec_fault.c new file +111 -0
111 lines not on this page · raw message
tools/testing/selftests/process_vm_exec/process_vm_exec_syscall.c new file +81 -0
81 lines not on this page · raw message
tools/testing/selftests/process_vm_exec/ptrace_vm_exec.c new file +111 -0
111 lines not on this page · raw message
Signature · 7 lines
2.29.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Quoted 7 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
It seems to me like your proposed API doesn't really fit either one of those usecases well...
Quoted 8 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.
In this case, since you really only want an mm_struct to run code under, it seems weird to create a whole task with its own PID and so on. It seems to me like something similar to the /dev/kvm API would be more appropriate here? Implementation options that I see for that would be:
1. mm_struct-based:
a set of syscalls to create a new mm_struct,
change memory mappings under that mm_struct, and switch to it
2. pagetable-mirroring-based:
like /dev/kvm, an API to create a new pagetable, mirror parts of the mm_struct's pagetables over into it with modified permissions (like KVM_SET_USER_MEMORY_REGION),
and run code under that context.
page fault handling would first handle the fault against mm->pgd as normal, then mirror the PTE over into the secondary pagetables. invalidation could be handled with MMU notifiers.
Quoted 4 lines
Another use-case is CRIU (Checkpoint/Restore in User-space). Several process properties can be received only from the process itself. Right now, we use a parasite code that is injected into the process. We do this with ptrace but it is slow, unsafe, and tricky.
But this API will only let you run code under the *mm* of the target process, not fully in the context of a target *task*, right? So you still won't be able to use this for accessing anything other than memory? That doesn't seem very generically useful to me.
Also, I don't doubt that anything involving ptrace is kinda tricky, but it would be nice to have some more detail on what exactly makes this slow, unsafe and tricky. Are there API additions for ptrace that would make this work better? I imagine you're thinking of things like an API for injecting a syscall into the target process without having to first somehow find an existing SYSCALL instruction in the target process?
Quoted 11 lines
process_vm_execcan
simplify the process of injecting a parasite code and it will allow pre-dump memory without stopping processes. The pre-dump here is when we enable a memory tracker and dump the memory while a process is continue running. On each interaction we dump memory that has been changed from the previous iteration. In the final step, we will stop processes and dump their full state. Right now the most effective way to dump process memory is to create a set of pipes and splice memory into these pipes from the parasite code. Withprocess_vm_exec, we will be able to call vmsplice directly. It means that we will not need to stop a process to inject the parasite code.
Alternatively you could add splice support to /proc/$pid/mem or add a syscall similar to process_vm_readv() that splices into a pipe, right?
Quoted 11 lines · Jann Horn
Quoted 7 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
It seems to me like your proposed API doesn't really fit either one of those usecases well...
We definitely can invent more specific interfaces for each of these problems. Sure, they will handle their use-cases a bit better than this generic one. But do we want to have two very specific interfaces with separate kernel implementations? My previous experiences showed that the kernel community doesn't like interfaces that are specific for only one narrow use-case.
So when I was working on process_vm_exec, I was thinking how to make one interfaces that will be good enough for all these use-cases.
Quoted 16 lines
Quoted 9 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.In this case, since you really only want an
mm_structto run code under, it seems weird to create a whole task with its own PID and so on. It seems to me like something similar to the/dev/kvmAPI would be more appropriate here? Implementation options that I see for that would be:
Quoted 11 lines
1. mm_struct-based:
a set of syscalls to create a newmm_struct,
change memory mappings under thatmm_struct, and switch to it
2. pagetable-mirroring-based:
like/dev/kvm, an API to create a new pagetable, mirror parts of the mm_struct's pagetables over into it with modified permissions (likeKVM_SET_USER_MEMORY_REGION),
and run code under that context.
page fault handling would first handle the fault againstmm->pgdas normal, then mirror the PTE over into the secondary pagetables. invalidation could be handled with MMU notifiers.
We are ready to discuss this sort of interfaces if the community will agree to accept it. Are there any other users except sandboxes that will need something like this? Will the sandbox use-case enough to justify the addition of this interface?
Quoted 10 lines
Quoted 5 lines
Another use-case is CRIU (Checkpoint/Restore in User-space). Several process properties can be received only from the process itself. Right now, we use a parasite code that is injected into the process. We do this with ptrace but it is slow, unsafe, and tricky.
But this API will only let you run code under the *mm* of the target process, not fully in the context of a target *task*, right? So you still won't be able to use this for accessing anything other than memory? That doesn't seem very generically useful to me.
You are right, this will not rid us of the need to run a parasite code. I wrote that it will make a process of injecting a parasite code a bit simpler.
Quoted 8 lines
Also, I don't doubt that anything involving ptrace is kinda tricky, but it would be nice to have some more detail on what exactly makes this slow, unsafe and tricky. Are there API additions for ptrace that would make this work better? I imagine you're thinking of things like an API for injecting a syscall into the target process without having to first somehow find an existing SYSCALL instruction in the target process?
You describe the first problem right. We need to find or inject a syscall instruction to a target process.
Right now, we need to do these steps to execute a system call:
* inject the syscall instruction (PTRACE_PEEKDATA/PTRACE_POKEDATA).
* get origin registers
* set new registers
* get a signal mask.
* block signals
* resume the process
* stop it on the next syscall-exit
* get registers
* set origin registers
* restore a signal mask.
One of the CRIU principals is to avoid changing a process state, so if criu is interrupted, processes must be resumed and continue running. The procedure of injecting a system call creates a window when a process is in an inconsistent state, and a disappearing CRIU at such moments will be fatal for the process. We don't think that we can eliminate such windows, but we want to make them smaller.
In CRIU, we have a self-healed parasite. The idea is to inject a parasite code with a signal frame that contains the origin process state. The parasite runs in an "RPC daemon mode" and gets commands from criu via a unix socket. If it detects that criu disappeared, it calls rt_sigreturn and resumes the origin process.
As for the performance of the ptrace, there are a few reasons why it is slow. First, it is a number of steps what we need to do. Second, it is two synchronious context switches. Even if we will solve the first problem with a new ptrace command, it will be not enough to stop using a parasite in CRIU.
Quoted 13 lines
Quoted 12 lines
process_vm_execcan
simplify the process of injecting a parasite code and it will allow pre-dump memory without stopping processes. The pre-dump here is when we enable a memory tracker and dump the memory while a process is continue running. On each interaction we dump memory that has been changed from the previous iteration. In the final step, we will stop processes and dump their full state. Right now the most effective way to dump process memory is to create a set of pipes and splice memory into these pipes from the parasite code. Withprocess_vm_exec, we will be able to call vmsplice directly. It means that we will not need to stop a process to inject the parasite code.
Quoted 2 lines
Alternatively you could add splice support to
/proc/$pid/memor add a syscall similar toprocess_vm_readv()that splices into a pipe, right?
We send patches to introcude process_vm_splice:
https://lore.kernel.org/patchwork/cover/871116/
but they were not merged and the main reason was a lack of enough users to justify its addition.
Quoted 27 lines · Jann Horn
Quoted 7 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
It seems to me like your proposed API doesn't really fit either one of those usecases well...
Quoted 8 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.In this case, since you really only want an
mm_structto run code under, it seems weird to create a whole task with its own PID and so on. It seems to me like something similar to the/dev/kvmAPI would be more appropriate here? Implementation options that I see for that would be:
Quoted 3 lines
1. mm_struct-based:
a set of syscalls to create a newmm_struct, change memory mappings under thatmm_struct, and switch to it
I like the idea to have a handle for mm. Instead of pid, we will pass this handle to process_vm_exec. We have pidfd for processes and we can introduce mmfd for mm_struct.
Quoted 9 lines
2. pagetable-mirroring-based:
like/dev/kvm, an API to create a new pagetable, mirror parts of the mm_struct's pagetables over into it with modified permissions (likeKVM_SET_USER_MEMORY_REGION),
and run code under that context.
page fault handling would first handle the fault againstmm->pgdas normal, then mirror the PTE over into the secondary pagetables. invalidation could be handled with MMU notifiers.
I found this idea interesting and decided to look at it more closely. After reading the kernel code for a few days, I realized that it would not be easy to implement something like this, but more important is that I don’t understand what problem it solves. Will it simplify the user-space code? I don’t think so. Will it improve performance? It is unclear for me too.
First, in the KVM case, we have a few big linear mappings and need to support one “shadow” address space. In the case of sandboxes, we can have a tremendous amount of mappings and many address spaces that we need to manage. Memory mappings will be mapped with different addresses in a supervisor address space and “guest” address spaces. If guest address spaces will not have their mm_structs, we will need to reinvent vma-s in some form. If guest address spaces have mm_structs, this will look similar to https://lwn.net/Articles/830648/.
Second, each pagetable is tied up with mm_stuct. You suggest creating new pagetables that will not have their mm_struct-s (sorry if I misunderstood something). I am not sure that it will be easy to implement. How many corner cases will be there?
As for page faults in a secondary address space, we will need to find a fault address in the main address space, handle the fault there and then mirror the PTE to the secondary pagetable. Effectively, it means that page faults will be handled in two address spaces. Right now, we use memfd and shared mappings. It means that each fault is handled only in one address space, and we map a guest memory region to the supervisor address space only when we need to access it. A large portion of guest anonymous memory is never mapped to the supervisor address space. Will an overhead of mirrored address spaces be smaller than memfd shared mappings? I am not sure.
Third, this approach will not get rid of having process_vm_exec. We will need to switch to a guest address space with a specified state and switch back on faults or syscalls. If the main concern is the ability to run syscalls on a remote mm, we can think about how to fix this. I see two ways what we can do here:
* Specify the exact list of system calls that are allowed. The first three candidates are mmap, munmap, and vmsplice.
* Instead of allowing us to run system calls, we can implement this in the form of commands. In the case of sandboxes, we need to implement only two commands to create and destroy memory mappings in a target address space.
Thanks,
Andrei
Quoted 28 lines · Andrei Vagin
Quoted 27 lines · Jann Horn
Quoted 7 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
It seems to me like your proposed API doesn't really fit either one of those usecases well...
Quoted 8 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.In this case, since you really only want an
mm_structto run code under, it seems weird to create a whole task with its own PID and so on. It seems to me like something similar to the/dev/kvmAPI would be more appropriate here? Implementation options that I see for that would be:
Quoted 7 lines
Quoted 3 lines
1. mm_struct-based:
a set of syscalls to create a newmm_struct, change memory mappings under thatmm_struct, and switch to itI like the idea to have a handle for mm. Instead of pid, we will pass this handle to
process_vm_exec. We have pidfd for processes and we can introduce mmfd formm_struct.
I personally think that it might be quite unwieldy when it comes to the restrictions you get from trying to have shared memory with the owning process - I'm having trouble figuring out how you can implement copy-on-write semantics without relying on copy-on-write logic in the host OS and without being able to use userfaultfd.
But if that's not a problem somehow, and you can find some reasonable way to handle memory usage accounting and fix up everything that assumes that multithreaded userspace threads don't switch ->mm, I guess this might work for your usecase.
Quoted 10 lines
Quoted 9 lines
2. pagetable-mirroring-based:
like/dev/kvm, an API to create a new pagetable, mirror parts of the mm_struct's pagetables over into it with modified permissions (likeKVM_SET_USER_MEMORY_REGION),
and run code under that context.
page fault handling would first handle the fault againstmm->pgdas normal, then mirror the PTE over into the secondary pagetables. invalidation could be handled with MMU notifiers.
Quoted 3 lines
I found this idea interesting and decided to look at it more closely. After reading the kernel code for a few days, I realized that it would not be easy to implement something like this,
Yeah, it might need architecture-specific code to flip the page tables on userspace entry/exit, and maybe also for mirroring them. And for the TLB flushing logic...
Quoted 4 lines
but more important is that
I don’t understand what problem it solves. Will it simplify the user-space code? I don’t think so. Will it improve performance? It is unclear for me too.
Some reasons I can think of are:
- direct guest memory access: I imagined you'd probably want to be able to directly access userspace memory from the supervisor, and with this approach that'd become easy.
- integration with on-demand paging of the host OS: You'd be able to create things like file-backed copy-on-write mappings from the host filesystem, or implement your own mappings backed by some kind of storage using userfaultfd.
- sandboxing: For sandboxing usecases (not your usecase), it would be possible to e.g. create a read-only clone of the entire address space of a process and give write access to specific parts of it, or something like that.
These address space clones could potentially be created and destroyed fairly quickly.
- accounting: memory usage would be automatically accounted to the supervisor process, so even without a parasite process, you'd be able to see the memory usage correctly in things like "top".
- small (non-pageable) memory footprint in the host kernel:
The only things the host kernel would have to persistently store would be the normal MM data structures for the supervisor plus the mappings from "guest userspace" memory ranges to supervisor memory ranges; userspace pagetables would be discardable, and could even be shared with those of the supervisor in cases where the alignment fits. So with this, large anonymous mappings with 4K granularity only cost you ~0.20% overhead across host and guest address space; without this, if you used shared mappings instead, you'd pay twice that for every 2MiB range from which parts are accessed in both contexts, plus probably another ~0.2% or so for the "struct address_space"?
- all memory-management-related syscalls could be directly performed in the "kernel" process
But yeah, some of those aren't really relevant for your usecase, and I guess things like the accounting aspect could just as well be solved differently...
Quoted 9 lines
First, in the KVM case, we have a few big linear mappings and need to support one “shadow” address space. In the case of sandboxes, we can have a tremendous amount of mappings and many address spaces that we need to manage. Memory mappings will be mapped with different addresses in a supervisor address space and “guest” address spaces. If guest address spaces will not have their
mm_structs, we will need to reinvent vma-s in some form. If guest address spaces havemm_structs, this will look similar to https://lwn.net/Articles/830648/.
Quoted 3 lines
Second, each pagetable is tied up with
mm_stuct. You suggest creating new pagetables that will not have their mm_struct-s (sorry if I misunderstood something).
Yeah, that's what I had in mind, page tables without an mm_struct.
Quoted 2 lines
I am not sure that it will be easy to
implement. How many corner cases will be there?
Yeah, it would require some work around TLB flushing and entry/exit from userspace. But from a high-level perspective it feels to me like a change with less systematic impact. Maybe I'm wrong about that.
Quoted 3 lines
As for page faults in a secondary address space, we will need to find a fault address in the main address space, handle the fault there and then mirror the PTE to the secondary pagetable.
Right.
Quoted 8 lines
Effectively, it means that
page faults will be handled in two address spaces. Right now, we use memfd and shared mappings. It means that each fault is handled only in one address space, and we map a guest memory region to the supervisor address space only when we need to access it. A large portion of guest anonymous memory is never mapped to the supervisor address space. Will an overhead of mirrored address spaces be smaller than memfd shared mappings? I am not sure.
But as long as the mappings are sufficiently big and aligned properly, or you explicitly manage the supervisor address space, some of that cost disappears: E.g. even if a page is mapped in both address spaces, you wouldn't have a memory cost for the second mapping if the page tables are shared.
Quoted 3 lines
Third, this approach will not get rid of having
process_vm_exec. We will need to switch to a guest address space with a specified state and switch back on faults or syscalls.
Yeah, you'd still need a syscall for running code under a different set of page tables. But that's something that KVM _almost_ already does.
Quoted 11 lines
If the main concern is the ability to
run syscalls on a remote mm, we can think about how to fix this. I see two ways what we can do here:* Specify the exact list of system calls that are allowed. The first three candidates are mmap, munmap, and vmsplice.
* Instead of allowing us to run system calls, we can implement this in the form of commands. In the case of sandboxes, we need to implement only two commands to create and destroy memory mappings in a target address space.
FWIW, there is precedent for something similar: The Android folks already added process_madvise() for remotely messing with the VMAs of another process to some degree.
Quoted 37 lines · Jann Horn
Quoted 28 lines · Andrei Vagin
Quoted 27 lines · Jann Horn
Quoted 7 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
It seems to me like your proposed API doesn't really fit either one of those usecases well...
Quoted 8 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.In this case, since you really only want an
mm_structto run code under, it seems weird to create a whole task with its own PID and so on. It seems to me like something similar to the/dev/kvmAPI would be more appropriate here? Implementation options that I see for that would be:Quoted 7 lines
Quoted 3 lines
1. mm_struct-based:
a set of syscalls to create a newmm_struct, change memory mappings under thatmm_struct, and switch to itI like the idea to have a handle for mm. Instead of pid, we will pass this handle to
process_vm_exec. We have pidfd for processes and we can introduce mmfd formm_struct.
Quoted 5 lines
I personally think that it might be quite unwieldy when it comes to the restrictions you get from trying to have shared memory with the owning process - I'm having trouble figuring out how you can implement copy-on-write semantics without relying on copy-on-write logic in the host OS and without being able to use userfaultfd.
It is easy. COW mappings are mapped to guest address spaces without the write permission. If one of processes wants to write something, it triggers a fault that is handled in the Sentry (supervisor/kernel).
Quoted 29 lines
But if that's not a problem somehow, and you can find some reasonable way to handle memory usage accounting and fix up everything that assumes that multithreaded userspace threads don't switch ->mm, I guess this might work for your usecase.
Quoted 10 lines
Quoted 9 lines
2. pagetable-mirroring-based:
like/dev/kvm, an API to create a new pagetable, mirror parts of the mm_struct's pagetables over into it with modified permissions (likeKVM_SET_USER_MEMORY_REGION),
and run code under that context.
page fault handling would first handle the fault againstmm->pgdas normal, then mirror the PTE over into the secondary pagetables. invalidation could be handled with MMU notifiers.Quoted 3 lines
I found this idea interesting and decided to look at it more closely. After reading the kernel code for a few days, I realized that it would not be easy to implement something like this,
Yeah, it might need architecture-specific code to flip the page tables on userspace entry/exit, and maybe also for mirroring them. And for the TLB flushing logic...
Quoted 4 lines
but more important is that
I don’t understand what problem it solves. Will it simplify the user-space code? I don’t think so. Will it improve performance? It is unclear for me too.
Quoted 5 lines
Some reasons I can think of are:
- direct guest memory access: I imagined you'd probably want to be able to directly access userspace memory from the supervisor, and with this approach that'd become easy.
Right now, we use shared memory regions for that and they work fine. As I already mentioned the most part of memory are never mapped to the supervisor address space.
Quoted 5 lines
- integration with on-demand paging of the host OS: You'd be able to create things like file-backed copy-on-write mappings from the host filesystem, or implement your own mappings backed by some kind of storage using userfaultfd.
This isn't a problem either...
Quoted 7 lines
- sandboxing: For sandboxing usecases (not your usecase), it would be possible to e.g. create a read-only clone of the entire address space of a process and give write access to specific parts of it, or something like that.
These address space clones could potentially be created and destroyed fairly quickly.
This is a very valid example and I would assume this is where your idea was coming from. I have some doubts about the idea of additional sub-page-tables in the kernel, but I know a good way how to implement your idea with KVM. You can look at how the KVM platform is implemented in gVisor and this sort of sandboxing can be implemented in the same way.
In a few words, we create a KVM virtual machine, repeat the process address space in the guest ring0, implement basic operating system-level stubs, so that the process can jump between the host ring3 and the guest ring0.
https://github.com/google/gvisor/blob/master/pkg/ring0/
https://github.com/google/gvisor/tree/master/pkg/sentry/platform/kvm
When we have all these bits, we can create any page tables for a guest ring3 and run untrusted code there. The sandbox process switches to the guest ring0 and then it switches to a guest ring3 with a specified page tables and a state.
https://cs.opensource.google/gvisor/gvisor/+/master:pkg/sentry/platform/kvm/machine_amd64.go;l=356
With this scheme, the sandbox process will have direct access to page tables and will be able to change them.
Quoted 5 lines
- accounting: memory usage would be automatically accounted to the supervisor process, so even without a parasite process, you'd be able to see the memory usage correctly in things like "top".
Quoted 11 lines
- small (non-pageable) memory footprint in the host kernel:
The only things the host kernel would have to persistently store would be the normal MM data structures for the supervisor plus the mappings from "guest userspace" memory ranges to supervisor memory ranges; userspace pagetables would be discardable, and could even be shared with those of the supervisor in cases where the alignment fits. So with this, large anonymous mappings with 4K granularity only cost you ~0.20% overhead across host and guest address space; without this, if you used shared mappings instead, you'd pay twice that for every 2MiB range from which parts are accessed in both contexts, plus probably another ~0.2% or so for the "structaddress_space"?
If we use shared mappings, we don't map the most part of guest memory to the supervisor address space and don't have page tables for it there. I would say that this is a question where a memory footprint will be smaller...
Quoted 45 lines
- all memory-management-related syscalls could be directly performed in the "kernel" process
But yeah, some of those aren't really relevant for your usecase, and I guess things like the accounting aspect could just as well be solved differently...
Quoted 9 lines
First, in the KVM case, we have a few big linear mappings and need to support one “shadow” address space. In the case of sandboxes, we can have a tremendous amount of mappings and many address spaces that we need to manage. Memory mappings will be mapped with different addresses in a supervisor address space and “guest” address spaces. If guest address spaces will not have their
mm_structs, we will need to reinvent vma-s in some form. If guest address spaces havemm_structs, this will look similar to https://lwn.net/Articles/830648/.Quoted 3 lines
Second, each pagetable is tied up with
mm_stuct. You suggest creating new pagetables that will not have their mm_struct-s (sorry if I misunderstood something).Yeah, that's what I had in mind, page tables without an
mm_struct.Quoted 2 lines
I am not sure that it will be easy to
implement. How many corner cases will be there?Yeah, it would require some work around TLB flushing and entry/exit from userspace. But from a high-level perspective it feels to me like a change with less systematic impact. Maybe I'm wrong about that.
Quoted 3 lines
As for page faults in a secondary address space, we will need to find a fault address in the main address space, handle the fault there and then mirror the PTE to the secondary pagetable.
Right.
Quoted 8 lines
Effectively, it means that
page faults will be handled in two address spaces. Right now, we use memfd and shared mappings. It means that each fault is handled only in one address space, and we map a guest memory region to the supervisor address space only when we need to access it. A large portion of guest anonymous memory is never mapped to the supervisor address space. Will an overhead of mirrored address spaces be smaller than memfd shared mappings? I am not sure.
Quoted 5 lines
But as long as the mappings are sufficiently big and aligned properly, or you explicitly manage the supervisor address space, some of that cost disappears: E.g. even if a page is mapped in both address spaces, you wouldn't have a memory cost for the second mapping if the page tables are shared.
You are right. It is interesting how many pte-s will be shared. For example, if a guest process forks a child, all anon memory will be COW, this means we will need to remove the W bit from pte-s and so we will need to allocate pte-s for both processes...
Quoted 8 lines
Quoted 4 lines
Third, this approach will not get rid of having
process_vm_exec. We will need to switch to a guest address space with a specified state and switch back on faults or syscalls.Yeah, you'd still need a syscall for running code under a different set of page tables. But that's something that KVM _almost_ already does.
I don't understand this analogy with KVM...
Quoted 8 lines
Quoted 8 lines
If the main concern is the ability to
run syscalls on a remote mm, we can think about how to fix this. I see two ways what we can do here:* Specify the exact list of system calls that are allowed. The first three candidates are mmap, munmap, and vmsplice.
Quoted 8 lines
Quoted 4 lines
* Instead of allowing us to run system calls, we can implement this in the form of commands. In the case of sandboxes, we need to implement only two commands to create and destroy memory mappings in a target address space.
FWIW, there is precedent for something similar: The Android folks already added
process_madvise()for remotely messing with the VMAs of another process to some degree.
I know. We tried to implement process_vm_mmap and process_vm_splice:
https://lkml.org/lkml/2018/1/9/32
https://patchwork.kernel.org/project/linux-mm/cover/155836064844.2441.10911127801797083064.stgit@localhost.localdomain/
Thanks,
Andrei
Anton Ivanov
maintainerQuoted 8 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
Quoted 8 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.
Certainly interesting, but will require um to rework most of its memory management and we will most likely need extra mm support to make use of it in UML. We are not likely to get away just with one syscall there.
Quoted 110 lines
Another use-case is CRIU (Checkpoint/Restore in User-space). Several process properties can be received only from the process itself. Right now, we use a parasite code that is injected into the process. We do this with ptrace but it is slow, unsafe, and tricky.
process_vm_execcan simplify the process of injecting a parasite code and it will allow pre-dump memory without stopping processes. The pre-dump here is when we enable a memory tracker and dump the memory while a process is continue running. On each interaction we dump memory that has been changed from the previous iteration. In the final step, we will stop processes and dump their full state. Right now the most effective way to dump process memory is to create a set of pipes and splice memory into these pipes from the parasite code. Withprocess_vm_exec, we will be able to call vmsplice directly. It means that we will not need to stop a process to inject the parasite code.= How it works =
process_vm_exechas two modes:* Execute code in an address space of a target process and stop on any signal or system call.
* Execute a system call in an address space of a target process.
int process_vm_exec(pid_t pid, struct sigcontext uctx,
unsigned long flags,siginfo_tsiginfo,sigset_t*sigmask,size_tsizemask)PID - target process identification. We can consider to use pidfd instead of PID here.
sigcontext contains a process state with what the process will be resumed after switching the address space and then when a process will be stopped, its sate will be saved back to sigcontext.
siginfo is information about a signal that has interrupted the process. If a process is interrupted by a system call, signfo will contain a synthetic siginfo of the SIGSYS signal.
sigmask is a set of signals that
process_vm_execreturns via signfo.# How fast is it
In the fourth patch, you can find two benchmarks that execute a function that calls system calls in a loop.
ptrace_vm_exeuses ptrace to trap system calls,proces_vm_execuses theprocess_vm_execsyscall to do the same thing.ptrace_vm_exec: 1446 ns/syscall ptrocess_vm_exec: 289 ns/syscallPS: This version is just a prototype. Its goal is to collect the initial feedback, to discuss the interfaces, and maybe to get some advice on implementation..
Cc Andrew Morton <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Anton Ivanov <[email protected]>Cc Christian Brauner <[email protected]>Cc Dmitry Safonov <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jeff Dike <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michael Kerrisk (man-pages) <[email protected]>Cc Oleg Nesterov <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Richard Weinberger <[email protected]>Cc Thomas Gleixner <[email protected]>Andrei Vagin (4):
signal: add a helper to restore a process state from sigcontexarch/x86: implement theprocess_vm_execsyscallarch/x86: allow to execute syscalls viaprocess_vm_exec
selftests: add tests forprocess_vm_execarch/Kconfig | 15 ++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 19 +++ arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/include/asm/sigcontext.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/process_vm_exec.c | 160 ++++++++++++++++++ arch/x86/kernel/signal.c | 125 ++++++++++---- include/linux/entry-common.h | 2 + include/linux/process_vm_exec.h | 17 ++ include/linux/sched.h | 7 + include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 4 +- include/uapi/linux/process_vm_exec.h | 8 + kernel/entry/common.c | 2 +- kernel/fork.c | 9 + kernel/sys_ni.c | 2 + .../selftests/process_vm_exec/Makefile | 7 + tools/testing/selftests/process_vm_exec/log.h | 26 +++ .../process_vm_exec/process_vm_exec.c | 105 ++++++++++++ .../process_vm_exec/process_vm_exec_fault.c | 111 ++++++++++++ .../process_vm_exec/process_vm_exec_syscall.c | 81 +++++++++ .../process_vm_exec/ptrace_vm_exec.c | 111 ++++++++++++ 23 files changed, 785 insertions(+), 37 deletions(-)create mode 100644
arch/x86/kernel/process_vm_exec.c
create mode 100644include/linux/process_vm_exec.h
create mode 100644include/uapi/linux/process_vm_exec.h
create mode 100644tools/testing/selftests/process_vm_exec/Makefilecreate mode 100644tools/testing/selftests/process_vm_exec/log.hcreate mode 100644tools/testing/selftests/process_vm_exec/process_vm_exec.ccreate mode 100644tools/testing/selftests/process_vm_exec/process_vm_exec_fault.ccreate mode 100644tools/testing/selftests/process_vm_exec/process_vm_exec_syscall.ccreate mode 100644tools/testing/selftests/process_vm_exec/ptrace_vm_exec.c
Signature · 8 lines
Anton R. Ivanov Cambridgegreys Limited. Registered in England. Company Number 10273661 https://www.cambridgegreys.com/ _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerQuoted 18 lines · Anton Ivanov
Quoted 8 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
Quoted 8 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that are used to manage guest address spaces. Guest code is executed in the context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes use
PTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.
Quoted 3 lines
Certainly interesting, but will require um to rework most of its memory management and we will most likely need extra mm support to make use of it in UML. We are not likely to get away just with one syscall there.
Might help the seccomp mode though:
https://patchwork.ozlabs.org/project/linux-um/list/?series=231980
johannes
Quoted 31 lines · Johannes Berg
Quoted 24 lines · Anton Ivanov
Quoted 11 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write
to a process memory faster than we can do this with ptrace. And now it
is time forprocess_vm_execthat allows executing code in an address
space of another process. We can do this with ptrace but it is much slower.= Use-cases =
Quoted 11 lines
Here are two known use-cases. The first one is “application kernel” sandboxes like User-mode Linux and gVisor. In this case, we have a process that runs the sandbox kernel and a set of stub processes that
are used to manage guest address spaces. Guest code is executed in the
context of stub processes but all system calls are intercepted and handled in the sandbox kernel. Right now, these sort of sandboxes usePTRACE_SYSEMUto trap system calls, but theprocess_vm_execcan significantly speed them up.Quoted 5 lines
Certainly interesting, but will require um to rework most of its memory
management and we will most likely need extra mm support to make use of
it in UML. We are not likely to get away just with one syscall there.
Quoted 3 lines
Might help the seccomp mode though:
https://patchwork.ozlabs.org/project/linux-um/list/?series=231980
Hmm, to me it sounds like it replaces both ptrace and seccomp mode while completely avoiding the scheduling overhead that these techniques have. I think everything UML needs is covered:
* The new API can do syscalls in the target memory space (we can modify the address space)
* The new API can run code until the next syscall happens (or a signal happens, which means SIGALRM for scheduling works)
* Single step tracing should work by setting EFLAGS
I think the memory management itself stays fundamentally the same. We just do the initial clone() using CLONE_STOPPED. We don't need any stub code/data and we have everything we need to modify the address space and run the userspace process.
Benjamin
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
* Andrei Vagin:
Quoted 7 lines
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
We also have some vaguely related within the same address space: running code on another thread, without modifying its stack, while it has signal handlers blocked, and without causing system calls to fail with EINTR. This can be used to implement certain kinds of memory barriers. It is also necessary to implement set*id with POSIX semantics in userspace. (Linux only changes the current thread credentials, POSIX requires process-wide changes.) We currently use a signal for set*id, but it has issues (it can be blocked, the signal could come from somewhere, etc.). We can't use signals for barriers because of the EINTR issue, and because the signal context is stored on the stack.
Thanks,
Florian
Quoted 9 lines · Florian Weimer
* Andrei Vagin:
Quoted 6 lines
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.
Quoted 6 lines
Quoted 1 line
= Use-cases =
We also have some vaguely related within the same address space: running code on another thread, without modifying its stack, while it has signal handlers blocked, and without causing system calls to fail with EINTR. This can be used to implement certain kinds of memory barriers.
That's what the membarrier() syscall is for, right? Unless you don't want to register all threads for expedited membarrier use?
Quoted 7 lines
It is
also necessary to implement set*id with POSIX semantics in userspace. (Linux only changes the current thread credentials, POSIX requires process-wide changes.) We currently use a signal for set*id, but it has issues (it can be blocked, the signal could come from somewhere, etc.). We can't use signals for barriers because of the EINTR issue, and because the signal context is stored on the stack.
This essentially becomes a question of "how much is set*id allowed to block and what level of guarantee should there be by the time it returns that no threads will perform privileged actions anymore after it returns", right?
Like, if some piece of kernel code grabs a pointer to the current credentials or acquires a temporary reference to some privileged resource, then blocks on reading an argument from userspace, and then performs a privileged action using the previously-grabbed credentials or resource, what behavior do you want? Should setuid() block until that privileged action has completed? Should it abort that action (which is kinda what you get with the signals approach)? Should it just return immediately even though an attacker who can write to process memory at that point might still be able to influence a privileged operation that hasn't read all its inputs yet? Should the kernel be designed to keep track of whether it is currently holding a privileged resource? Or should the kernel just specifically permit credential changes in specific places where it is known that a task might block for a long time and it is not holding any privileged resources (kinda like the approach taken for freezer stuff)?
If userspace wants multithreaded setuid() without syscall aborting, things get gnarly really fast; and having an interface to remotely perform operations under another task's context isn't really relevant to the core problem here, I think.
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
* Jann Horn:
Quoted 12 lines · Florian Weimer
Quoted 11 lines
* Andrei Vagin:
Quoted 7 lines
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
Quoted 7 lines
Quoted 4 lines
We also have some vaguely related within the same address space: running code on another thread, without modifying its stack, while it has signal handlers blocked, and without causing system calls to fail with EINTR. This can be used to implement certain kinds of memory barriers.
That's what the
membarrier()syscall is for, right? Unless you don't want to register all threads for expedited membarrier use?
membarrier is not sufficiently powerful for revoking biased locks, for example.
For the EINTR issue, <https://github.com/golang/go/issues/38836> is an example. I believe CIFS has since seen a few fixes (after someone reported that tar on CIFS wouldn't work because the SIGCHLD causing utimensat to fail—and there isn't even a signal handler for SIGCHLD!), but the time it took to get to this point doesn't give me confidence that it is safe to send signals to a thread that is running unknown code.
But as you explained regarding the set*id broadcast, it seems that if we had this run-on-another-thread functionality, we would likely encounter issues similar to those with SA_RESTART. We don't see the issue with set*id today because it's a rare operation, and multi-threaded file servers that need to change credentials frequently opt out of the set*id broadcast anyway. (What I have in mind is a future world where any printf call, any malloc call, can trigger such a broadcast.)
The cross-VM CRIU scenario would probably somewhere in between (not quite the printf/malloc level, but more frequent than set*id).
Thanks,
Florian
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Quoted 20 lines · Florian Weimer
* Jann Horn:
Quoted 17 lines · Florian Weimer
Quoted 15 lines
* Andrei Vagin:
Quoted 7 lines
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.= Use-cases =
We also have some vaguely related within the same address space: running code on another thread, without modifying its stack, while it has signal handlers blocked, and without causing system calls to fail with EINTR. This can be used to implement certain kinds of memory barriers.
Quoted 5 lines
Quoted 2 lines
That's what the
membarrier()syscall is for, right? Unless you don't want to register all threads for expedited membarrier use?membarrier is not sufficiently powerful for revoking biased locks, for example.
But on Linux >=5.10, together with rseq, it is, right? Then lock acquisition could look roughly like this, in pseudo-C (yes, I know, real rseq doesn't quite look like that, you'd need inline asm for that unless the compiler adds special support for this):
enum local_state {
STATE_FREE_OR_BIASED,
STATE_LOCKED
};
#define OWNER_LOCKBIT (1U<<31)
#define OWNER_WAITER_BIT (1U<<30) /* notify futex when OWNER_LOCKBIT is cleared */
struct biased_lock {
unsigned int owner_with_lockbit;
enum local_state local_state;
};
void lock(struct biased_lock *L) {
unsigned int my_tid = THREAD_SELF->tid; RSEQ_SEQUENCE_START(); // restart here on failure
if (READ_ONCE(L->owner) == my_tid) {
if (READ_ONCE(L->local_state) == STATE_LOCKED) {
RSEQ_SEQUENCE_END();
/*
* Deadlock, abort execution.
* Note that we are not necessarily actually *holding* the lock;
* this can also happen if we entered a signal handler while we
* were in the process of acquiring the lock.
* But in that case it could just as well have happened that we
* already grabbed the lock, so the caller is wrong anyway. */
fatal_error();
}
RSEQ_COMMIT(L->local_state = STATE_LOCKED);
return; /* fastpath success */
}
RSEQ_SEQUENCE_END();
/* slowpath */
/* acquire and lock owner field */
unsigned int old_owner_with_lockbit;
while (1) {
old_owner_with_lockbit = READ_ONCE(L->owner_with_lockbit);
if (old_owner_with_lockbit & OWNER_LOCKBIT) {
if (!__sync_bool_compare_and_swap (&L->owner_with_lockbit,
old_owner_with_lockbit, my_tid | OWNER_LOCKBIT | OWNER_WAITER_BIT))
continue;
futex(&L->owner_with_lockbit, FUTEX_WAIT,
old_owner_with_lockbit, NULL, NULL, 0);
continue;
} else {
if (__sync_bool_compare_and_swap (&L->owner_with_lockbit,
old_owner_with_lockbit, my_tid | OWNER_LOCKBIT))
break;
}}
/*
* ensure old owner won't lock local_state anymore.
* we only have to worry about the owner that directly preceded us here;
* it will have done this step for the owners that preceded it before clearing
* the LOCKBIT; so if we were the old owner, we don't have to sync. */
if (old_owner_with_lockbit != my_tid) {
if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ, 0, 0))
fatal_error();
}
/*
* As soon as the lock becomes STATE_FREE_OR_BIASED, we own it; but
* at this point it might still be locked.
*/
while (READ_ONCE(L->local_state) == STATE_LOCKED) {
futex(&L->local_state, FUTEX_WAIT, STATE_LOCKED, NULL, NULL, 0);
} /* OK, now the lock is biased to us and we can grab it. */ WRITE_ONCE(L->local_state, STATE_LOCKED);
/* drop lockbit */
unsigned int old_owner_with_lockbit;
while (1) {
old_owner_with_lockbit = READ_ONCE(L->owner_with_lockbit);
if (__sync_bool_compare_and_swap (&L->owner_with_lockbit,
old_owner_with_lockbit, my_tid))
break;
}
if (old_owner_with_lockbit & OWNER_WAITER_BIT)
futex(&L->owner_with_lockbit, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
}void unlock(struct biased_lock *L) {
unsigned int my_tid = THREAD_SELF->tid;
/*
* If we run before the membarrier(), the lock() path will immediately
* see the lock as uncontended, and we don't need to call futex().
* If we run after the membarrier(), the ->owner_with_lockbit read
* here will observe the new owner and we'll wake the futex. */
RSEQ_SEQUENCE_START();
unsigned int old_owner_with_lockbit = READ_ONCE(L->owner_with_lockbit); RSEQ_COMMIT(WRITE_ONCE(L->local_state, STATE_FREE_OR_BIASED)); if (old_owner_with_lockbit != my_tid)
futex(&L->local_state, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); }
Quoted 5 lines · Andrei Vagin
We already have
process_vm_readvandprocess_vm_writevto read and write to a process memory faster than we can do this with ptrace. And now it is time forprocess_vm_execthat allows executing code in an address space of another process. We can do this with ptrace but it is much slower.
I'd like to add that there are cases when using ptrace is even hardly possible:
in my situation one process needs to modify address space of another process while that target process is being blocked under pagefault. From
https://lab.nexedi.com/kirr/wendelin.core/blob/539ec405/wcfs/notes.txt#L149-171 ,
https://lab.nexedi.com/kirr/wendelin.core/blob/539ec405/wcfs/wcfs.go#L395-397 :
---- 8< ----
Client cannot be ptraced while under pagefault
==============================================
We cannot use ptrace to run code on client thread that is under pagefault:
The kernel sends SIGSTOP to interrupt tracee, but the signal will be processed only when the process returns from kernel space, e.g. here
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/entry/common.c?id=v4.19-rc8-151-g23469de647c4#n160
This way the tracer won't receive obligatory information that tracee stopped (via wait...) and even though ptrace(ATTACH) succeeds, all other ptrace commands will fail:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/ptrace.c?id=v4.19-rc8-151-g23469de647c4#n1140 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/ptrace.c?id=v4.19-rc8-151-g23469de647c4#n207
My original idea was to use ptrace to run code in process to change it's memory mappings, while the triggering process is under pagefault/read to wcfs, and the above shows it won't work - trying to ptrace the client from under wcfs will just block forever (the kernel will be waiting for read operation to finish for ptrace, and read will be first waiting on ptrace stopping to complete = deadlock)
...
// ( one could imagine adjusting mappings synchronously via running
// wcfs-trusted code via ptrace that wcfs injects into clients, but ptrace
// won't work when client thread is blocked under pagefault or syscall(^) )
---- 8< ----
To workaround that I need to add special thread into target process and implement custom additional "isolation protocol" in between my filesystem and client processes that use it:
https://lab.nexedi.com/kirr/wendelin.core/blob/539ec405/wcfs/wcfs.go#L94-182
https://lab.nexedi.com/kirr/wendelin.core/blob/539ec405/wcfs/client/wcfs.h#L20-96
https://lab.nexedi.com/kirr/wendelin.core/blob/539ec405/wcfs/client/wcfs.cpp#L24-203
Most parts of that dance would be much easier, or completely unnecessary, if it could be possible to reliably make changes to address space of target process from outside.
Kirill
Just to add to the list of use cases for PROCESS_VM_EXEC_SYSCALL, another use case is initializing a process from the "outside", instead of from the "inside" as fork requires. This can be much easier to work with. http://catern.com/rsys21.pdf goes into this use case in some depth.
It relies heavily on a remote syscall primitive:
https://github.com/catern/rsyscall. The PROCESS_VM_EXEC_SYSCALL API proposed in this patch would be a great replacement for the current implementation, which relies on running code inside the target process.
Andy Lutomirski
maintainerQuoted 4 lines · Andrei Vagin
process_vm_exechas two modes:* Execute code in an address space of a target process and stop on any signal or system call.
We already have a perfectly good context switch mechanism: context switches. If you execute code, you are basically guaranteed to be subject to being hijacked, which means you pretty much can't allow syscalls. But there's a lot of non-syscall state, and I think context switching needs to be done with extreme care.
(Just as example, suppose you switch mms, then set %gs to point to the LDT, then switch back. Now you're in a weird state. With %ss the plot is a bit thicker. And there are emulated vsyscalls and such.)
If you, PeterZ, and the UMCG could all find an acceptable, efficient way to wake-and-wait so you can switch into an injected task in the target process and switch back quickly, then I think a much nicer solution will become available.
Quoted 2 lines
* Execute a system call in an address space of a target process.
I could get behind this, but there are plenty of cans of worms to watch out for. Serious auditing would be needed.
Quoted 13 lines · Andy Lutomirski
Quoted 4 lines · Andrei Vagin
process_vm_exechas two modes:* Execute code in an address space of a target process and stop on any signal or system call.
We already have a perfectly good context switch mechanism: context switches. If you execute code, you are basically guaranteed to be subject to being hijacked, which means you pretty much can't allow syscalls. But there's a lot of non-syscall state, and I think context switching needs to be done with extreme care.
Quoted 8 lines
(Just as example, suppose you switch mms, then set %gs to point to the LDT, then switch back. Now you're in a weird state. With %ss the plot is a bit thicker. And there are emulated vsyscalls and such.)
If you, PeterZ, and the UMCG could all find an acceptable, efficient way to wake-and-wait so you can switch into an injected task in the target process and switch back quickly, then I think a much nicer solution will become available.
I know about umcg and I even did a prototype that used fuxet_swap (the previous attempt of umcg). Here are a few problems and maybe you will have some ideas on how to solve them.
The main question is how to hijack a stub process where a guest code is executing. We need to trap system calls, memory faults, and other exceptions and handle them in the Sentry (supervisor/kernel). All interested events except system calls generate signals. We can use seccomp to get signals on system calls too. In my prototype, a guest code is running in stub processes. One stub process is for each guest address space. In a stub process, I set a signal handler for SIGSEGV, SIGBUS, SIGFPE, SIGSYS, SIGILL, set an alternate signal stack, and set seccomp rules. The signal handler communicates with the Sentry (supervisor/kernel) via shared memory and uses futex_swap to make fast switches to the Sentry and back to a stub process.
Here are a few problems. First, we have a signal handler code, its stack, and a shared memory region in a guest address space, and we need to guarantee that a guest code will not be able to use them to do something unexpected.
The second problem is performance. It is much faster if we compare it with the ptrace platform, but it is still a few times slower than process_vm_exec. Signal handling is expensive. The kernel has to generate a signal frame, execute a signal handler, and then it needs to call rt_sigreturn. Futex_swap makes fast context switches, but it is still slower than process_vm_exec. UMCG should be faster because it doesn’t have a futex overhead.
Andy, what do you think about the idea to rework process_vm_exec so that it executes code and syscalls in the context of a target process? Maybe you see other ways how we can “hijack” a remote process?
Thanks,
Andrei
Quoted 6 lines
Quoted 3 lines
* Execute a system call in an address space of a target process.
I could get behind this, but there are plenty of cans of worms to watch out for. Serious auditing would be needed.
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um