* [PATCH v2 bpf-next 1/4] bpf: Refuse the vmlinux prototype for a module function
2026-12-04 15:46 [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
@ 2026-12-04 15:47 ` Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
` (3 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Anton Protopopov @ 2026-12-04 15:47 UTC (permalink / raw)
To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Viktor Malik
Cc: Anton Protopopov
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: 31bf1dbccfb0 ("bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules")
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
include/linux/btf.h | 2 ++
kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++--------
kernel/bpf/verifier.c | 11 +++++++++++
3 files changed, 45 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/verifier.c b/kernel/bpf/verifier.c
index 832f2efcbce4..a6bff31c462c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -26209,6 +26209,17 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id);
return -EINVAL;
}
+ /*
+ * 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 (!tgt_prog && 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;
+ }
if (tgt_prog) {
struct bpf_prog_aux *aux = tgt_prog->aux;
enum bpf_cgroup_storage_type stype;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links
2026-12-04 15:46 [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 1/4] " Anton Protopopov
@ 2026-12-04 15:47 ` Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Anton Protopopov @ 2026-12-04 15:47 UTC (permalink / raw)
To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Viktor Malik
Cc: Anton Protopopov
A 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_check_attach_target() does for one
target. Refuse the ID of a vmlinux function there in the same case.
Fixes: aef4dfa790b2 ("bpf: Add bpf_trampoline_multi_attach/detach functions")
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
kernel/bpf/verifier.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index a6bff31c462c..8e4a62b443ae 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -26976,6 +26976,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_check_attach_target() */
+ 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;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first
2026-12-04 15:46 [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 1/4] " Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
@ 2026-12-04 15:47 ` Anton Protopopov
2026-12-05 11:45 ` Jiri Olsa
2026-12-04 15:47 ` [PATCH v2 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov
2026-12-05 3:47 ` [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Alexei Starovoitov
4 siblings, 1 reply; 9+ messages in thread
From: Anton Protopopov @ 2026-12-04 15:47 UTC (permalink / raw)
To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Viktor Malik
Cc: Anton Protopopov
For "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.
Fixes: 8f8a024272f3 ("libbpf: support "module: Function" syntax for tracing programs")
Fixes: f2aa370dfe57 ("libbpf: Add support to create tracing multi link")
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
tools/lib/bpf/libbpf.c | 75 ++++++++++++++++++++++++++++--------------
1 file changed, 51 insertions(+), 24 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 8ed4786e0e51..72330598a0f8 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -11869,7 +11869,14 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
*/
if (ret < 0 || ret >= sizeof(btf_type_name))
return -ENAMETOOLONG;
- return btf__find_by_name_kind(btf, btf_type_name, kind);
+ /*
+ * 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__base_btf(btf), btf_type_name, kind);
}
static inline int find_attach_btf_id(struct btf *btf, const char *name,
@@ -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)
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first
2026-12-04 15:47 ` [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
@ 2026-12-05 11:45 ` Jiri Olsa
0 siblings, 0 replies; 9+ messages in thread
From: Jiri Olsa @ 2026-12-05 11:45 UTC (permalink / raw)
To: Anton Protopopov
Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Emil Tsalapatis, Ihor Solodrai,
John Fastabend, Viktor Malik
On Fri, Dec 04, 2026 at 03:47:02PM +0000, Anton Protopopov wrote:
> For "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.
nice, as Alexei noted, this change is still needed because of the
duplicate ids check in the multi link attach, one nit below
Acked-by: Jiri Olsa <jolsa@kernel.org>
thanks,
jirka
>
> Fixes: 8f8a024272f3 ("libbpf: support "module: Function" syntax for tracing programs")
> Fixes: f2aa370dfe57 ("libbpf: Add support to create tracing multi link")
> Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
> ---
> tools/lib/bpf/libbpf.c | 75 ++++++++++++++++++++++++++++--------------
> 1 file changed, 51 insertions(+), 24 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 8ed4786e0e51..72330598a0f8 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -11869,7 +11869,14 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
> */
> if (ret < 0 || ret >= sizeof(btf_type_name))
> return -ENAMETOOLONG;
> - return btf__find_by_name_kind(btf, btf_type_name, kind);
> + /*
> + * 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__base_btf(btf), btf_type_name, kind);
> }
>
> static inline int find_attach_btf_id(struct btf *btf, const char *name,
> @@ -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);
nit, add_func_id_by_glob fails only with -ENOMEM, so we can drop
err and return -ENOMEM directly on the error path
> + 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)
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module
2026-12-04 15:46 [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
` (2 preceding siblings ...)
2026-12-04 15:47 ` [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
@ 2026-12-04 15:47 ` Anton Protopopov
2026-12-05 3:47 ` [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Alexei Starovoitov
4 siblings, 0 replies; 9+ messages in thread
From: Anton Protopopov @ 2026-12-04 15:47 UTC (permalink / raw)
To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Viktor Malik
Cc: Anton Protopopov
Add 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.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
.../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(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/module_attach.c b/tools/testing/selftests/bpf/prog_tests/module_attach.c
index 92c336333fcb..c63d7f282fd8 100644
--- a/tools/testing/selftests/bpf/prog_tests/module_attach.c
+++ b/tools/testing/selftests/bpf/prog_tests/module_attach.c
@@ -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"))
diff --git a/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c b/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c
index bea05f78de5f..0d44a6a0ace6 100644
--- a/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c
+++ b/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c
@@ -8,6 +8,7 @@
static const char *module_name = "bpf_testmod";
static const char *symbol_name = "bpf_fentry_shadow_test";
+static const char *other_name = "bpf_fexit_test_ret_ppfile";
static int get_bpf_testmod_btf_fd(void)
{
@@ -53,20 +54,16 @@ static int get_bpf_testmod_btf_fd(void)
return -ENOENT;
}
-void test_module_fentry_shadow(void)
+static void test_shadow(void)
{
struct btf *vmlinux_btf = NULL, *mod_btf = NULL;
- int err, i;
+ int err, i, fd;
+ __s32 id;
int btf_fd[2] = {};
int prog_fd[2] = {};
int link_fd[2] = {};
__s32 btf_id[2] = {};
- if (!env.has_testmod) {
- test__skip();
- return;
- }
-
LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
.expected_attach_type = BPF_TRACE_FENTRY,
);
@@ -120,6 +117,25 @@ void test_module_fentry_shadow(void)
err = bpf_prog_test_run_opts(prog_fd[0], NULL);
ASSERT_OK(err, "running test");
+ /*
+ * The bpf_fexit_test_ret_ppfile() of bpf_testmod has a prototype other
+ * than the one of vmlinux, so the vmlinux one is refused with the BTF
+ * of the module
+ */
+ id = btf__find_by_name_kind_own(mod_btf, other_name, BTF_KIND_FUNC);
+ if (!ASSERT_GT(id, 0, "module func"))
+ goto out;
+ id = btf__find_by_name_kind(vmlinux_btf, other_name, BTF_KIND_FUNC);
+ if (!ASSERT_GT(id, 0, "vmlinux func"))
+ goto out;
+ load_opts.attach_btf_id = id;
+ load_opts.attach_btf_obj_fd = btf_fd[1];
+ fd = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL", trace_program,
+ ARRAY_SIZE(trace_program), &load_opts);
+ ASSERT_EQ(fd, -EINVAL, "vmlinux func with module btf");
+ if (fd >= 0)
+ close(fd);
+
out:
btf__free(vmlinux_btf);
btf__free(mod_btf);
@@ -132,3 +148,69 @@ void test_module_fentry_shadow(void)
close(link_fd[i]);
}
}
+
+/* A multi program gets the ID with its link */
+static void test_multi(void)
+{
+ LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
+ .expected_attach_type = BPF_TRACE_FENTRY_MULTI,
+ );
+ LIBBPF_OPTS(bpf_link_create_opts, link_opts,
+ .tracing_multi.cnt = 1,
+ );
+ const struct bpf_insn trace_program[] = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ struct btf *vmlinux_btf;
+ int btf_fd, fd, link_fd;
+ __u32 ids[1];
+ __s32 id;
+
+#ifndef __x86_64__
+ printf("%s:SKIP:tracing multi is supported only on x86_64\n", __func__);
+ test__skip();
+ return;
+#endif
+
+ vmlinux_btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(vmlinux_btf, "load_vmlinux_btf"))
+ return;
+ id = btf__find_by_name_kind(vmlinux_btf, other_name, BTF_KIND_FUNC);
+ btf__free(vmlinux_btf);
+ if (!ASSERT_GT(id, 0, "vmlinux func"))
+ return;
+
+ btf_fd = get_bpf_testmod_btf_fd();
+ if (!ASSERT_GE(btf_fd, 0, "get_bpf_testmod_btf_fd"))
+ return;
+
+ load_opts.attach_btf_obj_fd = btf_fd;
+ fd = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL", trace_program,
+ ARRAY_SIZE(trace_program), &load_opts);
+ if (!ASSERT_GE(fd, 0, "load multi"))
+ goto out;
+
+ ids[0] = id;
+ link_opts.tracing_multi.ids = ids;
+ link_fd = bpf_link_create(fd, 0, BPF_TRACE_FENTRY_MULTI, &link_opts);
+ ASSERT_EQ(link_fd, -EINVAL, "vmlinux func with module btf, multi");
+ if (link_fd >= 0)
+ close(link_fd);
+ close(fd);
+out:
+ close(btf_fd);
+}
+
+void test_module_fentry_shadow(void)
+{
+ if (!env.has_testmod) {
+ test__skip();
+ return;
+ }
+
+ if (test__start_subtest("shadow"))
+ test_shadow();
+ if (test__start_subtest("multi"))
+ test_multi();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_module_attach.c b/tools/testing/selftests/bpf/progs/test_module_attach.c
index 5609e388fb58..3f98562443ac 100644
--- a/tools/testing/selftests/bpf/progs/test_module_attach.c
+++ b/tools/testing/selftests/bpf/progs/test_module_attach.c
@@ -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,
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 86d25638a727..2e72b54d2445 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -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 = {
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
2026-12-04 15:46 [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
` (3 preceding siblings ...)
2026-12-04 15:47 ` [PATCH v2 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov
@ 2026-12-05 3:47 ` Alexei Starovoitov
2026-12-05 11:45 ` Jiri Olsa
4 siblings, 1 reply; 9+ messages in thread
From: Alexei Starovoitov @ 2026-12-05 3:47 UTC (permalink / raw)
To: Anton Protopopov, bpf, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Viktor Malik
On Fri, Dec 04, 2026 at 07:46 AM Anton Protopopov <a.s.protopopov@gmail.com> wrote:
> 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
It's more than one case, I think.
vmlinux has foo(a, b), the module has foo(a), the prog doesn't read 'b'.
x86 trampoline does:
nr_regs = bpf_jit_place_args(&x86_arg_abi, m, pos_of_slot);
It saves 2 regs, the module needs 1. Such prog works today.
libbpf passes the vmlinux ID for "mod:foo" since commit 8f8a024272f3.
These progs get -EINVAL until their libbpf is updated.
What a prog gets with module BTF and the vmlinux ID:
FUNC of the module today series diff below
---------------------------- ----------- ------- ----------
other names of params works -EINVAL works
fewer args, 'b' is not read works -EINVAL works
other type of an arg wrong type -EINVAL right type
more args not saved -EINVAL saved
> A proper longer-term (to deploy) fix: 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.
The 2nd case doesn't go away. It grows to every name that is in both.
test_shadow() has:
btf_id[1] = btf__find_by_name_kind(mod_btf, symbol_name, BTF_KIND_FUNC);
That's the vmlinux ID. With such pahole bpf_testmod has its own FUNC
for bpf_fentry_shadow_test() and patch 1 refuses the load.
The series finds the FUNC of the module already.
Lets use it instead of failing.
Old loaders get the right prototype, before and after the follow up.
A FUNC that pahole dropped is still not covered.
A glob that passes both IDs still gets -EINVAL. That needs patch 3.
btf_own_func_id() replaces patches 1 and 2.
diff --git a/include/linux/btf.h b/include/linux/btf.h
index 6014282..510f587 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -254,6 +254,7 @@ 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);
+u32 btf_own_func_id(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 3597ba1..6312b7c 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,42 @@ 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);
+}
+
+/*
+ * The address of an attach target is looked up by name in the module. Take
+ * the FUNC of the module when it has one of that name. Otherwise the dedup
+ * merged it into the FUNC of vmlinux.
+ */
+u32 btf_own_func_id(const struct btf *btf, u32 id)
+{
+ const struct btf_type *t;
+ s32 own_id;
+
+ if (id >= btf->start_id)
+ return id;
+
+ t = btf_type_by_id(btf, id);
+ if (!btf_type_is_func(t))
+ return id;
+
+ own_id = btf_find_by_name_kind_own(btf, btf_name_by_offset(btf, t->name_off),
+ BTF_KIND_FUNC);
+ return own_id > 0 ? own_id : id;
+}
+
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 b9cd3d5..2258861 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3069,6 +3069,7 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr
goto put_token;
}
}
+ attach_btf_id = btf_own_func_id(attach_btf, attach_btf_id);
} 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 */
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 2031c37..42963a1 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -3918,6 +3918,11 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
}
}
+ for (int i = 0; i < cnt; i++) {
+ ids[i] = btf_own_func_id(prog->aux->attach_btf, ids[i]);
+ cond_resched();
+ }
+
err = check_dup_ids(ids, cookies, cnt, log);
if (err)
goto error;
pw-bot: cr
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
2026-12-05 3:47 ` [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Alexei Starovoitov
@ 2026-12-05 11:45 ` Jiri Olsa
2026-12-05 17:31 ` Alexei Starovoitov
0 siblings, 1 reply; 9+ messages in thread
From: Jiri Olsa @ 2026-12-05 11:45 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Anton Protopopov, bpf, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Emil Tsalapatis, Ihor Solodrai,
John Fastabend, Viktor Malik
On Sat, Dec 05, 2026 at 03:47:28AM +0000, Alexei Starovoitov wrote:
> On Fri, Dec 04, 2026 at 07:46 AM Anton Protopopov <a.s.protopopov@gmail.com> wrote:
> > 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
>
> It's more than one case, I think.
> vmlinux has foo(a, b), the module has foo(a), the prog doesn't read 'b'.
> x86 trampoline does:
>
> nr_regs = bpf_jit_place_args(&x86_arg_abi, m, pos_of_slot);
>
> It saves 2 regs, the module needs 1. Such prog works today.
> libbpf passes the vmlinux ID for "mod:foo" since commit 8f8a024272f3.
> These progs get -EINVAL until their libbpf is updated.
> What a prog gets with module BTF and the vmlinux ID:
>
> FUNC of the module today series diff below
> ---------------------------- ----------- ------- ----------
> other names of params works -EINVAL works
> fewer args, 'b' is not read works -EINVAL works
> other type of an arg wrong type -EINVAL right type
> more args not saved -EINVAL saved
>
> > A proper longer-term (to deploy) fix: 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.
>
> The 2nd case doesn't go away. It grows to every name that is in both.
> test_shadow() has:
>
> btf_id[1] = btf__find_by_name_kind(mod_btf, symbol_name, BTF_KIND_FUNC);
>
> That's the vmlinux ID. With such pahole bpf_testmod has its own FUNC
> for bpf_fentry_shadow_test() and patch 1 refuses the load.
>
> The series finds the FUNC of the module already.
> Lets use it instead of failing.
> Old loaders get the right prototype, before and after the follow up.
> A FUNC that pahole dropped is still not covered.
> A glob that passes both IDs still gets -EINVAL. That needs patch 3.
>
> btf_own_func_id() replaces patches 1 and 2.
>
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 6014282..510f587 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -254,6 +254,7 @@ 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);
> +u32 btf_own_func_id(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 3597ba1..6312b7c 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,42 @@ 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);
> +}
> +
> +/*
> + * The address of an attach target is looked up by name in the module. Take
> + * the FUNC of the module when it has one of that name. Otherwise the dedup
> + * merged it into the FUNC of vmlinux.
> + */
> +u32 btf_own_func_id(const struct btf *btf, u32 id)
> +{
> + const struct btf_type *t;
> + s32 own_id;
> +
> + if (id >= btf->start_id)
> + return id;
> +
> + t = btf_type_by_id(btf, id);
> + if (!btf_type_is_func(t))
> + return id;
> +
> + own_id = btf_find_by_name_kind_own(btf, btf_name_by_offset(btf, t->name_off),
> + BTF_KIND_FUNC);
> + return own_id > 0 ? own_id : id;
> +}
> +
> 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 b9cd3d5..2258861 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3069,6 +3069,7 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr
> goto put_token;
> }
> }
> + attach_btf_id = btf_own_func_id(attach_btf, attach_btf_id);
> } 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 */
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 2031c37..42963a1 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -3918,6 +3918,11 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
> }
> }
>
> + for (int i = 0; i < cnt; i++) {
> + ids[i] = btf_own_func_id(prog->aux->attach_btf, ids[i]);
I like the fix
one nit: the btf_own_func_id name seems a bit misleading to me
when the module does not have that function we keep the vmlinux id,
so the result is not always module's own id. Would something like
btf_remap_func_id fit better?
thanks,
jirka
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
2026-12-05 11:45 ` Jiri Olsa
@ 2026-12-05 17:31 ` Alexei Starovoitov
0 siblings, 0 replies; 9+ messages in thread
From: Alexei Starovoitov @ 2026-12-05 17:31 UTC (permalink / raw)
To: Jiri Olsa
Cc: Anton Protopopov, bpf, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Emil Tsalapatis, Ihor Solodrai,
John Fastabend, Viktor Malik
On Sat, Dec 05, 2026 at 03:45 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> one nit: the btf_own_func_id name seems a bit misleading to me
> when the module does not have that function we keep the vmlinux id,
> so the result is not always module's own id. Would something like
> btf_remap_func_id fit better?
Good point. btf_remap_func_id() is better.
^ permalink raw reply [flat|nested] 9+ messages in thread