Skip to content

Commit 8f14852

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Tag argument to be released in bpf_func_proto
Add a new type flag for bpf_arg_type that when set tells verifier that for a release function, that argument's register will be the one for which meta.ref_obj_id will be set, and which will then be released using release_reference. To capture the regno, introduce a new field release_regno in bpf_call_arg_meta. This would be required in the next patch, where we may either pass NULL or a refcounted pointer as an argument to the release function bpf_kptr_xchg. Just releasing only when meta.ref_obj_id is set is not enough, as there is a case where the type of argument needed matches, but the ref_obj_id is set to 0. Hence, we must enforce that whenever meta.ref_obj_id is zero, the register that is to be released can only be NULL for a release function. Since we now indicate whether an argument is to be released in bpf_func_proto itself, is_release_function helper has lost its utitlity, hence refactor code to work without it, and just rely on meta.release_regno to know when to release state for a ref_obj_id. Still, the restriction of one release argument and only one ref_obj_id passed to BPF helper or kfunc remains. This may be lifted in the future. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 61df10c commit 8f14852

File tree

8 files changed

+60
-49
lines changed

8 files changed

+60
-49
lines changed

include/linux/bpf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ enum bpf_type_flag {
366366
*/
367367
MEM_PERCPU = BIT(4 + BPF_BASE_TYPE_BITS),
368368

369-
__BPF_TYPE_LAST_FLAG = MEM_PERCPU,
369+
/* Indicates that the argument will be released. */
370+
OBJ_RELEASE = BIT(5 + BPF_BASE_TYPE_BITS),
371+
372+
__BPF_TYPE_LAST_FLAG = OBJ_RELEASE,
370373
};
371374

372375
/* Max number of base types. */

include/linux/bpf_verifier.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,7 @@ int check_ptr_off_reg(struct bpf_verifier_env *env,
523523
const struct bpf_reg_state *reg, int regno);
524524
int check_func_arg_reg_off(struct bpf_verifier_env *env,
525525
const struct bpf_reg_state *reg, int regno,
526-
enum bpf_arg_type arg_type,
527-
bool is_release_func);
526+
enum bpf_arg_type arg_type);
528527
int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
529528
u32 regno);
530529
int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,

