Skip to contentSkip to conversation

bpf: Refuse the vmlinux prototype for a module function

PATCH In review Anton Protopopov 2 participants 6 messages Dec 4 – Dec 5

Subsystems (9)
Kselftest BPF Tracing Arm STM32 +5 more subsystems
lore
Share & tools
lore
#0 lore raw

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

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.

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.

v1 -> v2:
* Refuse the ID in bpf_check_attach_target(), which logs why, and fix the Fixes tag (patch 1)
* Don't search the module twice when it has no function of that name (patch 3)

v1: https://lore.kernel.org/bpf/[email protected]

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/verifier.c                         | 17 ++++
 tools/lib/bpf/libbpf.c                        | 75 ++++++++++-----
 .../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 ++
 8 files changed, 228 insertions(+), 39 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 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.

Fixes 31bf1dbccfb0 ("bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules")
---
 include/linux/btf.h   |  2 ++
 kernel/bpf/btf.c      | 40 ++++++++++++++++++++++++++++++++--------
 kernel/bpf/verifier.c | 11 +++++++++++
 3 files changed, 45 insertions(+), 8 deletions(-)
include/linux/btf.h +2 -0
index 6014282feacb..d82efac8a4d0 100644
@@ -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
index 3597ba1b5594..12cf8b9c7d3c 100644
@@ -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;
kernel/bpf/verifier.c +11 -0
index 832f2efcbce4..a6bff31c462c 100644
@@ -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;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/4 bpf: Refuse the vmlinux prototype for a module function in multi links

In reply to 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")
---
 kernel/bpf/verifier.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/bpf/verifier.c +6 -0
index a6bff31c462c..8e4a62b443ae 100644
@@ -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;
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/4 libbpf: Look up the functions of a module first

In reply to 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")
---
 tools/lib/bpf/libbpf.c | 75 ++++++++++++++++++++++++++++--------------
 1 file changed, 51 insertions(+), 24 deletions(-)
tools/lib/bpf/libbpf.c +51 -24
index 8ed4786e0e51..72330598a0f8 100644
@@ -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)
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/4 selftests/bpf: Test a vmlinux function ID with the BTF of a module

In reply to 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.

---
 .../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
index 92c336333fcb..c63d7f282fd8 100644
@@ -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
index 5609e388fb58..3f98562443ac 100644
@@ -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
index 86d25638a727..2e72b54d2445 100644
@@ -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
#5 lore raw
In reply to Anton Protopopov
Quoted 5 lines · Anton Protopopov

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
Quoted 7 lines

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.

include/linux/btf.h +1 -0
index 6014282..510f587 100644
@@ -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);
kernel/bpf/btf.c +38 -8

63 lines not on this page · raw message

kernel/bpf/syscall.c +1 -0
index b9cd3d5..2258861 100644
@@ -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 */
kernel/trace/bpf_trace.c +5 -0
index 2031c37..42963a1 100644
@@ -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