Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -10974,6 +10974,10 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
bool in_callback_fn;
int err;

err = bpf_update_live_stack(env);
if (err)
return err;

callee = state->frame[state->curframe];
r0 = &callee->regs[BPF_REG_0];
if (r0->type == PTR_TO_STACK) {
Expand Down Expand Up @@ -11884,6 +11888,24 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
env->prog->call_get_func_ip = true;
}

if (func_id == BPF_FUNC_tail_call) {
if (env->cur_state->curframe) {
struct bpf_verifier_state *branch;
mark_reg_scratched(env, BPF_REG_0);
branch = push_stack(env, env->insn_idx + 1, env->insn_idx, false);
if (IS_ERR(branch))
return PTR_ERR(branch);
clear_all_pkt_pointers(env);
mark_reg_unknown(env, regs, BPF_REG_0);
err = prepare_func_exit(env, &env->insn_idx);
if (err)
return err;
env->insn_idx--;
} else {
changes_data = false;
}
}

if (changes_data)
clear_all_pkt_pointers(env);
return 0;
Expand Down Expand Up @@ -19782,9 +19804,6 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,
return PROCESS_BPF_EXIT;

if (env->cur_state->curframe) {
err = bpf_update_live_stack(env);
if (err)
return err;
/* exit from nested function */
err = prepare_func_exit(env, &env->insn_idx);
if (err)
Expand Down
39 changes: 37 additions & 2 deletions tools/testing/selftests/bpf/progs/verifier_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,10 +1117,17 @@ int tail_call(struct __sk_buff *sk)
return 0;
}

/* Tail calls invalidate packet pointers. */
static __noinline
int static_tail_call(struct __sk_buff *sk)
{
bpf_tail_call_static(sk, &jmp_table, 0);
return 0;
}

/* Tail calls in sub-programs invalidate packet pointers. */
SEC("tc")
__failure __msg("invalid mem access")
int invalidate_pkt_pointers_by_tail_call(struct __sk_buff *sk)
int invalidate_pkt_pointers_by_global_tail_call(struct __sk_buff *sk)
{
int *p = (void *)(long)sk->data;

Expand All @@ -1131,4 +1138,32 @@ int invalidate_pkt_pointers_by_tail_call(struct __sk_buff *sk)
return TCX_PASS;
}

/* Tail calls in static sub-programs invalidate packet pointers. */
SEC("tc")
__failure __msg("invalid mem access")
int invalidate_pkt_pointers_by_static_tail_call(struct __sk_buff *sk)
{
int *p = (void *)(long)sk->data;

if ((void *)(p + 1) > (void *)(long)sk->data_end)
return TCX_DROP;
static_tail_call(sk);
*p = 42; /* this is unsafe */
return TCX_PASS;
}

/* Direct tail calls do not invalidate packet pointers. */
SEC("tc")
__success
int invalidate_pkt_pointers_by_tail_call(struct __sk_buff *sk)
{
int *p = (void *)(long)sk->data;

if ((void *)(p + 1) > (void *)(long)sk->data_end)
return TCX_DROP;
bpf_tail_call_static(sk, &jmp_table, 0);
*p = 42; /* this is NOT unsafe: tail calls don't return */
return TCX_PASS;
}

char _license[] SEC("license") = "GPL";
Loading