]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf,uprobes: fix user stack traces in the presence of pending uretprobes
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 22 May 2024 01:38:43 +0000 (18:38 -0700)
committerMasami Hiramatsu (Google) <mhiramat@kernel.org>
Tue, 25 Jun 2024 01:03:23 +0000 (10:03 +0900)
When kernel has pending uretprobes installed, it hijacks original user
function return address on the stack with a uretprobe trampoline
address. There could be multiple such pending uretprobes (either on
different user functions or on the same recursive one) at any given
time within the same task.

This approach interferes with the user stack trace capture logic, which
would report suprising addresses (like 0x7fffffffe000) that correspond
to a special "[uprobes]" section that kernel installs in the target
process address space for uretprobe trampoline code, while logically it
should be an address somewhere within the calling function of another
traced user function.

This is easy to correct for, though. Uprobes subsystem keeps track of
pending uretprobes and records original return addresses. This patch is
using this to do a post-processing step and restore each trampoline
address entries with correct original return address. This is done only
if there are pending uretprobes for current task.

This is a similar approach to what fprobe/kretprobe infrastructure is
doing when capturing kernel stack traces in the presence of pending
return probes.

Link: https://lore.kernel.org/all/20240522013845.1631305-3-andrii@kernel.org/
Reported-by: Riham Selim <rihams@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
kernel/events/callchain.c
kernel/events/uprobes.c

index 1273be84392cfcd00a9c2752a4c2c45a9f21da78..b17e3323f7f637d8d1706f922515f805868a8678 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/perf_event.h>
 #include <linux/slab.h>
 #include <linux/sched/task_stack.h>
+#include <linux/uprobes.h>
 
 #include "internal.h"
 
@@ -176,13 +177,51 @@ put_callchain_entry(int rctx)
        put_recursion_context(this_cpu_ptr(callchain_recursion), rctx);
 }
 
+static void fixup_uretprobe_trampoline_entries(struct perf_callchain_entry *entry,
+                                              int start_entry_idx)
+{
+#ifdef CONFIG_UPROBES
+       struct uprobe_task *utask = current->utask;
+       struct return_instance *ri;
+       __u64 *cur_ip, *last_ip, tramp_addr;
+
+       if (likely(!utask || !utask->return_instances))
+               return;
+
+       cur_ip = &entry->ip[start_entry_idx];
+       last_ip = &entry->ip[entry->nr - 1];
+       ri = utask->return_instances;
+       tramp_addr = uprobe_get_trampoline_vaddr();
+
+       /*
+        * If there are pending uretprobes for the current thread, they are
+        * recorded in a list inside utask->return_instances; each such
+        * pending uretprobe replaces traced user function's return address on
+        * the stack, so when stack trace is captured, instead of seeing
+        * actual function's return address, we'll have one or many uretprobe
+        * trampoline addresses in the stack trace, which are not helpful and
+        * misleading to users.
+        * So here we go over the pending list of uretprobes, and each
+        * encountered trampoline address is replaced with actual return
+        * address.
+        */
+       while (ri && cur_ip <= last_ip) {
+               if (*cur_ip == tramp_addr) {
+                       *cur_ip = ri->orig_ret_vaddr;
+                       ri = ri->next;
+               }
+               cur_ip++;
+       }
+#endif
+}
+
 struct perf_callchain_entry *
 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                   u32 max_stack, bool crosstask, bool add_mark)
 {
        struct perf_callchain_entry *entry;
        struct perf_callchain_entry_ctx ctx;
-       int rctx;
+       int rctx, start_entry_idx;
 
        entry = get_callchain_entry(&rctx);
        if (!entry)
@@ -215,7 +254,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                        if (add_mark)
                                perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
 
+                       start_entry_idx = entry->nr;
                        perf_callchain_user(&ctx, regs);
+                       fixup_uretprobe_trampoline_entries(entry, start_entry_idx);
                }
        }
 
index 2816e65729ac13783dfc1aef4fc4f7e1497addd7..99be2adedbc01753a6bd3595357b0fa9bbb8a623 100644 (file)
@@ -2159,6 +2159,15 @@ void uprobe_handle_trampoline(struct pt_regs *regs)
 
                instruction_pointer_set(regs, ri->orig_ret_vaddr);
                do {
+                       /* pop current instance from the stack of pending return instances,
+                        * as it's not pending anymore: we just fixed up original
+                        * instruction pointer in regs and are about to call handlers;
+                        * this allows fixup_uretprobe_trampoline_entries() to properly fix up
+                        * captured stack traces from uretprobe handlers, in which pending
+                        * trampoline addresses on the stack are replaced with correct
+                        * original return addresses
+                        */
+                       utask->return_instances = ri->next;
                        if (valid)
                                handle_uretprobe_chain(ri, regs);
                        ri = free_ret_instance(ri);