Skip to content

Commit 6e1bab7

Browse files
teckiKernel Patches Daemon
authored andcommitted
bpf: properly verify tail call behavior
A successful ebpf tail call does not return to the caller, but to the caller-of-the-caller, often just finishing the ebpf program altogether. Any restrictions that the verifier needs to take into account - notably the fact that the tail call might have modified packet pointers - are to be checked on the caller-of-the-caller. Checking it on the caller made the verifier refuse perfectly fine programs that would use the packet pointers after a tail call, which is no problem as this code is only executed if the tail call was unsuccessful, i.e. nothing happened. This patch simulates the behavior of a tail call in the verifier. A conditional jump to the code after the tail call is added for the case of an unsucessful tail call, and a return to the caller is simulated for a successful tail call. For the successful case we assume that the tail call returns an int, as tail calls are currently only allowed in functions that return and int. We always assume that the tail call modified the packet pointers, as we do not know what the tail call did. For the unsuccessful case we know nothing happened, so we do not need to add new constraints. This approach also allows to check other problems that may occur with tail calls, namely we are now able to check that precision is properly propagated into subprograms using tail calls, as well as checking the live slots in such a subprogram. Fixes: 1a4607f ("bpf: consider that tail calls invalidate packet pointers") Link: https://lore.kernel.org/bpf/[email protected]/ Signed-off-by: Martin Teichmann <[email protected]> Acked-by: Eduard Zingerman <[email protected]>
1 parent 7b6b51d commit 6e1bab7

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

kernel/bpf/verifier.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,6 +4438,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
44384438
bt_reg_mask(bt));
44394439
return -EFAULT;
44404440
}
4441+
if (insn->src_reg == BPF_REG_0 && insn->imm == BPF_FUNC_tail_call
4442+
&& subseq_idx - idx != 1) {
4443+
if (bt_subprog_enter(bt))
4444+
return -EFAULT;
4445+
}
44414446
} else if (opcode == BPF_EXIT) {
44424447
bool r0_precise;
44434448

@@ -11064,6 +11069,10 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
1106411069
bool in_callback_fn;
1106511070
int err;
1106611071

11072+
err = bpf_update_live_stack(env);
11073+
if (err)
11074+
return err;
11075+
1106711076
callee = state->frame[state->curframe];
1106811077
r0 = &callee->regs[BPF_REG_0];
1106911078
if (r0->type == PTR_TO_STACK) {
@@ -11970,6 +11979,25 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1197011979
env->prog->call_get_func_ip = true;
1197111980
}
1197211981

11982+
if (func_id == BPF_FUNC_tail_call) {
11983+
if (env->cur_state->curframe) {
11984+
struct bpf_verifier_state *branch;
11985+
11986+
mark_reg_scratched(env, BPF_REG_0);
11987+
branch = push_stack(env, env->insn_idx + 1, env->insn_idx, false);
11988+
if (IS_ERR(branch))
11989+
return PTR_ERR(branch);
11990+
clear_all_pkt_pointers(env);
11991+
mark_reg_unknown(env, regs, BPF_REG_0);
11992+
err = prepare_func_exit(env, &env->insn_idx);
11993+
if (err)
11994+
return err;
11995+
env->insn_idx--;
11996+
} else {
11997+
changes_data = false;
11998+
}
11999+
}
12000+
1197312001
if (changes_data)
1197412002
clear_all_pkt_pointers(env);
1197512003
return 0;
@@ -20140,9 +20168,6 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,
2014020168
return PROCESS_BPF_EXIT;
2014120169

2014220170
if (env->cur_state->curframe) {
20143-
err = bpf_update_live_stack(env);
20144-
if (err)
20145-
return err;
2014620171
/* exit from nested function */
2014720172
err = prepare_func_exit(env, &env->insn_idx);
2014820173
if (err)

0 commit comments

Comments
 (0)