Skip to content

Commit 2537512

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: kretprobes: acquire the regs via a BRK exception
On arm64, kprobes always take an exception and so create a struct pt_regs through the usual exception entry logic. Similarly kretprobes taskes and exception for function entry, but for function returns it uses a trampoline which attempts to create a struct pt_regs without taking an exception. This is problematic for a few reasons, including: 1) The kretprobes trampoline neither saves nor restores all of the portions of PSTATE. Before invoking the handler it saves a number of portions of PSTATE, and after returning from the handler it restores NZCV before returning to the original return address provided by the handler. 2) The kretprobe trampoline constructs the PSTATE value piecemeal from special purpose registers as it cannot read all of PSTATE atomically without taking an exception. This is somewhat fragile, and it's not possible to reliably recover PSTATE information which only exists on some physical CPUs (e.g. when SSBS support is mismatched). Today the kretprobes trampoline does not record: - BTYPE - SSBS - ALLINT - SS - PAN - UAO - DIT - TCO ... and this will only get worse with future architecture extensions which add more PSTATE bits. 3) The kretprobes trampoline doesn't store portions of struct pt_regs (e.g. the PMR value when using pseudo-NMIs). Due to this, helpers which operate on a struct pt_regs, such as interrupts_enabled(), may not work correctly. 4) The function entry and function exit handlers run in different contexts. The entry handler will always be run in a debug exception context (which is currently treated as an NMI), but the return will be treated as whatever context the instrumented function was executed in. The differences between these contexts are liable to cause problems (e.g. as the two can be differently interruptible or preemptible, adversely affecting synchronization between the handlers). 5) As the kretprobes trampoline runs in the same context as the code being probed, it is subject to the same single-stepping context, which may not be desirable if this is being driven by the kprobes handlers. Overall, this is fragile, painful to maintain, and gets in the way of supporting other things (e.g. RELIABLE_STACKTRACE, FEAT_NMI). This patch addresses these issues by replacing the kretprobes trampoline with a `BRK` instruction, and using an exception boundary to acquire and restore the regs, in the same way as the regular kprobes trampoline. Ive tested this atop v6.8-rc3: | KTAP version 1 | 1..1 | KTAP version 1 | # Subtest: kprobes_test | # module: test_kprobes | 1..7 | ok 1 test_kprobe | ok 2 test_kprobes | ok 3 test_kprobe_missed | ok 4 test_kretprobe | ok 5 test_kretprobes | ok 6 test_stacktrace_on_kretprobe | ok 7 test_stacktrace_on_nested_kretprobe | # kprobes_test: pass:7 fail:0 skip:0 total:7 | # Totals: pass:7 fail:0 skip:0 total:7 | ok 1 kprobes_test Signed-off-by: Mark Rutland <[email protected]> Cc: Will Deacon <[email protected]> Cc: Florent Revest <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Steven Rostedt <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent d044d6b commit 2537512

File tree

3 files changed

+24
-77
lines changed

3 files changed

+24
-77
lines changed

arch/arm64/include/asm/brk-imm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* 0x004: for installing kprobes
1212
* 0x005: for installing uprobes
1313
* 0x006: for kprobe software single-step
14+
* 0x007: for kretprobe return
1415
* Allowed values for kgdb are 0x400 - 0x7ff
1516
* 0x100: for triggering a fault on purpose (reserved)
1617
* 0x400: for dynamic BRK instruction
@@ -23,6 +24,7 @@
2324
#define KPROBES_BRK_IMM 0x004
2425
#define UPROBES_BRK_IMM 0x005
2526
#define KPROBES_BRK_SS_IMM 0x006
27+
#define KRETPROBES_BRK_IMM 0x007
2628
#define FAULT_BRK_IMM 0x100
2729
#define KGDB_DYN_DBG_BRK_IMM 0x400
2830
#define KGDB_COMPILED_DBG_BRK_IMM 0x401

arch/arm64/kernel/probes/kprobes.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,21 @@ static struct break_hook kprobes_break_ss_hook = {
371371
.fn = kprobe_breakpoint_ss_handler,
372372
};
373373

