Skip to content

Commit b5c0a07

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
bpf: Unify dynptr handling in the verifier
Simplify dynptr checking for helper and kfunc by unifying it. Remember the initialized dynptr (i.e.,g !(arg_type |= MEM_UNINIT)) pass to a dynptr kfunc during process_dynptr_func() so that we can easily retrieve the information for verification later. By saving it in meta->dynptr, there is no need to call dynptr helpers such as dynptr_id(), dynptr_ref_obj_id() and dynptr_type() in check_func_arg(). Remove and open code the helpers in process_dynptr_func() when saving id, ref_obj_id, and type. Besides, since dynptr ref_obj_id information is now pass around in meta->bpf_dynptr_desc, drop the check in helper_multiple_ref_obj_use. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Acked-by: Mykyta Yatsenko <yatsenko@meta.com> Signed-off-by: Amery Hung <ameryhung@gmail.com> Link: https://lore.kernel.org/r/20260529014936.2811085-3-ameryhung@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent bab08d1 commit b5c0a07

2 files changed

Lines changed: 40 additions & 151 deletions

File tree

include/linux/bpf_verifier.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,13 @@ struct bpf_map_desc {
14381438
int uid;
14391439
};
14401440

1441+
/* The last initialized dynptr; Populated by process_dynptr_func() */
1442+
struct bpf_dynptr_desc {
1443+
enum bpf_dynptr_type type;
1444+
u32 id;
1445+
u32 ref_obj_id;
1446+
};
1447+
14411448
struct bpf_kfunc_call_arg_meta {
14421449
/* In parameters */
14431450
struct btf *btf;
@@ -1478,16 +1485,12 @@ struct bpf_kfunc_call_arg_meta {
14781485
struct {
14791486
struct btf_field *field;
14801487
} arg_rbtree_root;
1481-
struct {
1482-
enum bpf_dynptr_type type;
1483-
u32 id;
1484-
u32 ref_obj_id;
1485-
} initialized_dynptr;
14861488
struct {
14871489
u8 spi;
14881490
u8 frameno;
14891491
} iter;
14901492
struct bpf_map_desc map;
1493+
struct bpf_dynptr_desc dynptr;
14911494
u64 mem_size;
14921495
};
14931496

kernel/bpf/verifier.c

Lines changed: 32 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)
233233

234234
struct bpf_call_arg_meta {
235235
struct bpf_map_desc map;
236+
struct bpf_dynptr_desc dynptr;
236237
bool raw_mode;
237238
bool pkt_access;
238239
u8 release_regno;
@@ -241,7 +242,6 @@ struct bpf_call_arg_meta {
241242
int mem_size;
242243
u64 msize_max_value;
243244
int ref_obj_id;
244-
int dynptr_id;
245245
int func_id;
246246
struct btf *btf;
247247
u32 btf_id;
@@ -470,11 +470,6 @@ static bool is_ptr_cast_function(enum bpf_func_id func_id)
470470
func_id == BPF_FUNC_skc_to_tcp_request_sock;
471471
}
472472

473-
static bool is_dynptr_ref_function(enum bpf_func_id func_id)
474-
{
475-
return func_id == BPF_FUNC_dynptr_data;
476-
}
477-
478473
static bool is_sync_callback_calling_kfunc(u32 btf_id);
479474
static bool is_async_callback_calling_kfunc(u32 btf_id);
480475
static bool is_callback_calling_kfunc(u32 btf_id);
@@ -542,8 +537,6 @@ static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id,
542537
ref_obj_uses++;
543538
if (is_acquire_function(func_id, map))
544539
ref_obj_uses++;
545-
if (is_dynptr_ref_function(func_id))
546-
ref_obj_uses++;
547540

548541
return ref_obj_uses > 1;
549542
}
@@ -7221,8 +7214,9 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
72217214
* use case. The second level is tracked using the upper bit of bpf_dynptr->size
72227215
* and checked dynamically during runtime.
72237216
*/
7224-
static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno, int insn_idx,
7225-
enum bpf_arg_type arg_type, int clone_ref_obj_id)
7217+
static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
7218+
argno_t argno, int insn_idx, enum bpf_arg_type arg_type,
7219+
int clone_ref_obj_id, struct bpf_dynptr_desc *dynptr)
72267220
{
72277221
int spi, err = 0;
72287222

@@ -7287,6 +7281,8 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
72877281
}
72887282

