seccomp: add thread sync ability
Subsystems (12)
v9 cover seccomp: add thread sync ability
This adds the ability for threads to request seccomp filter synchronization across their thread group (at filter attach time). For example, for Chrome to make sure graphic driver threads are fully confined after seccomp filters have been attached.
To support this, locking on seccomp changes via thread-group-shared sighand lock is introduced, along with refactoring of no_new_privs. Races with thread creation are handled via delayed duplication of the seccomp task struct field.
This includes a new syscall (instead of adding a new prctl option), as suggested by Andy Lutomirski and Michael Kerrisk.
Thanks!
-Kees
v9:
- rearranged/split patches to make things more reviewable
- added use of cred_guard_mutex to solve exec race (oleg, luto)
- added barriers for TIF_SECCOMP vs seccomp.mode race (oleg, luto)
- fixed missed copying of nnp state after v8 refactor (oleg)
v8:
- drop use of tasklist_lock, appears redundant against sighand (oleg)
- reduced use of smp_load_acquire to logical minimum (oleg)
- change nnp to a task struct held atomic flags field (oleg, luto)
- drop needless irqflags changes in fork.c for holding sighand lock (oleg)
- cleaned up use of thread for-each loop (oleg)
- rearranged patch order to keep syscall changes adjacent
- added example code to manpage (mtk)
v7:
- rebase on Linus's tree (merged with network bpf changes)
- wrote manpage text documenting API (follows this series)
v6:
- switch from seccomp-specific lock to thread-group lock to gain atomicity
- implement seccomp syscall across all architectures with seccomp filter
- clean up sparse warnings around locking
v5:
- move includes around (drysdale)
- drop set_nnp return value (luto)
- use smp_load_acquire/store_release (luto)
- merge nnp changes to seccomp always, fewer ifdef (luto)
v4:
- cleaned up locking further, as noticed by David Drysdale
v3:
- added SECCOMP_EXT_ACT_FILTER for new filter install options
v2:
- reworked to avoid clone races
PATCH v9 1/11 seccomp: create internal mode-setting function
In preparation for having other callers of the seccomp mode setting logic, split the prctl entry point away from the core logic that performs seccomp mode setting.
--- kernel/seccomp.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
kernel/seccomp.c +14 -2
@@ -473,7 +473,7 @@ long prctl_get_seccomp(void) }/** -* prctl_set_seccomp: configures current->seccomp.mode +* seccomp_set_mode: internal function for setting seccomp mode * @seccomp_mode: requested mode to use * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER *
@@ -486,7 +486,7 @@ long prctl_get_seccomp(void)* * Returns 0 on success or -EINVAL on failure. */ -long prctl_set_seccomp (unsigned long seccomp_mode,char __user *filter) +static long seccomp_set_mode (unsigned long seccomp_mode,char __user *filter) {long ret = -EINVAL ;
@@ -517,3 +517,15 @@ long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter) out:return ret; } + +/** +* prctl_set_seccomp: configures current->seccomp.mode +* @seccomp_mode: requested mode to use +* @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER +* +* Returns 0 on success or -EINVAL on failure. +*/ +long prctl_set_seccomp (unsigned long seccomp_mode,char __user *filter) +{ +return seccomp_set_mode (seccomp_mode, filter); +}
Signature · 1 line
1.7.9.5
PATCH v9 2/11 seccomp: extract check/assign mode helpers
In reply to Kees CookTo support splitting mode 1 from mode 2, extract the mode checking and assignment logic into common functions.
--- kernel/seccomp.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
kernel/seccomp.c +18 -4
@@ -194,7 +194,23 @@ static u32 seccomp_run_filters(int syscall) }return ret; } +#endif /* CONFIG_SECCOMP_FILTER */ +static inline bool seccomp_check_mode (unsigned long seccomp_mode) +{ +if (current->seccomp.mode && current->seccomp.mode != seccomp_mode) +return false; + +return true; +} + +static inline void seccomp_assign_mode (unsigned long seccomp_mode) +{ + current->seccomp.mode = seccomp_mode; +set_tsk_thread_flag (current,TIF_SECCOMP ); +} + +#ifdef CONFIG_SECCOMP_FILTER /** * seccomp_attach_filter: Attaches a seccomp filter to current. * @fprog: BPF program to install
@@ -490,8 +506,7 @@ static long seccomp_set_mode(unsigned long seccomp_mode, char __user *filter) {long ret = -EINVAL ; -if (current->seccomp.mode && - current->seccomp.mode != seccomp_mode) +if (!seccomp_check_mode (seccomp_mode)) goto out;switch (seccomp_mode) {
@@ -512,8 +527,7 @@ static long seccomp_set_mode(unsigned long seccomp_mode, char __user *filter) goto out; } - current->seccomp.mode = seccomp_mode; -set_thread_flag (TIF_SECCOMP ); +seccomp_assign_mode (seccomp_mode); out:return ret; }
Signature · 1 line
1.7.9.5
PATCH v9 3/11 seccomp: split mode setting routines
In reply to Kees CookSeparates the two mode setting paths to make things more readable with fewer #ifdefs within function bodies.
--- kernel/seccomp.c | 71 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 23 deletions(-)
kernel/seccomp.c +48 -23
@@ -489,48 +489,66 @@ long prctl_get_seccomp(void) }/** -* seccomp_set_mode: internal function for setting seccomp mode -* @seccomp_mode: requested mode to use -* @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER -* -* This function may be called repeatedly with a @seccomp_mode of -* SECCOMP_MODE_FILTER to install additional filters. Every filter -* successfully installed will be evaluated (in reverse order) for each system -* call the task makes. +* seccomp_set_mode_strict: internal function for setting strict seccomp * * Once current->seccomp.mode is non-zero, it may not be changed. * * Returns 0 on success or -EINVAL on failure. */ -static long seccomp_set_mode (unsigned long seccomp_mode,char __user *filter) +static long seccomp_set_mode_strict (void ) { +const unsigned long seccomp_mode =SECCOMP_MODE_STRICT ;long ret = -EINVAL ;if (!seccomp_check_mode (seccomp_mode)) goto out; -switch (seccomp_mode) { -case SECCOMP_MODE_STRICT : - ret =0 ;#ifdef TIF_NOTSC -disable_TSC (); +disable_TSC ();#endif -break ; +seccomp_assign_mode (seccomp_mode); + ret =0 ; + +out: + +return ret; +} +#ifdef CONFIG_SECCOMP_FILTER -case SECCOMP_MODE_FILTER : - ret =seccomp_attach_user_filter (filter); -if (ret) - goto out; -break ; -#endif -default : +/** +* seccomp_set_mode_filter: internal function for setting seccomp filter +* @filter: struct sock_fprog containing filter +* +* This function may be called repeatedly to install additional filters. +* Every filter successfully installed will be evaluated (in reverse order) +* for each system call the task makes. +* +* Once current->seccomp.mode is non-zero, it may not be changed. +* +* Returns 0 on success or -EINVAL on failure. +*/ +static long seccomp_set_mode_filter (char __user *filter) +{ +const unsigned long seccomp_mode =SECCOMP_MODE_FILTER ; +long ret = -EINVAL ; + +if (!seccomp_check_mode (seccomp_mode)) + goto out; + + ret =seccomp_attach_user_filter (filter); +if (ret) goto out; - }seccomp_assign_mode (seccomp_mode); out:return ret; } +#else +static inline long seccomp_set_mode_filter (char __user *filter) +{ +return -EINVAL ; +} +#endif /** * prctl_set_seccomp: configures current->seccomp.mode
@@ -541,5 +559,12 @@ out:*/ long prctl_set_seccomp (unsigned long seccomp_mode,char __user *filter) { -return seccomp_set_mode (seccomp_mode, filter); +switch (seccomp_mode) { +case SECCOMP_MODE_STRICT : +return seccomp_set_mode_strict (); +case SECCOMP_MODE_FILTER : +return seccomp_set_mode_filter (filter); +default : +return -EINVAL ; + } }
Signature · 1 line
1.7.9.5
PATCH v9 4/11 seccomp: add "seccomp" syscall
In reply to Kees CookThis adds the new "seccomp" syscall with both an "operation" and "flags" parameter for future expansion. The third argument is a pointer value, used with the SECCOMP_SET_MODE_FILTER operation. Currently, flags must be 0. This is functionally equivalent to prctl(PR_SET_SECCOMP, ...).
In addition to the TSYNC flag later in this patch series, there is a non-zero chance that this syscall could be used for configuring a fixed argument area for seccomp-tracer-aware processes to pass syscall arguments in the future. Hence, the use of "seccomp" not simply "seccomp_add_filter" for this syscall. Additionally, this syscall uses operation, flags, and user pointer for arguments because strictly passing arguments via a user pointer would mean seccomp itself would be unable to trivially filter the seccomp syscall itself.
--- arch/Kconfig | 1 + arch/x86/syscalls/syscall_32.tbl | 1 + arch/x86/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 2 ++ include/uapi/asm-generic/unistd.h | 4 ++- include/uapi/linux/seccomp.h | 4 +++ kernel/seccomp.c | 55 +++++++++++++++++++++++++++++++++---- kernel/sys_ni.c | 3 ++ 8 files changed, 65 insertions(+), 6 deletions(-)
arch/Kconfig +1 -0
@@ -321,6 +321,7 @@ config HAVE_ARCH_SECCOMP_FILTER - secure_computing is called from a ptrace_event()-safe context - secure_computing return value is checked and a return value of -1 results in the system call being skipped immediately. + - seccomp syscall wired up config SECCOMP_FILTER def_bool y
arch/x86/syscalls/syscall_32.tbl +1 -0
@@ -360,3 +360,4 @@ 351 i386 sched_setattr sys_sched_setattr 352 i386 sched_getattr sys_sched_getattr 353 i386 renameat2 sys_renameat2 +354 i386 seccomp sys_seccomp
arch/x86/syscalls/syscall_64.tbl +1 -0
@@ -323,6 +323,7 @@ 314 common sched_setattr sys_sched_setattr 315 common sched_getattr sys_sched_getattr 316 common renameat2 sys_renameat2 +317 common seccomp sys_seccomp # # x32-specific system call numbers start at 512 to avoid cache impact
include/linux/syscalls.h +2 -0
@@ -866,4 +866,6 @@ asmlinkage long sys_process_vm_writev(pid_t pid,asmlinkage long sys_kcmp (pid_t pid1,pid_t pid2,int type,unsigned long idx1,unsigned long idx2);asmlinkage long sys_finit_module (int fd,const char __user *uargs,int flags); +asmlinkage long sys_seccomp (unsigned int op,unsigned int flags, +const char __user *uargs);#endif
include/uapi/asm-generic/unistd.h +3 -1
@@ -699,9 +699,11 @@ __SYSCALL(__NR_sched_setattr, sys_sched_setattr) __SYSCALL(__NR_sched_getattr, sys_sched_getattr)#define __NR_renameat2 276 __SYSCALL(__NR_renameat2, sys_renameat2) +#define __NR_seccomp 277 +__SYSCALL(__NR_seccomp, sys_seccomp)#undef __NR_syscalls -#define __NR_syscalls 277 +#define __NR_syscalls 278/* * All syscalls below here should go away really,
include/uapi/linux/seccomp.h +4 -0
@@ -10,6 +10,10 @@#define SECCOMP_MODE_STRICT 1/* uses hard-coded filter. */ #define SECCOMP_MODE_FILTER 2/* uses user-supplied filter. */ +/* Valid operations for seccomp syscall. */ +#define SECCOMP_SET_MODE_STRICT 0 +#define SECCOMP_SET_MODE_FILTER 1 +/* * All BPF programs must return a 32-bit value. * The bottom 16-bits are for optional return data.
kernel/seccomp.c +50 -5
104 lines not on this page · raw message
kernel/sys_ni.c +3 -0
@@ -213,3 +213,6 @@ cond_syscall(compat_sys_open_by_handle_at);/* compare kernel pointers */ cond_syscall (sys_kcmp); + +/* operate on Secure Computing state */ +cond_syscall (sys_seccomp);
Signature · 1 line
1.7.9.5
PATCH v9 5/11 ARM: add seccomp syscall
In reply to Kees CookWires up the new seccomp syscall.
--- arch/arm/include/uapi/asm/unistd.h | 1 + arch/arm/kernel/calls.S | 1 + 2 files changed, 2 insertions(+)
arch/arm/include/uapi/asm/unistd.h +1 -0
@@ -409,6 +409,7 @@#define __NR_sched_setattr (__NR_SYSCALL_BASE+380)#define __NR_sched_getattr (__NR_SYSCALL_BASE+381)#define __NR_renameat2 (__NR_SYSCALL_BASE+382) +#define __NR_seccomp (__NR_SYSCALL_BASE+383)/* * This may need to be greater than __NR_last_syscall+1 in order to
arch/arm/kernel/calls.S +1 -0
@@ -392,6 +392,7 @@ /* 380 */ CALL(sys_sched_setattr) CALL(sys_sched_getattr) CALL(sys_renameat2) + CALL(sys_seccomp) #ifndef syscalls_counted .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls #define syscalls_counted
Signature · 1 line
1.7.9.5
PATCH v9 6/11 MIPS: add seccomp syscall
In reply to Kees CookWires up the new seccomp syscall.
--- arch/mips/include/uapi/asm/unistd.h | 15 +++++++++------ arch/mips/kernel/scall32-o32.S | 1 + arch/mips/kernel/scall64-64.S | 1 + arch/mips/kernel/scall64-n32.S | 1 + arch/mips/kernel/scall64-o32.S | 1 + 5 files changed, 13 insertions(+), 6 deletions(-)
arch/mips/include/uapi/asm/unistd.h +9 -6
@@ -372,16 +372,17 @@#define __NR_sched_setattr (__NR_Linux + 349)#define __NR_sched_getattr (__NR_Linux + 350)#define __NR_renameat2 (__NR_Linux + 351) +#define __NR_seccomp (__NR_Linux + 352)/* * Offset of the last Linux o32 flavoured syscall */ -#define __NR_Linux_syscalls 351 +#define __NR_Linux_syscalls 352#endif /* _MIPS_SIM == _MIPS_SIM_ABI32 */ #define __NR_O32_Linux 4000 -#define __NR_O32_Linux_syscalls 351 +#define __NR_O32_Linux_syscalls 352#if _MIPS_SIM == _MIPS_SIM_ABI64
@@ -701,16 +702,17 @@#define __NR_sched_setattr (__NR_Linux + 309)#define __NR_sched_getattr (__NR_Linux + 310)#define __NR_renameat2 (__NR_Linux + 311) +#define __NR_seccomp (__NR_Linux + 312)/* * Offset of the last Linux 64-bit flavoured syscall */ -#define __NR_Linux_syscalls 311 +#define __NR_Linux_syscalls 312#endif /* _MIPS_SIM == _MIPS_SIM_ABI64 */ #define __NR_64_Linux 5000 -#define __NR_64_Linux_syscalls 311 +#define __NR_64_Linux_syscalls 312#if _MIPS_SIM == _MIPS_SIM_NABI32
@@ -1034,15 +1036,16 @@#define __NR_sched_setattr (__NR_Linux + 313)#define __NR_sched_getattr (__NR_Linux + 314)#define __NR_renameat2 (__NR_Linux + 315) +#define __NR_seccomp (__NR_Linux + 316)/* * Offset of the last N32 flavoured syscall */ -#define __NR_Linux_syscalls 315 +#define __NR_Linux_syscalls 316#endif /* _MIPS_SIM == _MIPS_SIM_NABI32 */ #define __NR_N32_Linux 6000 -#define __NR_N32_Linux_syscalls 315 +#define __NR_N32_Linux_syscalls 316#endif /* _UAPI_ASM_UNISTD_H */
arch/mips/kernel/scall32-o32.S +1 -0
@@ -578,3 +578,4 @@ EXPORT(sys_call_table) PTR sys_sched_setattr PTR sys_sched_getattr /* 4350 */ PTR sys_renameat2 + PTR sys_seccomp
arch/mips/kernel/scall64-64.S +1 -0
@@ -431,4 +431,5 @@ EXPORT(sys_call_table) PTR sys_sched_setattr PTR sys_sched_getattr /* 5310 */ PTR sys_renameat2 + PTR sys_seccomp .size sys_call_table,.-sys_call_table
arch/mips/kernel/scall64-n32.S +1 -0
@@ -424,4 +424,5 @@ EXPORT(sysn32_call_table) PTR sys_sched_setattr PTR sys_sched_getattr PTR sys_renameat2 /* 6315 */ + PTR sys_seccomp .size sysn32_call_table,.-sysn32_call_table
arch/mips/kernel/scall64-o32.S +1 -0
@@ -557,4 +557,5 @@ EXPORT(sys32_call_table) PTR sys_sched_setattr PTR sys_sched_getattr /* 4350 */ PTR sys_renameat2 + PTR sys_seccomp .size sys32_call_table,.-sys32_call_table
Signature · 1 line
1.7.9.5
PATCH v9 7/11 sched: move no_new_privs into new atomic flags
In reply to Kees CookSince seccomp transitions between threads requires updates to the no_new_privs flag to be atomic, the flag must be part of an atomic flag set. This moves the nnp flag into a separate task field, and introduces accessors.
--- fs/exec.c | 4 ++-- include/linux/sched.h | 18 +++++++++++++++--- kernel/seccomp.c | 2 +- kernel/sys.c | 4 ++-- security/apparmor/domain.c | 4 ++-- 5 files changed, 22 insertions(+), 10 deletions(-)
fs/exec.c +2 -2
@@ -1234,7 +1234,7 @@ static void check_unsafe_exec(struct linux_binprm *bprm)* This isn't strictly necessary, but it makes it harder for LSMs to * mess up. */ -if (current->no_new_privs) +if (task_no_new_privs (current)) bprm->unsafe |=LSM_UNSAFE_NO_NEW_PRIVS ; t = p;
@@ -1272,7 +1272,7 @@ int prepare_binprm(struct linux_binprm *bprm) bprm->cred->egid =current_egid ();if (!(bprm->file->f_path.mnt->mnt_flags &MNT_NOSUID ) && - !current->no_new_privs && + !task_no_new_privs (current) &&kuid_has_mapping (bprm->cred->user_ns, inode->i_uid) &&kgid_has_mapping (bprm->cred->user_ns, inode->i_gid)) {/* Set-uid? */
include/linux/sched.h +15 -3
@@ -1307,13 +1307,12 @@ struct task_struct {* execve */ unsigned in_iowait:1; -/* task may not gain privileges */ - unsigned no_new_privs:1; -/* Revert to default priority/policy when forking */ unsigned sched_reset_on_fork:1; unsigned sched_contributes_to_load:1; +unsigned long atomic_flags;/* Flags needing atomic access. */ +pid_t pid;pid_t tgid;
@@ -1967,6 +1966,19 @@ static inline void memalloc_noio_restore(unsigned int flags) current->flags = (current->flags & ~PF_MEMALLOC_NOIO ) | flags; } +/* Per-process atomic flags. */ +#define PFA_NO_NEW_PRIVS 0x00000001/* May not gain new privileges. */ + +static inline bool task_no_new_privs (struct task_struct *p) +{ +return test_bit (PFA_NO_NEW_PRIVS , &p->atomic_flags); +} + +static inline void task_set_no_new_privs (struct task_struct *p) +{ +set_bit (PFA_NO_NEW_PRIVS , &p->atomic_flags); +} +/* * task->jobctl flags */
kernel/seccomp.c +1 -1
@@ -241,7 +241,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)* This avoids scenarios where unprivileged tasks can affect the * behavior of privileged children. */ -if (!current->no_new_privs && +if (!task_no_new_privs (current) &&security_capable_noaudit (current_cred (),current_user_ns (),CAP_SYS_ADMIN ) !=0 )return -EACCES ;
kernel/sys.c +2 -2
@@ -1990,12 +1990,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2 !=1 || arg3 || arg4 || arg5)return -EINVAL ; - current->no_new_privs =1 ; +task_set_no_new_privs (current);break ;case PR_GET_NO_NEW_PRIVS :if (arg2 || arg3 || arg4 || arg5)return -EINVAL ; -return current->no_new_privs ?1 :0 ; +return task_no_new_privs (current) ?1 :0 ;case PR_GET_THP_DISABLE :if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;
security/apparmor/domain.c +2 -2
@@ -621,7 +621,7 @@ int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)* There is no exception for unconfined as change_hat is not * available. */ -if (current->no_new_privs) +if (task_no_new_privs (current))return -EPERM ;/* released below */
@@ -776,7 +776,7 @@ int aa_change_profile(const char *ns_name, const char *hname, bool onexec,* no_new_privs is set because this aways results in a reduction * of permissions. */ -if (current->no_new_privs && !unconfined (profile)) { +if (task_no_new_privs (current) && !unconfined (profile)) {put_cred (cred);return -EPERM ; }
Signature · 1 line
1.7.9.5
PATCH v9 8/11 seccomp: split filter prep from check and apply
In reply to Kees CookIn preparation for adding seccomp locking, move filter creation away from where it is checked and applied. This will allow for locking where no memory allocation is happening. The validation, filter attachment, and seccomp mode setting can all happen under the future locks.
--- kernel/seccomp.c | 93 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 29 deletions(-)
kernel/seccomp.c +64 -29
@@ -18,6 +18,7 @@#include <linux/compat.h> #include <linux/sched.h> #include <linux/seccomp.h> +#include <linux/slab.h> #include <linux/syscalls.h> /* #define SECCOMP_DEBUG 1 */
@@ -27,7 +28,6 @@#include <linux/filter.h> #include <linux/ptrace.h> #include <linux/security.h> -#include <linux/slab.h> #include <linux/tracehook.h> #include <linux/uaccess.h>
@@ -213,27 +213,21 @@ static inline void seccomp_assign_mode(unsigned long seccomp_mode)#ifdef CONFIG_SECCOMP_FILTER /** -* seccomp_attach_filter: Attaches a seccomp filter to current. +* seccomp_prepare_filter: Prepares a seccomp filter for use. * @fprog: BPF program to install * -* Returns 0 on success or an errno on failure. +* Returns filter on success or an ERR_PTR on failure. */ -static long seccomp_attach_filter (struct sock_fprog *fprog) +static struct seccomp_filter *seccomp_prepare_filter (struct sock_fprog *fprog) {struct seccomp_filter *filter;unsigned long fp_size = fprog->len *sizeof (struct sock_filter ); -unsigned long total_insns = fprog->len;struct sock_filter *fp;int new_len;long ret;if (fprog->len ==0 || fprog->len >BPF_MAXINSNS ) -return -EINVAL ; - -for (filter = current->seccomp.filter; filter; filter = filter->prev) - total_insns += filter->prog->len +4 ;/* include a 4 instr penalty */ -if (total_insns >MAX_INSNS_PER_PATH ) -return -ENOMEM ; +return ERR_PTR (-EINVAL );/* * Installing a seccomp filter requires that the task has
@@ -244,11 +238,11 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)if (!task_no_new_privs (current) &&security_capable_noaudit (current_cred (),current_user_ns (),CAP_SYS_ADMIN ) !=0 ) -return -EACCES ; +return ERR_PTR (-EACCES ); fp =kzalloc (fp_size,GFP_KERNEL |__GFP_NOWARN);if (!fp) -return -ENOMEM ; +return ERR_PTR (-ENOMEM );/* Copy the instructions from fprog. */ ret = -EFAULT ;
@@ -292,13 +286,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)sk_filter_select_runtime (filter->prog); -/* -* If there is an existing filter, make it the prev and don't drop its -* task reference. -*/ - filter->prev = current->seccomp.filter; - current->seccomp.filter = filter; -return 0 ; +return filter; free_filter_prog:kfree (filter->prog);
@@ -306,19 +294,20 @@ free_filter:kfree (filter); free_prog:kfree (fp); -return ret; +return ERR_PTR (ret); }/** -* seccomp_attach_user_filter - attaches a user-supplied sock_fprog +* seccomp_prepare_user_filter - prepares a user-supplied sock_fprog * @user_filter: pointer to the user data containing a sock_fprog. * * Returns 0 on success and non-zero otherwise. */ -static long seccomp_attach_user_filter (const char __user *user_filter) +static struct seccomp_filter * +seccomp_prepare_user_filter (const char __user *user_filter) {struct sock_fprog fprog; -long ret = -EFAULT ; +struct seccomp_filter *filter =ERR_PTR (-EFAULT );#ifdef CONFIG_COMPAT if (is_compat_task ()) {
@@ -331,9 +320,39 @@ static long seccomp_attach_user_filter(const char __user *user_filter)#endif if (copy_from_user (&fprog, user_filter,sizeof (fprog))) goto out; - ret =seccomp_attach_filter (&fprog); + filter =seccomp_prepare_filter (&fprog); out: -return ret; +return filter; +} + +/** +* seccomp_attach_filter: validate and attach filter +* @flags: flags to change filter behavior +* @filter: seccomp filter to add to the current process +* +* Returns 0 on success, -ve on error. +*/ +static long seccomp_attach_filter (unsigned int flags, +struct seccomp_filter *filter) +{ +unsigned long total_insns; +struct seccomp_filter *walker; + +/* Validate resulting filter length. */ + total_insns = filter->prog->len; +for (walker = current->seccomp.filter; walker; walker = walker->prev) + total_insns += walker->prog->len +4 ;/* 4 instr penalty */ +if (total_insns >MAX_INSNS_PER_PATH ) +return -ENOMEM ; + +/* +* If there is an existing filter, make it the prev and don't drop its +* task reference. +*/ + filter->prev = current->seccomp.filter; + current->seccomp.filter = filter; + +return 0 ; }/* get_seccomp_filter - increments the reference count of the filter on @tsk */
@@ -346,6 +365,14 @@ void get_seccomp_filter(struct task_struct *tsk)atomic_inc (&orig->usage); } +static inline void seccomp_filter_free (struct seccomp_filter *filter) +{ +if (filter) { +sk_filter_free (filter->prog); +kfree (filter); + } +} +/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ void put_seccomp_filter (struct task_struct *tsk) {
@@ -354,8 +381,7 @@ void put_seccomp_filter(struct task_struct *tsk)while (orig &&atomic_dec_and_test (&orig->usage)) {struct seccomp_filter *freeme = orig; orig = orig->prev; -sk_filter_free (freeme->prog); -kfree (freeme); +seccomp_filter_free (freeme); } }
@@ -533,21 +559,30 @@ static long seccomp_set_mode_filter(unsigned int flags, const char __user *filter) {const unsigned long seccomp_mode =SECCOMP_MODE_FILTER ; +struct seccomp_filter *prepared =NULL ;long ret = -EINVAL ;/* Validate flags. */ if (flags !=0 ) goto out; +/* Prepare the new filter before holding any locks. */ + prepared =seccomp_prepare_user_filter (filter); +if (IS_ERR (prepared)) +return PTR_ERR (prepared); +if (!seccomp_check_mode (seccomp_mode)) goto out; - ret =seccomp_attach_user_filter (filter); + ret =seccomp_attach_filter (flags, prepared);if (ret) goto out; +/* Do not free the successfully attached filter. */ + prepared =NULL ;seccomp_assign_mode (seccomp_mode); out: +seccomp_filter_free (prepared);return ret; }#else
Signature · 1 line
1.7.9.5
PATCH v9 9/11 seccomp: introduce writer locking
In reply to Kees CookNormally, task_struct.seccomp.filter is only ever read or modified by the task that owns it (current). This property aids in fast access during system call filtering as read access is lockless.
Updating the pointer from another task, however, opens up race conditions. To allow cross-thread filter pointer updates, writes to the seccomp fields are now protected by the sighand spinlock (which is unique to the thread group). Read access remains lockless because pointer updates themselves are atomic. However, writes (or cloning) often entail additional checking (like maximum instruction counts) which require locking to perform safely.
In the case of cloning threads, the child is invisible to the system until it enters the task list. To make sure a child can't be cloned from a thread and left in a prior state, seccomp duplication is additionally moved under the sighand lock. Then parent and child are certain have the same seccomp state when they exit the lock.
Based on patches by Will Drewry and David Drysdale.
--- include/linux/seccomp.h | 6 +++--- kernel/fork.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- kernel/seccomp.c | 26 ++++++++++++++++++++------ 3 files changed, 67 insertions(+), 10 deletions(-)
include/linux/seccomp.h +3 -3
@@ -14,11 +14,11 @@ struct seccomp_filter;* * @mode: indicates one of the valid values above for controlled * system calls available to a process. -* @filter: The metadata and ruleset for determining what system calls -* are allowed for a task. +* @filter: must always point to a valid seccomp-filter or NULL as it is +* accessed without locking during system call entry. * * @filter must only be accessed from the context of current as there -* is no locking. +* is no read locking. */ struct seccomp {int mode;
kernel/fork.c +44 -1
69 lines not on this page · raw message
kernel/seccomp.c +20 -6
@@ -173,12 +173,12 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)*/ static u32 seccomp_run_filters (int syscall) { -struct seccomp_filter *f; +struct seccomp_filter *f =ACCESS_ONCE (current->seccomp.filter);struct seccomp_data sd;u32 ret =SECCOMP_RET_ALLOW ;/* Ensure unexpected behavior doesn't result in failing open. */ -if (WARN_ON (current->seccomp.filter ==NULL )) +if (unlikely (WARN_ON (f ==NULL )))return SECCOMP_RET_KILL ;populate_seccomp_data (&sd);
@@ -187,7 +187,7 @@ static u32 seccomp_run_filters(int syscall)* All filters in the list are evaluated and the lowest BPF return * value always takes priority (ignoring the DATA). */ -for (f = current->seccomp.filter; f; f = f->prev) { +for (; f; f = f->prev) {u32 cur_ret =SK_RUN_FILTER (f->prog, (void *)&sd);if ((cur_ret &SECCOMP_RET_ACTION ) < (ret &SECCOMP_RET_ACTION ))
@@ -199,6 +199,8 @@ static u32 seccomp_run_filters(int syscall)static inline bool seccomp_check_mode (unsigned long seccomp_mode) { +BUG_ON (!spin_is_locked (¤t->sighand->siglock)); +if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)return false;
@@ -207,6 +209,8 @@ static inline bool seccomp_check_mode(unsigned long seccomp_mode)static inline void seccomp_assign_mode (unsigned long seccomp_mode) { +BUG_ON (!spin_is_locked (¤t->sighand->siglock)); + current->seccomp.mode = seccomp_mode;set_tsk_thread_flag (current,TIF_SECCOMP ); }
@@ -330,6 +334,8 @@ out:* @flags: flags to change filter behavior * @filter: seccomp filter to add to the current process * +* Caller must be holding current->sighand->siglock lock. +* * Returns 0 on success, -ve on error. */ static long seccomp_attach_filter(unsigned int flags,
@@ -338,6 +344,8 @@ static long seccomp_attach_filter(unsigned int flags,unsigned long total_insns;struct seccomp_filter *walker; +BUG_ON (!spin_is_locked (¤t->sighand->siglock)); +/* Validate resulting filter length. */ total_insns = filter->prog->len;for (walker = current->seccomp.filter; walker; walker = walker->prev)
@@ -350,7 +358,7 @@ static long seccomp_attach_filter(unsigned int flags,* task reference. */ filter->prev = current->seccomp.filter; - current->seccomp.filter = filter; +smp_store_release (¤t->seccomp.filter, filter);return 0 ; }
@@ -376,7 +384,7 @@ static inline void seccomp_filter_free(struct seccomp_filter *filter)/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ void put_seccomp_filter (struct task_struct *tsk) { -struct seccomp_filter *orig = tsk->seccomp.filter; +struct seccomp_filter *orig =smp_load_acquire (&tsk->seccomp.filter);/* Clean up single-reference branches iteratively. */ while (orig &&atomic_dec_and_test (&orig->usage)) {struct seccomp_filter *freeme = orig;
@@ -527,6 +535,8 @@ static long seccomp_set_mode_strict(void)const unsigned long seccomp_mode =SECCOMP_MODE_STRICT ;long ret = -EINVAL ; +spin_lock_irq (¤t->sighand->siglock); +if (!seccomp_check_mode (seccomp_mode)) goto out;
@@ -537,6 +547,7 @@ static long seccomp_set_mode_strict(void) ret =0 ; out: +spin_unlock_irq (¤t->sighand->siglock);return ret; }
@@ -564,13 +575,15 @@ static long seccomp_set_mode_filter(unsigned int flags,/* Validate flags. */ if (flags !=0 ) - goto out; +return -EINVAL ;/* Prepare the new filter before holding any locks. */ prepared =seccomp_prepare_user_filter (filter);if (IS_ERR (prepared))return PTR_ERR (prepared); +spin_lock_irq (¤t->sighand->siglock); +if (!seccomp_check_mode (seccomp_mode)) goto out;
@@ -582,6 +595,7 @@ static long seccomp_set_mode_filter(unsigned int flags,seccomp_assign_mode (seccomp_mode); out: +spin_unlock_irq (¤t->sighand->siglock);seccomp_filter_free (prepared);return ret; }
Signature · 1 line
1.7.9.5
Re: [PATCH v9 09/11] seccomp: introduce writer locking
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 176
Quoted 5 lines · Kees Cook
static u32
seccomp_run_filters(int syscall)
{kernel/seccomp.c +1
-1-struct seccomp_filter *f; +struct seccomp_filter *f =ACCESS_ONCE (current->seccomp.filter);
I am not sure...
This is fine if this ->filter is the 1st (and only) one, in this case we can rely on rmb() in the caller.
But the new filter can be installed at any moment. Say, right after that rmb() although this doesn't matter. Either we need smp_read_barrier_depends() after that, or smp_load_acquire() like the previous version did?
Oleg.
Re: [PATCH v9 09/11] seccomp: introduce writer locking
Quoted 10 lines · Oleg Nesterov
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 176
Quoted 5 lines · Kees Cook
static u32
seccomp_run_filters(int syscall)
{kernel/seccomp.c +1
-1-struct seccomp_filter *f; +struct seccomp_filter *f =ACCESS_ONCE (current->seccomp.filter);I am not sure...
Quoted 6 lines
This is fine if this ->filter is the 1st (and only) one, in this case we can rely on
rmb()in the caller.But the new filter can be installed at any moment. Say, right after that
rmb()although this doesn't matter. Either we needsmp_read_barrier_depends()after that, orsmp_load_acquire()like the previous version did?
Wait... and it seems that seccomp_sync_threads() needs smp_store_release() when it sets thread->filter = current->filter by the same reason?
OTOH. smp_store_release() in seccomp_attach_filter() can die, "current" doesn't need a barrier to serialize with itself.
Oleg.
Re: [PATCH v9 09/11] seccomp: introduce writer locking
Quoted 18 lines · Oleg Nesterov
Quoted 8 lines · Oleg Nesterov
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 176
Quoted 5 lines · Kees Cook
static u32
seccomp_run_filters(int syscall)
{kernel/seccomp.c +1
-1-struct seccomp_filter *f; +struct seccomp_filter *f =ACCESS_ONCE (current->seccomp.filter);Quoted 8 lines
I am not sure...
This is fine if this ->filter is the 1st (and only) one, in this case we can rely on
rmb()in the caller.But the new filter can be installed at any moment. Say, right after that
rmb()although this doesn't matter. Either we needsmp_read_barrier_depends()after that, orsmp_load_acquire()like the previous version did?
Quoted 5 lines
Wait... and it seems that
seccomp_sync_threads()needssmp_store_release()when it setsthread->filter=current->filterby the same reason?OTOH.
smp_store_release()inseccomp_attach_filter()can die, "current" doesn't need a barrier to serialize with itself.
I have lost track of what you're suggesting to change. :)
Since rmb() happens before run_filters, isn't the ACCESS_ONCE sufficient? We only care that TIF_SECCOMP, mode, and some filter is valid. In a tsync thread race, it's okay to use not use the deepest filter node in the list, it just needs A filter.
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCH v9 09/11] seccomp: introduce writer locking
Quoted 20 lines · Kees Cook
Quoted 18 lines · Oleg Nesterov
Quoted 16 lines · Oleg Nesterov
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 176
Quoted 5 lines · Kees Cook
static u32
seccomp_run_filters(int syscall)
{kernel/seccomp.c +1
-1-struct seccomp_filter *f; +struct seccomp_filter *f =ACCESS_ONCE (current->seccomp.filter);I am not sure...
This is fine if this ->filter is the 1st (and only) one, in this case we can rely on
rmb()in the caller.But the new filter can be installed at any moment. Say, right after that
rmb()although this doesn't matter. Either we needsmp_read_barrier_depends()after that, orsmp_load_acquire()like the previous version did?
Quoted 7 lines
Quoted 5 lines
Wait... and it seems that
seccomp_sync_threads()needssmp_store_release()when it setsthread->filter=current->filterby the same reason?OTOH.
smp_store_release()inseccomp_attach_filter()can die, "current" doesn't need a barrier to serialize with itself.I have lost track of what you're suggesting to change. :)
Perhaps I am just trying to confuse you and myself ;)
But,
Quoted 2 lines
Since
rmb()happens beforerun_filters, isn't theACCESS_ONCEsufficient?
Yes. But see above. ACCESS_ONCE is sufficient if we read the first filter installed by another thread, in this case rmb() pairs with mb_before_atomic() before set_bit(TIF_SECCOMP).
IOW, if this threads sees TIF_SECCOMP, it should also see all modifications which were done before set_bit, including the data in ->filter points to.
Quoted 3 lines
We only care that
TIF_SECCOMP, mode, and some filter is valid. In a tsync thread race, it's okay to use not use the deepest filter node in the list,
Yes, it is fine if we miss yet another filter which was just installed by another thread.
But, unless I missed something, the problem is that we can get this new filter.
Just to simplify. Suppose TIF_SECCOMP was set a long ago. This thread has a single filter F1 and it enters seccomp_run_filters().
Right before it does ACCESS_ONCE() to read the pointer, another thread does seccomp_sync_threads() and sets .filter = F2.
If ACCESS_ONCE() returns F1 - everything is fine. But it can see the new pointer F2, and in this case we need a barrier to ensure that, say, LOAD(F2->prog) will see all the preceding changes in this memory.
Oleg.
Re: [PATCH v9 09/11] seccomp: introduce writer locking
Quoted 56 lines · Oleg Nesterov
Quoted 20 lines · Kees Cook
Quoted 18 lines · Oleg Nesterov
Quoted 16 lines · Oleg Nesterov
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 176
Quoted 5 lines · Kees Cook
static u32
seccomp_run_filters(int syscall)
{kernel/seccomp.c +1
-1-struct seccomp_filter *f; +struct seccomp_filter *f =ACCESS_ONCE (current->seccomp.filter);I am not sure...
This is fine if this ->filter is the 1st (and only) one, in this case we can rely on
rmb()in the caller.But the new filter can be installed at any moment. Say, right after that
rmb()although this doesn't matter. Either we needsmp_read_barrier_depends()after that, orsmp_load_acquire()like the previous version did?Quoted 7 lines
Quoted 5 lines
Wait... and it seems that
seccomp_sync_threads()needssmp_store_release()when it setsthread->filter=current->filterby the same reason?OTOH.
smp_store_release()inseccomp_attach_filter()can die, "current" doesn't need a barrier to serialize with itself.I have lost track of what you're suggesting to change. :)
Perhaps I am just trying to confuse you and myself ;)
But,
Quoted 2 lines
Since
rmb()happens beforerun_filters, isn't theACCESS_ONCEsufficient?Yes. But see above.
ACCESS_ONCEis sufficient if we read the first filter installed by another thread, in this casermb()pairs withmb_before_atomic()beforeset_bit(TIF_SECCOMP).IOW, if this threads sees
TIF_SECCOMP, it should also see all modifications which were done beforeset_bit, including the data in ->filter points to.Quoted 3 lines
We only care that
TIF_SECCOMP, mode, and some filter is valid. In a tsync thread race, it's okay to use not use the deepest filter node in the list,Yes, it is fine if we miss yet another filter which was just installed by another thread.
But, unless I missed something, the problem is that we can get this new filter.
Just to simplify. Suppose
TIF_SECCOMPwas set a long ago. This thread has a single filter F1 and it entersseccomp_run_filters().
Quoted 6 lines
Right before it does
ACCESS_ONCE()to read the pointer, another thread doesseccomp_sync_threads()and sets .filter = F2.If
ACCESS_ONCE()returns F1 - everything is fine. But it can see the new pointer F2, and in this case we need a barrier to ensure that, say,LOAD(F2->prog)will see all the preceding changes in this memory.
And the rmb() isn't sufficient for that? Is another barrier needed before assigning the filter pointer to make sure the contents it points to are flushed?
What's the least time-consuming operation I can use in run_filters?
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCH v9 09/11] seccomp: introduce writer locking
Quoted 6 lines · Kees Cook
Quoted 4 lines · Oleg Nesterov
Just to simplify. Suppose
TIF_SECCOMPwas set a long ago. This thread has a single filter F1 and it entersseccomp_run_filters().
Quoted 8 lines
Quoted 6 lines
Right before it does
ACCESS_ONCE()to read the pointer, another thread doesseccomp_sync_threads()and sets .filter = F2.If
ACCESS_ONCE()returns F1 - everything is fine. But it can see the new pointer F2, and in this case we need a barrier to ensure that, say,LOAD(F2->prog)will see all the preceding changes in this memory.And the
rmb()isn't sufficient for that?
But it has no effect if the pointer was changed _after_ rmb() was already called.
And, you need a barrier _after_ ACCESS_ONCE().
(Unless, again, we know that this is the first filter, but this is only
by accident).
Quoted 3 lines
Is another barrier needed
before assigning the filter pointer to make sure the contents it points to are flushed?
I think smp_store_release() should be moved from seccomp_attach_filter() to seccomp_sync_threads(). Although probably it _should_ work either way, but at least this looks confusing because a) "current" doesn't need a barrier to serialize wuth itself, and b) it is not clear why it is safe to change the pointer dereferenced by another thread without a barrier.
Quoted 1 line
What's the least time-consuming operation I can use in
run_filters?
As I said smp_read_barrier_depends() (nop unless alpha) or smp_load_acquire() which you used in the previous version.
And to remind, afaics smp_load_acquire() in put_filter() should die ;)
Oleg.
Re: [PATCH v9 09/11] seccomp: introduce writer locking
In reply to Kees Cookkernel/seccomp.c static inline void seccomp_filter_free(struct seccomp_filter *filter) · line 389
Quoted 7 lines · Kees Cook
void
put_seccomp_filter(struct task_struct *tsk)
{kernel/seccomp.c +1
-1-struct seccomp_filter *orig = tsk->seccomp.filter; +struct seccomp_filter *orig =smp_load_acquire (&tsk->seccomp.filter);/* Clean up single-reference branches iteratively. */ while (orig &&atomic_dec_and_test (&orig->usage)) {
And this smp_load_acquire() looks unnecessary. atomic_dec_and_test() is a barrier.
Oleg.
PATCH v9 10/11 seccomp: allow mode setting across threads
In reply to Kees CookThis changes the mode setting helper to allow threads to change the seccomp mode from another thread. We must maintain barriers to keep TIF_SECCOMP synchronized with the rest of the seccomp state.
--- kernel/seccomp.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
kernel/seccomp.c +19 -8
@@ -207,12 +207,18 @@ static inline bool seccomp_check_mode(unsigned long seccomp_mode)return true; } -static inline void seccomp_assign_mode (unsigned long seccomp_mode) +static inline void seccomp_assign_mode (struct task_struct *task, +unsigned long seccomp_mode) { -BUG_ON (!spin_is_locked (¤t->sighand->siglock)); +BUG_ON (!spin_is_locked (&task->sighand->siglock)); - current->seccomp.mode = seccomp_mode; -set_tsk_thread_flag (current,TIF_SECCOMP ); + task->seccomp.mode = seccomp_mode; +/* +* Make sure TIF_SECCOMP cannot be set before the mode (and +* filter) is set. +*/ +smp_mb__before_atomic (); +set_tsk_thread_flag (task,TIF_SECCOMP ); }#ifdef CONFIG_SECCOMP_FILTER
@@ -433,12 +439,17 @@ static int mode1_syscalls_32[] = {int __secure_computing (int this_syscall) { -int mode = current->seccomp.mode;int exit_sig =0 ;int *syscall;u32 ret; -switch (mode) { +/* +* Make sure that any changes to mode from another thread have +* been seen after TIF_SECCOMP was seen. +*/ +rmb (); + +switch (current->seccomp.mode) {case SECCOMP_MODE_STRICT : syscall = mode1_syscalls;#ifdef CONFIG_COMPAT
@@ -543,7 +554,7 @@ static long seccomp_set_mode_strict(void)#ifdef TIF_NOTSC disable_TSC ();#endif -seccomp_assign_mode (seccomp_mode); +seccomp_assign_mode (current, seccomp_mode); ret =0 ; out:
@@ -593,7 +604,7 @@ static long seccomp_set_mode_filter(unsigned int flags,/* Do not free the successfully attached filter. */ prepared =NULL ; -seccomp_assign_mode (seccomp_mode); +seccomp_assign_mode (current, seccomp_mode); out:spin_unlock_irq (¤t->sighand->siglock);seccomp_filter_free (prepared);
Signature · 1 line
1.7.9.5
PATCH v9 11/11 seccomp: implement SECCOMP_FILTER_FLAG_TSYNC
In reply to Kees CookApplying restrictive seccomp filter programs to large or diverse codebases often requires handling threads which may be started early in the process lifetime (e.g., by code that is linked in). While it is possible to apply permissive programs prior to process start up, it is difficult to further restrict the kernel ABI to those threads after that point.
This change adds a new seccomp syscall flag to SECCOMP_SET_MODE_FILTER for synchronizing thread group seccomp filters at filter installation time.
When calling seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, filter) an attempt will be made to synchronize all threads in current's threadgroup to its new seccomp filter program. This is possible iff all threads are using a filter that is an ancestor to the filter current is attempting to synchronize to. NULL filters (where the task is running as SECCOMP_MODE_NONE) are also treated as ancestors allowing threads to be transitioned into SECCOMP_MODE_FILTER. If prctrl(PR_SET_NO_NEW_PRIVS, ...) has been set on the calling thread, no_new_privs will be set for all synchronized threads too. On success, 0 is returned. On failure, the pid of one of the failing threads will be returned and no filters will have been applied.
The race conditions against another thread are:
- requesting TSYNC (already handled by sighand lock)
- performing a clone (already handled by sighand lock)
- changing its filter (already handled by sighand lock)
- calling exec (handled by cred_guard_mutex)
The clone case is assisted by the fact that new threads will have their seccomp state duplicated from their parent before appearing on the tasklist.
Holding cred_guard_mutex means that seccomp filters cannot be assigned while in the middle of another thread's exec (potentially bypassing no_new_privs or similar). To make sure that de_thread() is actually able to kill other threads during an exec, any sighand holders need to check if they've been scheduled to be killed, and to give up on their work.
Based on patches by Will Drewry.
--- fs/exec.c | 2 +- include/linux/seccomp.h | 2 + include/uapi/linux/seccomp.h | 3 + kernel/seccomp.c | 139 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 144 insertions(+), 2 deletions(-)
fs/exec.c +1 -1
@@ -1216,7 +1216,7 @@ EXPORT_SYMBOL(install_exec_creds);/* * determine how safe it is to execute the proposed program * - the caller must hold ->cred_guard_mutex to protect against -* PTRACE_ATTACH +* PTRACE_ATTACH or seccomp thread-sync */ static void check_unsafe_exec (struct linux_binprm *bprm) {
include/linux/seccomp.h +2 -0
@@ -3,6 +3,8 @@#include <uapi/linux/seccomp.h> +#define SECCOMP_FILTER_FLAG_MASK ~(SECCOMP_FILTER_FLAG_TSYNC) +#ifdef CONFIG_SECCOMP #include <linux/thread_info.h>
include/uapi/linux/seccomp.h +3 -0
@@ -14,6 +14,9 @@#define SECCOMP_SET_MODE_STRICT 0#define SECCOMP_SET_MODE_FILTER 1 +/* Valid flags for SECCOMP_SET_MODE_FILTER */ +#define SECCOMP_FILTER_FLAG_TSYNC 1 +/* * All BPF programs must return a 32-bit value. * The bottom 16-bits are for optional return data.
kernel/seccomp.c +138 -1
188 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [PATCH v9 11/11] seccomp: implement SECCOMP_FILTER_FLAG_TSYNC
First of all, sorry for delay ;)
So far I quickly glanced at this series and everything look fine, but I am confused by the signal_group_exit() check,
Quoted 4 lines · Kees Cook
To make sure that
de_thread()is actually able
to kill other threads during an exec, any sighand holders need to check if they've been scheduled to be killed, and to give up on their work.
Probably this connects to that check below? I can't understand this...
kernel/seccomp.c static long seccomp_set_mode_filter(unsigned int flags, · line 723
Quoted 7 lines
kernel/seccomp.c +7
-0+/* +* Make sure we cannot change seccomp or nnp state via TSYNC +* while another thread is in the middle of calling exec. +*/ +if (flags &SECCOMP_FILTER_FLAG_TSYNC && +mutex_lock_killable (¤t->signal->cred_guard_mutex)) + goto out_free;
-EINVAL looks a bit confusing in this case, but this is cosemtic because userspace won't see this error-code anyway.
kernel/seccomp.c static long seccomp_set_mode_strict(void) · line 667
Quoted 3 lines
kernel/seccomp.c +2
-0spin_lock_irq (¤t->sighand->siglock); +if (unlikely (signal_group_exit (current->signal))) { +/* If thread is dying, return to process the signal. */
OK, this doesn't hurt, but why?
You could check __fatal_signal_pending() with the same effect. And since we hold this mutex, exec (de_thread) can be the source of that SIGKILL. We take this mutex specially to avoid the race with exec.
So why do we need to abort if we race with kill() or exit_grouo() ?
Oleg.
Re: [PATCH v9 11/11] seccomp: implement SECCOMP_FILTER_FLAG_TSYNC
Quoted 21 lines · Oleg Nesterov
First of all, sorry for delay ;)
So far I quickly glanced at this series and everything look fine, but I am confused by the
signal_group_exit()check,Quoted 4 lines · Kees Cook
To make sure that
de_thread()is actually able
to kill other threads during an exec, any sighand holders need to check if they've been scheduled to be killed, and to give up on their work.Probably this connects to that check below? I can't understand this...
kernel/seccomp.c static long seccomp_set_mode_filter(unsigned int flags, · line 723
Quoted 7 lines
kernel/seccomp.c +7
-0+/* +* Make sure we cannot change seccomp or nnp state via TSYNC +* while another thread is in the middle of calling exec. +*/ +if (flags &SECCOMP_FILTER_FLAG_TSYNC && +mutex_lock_killable (¤t->signal->cred_guard_mutex)) + goto out_free;
Quoted 2 lines
-EINVALlooks a bit confusing in this case, but this is cosemtic because userspace won't see this error-code anyway.
Happy to use whatever since, as you say, it's cosmetic. Perhaps -EAGAIN?
Quoted 5 lines
Review
Quoted 4 lines
spin_lock_irq(¤t->sighand->siglock);(quoted diff) +2
-0+ if (unlikely(signal_group_exit(current->signal))) { + /* If thread is dying, return to process the signal. */
Quoted 7 lines
OK, this doesn't hurt, but why?
You could check
__fatal_signal_pending()with the same effect. And since we hold this mutex, exec (de_thread) can be the source of that SIGKILL. We take this mutex specially to avoid the race with exec.So why do we need to abort if we race with
kill()orexit_grouo()?
In my initial code inspection that we could block waiting for the cred_guard mutex, with exec holding it, exec would schedule death in de_thread, and then once it released, the tsync thread would try to keep running.
However, in looking at this again, now I'm concerned this produces a dead-lock in de_thread, since it waits for all threads to actually die, but tsync will be waiting with the killable mutex.
So I think I got too defensive when I read the top of de_thread where it checks for pending signals itself.
It seems like I can just safely remove the singal_group_exit checks? The other paths (non-tsync seccomp_set_mode_filter, and seccomp_set_mode_strict) would just run until it finished the syscall, and then died. I can't decide which feels cleaner: just letting stuff clean up naturally on death or to short-circuit after taking sighand->siglock.
What do you think?
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCH v9 11/11] seccomp: implement SECCOMP_FILTER_FLAG_TSYNC
Quoted 11 lines · Kees Cook
Quoted 9 lines · Oleg Nesterov
Quoted 8 lines
kernel/seccomp.c +7
-0+/* +* Make sure we cannot change seccomp or nnp state via TSYNC +* while another thread is in the middle of calling exec. +*/ +if (flags &SECCOMP_FILTER_FLAG_TSYNC && +mutex_lock_killable (¤t->signal->cred_guard_mutex)) + goto out_free;
Quoted 4 lines
Quoted 2 lines
-EINVALlooks a bit confusing in this case, but this is cosemtic because userspace won't see this error-code anyway.Happy to use whatever since, as you say, it's cosmetic. Perhaps
-EAGAIN?
Or -EINTR. I do not really mind, I only mentioned this because I had another nit.
Quoted 12 lines
Quoted 11 lines
Review
Quoted 3 lines
spin_lock_irq(¤t->sighand->siglock);(quoted diff) +2
-0+ if (unlikely(signal_group_exit(current->signal))) { + /* If thread is dying, return to process the signal. */OK, this doesn't hurt, but why?
You could check
__fatal_signal_pending()with the same effect. And since we hold this mutex, exec (de_thread) can be the source of that SIGKILL. We take this mutex specially to avoid the race with exec.So why do we need to abort if we race with
kill()orexit_grouo()?
Quoted 8 lines
In my initial code inspection that we could block waiting for the
cred_guardmutex, with exec holding it, exec would schedule death inde_thread, and then once it released, the tsync thread would try to keep running.However, in looking at this again, now I'm concerned this produces a dead-lock in
de_thread, since it waits for all threads to actually die, but tsync will be waiting with the killable mutex.
That is why you should always use _killable (or _interruptible) if you want to take ->cred_guard_mutex.
If this thread races with de_thread() which holds this mutex, it will be killed and mutex_lock_killable() will fail.
(to clarify; this deadlock is not "fatal", de_thread() can be killed too,
but this doesn't really matter).
Quoted 6 lines
So I think I got too defensive when I read the top of
de_threadwhere it checks for pending signals itself.It seems like I can just safely remove the
singal_group_exitchecks? The other paths (non-tsyncseccomp_set_mode_filter, andseccomp_set_mode_strict)
Yes, I missed another signal_group_exit() in seccomp_set_mode_strict(). It looks equally unneeded.
Quoted 3 lines
I can't decide which feels cleaner: just letting stuff clean up naturally on death or to short-circuit after taking
sighand->siglock.
I'd prefer to simply remove the singal_group_exit checks.
I won't argue if you prefer to keep them, but then please add a comment to explain that this is not needed for correctness.
Because otherwise the code looks confusing, as if there is a subtle reason why we must not do this if killed.
Oleg.
Re: [PATCH v9 11/11] seccomp: implement SECCOMP_FILTER_FLAG_TSYNC
Quoted 64 lines · Oleg Nesterov
Quoted 11 lines · Kees Cook
Quoted 9 lines · Oleg Nesterov
Quoted 8 lines
kernel/seccomp.c +7
-0+/* +* Make sure we cannot change seccomp or nnp state via TSYNC +* while another thread is in the middle of calling exec. +*/ +if (flags &SECCOMP_FILTER_FLAG_TSYNC && +mutex_lock_killable (¤t->signal->cred_guard_mutex)) + goto out_free;Quoted 4 lines
Quoted 2 lines
-EINVALlooks a bit confusing in this case, but this is cosemtic because userspace won't see this error-code anyway.Happy to use whatever since, as you say, it's cosmetic. Perhaps
-EAGAIN?Or
-EINTR. I do not really mind, I only mentioned this because I had another nit.Quoted 12 lines
Quoted 11 lines
Review
Quoted 3 lines
spin_lock_irq(¤t->sighand->siglock);(quoted diff) +2
-0+ if (unlikely(signal_group_exit(current->signal))) { + /* If thread is dying, return to process the signal. */OK, this doesn't hurt, but why?
You could check
__fatal_signal_pending()with the same effect. And since we hold this mutex, exec (de_thread) can be the source of that SIGKILL. We take this mutex specially to avoid the race with exec.So why do we need to abort if we race with
kill()orexit_grouo()?Quoted 8 lines
In my initial code inspection that we could block waiting for the
cred_guardmutex, with exec holding it, exec would schedule death inde_thread, and then once it released, the tsync thread would try to keep running.However, in looking at this again, now I'm concerned this produces a dead-lock in
de_thread, since it waits for all threads to actually die, but tsync will be waiting with the killable mutex.That is why you should always use
_killable(or_interruptible) if you want to take ->cred_guard_mutex.If this thread races with
de_thread()which holds this mutex, it will be killed andmutex_lock_killable()will fail.(to clarify; this deadlock is not "fatal",
de_thread()can be killed too,
but this doesn't really matter).Quoted 6 lines
So I think I got too defensive when I read the top of
de_threadwhere it checks for pending signals itself.It seems like I can just safely remove the
singal_group_exitchecks? The other paths (non-tsyncseccomp_set_mode_filter, andseccomp_set_mode_strict)Yes, I missed another
signal_group_exit()inseccomp_set_mode_strict(). It looks equally unneeded.Quoted 3 lines
I can't decide which feels cleaner: just letting stuff clean up naturally on death or to short-circuit after taking
sighand->siglock.
Quoted 7 lines
I'd prefer to simply remove the
singal_group_exitchecks.I won't argue if you prefer to keep them, but then please add a comment to explain that this is not needed for correctness.
Because otherwise the code looks confusing, as if there is a subtle reason why we must not do this if killed.
Sounds good! I'll clean it all up for v10.
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security