Skip to content

Commit 6342adc

Browse files
krisman-at-collaboraKAGA-KOKO
authored andcommitted
entry: Ensure trap after single-step on system call return
Commit 2991552 ("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: 2991552 ("entry: Drop usage of TIF flags in the generic syscall code") Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Kyle Huey <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1048ba8 commit 6342adc

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

arch/x86/include/asm/entry-common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ static __always_inline void arch_check_user_regs(struct pt_regs *regs)
4343
}
4444
#define arch_check_user_regs arch_check_user_regs
4545

46-
#define ARCH_SYSCALL_EXIT_WORK (_TIF_SINGLESTEP)
47-
4846
static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
4947
unsigned long ti_work)
5048
{

arch/x86/kernel/step.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ static int enable_single_step(struct task_struct *child)
127127
regs->flags |= X86_EFLAGS_TF;
128128

129129
/*
130-
* Always set TIF_SINGLESTEP - this guarantees that
131-
* we single-step system calls etc.. This will also
130+
* Always set TIF_SINGLESTEP. This will also
132131
* cause us to set TF when returning to user mode.
133132
*/
134133
set_tsk_thread_flag(child, TIF_SINGLESTEP);
135134

135+
/*
136+
* Ensure that a trap is triggered once stepping out of a system
137+
* call prior to executing any user instruction.
138+
*/
139+
set_task_syscall_work(child, SYSCALL_EXIT_TRAP);
140+
136141
oflags = regs->flags;
137142

138143
/* Set TF on the kernel stack.. */
@@ -230,6 +235,7 @@ void user_disable_single_step(struct task_struct *child)
230235

231236
/* Always clear TIF_SINGLESTEP... */
232237
clear_tsk_thread_flag(child, TIF_SINGLESTEP);
238+
clear_task_syscall_work(child, SYSCALL_EXIT_TRAP);
233239

234240
/* But touch TF only if it was set by us.. */
235241
if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))

include/linux/entry-common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
SYSCALL_WORK_SYSCALL_TRACE | \
4747
SYSCALL_WORK_SYSCALL_AUDIT | \
4848
SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
49+
SYSCALL_WORK_SYSCALL_EXIT_TRAP | \
4950
ARCH_SYSCALL_WORK_EXIT)
5051

5152
/*

include/linux/thread_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum syscall_work_bit {
4343
SYSCALL_WORK_BIT_SYSCALL_EMU,
4444
SYSCALL_WORK_BIT_SYSCALL_AUDIT,
4545
SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
46+
SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
4647
};
4748

4849
#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -51,6 +52,7 @@ enum syscall_work_bit {
5152
#define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)
5253
#define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT)
5354
#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
55+
#define SYSCALL_WORK_SYSCALL_EXIT_TRAP BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
5456
#endif
5557

5658
#include <asm/thread_info.h>

kernel/entry/common.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,18 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)
209209
lockdep_sys_exit();
210210
}
211211

212-
#ifndef _TIF_SINGLESTEP
213-
static inline bool report_single_step(unsigned long work)
214-
{
215-
return false;
216-
}
217-
#else
218212
/*
219213
* If SYSCALL_EMU is set, then the only reason to report is when
220-
* TIF_SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
214+
* SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
221215
* instruction has been already reported in syscall_enter_from_user_mode().
222216
*/
223217
static inline bool report_single_step(unsigned long work)
224218
{
225219
if (work & SYSCALL_WORK_SYSCALL_EMU)
226220
return false;
227221

228-
return !!(current_thread_info()->flags & _TIF_SINGLESTEP);
222+
return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
229223
}
230-
#endif
231-
232224

233225
static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
234226
{

0 commit comments

Comments
 (0)