SECCOMP based userspace for UML
Subsystems (3)
v2 cover SECCOMP based userspace for UML
From: Benjamin Berg <[email protected]>
Hi all,
here is an updated version of the SECCOMP patchset. The main improvement to the previous RFC version is that now FP registers will work correctly on 32 bit. I hope it is in a relatively good state overall, but I expect we will not merge this into 6.13.
The patchset adds a new userspace handling mode to UML that is based on a SECCOMP filter and trusted code within each userspace process.
One advantage of this approach is that it saves quite a few context switches when handling pagefaults (and syscalls to some extend). The reason is that the ptrace code needs a separate context switch to execute syscalls in the stub as well as another one to grab the segfault information.
Benjamin
RFCv2:
- Fix FP handling on i386
- Improved MM list for userspace sigchild handling
Benjamin Berg (9):
um: Store full CSGSFS and SS register from mcontext
um: Move faultinfo extraction into userspace routine
um: Add UML_SECCOMP configuration option
um: Add stub side of SECCOMP/futex based process handling
um: Add helper functions to get/set state for SECCOMP
um: Add SECCOMP support detection and initialization
um: Track userspace children dying in SECCOMP mode
um: Implement kernel side of SECCOMP based process handling
um: pass FD for memory operations when needed
arch/um/Kconfig | 19 + arch/um/include/asm/irq.h | 5 +- arch/um/include/asm/mmu.h | 3 + arch/um/include/shared/common-offsets.h | 4 + arch/um/include/shared/irq_user.h | 1 + arch/um/include/shared/os.h | 3 +- arch/um/include/shared/skas/mm_id.h | 13 + arch/um/include/shared/skas/skas.h | 6 + arch/um/include/shared/skas/stub-data.h | 21 +- arch/um/kernel/irq.c | 5 + arch/um/kernel/skas/mmu.c | 90 +++- arch/um/kernel/skas/stub.c | 131 +++++- arch/um/kernel/skas/stub_exe.c | 156 ++++++- arch/um/os-Linux/internal.h | 4 + arch/um/os-Linux/process.c | 31 ++ arch/um/os-Linux/registers.c | 4 +- arch/um/os-Linux/signal.c | 19 +- arch/um/os-Linux/skas/mem.c | 104 ++++- arch/um/os-Linux/skas/process.c | 493 +++++++++++++++------ arch/um/os-Linux/start_up.c | 148 ++++++- arch/x86/um/os-Linux/mcontext.c | 223 +++++++++- arch/x86/um/ptrace.c | 76 +++- arch/x86/um/shared/sysdep/kernel-offsets.h | 2 + arch/x86/um/shared/sysdep/mcontext.h | 10 + arch/x86/um/shared/sysdep/stub-data.h | 23 + arch/x86/um/shared/sysdep/stub.h | 2 + arch/x86/um/shared/sysdep/stub_32.h | 13 + arch/x86/um/shared/sysdep/stub_64.h | 14 + arch/x86/um/tls_32.c | 23 +- 29 files changed, 1437 insertions(+), 209 deletions(-) create mode 100644 arch/x86/um/shared/sysdep/stub-data.h
Signature · 1 line
2.47.0
RFC PATCH v2 1/9 um: Store full CSGSFS and SS register from mcontext
Doing this allows using registers as retrieved from an mcontext to be pushed to a process using PTRACE_SETREGS.
It is not entirely clear to me why CSGSFS was masked. Doing so creates issues when using the mcontext as process state in seccomp and simply copying the register appears to work perfectly fine for ptrace.
--- arch/x86/um/os-Linux/mcontext.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/um/os-Linux/mcontext.c +1 -2
@@ -27,7 +27,6 @@ void get_regs_from_mc(struct uml_pt_regs *regs, mcontext_t *mc)COPY (RIP );COPY2 (EFLAGS ,EFL );COPY2 (CS ,CSGSFS ); - regs->gp[CS /sizeof (unsigned long )] &=0xffff ; - regs->gp[CS /sizeof (unsigned long )] |=3 ; + regs->gp[SS /sizeof (unsigned long )] = mc->gregs[REG_CSGSFS ] >>48 ;#endif }
Signature · 1 line
2.47.0
RFC PATCH v2 2/9 um: Move faultinfo extraction into userspace routine
In reply to Benjamin BergThe segv handler is called slightly differently depending on whether PTRACE_FULL_FAULTINFO is set or not (32bit vs. 64bit). The only difference is that we don't try to pass the registers and instruction pointer to the segv handler.
It would be good to either document or remove the difference, but I do not know why this difference exists. And, passing NULL can even result in a crash.
--- arch/um/os-Linux/skas/process.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
arch/um/os-Linux/skas/process.c +6 -11
@@ -163,12 +163,6 @@ static void get_skas_faultinfo(int pid, struct faultinfo *fi)memcpy (fi, (void *)current_stub_stack (),sizeof (*fi)); } -static void handle_segv (int pid,struct uml_pt_regs *regs) -{ -get_skas_faultinfo (pid, ®s->faultinfo); -segv (regs->faultinfo,0 ,1 ,NULL ); -} -static void handle_trap (int pid,struct uml_pt_regs *regs) {if ((UPT_IP (regs) >=STUB_START ) && (UPT_IP (regs) <STUB_END ))
@@ -509,13 +503,14 @@ void userspace(struct uml_pt_regs *regs)switch (sig) {case SIGSEGV : -if (PTRACE_FULL_FAULTINFO ) { -get_skas_faultinfo (pid, - ®s->faultinfo); +get_skas_faultinfo (pid, ®s->faultinfo); + +if (PTRACE_FULL_FAULTINFO ) (*sig_info[SIGSEGV ])(SIGSEGV , (struct siginfo *)&si, regs); - } -else handle_segv (pid, regs); +else +segv (regs->faultinfo,0 ,1 ,NULL ); +break ;case SIGTRAP +0x80 :handle_trap (pid, regs);
Signature · 1 line
2.47.0
RFC PATCH v2 3/9 um: Add UML_SECCOMP configuration option
In reply to Benjamin BergAdd the UML_SECCOMP configuration options. The next commits will add the support itself in smaller chunks.
Only x86_64 will be supported for now.
---
RFCv2:
- Remove "default n"
--- arch/um/Kconfig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/um/Kconfig +19 -0
@@ -257,6 +257,25 @@ config KASAN_SHADOW_OFFSET set to a large value. On low-memory systems, try 0x7fff8000, as it fits into the immediate of most instructions, improving performance. +config UML_SECCOMP + bool "SECCOMP based userspace" + help + With SECCOMP userspace processes work collaboratively with the kernel + instead of being traced using ptrace. All syscalls from the application + are caught and redirected using a signal. This signal handler in turn + is permitted to do the selected set of syscalls to communicate with + the UML kernel and do the required memory management. + + This method is overall faster than the ptrace based userspace, + primarily because it reduces the number of context switches for + (minor) page faults. + However, the SECCOMP filter is not (yet) restrictive enough to prevent + userspace from reading and writing all physical memory. Userspace + processes could also trick the stub into disabling SIGALRM which + prevents it from being interrupted for scheduling purposes. + + If in doubt say N, as the feature has security implications. + endmenu source "arch/um/drivers/Kconfig"
Signature · 1 line
2.47.0
RFC PATCH v2 4/9 um: Add stub side of SECCOMP/futex based process handling
In reply to Benjamin BergThis adds the stub side for the new seccomp process management code. In this case we do register save/restore through the signal handler mcontext. For the FS_BASE/GS_BASE register we need special handling.
---
RFCv2:
- Add include guards into new architecture specific header file
--- arch/um/include/shared/common-offsets.h | 2 + arch/um/include/shared/skas/stub-data.h | 15 +++++++ arch/um/kernel/skas/stub.c | 53 +++++++++++++++++++++++++ arch/x86/um/shared/sysdep/stub-data.h | 23 +++++++++++ arch/x86/um/shared/sysdep/stub.h | 2 + arch/x86/um/shared/sysdep/stub_32.h | 13 ++++++ arch/x86/um/shared/sysdep/stub_64.h | 14 +++++++ 7 files changed, 122 insertions(+) create mode 100644 arch/x86/um/shared/sysdep/stub-data.h
arch/um/include/shared/common-offsets.h +2 -0
@@ -33,3 +33,5 @@ DEFINE(UML_CONFIG_UML_MAX_USERSPACE_ITERATIONS, CONFIG_UML_MAX_USERSPACE_ITERATI#else DEFINE (UML_CONFIG_UML_MAX_USERSPACE_ITERATIONS ,0 );#endif + +DEFINE (UM_KERN_GDT_ENTRY_TLS_ENTRIES ,GDT_ENTRY_TLS_ENTRIES );
arch/um/include/shared/skas/stub-data.h +15 -0
@@ -8,9 +8,14 @@ #ifndef __STUB_DATA_H #define __STUB_DATA_H +#include <linux/kconfig.h> #include <linux/compiler_types.h> #include <as-layout.h> #include <sysdep/tls.h> +#include <sysdep/stub-data.h> + +#define FUTEX_IN_CHILD 0 +#define FUTEX_IN_KERN 1 struct stub_init_data { unsigned long stub_start;
@@ -52,6 +57,16 @@ struct stub_data {/* 128 leaves enough room for additional fields in the struct */ struct stub_syscall syscall_data[(UM_KERN_PAGE_SIZE -128 ) /sizeof (struct stub_syscall )]__aligned(16) ; +/* data shared with signal handler (only used in seccomp mode) */ +short restart_wait; +unsigned int futex; +int signal; +unsigned short si_offset; +unsigned short mctx_offset; + +/* seccomp architecture specific state restore */ +struct stub_data_arch arch_data; +/* Stack for our signal handlers and for calling into . */ unsigned char sigstack[UM_KERN_PAGE_SIZE ]__aligned(UM_KERN_PAGE_SIZE) ; };
arch/um/kernel/skas/stub.c +53 -0
62 lines not on this page · raw message
arch/x86/um/shared/sysdep/stub-data.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ARCH_STUB_DATA_H +#define __ARCH_STUB_DATA_H + +#ifdef __i386__ +#include <generated/asm-offsets.h> +#include <asm/ldt.h> + +struct stub_data_arch { +int sync; +struct user_desc tls[UM_KERN_GDT_ENTRY_TLS_ENTRIES ]; +}; +#else +#define STUB_SYNC_FS_BASE (1 << 0) +#define STUB_SYNC_GS_BASE (1 << 1) +struct stub_data_arch { +int sync; +unsigned long fs_base; +unsigned long gs_base; +}; +#endif + +#endif /* __ARCH_STUB_DATA_H */
arch/x86/um/shared/sysdep/stub.h +2 -0
@@ -13,3 +13,5 @@extern void stub_segv_handler (int ,siginfo_t *,void *);extern void stub_syscall_handler (void ); +extern void stub_signal_interrupt (int ,siginfo_t *,void *); +extern void stub_signal_restorer (void );
arch/x86/um/shared/sysdep/stub_32.h +13 -0
@@ -131,4 +131,17 @@ static __always_inline void *get_stub_data(void) "call *%%eax ;" \ :: "i" ((1 + STUB_DATA_PAGES) * UM_KERN_PAGE_SIZE), \ "i" (&fn)) + +static __always_inline void +stub_seccomp_restore_state (struct stub_data_arch *arch) +{ +for (int i =0 ; i <sizeof (arch->tls) /sizeof (arch->tls[0 ]); i++) { +if (arch->sync & (1 << i)) +stub_syscall1 (__NR_set_thread_area, + (unsigned long ) &arch->tls[i]); + } + + arch->sync =0 ; +} +#endif
arch/x86/um/shared/sysdep/stub_64.h +14 -0
@@ -10,6 +10,7 @@#include <sysdep/ptrace_user.h> #include <generated/asm-offsets.h> #include <linux/stddef.h> +#include <asm/prctl.h> #define STUB_MMAP_NR __NR_mmap#define MMAP_OFFSET (o) (o)
@@ -134,4 +135,17 @@ static __always_inline void *get_stub_data(void) "call *%%rax ;" \ :: "i" ((1 + STUB_DATA_PAGES) * UM_KERN_PAGE_SIZE), \ "i" (&fn)) + +static __always_inline void +stub_seccomp_restore_state (struct stub_data_arch *arch) +{ +/* TODO: Use _writefsbase_u64/_writegsbase_u64 when possible */ +if (arch->sync &STUB_SYNC_FS_BASE ) +stub_syscall2 (__NR_arch_prctl,ARCH_SET_FS , arch->fs_base); +if (arch->sync &STUB_SYNC_GS_BASE ) +stub_syscall2 (__NR_arch_prctl,ARCH_SET_GS , arch->gs_base); + + arch->sync =0 ; +} +#endif
Signature · 1 line
2.47.0
RFC PATCH v2 5/9 um: Add helper functions to get/set state for SECCOMP
In reply to Benjamin BergWhen not using ptrace, we need to both save and restore registers through the mcontext as provided by the host kernel to our signal handlers.
Add corresponding functions to store the state to an mcontext and helpers to access the mcontext of the subprocess through the stub data.
---
RFCv2:
- Proper FP register handling
--- arch/x86/um/os-Linux/mcontext.c | 220 ++++++++++++++++++++++++++- arch/x86/um/ptrace.c | 76 ++++++--- arch/x86/um/shared/sysdep/mcontext.h | 10 ++ 3 files changed, 286 insertions(+), 20 deletions(-)
arch/x86/um/os-Linux/mcontext.c +219 -1
@@ -1,9 +1,12 @@// SPDX-License-Identifier: GPL-2.0 -#include <sys/ucontext.h> #define __FRAME_OFFSETS +#include <linux/errno.h> +#include <linux/string.h> +#include <sys/ucontext.h> #include <asm/ptrace.h> #include <sysdep/ptrace.h> #include <sysdep/mcontext.h> +#include <asm/sigcontext.h> void get_regs_from_mc (struct uml_pt_regs *regs,mcontext_t *mc) {
@@ -17,6 +20,10 @@ void get_regs_from_mc(struct uml_pt_regs *regs, mcontext_t *mc)COPY2 (UESP ,ESP );/* sic */ COPY (EBX );COPY (EDX );COPY (ECX );COPY (EAX );COPY (EIP );COPY_SEG_CPL3 (CS );COPY (EFL );COPY_SEG_CPL3 (SS ); +#undef COPY2 +#undef COPY +#undef COPY_SEG +#undef COPY_SEG_CPL3#else #define COPY2 (X ,Y ) regs->gp[X/sizeof(unsigned long)] = mc->gregs[REG_##Y]#define COPY (X ) regs->gp[X/sizeof(unsigned long)] = mc->gregs[REG_##X]
@@ -28,5 +35,216 @@ void get_regs_from_mc(struct uml_pt_regs *regs, mcontext_t *mc)COPY2 (EFLAGS ,EFL );COPY2 (CS ,CSGSFS ); regs->gp[SS /sizeof (unsigned long )] = mc->gregs[REG_CSGSFS ] >>48 ; +#undef COPY2 +#undef COPY +#endif +} + +#ifdef CONFIG_UML_SECCOMP +/* Same thing, but the copy macros are turned around. */ +void get_mc_from_regs (struct uml_pt_regs *regs,mcontext_t *mc,int single_stepping) +{ +#ifdef __i386__ +#define COPY2 (X ,Y ) mc->gregs[REG_##Y] = regs->gp[X] +#define COPY (X ) mc->gregs[REG_##X] = regs->gp[X] +#define COPY_SEG (X ) mc->gregs[REG_##X] = regs->gp[X] & 0xffff; +#define COPY_SEG_CPL3 (X ) mc->gregs[REG_##X] = (regs->gp[X] & 0xffff) | 3; +COPY_SEG (GS );COPY_SEG (FS );COPY_SEG (ES );COPY_SEG (DS ); +COPY (EDI );COPY (ESI );COPY (EBP ); +COPY2 (UESP ,ESP );/* sic */ +COPY (EBX );COPY (EDX );COPY (ECX );COPY (EAX ); +COPY (EIP );COPY_SEG_CPL3 (CS );COPY (EFL );COPY_SEG_CPL3 (SS ); +#else +#define COPY2 (X ,Y ) mc->gregs[REG_##Y] = regs->gp[X/sizeof(unsigned long)] +#define COPY (X ) mc->gregs[REG_##X] = regs->gp[X/sizeof(unsigned long)] +COPY (R8 );COPY (R9 );COPY (R10 );COPY (R11 ); +COPY (R12 );COPY (R13 );COPY (R14 );COPY (R15 ); +COPY (RDI );COPY (RSI );COPY (RBP );COPY (RBX ); +COPY (RDX );COPY (RAX );COPY (RCX );COPY (RSP ); +COPY (RIP ); +COPY2 (EFLAGS ,EFL ); + mc->gregs[REG_CSGSFS ] = mc->gregs[REG_CSGSFS ] &0xffffffffffffl ; + mc->gregs[REG_CSGSFS ] |= (regs->gp[SS /sizeof (unsigned long )] &0xffff ) <<48 ;#endif + +if (single_stepping) + mc->gregs[REG_EFL ] |=X86_EFLAGS_TF ; +else + mc->gregs[REG_EFL ] &= ~X86_EFLAGS_TF ; +} + +#ifdef CONFIG_X86_32 +struct _xstate_64 { +struct _fpstate_64 fpstate; +struct _header xstate_hdr; +struct _ymmh_state ymmh; +/* New processor state extensions go here: */ +}; + +/* Not quite the right structures as these contain more information */ +int um_i387_from_fxsr (struct _fpstate_32 *i387, +const struct _fpstate_64 *fxsave); +int um_fxsr_from_i387 (struct _fpstate_64 *fxsave, +const struct _fpstate_32 *from); +#else +#define _xstate_64 _xstate +#endif + +static struct _fpstate *get_fpstate (struct stub_data *data, +mcontext_t *mcontext, +int *fp_size) +{ +struct _fpstate *res; + +/* Assume floating point registers are on the same page */ + res = (void *)(((unsigned long )mcontext->fpregs & + (UM_KERN_PAGE_SIZE -1 )) + + (unsigned long )&data->sigstack[0 ]); + +if ((void *)res +sizeof (struct _fpstate ) > + (void *)data->sigstack +sizeof (data->sigstack)) +return NULL ; + +if (res->sw_reserved.magic1 !=FP_XSTATE_MAGIC1 ) { + *fp_size =sizeof (struct _fpstate ); + }else { +char *magic2_addr; + + magic2_addr = (void *)res; + magic2_addr += res->sw_reserved.extended_size; + magic2_addr -=FP_XSTATE_MAGIC2_SIZE ; + +/* We still need to be within our stack */ +if ((void *)magic2_addr > + (void *)data->sigstack +sizeof (data->sigstack)) +return NULL ; + +/* If we do not read MAGIC2, then we did something wrong */ +if (*(__u32 *)magic2_addr !=FP_XSTATE_MAGIC2 ) +return NULL ; + +/* Remove MAGIC2 from the size, we do not save/restore it */ + *fp_size = res->sw_reserved.extended_size - +FP_XSTATE_MAGIC2_SIZE ; + } + +return res; } + +int get_stub_state (struct uml_pt_regs *regs,struct stub_data *data, +unsigned long *fp_size_out) +{ +mcontext_t *mcontext; +struct _fpstate *fpstate_stub; +struct _xstate_64 *xstate_stub; +int fp_size, xstate_size; + +/* mctx_offset is verified by wait_stub_done_seccomp */ + mcontext = (void *)&data->sigstack[data->mctx_offset]; + +get_regs_from_mc (regs, mcontext); + + fpstate_stub =get_fpstate (data, mcontext, &fp_size); +if (!fpstate_stub) +return -EINVAL ; + +#ifdef CONFIG_X86_32 + xstate_stub = (void *)&fpstate_stub->_fxsr_env; + xstate_size = fp_size - offsetof(struct _fpstate_32 , _fxsr_env); +#else + xstate_stub = (void *)fpstate_stub; + xstate_size = fp_size; +#endif + +if (fp_size_out) + *fp_size_out = xstate_size; + +if (xstate_size > host_fp_size) +return -ENOSPC ; + +memcpy (®s->fp, xstate_stub, xstate_size); + +/* We do not need to read the x86_64 FS_BASE/GS_BASE registers as +* we do not permit userspace to set them directly. +*/ + +#ifdef CONFIG_X86_32 +/* Read the i387 legacy FP registers */ +if (um_fxsr_from_i387 ((void *)®s->fp, fpstate_stub)) +return -EINVAL ; +#endif + +return 0 ; +} + +/* Copied because we cannot include regset.h here. */ +struct task_struct ; +struct user_regset ; +struct membuf { +void *p; +size_t left; +}; + +int fpregs_legacy_get (struct task_struct *target, +const struct user_regset *regset, +struct membuf to); + +int set_stub_state (struct uml_pt_regs *regs,struct stub_data *data, +int single_stepping) +{ +mcontext_t *mcontext; +struct _fpstate *fpstate_stub; +struct _xstate_64 *xstate_stub; +int fp_size, xstate_size; + +/* mctx_offset is verified by wait_stub_done_seccomp */ + mcontext = (void *)&data->sigstack[data->mctx_offset]; + +if ((unsigned long )mcontext < (unsigned long )data->sigstack || + (unsigned long )mcontext > + (unsigned long ) data->sigstack + +sizeof (data->sigstack) -sizeof (*mcontext)) +return -EINVAL ; + +get_mc_from_regs (regs, mcontext, single_stepping); + + fpstate_stub =get_fpstate (data, mcontext, &fp_size); +if (!fpstate_stub) +return -EINVAL ; + +#ifdef CONFIG_X86_32 + xstate_stub = (void *)&fpstate_stub->_fxsr_env; + xstate_size = fp_size - offsetof(struct _fpstate_32 , _fxsr_env); +#else + xstate_stub = (void *)fpstate_stub; + xstate_size = fp_size; +#endif + +memcpy (fpstate_stub, ®s->fp, fp_size); + +#ifdef __i386__ +/* +* On x86, the GDT entries are updated by arch_set_tls. +*/ + +/* Store the i387 legacy FP registers which the host will use */ +if (um_i387_from_fxsr (fpstate_stub, (void *)®s->fp)) +return -EINVAL ; +#else +/* +* On x86_64, we need to sync the FS_BASE/GS_BASE registers using the +* arch specific data. +*/ +if (data->arch_data.fs_base != regs->gp[FS_BASE /sizeof (unsigned long )]) { + data->arch_data.fs_base = regs->gp[FS_BASE /sizeof (unsigned long )]; + data->arch_data.sync |=STUB_SYNC_FS_BASE ; + } +if (data->arch_data.gs_base != regs->gp[GS_BASE /sizeof (unsigned long )]) { + data->arch_data.gs_base = regs->gp[GS_BASE /sizeof (unsigned long )]; + data->arch_data.sync |=STUB_SYNC_GS_BASE ; + } +#endif + +return 0 ; +} +#endif
arch/x86/um/ptrace.c +57 -19
105 lines not on this page · raw message
arch/x86/um/shared/sysdep/mcontext.h +10 -0
@@ -6,7 +6,17 @@#ifndef __SYS_SIGCONTEXT_X86_H#define __SYS_SIGCONTEXT_X86_H +#include <linux/kconfig.h> +#include <stub-data.h> +extern void get_regs_from_mc (struct uml_pt_regs *,mcontext_t *); +extern void get_mc_from_regs (struct uml_pt_regs *regs,mcontext_t *mc, +int single_stepping); + +extern int get_stub_state (struct uml_pt_regs *regs,struct stub_data *data, +unsigned long *fp_size_out); +extern int set_stub_state (struct uml_pt_regs *regs,struct stub_data *data, +int single_stepping);#ifdef __i386__
Signature · 1 line
2.47.0
RFC PATCH v2 6/9 um: Add SECCOMP support detection and initialization
In reply to Benjamin BergThis detects seccomp support, sets the global using_seccomp variable and initilizes the exec registers. For now, the implementation simply falls through to the ptrace startup code, meaning that it is unused.
--- arch/um/include/shared/skas/skas.h | 6 ++ arch/um/os-Linux/registers.c | 4 +- arch/um/os-Linux/skas/process.c | 3 + arch/um/os-Linux/start_up.c | 151 ++++++++++++++++++++++++++++- 4 files changed, 160 insertions(+), 4 deletions(-)
arch/um/include/shared/skas/skas.h +6 -0
@@ -6,8 +6,14 @@#ifndef __SKAS_H#define __SKAS_H +#include <linux/kconfig.h> #include <sysdep/ptrace.h> +#ifdef CONFIG_UML_SECCOMP +extern int using_seccomp; +#else +#define using_seccomp 0 +#endif extern int userspace_pid[];extern void new_thread_handler (void );
arch/um/os-Linux/registers.c +2 -2
@@ -14,8 +14,8 @@/* This is set once at boot time and not changed thereafter */ -static unsigned long exec_regs[MAX_REG_NR ]; -static unsigned long *exec_fp_regs; +unsigned long exec_regs[MAX_REG_NR ]; +unsigned long *exec_fp_regs;int init_pid_registers (int pid) {
arch/um/os-Linux/skas/process.c +3 -0
@@ -299,6 +299,9 @@ static int __init init_stub_exe_fd(void) }__initcall (init_stub_exe_fd); +#ifdef CONFIG_UML_SECCOMP +int using_seccomp; +#endif int userspace_pid[NR_CPUS ];/**
arch/um/os-Linux/start_up.c +149 -2
182 lines not on this page · raw message
Signature · 1 line
2.47.0
RFC PATCH v2 7/9 um: Track userspace children dying in SECCOMP mode
In reply to Benjamin BergWhen in seccomp mode, we would hang forever on the futex if a child has died unexpectedly. In contrast, ptrace mode will notice it and kill the corresponding thread when it fails to run it.
Fix this issue using a new IRQ that is fired after a SIGCHLD and keeping an (internal) list of all MMs. In the IRQ handler, find the affected MM and set its PID to -1 as well as the futex variable to FUTEX_IN_KERN.
This, together with futex returning -EINTR after the signal is sufficient to implement a race-free detection of a child dying.
---
RFCv2:
- Use "struct list_head" for the list by placing it into the mm_context
--- arch/um/include/asm/irq.h | 5 +- arch/um/include/asm/mmu.h | 3 ++ arch/um/include/shared/irq_user.h | 1 + arch/um/include/shared/os.h | 1 + arch/um/include/shared/skas/mm_id.h | 2 + arch/um/kernel/irq.c | 5 ++ arch/um/kernel/skas/mmu.c | 83 +++++++++++++++++++++++++++-- arch/um/os-Linux/process.c | 31 +++++++++++ arch/um/os-Linux/signal.c | 19 ++++++- 9 files changed, 142 insertions(+), 8 deletions(-)
arch/um/include/asm/irq.h +3 -2
@@ -13,17 +13,18 @@#define TELNETD_IRQ 8#define XTERM_IRQ 9#define RANDOM_IRQ 10 +#define SIGCHLD_IRQ 11#ifdef CONFIG_UML_NET_VECTOR -#define VECTOR_BASE_IRQ (RANDOM_IRQ + 1) +#define VECTOR_BASE_IRQ (SIGCHLD_IRQ + 1)#define VECTOR_IRQ_SPACE 8#define UM_FIRST_DYN_IRQ (VECTOR_IRQ_SPACE + VECTOR_BASE_IRQ)#else -#define UM_FIRST_DYN_IRQ (RANDOM_IRQ + 1) +#define UM_FIRST_DYN_IRQ (SIGCHLD_IRQ + 1)#endif
arch/um/include/asm/mmu.h +3 -0
@@ -6,11 +6,14 @@ #ifndef __ARCH_UM_MMU_H #define __ARCH_UM_MMU_H +#include "linux/types.h" #include <mm_id.h> typedef struct mm_context { struct mm_id id; + struct list_head list; + /* Address range in need of a TLB sync */ unsigned long sync_tlb_range_from; unsigned long sync_tlb_range_to;
arch/um/include/shared/irq_user.h +1 -0
@@ -16,6 +16,7 @@ enum um_irq_type {struct siginfo ;extern void sigio_handler (int sig,struct siginfo *unused_si,struct uml_pt_regs *regs); +extern void sigchld_handler (int sig,struct siginfo *unused_si,struct uml_pt_regs *regs);void sigio_run_timetravel_handlers (void );extern void free_irq_by_fd (int fd);extern void deactivate_fd (int fd,int irqnum);
arch/um/include/shared/os.h +1 -0
@@ -199,6 +199,7 @@ extern int create_mem_file(unsigned long long len);extern void report_enomem (void );/* process.c */ +pid_t os_reap_child (void );extern void os_alarm_process (int pid);extern void os_kill_process (int pid,int reap_child);extern void os_kill_ptraced_process (int pid,int reap_child);
arch/um/include/shared/skas/mm_id.h +2 -0
@@ -14,4 +14,6 @@ struct mm_id {void __switch_mm (struct mm_id *mm_idp); +void notify_mm_kill (int pid); +#endif
arch/um/kernel/irq.c +5 -0
@@ -786,3 +786,8 @@ unsigned long from_irq_stack(int nested)return mask & ~1 ; } +extern void sigchld_handler (int sig,struct siginfo *unused_si, +struct uml_pt_regs *regs) +{ +do_IRQ (SIGCHLD_IRQ , regs); +}
arch/um/kernel/skas/mmu.c +78 -5
112 lines not on this page · raw message
arch/um/os-Linux/process.c +31 -0
@@ -17,17 +17,29 @@#include <init.h> #include <longjmp.h> #include <os.h> +#include <skas/skas.h> void os_alarm_process (int pid) { +if (pid <=0 ) +return ; +kill (pid,SIGALRM ); }void os_kill_process (int pid,int reap_child) { +if (pid <=0 ) +return ; + +/* Block signals until child is reaped */ +block_signals (); +kill (pid,SIGKILL );if (reap_child)CATCH_EINTR (waitpid (pid,NULL , __WALL)); + +unblock_signals (); }/* Kill off a ptraced child by all means available. kill it normally first,
@@ -37,11 +49,27 @@ void os_kill_process(int pid, int reap_child)void os_kill_ptraced_process (int pid,int reap_child) { +if (pid <=0 ) +return ; + +/* Block signals until child is reaped */ +block_signals (); +kill (pid,SIGKILL );ptrace (PTRACE_KILL , pid);ptrace (PTRACE_CONT , pid);if (reap_child)CATCH_EINTR (waitpid (pid,NULL , __WALL)); + +unblock_signals (); +} + +pid_t os_reap_child (void ) +{ +int status; + +/* Try to reap a child */ +return waitpid (-1 , &status,WNOHANG ); }/* Don't use the glibc version, which caches the result in TLS. It misses some
@@ -201,5 +229,8 @@ void init_new_thread_signals(void)set_handler (SIGBUS );signal (SIGHUP ,SIG_IGN );set_handler (SIGIO ); +/* We (currently) only use the child reaper IRQ in seccomp mode */ +if (using_seccomp) +set_handler (SIGCHLD );signal (SIGWINCH ,SIG_IGN ); }
arch/um/os-Linux/signal.c +18 -1
@@ -29,6 +29,7 @@ void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = { [SIGBUS] = relay_signal, [SIGSEGV] = segv_handler, [SIGIO] = sigio_handler, + [SIGCHLD] = sigchld_handler, };static void sig_handler_common (int sig,struct siginfo *si,mcontext_t *mc)
@@ -44,7 +45,7 @@ static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) }/* enable signals if sig isn't IRQ signal */ -if ((sig !=SIGIO ) && (sig !=SIGWINCH )) +if ((sig !=SIGIO ) && (sig !=SIGWINCH ) && (sig !=SIGCHLD ))unblock_signals_trace (); (*sig_info[sig])(sig, si, &r);
@@ -64,6 +65,9 @@ static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)#define SIGALRM_BIT 1#define SIGALRM_MASK (1 << SIGALRM_BIT) +#define SIGCHLD_BIT 2 +#define SIGCHLD_MASK (1 << SIGCHLD_BIT) +int signals_enabled;#ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT static int signals_blocked, signals_blocked_pending;
@@ -102,6 +106,11 @@ static void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)return ; } +if (!enabled && (sig ==SIGCHLD )) { + signals_pending |=SIGCHLD_MASK ; +return ; + } +block_signals_trace ();sig_handler_common (sig, si, mc);
@@ -181,6 +190,8 @@ static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = { [SIGIO] = sig_handler, [SIGWINCH] = sig_handler, +/* SIGCHLD is only actually registered in seccomp mode. */ + [SIGCHLD] = sig_handler, [SIGALRM] = timer_alarm_handler, [SIGUSR1] = sigusr1_handler,
@@ -344,6 +355,12 @@ void unblock_signals(void)if (save_pending &SIGIO_MASK )sig_handler_common (SIGIO ,NULL ,NULL ); +if (save_pending &SIGCHLD_MASK ) { +struct uml_pt_regs regs = {}; + +sigchld_handler (SIGCHLD ,NULL , ®s); + } +/* Do not reenter the handler */ if ((save_pending &SIGALRM_MASK ) && (!(signals_active &SIGALRM_MASK )))
Signature · 1 line
2.47.0
RFC PATCH v2 8/9 um: Implement kernel side of SECCOMP based process handling
In reply to Benjamin BergThis adds the kernel side of the seccomp based process handling.
--- arch/um/include/shared/common-offsets.h | 2 + arch/um/include/shared/os.h | 2 +- arch/um/include/shared/skas/stub-data.h | 5 +- arch/um/kernel/skas/mmu.c | 6 +- arch/um/kernel/skas/stub_exe.c | 141 +++++++- arch/um/os-Linux/internal.h | 4 + arch/um/os-Linux/skas/mem.c | 38 ++- arch/um/os-Linux/skas/process.c | 374 +++++++++++++++------ arch/um/os-Linux/start_up.c | 3 - arch/x86/um/shared/sysdep/kernel-offsets.h | 2 + arch/x86/um/tls_32.c | 23 +- 11 files changed, 460 insertions(+), 140 deletions(-)
arch/um/include/shared/common-offsets.h +2 -0
@@ -35,3 +35,5 @@ DEFINE(UML_CONFIG_UML_MAX_USERSPACE_ITERATIONS, 0);#endif DEFINE (UM_KERN_GDT_ENTRY_TLS_ENTRIES ,GDT_ENTRY_TLS_ENTRIES ); + +DEFINE (UM_SECCOMP_ARCH_NATIVE ,SECCOMP_ARCH_NATIVE );
arch/um/include/shared/os.h +1 -1
@@ -283,7 +283,7 @@ int unmap(struct mm_id *mm_idp, unsigned long addr, unsigned long len);/* skas/process.c */ extern int is_skas_winch (int pid,int fd,void *data); -extern int start_userspace (unsigned long stub_stack); +extern int start_userspace (struct mm_id *mm_id);extern void userspace (struct uml_pt_regs *regs);extern void new_thread (void *stack,jmp_buf *buf,void (*handler)(void ));extern void switch_threads (jmp_buf *me,jmp_buf *you);
arch/um/include/shared/skas/stub-data.h +4 -1
@@ -18,6 +18,8 @@#define FUTEX_IN_KERN 1struct stub_init_data { +int seccomp; +unsigned long stub_start;int stub_code_fd;
@@ -25,7 +27,8 @@ struct stub_init_data {int stub_data_fd;unsigned long stub_data_offset; -unsigned long segv_handler; +unsigned long signal_handler; +unsigned long signal_restorer; };#define STUB_NEXT_SYSCALL (s) \
arch/um/kernel/skas/mmu.c +2 -4
@@ -39,13 +39,11 @@ int init_new_context(struct task_struct *task, struct mm_struct *mm)/* Insert into list, used for lookups when the child dies */ list_add (&mm->context.list, &mm_list); - new_id->pid =start_userspace (stack); + ret =start_userspace (new_id); } -if (new_id->pid <0 ) { - ret = new_id->pid; +if (ret <0 ) goto out_free; - }/* Ensure the new MM is clean and nothing unwanted is mapped */ unmap (new_id,0 ,STUB_START );
arch/um/kernel/skas/stub_exe.c +130 -11
168 lines not on this page · raw message
arch/um/os-Linux/internal.h +4 -0
@@ -2,6 +2,9 @@#ifndef __UM_OS_LINUX_INTERNAL_H#define __UM_OS_LINUX_INTERNAL_H +#include <mm_id.h> +#include <stub-data.h> +/* * elf_aux.c */
@@ -16,5 +19,6 @@ void check_tmpexec(void);* skas/process.c */ void wait_stub_done (int pid); +void wait_stub_done_seccomp (struct mm_id *mm_idp,int running,int wait_sigsys);#endif /* __UM_OS_LINUX_INTERNAL_H */
arch/um/os-Linux/skas/mem.c +22 -16
@@ -4,6 +4,7 @@* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) */ +#include <linux/kconfig.h> #include <stddef.h> #include <unistd.h> #include <errno.h>
@@ -80,27 +81,32 @@ static inline long do_syscall_stub(struct mm_id *mm_idp)int n, i;int err, pid = mm_idp->pid; - n =ptrace_setregs (pid, syscall_regs); -if (n <0 ) { -printk (UM_KERN_ERR "Registers - \n" ); -for (i =0 ; i <MAX_REG_NR ; i++) -printk (UM_KERN_ERR "\t%d\t0x%lx\n" , i, syscall_regs[i]); -panic ("%s : PTRACE_SETREGS failed, errno = %d\n" , - __func__, -n); - } -/* Inform process how much we have filled in. */ proc_data->syscall_data_len = mm_idp->syscall_data_len; - err =ptrace (PTRACE_CONT , pid,0 ,0 ); -if (err) -panic ("Failed to continue stub, pid = %d, errno = %d\n" , pid, - errno); - -wait_stub_done (pid); +if (using_seccomp) { + proc_data->restart_wait =1 ; +wait_stub_done_seccomp (mm_idp,0 ,1 ); + }else { + n =ptrace_setregs (pid, syscall_regs); +if (n <0 ) { +printk (UM_KERN_ERR "Registers -\n" ); +for (i =0 ; i <MAX_REG_NR ; i++) +printk (UM_KERN_ERR "\t%d\t0x%lx\n" , i, syscall_regs[i]); +panic ("%s : PTRACE_SETREGS failed, errno = %d\n" , + __func__, -n); + } + + err =ptrace (PTRACE_CONT , pid,0 ,0 ); +if (err) +panic ("Failed to continue stub, pid = %d, errno = %d\n" , + pid, errno); + +wait_stub_done (pid); + }/* -* proc_data->err will be non-zero if there was an (unexpected) error. +* proc_data->err will be negative if there was an (unexpected) error. * In that case, syscall_data_len points to the last executed syscall, * otherwise it will be zero (but we do not need to rely on that). */
arch/um/os-Linux/skas/process.c +274 -100
478 lines not on this page · raw message
arch/um/os-Linux/start_up.c +0 -3
@@ -429,12 +429,9 @@ void __init os_early_checks(void) using_seccomp =0 ;if (init_seccomp ()) { -/* Not yet fully implemented */ -#if 0 using_seccomp =1 ;return ; -#endif }#endif
arch/x86/um/shared/sysdep/kernel-offsets.h +2 -0
@@ -4,7 +4,9 @@#include <linux/elf.h> #include <linux/crypto.h> #include <linux/kbuild.h> +#include <linux/audit.h> #include <asm/mman.h> +#include <asm/seccomp.h> /* workaround for a warning with -Wmissing-prototypes */ void foo (void );
arch/x86/um/tls_32.c +19 -4
@@ -12,6 +12,7 @@#include <skas.h> #include <sysdep/tls.h> #include <asm/desc.h> +#include <stub-data.h> /* * If needed we can detect when it's uninitialized.
@@ -21,13 +22,27 @@static int host_supports_tls =-1 ;int host_gdt_entry_tls_min; -static int do_set_thread_area (struct user_desc *info) +static int do_set_thread_area (struct task_struct * task,struct user_desc *info) {int ret;u32 cpu; +if (info->entry_number < host_gdt_entry_tls_min || + info->entry_number >= host_gdt_entry_tls_min +GDT_ENTRY_TLS_ENTRIES ) +return -EINVAL ; + +if (using_seccomp) { +int idx = info->entry_number - host_gdt_entry_tls_min; +struct stub_data *data = (void *)task->mm->context.id.stack; + + data->arch_data.tls[idx] = *info; + data->arch_data.sync |=BIT (idx); + +return 0 ; + } + cpu =get_cpu (); - ret =os_set_thread_area (info, userspace_pid[cpu]); + ret =os_set_thread_area (info, task->mm->context.id.pid);put_cpu ();if (ret)
@@ -97,7 +112,7 @@ static int load_TLS(int flags, struct task_struct *to)if (!(flags &O_FORCE ) && curr->flushed)continue ; - ret =do_set_thread_area (&curr->tls); + ret =do_set_thread_area (current, &curr->tls);if (ret) goto out;
@@ -275,7 +290,7 @@ SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, user_desc)return -EFAULT ; } - ret =do_set_thread_area (&info); + ret =do_set_thread_area (current, &info);if (ret)return ret;return set_tls_entry (current, &info, idx,1 );
Signature · 1 line
2.47.0
RFC PATCH v2 9/9 um: pass FD for memory operations when needed
In reply to Benjamin BergFrom: Benjamin Berg <[email protected]>
Instead of always sharing the FDs with the userspace process, only hand over the FDs needed for mmap when required. The idea is that userspace might be able to force the stub into executing an mmap syscall, however, it will not be able to manipulate the control flow sufficiently to have access to an FD that would allow mapping arbitrary memory.
Security wise, we need to be sure that only the expected syscalls are executed after the kernel sends FDs through the socket. This is currently not the case, as userspace can trivially jump the rt_sigreturn syscall instruction to execute any syscall that the stub is permitted to do. With this, it can trick the kernel to send the FD, which in turn allows userspace to freely map any physical memory.
As such, this is currently *not* secure. However, in principle the approach should be fine with a more strict SECCOMP filter and a careful review of the stub control flow (as userspace can prepare a stack). With some care, it is likely possible to extend the security model to SMP if desired.
--- arch/um/include/shared/skas/mm_id.h | 11 ++ arch/um/include/shared/skas/stub-data.h | 1 + arch/um/kernel/skas/mmu.c | 3 + arch/um/kernel/skas/stub.c | 90 ++++++++++++++-- arch/um/kernel/skas/stub_exe.c | 21 +++- arch/um/os-Linux/skas/mem.c | 66 +++++++++++- arch/um/os-Linux/skas/process.c | 137 +++++++++++++++++------- 7 files changed, 273 insertions(+), 56 deletions(-)
arch/um/include/shared/skas/mm_id.h +11 -0
@@ -6,10 +6,21 @@#ifndef __MM_ID_H#define __MM_ID_H +#ifdef CONFIG_UML_SECCOMP +#define STUB_MAX_FDS 4 +#else +#define STUB_MAX_FDS 0 +#endif +struct mm_id {int pid;unsigned long stack;int syscall_data_len; + +/* Only used with SECCOMP mode */ +int sock; +int syscall_fd_num; +int syscall_fd_map[STUB_MAX_FDS ]; };void __switch_mm (struct mm_id *mm_idp);
arch/um/include/shared/skas/stub-data.h +1 -0
@@ -13,6 +13,7 @@#include <as-layout.h> #include <sysdep/tls.h> #include <sysdep/stub-data.h> +#include <mm_id.h> #define FUTEX_IN_CHILD 0#define FUTEX_IN_KERN 1
arch/um/kernel/skas/mmu.c +3 -0
@@ -80,6 +80,9 @@ void destroy_context(struct mm_struct *mm) mmu->id.pid =-1 ; } +if (using_seccomp && mmu->id.sock) +os_close_file (mmu->id.sock); +free_pages (mmu->id.stack,ilog2 (STUB_DATA_PAGES ));guard (spinlock_irqsave)(&mm_list_lock);
arch/um/kernel/skas/stub.c +80 -10
142 lines not on this page · raw message
arch/um/kernel/skas/stub_exe.c +17 -4
@@ -1,5 +1,6 @@#include <sys/ptrace.h> #include <sys/prctl.h> +#include <sys/fcntl.h> #include <asm/unistd.h> #include <sysdep/stub.h> #include <stub-data.h>
@@ -45,7 +46,11 @@ noinline static void real_init(void)if (res !=sizeof (init_data))stub_syscall1 (__NR_exit,10 ); -stub_syscall1 (__NR_close,0 ); +/* In SECCOMP mode, FD 0 is a socket and is later used for FD passing */ +if (!init_data.seccomp) +stub_syscall1 (__NR_close,0 ); +else +stub_syscall3 (__NR_fcntl,0 ,F_SETFL ,O_NONBLOCK );/* map stub code + data */ res = stub_syscall6(STUB_MMAP_NR,
@@ -63,6 +68,10 @@ noinline static void real_init(void)if (res != init_data.stub_start +UM_KERN_PAGE_SIZE )stub_syscall1 (__NR_exit,12 ); +/* In SECCOMP mode, we only need the signalling FD from now on */ +if (init_data.seccomp) +stub_syscall3 (__NR_close_range,1 , ~0U ,0 ); +/* setup signal stack inside stub data */ stack.ss_sp = (void *)init_data.stub_start +UM_KERN_PAGE_SIZE ;stub_syscall2 (__NR_sigaltstack, (unsigned long )&stack,0 );
@@ -153,8 +162,12 @@ noinline static void real_init(void)BPF_STMT (BPF_LD |BPF_W |BPF_ABS , offsetof(struct seccomp_data , nr)), -/* [10-14] Check against permitted syscalls */ +/* [10-16] Check against permitted syscalls */ BPF_JUMP (BPF_JMP |BPF_JEQ |BPF_K , __NR_futex, +7 ,0 ), +BPF_JUMP (BPF_JMP |BPF_JEQ |BPF_K ,__NR_recvmsg, +6 ,0 ), +BPF_JUMP (BPF_JMP |BPF_JEQ |BPF_K ,__NR_close,5 ,0 ),BPF_JUMP (BPF_JMP |BPF_JEQ |BPF_K ,STUB_MMAP_NR , 4, 0),
@@ -170,10 +183,10 @@ noinline static void real_init(void)BPF_JUMP (BPF_JMP |BPF_JEQ |BPF_K , __NR_rt_sigreturn,1 ,0 ), -/* [15] Not one of the permitted syscalls */ +/* [17] Not one of the permitted syscalls */ BPF_STMT (BPF_RET |BPF_K ,SECCOMP_RET_KILL_PROCESS ), -/* [16] Permitted call for the stub */ +/* [18] Permitted call for the stub */ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), }; struct sock_fprog prog = {
arch/um/os-Linux/skas/mem.c +63 -3
93 lines not on this page · raw message
arch/um/os-Linux/skas/process.c +98 -39
241 lines not on this page · raw message
Signature · 1 line
2.47.0
Re: [RFC PATCH v2 9/9] um: pass FD for memory operations when needed
On 2024/10/23 22:08, Benjamin Berg wrote:
[...]
Quoted 34 lines
arch/um/os-Linux/skas/process.c +18
-0@@ -16,6 +16,7 @@#include <sys/mman.h> #include <sys/wait.h> #include <sys/stat.h> +#include <sys/socket.h> #include <asm/unistd.h> #include <as-layout.h> #include <init.h> @@ -153,7 +154,39 @@ void wait_stub_done_seccomp(struct mm_id *mm_idp, int running, int wait_sigsys)int ret; do { + const char byte = 0; + struct iovec iov = { + .iov_base = (void *)&byte, + .iov_len = sizeof(byte), + }; + union { + char data[CMSG_SPACE(sizeof(mm_idp->syscall_fd_map))]; + struct cmsghdr align; + } ctrl; + struct msghdr msgh = { + .msg_iov = &iov, + .msg_iovlen = 1, + }; + if (!running) { + if (mm_idp->syscall_fd_num) { + unsigned int fds_size = + sizeof(int) * mm_idp->syscall_fd_num;
arch/um/os-Linux/skas/process.c void wait_stub_done_seccomp(struct mm_id *mm_idp, int running, int wait_sigsys) · line 184
Quoted 10 lines
arch/um/os-Linux/skas/process.c +10
-0+struct cmsghdr *cmsg; + + msgh.msg_control = ctrl.data; + msgh.msg_controllen =CMSG_SPACE (fds_size); + cmsg =CMSG_FIRSTHDR (&msgh); + cmsg->cmsg_level =SOL_SOCKET ; + cmsg->cmsg_type =SCM_RIGHTS ; + cmsg->cmsg_len =CMSG_LEN (fds_size); +memcpy (CMSG_DATA (cmsg), mm_idp->syscall_fd_map, + fds_size);
It looks the memcpy could trigger a crash when UML_SECCOMP is enabled:
Run /sbin/init as init process
*** buffer overflow detected ***: terminated
Thread 1 "linux" received signal SIGABRT, Aborted.__pthread_kill_implementation (no_tid=0, signo=6, threadid=140737353807680) at ./nptl/pthread_kill.c:44
44 ./nptl/pthread_kill.c: No such file or directory.
(gdb) bt
#0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737353807680) at ./nptl/pthread_kill.c:44 #1 __pthread_kill_internal (signo=6, threadid=140737353807680) at ./nptl/pthread_kill.c:78 #2 __GI___pthread_kill (threadid=140737353807680, signo=signo@entry=6) at ./nptl/pthread_kill.c:89 #3 0x00007ffff7c42476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #4 0x00007ffff7c287f3 in __GI_abort () at ./stdlib/abort.c:79 #5 0x00007ffff7c89676 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7ddb92e "*** %s ***: terminated\n") at ../sysdeps/posix/libc_fatal.c:155 #6 0x00007ffff7d3659a in __GI___fortify_fail (msg=msg@entry=0x7ffff7ddb8d4 "buffer overflow detected") at ./debug/fortify_fail.c:26 #7 0x00007ffff7d34f16 in __GI___chk_fail () at ./debug/chk_fail.c:28 #8 0x00000000600376ee in memcpy (__len=4, __src=<optimized out>, __dest=0xe0803e60) at /usr/include/x86_64-linux-gnu/bits/string_fortified.h:29 #9 wait_stub_done_seccomp (mm_idp=mm_idp@entry=0x608e69e0, running=<optimized out>, running@entry=0, wait_sigsys=wait_sigsys@entry=0) at arch/um/os-Linux/skas/process.c:183
#10 0x0000000060037cc6 in userspace (regs=0x60828788) at arch/um/os-Linux/skas/process.c:605 #11 0x00000000600228c1 in new_thread_handler () at arch/um/kernel/process.c:119
It can be fixed with changes like below on my machine:
arch/um/include/shared/skas/mm_id.h +2 -0
@@ -6,6 +6,8 @@#ifndef __MM_ID_H#define __MM_ID_H +#include <linux/kconfig.h> +#ifdef CONFIG_UML_SECCOMP #define STUB_MAX_FDS 4#else
Regards,
Tiwei
Re: [RFC PATCH v2 9/9] um: pass FD for memory operations when needed
Hi,
Quoted 22 lines · Tiwei Bie
On 2024/10/23 22:08, Benjamin Berg wrote:
[...]It looks the memcpy could trigger a crash when
UML_SECCOMPis enabled:[...]
It can be fixed with changes like below on my machine:
arch/um/include/shared/skas/mm_id.h +0
-0@@ -6,6 +6,8 @@ #ifndef __MM_ID_H #define __MM_ID_Harch/um/include/shared/skas/mm_id.h +2
-0+#include <linux/kconfig.h> +
Quoted 3 lines
#ifdef
CONFIG_UML_SECCOMP
#defineSTUB_MAX_FDS4
#else
Hmm, the "#include <linux/kconfig.h>" in userspace code again. I dropped the include from process.c after Johanne's comment …
Maybe we can really just include kconfig.h. But we can also avoid it by adding the definition into common-offsets.h, or just keeping a couple of extra integers around in non-seccomp compiles :-)
Benjamin