diff mbox series

[v3,1/4] arm64: kprobes: Recover pstate.D in single-step exception handler

Message ID 156404255444.2020.3301023170351823334.stgit@devnote2 (mailing list archive)
State New, archived
Headers show
Series arm64: kprobes: Fix some bugs in arm64 kprobes | expand

Commit Message

Masami Hiramatsu (Google) July 25, 2019, 8:15 a.m. UTC
kprobes manipulates the interrupted PSTATE for single step, and
doesn't restore it. Thus, if we put a kprobe where the pstate.D
(debug) masked, the mask will be cleared after the kprobe hits.

Moreover, in the most complicated case, this can lead a kernel
crash with below message when a nested kprobe hits.

[  152.118921] Unexpected kernel single-step exception at EL1

When the 1st kprobe hits, do_debug_exception() will be called.
At this point, debug exception (= pstate.D) must be masked (=1).
But if another kprobes hits before single-step of the first kprobe
(e.g. inside user pre_handler), it unmask the debug exception
(pstate.D = 0) and return.
Then, when the 1st kprobe setting up single-step, it saves current
DAIF, mask DAIF, enable single-step, and restore DAIF.
However, since "D" flag in DAIF is cleared by the 2nd kprobe, the
single-step exception happens soon after restoring DAIF.