72897283
if (reg->type != CONST_PTR_TO_DYNPTR) {
7284+
struct bpf_func_state *state = bpf_func(env, reg);
7285+
72907286
spi = dynptr_get_spi(env, reg);
72917287
if (spi < 0)
72927288
return spi;
@@ -7296,6 +7292,14 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
72967292
* in check_helper_call and mark_btf_func_reg_size in check_kfunc_call.
72977293
*/
72987294
mark_stack_slots_scratched(env, spi, BPF_DYNPTR_NR_SLOTS);
7295+
7296+
reg = &state->stack[spi].spilled_ptr;
7297+
}
7298+
7299+
if (dynptr) {
7300+
dynptr->type = reg->dynptr.type;
7301+
dynptr->id = reg->id;
7302+
dynptr->ref_obj_id = reg->ref_obj_id;
72997303
}
73007304
}
73017305
return err;
@@ -8065,72 +8069,6 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env,
80658069
}
80668070
}
80678071

8068-
static struct bpf_reg_state *get_dynptr_arg_reg(struct bpf_verifier_env *env,
8069-
const struct bpf_func_proto *fn,
8070-
struct bpf_reg_state *regs)
8071-
{
8072-
struct bpf_reg_state *state = NULL;
8073-
int i;
8074-
8075-
for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++)
8076-
if (arg_type_is_dynptr(fn->arg_type[i])) {
8077-
if (state) {
8078-
verbose(env, "verifier internal error: multiple dynptr args\n");
8079-
return NULL;
8080-
}
8081-
state = &regs[BPF_REG_1 + i];
8082-
}
8083-
8084-
if (!state)
8085-
verbose(env, "verifier internal error: no dynptr arg found\n");
8086-
8087-
return state;
8088-
}
8089-
8090-
static int dynptr_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
8091-
{
8092-
struct bpf_func_state *state = bpf_func(env, reg);
8093-
int spi;
8094-
8095-
if (reg->type == CONST_PTR_TO_DYNPTR)
8096-
return reg->id;
8097-
spi = dynptr_get_spi(env, reg);
8098-
if (spi < 0)
8099-
return spi;
8100-
return state->stack[spi].spilled_ptr.id;
8101-
}
8102-
8103-
static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
8104-
{
8105-
struct bpf_func_state *state = bpf_func(env, reg);
8106-
int spi;
8107-
8108-
if (reg->type == CONST_PTR_TO_DYNPTR)
8109-
return reg->ref_obj_id;
8110-
spi = dynptr_get_spi(env, reg);
8111-
if (spi < 0)
8112-
return spi;
8113-
return state->stack[spi].spilled_ptr.ref_obj_id;
8114-
}
8115-
8116-
static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,
8117-
struct bpf_reg_state *reg)
8118-
{
8119-
struct bpf_func_state *state = bpf_func(env, reg);
8120-
int spi;
8121-
8122-
if (reg->type == CONST_PTR_TO_DYNPTR)
8123-
return reg->dynptr.type;
8124-
8125-
spi = bpf_get_spi(reg->var_off.value);
8126-
if (spi < 0) {
8127-
verbose(env, "verifier internal error: invalid spi when querying dynptr type\n");
8128-
return BPF_DYNPTR_TYPE_INVALID;
8129-
}
8130-
8131-
return state->stack[spi].spilled_ptr.dynptr.type;
8132-
}
8133-
81348072
static int check_arg_const_str(struct bpf_verifier_env *env,
81358073
struct bpf_reg_state *reg, argno_t argno)
81368074
{
@@ -8488,7 +8426,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
84888426
true, meta);
84898427
break;
84908428
case ARG_PTR_TO_DYNPTR:
8491-
err = process_dynptr_func(env, reg, argno_from_reg(regno), insn_idx, arg_type, 0);
8429+
err = process_dynptr_func(env, reg, argno_from_reg(regno), insn_idx, arg_type, 0,
8430+
&meta->dynptr);
84928431
if (err)
84938432
return err;
84948433
break;
@@ -9170,7 +9109,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
91709109
if (ret)
91719110
return ret;
91729111

