Synchronize kernel RSE to user-space and back
PATCH 2/3 Synchronize kernel RSE to user-space and back
This is base kernel patch for ptrace RSE bug. It's basically a backport from the utrace RSE patch I sent out several weeks ago. please review.
when a thread is stopped (ptraced), debugger might change thread's user stack (change memory directly), and we must avoid the RSE stored in kernel to override user stack (user space's RSE is newer than kernel's in the case). To workaround the issue, we copy kernel RSE to user RSE before the task is stopped, so user RSE has updated data. we then copy user RSE to kernel after the task is resummed from traced stop and kernel will use the newer RSE to return to user.
--- arch/ia64/kernel/process.c | 6 +++ arch/ia64/kernel/ptrace.c | 80 +++++++++++++++++++++++++++++++++++++++++ include/asm-ia64/ptrace.h | 5 ++ include/asm-ia64/thread_info.h | 2 + include/linux/ptrace.h | 8 ++++ kernel/signal.c | 33 ++++++++++++++++ 6 files changed, 133 insertions(+), 1 deletion(-)
arch/ia64/kernel/process.c +6 -0
@@ -161,6 +161,8 @@ #ifdef CONFIG_PERFMONif (tsk->thread.pfm_needs_checking)return ;#endif +if (test_ti_thread_flag (task_thread_info (tsk),TIF_RESTORE_RSE )) +return ;clear_ti_thread_flag (task_thread_info (tsk),TIF_NOTIFY_RESUME ); }
@@ -182,6 +184,10 @@ #endif/* deal with pending signal delivery */ if (test_thread_flag (TIF_SIGPENDING )||test_thread_flag (TIF_RESTORE_SIGMASK ))ia64_do_signal (scr, in_syscall); + +/* copy user rbs to kernel rbs */ +if (unlikely (test_thread_flag (TIF_RESTORE_RSE ))) +ia64_sync_krbs (); }static int pal_halt =1 ;
arch/ia64/kernel/ptrace.c +80 -0
108 lines not on this page · raw message
include/asm-ia64/ptrace.h +5 -0
@@ -292,6 +292,7 @@ # define force_successful_syscall_return unsigned long, long);extern void ia64_flush_fph (struct task_struct *);extern void ia64_sync_fph (struct task_struct *); +extern void ia64_sync_krbs (void );extern long ia64_sync_user_rbs (struct task_struct *,struct switch_stack *,unsigned long ,unsigned long );
@@ -303,6 +304,10 @@ # define force_successful_syscall_returnextern void ia64_increment_ip (struct pt_regs *pt);extern void ia64_decrement_ip (struct pt_regs *pt); +extern void ia64_ptrace_stop (void ); + +#define arch_ptrace_stop ia64_ptrace_stop +#define arch_ptrace_stop_needed () 1#endif /* !__KERNEL__ */ /* pt_all_user_regs is used for PTRACE_GETREGS PTRACE_SETREGS */
include/asm-ia64/thread_info.h +2 -0
@@ -94,6 +94,7 @@ #define TIF_MEMDIE 17#define TIF_MCA_INIT 18/* this task is processing MCA or INIT */ #define TIF_DB_DISABLED 19/* debug trap disabled for fsyscall */ #define TIF_FREEZE 20/* is freezing for suspend */ +#define TIF_RESTORE_RSE 21/* user RBS is newer than kernel RBS */ #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
@@ -107,6 +108,7 @@ #define _TIF_POLLING_NRFLAG (1 << TIF_PO#define _TIF_MCA_INIT (1 << TIF_MCA_INIT)#define _TIF_DB_DISABLED (1 << TIF_DB_DISABLED)#define _TIF_FREEZE (1 << TIF_FREEZE) +#define _TIF_RESTORE_RSE (1 << TIF_RESTORE_RSE)/* "work to do on user-return" bits */ #define TIF_ALLWORK_MASK (_TIF_SIGPENDING|_TIF_NOTIFY_RESUME|_TIF_SYSCALL_AUDIT|\
include/linux/ptrace.h +8 -0
@@ -128,6 +128,14 @@ #ifndef force_successful_syscall_return#define force_successful_syscall_return () do { } while (0)#endif +#ifndef arch_ptrace_stop +#define arch_ptrace_stop () do { } while (0) +#endif + +#ifndef arch_ptrace_stop_needed +#define arch_ptrace_stop_needed () 0 +#endif +#endif #endif
kernel/signal.c +32 -1
@@ -1594,6 +1594,17 @@ static inline int may_ptrace_stop(void) }/* +* Return non-zero if there is a SIGKILL that should be waking us up. +* Called with the siglock held. +*/ +static int sigkill_pending (struct task_struct *tsk) +{ +return ((sigismember (&tsk->pending.signal,SIGKILL ) || +sigismember (&tsk->signal->shared_pending.signal,SIGKILL )) && + !unlikely (sigismember (&tsk->blocked,SIGKILL ))); +} + +/* * This must be called with current->sighand->siglock held. * * This should be the path for all ptrace stops.
@@ -1606,6 +1617,26 @@ static inline int may_ptrace_stop(void)*/ static void ptrace_stop (int exit_code,int nostop_code,siginfo_t *info) { +int killed =0 ; + +if (arch_ptrace_stop_needed ()) { +/* +* The arch code has something special to do before a +* ptrace stop. This is allowed to block, e.g. for faults +* on user stack pages. We can't keep the siglock while +* calling arch_ptrace_stop, so we must release it now. +* To preserve proper semantics, we must do this before +* any signal bookkeeping like checking group_stop_count. +* Meanwhile, a SIGKILL could come in before we retake the +* siglock. That must prevent us from sleeping in TASK_TRACED. +* So after regaining the lock, we must check for SIGKILL. +*/ +spin_unlock_irq (¤t->sighand->siglock); +arch_ptrace_stop (); +spin_lock_irq (¤t->sighand->siglock); + killed =sigkill_pending (current); + } +/* * If there is a group stop in progress, * we must participate in the bookkeeping.
@@ -1621,7 +1652,7 @@ static void ptrace_stop(int exit_code, ispin_unlock_irq (¤t->sighand->siglock);try_to_freeze ();read_lock (&tasklist_lock); -if (may_ptrace_stop ()) { +if (likely (!killed) &&may_ptrace_stop ()) {do_notify_parent_cldstop (current,CLD_TRAPPED );read_unlock (&tasklist_lock);schedule ();
include/asm-ia64/ptrace.h # define force_successful_syscall_return · line 309
Quoted 3 lines
include/asm-ia64/ptrace.h +3
-0+extern void ia64_ptrace_stop (void ); + +#define arch_ptrace_stop ia64_ptrace_stop
My version of the macro takes arguments, in case some arch ever cares about the details of the stop. I saw no reason to remove them. So:
#define arch_ptrace_stop(code, info) ia64_ptrace_stop()
Quoted 1 line
+ #define
arch_ptrace_stop_needed()1
This should be:
#define arch_ptrace_stop_needed() (!test_thread_flag(TIF_RESTORE_RSE))
It is worthwhile to avoid dropping the siglock when it's not necessary, e.g. on subsequent signal stops after a previous stop.
Thanks,
Roland
PATCH 2/3 Synchronize kernel RSE to user-space and back
In reply to Petr TesarikThis is base kernel patch for ptrace RSE bug. It's basically a backport from the utrace RSE patch I sent out several weeks ago. please review.
when a thread is stopped (ptraced), debugger might change thread's user stack (change memory directly), and we must avoid the RSE stored in kernel to override user stack (user space's RSE is newer than kernel's in the case). To workaround the issue, we copy kernel RSE to user RSE before the task is stopped, so user RSE has updated data. we then copy user RSE to kernel after the task is resummed from traced stop and kernel will use the newer RSE to return to user.
--- arch/ia64/kernel/process.c | 6 +++ arch/ia64/kernel/ptrace.c | 82 ++++++++++++++++++++++++++++++++++++++++ include/asm-ia64/ptrace.h | 7 +++ include/asm-ia64/thread_info.h | 2 + 4 files changed, 97 insertions(+), 0 deletions(-)
arch/ia64/kernel/process.c +6 -0
@@ -161,6 +161,8 @@ #ifdef CONFIG_PERFMONif (tsk->thread.pfm_needs_checking)return ;#endif +if (test_ti_thread_flag (task_thread_info (tsk),TIF_RESTORE_RSE )) +return ;clear_ti_thread_flag (task_thread_info (tsk),TIF_NOTIFY_RESUME ); }
@@ -182,6 +184,10 @@ #endif/* deal with pending signal delivery */ if (test_thread_flag (TIF_SIGPENDING )||test_thread_flag (TIF_RESTORE_SIGMASK ))ia64_do_signal (scr, in_syscall); + +/* copy user rbs to kernel rbs */ +if (unlikely (test_thread_flag (TIF_RESTORE_RSE ))) +ia64_sync_krbs (); }static int pal_halt =1 ;
arch/ia64/kernel/ptrace.c +82 -0
116 lines not on this page · raw message
include/asm-ia64/ptrace.h +7 -0
@@ -292,6 +292,7 @@ # define force_successful_syscall_return unsigned long, long);extern void ia64_flush_fph (struct task_struct *);extern void ia64_sync_fph (struct task_struct *); +extern void ia64_sync_krbs (void );extern long ia64_sync_user_rbs (struct task_struct *,struct switch_stack *,unsigned long ,unsigned long );
@@ -303,6 +304,12 @@ # define force_successful_syscall_returnextern void ia64_increment_ip (struct pt_regs *pt);extern void ia64_decrement_ip (struct pt_regs *pt); +extern void ia64_ptrace_stop (void ); +#define arch_ptrace_stop (code, info) \ + ia64_ptrace_stop() +#define arch_ptrace_stop_needed (code, info) \ + (!test_thread_flag(TIF_RESTORE_RSE)) +#endif /* !__KERNEL__ */ /* pt_all_user_regs is used for PTRACE_GETREGS PTRACE_SETREGS */
include/asm-ia64/thread_info.h +2 -0
@@ -94,6 +94,7 @@ #define TIF_MEMDIE 17#define TIF_MCA_INIT 18/* this task is processing MCA or INIT */ #define TIF_DB_DISABLED 19/* debug trap disabled for fsyscall */ #define TIF_FREEZE 20/* is freezing for suspend */ +#define TIF_RESTORE_RSE 21/* user RBS is newer than kernel RBS */ #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
@@ -107,6 +108,7 @@ #define _TIF_POLLING_NRFLAG (1 << TIF_PO#define _TIF_MCA_INIT (1 << TIF_MCA_INIT)#define _TIF_DB_DISABLED (1 << TIF_DB_DISABLED)#define _TIF_FREEZE (1 << TIF_FREEZE) +#define _TIF_RESTORE_RSE (1 << TIF_RESTORE_RSE)/* "work to do on user-return" bits */ #define TIF_ALLWORK_MASK (_TIF_SIGPENDING|_TIF_NOTIFY_RESUME|_TIF_SYSCALL_AUDIT|\