Skip to contentSkip to conversation

Fix attaching fentry/fexit/fmod_ret/lsm to modules

PATCH Superseded Viktor Malik 3 participants 6 messages Nov 28, 2022

Subsystems (6)
Networking Kselftest BPF Arm STM32 +2 more subsystems

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules

In reply to Viktor Malik

When attaching fentry/fexit/fmod_ret/lsm to a function located in a module without specifying the target program, the verifier tries to find the address to attach to in kallsyms. This is always done by searching the entire kallsyms, not respecting the module in which the function is located.

This approach causes an incorrect attachment address to be computed if the function to attach to is shadowed by a function of the same name located earlier in kallsyms.

Since the attachment must contain the BTF of the program to attach to, we may extract the module name from it (if the attach target is a module) and search for the function address in the correct module.

---
 include/linux/btf.h   | 1 +
 kernel/bpf/btf.c      | 5 +++++
 kernel/bpf/verifier.c | 9 ++++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)
include/linux/btf.h +1 -0
index 9ed00077db6e..bdbf3eb7083d 100644
@@ -187,6 +187,7 @@ u32 btf_obj_id(const struct btf *btf);
 bool btf_is_kernel(const struct btf *btf);
 bool btf_is_module(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
+const char *btf_module_name(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   const struct btf_member *m,
kernel/bpf/btf.c +5 -0
index 1a59cc7ad730..211fcbb7649d 100644
@@ -7192,6 +7192,11 @@ bool btf_is_module(const struct btf *btf)
 	return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
 }
 
+const char *btf_module_name(const struct btf *btf)
+{
+	return btf->name;
+}
+
 enum {
 	BTF_MODULE_F_LIVE = (1 << 0),
 };
kernel/bpf/verifier.c +8 -1
index 9528a066cfa5..acbe62a73559 100644
@@ -16343,7 +16343,14 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 			else
 				addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
 		} else {
-			addr = kallsyms_lookup_name(tname);
+			if (btf_is_module(btf)) {
+				char tmodname[MODULE_NAME_LEN + KSYM_NAME_LEN + 1];
+				snprintf(tmodname, sizeof(tmodname), "%s:%s",
+					 btf_module_name(btf), tname);
+				addr = module_kallsyms_lookup_name(tmodname);
+			}
+			else
+				addr = kallsyms_lookup_name(tname);
 			if (!addr) {
 				bpf_log(log,
 					"The address of function %s cannot be found\n",
Signature · 1 line
2.38.1
#0 lore raw

cover Fix attaching fentry/fexit/fmod_ret/lsm to modules

While working on bpftrace support for BTF in modules [1], I noticed that the verifier behaves incorrectly when attaching to fentry of multiple functions of the same name located in different modules (or in vmlinux).

The reason for this is that if the target program is not specified, the verifier will search kallsyms for the trampoline address to attach to. The entire kallsyms is always searched, not respecting the module in which the function to attach to is located.

This patch fixes the above issue by extracting the module name from the BTF of the attachment target (which must be specified) and by doing the search in kallsyms of the correct module.

This also adds a new test in test_progs which tries to attach a program to fentry of two functions of the same name, one located in vmlinux and the other in bpf_testmod. Prior to the fix, the verifier would always use the vmlinux function address as the target trampoline, attempting to attach two functions to the same trampoline (which is prohibited).

[1] https://github.com/iovisor/bpftrace/pull/2315

Viktor Malik (2):
bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules bpf/selftests: Test fentry attachment to shadowed functions

 include/linux/btf.h                           |   1 +
 kernel/bpf/btf.c                              |   5 +
 kernel/bpf/verifier.c                         |   9 +-
 net/bpf/test_run.c                            |   5 +
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |   7 +
 .../bpf/prog_tests/module_attach_shadow.c     | 120 ++++++++++++++++++
 6 files changed, 146 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c
Signature · 1 line
2.38.1
#4 lore raw

PATCH 2/2 bpf/selftests: Test fentry attachment to shadowed functions

Adds a new test that tries to attach a program to fentry of two functions of the same name, one located in vmlinux and the other in bpf_testmod.

To avoid conflicts with existing tests, a new function "bpf_fentry_shadow_test" was created both in vmlinux and in bpf_testmod.

The previous commit fixed a bug which caused this test to fail. The verifier would always use the vmlinux function's address as the target trampoline address, hence trying to attach two programs to the same trampoline.

---
 net/bpf/test_run.c                            |   5 +
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |   7 +
 .../bpf/prog_tests/module_attach_shadow.c     | 120 ++++++++++++++++++
 3 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c
net/bpf/test_run.c +5 -0
index 6fba440efc40..c60e46b1e768 100644
@@ -536,6 +536,11 @@ int noinline bpf_modify_return_test(int a, int *b)
 	return a + *b;
 }
 
+int noinline bpf_fentry_shadow_test(int a)
+{
+	return a + 1;
+}
+
 u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
 {
 	return a + b + c + d;
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +7 -0
index 5085fea3cac5..d23127a5ec68 100644
@@ -229,6 +229,13 @@ static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
 	.set   = &bpf_testmod_check_kfunc_ids,
 };
 
+noinline int bpf_fentry_shadow_test(int a)
+{
+	return a + 2;
+}
+EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test);
+ALLOW_ERROR_INJECTION(bpf_fentry_shadow_test, ERRNO);
+
 extern int bpf_fentry_test1(int a);
 
 static int bpf_testmod_init(void)
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
2.38.1
#2 lore raw

Re: [PATCH bpf-next 1/2] bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules

In reply to Viktor Malik
Quoted 54 lines · Viktor Malik

When attaching fentry/fexit/fmod_ret/lsm to a function located in a module without specifying the target program, the verifier tries to find the address to attach to in kallsyms. This is always done by searching the entire kallsyms, not respecting the module in which the function is located.

This approach causes an incorrect attachment address to be computed if the function to attach to is shadowed by a function of the same name located earlier in kallsyms.

Since the attachment must contain the BTF of the program to attach to, we may extract the module name from it (if the attach target is a module) and search for the function address in the correct module.

---
 include/linux/btf.h   | 1 +
 kernel/bpf/btf.c      | 5 +++++
 kernel/bpf/verifier.c | 9 ++++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)
