Skip to content

Commit b3980e4

Browse files
mhiramatwilldeacon
authored andcommitted
arm64: kprobes: Recover pstate.D in single-step exception handler
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 7419333 ("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 <[email protected]> Fixes: 7419333 ("arm64: kprobe: Always clear pstate.D in breakpoint exception handler") Reviewed-by: James Morse <[email protected]> Tested-by: James Morse <[email protected]> Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 7732d20 commit b3980e4

File tree

2 files changed

+8
-34
lines changed

2 files changed

+8
-34
lines changed

arch/arm64/include/asm/daifflags.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define DAIF_PROCCTX 0
1414
#define DAIF_PROCCTX_NOIRQ PSR_I_BIT
1515
#define DAIF_ERRCTX (PSR_I_BIT | PSR_A_BIT)
16+
#define DAIF_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
17+
1618

1719
/* mask/save/unmask/restore all exceptions, including interrupts. */
1820
static inline void local_daif_mask(void)

arch/arm64/kernel/probes/kprobes.c

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <asm/ptrace.h>
2222
#include <asm/cacheflush.h>
2323
#include <asm/debug-monitors.h>
24+
#include <asm/daifflags.h>
2425
#include <asm/system_misc.h>
2526
#include <asm/insn.h>
2627
#include <linux/uaccess.h>
@@ -167,33 +168,6 @@ static void __kprobes set_current_kprobe(struct kprobe *p)
167168
__this_cpu_write(current_kprobe, p);
168169
}
169170

170-
/*
171-
* When PSTATE.D is set (masked), then software step exceptions can not be
172-
* generated.
173-
* SPSR's D bit shows the value of PSTATE.D immediately before the
174-
* exception was taken. PSTATE.D is set while entering into any exception
175-
* mode, however software clears it for any normal (none-debug-exception)
176-
* mode in the exception entry. Therefore, when we are entering into kprobe
177-
* breakpoint handler from any normal mode then SPSR.D bit is already
178-
* cleared, however it is set when we are entering from any debug exception
179-
* mode.
180-
* Since we always need to generate single step exception after a kprobe
181-
* breakpoint exception therefore we need to clear it unconditionally, when
182-
* we become sure that the current breakpoint exception is for kprobe.
183-
*/
184-
static void __kprobes
185-
spsr_set_debug_flag(struct pt_regs *regs, int mask)
186-
{
187-
unsigned long spsr = regs->pstate;
188-
189-
if (mask)
190-
spsr |= PSR_D_BIT;
191-
else
192-
spsr &= ~PSR_D_BIT;
193-
194-
regs->pstate = spsr;
195-
}
196-
197171
/*
198172
* Interrupts need to be disabled before single-step mode is set, and not
199173
* reenabled until after single-step mode ends.
@@ -205,17 +179,17 @@ spsr_set_debug_flag(struct pt_regs *regs, int mask)
205179
static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
206180
struct pt_regs *regs)
207181
{
208-
kcb->saved_irqflag = regs->pstate;
182+
kcb->saved_irqflag = regs->pstate & DAIF_MASK;
209183
regs->pstate |= PSR_I_BIT;
184+
/* Unmask PSTATE.D for enabling software step exceptions. */
185+
regs->pstate &= ~PSR_D_BIT;
210186
}
211187

212188
static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
213189
struct pt_regs *regs)
214190
{
215-
if (kcb->saved_irqflag & PSR_I_BIT)
216-
regs->pstate |= PSR_I_BIT;
217-
else
218-
regs->pstate &= ~PSR_I_BIT;
191+
regs->pstate &= ~DAIF_MASK;
192+
regs->pstate |= kcb->saved_irqflag;
219193
}
220194

221195
static void __kprobes
@@ -252,8 +226,6 @@ static void __kprobes setup_singlestep(struct kprobe *p,
252226

253227
set_ss_context(kcb, slot); /* mark pending ss */
254228

255-
spsr_set_debug_flag(regs, 0);
256-
257229
/* IRQs and single stepping do not mix well. */
258230
kprobes_save_local_irqflag(kcb, regs);
259231
kernel_enable_single_step(regs);

0 commit comments

Comments
 (0)