Skip to content

Commit b5e9ad5

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Repeat check_max_stack_depth for async callbacks
While the check_max_stack_depth function explores call chains emanating from the main prog, which is typically enough to cover all possible call chains, it doesn't explore those rooted at async callbacks unless the async callback will have been directly called, since unlike non-async callbacks it skips their instruction exploration as they don't contribute to stack depth. It could be the case that the async callback leads to a callchain which exceeds the stack depth, but this is never reachable while only exploring the entry point from main subprog. Hence, repeat the check for the main subprog *and* all async callbacks marked by the symbolic execution pass of the verifier, as execution of the program may begin at any of them. Consider functions with following stack depths: main: 256 async: 256 foo: 256 main: rX = async bpf_timer_set_callback(...) async: foo() Here, async is not descended as it does not contribute to stack depth of main (since it is referenced using bpf_pseudo_func and not bpf_pseudo_call). However, when async is invoked asynchronously, it will end up breaching the MAX_BPF_STACK limit by calling foo. Hence, in addition to main, we also need to explore call chains beginning at all async callback subprogs in a program. Fixes: 7ddc80a ("bpf: Teach stack depth check about async callbacks.") Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20230717161530.1238-3-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent ba7b3e7 commit b5e9ad5

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5573,16 +5573,17 @@ static int update_stack_depth(struct bpf_verifier_env *env,
55735573
* Since recursion is prevented by check_cfg() this algorithm
55745574
* only needs a local stack of MAX_CALL_FRAMES to remember callsites
55755575
*/
5576-
static int check_max_stack_depth(struct bpf_verifier_env *env)
5576+
static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx)
55775577
{
5578-
int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
55795578
struct bpf_subprog_info *subprog = env->subprog_info;
55805579
struct bpf_insn *insn = env->prog->insnsi;
5580+
int depth = 0, frame = 0, i, subprog_end;
55815581
bool tail_call_reachable = false;
55825582
int ret_insn[MAX_CALL_FRAMES];
55835583
int ret_prog[MAX_CALL_FRAMES];
55845584
int j;
55855585

5586+
i = subprog[idx].start;
55865587
process_func:
55875588
/* protect against potential stack overflow that might happen when
55885589
* bpf2bpf calls get combined with tailcalls. Limit the caller's stack
@@ -5683,6 +5684,22 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
56835684
goto continue_func;
56845685
}
56855686

5687+
static int check_max_stack_depth(struct bpf_verifier_env *env)
5688+
{
5689+
struct bpf_subprog_info *si = env->subprog_info;
5690+
int ret;
5691+
5692+
for (int i = 0; i < env->subprog_cnt; i++) {
5693+
if (!i || si[i].is_async_cb) {
5694+
ret = check_max_stack_depth_subprog(env, i);
5695+
if (ret < 0)
5696+
return ret;
5697+
}
5698+
continue;
5699+
}
5700+
return 0;
5701+
}
5702+
56865703
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
56875704
static int get_callee_stack_depth(struct bpf_verifier_env *env,
56885705
const struct bpf_insn *insn, int idx)

0 commit comments

Comments
 (0)