Skip to content

Commit baebe9a

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: allow passing struct bpf_iter_<type> as kfunc arguments
There are potentially useful cases where a specific iterator type might need to be passed into some kfunc. So, in addition to existing bpf_iter_<type>_{new,next,destroy}() kfuncs, allow to pass iterator pointer to any kfunc. We employ "__iter" naming suffix for arguments that are meant to accept iterators. We also enforce that they accept PTR -> STRUCT btf_iter_<type> type chain and point to a valid initialized on-the-stack iterator state. Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 496ddd1 commit baebe9a

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

kernel/bpf/verifier.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7970,27 +7970,38 @@ static bool is_iter_destroy_kfunc(struct bpf_kfunc_call_arg_meta *meta)
79707970
return meta->kfunc_flags & KF_ITER_DESTROY;
79717971
}
79727972

7973-
static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg)
7973+
static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg_idx,
7974+
const struct btf_param *arg)
79747975
{
79757976
/* btf_check_iter_kfuncs() guarantees that first argument of any iter
79767977
* kfunc is iter state pointer
79777978
*/
7978-
return arg == 0 && is_iter_kfunc(meta);
7979+
if (is_iter_kfunc(meta))
7980+
return arg_idx == 0;
7981+
7982+
/* iter passed as an argument to a generic kfunc */
7983+
return btf_param_match_suffix(meta->btf, arg, "__iter");
79797984
}
79807985

79817986
static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx,
79827987
struct bpf_kfunc_call_arg_meta *meta)
79837988
{
79847989
struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
79857990
const struct btf_type *t;
7986-
const struct btf_param *arg;
7987-
int spi, err, i, nr_slots;
7988-
u32 btf_id;
7991+
int spi, err, i, nr_slots, btf_id;
79897992

7990-
/* btf_check_iter_kfuncs() ensures we don't need to validate anything here */
7991-
arg = &btf_params(meta->func_proto)[0];
7992-
t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */
7993-
t = btf_type_skip_modifiers(meta->btf, t->type, &btf_id); /* STRUCT */
7993+
/* For iter_{new,next,destroy} functions, btf_check_iter_kfuncs()
7994+
* ensures struct convention, so we wouldn't need to do any BTF
7995+
* validation here. But given iter state can be passed as a parameter
7996+
* to any kfunc, if arg has "__iter" suffix, we need to be a bit more
7997+
* conservative here.
7998+
*/
7999+
btf_id = btf_check_iter_arg(meta->btf, meta->func_proto, regno - 1);
8000+
if (btf_id < 0) {
8001+
verbose(env, "expected valid iter pointer as arg #%d\n", regno);
8002+
return -EINVAL;
8003+
}
8004+
t = btf_type_by_id(meta->btf, btf_id);
79948005
nr_slots = t->size / BPF_REG_SIZE;
79958006

79968007
if (is_iter_new_kfunc(meta)) {
@@ -8012,7 +8023,9 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id
80128023
if (err)
80138024
return err;
80148025
} else {
8015-
/* iter_next() or iter_destroy() expect initialized iter state*/
8026+
/* iter_next() or iter_destroy(), as well as any kfunc
8027+
* accepting iter argument, expect initialized iter state
8028+
*/
80168029
err = is_iter_reg_valid_init(env, reg, meta->btf, btf_id, nr_slots);
80178030
switch (err) {
80188031
case 0:
@@ -11382,7 +11395,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1138211395
if (is_kfunc_arg_dynptr(meta->btf, &args[argno]))
1138311396
return KF_ARG_PTR_TO_DYNPTR;
1138411397

11385-
if (is_kfunc_arg_iter(meta, argno))
11398+
if (is_kfunc_arg_iter(meta, argno, &args[argno]))
1138611399
return KF_ARG_PTR_TO_ITER;
1138711400

1138811401
if (is_kfunc_arg_list_head(meta->btf, &args[argno]))

0 commit comments

Comments
 (0)