9173-
ret = process_dynptr_func(env, reg, argno, -1, arg->arg_type, 0);
9112+
ret = process_dynptr_func(env, reg, argno, -1, arg->arg_type, 0, NULL);
91749113
if (ret)
91759114
return ret;
91769115
} else if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
@@ -10278,52 +10217,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1027810217
}
1027910218
}
1028010219
break;
10281-
case BPF_FUNC_dynptr_data:
10282-
{
10283-
struct bpf_reg_state *reg;
10284-
int id, ref_obj_id;
10285-
10286-
reg = get_dynptr_arg_reg(env, fn, regs);
10287-
if (!reg)
10288-
return -EFAULT;
10289-
10290-
10291-
if (meta.dynptr_id) {
10292-
verifier_bug(env, "meta.dynptr_id already set");
10293-
return -EFAULT;
10294-
}
10295-
if (meta.ref_obj_id) {
10296-
verifier_bug(env, "meta.ref_obj_id already set");
10297-
return -EFAULT;
10298-
}
10299-
10300-
id = dynptr_id(env, reg);
10301-
if (id < 0) {
10302-
verifier_bug(env, "failed to obtain dynptr id");
10303-
return id;
10304-
}
10305-
10306-
ref_obj_id = dynptr_ref_obj_id(env, reg);
10307-
if (ref_obj_id < 0) {
10308-
verifier_bug(env, "failed to obtain dynptr ref_obj_id");
10309-
return ref_obj_id;
10310-
}
10311-
10312-
meta.dynptr_id = id;
10313-
meta.ref_obj_id = ref_obj_id;
10314-
10315-
break;
10316-
}
1031710220
case BPF_FUNC_dynptr_write:
1031810221
{
10319-
enum bpf_dynptr_type dynptr_type;
10320-
struct bpf_reg_state *reg;
10222+
enum bpf_dynptr_type dynptr_type = meta.dynptr.type;
1032110223

10322-
reg = get_dynptr_arg_reg(env, fn, regs);
10323-
if (!reg)
10324-
return -EFAULT;
10325-
10326-
dynptr_type = dynptr_get_type(env, reg);
1032710224
if (dynptr_type == BPF_DYNPTR_TYPE_INVALID)
1032810225
return -EFAULT;
1032910226

@@ -10515,10 +10412,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1051510412
return -EFAULT;
1051610413
}
1051710414

