Skip to content

Commit b7dd2b3

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
bpf: Unify referenced object tracking in verifier
Helpers and kfuncs independently tracked referenced object metadata using standalone id fields in their respective arg_meta structs. This led to duplicated logic and inconsistent error handling between the two paths. Introduce struct ref_obj_desc to consolidate id and parent_id along with a count of how many arguments carry a reference. Add update_ref_obj() to populate it from a bpf_reg_state, replacing open-coded assignments in check_func_arg(), check_kfunc_args(), and process_iter_arg(). Add validate_ref_obj() to check for ambiguous ref_obj before using it. For ref_obj releasing helpers and kfuncs, keep checking it before calling update_ref_obj() for now. A later patch will make these functions not depending on ref_obj. For other users of ref_obj, move the checks to the use locations. For helper, this means moving the checks inside helper_multiple_ref_obj_use() to use locations. is_acquire_function() is dropped as ref_obj is never used. Pass ref_obj_desc into process_dynptr_func()/mark_stack_slots_dynptr() instead of a bare parent_id to make it less confusing. Drop the selftest introduced in 7ec899a ("selftests/bpf: Negative test case for ref_obj_id in args") since the verifier no longer complains about ambiguous ref_obj if it is not used. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Amery Hung <ameryhung@gmail.com> Link: https://lore.kernel.org/r/20260529014936.2811085-8-ameryhung@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 92d681b commit b7dd2b3

4 files changed

Lines changed: 78 additions & 93 deletions

File tree

include/linux/bpf_verifier.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,18 @@ struct bpf_dynptr_desc {
14241424
u32 parent_id;
14251425
};
14261426

1427+
/*
1428+
* The last seen rereferenced object; Updated by update_ref_obj() when a register refers to a
1429+
* referenced object. Used when the helper or kfunc is releasing a referenced object, casting
1430+
* a referenced object, returning allocated memory derived from referenced object or creating
1431+
* a dynptr with a referenced object as parent.
1432+
*/
1433+
struct ref_obj_desc {
1434+
u32 id;
1435+
u32 parent_id;
1436+
u8 cnt;
1437+
};
1438+
14271439
struct bpf_kfunc_call_arg_meta {
14281440
/* In parameters */
14291441
struct btf *btf;
@@ -1432,7 +1444,6 @@ struct bpf_kfunc_call_arg_meta {
14321444
const struct btf_type *func_proto;
14331445
const char *func_name;
14341446
/* Out parameters */
1435-
u32 id;
14361447
u8 release_regno;
14371448
bool r0_rdonly;
14381449
u32 ret_btf_id;
@@ -1470,6 +1481,7 @@ struct bpf_kfunc_call_arg_meta {
14701481
} iter;
14711482
struct bpf_map_desc map;
14721483
struct bpf_dynptr_desc dynptr;
1484+
struct ref_obj_desc ref_obj;
14731485
u64 mem_size;
14741486
};
14751487

kernel/bpf/verifier.c

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,35 @@ static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)
231231
(poisoned ? BPF_MAP_KEY_POISON : 0ULL);
232232
}
233233

234+
static void update_ref_obj(struct ref_obj_desc *ref_obj, struct bpf_reg_state *reg)
235+
{
236+
ref_obj->id = reg->id;
237+
ref_obj->parent_id = reg->parent_id;
238+
ref_obj->cnt++;
239+
}
240+
241+
static int validate_ref_obj(struct bpf_verifier_env *env, struct ref_obj_desc *ref_obj)
242+
{
243+
if (ref_obj->cnt > 1) {
244+
verifier_bug(env, "function expects only one referenced object but got %d\n",
245+
ref_obj->cnt);
246+
return -EFAULT;
247+
}
248+
249+
return 0;
250+
}
251+
234252
struct bpf_call_arg_meta {
235253
struct bpf_map_desc map;
236254
struct bpf_dynptr_desc dynptr;
255+
struct ref_obj_desc ref_obj;
237256
bool raw_mode;
238257
bool pkt_access;
239258
u8 release_regno;
240259
int regno;
241260
int access_size;
242261
int mem_size;
243262
u64 msize_max_value;
244-
u32 id;
245263
int func_id;
246264
struct btf *btf;
247265
u32 btf_id;
@@ -528,20 +546,6 @@ bool bpf_is_may_goto_insn(struct bpf_insn *insn)
528546
return insn->code == (BPF_JMP | BPF_JCOND) && insn->src_reg == BPF_MAY_GOTO;
529547
}
530548

