bpf: Refuse the vmlinux prototype for a module function
Subsystems (6)
cover bpf: Refuse the vmlinux prototype for a module function
A tracing program can be loaded with the BTF of a module and the ID of a vmlinux function. The kernel then attaches it to the module function but with the vmlinux prototype. If the two prototypes differ the program reads the arguments as wrong types. Even worse, if the module function takes more arguments the trampoline doesn't save them. A tracing multi link has the same problem with its IDs. And libbpf makes such pairs itself for "fentry/mod:func" and for module globs, since it searches the types of vmlinux first.
Patches 1 and 2 refuse the ID of a vmlinux function with the BTF of a module if the module has its own function of that name. This is checked at load and at the creation of a tracing multi link. Patch 3 makes libbpf look up the functions of a module first. Patch 4 tests the kernel and libbpf.
A simpler check "with the BTF of a module the ID must be a module type" doesn't work, because pahole and resolve_btfids dedup the BTF of a module against vmlinux and a module function with the name and the prototype of a vmlinux function is merged into the vmlinux FUNC. The vmlinux ID is then the only way to attach to it. Some selftests rely on this.
One case stays open: a module function which pahole leaves out of BTF and which has the name of a vmlinux function of another prototype. pahole drops a function whose prototype doesn't match how it is compiled or whose name has several prototypes in the module. Its symbol is still in kallsyms and the module has no FUNC to tell it by, so the kernel takes the vmlinux ID and uses the wrong prototype.
And one case which worked before is now refused: a caller which passes the vmlinux ID while the module has its own FUNC of a compatible prototype, e.g. one with other parameter names, which dedup doesn't merge. libbpf with patch 3 picks the module's own ID. Other loaders have to do the same. Such functions are rare. In a stock Ubuntu 7.0 arm64 kernel six module functions have their own FUNC with the name of a vmlinux function. Four of them have another prototype and are the bug which patches 1 and 2 fix. Only two, ghash_setkey() and ghash_update() of ghash_ce, differ just in parameter names.
Both cases go away if btf__dedup() stops merging the FUNCs of a module into the FUNCs of vmlinux. A FUNC stands for a symbol of one object, so the two aren't the same type even when they look equal. Then the kernel can refuse any vmlinux function ID with the BTF of a module. This needs a change in btf__dedup() for split BTF and a pahole which picks it up. I'm looking for comments on whether this follow-up is ok.
Patches 1-3 have Fixes tags, however the bug is old and needs CAP_BPF and CAP_PERFMON, so I am sending this to bpf-next.
Anton Protopopov (4):
bpf: Refuse the vmlinux prototype for a module function
bpf: Refuse the vmlinux prototype for a module function in multi links
libbpf: Look up the functions of a module first selftests/bpf: Test a vmlinux function ID with the BTF of a module
include/linux/btf.h | 2 + kernel/bpf/btf.c | 40 ++++++-- kernel/bpf/syscall.c | 14 +++ kernel/bpf/verifier.c | 6 ++ tools/lib/bpf/libbpf.c | 73 +++++++++----- .../selftests/bpf/prog_tests/module_attach.c | 11 +++ .../bpf/prog_tests/module_fentry_shadow.c | 96 +++++++++++++++++-- .../selftests/bpf/progs/test_module_attach.c | 16 ++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 10 ++ 9 files changed, 230 insertions(+), 38 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 bpf: Refuse the vmlinux prototype for a module function
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.
--- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++-------- kernel/bpf/syscall.c | 14 ++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-)
include/linux/btf.h +2 -0
@@ -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);
kernel/bpf/btf.c +32 -8
@@ -664,19 +664,13 @@ static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char *namereturn 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;
kernel/bpf/syscall.c +14 -0
@@ -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 */
Signature · 1 line
2.43.0
[email protected]
AI reviewTL;DR: - Fixes: tag may point at the wrong commit; backports to stable kernels without the mismatch could break loads that worked - New -EINVAL refusal in bpf_prog_alloc_attr() gives no verifier…
Quoted 1 line
bpf: Refuse the vmlinux prototype for a module function
TL;DR:
- Fixes: tag may point at the wrong commit; backports to stable kernels without the mismatch could break loads that worked
- New -EINVAL refusal in bpf_prog_alloc_attr() gives no verifier log, so users of older libbpf can't tell why loads now fail
Quoted 13 lines
bpf: Refuse the vmlinux prototype for a module function
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.
Quoted 2 lines
Fixes 290248a5b7d8 ("bpf: Allow to specify kernel module BTFs when attaching BPF programs")Signed-off-by Anton Protopopov <[email protected]>
Is the Fixes: tag pointing at the right commit?
At 290248a5b7d8, bpf_check_attach_target() resolved the address with the global lookup:
addr = kallsyms_lookup_name(tname);
kallsyms_lookup_name() searches vmlinux first and only then calls module_kallsyms_lookup_name(). A module BTF fd paired with the vmlinux FUNC id of a shadowed name therefore resolved to the vmlinux function, and the vmlinux prototype matched it.
The prototype mismatch, where the module function gets the vmlinux prototype, came with 31bf1dbccfb0 ("bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules"). That commit replaced the global lookup with the per-module one that is still in the tree:
if (btf_is_module(btf)) {
mod = btf_try_get_module(btf);
if (mod)
addr = find_kallsyms_symbol_value(mod, tname);Stable kernels that have 290248a5b7d8 (v5.11+) but not 31bf1dbccfb0 (v6.4+) would get this backport even though the mismatch does not happen there, and loads that worked on those kernels would start failing with -EINVAL.
Should the tag be:
No later commit in the series (0a1bd37aaae5, 06ce2ee959a3, 017fed259061) changes this tag.
Quoted 4 lines
kernel/bpf/syscall.c +0
-0
[ ... ]
Quoted 8 lines
kernel/bpf/syscall.c +4
-0@@ -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
kernel/bpf/syscall.c static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr · line 3085
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+* 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; + }
Can this refusal leave users of existing libbpf with no way to tell what went wrong?
For SEC("fentry/mod:func"), libbpf's find_kernel_btf_id() calls find_attach_btf_id(mod->btf, ...), which goes through find_btf_by_prefix_kind() to btf__find_by_name_kind(). That searches the vmlinux base first, so libbpf releases before 06ce2ee959a3 ("libbpf: Look up the functions of a module first") pass the vmlinux FUNC id together with mod->fd. Such programs loaded before this patch. Now they get -EINVAL from bpf_prog_alloc_attr(), which takes no log and runs before bpf_check(&prog, attr, uattr, attr_log) sets up the verifier log, so the log is empty.
The refusal is also not limited to prototypes that really differ. libbpf's dedup compares FUNC_PROTO parameter names (btf_equal_fnproto()), so a module function whose prototype differs from the vmlinux one only in parameter names also keeps its own FUNC and is refused. The multi-link patch later in the series (0a1bd37aaae5) logs the same condition with:
bpf_log(log, "Cannot attach to vmlinux target %u:%s with the BTF of a module.\n", ...)
bpf_check_attach_target() already resolves the module address by name (find_kallsyms_symbol_value(mod, tname)) and has a verifier log. Could the check go in its btf_is_module(btf) branch, or otherwise report a message, so users of existing libbpf can tell why a program that loaded before is now rejected?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47163865052
PATCH 2/4 bpf: Refuse the vmlinux prototype for a module function in multi links
In reply to Anton ProtopopovA tracing multi program loaded with the BTF of a module gets the IDs of its targets with its link. bpf_check_attach_btf_id_multi() takes the prototype from the ID, but the address of the function of the same name in the module, as bpf_prog_load() does for one target. Refuse the ID of a vmlinux function there in the same case.
--- kernel/bpf/verifier.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/bpf/verifier.c +6 -0
@@ -26968,6 +26968,12 @@ int bpf_check_attach_btf_id_multi(struct bpf_verifier_log *log, struct btf *btf, tname =btf_name_by_offset (btf, t->name_off);if (!tname)return -EINVAL ; +/* The same as in bpf_prog_load() */ +if (btf_is_base_func (btf, btf_id) &&btf_has_own_func (btf, btf_id)) { +bpf_log (log,"Cannot attach to vmlinux target %u:%s with the BTF of a module.\n" , + btf_id, tname); +return -EINVAL ; + } t =btf_attach_func_proto (log, btf, btf_id);if (!t || !btf_type_is_func_proto (t))return -EINVAL ;
Signature · 1 line
2.43.0
PATCH 3/4 libbpf: Look up the functions of a module first
In reply to Anton ProtopopovFor "mod:func" libbpf looks func up in the BTF of the module, and that lookup searches the types of vmlinux first. If vmlinux has a function of that name too, libbpf takes its ID, and the program attaches to the module function with the vmlinux prototype. The kernel now refuses this when the module has a function of its own of that name. A glob of a tracing multi program for a module walks the types of vmlinux too, and takes both IDs for such a name.
Look up the function of the module first, and take the one of vmlinux only if the module has no function of that name. The dedup of BTF merges a module function of the same prototype into the vmlinux one, which is then the type of the module function.
--- tools/lib/bpf/libbpf.c | 73 +++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 23 deletions(-)
tools/lib/bpf/libbpf.c +50 -23
@@ -11869,6 +11869,13 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,*/ if (ret <0 || ret >=sizeof (btf_type_name))return -ENAMETOOLONG ; +/* +* A function of a module first, then the one of vmlinux, which the +* dedup of BTF merges a module function of the same prototype into +*/ + ret =btf__find_by_name_kind_own (btf, btf_type_name, kind); +if (ret >0 || !btf__base_btf (btf)) +return ret;return btf__find_by_name_kind (btf, btf_type_name, kind); }
@@ -13887,40 +13894,60 @@ bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t)return true; } +static int add_func_id_by_glob (const struct btf *btf,__u32 type_id,const char *pattern, +bool skip_own,__u32 **ids,size_t *cap,size_t *cnt) +{ +const struct btf_type *t =btf__type_by_id (btf, type_id); +const char *name; + +if (btf_kind (t) !=BTF_KIND_FUNC ) +return 0 ; + name =btf__name_by_offset (btf, t->name_off); +if (!name || !glob_match (name, pattern)) +return 0 ; +if (!btf_type_is_traceable_func (btf, t)) +return 0 ; +if (skip_own &&btf__find_by_name_kind_own (btf, name,BTF_KIND_FUNC ) >0 ) +return 0 ; + +if (libbpf_ensure_mem ((void **) ids, cap,sizeof (**ids), *cnt +1 )) +return -ENOMEM ; + (*ids)[(*cnt)++] = type_id; +return 0 ; +} +static int collect_btf_func_ids_by_glob (const struct btf *btf,const char *pattern,__u32 **ids) { -__u32 type_id, nr_types =btf__type_cnt (btf); +const struct btf *base_btf =btf__base_btf (btf); +__u32 type_id, start_id, nr_types =btf__type_cnt (btf);size_t cap =0 , cnt =0 ; +int err;if (!pattern)return -EINVAL ; -for (type_id =1 ; type_id < nr_types; type_id++) { -const struct btf_type *t =btf__type_by_id (btf, type_id); -const char *name; -int err; - -if (btf_kind (t) !=BTF_KIND_FUNC ) -continue ; - name =btf__name_by_offset (btf, t->name_off); -if (!name) -continue ; - -if (!glob_match (name, pattern)) -continue ; -if (!btf_type_is_traceable_func (btf, t)) -continue ; - - err =libbpf_ensure_mem ((void **) ids, &cap,sizeof (**ids), cnt +1 ); -if (err) { -free (*ids); -return -ENOMEM ; - } - (*ids)[cnt++] = type_id; +/* +* The functions of a module first, then the ones of vmlinux, which the +* dedup of BTF merges module functions of the same prototype into +*/ + start_id = base_btf ?btf__type_cnt (base_btf) :1 ; +for (type_id = start_id; type_id < nr_types; type_id++) { + err =add_func_id_by_glob (btf, type_id, pattern, false, ids, &cap, &cnt); +if (err) + goto err_out; + } +for (type_id =1 ; type_id < start_id; type_id++) { + err =add_func_id_by_glob (btf, type_id, pattern, true, ids, &cap, &cnt); +if (err) + goto err_out; }return cnt; + +err_out: +free (*ids); +return err; }static int collect_func_ids_by_glob (const struct bpf_program *prog,const char *pattern,__u32 **ids)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] libbpf: redundant own-BTF scan on miss in find_btf_by_prefix_kind() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] libbpf: redundant own-BTF scan on miss in find_btf_by_prefix_kind()
--
commit 22c83c9c42525589b71bc7e58f2a6c4584fceb13
Author: Anton Protopopov <[email protected]>
libbpf: Look up the functions of a module first
This commit changes libbpf to search a module's own BTF types before falling back to vmlinux BTF when resolving tracing targets and tracing multi globs.
Quoted 7 lines
tools/lib/bpf/libbpf.c +0
-0@@ -11869,6 +11869,13 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,*/ if (ret <0 || ret >=sizeof (btf_type_name))
tools/lib/bpf/libbpf.c static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, · line 11880
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-0return -ENAMETOOLONG ; +/* +* A function of a module first, then the one of vmlinux, which the +* dedup of BTF merges a module function of the same prototype into +*/ + ret =btf__find_by_name_kind_own (btf, btf_type_name, kind); +if (ret >0 || !btf__base_btf (btf)) +return ret;return btf__find_by_name_kind (btf, btf_type_name, kind); }
[Severity: Low]
Should the fallback call btf__find_by_name_kind() on the base BTF rather than btf?
When the symbol is absent from both btf and its base BTF, btf__find_by_name_kind(btf, ...) searches base_btf and then scans btf's own types a second time. For an unqualified attach name, find_kernel_btf_id() iterates over all loaded module BTFs, doubling the own-BTF scan for every non-matching module.
Saving base_btf = btf__base_btf(btf) and falling back to btf__find_by_name_kind(base_btf, btf_type_name, kind) avoids the second scan.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/4 selftests/bpf: Test a vmlinux function ID with the BTF of a module
In reply to Anton ProtopopovAdd a bpf_fexit_test_ret_ppfile() to bpf_testmod which takes an argument. The vmlinux function of the same name takes none, so the module has a FUNC of its own for it. Check in module_fentry_shadow that the ID of the vmlinux function is refused with the BTF of the module, both at load and at the creation of a tracing multi link. Check in module_attach that libbpf takes the function of the module for "bpf_testmod:bpf_fexit_test_ret_ppfile", for both fentry and fentry.multi. Only x86_64 has tracing multi, so the multi parts are skipped elsewhere. Split module_fentry_shadow into the subtests shadow and multi for that.
--- .../selftests/bpf/prog_tests/module_attach.c | 11 +++ .../bpf/prog_tests/module_fentry_shadow.c | 96 +++++++++++++++++-- .../selftests/bpf/progs/test_module_attach.c | 16 ++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 10 ++ 4 files changed, 126 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/module_attach.c +11 -0
@@ -179,6 +179,17 @@ void test_module_attach(void)READ_SZ , "bpf_testmod:bpf_testmod_test_read", 0); } +/* libbpf must pick the function of the module, not the one of vmlinux */ +if (test__start_subtest ("handle_fentry_own_func" )) +test_module_attach_prog ("handle_fentry_own_func" ,0 ,NULL ,0 ); +if (test__start_subtest ("handle_fentry_multi_own_func" )) { +#ifdef __x86_64__ +test_module_attach_prog ("handle_fentry_multi_own_func" ,0 ,NULL ,0 ); +#else +printf ("%s:SKIP:tracing multi is supported only on x86_64\n" , __func__); +test__skip (); +#endif + }if (test__start_subtest ("handle_fexit" ))test_module_attach_prog ("handle_fexit" ,READ_SZ ,NULL , -EIO );if (test__start_subtest ("handle_fexit_ret" ))
tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c +89 -7
124 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_module_attach.c +16 -0
@@ -56,6 +56,22 @@ int BPF_PROG(handle_fentry,return 0 ; } +/* +* bpf_testmod has a function of its own with the name of a vmlinux function +* of another prototype +*/ +SEC("?fentry/bpf_testmod:bpf_fexit_test_ret_ppfile") +int BPF_PROG (handle_fentry_own_func ,int a) +{ +return 0 ; +} + +SEC("?fentry.multi/bpf_testmod:bpf_fexit_test_ret_ppfile") +int BPF_PROG (handle_fentry_multi_own_func ) +{ +return 0 ; +} + SEC("?fentry") int BPF_PROG(handle_fentry_manual, struct file *file, struct kobject *kobj,
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +10 -0
@@ -922,6 +922,16 @@ noinline int bpf_fentry_shadow_test(int a) }EXPORT_SYMBOL_GPL (bpf_fentry_shadow_test); +/* +* Has the name of a vmlinux function, but not its prototype, so it has a +* FUNC of its own +*/ +noinline int bpf_fexit_test_ret_ppfile(int a) +{ +return a; +} +EXPORT_SYMBOL_GPL (bpf_fexit_test_ret_ppfile); +__bpf_hook_end ();static struct bin_attribute bin_attr_bpf_testmod_file__ro_after_init = {
Signature · 1 line
2.43.0