Skip to content

Commit 6764e76

Browse files
Sebastian Andrzej Siewiorborkmann
authored andcommitted
bpf: Assign bpf_tramp_run_ctx::saved_run_ctx before recursion check.
__bpf_prog_enter_recur() assigns bpf_tramp_run_ctx::saved_run_ctx before performing the recursion check which means in case of a recursion __bpf_prog_exit_recur() uses the previously set bpf_tramp_run_ctx::saved_run_ctx value. __bpf_prog_enter_sleepable_recur() assigns bpf_tramp_run_ctx::saved_run_ctx after the recursion check which means in case of a recursion __bpf_prog_exit_sleepable_recur() uses an uninitialized value. This does not look right. If I read the entry trampoline code right, then bpf_tramp_run_ctx isn't initialized upfront. Align __bpf_prog_enter_sleepable_recur() with __bpf_prog_enter_recur() and set bpf_tramp_run_ctx::saved_run_ctx before the recursion check is made. Remove the assignment of saved_run_ctx in kern_sys_bpf() since it happens a few cycles later. Fixes: e384c7b ("bpf, x86: Create bpf_tramp_run_ctx on the caller thread's stack") Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 7645629 commit 6764e76

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

kernel/bpf/syscall.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5502,7 +5502,6 @@ int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
55025502
}
55035503

55045504
run_ctx.bpf_cookie = 0;
5505-
run_ctx.saved_run_ctx = NULL;
55065505
if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
55075506
/* recursion detected */
55085507
__bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx);

kernel/bpf/trampoline.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,13 +926,12 @@ u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
926926
migrate_disable();
927927
might_fault();
928928

929+
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
930+
929931
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
930932
bpf_prog_inc_misses_counter(prog);
931933
return 0;
932934
}
933-
934-
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
935-
936935
return bpf_prog_start_time();
937936
}
938937

0 commit comments

Comments
 (0)