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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2/21/23 16:17, Jiri Olsa wrote:
On Mon, Feb 20, 2023 at 11:42:52AM +0100, Viktor Malik wrote:

SNIP

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 388245e8826e..6da830df3ea5 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -24,6 +24,7 @@
  #include <linux/bpf_lsm.h>
  #include <linux/btf_ids.h>
  #include <linux/poison.h>
+#include "../module/internal.h"
#include "disasm.h" @@ -16868,6 +16869,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
  	const char *tname;
  	struct btf *btf;
  	long addr = 0;
+	struct module *mod = NULL;
if (!btf_id) {
  		bpf_log(log, "Tracing programs must provide btf_id\n");
@@ -17041,7 +17043,15 @@ 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)) {
+				mod = btf_try_get_module(btf);
+				if (mod)
+					addr = find_kallsyms_symbol_value(mod, tname);
+				else
+					addr = 0;
+			} else {
+				addr = kallsyms_lookup_name(tname);
+			}

there are some error paths below this point which I think we could
hit also for module id/address, so we need to put the mod ref

Right, I didn't notice those, thanks. I'll fix that.


also there's bpf_trampoline_link_cgroup_shim caller of
bpf_check_attach_target, but I'm not sure that could endup with
id/address in module code

I did check this and IIUC, this is for BPF_LSM_CGROUP programs and
bpf_lsm_* hooks are always placed in vmlinux, so this shouldn't occur.

Viktor


jirka

  			if (!addr) {
  				bpf_log(log,
  					"The address of function %s cannot be found\n",
@@ -17105,6 +17115,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
  	tgt_info->tgt_addr = addr;
  	tgt_info->tgt_name = tname;
  	tgt_info->tgt_type = t;
+	tgt_info->tgt_mod = mod;
  	return 0;
  }
@@ -17184,6 +17195,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
  	/* store info about the attachment target that will be used later */
  	prog->aux->attach_func_proto = tgt_info.tgt_type;
  	prog->aux->attach_func_name = tgt_info.tgt_name;
+	prog->aux->mod = tgt_info.tgt_mod;
if (tgt_prog) {
  		prog->aux->saved_dst_prog_type = tgt_prog->type;
diff --git a/kernel/module/internal.h b/kernel/module/internal.h
index 2e2bf236f558..5cb103a46018 100644
--- a/kernel/module/internal.h
+++ b/kernel/module/internal.h
@@ -256,6 +256,11 @@ static inline bool sect_empty(const Elf_Shdr *sect)
  static inline void init_build_id(struct module *mod, const struct load_info *info) { }
  static inline void layout_symtab(struct module *mod, struct load_info *info) { }
  static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
+static inline unsigned long find_kallsyms_symbol_value(struct module *mod
+						       const char *name)
+{
+	return 0;
+}
  #endif /* CONFIG_KALLSYMS */
#ifdef CONFIG_SYSFS
--
2.39.1






[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux