kprobes: Make KUnit and add stacktrace on kretprobe tests
Subsystems (5)
Masami Hiramatsu
maintainercover kprobes: Make KUnit and add stacktrace on kretprobe tests
Hi,
Here is the series to change the kprobes selftest to KUnit and add testcases for stacktrace on kretprobes, which has been fixed recently on x86.
KUnit conversion work has been done by Sven. (Thanks!)
I found that if there are testcases for the stacktrace on kretprobes we can make sure that the stacktrace can work or not while booting. So I added 2 additinal Kunit test cases for stacktrace on kretprobe. One is for a simple stacktrace on kretprobe, the other is for the stacktrace with kretprobes on nested functions.
But actually, this is currently only for the x86. Thus I also added some fixes to enable stacktrace on kretprobe on arm and arm64.
Note that for the arm, this series only supports framepointer based stacktrace. To support ARM unwinder, will need more work.
Thank you,
---
Masami Hiramatsu (7):
kprobes: Add a test case for stacktrace from kretprobe handler
arm64: kprobes: Record frame pointer with kretprobe instance
arm64: kprobes: Make a frame pointer on __kretprobe_trampoline
arm64: Recover kretprobe modified return address in stacktrace
ARM: clang: Do not relay on lr register for stacktrace
ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
ARM: Recover kretprobe modified return address in stacktrace
Sven Schnelle (1):
kprobes: convert tests to kunit
arch/Kconfig | 8 + arch/arm/Kconfig | 1 arch/arm/include/asm/stacktrace.h | 5 arch/arm/kernel/return_address.c | 2 arch/arm/kernel/stacktrace.c | 11 + arch/arm/probes/kprobes/core.c | 29 ++ arch/arm64/Kconfig | 1 arch/arm64/include/asm/stacktrace.h | 2 arch/arm64/kernel/probes/kprobes.c | 4 arch/arm64/kernel/probes/kprobes_trampoline.S | 4 arch/arm64/kernel/stacktrace.c | 3 arch/x86/Kconfig | 1 kernel/kprobes.c | 3 kernel/test_kprobes.c | 374 ++++++++++++++----------- lib/Kconfig.debug | 3 15 files changed, 278 insertions(+), 173 deletions(-)
--
Masami Hiramatsu (Linaro) <[email protected]>
Masami Hiramatsu
maintainerPATCH 1/8 kprobes: convert tests to kunit
From: Sven Schnelle <[email protected]>
This converts the kprobes testcases to use the kunit framework. It adds a dependency on CONFIG_KUNIT, and the output will change to TAP:
TAP version 14
1..1
# Subtest: kprobes_test
1..4
random: crng init done
ok 1 - test_kprobe
ok 2 - test_kprobes
ok 3 - test_kretprobe
ok 4 - test_kretprobes
ok 1 - kprobes_test
Note that the kprobes testcases are no longer run immediately after kprobes initialization, but as a late initcall when kunit is initialized. kprobes itself is initialized with an early initcall, so the order is still correct.
--- kernel/kprobes.c | 3 - kernel/test_kprobes.c | 222 +++++++++++++------------------------------------ lib/Kconfig.debug | 3 - 3 files changed, 61 insertions(+), 167 deletions(-)
kernel/kprobes.c +0 -3
@@ -2581,9 +2581,6 @@ static int __init init_kprobes(void) err =register_module_notifier (&kprobe_module_nb); kprobes_initialized = (err ==0 ); - -if (!err) -init_test_probes ();return err; }early_initcall (init_kprobes);
kernel/test_kprobes.c +59 -163
338 lines not on this page · raw message
lib/Kconfig.debug +2 -1
@@ -2080,9 +2080,10 @@ config TEST_DIV64 If unsure, say N. config KPROBES_SANITY_TEST - bool "Kprobes sanity tests" + tristate "Kprobes sanity tests" depends on DEBUG_KERNEL depends on KPROBES + depends on KUNIT help This option provides for testing basic kprobes functionality on boot. Samples of kprobe and kretprobe are inserted and
Masami Hiramatsu
maintainerPATCH 2/8 kprobes: Add a test case for stacktrace from kretprobe handler
In reply to Masami HiramatsuAdd a test case for stacktrace from kretprobe handler and nested kretprobe handlers.
This test checks both of stack trace inside kretprobe handler and stack trace from pt_regs. Those stack trace must include actual function return address instead of kretprobe trampoline. The nested kretprobe stacktrace test checks whether the unwinder can correctly unwind the call frame on the stack which has been modified by the kretprobe.
Since the stacktrace on kretprobe is correctly fixed only on x86, this introduces a meta kconfig ARCH_CORRECT_STACKTRACE_ON_KRETPROBE which tells user that the stacktrace on kretprobe is correct or not.
The test results will be shown like below;
TAP version 14
1..1
# Subtest: kprobes_test
1..6
ok 1 - test_kprobe
ok 2 - test_kprobes
ok 3 - test_kretprobe
ok 4 - test_kretprobes
ok 5 - test_stacktrace_on_kretprobe
ok 6 - test_stacktrace_on_nested_kretprobe
# kprobes_test: pass:6 fail:0 skip:0 total:6
# Totals: pass:6 fail:0 skip:0 total:6
ok 1 - kprobes_test
--- arch/Kconfig | 8 ++ arch/x86/Kconfig | 1 kernel/test_kprobes.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+)
arch/Kconfig +8 -0
@@ -191,6 +191,14 @@ config HAVE_OPTPROBES config HAVE_KPROBES_ON_FTRACE bool +config ARCH_CORRECT_STACKTRACE_ON_KRETPROBE + bool + help + Since kretprobes modifies return address on the stack, the + stacktrace may see the kretprobe trampoline address instead + of correct one. If the architecture stacktrace code and + unwinder can adjust such entries, select this configuration. + config HAVE_FUNCTION_ERROR_INJECTION bool
arch/x86/Kconfig +1 -0
@@ -61,6 +61,7 @@ config X86 select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI select ARCH_32BIT_OFF_T if X86_32 select ARCH_CLOCKSOURCE_INIT + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if X86_64 && HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG if X86_64 || (X86_32 && HIGHMEM) select ARCH_ENABLE_MEMORY_HOTREMOVE if MEMORY_HOTPLUG
kernel/test_kprobes.c +158 -0
188 lines not on this page · raw message
Masami Hiramatsu
maintainerPATCH 3/8 arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Masami HiramatsuRecord the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enable CONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).
--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kernel/probes/kprobes.c +2 -2
@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;
Masami Hiramatsu
maintainerPATCH 4/8 arm64: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuMake a frame pointer (make the x29 register points the address of pt_regs->regs[29]) on __kretprobe_trampoline.
This frame pointer will be used by the stacktracer when it is called from the kretprobe handlers. In this case, the stack tracer will unwind stack to trampoline_probe_handler() and find the next frame pointer in the stack frame of the __kretprobe_trampoline().
--- arch/arm64/kernel/probes/kprobes_trampoline.S | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kernel/probes/kprobes_trampoline.S +4 -0
@@ -66,6 +66,9 @@ SYM_CODE_START(__kretprobe_trampoline) save_all_base_regs + /* Setup a frame pointer. */ + add x29, sp, #S_FP + mov x0, sp bl trampoline_probe_handler /*
@@ -74,6 +77,7 @@ SYM_CODE_START(__kretprobe_trampoline) */ mov lr, x0 + /* The frame pointer (x29) is restored with other registers. */ restore_all_base_regs add sp, sp, #PT_REGS_SIZE
Masami Hiramatsu
maintainerPATCH 5/8 arm64: Recover kretprobe modified return address in stacktrace
In reply to Masami HiramatsuSince the kretprobe replaces the function return address with the kretprobe_trampoline on the stack, stack unwinder shows it instead of the correct return address.
This checks whether the next return address is the __kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list.
With this fix, now arm64 can enable CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 2 ++ arch/arm64/kernel/stacktrace.c | 3 +++ 3 files changed, 6 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVE
arch/arm64/include/asm/stacktrace.h +2 -0
@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> #include <linux/types.h> +#include <linux/llist.h> #include <asm/memory.h> #include <asm/ptrace.h>
@@ -59,6 +60,7 @@ struct stackframe {#ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph;#endif +struct llist_node *kr_cur; };extern int unwind_frame (struct task_struct *tsk,struct stackframe *frame);
arch/arm64/kernel/stacktrace.c +3 -0
@@ -129,6 +129,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur); frame->pc =ptrauth_strip_insn_pac (frame->pc);
@@ -224,6 +226,7 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, {struct stackframe frame; +memset (&frame,0 ,sizeof (frame));if (regs)start_backtrace (&frame, regs->regs[29 ], regs->pc);else if (task == current)
Masami Hiramatsu
maintainerPATCH 6/8 ARM: clang: Do not relay on lr register for stacktrace
In reply to Masami HiramatsuCurrently the stacktrace on clang compiled arm kernel uses the 'lr' register to find the first frame address from pt_regs. However, that is wrong after calling another function, because the 'lr' register is used by 'bl' instruction and never be recovered.
As same as gcc arm kernel, directly use the frame pointer (x11) of the pt_regs to find the first frame address.
--- arch/arm/kernel/stacktrace.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/kernel/stacktrace.c +1 -2
@@ -54,8 +54,7 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = frame->fp; frame->fp = *(unsigned long *)(fp); - frame->pc = frame->lr; - frame->lr = *(unsigned long *)(fp +4 ); + frame->pc = *(unsigned long *)(fp +4 );#else /* check current frame pointer is within bounds */ if (fp < low +12 || fp > high -4 )
Masami Hiramatsu
maintainerPATCH 7/8 ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuCurrently kretprobe on ARM just fills r0-r11 of pt_regs, but that is not enough for the stacktrace. Moreover, from the user kretprobe handler, stacktrace needs a frame pointer on the __kretprobe_trampoline.
This adds a frame pointer on __kretprobe_trampoline for both gcc and clang case. Those have different frame pointer so we need different but similar stack on pt_regs.
Gcc makes the frame pointer (fp) to point the 'pc' address of the {fp, ip (=sp), lr, pc}, this means {r11, r13, r14, r15}. Thus if we save the r11 (fp) on pt_regs->r12, we can make this set on the end of pt_regs.
On the other hand, Clang makes the frame pointer to point the 'fp' address of {fp, lr} on stack. Since the next to the pt_regs->lr is pt_regs->sp, I reused the pair of pt_regs->fp and pt_regs->ip.
So this stores the 'lr' on pt_regs->ip and make the fp to point pt_regs->fp.
For both cases, saves __kretprobe_trampoline address to pt_regs->lr, so that the stack tracer can identify this frame pointer has been made by the __kretprobe_trampoline.
Note that if the CONFIG_FRAME_POINTER is not set, this keeps fp as is.
--- arch/arm/probes/kprobes/core.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
arch/arm/probes/kprobes/core.c +24 -5
@@ -368,16 +368,35 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,/* * When a retprobed function returns, trampoline_handler() is called, * calling the kretprobe's handler. We construct a struct pt_regs to -* give a view of registers r0-r11 to the user return-handler. This is -* not a complete pt_regs structure, but that should be plenty sufficient -* for kretprobe handlers which should normally be interested in r0 only -* anyway. +* give a view of registers r0-r11, sp, lr, and pc to the user +* return-handler. This is not a complete pt_regs structure, but that +* should be enough for stacktrace from the return handler with or +* without pt_regs. */ void __naked __kprobes __kretprobe_trampoline (void ) { __asm__ __volatile__ ( -"sub sp, sp, #16 \n\t" + "ldr lr, =__kretprobe_trampoline \n\t" + "stmdb sp!, {sp, lr, pc} \n\t" +#ifdef CONFIG_FRAME_POINTER +/* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG +/* In clang case, pt_regs->ip = lr. */ +"stmdb sp!, {lr} \n\t" "stmdb sp!, {r0 - r11} \n\t" +/* fp points regs->r11 (fp) */ + "add fp, sp, #44 \n\t" +#else /* !CONFIG_CC_IS_CLANG */ +/* In gcc case, pt_regs->ip = fp. */ +"stmdb sp!, {fp} \n\t" +"stmdb sp!, {r0 - r11} \n\t" +/* fp points regs->r15 (pc) */ + "add fp, sp, #60 \n\t" +#endif /* CONFIG_CC_IS_CLANG */ +#else /* !CONFIG_FRAME_POINTER */ +"sub sp, sp, #4 \n\t" + "stmdb sp!, {r0 - r11} \n\t" +#endif /* CONFIG_FRAME_POINTER */ "mov r0, sp \n\t" "bl trampoline_handler \n\t" "mov lr, r0 \n\t"
Masami Hiramatsu
maintainerPATCH 8/8 ARM: Recover kretprobe modified return address in stacktrace
In reply to Masami HiramatsuSince the kretprobe replaces the function return address with the kretprobe_trampoline on the stack, arm unwinder shows it instead of the correct return address.
This finds the correct return address from the per-task kretprobe_instances list and verify it is in between the caller fp and callee fp.
Note that this supports both GCC and clang if CONFIG_FRAME_POINTER=y and CONFIG_ARM_UNWIND=n. For the ARM unwinder, this is still not working correctly.
--- arch/arm/Kconfig | 1 + arch/arm/include/asm/stacktrace.h | 5 +++++ arch/arm/kernel/return_address.c | 2 ++ arch/arm/kernel/stacktrace.c | 8 ++++++++ 4 files changed, 16 insertions(+)
arch/arm/Kconfig +1 -0
@@ -3,6 +3,7 @@ config ARM bool default y select ARCH_32BIT_OFF_T + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE if HAVE_KRETPROBES && FRAME_POINTER && !ARM_UNWIND select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE
arch/arm/include/asm/stacktrace.h +5 -0
@@ -3,6 +3,7 @@#define __ASM_STACKTRACE_H#include <asm/ptrace.h> +#include <linux/llist.h> struct stackframe {/*
@@ -13,6 +14,8 @@ struct stackframe {unsigned long sp;unsigned long lr;unsigned long pc; +struct llist_node *kr_cur; +struct task_struct *tsk; }; static __always_inline
@@ -22,6 +25,8 @@ void arm_get_current_stackframe(struct pt_regs *regs, struct stackframe *frame) frame->sp = regs->ARM_sp; frame->lr = regs->ARM_lr; frame->pc = regs->ARM_pc; + frame->kr_cur =NULL ; + frame->tsk = current; }extern int unwind_frame (struct stackframe *frame);
arch/arm/kernel/return_address.c +2 -0
@@ -42,6 +42,8 @@ void *return_address(unsigned int level) frame.sp = current_stack_pointer; frame.lr = (unsigned long )__builtin_return_address (0 ); frame.pc = (unsigned long )return_address; + frame.tsk = current; + frame.kr_cur =NULL ;walk_stackframe (&frame, save_return_addr, &data);
arch/arm/kernel/stacktrace.c +8 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/export.h> +#include <linux/kprobes.h> #include <linux/sched.h> #include <linux/sched/debug.h> #include <linux/stacktrace.h>
@@ -65,6 +66,9 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = *(unsigned long *)(fp -8 ); frame->pc = *(unsigned long *)(fp -4 );#endif +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (frame->tsk, + (void *)frame->fp, &frame->kr_cur);return 0 ; }
@@ -156,6 +160,8 @@ static noinline void __save_stack_trace(struct task_struct *tsk, frame.lr = (unsigned long )__builtin_return_address (0 ); frame.pc = (unsigned long )__save_stack_trace; } + frame.kr_cur =NULL ; + frame.tsk = tsk;walk_stackframe (&frame, save_trace, &data); }
@@ -173,6 +179,8 @@ void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) frame.sp = regs->ARM_sp; frame.lr = regs->ARM_lr; frame.pc = regs->ARM_pc; + frame.kr_cur =NULL ; + frame.tsk = current;walk_stackframe (&frame, save_trace, &data); }
Re: [PATCH 6/8] ARM: clang: Do not relay on lr register for stacktrace
In reply to Masami HiramatsuQuoted 8 lines · Masami Hiramatsu
Currently the stacktrace on clang compiled arm kernel uses the 'lr' register to find the first frame address from
pt_regs. However, that is wrong after calling another function, because the 'lr' register is used by 'bl' instruction and never be recovered.As same as gcc arm kernel, directly use the frame pointer (x11) of the
pt_regsto find the first frame address.
Hi Masami,
Thanks for the patch. Testing with ARCH=arm defconfig (multi_v7_defconfig)
Before this patch:
$ mount -t proc /proc $ echo 0 > /proc/sys/kernel/kptr_restrict $ cat /proc/self/stack [<0>] proc_single_show+0x4c/0xb8 [<0>] seq_read_iter+0x174/0x4d8 [<0>] seq_read+0x134/0x158 [<0>] vfs_read+0xcc/0x2f8 [<0>] ksys_read+0x74/0xd0 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xbea38cc0
After this patch:
$ mount -t proc /proc $ echo 0 > /proc/sys/kernel/kptr_restrict $ cat /proc/self/stack [<0>] proc_single_show+0x4c/0xb8 [<0>] seq_read_iter+0x174/0x4d8 [<0>] seq_read+0x134/0x158 [<0>] vfs_read+0xcc/0x2f8 [<0>] ksys_read+0x74/0xd0 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xbeb55cc0
Is there a different way to test/verify this patch? (I'm pretty sure we had verified the WARN_ONCE functionality with this, too.)
If I change from CONFIG_UNWINDER_ARM=y toCONFIG_UNWINDER_FRAME_POINTER=y, before:
# cat /proc/self/stack [<0>] stack_trace_save_tsk+0x50/0x6c [<0>] proc_pid_stack+0xa0/0xf8 [<0>] proc_single_show+0x50/0xbc [<0>] seq_read_iter+0x178/0x4ec [<0>] seq_read+0x138/0x15c [<0>] vfs_read+0xd0/0x304 [<0>] ksys_read+0x78/0xd4 [<0>] sys_read+0xc/0x10
after:
# cat /proc/self/stack [<0>] proc_pid_stack+0xa0/0xf8 [<0>] proc_single_show+0x50/0xbc [<0>] seq_read_iter+0x178/0x4ec [<0>] seq_read+0x138/0x15c [<0>] vfs_read+0xd0/0x304 [<0>] ksys_read+0x78/0xd4 [<0>] sys_read+0xc/0x10 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xffffffff
So I guess this helps the CONFIG_UNWINDER_FRAME_POINTER=y case? (That final frame address looks wrong, but is potentially yet another bug; perhaps for clang we need to manually store the previous frame's pc at a different offset before jumping to __entry_text_start).
Also, I'm curious about CONFIG_THUMB2_KERNEL (forces CONFIG_UNWINDER_ARM=y).
before:
# cat /proc/self/stack [<0>] proc_single_show+0x31/0x86 [<0>] seq_read_iter+0xff/0x326 [<0>] seq_read+0xd7/0xf2 [<0>] vfs_read+0x93/0x20e [<0>] ksys_read+0x53/0x92 [<0>] ret_fast_syscall+0x1/0x52 [<0>] 0xbe9a9cc0
after:
# cat /proc/self/stack [<0>] proc_single_show+0x31/0x86 [<0>] seq_read_iter+0xff/0x326 [<0>] seq_read+0xd7/0xf2 [<0>] vfs_read+0x93/0x20e [<0>] ksys_read+0x53/0x92 [<0>] ret_fast_syscall+0x1/0x52 [<0>] 0xbec08cc0
so likely this fixes/improves CONFIG_UNWINDER_FRAME_POINTER=y? Is that correct?
Quoted 21 lines
Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm/kernel/stacktrace.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/arm/kernel/stacktrace.c +1
-2@@ -54,8 +54,7 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = frame->fp; frame->fp = *(unsigned long *)(fp); - frame->pc = frame->lr; - frame->lr = *(unsigned long *)(fp +4 ); + frame->pc = *(unsigned long *)(fp +4 );#else /* check current frame pointer is within bounds */ if (fp < low +12 || fp > high -4 )
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCH 7/8] ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuQuoted 61 lines · Masami Hiramatsu
Currently kretprobe on ARM just fills r0-r11 of
pt_regs, but that is not enough for the stacktrace. Moreover, from the user kretprobe handler, stacktrace needs a frame pointer on the__kretprobe_trampoline.This adds a frame pointer on
__kretprobe_trampolinefor both gcc and clang case. Those have different frame pointer so we need different but similar stack onpt_regs.Gcc makes the frame pointer (fp) to point the 'pc' address of the {fp, ip (=sp), lr, pc}, this means {r11, r13, r14, r15}. Thus if we save the r11 (fp) on
pt_regs->r12, we can make this set on the end ofpt_regs.On the other hand, Clang makes the frame pointer to point the 'fp' address of {fp, lr} on stack. Since the next to the
pt_regs->lrispt_regs->sp, I reused the pair ofpt_regs->fpandpt_regs->ip.
So this stores the 'lr' onpt_regs->ipand make the fp to pointpt_regs->fp.For both cases, saves
__kretprobe_trampolineaddress topt_regs->lr, so that the stack tracer can identify this frame pointer has been made by the__kretprobe_trampoline.Note that if the
CONFIG_FRAME_POINTERis not set, this keeps fp as is.Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm/probes/kprobes/core.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)arch/arm/probes/kprobes/core.c +10
-5@@ -368,16 +368,35 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,/* * When a retprobed function returns, trampoline_handler() is called, * calling the kretprobe's handler. We construct a struct pt_regs to -* give a view of registers r0-r11 to the user return-handler. This is -* not a complete pt_regs structure, but that should be plenty sufficient -* for kretprobe handlers which should normally be interested in r0 only -* anyway. +* give a view of registers r0-r11, sp, lr, and pc to the user +* return-handler. This is not a complete pt_regs structure, but that +* should be enough for stacktrace from the return handler with or +* without pt_regs. */ void __naked __kprobes __kretprobe_trampoline (void ) { __asm__ __volatile__ ( - "sub sp, sp, #16 \n\t" +"ldr lr, =__kretprobe_trampoline \n\t" +"stmdb sp!, {sp, lr, pc} \n\t" +#ifdef CONFIG_FRAME_POINTER +/* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG + /* In clang case, pt_regs->ip = lr. */
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 394
Quoted 10 lines
arch/arm/probes/kprobes/core.c +9
-0+"stmdb sp!, {lr} \n\t" "stmdb sp!, {r0 - r11} \n\t" +/* fp points regs->r11 (fp) */ + "add fp, sp, #44 \n\t" +#else /* !CONFIG_CC_IS_CLANG */ +/* In gcc case, pt_regs->ip = fp. */ +"stmdb sp!, {fp} \n\t" +"stmdb sp!, {r0 - r11} \n\t" +/* fp points regs->r15 (pc) */ +"add fp, sp, #60 \n\t"
Quoted 9 lines
arch/arm/probes/kprobes/core.c +5
-0+#endif /* CONFIG_CC_IS_CLANG */ +#else /* !CONFIG_FRAME_POINTER */ +"sub sp, sp, #4 \n\t" + "stmdb sp!, {r0 - r11} \n\t" +#endif /* CONFIG_FRAME_POINTER */ "mov r0, sp \n\t" "bl trampoline_handler \n\t" "mov lr, r0 \n\t"
Signature · 2 lines
Thanks, ~Nick Desaulniers
Masami Hiramatsu
maintainerRe: [PATCH 6/8] ARM: clang: Do not relay on lr register for stacktrace
In reply to Nick DesaulniersHi Nick,
Quoted 38 lines · On Mon, 11 Oct 2021 11:45:22 -0700 Nick Desaulniers <[email protected]>…
Quoted 8 lines · Masami Hiramatsu
Currently the stacktrace on clang compiled arm kernel uses the 'lr' register to find the first frame address from
pt_regs. However, that is wrong after calling another function, because the 'lr' register is used by 'bl' instruction and never be recovered.As same as gcc arm kernel, directly use the frame pointer (x11) of the
pt_regsto find the first frame address.Hi Masami,
Thanks for the patch. Testing with ARCH=arm defconfig (multi_v7_defconfig)Before this patch:
$ mount -t proc /proc $ echo 0 > /proc/sys/kernel/kptr_restrict $ cat /proc/self/stack [<0>] proc_single_show+0x4c/0xb8 [<0>] seq_read_iter+0x174/0x4d8 [<0>] seq_read+0x134/0x158 [<0>] vfs_read+0xcc/0x2f8 [<0>] ksys_read+0x74/0xd0 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xbea38cc0After this patch:
$ mount -t proc /proc $ echo 0 > /proc/sys/kernel/kptr_restrict $ cat /proc/self/stack [<0>] proc_single_show+0x4c/0xb8 [<0>] seq_read_iter+0x174/0x4d8 [<0>] seq_read+0x134/0x158 [<0>] vfs_read+0xcc/0x2f8 [<0>] ksys_read+0x74/0xd0 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xbeb55cc0
Quoted 2 lines
Is there a different way to test/verify this patch? (I'm pretty sure we had verified the
WARN_ONCEfunctionality with this, too.)
Hmm, I found this bug while testing my kretprobe-stacktrace test ([2/8] in this series), so if you apply this series and revert this patch and enable CONFIG_KPROBES_SANITY_TEST, you'll see that the tests failures as below.
[ 4.062056] ok 4 - test_kretprobes [ 4.069944] # test_stacktrace_on_kretprobe: EXPECTATION FAILED at kernel/test_kprobes.c:235 [ 4.069944] Expected i != ret, but [ 4.069944] i == 10 [ 4.069944] ret == 10 [ 4.072692] not ok 5 - test_stacktrace_on_kretprobe [ 4.088171] # test_stacktrace_on_nested_kretprobe: EXPECTATION FAILED at kernel/test_kprobes.c:235 [ 4.088171] Expected i != ret, but [ 4.088171] i == 10 [ 4.088171] ret == 10 [ 4.091265] not ok 6 - test_stacktrace_on_nested_kretprobe
This means the test failed to find the correct return address from the stacktrace.
Applying this patch,
[ 4.283953] ok 4 - test_kretprobes [ 4.290206] ok 5 - test_stacktrace_on_kretprobe [ 4.300429] ok 6 - test_stacktrace_on_nested_kretprobe [ 4.301743] # kprobes_test: pass:6 fail:0 skip:0 total:6
Quoted 26 lines
If I change from
CONFIG_UNWINDER_ARM=ytoCONFIG_UNWINDER_FRAME_POINTER=y, before:# cat /proc/self/stack [<0>] stack_trace_save_tsk+0x50/0x6c [<0>] proc_pid_stack+0xa0/0xf8 [<0>] proc_single_show+0x50/0xbc [<0>] seq_read_iter+0x178/0x4ec [<0>] seq_read+0x138/0x15c [<0>] vfs_read+0xd0/0x304 [<0>] ksys_read+0x78/0xd4 [<0>] sys_read+0xc/0x10after:
# cat /proc/self/stack [<0>] proc_pid_stack+0xa0/0xf8 [<0>] proc_single_show+0x50/0xbc [<0>] seq_read_iter+0x178/0x4ec [<0>] seq_read+0x138/0x15c [<0>] vfs_read+0xd0/0x304 [<0>] ksys_read+0x78/0xd4 [<0>] sys_read+0xc/0x10 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xffffffff
Quoted 4 lines
So I guess this helps the
CONFIG_UNWINDER_FRAME_POINTER=ycase? (That final frame address looks wrong, but is potentially yet another bug; perhaps for clang we need to manually store the previous frame's pc at a different offset before jumping to__entry_text_start).
Ah, yes. I didn't touch the UNWINDER_ARM. As you may know the unwind_frame()@arch/arm/kernel/stacktrace.c is compiled only CONFIG_UNWINDER_FRAME_POINTER=y.
Quoted 23 lines
Also, I'm curious about
CONFIG_THUMB2_KERNEL(forcesCONFIG_UNWINDER_ARM=y).before:
# cat /proc/self/stack [<0>] proc_single_show+0x31/0x86 [<0>] seq_read_iter+0xff/0x326 [<0>] seq_read+0xd7/0xf2 [<0>] vfs_read+0x93/0x20e [<0>] ksys_read+0x53/0x92 [<0>] ret_fast_syscall+0x1/0x52 [<0>] 0xbe9a9cc0after:
# cat /proc/self/stack [<0>] proc_single_show+0x31/0x86 [<0>] seq_read_iter+0xff/0x326 [<0>] seq_read+0xd7/0xf2 [<0>] vfs_read+0x93/0x20e [<0>] ksys_read+0x53/0x92 [<0>] ret_fast_syscall+0x1/0x52 [<0>] 0xbec08cc0
Quoted 3 lines
Tested-by Nick Desaulniers <[email protected]>so likely this fixes/improves
CONFIG_UNWINDER_FRAME_POINTER=y? Is that correct?
Yes, that is correct.
Thank you!
Quoted 26 lines
Quoted 22 lines
Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm/kernel/stacktrace.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/arm/kernel/stacktrace.c +1
-2@@ -54,8 +54,7 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = frame->fp; frame->fp = *(unsigned long *)(fp); - frame->pc = frame->lr; - frame->lr = *(unsigned long *)(fp +4 ); + frame->pc = *(unsigned long *)(fp +4 );#else /* check current frame pointer is within bounds */ if (fp < low +12 || fp > high -4 )Signature · 2 lines
Thanks, ~Nick Desaulniers
Signature · 1 line
Masami Hiramatsu <[email protected]>
Will Deacon
maintainerRe: [PATCH 5/8] arm64: Recover kretprobe modified return address in stacktrace
In reply to Masami HiramatsuQuoted 44 lines · Masami Hiramatsu
Since the kretprobe replaces the function return address with the
kretprobe_trampolineon the stack, stack unwinder shows it instead of the correct return address.This checks whether the next return address is the
__kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list.With this fix, now arm64 can enable
CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests.Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 2 ++ arch/arm64/kernel/stacktrace.c | 3 +++ 3 files changed, 6 insertions(+)arch/arm64/Kconfig +1
-0@@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVEarch/arm64/include/asm/stacktrace.h +1
-0@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> #include <linux/types.h> +#include <linux/llist.h> #include <asm/memory.h> #include <asm/ptrace.h>
arch/arm64/include/asm/stacktrace.h struct stackframe { · line 64
Quoted 5 lines
arch/arm64/include/asm/stacktrace.h +1
-0@@ -59,6 +60,7 @@ struct stackframe {#ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph;#endif +struct llist_node *kr_cur; };
Please update the comment above this structure to describe the new member you're adding. If it's only relevant for kprobes, then let's define it conditionally too (based on CONFIG_KRETPROBES ?)
Quoted 15 lines
arch/arm64/include/asm/stacktrace.h +0
-0extern int unwind_frame (struct task_struct *tsk,struct stackframe *frame);arch/arm64/kernel/stacktrace.c +2
-0@@ -129,6 +129,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur); frame->pc =ptrauth_strip_insn_pac (frame->pc);
arch/arm64/kernel/stacktrace.c noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, · line 229
Quoted 4 lines
arch/arm64/kernel/stacktrace.c +1
-0@@ -224,6 +226,7 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, {struct stackframe frame; +memset (&frame,0 ,sizeof (frame));
Why do we need this?
Will
Re: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Masami HiramatsuQuoted 21 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/probes/kprobes.c +1
-1@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }
arch/arm64/kernel/probes/kprobes.c int __init arch_populate_kprobe_blacklist(void) · line 414
Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +1
-1void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;
Will
Re: [PATCH 4/8] arm64: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuQuoted 29 lines · Masami Hiramatsu
Make a frame pointer (make the x29 register points the address of pt_regs->regs[29]) on
__kretprobe_trampoline.This frame pointer will be used by the stacktracer when it is called from the kretprobe handlers. In this case, the stack tracer will unwind stack to
trampoline_probe_handler()and find the next frame pointer in the stack frame of the__kretprobe_trampoline().Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm64/kernel/probes/kprobes_trampoline.S | 4 ++++ 1 file changed, 4 insertions(+)arch/arm64/kernel/probes/kprobes_trampoline.S +3
-0@@ -66,6 +66,9 @@ SYM_CODE_START(__kretprobe_trampoline) save_all_base_regs + /* Setup a frame pointer. */ + add x29, sp, #S_FP + mov x0, sp bl trampoline_probe_handler /*
arch/arm64/kernel/probes/kprobes_trampoline.S SYM_CODE_START(__kretprobe_trampoline) · line 83
Quoted 7 lines
arch/arm64/kernel/probes/kprobes_trampoline.S +1
-0@@ -74,6 +77,7 @@ SYM_CODE_START(__kretprobe_trampoline) */ mov lr, x0 + /* The frame pointer (x29) is restored with other registers. */ restore_all_base_regs add sp, sp, #PT_REGS_SIZE
Will
Re: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Masami HiramatsuQuoted 4 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).
Just to check, why do we need to use the FP rather than SP? It wasn't clear to me if that's necessary later in the series, or if I'm missing something here.
FWIW, I plan to rework arm64's ftrace bits to use FP for HAVE_FUNCTION_GRAPH_RET_ADDR_PTR, so I'm happy to do likewise here.
Quoted 1 line
Signed-off-by Masami Hiramatsu <[email protected]>
Regardless of the above:
Mark.
Quoted 26 lines
--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/probes/kprobes.c +2
-2@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;
Mark Rutland
reviewerRe: [PATCH 5/8] arm64: Recover kretprobe modified return address in stacktrace
In reply to Masami HiramatsuQuoted 44 lines · Masami Hiramatsu
Since the kretprobe replaces the function return address with the
kretprobe_trampolineon the stack, stack unwinder shows it instead of the correct return address.This checks whether the next return address is the
__kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list.With this fix, now arm64 can enable
CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests.Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 2 ++ arch/arm64/kernel/stacktrace.c | 3 +++ 3 files changed, 6 insertions(+)arch/arm64/Kconfig +1
-0@@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVEarch/arm64/include/asm/stacktrace.h +1
-0@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> #include <linux/types.h> +#include <linux/llist.h> #include <asm/memory.h> #include <asm/ptrace.h>
arch/arm64/include/asm/stacktrace.h struct stackframe { · line 63
Quoted 4 lines
arch/arm64/include/asm/stacktrace.h +1
-0@@ -59,6 +60,7 @@ struct stackframe {#ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph;#endif +struct llist_node *kr_cur;
As with the fgraph bits above, please make this depedn on the relevant Kconfig, i.e.
| #ifdef CONFIG_KRETPROBES | struct llist_node *kr_cur; | #endif
arch/arm64/kernel/stacktrace.c int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) · line 133
Quoted 13 lines
arch/arm64/include/asm/stacktrace.h +0
-0};extern int unwind_frame (struct task_struct *tsk,struct stackframe *frame);arch/arm64/kernel/stacktrace.c +2
-0@@ -129,6 +129,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur);
Please ifdef this, like the CONFIG_FUNCTION_GRAPH_TRACER bits above. i.e.
| #ifdef CONFIG_KRETPROBES | if (is_kretprobe_trampoline(frame->pc)) | frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur); | #endif
What does kretprobe_find_ret_addr() do when it can't find the original address? I couldn't spot it in this series or in mainline.
As a future thing, I'd like to be able to have monotonicity and completeness checks as part of the unwind, i.e. checking that we consume the kretprobe address *in-order*, and can identify whether we've skipped any, so that we can identify when unwinding has gone wrong. Does it do that today?
It'd be nice if it could signal failure reliably (without causing a BUG() or similar), e.g. by returning an error code.
arch/arm64/kernel/stacktrace.c noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, · line 229
Quoted 7 lines
arch/arm64/kernel/stacktrace.c +1
-0frame->pc =ptrauth_strip_insn_pac (frame->pc);@@ -224,6 +226,7 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, {struct stackframe frame; +memset (&frame,0 ,sizeof (frame));
Please initialise stackframe::kr_cur in start_backtrace, where we initialize all the other fields in struct stackframe, i.e. just after the CONFIG_FUNCTION_GRAPH_TRACER bit, have:
| #ifdef CONFIG_KRETPROBES | frame->kr_cur = NULL; | #endif
Thanks,
Mark.
Quoted 4 lines
arch/arm64/kernel/stacktrace.c +0
-0if (regs)start_backtrace (&frame, regs->regs[29 ], regs->pc);else if (task == current)
Nick Desaulniers
Reviewed-byRe: [PATCH 6/8] ARM: clang: Do not relay on lr register for stacktrace
In reply to Masami HiramatsuQuoted 32 lines · Masami Hiramatsu
Hi Nick,
Quoted 26 lines · On Mon, 11 Oct 2021 11:45:22 -0700 Nick Desaulniers <[email protected]>…
If I change from
CONFIG_UNWINDER_ARM=ytoCONFIG_UNWINDER_FRAME_POINTER=y, before:# cat /proc/self/stack [<0>] stack_trace_save_tsk+0x50/0x6c [<0>] proc_pid_stack+0xa0/0xf8 [<0>] proc_single_show+0x50/0xbc [<0>] seq_read_iter+0x178/0x4ec [<0>] seq_read+0x138/0x15c [<0>] vfs_read+0xd0/0x304 [<0>] ksys_read+0x78/0xd4 [<0>] sys_read+0xc/0x10after:
# cat /proc/self/stack [<0>] proc_pid_stack+0xa0/0xf8 [<0>] proc_single_show+0x50/0xbc [<0>] seq_read_iter+0x178/0x4ec [<0>] seq_read+0x138/0x15c [<0>] vfs_read+0xd0/0x304 [<0>] ksys_read+0x78/0xd4 [<0>] sys_read+0xc/0x10 [<0>] __entry_text_start+0x14/0x14 [<0>] 0xffffffff
Quoted 8 lines
Quoted 4 lines
So I guess this helps the
CONFIG_UNWINDER_FRAME_POINTER=ycase? (That final frame address looks wrong, but is potentially yet another bug; perhaps for clang we need to manually store the previous frame's pc at a different offset before jumping to__entry_text_start).Ah, yes. I didn't touch the
UNWINDER_ARM. As you may know the unwind_frame()@arch/arm/kernel/stacktrace.c is compiled onlyCONFIG_UNWINDER_FRAME_POINTER=y.
Ah, right, thanks for pointing that out.
Quoted 3 lines
Quoted 1 line
so likely this fixes/improves
CONFIG_UNWINDER_FRAME_POINTER=y? Is that correct?Yes, that is correct.
In that case, additionally:
Signature · 2 lines
Thanks, ~Nick Desaulniers
Masami Hiramatsu
maintainerRe: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Mark RutlandQuoted 9 lines · On Wed, 13 Oct 2021 11:01:39 +0100 Mark Rutland <[email protected]> wrote:
Quoted 4 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).Just to check, why do we need to use the FP rather than SP? It wasn't clear to me if that's necessary later in the series, or if I'm missing something here.
Actually, this is for finding correct return address from the per-task kretprobe instruction list (suppose it as a shadow stack) when it will be searched in stack-backtracing. At that point, the framepointer will be a reliable key.
Quoted 3 lines
FWIW, I plan to rework arm64's ftrace bits to use FP for
HAVE_FUNCTION_GRAPH_RET_ADDR_PTR, so I'm happy to do likewise here.
Yes, I think you can use FP for that too.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Masami Hiramatsu <[email protected]>Regardless of the above:
Acked-by Mark Rutland <[email protected]>
Thank you!
Quoted 29 lines
Mark.
Quoted 26 lines
--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/probes/kprobes.c +2
-2@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerRe: [PATCH 5/8] arm64: Recover kretprobe modified return address in stacktrace
In reply to Will DeaconQuoted 42 lines · On Wed, 13 Oct 2021 09:14:16 +0100 Will Deacon <[email protected]> wrote:
Quoted 41 lines · Masami Hiramatsu
Since the kretprobe replaces the function return address with the
kretprobe_trampolineon the stack, stack unwinder shows it instead of the correct return address.This checks whether the next return address is the
__kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list.With this fix, now arm64 can enable
CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests.Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 2 ++ arch/arm64/kernel/stacktrace.c | 3 +++ 3 files changed, 6 insertions(+)arch/arm64/Kconfig +1
-0@@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVEarch/arm64/include/asm/stacktrace.h +1
-0@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> #include <linux/types.h> +#include <linux/llist.h>
Quoted 11 lines
arch/arm64/include/asm/stacktrace.h struct stackframe { · line 64
Quoted 8 lines
arch/arm64/include/asm/stacktrace.h +1
-0#include <asm/memory.h> #include <asm/ptrace.h> @@ -59,6 +60,7 @@ struct stackframe {#ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph;#endif +struct llist_node *kr_cur; };Please update the comment above this structure to describe the new member you're adding.
OK, let me update that.
Quoted 2 lines
If it's only relevant for kprobes, then let's define it conditionally too (based on
CONFIG_KRETPROBES?)
Ah, good point! Yes, it must be only valid when CONFIG_KRETPROBES=y.
Thank you,
Quoted 24 lines
Quoted 16 lines
arch/arm64/include/asm/stacktrace.h +0
-0extern int unwind_frame (struct task_struct *tsk,struct stackframe *frame);arch/arm64/kernel/stacktrace.c +2
-0@@ -129,6 +129,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur); frame->pc =ptrauth_strip_insn_pac (frame->pc);arch/arm64/kernel/stacktrace.c noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, · line 229
Quoted 4 lines
arch/arm64/kernel/stacktrace.c +1
-0@@ -224,6 +226,7 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, {struct stackframe frame; +memset (&frame,0 ,sizeof (frame));Why do we need this?
Will
Signature · 1 line
Masami Hiramatsu <[email protected]>
Mark Rutland
reviewerRe: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Masami HiramatsuQuoted 9 lines · Masami Hiramatsu
Quoted 6 lines · On Wed, 13 Oct 2021 11:01:39 +0100 Mark Rutland <[email protected]> wrote:
Quoted 4 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).
Quoted 8 lines
Quoted 3 lines
Just to check, why do we need to use the FP rather than SP? It wasn't clear to me if that's necessary later in the series, or if I'm missing something here.
Actually, this is for finding correct return address from the per-task kretprobe instruction list (suppose it as a shadow stack) when it will be searched in stack-backtracing. At that point, the framepointer will be a reliable key.
Sure, my question was more "why isn't the SP a reliable key?", because both the SP and FP should be balanced at function-entry and function-return time. I'm asking because I think I'm missing a subtlety.
I'm perfectly happy to use the FP even if they're equivalent; I just want to make sure there's not some issue I'm unaware of that could affect unwinding.
Thanks,
Mark.
Quoted 47 lines
Quoted 2 lines
FWIW, I plan to rework arm64's ftrace bits to use FP for
HAVE_FUNCTION_GRAPH_RET_ADDR_PTR, so I'm happy to do likewise here.Yes, I think you can use FP for that too.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Masami Hiramatsu <[email protected]>Regardless of the above:
Acked-by Mark Rutland <[email protected]>Thank you!
Quoted 29 lines
Mark.
Quoted 26 lines
--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/probes/kprobes.c +2
-2@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerRe: [PATCH 5/8] arm64: Recover kretprobe modified return address in stacktrace
In reply to Mark RutlandQuoted 53 lines · On Wed, 13 Oct 2021 11:13:51 +0100 Mark Rutland <[email protected]> wrote:
Quoted 44 lines · Masami Hiramatsu
Since the kretprobe replaces the function return address with the
kretprobe_trampolineon the stack, stack unwinder shows it instead of the correct return address.This checks whether the next return address is the
__kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list.With this fix, now arm64 can enable
CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests.Signed-off-by Masami Hiramatsu <[email protected]>--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 2 ++ arch/arm64/kernel/stacktrace.c | 3 +++ 3 files changed, 6 insertions(+)arch/arm64/Kconfig +1
-0@@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVEarch/arm64/include/asm/stacktrace.h +1
-0@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> #include <linux/types.h> +#include <linux/llist.h> #include <asm/memory.h> #include <asm/ptrace.h> arch/arm64/include/asm/stacktrace.h struct stackframe { · line 63
Quoted 4 lines
arch/arm64/include/asm/stacktrace.h +1
-0@@ -59,6 +60,7 @@ struct stackframe {#ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph;#endif +struct llist_node *kr_cur;As with the fgraph bits above, please make this depedn on the relevant
Kconfig, i.e.
Quoted 5 lines
| #ifdef CONFIG_KRETPROBES | struct llist_node *kr_cur; | #endifQuoted 1 line
arch/arm64/include/asm/stacktrace.h +0
-0};
OK.
Quoted 16 lines
arch/arm64/kernel/stacktrace.c int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) · line 133
Quoted 12 lines
arch/arm64/include/asm/stacktrace.h +0
-0extern int unwind_frame (struct task_struct *tsk,struct stackframe *frame);arch/arm64/kernel/stacktrace.c +2
-0@@ -129,6 +129,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur);Please ifdef this, like the
CONFIG_FUNCTION_GRAPH_TRACERbits above. i.e.
Quoted 4 lines
| #ifdef CONFIG_KRETPROBES | if (is_kretprobe_trampoline(frame->pc)) | frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur); | #endif
OK.
Quoted 3 lines
What does
kretprobe_find_ret_addr()do when it can't find the original address? I couldn't spot it in this series or in mainline.
If it couldn't find, it returns NULL.
Hmm, should we check it and keep frame->pc if the return value is NULL? (anyway, it must not happen. If it happens, that task can not continue to run.)
Quoted 5 lines
As a future thing, I'd like to be able to have monotonicity and completeness checks as part of the unwind, i.e. checking that we consume the kretprobe address *in-order*, and can identify whether we've skipped any, so that we can identify when unwinding has gone wrong. Does it do that today?
Good question. No today, but is easy to do since we have the loop cursor (frame->kr_cur).
unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
struct llist_node **cur)
{
...
do {
ret = __kretprobe_find_ret_addr(tsk, cur);
if (!ret)
break;
ri = container_of(*cur, struct kretprobe_instance, llist);
} while (ri->fp != fp);...
Change this loop to;
ri = container_of(*cur, struct kretprobe_instance, llist);
prev_fp = ri->fp;
do {
ret = __kretprobe_find_ret_addr(tsk, cur);
if (WARN_ON_ONCE(!ret))
return ERR_PTR(-ENOENT);
ri = container_of(*cur, struct kretprobe_instance, llist);
} while (ri->fp == prev_fp);
if (ri->fp != fp)
return ERR_PTR(-EILSEQ);Then, we can detect that wrong sequence from stacktrace side.
Quoted 3 lines
It'd be nice if it could signal failure reliably (without causing a
BUG()or similar), e.g. by returning an error code.
As above, -EILSEQ is OK?
Quoted 13 lines
arch/arm64/kernel/stacktrace.c noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, · line 229
Quoted 8 lines
arch/arm64/kernel/stacktrace.c +1
-0frame->pc =ptrauth_strip_insn_pac (frame->pc);@@ -224,6 +226,7 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, {struct stackframe frame; +memset (&frame,0 ,sizeof (frame));Please initialise
stackframe::kr_curinstart_backtrace, where we initialize all the other fields in struct stackframe, i.e. just after theCONFIG_FUNCTION_GRAPH_TRACERbit, have:
Quoted 3 lines
| #ifdef CONFIG_KRETPROBES | frame->kr_cur = NULL; | #endif
OK, let me update it.
Thank you!
Quoted 8 lines
Thanks,
Mark.Quoted 4 lines
arch/arm64/kernel/stacktrace.c +0
-0if (regs)start_backtrace (&frame, regs->regs[29 ], regs->pc);else if (task == current)
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerRe: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Mark RutlandQuoted 14 lines · On Thu, 14 Oct 2021 10:13:32 +0100 Mark Rutland <[email protected]> wrote:
Quoted 13 lines · Masami Hiramatsu
Quoted 9 lines · On Wed, 13 Oct 2021 11:01:39 +0100 Mark Rutland <[email protected]> wrote:
Quoted 4 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).Just to check, why do we need to use the FP rather than SP? It wasn't clear to me if that's necessary later in the series, or if I'm missing something here.
Quoted 8 lines
Quoted 4 lines
Actually, this is for finding correct return address from the per-task kretprobe instruction list (suppose it as a shadow stack) when it will be searched in stack-backtracing. At that point, the framepointer will be a reliable key.
Sure, my question was more "why isn't the SP a reliable key?", because both the SP and FP should be balanced at function-entry and function-return time. I'm asking because I think I'm missing a subtlety.
Ah, because SP is not used while unwinding frame. For the kretprobe, either FP or SP is OK. But for the stacktrace.c, I can not use SP and is easy to change to use FP. :)
So, when we introduce ORC unwinder on arm64, I think I have to reconsider using SP based on the configuration.
Thank you,
Quoted 55 lines
I'm perfectly happy to use the FP even if they're equivalent; I just want to make sure there's not some issue I'm unaware of that could affect unwinding.
Thanks,
Mark.Quoted 47 lines
Quoted 2 lines
FWIW, I plan to rework arm64's ftrace bits to use FP for
HAVE_FUNCTION_GRAPH_RET_ADDR_PTR, so I'm happy to do likewise here.Yes, I think you can use FP for that too.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Masami Hiramatsu <[email protected]>Regardless of the above:
Acked-by Mark Rutland <[email protected]>Thank you!
Quoted 29 lines
Mark.
Quoted 26 lines
--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/probes/kprobes.c +2
-2@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 1 line
Masami Hiramatsu <[email protected]>
Mark Rutland
reviewerRe: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
Quoted 22 lines · Masami Hiramatsu
Quoted 19 lines · On Thu, 14 Oct 2021 10:13:32 +0100 Mark Rutland <[email protected]> wrote:
Quoted 17 lines · Masami Hiramatsu
Quoted 9 lines · On Wed, 13 Oct 2021 11:01:39 +0100 Mark Rutland <[email protected]> wrote:
Quoted 4 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).Just to check, why do we need to use the FP rather than SP? It wasn't clear to me if that's necessary later in the series, or if I'm missing something here.
Actually, this is for finding correct return address from the per-task kretprobe instruction list (suppose it as a shadow stack) when it will be searched in stack-backtracing. At that point, the framepointer will be a reliable key.
Quoted 7 lines
Quoted 3 lines
Sure, my question was more "why isn't the SP a reliable key?", because both the SP and FP should be balanced at function-entry and function-return time. I'm asking because I think I'm missing a subtlety.
Ah, because SP is not used while unwinding frame. For the kretprobe, either FP or SP is OK. But for the
stacktrace.c, I can not use SP and is easy to change to use FP. :)
Ah, so this is just so that stacktrace can match the address. For clarity, would you be happy to add a sentence to the commit message like:
| This will allow the stacktrace code to find the original return | address from the FP alone.
Thanks,
Mark.
Masami Hiramatsu
maintainerRe: [PATCH 3/8] arm64: kprobes: Record frame pointer with kretprobe instance
Quoted 31 lines · On Thu, 14 Oct 2021 11:27:02 +0100 Mark Rutland <[email protected]> wrote:
Quoted 22 lines · Masami Hiramatsu
Quoted 19 lines · On Thu, 14 Oct 2021 10:13:32 +0100 Mark Rutland <[email protected]> wrote:
Quoted 17 lines · Masami Hiramatsu
Quoted 9 lines · On Wed, 13 Oct 2021 11:01:39 +0100 Mark Rutland <[email protected]> wrote:
Quoted 4 lines · Masami Hiramatsu
Record the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enableCONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).Just to check, why do we need to use the FP rather than SP? It wasn't clear to me if that's necessary later in the series, or if I'm missing something here.
Actually, this is for finding correct return address from the per-task kretprobe instruction list (suppose it as a shadow stack) when it will be searched in stack-backtracing. At that point, the framepointer will be a reliable key.
Quoted 7 lines
Quoted 3 lines
Sure, my question was more "why isn't the SP a reliable key?", because both the SP and FP should be balanced at function-entry and function-return time. I'm asking because I think I'm missing a subtlety.
Ah, because SP is not used while unwinding frame. For the kretprobe, either FP or SP is OK. But for the
stacktrace.c, I can not use SP and is easy to change to use FP. :)
Quoted 5 lines
Ah, so this is just so that stacktrace can match the address. For clarity, would you be happy to add a sentence to the commit message like:
| This will allow the stacktrace code to find the original return | address from the FP alone.
Yes, I'm happy to update the changelog :)
Thanks!
Quoted 3 lines
Thanks,
Mark.
Signature · 1 line
Masami Hiramatsu <[email protected]>
Russell King (Oracle)
maintainerRe: [PATCH 6/8] ARM: clang: Do not relay on lr register for stacktrace
In reply to Masami HiramatsuQuoted 7 lines · Masami Hiramatsu
Currently the stacktrace on clang compiled arm kernel uses the 'lr' register to find the first frame address from
pt_regs. However, that is wrong after calling another function, because the 'lr' register is used by 'bl' instruction and never be recovered.As same as gcc arm kernel, directly use the frame pointer (x11) of the
pt_regsto find the first frame address.
Can I ask that the subject line is corrected. It's "rely" not "relay".
Also, the frame pointer is called "r11" not "x11" if you want to use the numerical register reference for 32-bit ARM registers.
Thanks.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Masami Hiramatsu
maintainerRe: [PATCH 6/8] ARM: clang: Do not relay on lr register for stacktrace
Quoted 10 lines · On Thu, 14 Oct 2021 17:53:24 +0100 "Russell King (Oracle)"…
Quoted 7 lines · Masami Hiramatsu
Currently the stacktrace on clang compiled arm kernel uses the 'lr' register to find the first frame address from
pt_regs. However, that is wrong after calling another function, because the 'lr' register is used by 'bl' instruction and never be recovered.As same as gcc arm kernel, directly use the frame pointer (x11) of the
pt_regsto find the first frame address.Can I ask that the subject line is corrected. It's "rely" not "relay".
Oops, yes, that's my typo. Thanks for correcting!
Quoted 3 lines
Also, the frame pointer is called "r11" not "x11" if you want to use the numerical register reference for 32-bit ARM registers.
Oh, I mixed up the register name between arm64 and ARM...
Thank you,
Quoted 6 lines
Thanks.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Signature · 1 line
Masami Hiramatsu <[email protected]>