Commit 6342adca authored by Gabriel Krisman Bertazi's avatar Gabriel Krisman Bertazi Committed by Thomas Gleixner

entry: Ensure trap after single-step on system call return

Commit 29915524 ("entry: Drop usage of TIF flags in the generic syscall
code") introduced a bug on architectures using the generic syscall entry
code, in which processes stopped by PTRACE_SYSCALL do not trap on syscall
return after receiving a TIF_SINGLESTEP.

The reason is that the meaning of TIF_SINGLESTEP flag is overloaded to
cause the trap after a system call is executed, but since the above commit,
the syscall call handler only checks for the SYSCALL_WORK flags on the exit
work.

Split the meaning of TIF_SINGLESTEP such that it only means single-step
mode, and create a new type of SYSCALL_WORK to request a trap immediately
after a syscall in single-step mode.  In the current implementation, the
SYSCALL_WORK flag shadows the TIF_SINGLESTEP flag for simplicity.

Update x86 to flip this bit when a tracer enables single stepping.

Fixes: 29915524 ("entry: Drop usage of TIF flags in the generic syscall code")
Suggested-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: default avatarGabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Tested-by: default avatarKyle Huey <me@kylehuey.com>
Link: https://lore.kernel.org/r/87h7mtc9pr.fsf_-_@collabora.com
parent 1048ba83
...@@ -43,8 +43,6 @@ static __always_inline void arch_check_user_regs(struct pt_regs *regs) ...@@ -43,8 +43,6 @@ static __always_inline void arch_check_user_regs(struct pt_regs *regs)
} }
#define arch_check_user_regs arch_check_user_regs #define arch_check_user_regs arch_check_user_regs
#define ARCH_SYSCALL_EXIT_WORK (_TIF_SINGLESTEP)
static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs, static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
unsigned long ti_work) unsigned long ti_work)
{ {
......
...@@ -127,12 +127,17 @@ static int enable_single_step(struct task_struct *child) ...@@ -127,12 +127,17 @@ static int enable_single_step(struct task_struct *child)
regs->flags |= X86_EFLAGS_TF; regs->flags |= X86_EFLAGS_TF;
/* /*
* Always set TIF_SINGLESTEP - this guarantees that * Always set TIF_SINGLESTEP. This will also
* we single-step system calls etc.. This will also
* cause us to set TF when returning to user mode. * cause us to set TF when returning to user mode.
*/ */
set_tsk_thread_flag(child, TIF_SINGLESTEP); set_tsk_thread_flag(child, TIF_SINGLESTEP);
/*
* Ensure that a trap is triggered once stepping out of a system
* call prior to executing any user instruction.
*/
set_task_syscall_work(child, SYSCALL_EXIT_TRAP);
oflags = regs->flags; oflags = regs->flags;
/* Set TF on the kernel stack.. */ /* Set TF on the kernel stack.. */
...@@ -230,6 +235,7 @@ void user_disable_single_step(struct task_struct *child) ...@@ -230,6 +235,7 @@ void user_disable_single_step(struct task_struct *child)
/* Always clear TIF_SINGLESTEP... */ /* Always clear TIF_SINGLESTEP... */
clear_tsk_thread_flag(child, TIF_SINGLESTEP); clear_tsk_thread_flag(child, TIF_SINGLESTEP);
clear_task_syscall_work(child, SYSCALL_EXIT_TRAP);
/* But touch TF only if it was set by us.. */ /* But touch TF only if it was set by us.. */
if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
......
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
SYSCALL_WORK_SYSCALL_TRACE | \ SYSCALL_WORK_SYSCALL_TRACE | \
SYSCALL_WORK_SYSCALL_AUDIT | \ SYSCALL_WORK_SYSCALL_AUDIT | \
SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
SYSCALL_WORK_SYSCALL_EXIT_TRAP | \
ARCH_SYSCALL_WORK_EXIT) ARCH_SYSCALL_WORK_EXIT)
/* /*
......
...@@ -43,6 +43,7 @@ enum syscall_work_bit { ...@@ -43,6 +43,7 @@ enum syscall_work_bit {
SYSCALL_WORK_BIT_SYSCALL_EMU, SYSCALL_WORK_BIT_SYSCALL_EMU,
SYSCALL_WORK_BIT_SYSCALL_AUDIT, SYSCALL_WORK_BIT_SYSCALL_AUDIT,
SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH, SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
}; };
#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP) #define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
...@@ -51,6 +52,7 @@ enum syscall_work_bit { ...@@ -51,6 +52,7 @@ enum syscall_work_bit {
#define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU) #define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)
#define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT) #define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT)
#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH) #define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
#define SYSCALL_WORK_SYSCALL_EXIT_TRAP BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
#endif #endif
#include <asm/thread_info.h> #include <asm/thread_info.h>
......
...@@ -209,15 +209,9 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs) ...@@ -209,15 +209,9 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)
lockdep_sys_exit(); lockdep_sys_exit();
} }
#ifndef _TIF_SINGLESTEP
static inline bool report_single_step(unsigned long work)
{
return false;
}
#else
/* /*
* If SYSCALL_EMU is set, then the only reason to report is when * If SYSCALL_EMU is set, then the only reason to report is when
* TIF_SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall * SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
* instruction has been already reported in syscall_enter_from_user_mode(). * instruction has been already reported in syscall_enter_from_user_mode().
*/ */
static inline bool report_single_step(unsigned long work) static inline bool report_single_step(unsigned long work)
...@@ -225,10 +219,8 @@ static inline bool report_single_step(unsigned long work) ...@@ -225,10 +219,8 @@ static inline bool report_single_step(unsigned long work)
if (work & SYSCALL_WORK_SYSCALL_EMU) if (work & SYSCALL_WORK_SYSCALL_EMU)
return false; return false;
return !!(current_thread_info()->flags & _TIF_SINGLESTEP); return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
} }
#endif
static void syscall_exit_work(struct pt_regs *regs, unsigned long work) static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment