seccomp: remove the 'sd' argument from __secure_computing()
Subsystems (5)
Oleg Nesterov
maintainerv2 cover seccomp: remove the 'sd' argument from __secure_computing()
Hello,
Link to v1: https://lore.kernel.org/all/[email protected]/
Only 2/4 was changed, please see interdiff at the end.
I've included the acks I got on 1/4, 3/4, and 4/4 (thanks!).
Oleg.
---
arch/mips/kernel/ptrace.c | 20 ++----------------- arch/powerpc/kernel/ptrace/ptrace.c | 2 +- include/linux/seccomp.h | 12 ++++-------- kernel/entry/common.c | 2 +- kernel/seccomp.c | 39 ++++++++++++++++++------------------- 5 files changed, 27 insertions(+), 48 deletions(-)
-------------------------------------------------------------------------------
include/linux/seccomp.h +2 -2
@@ -22,8 +22,9 @@#include <linux/atomic.h> #include <asm/seccomp.h> -#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER extern int __secure_computing (void ); + +#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER static inline int secure_computing (void ) {if (unlikely (test_syscall_work (SECCOMP )))
@@ -32,7 +33,6 @@ static inline int secure_computing(void) }#else extern void secure_computing_strict (int this_syscall); -static inline int __secure_computing (void ) {return 0 ; }#endif extern long prctl_get_seccomp (void );
kernel/seccomp.c +9 -4
@@ -29,13 +29,11 @@#include <linux/syscalls.h> #include <linux/sysctl.h> +#include <asm/syscall.h> +/* Not exposed in headers: strictly internal use only. */ #define SECCOMP_MODE_DEAD (SECCOMP_MODE_FILTER + 1) -#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER -#include <asm/syscall.h> -#endif -#ifdef CONFIG_SECCOMP_FILTER #include <linux/file.h> #include <linux/filter.h>
@@ -1062,6 +1060,13 @@ void secure_computing_strict(int this_syscall) else BUG(); } +int __secure_computing (void ) +{ +int this_syscall =syscall_get_nr (current,current_pt_regs ()); + +secure_computing_strict (this_syscall); +return 0 ; +}#else #ifdef CONFIG_SECCOMP_FILTER
Oleg Nesterov
maintainerPATCH v2 1/4 seccomp/mips: change syscall_trace_enter() to use secure_computing()
arch/mips/Kconfig selects HAVE_ARCH_SECCOMP_FILTER so syscall_trace_enter() can just use __secure_computing(NULL) and rely on populate_seccomp_data(sd) and "sd == NULL" checks in __secure_computing(sd) paths.
With the change above syscall_trace_enter() can just use secure_computing() and avoid #ifdef + test_thread_flag(TIF_SECCOMP). CONFIG_GENERIC_ENTRY is not defined, so test_syscall_work(SECCOMP) will check TIF_SECCOMP.
--- arch/mips/kernel/ptrace.c | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-)
arch/mips/kernel/ptrace.c +2 -18
@@ -1326,24 +1326,8 @@ asmlinkage long syscall_trace_enter(struct pt_regs *regs)return -1 ; } -#ifdef CONFIG_SECCOMP -if (unlikely (test_thread_flag (TIF_SECCOMP ))) { -int ret, i; -struct seccomp_data sd; -unsigned long args[6 ]; - - sd.nr =current_thread_info ()->syscall; - sd.arch =syscall_get_arch (current); -syscall_get_arguments (current, regs, args); -for (i =0 ; i <6 ; i++) - sd.args[i] = args[i]; - sd.instruction_pointer =KSTK_EIP (current); - - ret =__secure_computing (&sd); -if (ret ==-1 ) -return ret; - } -#endif +if (secure_computing ()) +return -1 ;if (unlikely (test_thread_flag (TIF_SYSCALL_TRACEPOINT )))trace_sys_enter (regs, regs->regs[2 ]);
Signature · 1 line
2.25.1.362.g51ebf55
Oleg Nesterov
maintainerPATCH v2 2/4 seccomp: fix the __secure_computing() stub for !HAVE_ARCH_SECCOMP_FILTER
In reply to Oleg NesterovDepending on CONFIG_HAVE_ARCH_SECCOMP_FILTER, __secure_computing(NULL) will crash or not. This is not consistent/safe, especially considering that after the previous change __secure_computing(sd) is always called with sd == NULL.
Fortunately, if CONFIG_HAVE_ARCH_SECCOMP_FILTER=n, __secure_computing() has no callers, these architectures use secure_computing_strict(). Yet it make sense make __secure_computing(NULL) safe in this case.
Note also that with this change we can unexport secure_computing_strict() and change the current callers to use __secure_computing(NULL).
--- include/linux/seccomp.h | 8 ++------ kernel/seccomp.c | 14 ++++++++++---- 2 files changed, 12 insertions(+), 10 deletions(-)
include/linux/seccomp.h +2 -6
@@ -22,8 +22,9 @@#include <linux/atomic.h> #include <asm/seccomp.h> -#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER extern int __secure_computing (const struct seccomp_data *sd); + +#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER static inline int secure_computing (void ) {if (unlikely (test_syscall_work (SECCOMP )))
@@ -32,11 +33,6 @@ static inline int secure_computing(void) }#else extern void secure_computing_strict (int this_syscall); -static inline int __secure_computing (const struct seccomp_data *sd) -{ -secure_computing_strict (sd->nr); -return 0 ; -}#endif extern long prctl_get_seccomp (void );
kernel/seccomp.c +10 -4
@@ -29,13 +29,11 @@#include <linux/syscalls.h> #include <linux/sysctl.h> +#include <asm/syscall.h> +/* Not exposed in headers: strictly internal use only. */ #define SECCOMP_MODE_DEAD (SECCOMP_MODE_FILTER + 1) -#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER -#include <asm/syscall.h> -#endif -#ifdef CONFIG_SECCOMP_FILTER #include <linux/file.h> #include <linux/filter.h>
@@ -1062,6 +1060,14 @@ void secure_computing_strict(int this_syscall) else BUG(); } +int __secure_computing (const struct seccomp_data *sd) +{ +int this_syscall = sd ? sd->nr : +syscall_get_nr (current,current_pt_regs ()); + +secure_computing_strict (this_syscall); +return 0 ; +}#else #ifdef CONFIG_SECCOMP_FILTER
Signature · 1 line
2.25.1.362.g51ebf55
Re: [PATCH v2 2/4] seccomp: fix the __secure_computing() stub for !HAVE_ARCH_SECCOMP_FILTER
Quoted 9 lines · Oleg Nesterov
Depending on
CONFIG_HAVE_ARCH_SECCOMP_FILTER,__secure_computing(NULL)will crash or not. This is not consistent/safe, especially considering that after the previous change__secure_computing(sd)is always called with sd == NULL.Fortunately, if
CONFIG_HAVE_ARCH_SECCOMP_FILTER=n,__secure_computing()has no callers, these architectures usesecure_computing_strict(). Yet it make sense make__secure_computing(NULL)safe in this case.
Quoted 5 lines
Note also that with this change we can unexport
secure_computing_strict()and change the current callers to use__secure_computing(NULL).Fixes 8cf8dfceebda ("seccomp: Stub for !HAVE_ARCH_SECCOMP_FILTER")Signed-off-by Oleg Nesterov <[email protected]>
I had no idea it was this complex, thanks a lot for looking into this Oleg!
Yours,
Linus Walleij
Oleg Nesterov
maintainerPATCH v2 3/4 seccomp: remove the 'sd' argument from __secure_computing()
In reply to Oleg NesterovAfter the previous changes 'sd' is always NULL.
--- arch/powerpc/kernel/ptrace/ptrace.c | 2 +- include/linux/seccomp.h | 6 +++--- kernel/entry/common.c | 2 +- kernel/seccomp.c | 12 +++++------- 4 files changed, 10 insertions(+), 12 deletions(-)
arch/powerpc/kernel/ptrace/ptrace.c +1 -1
@@ -215,7 +215,7 @@ static int do_seccomp(struct pt_regs *regs)* have already loaded -ENOSYS into r3, or seccomp has put * something else in r3 (via SECCOMP_RET_ERRNO/TRACE). */ -if (__secure_computing (NULL )) +if (__secure_computing ())return -1 ;/*
include/linux/seccomp.h +3 -3
@@ -22,13 +22,13 @@#include <linux/atomic.h> #include <asm/seccomp.h> -extern int __secure_computing (const struct seccomp_data *sd); +extern int __secure_computing (void );#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER static inline int secure_computing (void ) {if (unlikely (test_syscall_work (SECCOMP ))) -return __secure_computing (NULL ); +return __secure_computing ();return 0 ; }#else
@@ -54,7 +54,7 @@ static inline int secure_computing(void) { return 0; }#else static inline void secure_computing_strict (int this_syscall) {return ; }#endif -static inline int __secure_computing (const struct seccomp_data *sd) {return 0 ; } +static inline int __secure_computing (void ) {return 0 ; }static inline long prctl_get_seccomp (void ) {
kernel/entry/common.c +1 -1
@@ -49,7 +49,7 @@ long syscall_trace_enter(struct pt_regs *regs, long syscall,/* Do seccomp after ptrace, to catch any tracer changes. */ if (work &SYSCALL_WORK_SECCOMP ) { - ret =__secure_computing (NULL ); + ret =__secure_computing ();if (ret ==-1L )return ret; }
kernel/seccomp.c +5 -7
@@ -1060,10 +1060,9 @@ void secure_computing_strict(int this_syscall)else BUG (); } -int __secure_computing (const struct seccomp_data *sd) +int __secure_computing (void ) { -int this_syscall = sd ? sd->nr : -syscall_get_nr (current,current_pt_regs ()); +int this_syscall =syscall_get_nr (current,current_pt_regs ());secure_computing_strict (this_syscall);return 0 ;
@@ -1353,7 +1352,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, }#endif -int __secure_computing (const struct seccomp_data *sd) +int __secure_computing (void ) {int mode = current->seccomp.mode;int this_syscall;
@@ -1362,15 +1361,14 @@ int __secure_computing(const struct seccomp_data *sd) unlikely(current->ptrace & PT_SUSPEND_SECCOMP))return 0 ; - this_syscall = sd ? sd->nr : -syscall_get_nr (current,current_pt_regs ()); + this_syscall =syscall_get_nr (current,current_pt_regs ());switch (mode) {case SECCOMP_MODE_STRICT :__secure_computing_strict (this_syscall);/* may call do_exit */ return 0 ;case SECCOMP_MODE_FILTER : -return __seccomp_filter (this_syscall, sd, false); +return __seccomp_filter (this_syscall,NULL , false);/* Surviving SECCOMP_RET_KILL_* must be proactively impossible. */ case SECCOMP_MODE_DEAD :WARN_ON_ONCE (1 );
Signature · 1 line
2.25.1.362.g51ebf55
Oleg Nesterov
maintainerPATCH v2 4/4 seccomp: remove the 'sd' argument from __seccomp_filter()
In reply to Oleg NesterovAfter the previous change 'sd' is always NULL.
--- kernel/seccomp.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
kernel/seccomp.c +8 -13
@@ -1218,13 +1218,12 @@ static int seccomp_do_user_notification(int this_syscall,return -1 ; } -static int __seccomp_filter (int this_syscall,const struct seccomp_data *sd, -const bool recheck_after_trace) +static int __seccomp_filter (int this_syscall,const bool recheck_after_trace) {u32 filter_ret, action; +struct seccomp_data sd;struct seccomp_filter *match =NULL ;int data; -struct seccomp_data sd_local;/* * Make sure that any changes to mode from another thread have
@@ -1232,12 +1231,9 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,*/ smp_rmb (); -if (!sd) { -populate_seccomp_data (&sd_local); - sd = &sd_local; - } +populate_seccomp_data (&sd); - filter_ret =seccomp_run_filters (sd, &match); + filter_ret =seccomp_run_filters (&sd, &match); data = filter_ret &SECCOMP_RET_DATA ; action = filter_ret &SECCOMP_RET_ACTION_FULL ;
@@ -1295,13 +1291,13 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,* a reload of all registers. This does not goto skip since * a skip would have already been reported. */ -if (__seccomp_filter (this_syscall,NULL , true)) +if (__seccomp_filter (this_syscall, true))return -1 ;return 0 ;case SECCOMP_RET_USER_NOTIF : -if (seccomp_do_user_notification (this_syscall, match, sd)) +if (seccomp_do_user_notification (this_syscall, match, &sd)) goto skip;return 0 ;
@@ -1343,8 +1339,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, return -1; }#else -static int __seccomp_filter (int this_syscall,const struct seccomp_data *sd, -const bool recheck_after_trace) +static int __seccomp_filter (int this_syscall,const bool recheck_after_trace) {BUG ();
@@ -1368,7 +1363,7 @@ int __secure_computing(void)__secure_computing_strict (this_syscall);/* may call do_exit */ return 0 ;case SECCOMP_MODE_FILTER : -return __seccomp_filter (this_syscall,NULL , false); +return __seccomp_filter (this_syscall, false);/* Surviving SECCOMP_RET_KILL_* must be proactively impossible. */ case SECCOMP_MODE_DEAD :WARN_ON_ONCE (1 );
Signature · 1 line
2.25.1.362.g51ebf55
Quoted 8 lines · Oleg Nesterov
Link to v1: https://lore.kernel.org/all/[email protected]/
Only 2/4 was changed, please see interdiff at the end.I've included the acks I got on 1/4, 3/4, and 4/4 (thanks!).
Oleg.
[...]
Applied to for-next/seccomp, thanks!
[0/4] seccomp: remove the 'sd' argument from __secure_computing()
https://git.kernel.org/kees/c/1027cd8084bb [1/4] seccomp/mips: change syscall_trace_enter() to use secure_computing() https://git.kernel.org/kees/c/0fe1ebf3f056 [2/4] seccomp: fix the __secure_computing() stub for !HAVE_ARCH_SECCOMP_FILTER https://git.kernel.org/kees/c/b37778bec82b [3/4] seccomp: remove the 'sd' argument from __secure_computing() https://git.kernel.org/kees/c/1027cd8084bb [4/4] seccomp: remove the 'sd' argument from __seccomp_filter() https://git.kernel.org/kees/c/e1cec5107c39
Take care,
Signature · 1 line
Kees Cook