Skip to content

Commit 7d0d673

Browse files
thejhanakryiko
authored andcommitted
bpf: Fix theoretical prog_array UAF in __uprobe_perf_func()
Currently, the pointer stored in call->prog_array is loaded in __uprobe_perf_func(), with no RCU annotation and no immediately visible RCU protection, so it looks as if the loaded pointer can immediately be dangling. Later, bpf_prog_run_array_uprobe() starts a RCU-trace read-side critical section, but this is too late. It then uses rcu_dereference_check(), but this use of rcu_dereference_check() does not actually dereference anything. Fix it by aligning the semantics to bpf_prog_run_array(): Let the caller provide rcu_read_lock_trace() protection and then load call->prog_array with rcu_dereference_check(). This issue seems to be theoretical: I don't know of any way to reach this code without having handle_swbp() further up the stack, which is already holding a rcu_read_lock_trace() lock, so where we take rcu_read_lock_trace() in __uprobe_perf_func()/bpf_prog_run_array_uprobe() doesn't actually have any effect. Fixes: 8c7dcb8 ("bpf: implement sleepable uprobes by chaining gps") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c4441ca commit 7d0d673

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

include/linux/bpf.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,26 +2194,25 @@ bpf_prog_run_array(const struct bpf_prog_array *array,
21942194
* rcu-protected dynamically sized maps.
21952195
*/
21962196
static __always_inline u32
2197-
bpf_prog_run_array_uprobe(const struct bpf_prog_array __rcu *array_rcu,
2197+
bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
21982198
const void *ctx, bpf_prog_run_fn run_prog)
21992199
{
22002200
const struct bpf_prog_array_item *item;
22012201
const struct bpf_prog *prog;
2202-
const struct bpf_prog_array *array;
22032202
struct bpf_run_ctx *old_run_ctx;
22042203
struct bpf_trace_run_ctx run_ctx;
22052204
u32 ret = 1;
22062205

22072206
might_fault();
2207+
RCU_LOCKDEP_WARN(!rcu_read_lock_trace_held(), "no rcu lock held");
2208+
2209+
if (unlikely(!array))
2210+
return ret;
22082211

2209-
rcu_read_lock_trace();
22102212
migrate_disable();
22112213

22122214
run_ctx.is_uprobe = true;
22132215

2214-
array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
2215-
if (unlikely(!array))
2216-
goto out;
22172216
old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
22182217
item = &array->items[0];
22192218
while ((prog = READ_ONCE(item->prog))) {
@@ -2228,9 +2227,7 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array __rcu *array_rcu,
22282227
rcu_read_unlock();
22292228
}
22302229
bpf_reset_run_ctx(old_run_ctx);
2231-
out:
22322230
migrate_enable();
2233-
rcu_read_unlock_trace();
22342231
return ret;
22352232
}
22362233

kernel/trace/trace_uprobe.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,13 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
14021402

14031403
#ifdef CONFIG_BPF_EVENTS
14041404
if (bpf_prog_array_valid(call)) {
1405+
const struct bpf_prog_array *array;
14051406
u32 ret;
14061407

1407-
ret = bpf_prog_run_array_uprobe(call->prog_array, regs, bpf_prog_run);
1408+
rcu_read_lock_trace();
1409+
array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
1410+
ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
1411+
rcu_read_unlock_trace();
14081412
if (!ret)
14091413
return;
14101414
}

0 commit comments

Comments
 (0)