BPF List
 help / color / mirror / Atom feed
* [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
@ 2026-12-03 17:26 Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Anton Protopopov @ 2026-12-03 17:26 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 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(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v1 bpf-next 1/4] bpf: Refuse the vmlinux prototype for a module function
  2026-12-03 17:26 [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
@ 2026-12-03 17:26 ` Anton Protopopov
  2026-12-03 17:54   ` bot+bpf-ci
  2026-12-03 17:26 ` [PATCH v1 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Anton Protopopov @ 2026-12-03 17:26 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: 290248a5b7d8 ("bpf: Allow to specify kernel module BTFs when attaching BPF programs")
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links
  2026-12-03 17:26 [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
@ 2026-12-03 17:26 ` Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov
  3 siblings, 0 replies; 7+ messages in thread
From: Anton Protopopov @ 2026-12-03 17:26 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_prog_load() 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 d4598b6cd440..22e25aec5033 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first
  2026-12-03 17:26 [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
@ 2026-12-03 17:26 ` Anton Protopopov
  2026-12-03 18:57   ` sashiko-bot
  2026-12-03 17:26 ` [PATCH v1 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov
  3 siblings, 1 reply; 7+ messages in thread
From: Anton Protopopov @ 2026-12-03 17:26 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 | 73 +++++++++++++++++++++++++++++-------------
 1 file changed, 50 insertions(+), 23 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 8ed4786e0e51..b75b94add227 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module
  2026-12-03 17:26 [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
                   ` (2 preceding siblings ...)
  2026-12-03 17:26 ` [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
@ 2026-12-03 17:26 ` Anton Protopopov
  3 siblings, 0 replies; 7+ messages in thread
From: Anton Protopopov @ 2026-12-03 17:26 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] 7+ messages in thread

* Re: [PATCH v1 bpf-next 1/4] bpf: Refuse the vmlinux prototype for a module function
  2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
@ 2026-12-03 17:54   ` bot+bpf-ci
  0 siblings, 0 replies; 7+ messages in thread
From: bot+bpf-ci @ 2026-12-03 17:54 UTC (permalink / raw)
  To: a.s.protopopov, bpf, ast, daniel, andrii, eddyz87, memxor,
	martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai,
	john.fastabend, vmalik
  Cc: a.s.protopopov, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 5037 bytes --]

> 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

> 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.
> Fixes: 290248a5b7d8 ("bpf: Allow to specify kernel module BTFs when attaching BPF programs")
> Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>

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:

  Fixes: 31bf1dbccfb0 ("bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules")

No later commit in the series (0a1bd37aaae5, 06ce2ee959a3, 017fed259061)
changes this tag.

> 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;
> +			}

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first
  2026-12-03 17:26 ` [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
@ 2026-12-03 18:57   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 18:57 UTC (permalink / raw)
  To: Anton Protopopov, bpf

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 <a.s.protopopov@gmail.com>

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.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 8ed4786e0e512..b75b94add2276 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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);
>  }

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203172617.1137735-1-a.s.protopopov@gmail.com?part=3

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-03 18:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 17:26 [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
2026-12-03 17:54   ` bot+bpf-ci
2026-12-03 17:26 ` [PATCH v1 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
2026-12-03 17:26 ` [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
2026-12-03 18:57   ` sashiko-bot
2026-12-03 17:26 ` [PATCH v1 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox