A program can be loaded with the BTF of a module and the ID of a vmlinux function. It then attaches to the function of the same name in the module, but with the prototype of the vmlinux one. When the two prototypes differ, the program reads the arguments as wrong types. And if the module function takes more arguments, the trampoline doesn't save them, so the BPF programs can clobber them. Refuse the ID of a vmlinux function with the BTF of a module when the module has its own function of that name, which might have another prototype. A module function of the same prototype usually has no ID of its own, as the dedup of BTF merges it into the vmlinux one, and is still attached to with the vmlinux ID. Fixes: 290248a5b7d8 ("bpf: Allow to specify kernel module BTFs when attaching BPF programs") Signed-off-by: Anton Protopopov --- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++-------- kernel/bpf/syscall.c | 14 ++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 6014282feacb..d82efac8a4d0 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -254,6 +254,8 @@ int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec); bool btf_type_is_void(const struct btf_type *t); bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t); s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); +bool btf_is_base_func(const struct btf *btf, u32 id); +bool btf_has_own_func(const struct btf *btf, u32 id); s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); struct btf *btf_get_module_btf(const struct module *module); __u32 btf_relocate_id(const struct btf *btf, __u32 id); diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 3597ba1b5594..12cf8b9c7d3c 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -664,19 +664,13 @@ static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char *name return btf_nr_types(btf); } -s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) +/* Look up only the types of btf, not of its base */ +static s32 btf_find_by_name_kind_own(const struct btf *btf, const char *name, u8 kind) { - const struct btf *base_btf = btf_base_btf(btf); const struct btf_type *t; const char *tname; s32 id, total; - if (base_btf) { - id = btf_find_by_name_kind(base_btf, name, kind); - if (id > 0) - return id; - } - total = btf_nr_types(btf); if (btf->named_start_id > 0 && name[0]) { id = btf_find_by_name_kind_bsearch(btf, name); @@ -707,6 +701,36 @@ s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) return -ENOENT; } +s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) +{ + const struct btf *base_btf = btf_base_btf(btf); + s32 id; + + if (base_btf) { + id = btf_find_by_name_kind(base_btf, name, kind); + if (id > 0) + return id; + } + + return btf_find_by_name_kind_own(btf, name, kind); +} + +/* Whether id is a function of the base of the split btf */ +bool btf_is_base_func(const struct btf *btf, u32 id) +{ + return btf->base_btf && id < btf->start_id && + btf_type_is_func(btf_type_by_id(btf, id)); +} + +/* Whether btf has a function of its own with the name of the type id */ +bool btf_has_own_func(const struct btf *btf, u32 id) +{ + const struct btf_type *t = btf_type_by_id(btf, id); + + return t && btf_find_by_name_kind_own(btf, btf_name_by_offset(btf, t->name_off), + BTF_KIND_FUNC) > 0; +} + s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p) { struct btf *btf; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index b9cd3d50dee5..e0a04f64f2b1 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3069,6 +3069,20 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr goto put_token; } } + /* + * The target is looked up by name in the module, so a vmlinux + * function might have the wrong prototype for it. Take it only + * if the module has no function of its own of that name: the + * dedup of BTF merges a module function into the vmlinux one + * of the same prototype. + */ + if (btf_is_module(attach_btf) && + btf_is_base_func(attach_btf, attach_btf_id) && + btf_has_own_func(attach_btf, attach_btf_id)) { + btf_put(attach_btf); + err = -EINVAL; + goto put_token; + } } else if (dst_prog->aux->is_entry && dst_prog->aux->func_info[0].type_id != attr->attach_btf_id) { /* what an entry calls is a function of its owner */ -- 2.43.0