Skip to content

Commit e38e847

Browse files
mhiramatSasha Levin
authored andcommitted
arm64: kprobes: Recover pstate.D in single-step exception handler
[ Upstream commit b3980e4 ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 49d9e6c commit e38e847

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>
@@ -165,33 +166,6 @@ static void __kprobes set_current_kprobe(struct kprobe *p)
165166
__this_cpu_write(current_kprobe, p);
166167
}
167168

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

210186
static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
211187
struct pt_regs *regs)
212188
{
213-
if (kcb->saved_irqflag & PSR_I_BIT)
214-
regs->pstate |= PSR_I_BIT;
215-
else
216-
regs->pstate &= ~PSR_I_BIT;
189+
regs->pstate &= ~DAIF_MASK;
190+
regs->pstate |= kcb->saved_irqflag;
217191
}
218192

219193
static void __kprobes
@@ -250,8 +224,6 @@ static void __kprobes setup_singlestep(struct kprobe *p,
250224

251225
set_ss_context(kcb, slot); /* mark pending ss */
252226

253-
spsr_set_debug_flag(regs, 0);
254-
255227
/* IRQs and single stepping do not mix well. */
256228
kprobes_save_local_irqflag(kcb, regs);
257229
kernel_enable_single_step(regs);

0 commit comments

Comments
 (0)