include/linux/btf.h +1 -0
index 9ed00077db6e..bdbf3eb7083d 100644
@@ -187,6 +187,7 @@ u32 btf_obj_id(const struct btf *btf);
 bool btf_is_kernel(const struct btf *btf);
 bool btf_is_module(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
+const char *btf_module_name(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   const struct btf_member *m,
kernel/bpf/btf.c +5 -0
index 1a59cc7ad730..211fcbb7649d 100644
@@ -7192,6 +7192,11 @@ bool btf_is_module(const struct btf *btf)
 	return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
 }
 
+const char *btf_module_name(const struct btf *btf)
+{
+	return btf->name;
+}
+
 enum {
 	BTF_MODULE_F_LIVE = (1 << 0),
 };
kernel/bpf/verifier.c +0 -0
index 9528a066cfa5..acbe62a73559 100644

kernel/bpf/verifier.c int bpf_check_attach_target(struct bpf_verifier_log *log, · line 16347

Quoted 6 lines
kernel/bpf/verifier.c +2 -1
@@ -16343,7 +16343,14 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 			else
 				addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
 		} else {
-			addr = kallsyms_lookup_name(tname);
+			if (btf_is_module(btf)) {
+				char tmodname[MODULE_NAME_LEN + KSYM_NAME_LEN + 1];

looks good.. would be nice to have module_kallsyms lookup function that takes module name and symbol separately so we won't waste stack on that..

especially when module_kallsyms_lookup_name just separates it back again and does module lookup.. but not sure how much pain it'd be

jirka

Quoted 12 lines
kernel/bpf/verifier.c +6 -0
+				snprintf(tmodname, sizeof(tmodname), "%s:%s",
+					 btf_module_name(btf), tname);
+				addr = module_kallsyms_lookup_name(tmodname);
+			}
+			else
+				addr = kallsyms_lookup_name(tname);
 			if (!addr) {
 				bpf_log(log,
 					"The address of function %s cannot be found\n",
Signature · 1 line
2.38.1
#3 lore raw

Re: [PATCH bpf-next 1/2] bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules

Quoted 66 lines · Jiri Olsa
Quoted 54 lines · Viktor Malik

When attaching fentry/fexit/fmod_ret/lsm to a function located in a module without specifying the target program, the verifier tries to find the address to attach to in kallsyms. This is always done by searching the entire kallsyms, not respecting the module in which the function is located.

This approach causes an incorrect attachment address to be computed if the function to attach to is shadowed by a function of the same name located earlier in kallsyms.

Since the attachment must contain the BTF of the program to attach to, we may extract the module name from it (if the attach target is a module) and search for the function address in the correct module.

---
 include/linux/btf.h   | 1 +
 kernel/bpf/btf.c      | 5 +++++
 kernel/bpf/verifier.c | 9 ++++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)
include/linux/btf.h +1 -0
index 9ed00077db6e..bdbf3eb7083d 100644
@@ -187,6 +187,7 @@ u32 btf_obj_id(const struct btf *btf);
 bool btf_is_kernel(const struct btf *btf);
 bool btf_is_module(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
+const char *btf_module_name(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
                         const struct btf_member *m,
kernel/bpf/btf.c +5 -0
index 1a59cc7ad730..211fcbb7649d 100644
@@ -7192,6 +7192,11 @@ bool btf_is_module(const struct btf *btf)
      return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
 }
 
+const char *btf_module_name(const struct btf *btf)
+{
+     return btf->name;
+}
+
 enum {
      BTF_MODULE_F_LIVE = (1 << 0),
 };
kernel/bpf/verifier.c +0 -0
index 9528a066cfa5..acbe62a73559 100644

kernel/bpf/verifier.c int bpf_check_attach_target(struct bpf_verifier_log *log, · line 16347

Quoted 6 lines
kernel/bpf/verifier.c +2 -1
@@ -16343,7 +16343,14 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
                      else
                              addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
              } else {
-                     addr = kallsyms_lookup_name(tname);
+                     if (btf_is_module(btf)) {
+                             char tmodname[MODULE_NAME_LEN + KSYM_NAME_LEN + 1];

looks good.. would be nice to have module_kallsyms lookup function that takes module name and symbol separately so we won't waste stack on that..

Quoted 11 lines

especially when module_kallsyms_lookup_name just separates it back again and does module lookup.. but not sure how much pain it'd be

jirka

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+                             snprintf(tmodname, sizeof(tmodname), "%s:%s",
+                                      btf_module_name(btf), tname);
+                             addr = module_kallsyms_lookup_name(tmodname);
+                     }
+                     else
+                             addr = kallsyms_lookup_name(tname);

In addition to what Jiri suggested, we should also have brackets in the 'else' branch.

if (...) {
...
} else {
...
}

Quoted 6 lines
Quoted 6 lines
kernel/bpf/verifier.c +0 -1
                      if (!addr) {
                              bpf_log(log,
                                      "The address of function %s cannot be found\n",
--

2.38.1

#5 lore raw

Re: [PATCH bpf-next 2/2] bpf/selftests: Test fentry attachment to shadowed functions

In reply to Viktor Malik
Quoted 15 lines · Viktor Malik

Adds a new test that tries to attach a program to fentry of two functions of the same name, one located in vmlinux and the other in bpf_testmod.

To avoid conflicts with existing tests, a new function "bpf_fentry_shadow_test" was created both in vmlinux and in bpf_testmod.

The previous commit fixed a bug which caused this test to fail. The verifier would always use the vmlinux function's address as the target trampoline address, hence trying to attach two programs to the same trampoline.

---

<...>

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +5 -0
new file mode 100644
index 000000000000..0c604a0f22ca
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Red Hat */
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include "bpf/libbpf_internal.h"

tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c line 15

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +10 -0
+#include "cgroup_helpers.h"
+
+static const char *module_name = "bpf_testmod";
+static const char *symbol_name = "bpf_fentry_shadow_test";
+
+int get_bpf_testmod_btf_fd(void)
+{
+       struct bpf_btf_info info;
+       char name[64];
+       __u32 id, len;

We need to initialize 'id'.

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +1 -0
+       int err, fd;

<...>

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +4 -0
+}
+
+void test_module_fentry_shadow(void)
+{

<...>

tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c line 89

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +6 -0
+
+       btf_id[0] = btf__find_by_name(vmlinux_btf, symbol_name);
+       if (!ASSERT_GT(btf_id[0], 0, "btf_find_by_name"))
+               goto out;
+
+       btf_id[1] = btf__find_by_name(mod_btf, symbol_name);

btf__find_by_name_kind() may be better. It skips the name comparison if the kind doesn't match.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +3 -0
+       if (!ASSERT_GT(btf_id[1], 0, "btf_find_by_name"))
+               goto out;
+

<...>

tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c line 111

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +4 -0
+       err = bpf_prog_test_run_opts(prog_fd[0], &test_opts);
+       ASSERT_OK(err, "running test");
+
+out:

We also need to btf__free vmlinux_btf and mod_btf.

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c new file +9 -1
+       for (i = 0; i < 2; i++) {
+               if (btf_fd[i])
+                       close(btf_fd[i]);
+               if (prog_fd[i])
+                       close(prog_fd[i]);
+               if (link_fd[i])
+                       close(link_fd[i]);
+       }
+}
--

2.38.1