[PATCH] trace/trace_uprobe: Only invoke uprobe ebpf handler when event matches.

From: David Wang
Date: Fri Nov 04 2022 - 09:07:44 EST


Only invoke uprobe ebpf handler when event matches.

uprobe ebpf handler would be called even the event dose not
match any registered perf event, following steps would be
used to generate a unregistered perf event.
1. register a uprobe event on a specified pid <pid-a>
2. <pid-a> invokes syscall `clone` (via pthread_create),
new process <pid-b> generated. (Maybe it is a bug here, the
uprobe breakpoint is inherited from <pid-a>
3. <pid-b> invokes the function which is uprobed in step 1.
4. perf event generated...

Ebpf handler would be invoked even the event happened on <pid-b>,
but the default perf event handler make further check and ignore
the event because no registered perf event match on <pid-b>.

The patch means to fix the inconsistent behavior between ebpf and the default.
Before invoke uprobe ebpf handler, make sure current event match.

Signed-off-by: David Wang <00107082@xxxxxxx>
--
diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c
index fb58e86dd117..6f13163c0c0f 100644
--- a/kernel/trace/trace_uprobe.c
+++ b/kernel/trace/trace_uprobe.c
@@ -1346,27 +1346,20 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
void *data;
int size, esize;
int rctx;
+ bool trace_event_match = true;

-#ifdef CONFIG_BPF_EVENTS
- if (bpf_prog_array_valid(call)) {
- u32 ret;

- ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run);
- if (!ret)
- return;
+ preempt_disable();
+ head = this_cpu_ptr(call->perf_events);
+ if (hlist_empty(head)) {
+ trace_event_match = false;
+ goto out;
}
-#endif /* CONFIG_BPF_EVENTS */

esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
-
size = esize + tu->tp.size + dsize;
size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
- return;
-
- preempt_disable();
- head = this_cpu_ptr(call->perf_events);
- if (hlist_empty(head))
goto out;

entry = perf_trace_buf_alloc(size, NULL, &rctx);
@@ -1389,11 +1382,21 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,

memset(data + len, 0, size - esize - len);
}
-
perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
head, NULL);
out:
preempt_enable();
+
+#ifdef CONFIG_BPF_EVENTS
+ if (trace_event_match && bpf_prog_array_valid(call)) {
+ u32 ret;
+
+ ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run);
+ if (!ret)
+ return;
+ }
+#endif /* CONFIG_BPF_EVENTS */
+
}

/* uprobe profile handler */
--