Skip to content

Commit 4cc8c50

Browse files
juntongdengAlexei Starovoitov
authored andcommitted
bpf: Make the pointer returned by iter next method valid
Currently we cannot pass the pointer returned by iter next method as argument to KF_TRUSTED_ARGS or KF_RCU kfuncs, because the pointer returned by iter next method is not "valid". This patch sets the pointer returned by iter next method to be valid. This is based on the fact that if the iterator is implemented correctly, then the pointer returned from the iter next method should be valid. This does not make NULL pointer valid. If the iter next method has KF_RET_NULL flag, then the verifier will ask the ebpf program to check NULL pointer. KF_RCU_PROTECTED iterator is a special case, the pointer returned by iter next method should only be valid within RCU critical section, so it should be with MEM_RCU, not PTR_TRUSTED. Another special case is bpf_iter_num_next, which returns a pointer with base type PTR_TO_MEM. PTR_TO_MEM should not be combined with type flag PTR_TRUSTED (PTR_TO_MEM already means the pointer is valid). The pointer returned by iter next method of other types of iterators is with PTR_TRUSTED. In addition, this patch adds get_iter_from_state to help us get the current iterator from the current state. Signed-off-by: Juntong Deng <[email protected]> Link: https://lore.kernel.org/r/AM6PR03MB584869F8B448EA1C87B7CDA399962@AM6PR03MB5848.eurprd03.prod.outlook.com Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f628456 commit 4cc8c50

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

kernel/bpf/verifier.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8148,6 +8148,15 @@ static int widen_imprecise_scalars(struct bpf_verifier_env *env,
81488148
return 0;
81498149
}
81508150

8151+
static struct bpf_reg_state *get_iter_from_state(struct bpf_verifier_state *cur_st,
8152+
struct bpf_kfunc_call_arg_meta *meta)
8153+
{
8154+
int iter_frameno = meta->iter.frameno;
8155+
int iter_spi = meta->iter.spi;
8156+
8157+
return &cur_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr;
8158+
}
8159+
81518160
/* process_iter_next_call() is called when verifier gets to iterator's next
81528161
* "method" (e.g., bpf_iter_num_next() for numbers iterator) call. We'll refer
81538162
* to it as just "iter_next()" in comments below.
@@ -8232,12 +8241,10 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,
82328241
struct bpf_verifier_state *cur_st = env->cur_state, *queued_st, *prev_st;
82338242
struct bpf_func_state *cur_fr = cur_st->frame[cur_st->curframe], *queued_fr;
82348243
struct bpf_reg_state *cur_iter, *queued_iter;
8235-
int iter_frameno = meta->iter.frameno;
8236-
int iter_spi = meta->iter.spi;
82378244

82388245
BTF_TYPE_EMIT(struct bpf_iter);
82398246

8240-
cur_iter = &env->cur_state->frame[iter_frameno]->stack[iter_spi].spilled_ptr;
8247+
cur_iter = get_iter_from_state(cur_st, meta);
82418248

82428249
if (cur_iter->iter.state != BPF_ITER_STATE_ACTIVE &&
82438250
cur_iter->iter.state != BPF_ITER_STATE_DRAINED) {
@@ -8265,7 +8272,7 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,
82658272
if (!queued_st)
82668273
return -ENOMEM;
82678274

8268-
queued_iter = &queued_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr;
8275+
queued_iter = get_iter_from_state(queued_st, meta);
82698276
queued_iter->iter.state = BPF_ITER_STATE_ACTIVE;
82708277
queued_iter->iter.depth++;
82718278
if (prev_st)
@@ -12853,6 +12860,17 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1285312860
regs[BPF_REG_0].btf = desc_btf;
1285412861
regs[BPF_REG_0].type = PTR_TO_BTF_ID;
1285512862
regs[BPF_REG_0].btf_id = ptr_type_id;
12863+
12864+
if (is_iter_next_kfunc(&meta)) {
12865+
struct bpf_reg_state *cur_iter;
12866+
12867+
cur_iter = get_iter_from_state(env->cur_state, &meta);
12868+
12869+
if (cur_iter->type & MEM_RCU) /* KF_RCU_PROTECTED */
12870+
regs[BPF_REG_0].type |= MEM_RCU;
12871+
else
12872+
regs[BPF_REG_0].type |= PTR_TRUSTED;
12873+
}
1285612874
}
1285712875

1285812876
if (is_kfunc_ret_null(&meta)) {

0 commit comments

Comments
 (0)