Skip to content

Commit a1ff4e3

Browse files
jeromemarchandJiri Benc
authored andcommitted
bpf: Fix struct_meta lookup for bpf_obj_free_fields kfunc call
Bugzilla: https://bugzilla.redhat.com/2177177 Conflicts: Change in data structure from the missing commit 07236ea ("bpf: factor out fetching basic kfunc metadata"). Unfortunately backporting 07236ea wouldn't be a clean backport either unless several other patches would be backported too. commit f6a6a5a Author: Dave Marchevsky <davemarchevsky@fb.com> Date: Mon Apr 3 13:00:27 2023 -0700 bpf: Fix struct_meta lookup for bpf_obj_free_fields kfunc call bpf_obj_drop_impl has a void return type. In check_kfunc_call, the "else if" which sets insn_aux->kptr_struct_meta for bpf_obj_drop_impl is surrounded by a larger if statement which checks btf_type_is_ptr. As a result: * The bpf_obj_drop_impl-specific code will never execute * The btf_struct_meta input to bpf_obj_drop is always NULL * __bpf_obj_drop_impl will always see a NULL btf_record when called from BPF program, and won't call bpf_obj_free_fields * program-allocated kptrs which have fields that should be cleaned up by bpf_obj_free_fields may instead leak resources This patch adds a btf_type_is_void branch to the larger if and moves special handling for bpf_obj_drop_impl there, fixing the issue. Fixes: ac9f060 ("bpf: Introduce bpf_obj_drop") Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com> Link: https://lore.kernel.org/r/20230403200027.2271029-1-davemarchevsky@fb.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
1 parent 3ab477d commit a1ff4e3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

kernel/bpf/verifier.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9238,10 +9238,6 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
92389238
env->insn_aux_data[insn_idx].obj_new_size = ret_t->size;
92399239
env->insn_aux_data[insn_idx].kptr_struct_meta =
92409240
btf_find_struct_meta(ret_btf, ret_btf_id);
9241-
} else if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) {
9242-
env->insn_aux_data[insn_idx].kptr_struct_meta =
9243-
btf_find_struct_meta(meta.arg_obj_drop.btf,
9244-
meta.arg_obj_drop.btf_id);
92459241
} else if (meta.func_id == special_kfunc_list[KF_bpf_list_pop_front] ||
92469242
meta.func_id == special_kfunc_list[KF_bpf_list_pop_back]) {
92479243
struct btf_field *field = meta.arg_list_head.field;
@@ -9319,7 +9315,15 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
93199315
}
93209316
if (reg_may_point_to_spin_lock(&regs[BPF_REG_0]) && !regs[BPF_REG_0].id)
93219317
regs[BPF_REG_0].id = ++env->id_gen;
9322-
} /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */
9318+
} else if (btf_type_is_void(t)) {
9319+
if (meta.btf == btf_vmlinux && btf_id_set_contains(&special_kfunc_set, meta.func_id)) {
9320+
if (meta.func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) {
9321+
env->insn_aux_data[insn_idx].kptr_struct_meta =
9322+
btf_find_struct_meta(meta.arg_obj_drop.btf,
9323+
meta.arg_obj_drop.btf_id);
9324+
}
9325+
}
9326+
}
93239327

93249328
nargs = btf_type_vlen(func_proto);
93259329
args = (const struct btf_param *)(func_proto + 1);

0 commit comments

Comments
 (0)