arm64: kprobes: Recover pstate.D in single-step exception handler
authorMasami Hiramatsu <mhiramat@kernel.org>
Thu, 1 Aug 2019 14:25:49 +0000 (23:25 +0900)
committerWill Deacon <will@kernel.org>
Fri, 2 Aug 2019 10:55:50 +0000 (11:55 +0100)
commitb3980e48528c4d2a9e70b145a5bba328b73a0f93
treec3a7b606de4fa2f00e48c1a3c36606287523c0ea
parent7732d20a160c76006c7fe7bca5178aea6af1d2e8
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 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: 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>
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/include/asm/daifflags.h
arch/arm64/kernel/probes/kprobes.c