10518-
if (is_dynptr_ref_function(func_id))
10519-
regs[BPF_REG_0].dynptr_id = meta.dynptr_id;
10520-
10521-
if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) {
10415+
if (is_ptr_cast_function(func_id)) {
1052210416
/* For release_reference() */
1052310417
regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
1052410418
} else if (is_acquire_function(func_id, meta.map.ptr)) {
@@ -10532,6 +10426,11 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1053210426
regs[BPF_REG_0].ref_obj_id = id;
1053310427
}
1053410428

10429+
if (func_id == BPF_FUNC_dynptr_data) {
10430+
regs[BPF_REG_0].dynptr_id = meta.dynptr.id;
10431+
regs[BPF_REG_0].ref_obj_id = meta.dynptr.ref_obj_id;
10432+
}
10433+
1053510434
err = do_refine_retval_range(env, regs, fn->ret_type, func_id, &meta);
1053610435
if (err)
1053710436
return err;
@@ -12187,38 +12086,25 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1218712086
meta->release_regno = regno;
1218812087
} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_clone] &&
1218912088
(dynptr_arg_type & MEM_UNINIT)) {
12190-
enum bpf_dynptr_type parent_type = meta->initialized_dynptr.type;
12089+
enum bpf_dynptr_type parent_type = meta->dynptr.type;
1219112090

1219212091
if (parent_type == BPF_DYNPTR_TYPE_INVALID) {
1219312092
verifier_bug(env, "no dynptr type for parent of clone");
1219412093
return -EFAULT;
1219512094
}
1219612095

1219712096
dynptr_arg_type |= (unsigned int)get_dynptr_type_flag(parent_type);
12198-
clone_ref_obj_id = meta->initialized_dynptr.ref_obj_id;
12097+
clone_ref_obj_id = meta->dynptr.ref_obj_id;
1219912098
if (dynptr_type_refcounted(parent_type) && !clone_ref_obj_id) {
1220012099
verifier_bug(env, "missing ref obj id for parent of clone");
1220112100
return -EFAULT;
1220212101
}
1220312102
}
1220412103

12205-
ret = process_dynptr_func(env, reg, argno, insn_idx,
12206-
dynptr_arg_type, clone_ref_obj_id);
12104+
ret = process_dynptr_func(env, reg, argno, insn_idx, dynptr_arg_type,
12105+
clone_ref_obj_id, &meta->dynptr);
1220712106
if (ret < 0)
1220812107
return ret;
12209-
12210-
if (!(dynptr_arg_type & MEM_UNINIT)) {
12211-
int id = dynptr_id(env, reg);
12212-
12213-
if (id < 0) {
12214-
verifier_bug(env, "failed to obtain dynptr id");
12215-
return id;
12216-
}
12217-
meta->initialized_dynptr.id = id;
12218-
meta->initialized_dynptr.type = dynptr_get_type(env, reg);
12219-
meta->initialized_dynptr.ref_obj_id = dynptr_ref_obj_id(env, reg);
12220-
}
12221-
1222212108
break;
1222312109
}
1222412110
case KF_ARG_PTR_TO_ITER:
@@ -12849,7 +12735,7 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_ca
1284912735
}
1285012736
} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_slice] ||
1285112737
meta->func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) {
12852-
enum bpf_type_flag type_flag = get_dynptr_type_flag(meta->initialized_dynptr.type);
12738+
enum bpf_type_flag type_flag = get_dynptr_type_flag(meta->dynptr.type);
1285312739

1285412740
mark_reg_known_zero(env, regs, BPF_REG_0);
1285512741

@@ -12873,11 +12759,11 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_ca
1287312759
}
1287412760
}
1287512761

12876-
if (!meta->initialized_dynptr.id) {
12762+
if (!meta->dynptr.id) {
1287712763
verifier_bug(env, "no dynptr id");
1287812764
return -EFAULT;
1287912765
}
12880-
regs[BPF_REG_0].dynptr_id = meta->initialized_dynptr.id;
12766+
regs[BPF_REG_0].dynptr_id = meta->dynptr.id;
1288112767

1288212768
/* we don't need to set BPF_REG_0's ref obj id
1288312769
* because packet slices are not refcounted (see
@@ -13063,7 +12949,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1306312949
if (meta.release_regno) {
1306412950
struct bpf_reg_state *reg = &regs[meta.release_regno];
1306512951

13066-
if (meta.initialized_dynptr.ref_obj_id) {
12952+
if (meta.dynptr.ref_obj_id) {
1306712953
err = unmark_stack_slots_dynptr(env, reg);
1306812954
} else {
1306912955
err = release_reference(env, reg->ref_obj_id);

0 commit comments

Comments
 (0)