Skip to content

Commit ca3c4f6

Browse files
Pu LehuiAlexei Starovoitov
authored andcommitted
bpf: Move out synchronize_rcu_tasks_trace from mutex CS
Commit ef1b808 ("bpf: Fix UAF via mismatching bpf_prog/attachment RCU flavors") resolved a possible UAF issue in uprobes that attach non-sleepable bpf prog by explicitly waiting for a tasks-trace-RCU grace period. But, in the current implementation, synchronize_rcu_tasks_trace is included within the mutex critical section, which increases the length of the critical section and may affect performance. So let's move out synchronize_rcu_tasks_trace from mutex CS. Signed-off-by: Pu Lehui <[email protected]> Reviewed-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b8b1e30 commit ca3c4f6

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

kernel/trace/bpf_trace.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,7 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
22452245
{
22462246
struct bpf_prog_array *old_array;
22472247
struct bpf_prog_array *new_array;
2248+
struct bpf_prog *prog = NULL;
22482249
int ret;
22492250

22502251
mutex_lock(&bpf_event_mutex);
@@ -2265,18 +2266,22 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
22652266
}
22662267

22672268
put:
2268-
/*
2269-
* It could be that the bpf_prog is not sleepable (and will be freed
2270-
* via normal RCU), but is called from a point that supports sleepable
2271-
* programs and uses tasks-trace-RCU.
2272-
*/
2273-
synchronize_rcu_tasks_trace();
2274-
2275-
bpf_prog_put(event->prog);
2269+
prog = event->prog;
22762270
event->prog = NULL;
22772271

22782272
unlock:
22792273
mutex_unlock(&bpf_event_mutex);
2274+
2275+
if (prog) {
2276+
/*
2277+
* It could be that the bpf_prog is not sleepable (and will be freed
2278+
* via normal RCU), but is called from a point that supports sleepable
2279+
* programs and uses tasks-trace-RCU.
2280+
*/
2281+
synchronize_rcu_tasks_trace();
2282+
2283+
bpf_prog_put(prog);
2284+
}
22802285
}
22812286

22822287
int perf_event_query_prog_array(struct perf_event *event, void __user *info)

0 commit comments

Comments
 (0)