bpf: Fix UAF via mismatching bpf_prog/attachment RCU flavors
authorJann Horn <jannh@google.com>
Tue, 10 Dec 2024 16:32:13 +0000 (17:32 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 10 Dec 2024 18:14:02 +0000 (10:14 -0800)
Uprobes always use bpf_prog_run_array_uprobe() under tasks-trace-RCU
protection. But it is possible to attach a non-sleepable BPF program to a
uprobe, and non-sleepable BPF programs are freed via normal RCU (see
__bpf_prog_put_noref()). This leads to UAF of the bpf_prog because a normal
RCU grace period does not imply a tasks-trace-RCU grace period.

Fix it by explicitly waiting for a tasks-trace-RCU grace period after
removing the attachment of a bpf_prog to a perf_event.

Fixes: 8c7dcb84e3b7 ("bpf: implement sleepable uprobes by chaining gps")
Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/bpf/20241210-bpf-fix-actual-uprobe-uaf-v1-1-19439849dd44@google.com
kernel/trace/bpf_trace.c

index 949a3870946c381820e8fa7194851b84593d17d9..a403b05a7091384fb08e8c47ed02fad79c1a4874 100644 (file)
@@ -2258,6 +2258,13 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
                bpf_prog_array_free_sleepable(old_array);
        }
 
+       /*
+        * It could be that the bpf_prog is not sleepable (and will be freed
+        * via normal RCU), but is called from a point that supports sleepable
+        * programs and uses tasks-trace-RCU.
+        */
+       synchronize_rcu_tasks_trace();
+
        bpf_prog_put(event->prog);
        event->prog = NULL;