531-
static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id,
532-
const struct bpf_map *map)
533-
{
534-
int ref_obj_uses = 0;
535-
536-
if (is_ptr_cast_function(func_id))
537-
ref_obj_uses++;
538-
if (is_acquire_function(func_id, map))
539-
ref_obj_uses++;
540-
541-
return ref_obj_uses > 1;
542-
}
543-
544-
545549
static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots)
546550
{
547551
int allocated_slots = state->allocated_stack / BPF_REG_SIZE;
@@ -670,11 +674,11 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
670674
struct bpf_func_state *state, int spi);
671675

672676
static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
673-
enum bpf_arg_type arg_type, int insn_idx, int parent_id,
674-
struct bpf_dynptr_desc *dynptr)
677+
enum bpf_arg_type arg_type, int insn_idx,
678+
struct ref_obj_desc *ref_obj, struct bpf_dynptr_desc *dynptr)
675679
{
676680
struct bpf_func_state *state = bpf_func(env, reg);
677-
int spi, i, err;
681+
int spi, i, err, parent_id = 0;
678682
enum bpf_dynptr_type type;
679683

680684
spi = dynptr_get_spi(env, reg);
@@ -707,6 +711,13 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
707711
return -EINVAL;
708712

709713
if (dynptr->type == BPF_DYNPTR_TYPE_INVALID) { /* dynptr constructors */
714+
err = validate_ref_obj(env, ref_obj);
715+
if (err)
716+
return err;
717+
718+
/* Track parent's id if the parent is a referenced object */
719+
parent_id = ref_obj->id;
720+
710721
if (dynptr_type_referenced(type)) {
711722
int id;
712723

@@ -7188,7 +7199,7 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
71887199
*/
71897200
static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
71907201
argno_t argno, int insn_idx, enum bpf_arg_type arg_type,
7191-
int parent_id, struct bpf_dynptr_desc *dynptr)
7202+
struct ref_obj_desc *ref_obj, struct bpf_dynptr_desc *dynptr)
71927203
{
71937204
int spi, err = 0;
71947205

@@ -7229,7 +7240,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
72297240
return err;
72307241
}
72317242

7232-
err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, parent_id, dynptr);
7243+
err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, ref_obj, dynptr);
72337244
} else /* OBJ_RELEASE and None case from above */ {
72347245
/* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */
72357246
if (reg->type == CONST_PTR_TO_DYNPTR && (arg_type & OBJ_RELEASE)) {
@@ -7277,13 +7288,6 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
72777288
return err;
72787289
}
72797290

7280-
static u32 iter_ref_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int spi)
7281-
{
7282-
struct bpf_func_state *state = bpf_func(env, reg);
7283-
7284-
return state->stack[spi].spilled_ptr.id;
7285-
}
7286-
72877291
static bool is_iter_kfunc(struct bpf_kfunc_call_arg_meta *meta)
72887292
{
72897293
return meta->kfunc_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY);
@@ -7316,6 +7320,7 @@ static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg_idx,
73167320
static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno, int insn_idx,
73177321
struct bpf_kfunc_call_arg_meta *meta)
73187322
{
7323+
struct bpf_func_state *state = bpf_func(env, reg);
73197324
const struct btf_type *t;
73207325
u32 arg_idx = arg_idx_from_argno(argno);
73217326
int spi, err, i, nr_slots, btf_id;
@@ -7387,7 +7392,7 @@ static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *
73877392
/* remember meta->iter info for process_iter_next_call() */
73887393
meta->iter.spi = spi;
73897394
meta->iter.frameno = reg->frameno;
7390-
meta->id = iter_ref_id(env, reg, spi);
7395+
update_ref_obj(&meta->ref_obj, &state->stack[spi].spilled_ptr);
73917396

73927397
if (is_iter_destroy_kfunc(meta)) {
73937398
err = unmark_stack_slots_iter(env, reg, nr_slots);
@@ -8166,6 +8171,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
81668171
u32 regno = BPF_REG_1 + arg;
81678172
struct bpf_reg_state *reg = reg_state(env, regno);
81688173
enum bpf_arg_type arg_type = fn->arg_type[arg];
8174+
argno_t argno = argno_from_arg(arg + 1);
81698175
enum bpf_reg_type type = reg->type;
81708176
u32 *arg_btf_id = NULL;
81718177
u32 key_size;
@@ -8232,15 +8238,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
82328238
meta->release_regno = regno;
82338239
}
82348240

8235-
if (reg_is_referenced(env, reg) && base_type(arg_type) != ARG_KPTR_XCHG_DEST) {
8236-
if (meta->id) {
8237-
verbose(env, "more than one arg with referenced id R%d %u %u",
8238-
regno, reg->id,
8239-
meta->id);
8240-
return -EACCES;
8241-
}
8242-
meta->id = reg->id;
8243-
}
8241+
if (reg_is_referenced(env, reg))
8242+
update_ref_obj(&meta->ref_obj, reg);
82448243

82458244
switch (base_type(arg_type)) {
82468245
case ARG_CONST_MAP_PTR:
@@ -8379,7 +8378,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
83798378
true, meta);
83808379
break;
83818380
case ARG_PTR_TO_DYNPTR:
8382-
err = process_dynptr_func(env, reg, argno_from_reg(regno), insn_idx, arg_type, 0,
8381+
err = process_dynptr_func(env, reg, argno_from_reg(regno), insn_idx, arg_type, &meta->ref_obj,
83838382
&meta->dynptr);
83848383
if (err)
83858384
return err;
@@ -9042,6 +9041,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
90429041
struct bpf_subprog_info *sub = subprog_info(env, subprog);
90439042
struct bpf_func_state *caller = cur_func(env);
90449043
struct bpf_verifier_log *log = &env->log;
9044+
struct ref_obj_desc ref_obj = {};
90459045
u32 i;
90469046
int ret, err;
90479047

@@ -9119,7 +9119,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
91199119
if (ret)
91209120
return ret;
91219121

9122-
ret = process_dynptr_func(env, reg, argno, -1, arg->arg_type, 0, NULL);
9122+
ret = process_dynptr_func(env, reg, argno, -1, arg->arg_type, &ref_obj, NULL);
91239123
if (ret)
91249124
return ret;
91259125
} else if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
@@ -10125,8 +10125,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1012510125
err = -EINVAL;
1012610126
if (arg_type_is_dynptr(fn->arg_type[meta.release_regno - BPF_REG_1])) {
1012710127
err = unmark_stack_slots_dynptr(env, &regs[meta.release_regno]);
10128-
} else if (func_id == BPF_FUNC_kptr_xchg && meta.id) {
10129-
u32 id = meta.id;
10128+
} else if (func_id == BPF_FUNC_kptr_xchg && meta.ref_obj.id) {
10129+
u32 id = meta.ref_obj.id;
1013010130
bool in_rcu = in_rcu_cs(env);
1013110131
struct bpf_func_state *state;
1013210132
struct bpf_reg_state *reg;
@@ -10145,10 +10145,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1014510145
}
1014610146
}));
1014710147
}
10148-
} else if (meta.id) {
10149-
err = release_reference(env, meta.id);
10148+
} else if (meta.ref_obj.id) {
10149+
err = release_reference(env, meta.ref_obj.id);
1015010150
} else if (bpf_register_is_null(&regs[meta.release_regno])) {
10151-
/* meta.id can only be 0 if register that is meant to be
10151+
/* meta.ref_obj.id can only be 0 if register that is meant to be
1015210152
* released is NULL, which must be > R0.
1015310153
*/
1015410154
err = 0;
@@ -10413,17 +10413,15 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1041310413
if (type_may_be_null(regs[BPF_REG_0].type))
1041410414
regs[BPF_REG_0].id = ++env->id_gen;
1041510415

10416-
if (helper_multiple_ref_obj_use(func_id, meta.map.ptr)) {
10417-
verifier_bug(env, "func %s#%d sets ref_obj_id more than once",
10418-
func_id_name(func_id), func_id);
10419-
return -EFAULT;
10420-
}
10421-
1042210416
if (is_ptr_cast_function(func_id) &&
10423-
find_reference_state(env->cur_state, meta.id)) {
10417+
find_reference_state(env->cur_state, meta.ref_obj.id)) {
1042410418
struct bpf_verifier_state *branch;
1042510419
struct bpf_reg_state *r0;
1042610420

10421+
err = validate_ref_obj(env, &meta.ref_obj);
10422+
if (err)
10423+
return err;
10424+
1042710425
/*
1042810426
* In order for a release of any of the original or cast pointers
1042910427
* to invalidate all other pointers, reuse the same reference id for
@@ -10441,7 +10439,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1044110439
r0->type = SCALAR_VALUE;
1044210440

1044310441
regs[BPF_REG_0].type &= ~PTR_MAYBE_NULL;
10444-
regs[BPF_REG_0].id = meta.id;
10442+
regs[BPF_REG_0].id = meta.ref_obj.id;
1044510443
} else if (is_acquire_function(func_id, meta.map.ptr)) {
1044610444
int id = acquire_reference(env, insn_idx, 0);
1044710445

@@ -11915,13 +11913,13 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1191511913
}
1191611914

1191711915
if (reg_is_referenced(env, reg)) {
11918-
if (is_kfunc_release(meta) && meta->id) {
11919-
verifier_bug(env, "more than one arg with referenced id %s %u %u",
11920-
reg_arg_name(env, argno), reg->id,
11921-
meta->id);
11916+
if (is_kfunc_release(meta) && meta->ref_obj.cnt) {
11917+
verbose(env, "more than one arg with referenced id %s %u %u",
11918+
reg_arg_name(env, argno), reg->id,
11919+
meta->ref_obj.id);
1192211920
return -EFAULT;
1192311921
}
11924-
meta->id = reg->id;
11922+
update_ref_obj(&meta->ref_obj, reg);
1192511923
if (is_kfunc_release(meta)) {
1192611924
if (regno < 0) {
1192711925
verbose(env, "%s release arg cannot be a stack argument\n",
@@ -12104,7 +12102,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1210412102
}
1210512103

1210612104
ret = process_dynptr_func(env, reg, argno, insn_idx, dynptr_arg_type,
12107-
meta->id, &meta->dynptr);
12105+
&meta->ref_obj, &meta->dynptr);
1210812106
if (ret < 0)
1210912107
return ret;
1211012108
break;
@@ -13048,8 +13046,12 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1304813046
regs[BPF_REG_0].type |= MEM_RDONLY;
1304913047

1305013048
/* Ensures we don't access the memory after a release_reference() */
13051-
if (meta.id)
13052-
regs[BPF_REG_0].parent_id = meta.id;
13049+
if (meta.ref_obj.id) {
13050+
err = validate_ref_obj(env, &meta.ref_obj);
13051+
if (err)
13052+
return err;
13053+
regs[BPF_REG_0].parent_id = meta.ref_obj.id;
13054+
}
1305313055

1305413056
if (is_kfunc_rcu_protected(&meta))
1305513057
regs[BPF_REG_0].type |= MEM_RCU;

tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SEC("fentry/" SYS_PREFIX "sys_getpgid")
3535
int test_ringbuf_mem_map_key(void *ctx)
3636
{
3737
int cur_pid = bpf_get_current_pid_tgid() >> 32;
38-
struct sample *sample, sample_copy;
38+
struct sample *sample;
3939
int *lookup_val;
4040

4141
if (cur_pid != pid)
@@ -55,16 +55,11 @@ int test_ringbuf_mem_map_key(void *ctx)
5555
lookup_val = (int *)bpf_map_lookup_elem(&hash_map, sample);
5656
__sink(lookup_val);
5757

58-
/* workaround - memcpy is necessary so that verifier doesn't
59-
* complain with:
60-
* verifier internal error: more than one arg with ref_obj_id R3
61-
* when trying to do bpf_map_update_elem(&hash_map, sample, &sample->seq, BPF_ANY);
62-
*
58+
/*
6359
* Since bpf_map_lookup_elem above uses 'sample' as key, test using
6460
* sample field as value below
6561
*/
66-
__builtin_memcpy(&sample_copy, sample, sizeof(struct sample));
67-
bpf_map_update_elem(&hash_map, &sample_copy, &sample->seq, BPF_ANY);
62+
bpf_map_update_elem(&hash_map, sample, &sample->seq, BPF_ANY);
6863

6964
bpf_ringbuf_submit(sample, 0);
7065
return 0;

tools/testing/selftests/bpf/verifier/calls.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,27 +2410,3 @@
24102410
.errstr_unpriv = "",
24112411
.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
24122412
},
2413-
{
2414-
"calls: several args with ref_obj_id",
2415-
.insns = {
2416-
/* Reserve at least sizeof(struct iphdr) bytes in the ring buffer.
2417-
* With a smaller size, the verifier would reject the call to
2418-
* bpf_tcp_raw_gen_syncookie_ipv4 before we can reach the
2419-
* ref_obj_id error.
2420-
*/
2421-
BPF_MOV64_IMM(BPF_REG_2, 20),
2422-
BPF_MOV64_IMM(BPF_REG_3, 0),
2423-
BPF_LD_MAP_FD(BPF_REG_1, 0),
2424-
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_ringbuf_reserve),
2425-
/* if r0 == 0 goto <exit> */
2426-
BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
2427-
BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
2428-
BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
2429-
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_tcp_raw_gen_syncookie_ipv4),
2430-
BPF_EXIT_INSN(),
2431-
},
2432-
.fixup_map_ringbuf = { 2 },
2433-
.result = REJECT,
2434-
.errstr = "more than one arg with ref_obj_id",
2435-
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
2436-
},

0 commit comments

Comments
 (0)