2020-10-27 14:26:07

by Peter Zijlstra

[permalink] [raw]
Subject: [PATCH 3/3] x86/debug: Fix PTRACE_{BLOCK,SINGLE}STEP vs ptrace_get_debugreg(6)

Commit d53d9bc0cf78 ("x86/debug: Change thread.debugreg6 to
thread.virtual_dr6") changed the semantics of the variable from random
collection of bits, to exactly only those bits that ptrace() needs.

Unfortunately we lost DR_STEP for PTRACE_{BLOCK,SINGLE}STEP.

Fixes: d53d9bc0cf78 ("x86/debug: Change thread.debugreg6 to thread.virtual_dr6")
Reported-by: Kyle Huey <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
---
arch/x86/include/asm/ptrace.h | 2 ++
arch/x86/kernel/step.c | 9 +++++++++
arch/x86/kernel/traps.c | 2 +-
3 files changed, 12 insertions(+), 1 deletion(-)

--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -355,6 +355,8 @@ static inline unsigned long regs_get_ker
#define arch_has_block_step() (boot_cpu_data.x86 >= 6)
#endif

+extern unsigned long user_dr_step(unsigned long dr6);
+
#define ARCH_HAS_USER_SINGLE_STEP_REPORT

struct user_desc;
--- a/arch/x86/kernel/step.c
+++ b/arch/x86/kernel/step.c
@@ -235,3 +235,12 @@ void user_disable_single_step(struct tas
if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
}
+
+unsigned long user_dr_step(unsigned long dr6)
+{
+ if (test_thread_flag(TIF_BLOCKSTEP) ||
+ test_thread_flag(TIF_SINGLESTEP))
+ return dr6 & DR_STEP;
+
+ return 0;
+}
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -940,7 +940,7 @@ static __always_inline void exc_debug_us
* Clear the virtual DR6 value, ptrace() routines will set bits here
* for things it wants signals for.
*/
- current->thread.virtual_dr6 = 0;
+ current->thread.virtual_dr6 = user_dr_step(dr6);

/*
* The SDM says "The processor clears the BTF flag when it



2020-10-27 17:53:47

by Kyle Huey

[permalink] [raw]
Subject: Re: [PATCH 3/3] x86/debug: Fix PTRACE_{BLOCK,SINGLE}STEP vs ptrace_get_debugreg(6)

On Tue, Oct 27, 2020 at 2:44 AM Peter Zijlstra <[email protected]> wrote:
>
> Commit d53d9bc0cf78 ("x86/debug: Change thread.debugreg6 to
> thread.virtual_dr6") changed the semantics of the variable from random
> collection of bits, to exactly only those bits that ptrace() needs.
>
> Unfortunately we lost DR_STEP for PTRACE_{BLOCK,SINGLE}STEP.
>
> Fixes: d53d9bc0cf78 ("x86/debug: Change thread.debugreg6 to thread.virtual_dr6")
> Reported-by: Kyle Huey <[email protected]>
> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> ---
> arch/x86/include/asm/ptrace.h | 2 ++
> arch/x86/kernel/step.c | 9 +++++++++
> arch/x86/kernel/traps.c | 2 +-
> 3 files changed, 12 insertions(+), 1 deletion(-)
>
> --- a/arch/x86/include/asm/ptrace.h
> +++ b/arch/x86/include/asm/ptrace.h
> @@ -355,6 +355,8 @@ static inline unsigned long regs_get_ker
> #define arch_has_block_step() (boot_cpu_data.x86 >= 6)
> #endif
>
> +extern unsigned long user_dr_step(unsigned long dr6);
> +
> #define ARCH_HAS_USER_SINGLE_STEP_REPORT
>
> struct user_desc;
> --- a/arch/x86/kernel/step.c
> +++ b/arch/x86/kernel/step.c
> @@ -235,3 +235,12 @@ void user_disable_single_step(struct tas
> if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
> task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
> }
> +
> +unsigned long user_dr_step(unsigned long dr6)
> +{
> + if (test_thread_flag(TIF_BLOCKSTEP) ||
> + test_thread_flag(TIF_SINGLESTEP))
> + return dr6 & DR_STEP;
> +
> + return 0;
> +}
> --- a/arch/x86/kernel/traps.c
> +++ b/arch/x86/kernel/traps.c
> @@ -940,7 +940,7 @@ static __always_inline void exc_debug_us
> * Clear the virtual DR6 value, ptrace() routines will set bits here
> * for things it wants signals for.
> */
> - current->thread.virtual_dr6 = 0;
> + current->thread.virtual_dr6 = user_dr_step(dr6);
>
> /*
> * The SDM says "The processor clears the BTF flag when it
>
>

Tested-by: Kyle Huey <[email protected]>

Confirmed that this patch series fixes rr when applied on top of the
v5.10-rc1 tag.

- Kyle