This has been introduced by commit 7419333fa15e ("arm64: kprobe:
Always clear pstate.D in breakpoint exception handler")

To solve this issue, this stores all DAIF bits and restore it
after single stepping.

Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
Fixes: commit 7419333fa15e ("arm64: kprobe: Always clear pstate.D in breakpoint exception handler")
Reviewed-by: James Morse <james.morse@arm.com>
Tested-by: James Morse <james.morse@arm.com>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
  Changes in v3:
   - Update patch description
   - move PSR_DAIF_MASK in daifflags.h
  Changes in v2:
   - Save and restore all DAIF flags.
   - Operate pstate directly and remove spsr_set_debug_flag().
---
 arch/arm64/include/asm/daifflags.h |    2 ++
 arch/arm64/kernel/probes/kprobes.c |   39 +++++-------------------------------
 2 files changed, 7 insertions(+), 34 deletions(-)

Comments

Will Deacon Aug. 1, 2019, 12:16 p.m. UTC | #1
On Thu, Jul 25, 2019 at 05:15:54PM +0900, Masami Hiramatsu wrote:
> kprobes manipulates the interrupted PSTATE for single step, and
> doesn't restore it. Thus, if we put a kprobe where the pstate.D
> (debug) masked, the mask will be cleared after the kprobe hits.
> 
> Moreover, in the most complicated case, this can lead a kernel
> crash with below message when a nested kprobe hits.
> 
> [  152.118921] Unexpected kernel single-step exception at EL1
> 
> When the 1st kprobe hits, do_debug_exception() will be called.
> At this point, debug exception (= pstate.D) must be masked (=1).
> But if another kprobes hits before single-step of the first kprobe
> (e.g. inside user pre_handler), it unmask the debug exception
> (pstate.D = 0) and return.
> Then, when the 1st kprobe setting up single-step, it saves current
> DAIF, mask DAIF, enable single-step, and restore DAIF.
> However, since "D" flag in DAIF is cleared by the 2nd kprobe, the
> single-step exception happens soon after restoring DAIF.
> 
> This has been introduced by commit 7419333fa15e ("arm64: kprobe:
> Always clear pstate.D in breakpoint exception handler")
> 
> To solve this issue, this stores all DAIF bits and restore it
> after single stepping.
> 
> Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
> Fixes: commit 7419333fa15e ("arm64: kprobe: Always clear pstate.D in breakpoint exception handler")
> Reviewed-by: James Morse <james.morse@arm.com>
> Tested-by: James Morse <james.morse@arm.com>
> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> ---
>   Changes in v3:
>    - Update patch description
>    - move PSR_DAIF_MASK in daifflags.h
>   Changes in v2:
>    - Save and restore all DAIF flags.
>    - Operate pstate directly and remove spsr_set_debug_flag().
> ---
>  arch/arm64/include/asm/daifflags.h |    2 ++
>  arch/arm64/kernel/probes/kprobes.c |   39 +++++-------------------------------
>  2 files changed, 7 insertions(+), 34 deletions(-)

I'm seeing an allmodconfig build failure with this:

arch/arm64/kernel/probes/kprobes.c: In function ‘kprobes_save_local_irqflag’:
arch/arm64/kernel/probes/kprobes.c:181:38: error: ‘DAIF_MASK’ undeclared (first use in this function); did you mean ‘BIT_MASK’?
  kcb->saved_irqflag = regs->pstate & DAIF_MASK;
                                      ^~~~~~~~~
                                      BIT_MASK
arch/arm64/kernel/probes/kprobes.c:181:38: note: each undeclared identifier is reported only once for each function it appears in
arch/arm64/kernel/probes/kprobes.c: In function ‘kprobes_restore_local_irqflag’:
arch/arm64/kernel/probes/kprobes.c:190:19: error: ‘DAIF_MASK’ undeclared (first use in this function); did you mean ‘BIT_MASK’?
  regs->pstate &= ~DAIF_MASK;
                   ^~~~~~~~~
                   BIT_MASK
make[2]: *** [scripts/Makefile.build:274: arch/arm64/kernel/probes/kprobes.o] Error 1

Will
Masami Hiramatsu (Google) Aug. 1, 2019, 2:08 p.m. UTC | #2
On Thu, 1 Aug 2019 13:16:22 +0100
Will Deacon <will@kernel.org> wrote:

> On Thu, Jul 25, 2019 at 05:15:54PM +0900, Masami Hiramatsu wrote:
> > kprobes manipulates the interrupted PSTATE for single step, and
> > doesn't restore it. Thus, if we put a kprobe where the pstate.D
> > (debug) masked, the mask will be cleared after the kprobe hits.
> > 
> > Moreover, in the most complicated case, this can lead a kernel
> > crash with below message when a nested kprobe hits.
> > 
> > [  152.118921] Unexpected kernel single-step exception at EL1
> > 
> > When the 1st kprobe hits, do_debug_exception() will be called.
> > At this point, debug exception (= pstate.D) must be masked (=1).
> > But if another kprobes hits before single-step of the first kprobe
> > (e.g. inside user pre_handler), it unmask the debug exception
> > (pstate.D = 0) and return.
> > Then, when the 1st kprobe setting up single-step, it saves current
> > DAIF, mask DAIF, enable single-step, and restore DAIF.
> > However, since "D" flag in DAIF is cleared by the 2nd kprobe, the
> > single-step exception happens soon after restoring DAIF.
> > 
> > This has been introduced by commit 7419333fa15e ("arm64: kprobe:
> > Always clear pstate.D in breakpoint exception handler")
> > 
> > To solve this issue, this stores all DAIF bits and restore it
> > after single stepping.
> > 
> > Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
> > Fixes: commit 7419333fa15e ("arm64: kprobe: Always clear pstate.D in breakpoint exception handler")
> > Reviewed-by: James Morse <james.morse@arm.com>
> > Tested-by: James Morse <james.morse@arm.com>
> > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > ---
> >   Changes in v3:
> >    - Update patch description
> >    - move PSR_DAIF_MASK in daifflags.h
> >   Changes in v2:
> >    - Save and restore all DAIF flags.
> >    - Operate pstate directly and remove spsr_set_debug_flag().
> > ---
> >  arch/arm64/include/asm/daifflags.h |    2 ++
> >  arch/arm64/kernel/probes/kprobes.c |   39 +++++-------------------------------
> >  2 files changed, 7 insertions(+), 34 deletions(-)
> 
> I'm seeing an allmodconfig build failure with this:
> 
> arch/arm64/kernel/probes/kprobes.c: In function ‘kprobes_save_local_irqflag’:
> arch/arm64/kernel/probes/kprobes.c:181:38: error: ‘DAIF_MASK’ undeclared (first use in this function); did you mean ‘BIT_MASK’?
>   kcb->saved_irqflag = regs->pstate & DAIF_MASK;
>                                       ^~~~~~~~~
>                                       BIT_MASK
> arch/arm64/kernel/probes/kprobes.c:181:38: note: each undeclared identifier is reported only once for each function it appears in
> arch/arm64/kernel/probes/kprobes.c: In function ‘kprobes_restore_local_irqflag’:
> arch/arm64/kernel/probes/kprobes.c:190:19: error: ‘DAIF_MASK’ undeclared (first use in this function); did you mean ‘BIT_MASK’?
>   regs->pstate &= ~DAIF_MASK;
>                    ^~~~~~~~~
>                    BIT_MASK
> make[2]: *** [scripts/Makefile.build:274: arch/arm64/kernel/probes/kprobes.o] Error 1

Oops, daifflags.h is included via kvm_host.h...
OK, kprobes.c must include daifflags.h directly in this case.

I'll update this patch too.

Thank you,

> 
> Will
diff mbox series

Patch

diff --git a/arch/arm64/include/asm/daifflags.h b/arch/arm64/include/asm/daifflags.h
index 987926ed535e..063c964af705 100644
--- a/arch/arm64/include/asm/daifflags.h
+++ b/arch/arm64/include/asm/daifflags.h
@@ -13,6 +13,8 @@ 
 #define DAIF_PROCCTX		0
 #define DAIF_PROCCTX_NOIRQ	PSR_I_BIT
 #define DAIF_ERRCTX		(PSR_I_BIT | PSR_A_BIT)
+#define DAIF_MASK		(PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
+
 
 /* mask/save/unmask/restore all exceptions, including interrupts. */
 static inline void local_daif_mask(void)
diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
index bd5dfffca272..bf2259651b67 100644
--- a/arch/arm64/kernel/probes/kprobes.c
+++ b/arch/arm64/kernel/probes/kprobes.c
@@ -167,33 +167,6 @@  static void __kprobes set_current_kprobe(struct kprobe *p)
 	__this_cpu_write(current_kprobe, p);
 }
 
-/*
- * When PSTATE.D is set (masked), then software step exceptions can not be
- * generated.
- * SPSR's D bit shows the value of PSTATE.D immediately before the
- * exception was taken. PSTATE.D is set while entering into any exception
- * mode, however software clears it for any normal (none-debug-exception)
- * mode in the exception entry. Therefore, when we are entering into kprobe
- * breakpoint handler from any normal mode then SPSR.D bit is already
- * cleared, however it is set when we are entering from any debug exception
- * mode.
- * Since we always need to generate single step exception after a kprobe
- * breakpoint exception therefore we need to clear it unconditionally, when
- * we become sure that the current breakpoint exception is for kprobe.
- */
-static void __kprobes
-spsr_set_debug_flag(struct pt_regs *regs, int mask)
-{
-	unsigned long spsr = regs->pstate;
-
-	if (mask)
-		spsr |= PSR_D_BIT;
-	else
-		spsr &= ~PSR_D_BIT;
-
-	regs->pstate = spsr;
-}
-
 /*
  * Interrupts need to be disabled before single-step mode is set, and not
  * reenabled until after single-step mode ends.
@@ -205,17 +178,17 @@  spsr_set_debug_flag(struct pt_regs *regs, int mask)
 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
 						struct pt_regs *regs)
 {
-	kcb->saved_irqflag = regs->pstate;
+	kcb->saved_irqflag = regs->pstate & DAIF_MASK;
 	regs->pstate |= PSR_I_BIT;
+	/* Unmask PSTATE.D for enabling software step exceptions. */
+	regs->pstate &= ~PSR_D_BIT;
 }
 
 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
 						struct pt_regs *regs)
 {
-	if (kcb->saved_irqflag & PSR_I_BIT)
-		regs->pstate |= PSR_I_BIT;
-	else
-		regs->pstate &= ~PSR_I_BIT;
+	regs->pstate &= ~DAIF_MASK;
+	regs->pstate |= kcb->saved_irqflag;
 }
 
 static void __kprobes
@@ -252,8 +225,6 @@  static void __kprobes setup_singlestep(struct kprobe *p,
 
 		set_ss_context(kcb, slot);	/* mark pending ss */
 
-		spsr_set_debug_flag(regs, 0);
-
 		/* IRQs and single stepping do not mix well. */
 		kprobes_save_local_irqflag(kcb, regs);
 		kernel_enable_single_step(regs);