374+
static int __kprobes
375+
kretprobe_breakpoint_handler(struct pt_regs *regs, unsigned long esr)
376+
{
377+
if (regs->pc != (unsigned long)__kretprobe_trampoline)
378+
return DBG_HOOK_ERROR;
379+
380+
regs->pc = kretprobe_trampoline_handler(regs, (void *)regs->regs[29]);
381+
return DBG_HOOK_HANDLED;
382+
}
383+
384+
static struct break_hook kretprobes_break_hook = {
385+
.imm = KRETPROBES_BRK_IMM,
386+
.fn = kretprobe_breakpoint_handler,
387+
};
388+
374389
/*
375390
* Provide a blacklist of symbols identifying ranges which cannot be kprobed.
376391
* This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
@@ -396,11 +411,6 @@ int __init arch_populate_kprobe_blacklist(void)
396411
return ret;
397412
}
398413

399-
void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
400-
{
401-
return (void *)kretprobe_trampoline_handler(regs, (void *)regs->regs[29]);
402-
}
403-
404414
void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
405415
struct pt_regs *regs)
406416
{
@@ -420,6 +430,7 @@ int __init arch_init_kprobes(void)
420430
{
421431
register_kernel_break_hook(&kprobes_break_hook);
422432
register_kernel_break_hook(&kprobes_break_ss_hook);
433+
register_kernel_break_hook(&kretprobes_break_hook);
423434

424435
return 0;
425436
}

arch/arm64/kernel/probes/kprobes_trampoline.S

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,17 @@
44
*/
55

66
#include <linux/linkage.h>
7-
#include <asm/asm-offsets.h>
7+
#include <asm/asm-bug.h>
88
#include <asm/assembler.h>
99

1010
.text
1111

12-
.macro save_all_base_regs
13-
stp x0, x1, [sp, #S_X0]
14-
stp x2, x3, [sp, #S_X2]
15-
stp x4, x5, [sp, #S_X4]
16-
stp x6, x7, [sp, #S_X6]
17-
stp x8, x9, [sp, #S_X8]
18-
stp x10, x11, [sp, #S_X10]
19-
stp x12, x13, [sp, #S_X12]
20-
stp x14, x15, [sp, #S_X14]
21-
stp x16, x17, [sp, #S_X16]
22-
stp x18, x19, [sp, #S_X18]
23-
stp x20, x21, [sp, #S_X20]
24-
stp x22, x23, [sp, #S_X22]
25-
stp x24, x25, [sp, #S_X24]
26-
stp x26, x27, [sp, #S_X26]
27-
stp x28, x29, [sp, #S_X28]
28-
add x0, sp, #PT_REGS_SIZE
29-
stp lr, x0, [sp, #S_LR]
30-
/*
31-
* Construct a useful saved PSTATE
32-
*/
33-
mrs x0, nzcv
34-
mrs x1, daif
35-
orr x0, x0, x1
36-
mrs x1, CurrentEL
37-
orr x0, x0, x1
38-
mrs x1, SPSel
39-
orr x0, x0, x1
40-
stp xzr, x0, [sp, #S_PC]
41-
.endm
42-
43-
.macro restore_all_base_regs
44-
ldr x0, [sp, #S_PSTATE]
45-
and x0, x0, #(PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT)
46-
msr nzcv, x0
47-
ldp x0, x1, [sp, #S_X0]
48-
ldp x2, x3, [sp, #S_X2]
49-
ldp x4, x5, [sp, #S_X4]
50-
ldp x6, x7, [sp, #S_X6]
51-
ldp x8, x9, [sp, #S_X8]
52-
ldp x10, x11, [sp, #S_X10]
53-
ldp x12, x13, [sp, #S_X12]
54-
ldp x14, x15, [sp, #S_X14]
55-
ldp x16, x17, [sp, #S_X16]
56-
ldp x18, x19, [sp, #S_X18]
57-
ldp x20, x21, [sp, #S_X20]
58-
ldp x22, x23, [sp, #S_X22]
59-
ldp x24, x25, [sp, #S_X24]
60-
ldp x26, x27, [sp, #S_X26]
61-
ldp x28, x29, [sp, #S_X28]
62-
.endm
63-
6412
SYM_CODE_START(__kretprobe_trampoline)
65-
sub sp, sp, #PT_REGS_SIZE
66-
67-
save_all_base_regs
68-
69-
/* Setup a frame pointer. */
70-
add x29, sp, #S_FP
71-
72-
mov x0, sp
73-
bl trampoline_probe_handler
7413
/*
75-
* Replace trampoline address in lr with actual orig_ret_addr return
76-
* address.
14+
* Trigger a breakpoint exception. The PC will be adjusted by
15+
* kretprobe_breakpoint_handler(), and no subsequent instructions will
16+
* be executed from the trampoline.
7717
*/
78-
mov lr, x0
79-
80-
/* The frame pointer (x29) is restored with other registers. */
81-
restore_all_base_regs
82-
83-
add sp, sp, #PT_REGS_SIZE
84-
ret
85-
18+
brk #KRETPROBES_BRK_IMM
19+
ASM_BUG()
8620
SYM_CODE_END(__kretprobe_trampoline)

0 commit comments

Comments
 (0)