Skip to content

Commit e6be8cd

Browse files
ThinkerYzu1Martin KaFai Lau
authored andcommitted
bpf: Fix error checks against bpf_get_btf_vmlinux().
In bpf_struct_ops_map_alloc, it needs to check for NULL in the returned pointer of bpf_get_btf_vmlinux() when CONFIG_DEBUG_INFO_BTF is not set. ENOTSUPP is used to preserve the same behavior before the struct_ops kmod support. In the function check_struct_ops_btf_id(), instead of redoing the bpf_get_btf_vmlinux() that has already been done in syscall.c, the fix here is to check for prog->aux->attach_btf_id. BPF_PROG_TYPE_STRUCT_OPS must require attach_btf_id and syscall.c guarantees a valid attach_btf as long as attach_btf_id is set. When attach_btf_id is not set, this patch returns -ENOTSUPP because it is what the selftest in test_libbpf_probe_prog_types() and libbpf_probes.c are expecting for feature probing purpose. Changes from v1: - Remove an unnecessary NULL check in check_struct_ops_btf_id() Reported-by: [email protected] Closes: https://lore.kernel.org/bpf/[email protected]/ Reported-by: [email protected] Closes: https://lore.kernel.org/bpf/[email protected]/ Fixes: fcc2c1f ("bpf: pass attached BTF to the bpf_struct_ops subsystem") Signed-off-by: Kui-Feng Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent be4840b commit e6be8cd

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

kernel/bpf/bpf_struct_ops.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
669669
btf = bpf_get_btf_vmlinux();
670670
if (IS_ERR(btf))
671671
return ERR_CAST(btf);
672+
if (!btf)
673+
return ERR_PTR(-ENOTSUPP);
672674
}
673675

674676
st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);

kernel/bpf/verifier.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20298,7 +20298,10 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
2029820298
return -EINVAL;
2029920299
}
2030020300

20301-
btf = prog->aux->attach_btf ?: bpf_get_btf_vmlinux();
20301+
if (!prog->aux->attach_btf_id)
20302+
return -ENOTSUPP;
20303+
20304+
btf = prog->aux->attach_btf;
2030220305
if (btf_is_module(btf)) {
2030320306
/* Make sure st_ops is valid through the lifetime of env */
2030420307
env->attach_btf_mod = btf_try_get_module(btf);

0 commit comments

Comments
 (0)