kernel/bpf/btf.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6047,6 +6047,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
60476047
* verifier sees.
60486048
*/
60496049
for (i = 0; i < nargs; i++) {
6050+
enum bpf_arg_type arg_type = ARG_DONTCARE;
60506051
u32 regno = i + 1;
60516052
struct bpf_reg_state *reg = &regs[regno];
60526053

@@ -6067,7 +6068,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
60676068
ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
60686069
ref_tname = btf_name_by_offset(btf, ref_t->name_off);
60696070

6070-
ret = check_func_arg_reg_off(env, reg, regno, ARG_DONTCARE, rel);
6071+
if (rel && reg->ref_obj_id)
6072+
arg_type |= OBJ_RELEASE;
6073+
ret = check_func_arg_reg_off(env, reg, regno, arg_type);
60716074
if (ret < 0)
60726075
return ret;
60736076

@@ -6099,11 +6102,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
60996102
if (reg->type == PTR_TO_BTF_ID) {
61006103
reg_btf = reg->btf;
61016104
reg_ref_id = reg->btf_id;
6102-
/* Ensure only one argument is referenced
6103-
* PTR_TO_BTF_ID, check_func_arg_reg_off relies
6104-
* on only one referenced register being allowed
6105-
* for kfuncs.
6106-
*/
6105+
/* Ensure only one argument is referenced PTR_TO_BTF_ID */
61076106
if (reg->ref_obj_id) {
61086107
if (ref_obj_id) {
61096108
bpf_log(log, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",

kernel/bpf/ringbuf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
404404
const struct bpf_func_proto bpf_ringbuf_submit_proto = {
405405
.func = bpf_ringbuf_submit,
406406
.ret_type = RET_VOID,
407-
.arg1_type = ARG_PTR_TO_ALLOC_MEM,
407+
.arg1_type = ARG_PTR_TO_ALLOC_MEM | OBJ_RELEASE,
408408
.arg2_type = ARG_ANYTHING,
409409
};
410410

@@ -417,7 +417,7 @@ BPF_CALL_2(bpf_ringbuf_discard, void *, sample, u64, flags)
417417
const struct bpf_func_proto bpf_ringbuf_discard_proto = {
418418
.func = bpf_ringbuf_discard,
419419
.ret_type = RET_VOID,
420-
.arg1_type = ARG_PTR_TO_ALLOC_MEM,
420+
.arg1_type = ARG_PTR_TO_ALLOC_MEM | OBJ_RELEASE,
421421
.arg2_type = ARG_ANYTHING,
422422
};
423423

kernel/bpf/verifier.c

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ struct bpf_call_arg_meta {
245245
struct bpf_map *map_ptr;
246246
bool raw_mode;
247247
bool pkt_access;
248+
u8 release_regno;
248249
int regno;
249250
int access_size;
250251
int mem_size;
@@ -471,17 +472,6 @@ static bool type_may_be_null(u32 type)
471472
return type & PTR_MAYBE_NULL;
472473
}
473474

474-
/* Determine whether the function releases some resources allocated by another
475-
* function call. The first reference type argument will be assumed to be
476-
* released by release_reference().
477-
*/
478-
static bool is_release_function(enum bpf_func_id func_id)
479-
{
480-
return func_id == BPF_FUNC_sk_release ||
481-
func_id == BPF_FUNC_ringbuf_submit ||
482-
func_id == BPF_FUNC_ringbuf_discard;
483-
}
484-
485475
static bool may_be_acquire_function(enum bpf_func_id func_id)
486476
{
487477
return func_id == BPF_FUNC_sk_lookup_tcp ||
@@ -5326,6 +5316,11 @@ static bool arg_type_is_int_ptr(enum bpf_arg_type type)
53265316
type == ARG_PTR_TO_LONG;
53275317
}
53285318

5319+
static bool arg_type_is_release(enum bpf_arg_type type)
5320+
{
5321+
return type & OBJ_RELEASE;
5322+
}
5323+
53295324
static int int_ptr_type_to_size(enum bpf_arg_type type)
53305325
{
53315326
if (type == ARG_PTR_TO_INT)
@@ -5536,11 +5531,10 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
55365531

55375532
int check_func_arg_reg_off(struct bpf_verifier_env *env,
55385533
const struct bpf_reg_state *reg, int regno,
5539-
enum bpf_arg_type arg_type,
5540-
bool is_release_func)
5534+
enum bpf_arg_type arg_type)
55415535
{
5542-
bool fixed_off_ok = false, release_reg;
55435536
enum bpf_reg_type type = reg->type;
5537+
bool fixed_off_ok = false;
55445538

55455539
switch ((u32)type) {
55465540
case SCALAR_VALUE:
@@ -5558,27 +5552,25 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env,
55585552
/* Some of the argument types nevertheless require a
55595553
* zero register offset.
55605554
*/
5561-
if (arg_type != ARG_PTR_TO_ALLOC_MEM)
5555+
if (base_type(arg_type) != ARG_PTR_TO_ALLOC_MEM)
55625556
return 0;
55635557
break;
55645558
/* All the rest must be rejected, except PTR_TO_BTF_ID which allows
55655559
* fixed offset.
55665560
*/
55675561
case PTR_TO_BTF_ID:
55685562
/* When referenced PTR_TO_BTF_ID is passed to release function,
5569-
* it's fixed offset must be 0. We rely on the property that
5570-
* only one referenced register can be passed to BPF helpers and
5571-
* kfuncs. In the other cases, fixed offset can be non-zero.
5563+
* it's fixed offset must be 0. In the other cases, fixed offset
5564+
* can be non-zero.
55725565
*/
5573-
release_reg = is_release_func && reg->ref_obj_id;
5574-
if (release_reg && reg->off) {
5566+
if (arg_type_is_release(arg_type) && reg->off) {
55755567
verbose(env, "R%d must have zero offset when passed to release func\n",
55765568
regno);
55775569
return -EINVAL;
55785570
}
5579-
/* For release_reg == true, fixed_off_ok must be false, but we
5580-
* already checked and rejected reg->off != 0 above, so set to
5581-
* true to allow fixed offset for all other cases.
5571+
/* For arg is release pointer, fixed_off_ok must be false, but
5572+
* we already checked and rejected reg->off != 0 above, so set
5573+
* to true to allow fixed offset for all other cases.
55825574
*/
55835575
fixed_off_ok = true;
55845576
break;
@@ -5637,14 +5629,24 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
56375629
if (err)
56385630
return err;
56395631

5640-
err = check_func_arg_reg_off(env, reg, regno, arg_type, is_release_function(meta->func_id));
5632+
err = check_func_arg_reg_off(env, reg, regno, arg_type);
56415633
if (err)
56425634
return err;
56435635

56445636
skip_type_check:
5645-
/* check_func_arg_reg_off relies on only one referenced register being
5646-
* allowed for BPF helpers.
5647-
*/
5637+
if (arg_type_is_release(arg_type)) {
5638+
if (!reg->ref_obj_id && !register_is_null(reg)) {
5639+
verbose(env, "R%d must be referenced when passed to release function\n",
5640+
regno);
5641+
return -EINVAL;
5642+
}
5643+
if (meta->release_regno) {
5644+
verbose(env, "verifier internal error: more than one release argument\n");
5645+
return -EFAULT;
5646+
}
5647+
meta->release_regno = regno;
5648+
}
5649+
56485650
if (reg->ref_obj_id) {
56495651
if (meta->ref_obj_id) {
56505652
verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
@@ -6151,7 +6153,8 @@ static bool check_btf_id_ok(const struct bpf_func_proto *fn)
61516153
return true;
61526154
}
61536155

6154-
static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
6156+
static int check_func_proto(const struct bpf_func_proto *fn, int func_id,
6157+
struct bpf_call_arg_meta *meta)
61556158
{
61566159
return check_raw_mode_ok(fn) &&
61576160
check_arg_pair_ok(fn) &&
@@ -6835,7 +6838,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
68356838
memset(&meta, 0, sizeof(meta));
68366839
meta.pkt_access = fn->pkt_access;
68376840

6838-
err = check_func_proto(fn, func_id);
6841+
err = check_func_proto(fn, func_id, &meta);
68396842
if (err) {
68406843
verbose(env, "kernel subsystem misconfigured func %s#%d\n",
68416844
func_id_name(func_id), func_id);
@@ -6868,17 +6871,24 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
68686871
return err;
68696872
}
68706873

6871-
if (is_release_function(func_id)) {
6872-
err = release_reference(env, meta.ref_obj_id);
6874+
regs = cur_regs(env);
6875+
6876+
if (meta.release_regno) {
6877+
err = -EINVAL;
6878+
if (meta.ref_obj_id)
6879+
err = release_reference(env, meta.ref_obj_id);
6880+
/* meta.ref_obj_id can only be 0 if register that is meant to be
6881+
* released is NULL, which must be > R0.
6882+
*/
6883+
else if (register_is_null(&regs[meta.release_regno]))
6884+
err = 0;
68736885
if (err) {
68746886
verbose(env, "func %s#%d reference has not been acquired before\n",
68756887
func_id_name(func_id), func_id);
68766888
return err;
68776889
}
68786890
}
68796891

6880-
regs = cur_regs(env);
6881-
68826892
switch (func_id) {
68836893
case BPF_FUNC_tail_call:
68846894
err = check_reference_leak(env);

net/core/filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6621,7 +6621,7 @@ static const struct bpf_func_proto bpf_sk_release_proto = {
66216621
.func = bpf_sk_release,
66226622
.gpl_only = false,
66236623
.ret_type = RET_INTEGER,
6624-
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6624+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE,
66256625
};
66266626

66276627
BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@
796796
},
797797
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
798798
.result = REJECT,
799-
.errstr = "reference has not been acquired before",
799+
.errstr = "R1 must be referenced when passed to release function",
800800
},
801801
{
802802
/* !bpf_sk_fullsock(sk) is checked but !bpf_tcp_sock(sk) is not checked */

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
},
418418
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
419419
.result = REJECT,
420-
.errstr = "reference has not been acquired before",
420+
.errstr = "R1 must be referenced when passed to release function",
421421
},
422422
{
423423
"bpf_sk_release(bpf_sk_fullsock(skb->sk))",
@@ -436,7 +436,7 @@
436436
},
437437
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
438438
.result = REJECT,
439-
.errstr = "reference has not been acquired before",
439+
.errstr = "R1 must be referenced when passed to release function",
440440
},
441441
{
442442
"bpf_sk_release(bpf_tcp_sock(skb->sk))",
@@ -455,7 +455,7 @@
455455
},
456456
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
457457
.result = REJECT,
458-
.errstr = "reference has not been acquired before",
458+
.errstr = "R1 must be referenced when passed to release function",
459459
},
460460
{
461461
"sk_storage_get(map, skb->sk, NULL, 0): value == NULL",

0 commit comments

Comments
 (0)