Skip to contentSkip to conversation

bpf: Take module reference for trampoline in module

PATCH Superseded Jiri Olsa 3 participants 3 messages Mar 24, 2021 – Mar 25, 2021

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH v2 1/1 bpf: Take module reference for trampoline in module

Currently module can be unloaded even if there's a trampoline register in it. It's easily reproduced by running in parallel:

  # while :; do ./test_progs -t module_attach; done
  # while :; do rmmod bpf_testmod; sleep 0.5; done

Taking the module reference in case the trampoline's ip is within the module code. Releasing it when the trampoline's ip is unregistered.

---
v2 changes:
- fixed ip_module_put to do preempt_disable/preempt_enable

 kernel/bpf/trampoline.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
kernel/bpf/trampoline.c +31 -0
index 1f3a4be4b175..39e4280f94e4 100644
@@ -87,6 +87,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
 	return tr;
 }
 
+static struct module *ip_module_get(unsigned long ip)
+{
+	struct module *mod;
+	int err = 0;
+
+	preempt_disable();
+	mod = __module_text_address(ip);
+	if (mod && !try_module_get(mod))
+		err = -ENOENT;
+	preempt_enable();
+	return err ? ERR_PTR(err) : mod;
+}
+
+static void ip_module_put(unsigned long ip)
+{
+	preempt_disable();
+	module_put(__module_text_address(ip));
+	preempt_enable();
+}
+
 static int is_ftrace_location(void *ip)
 {
 	long addr;
@@ -108,6 +128,9 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
 		ret = unregister_ftrace_direct((long)ip, (long)old_addr);
 	else
 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
+
+	if (!ret)
+		ip_module_put((unsigned long) ip);
 	return ret;
 }
 
@@ -126,6 +149,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad
 /* first time registering */
 static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
 {
+	struct module *mod;
 	void *ip = tr->func.addr;
 	int ret;
 
@@ -134,10 +158,17 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
 		return ret;
 	tr->func.ftrace_managed = ret;
 
+	mod = ip_module_get((unsigned long) ip);
+	if (IS_ERR(mod))
+		return -ENOENT;
+
 	if (tr->func.ftrace_managed)
 		ret = register_ftrace_direct((long)ip, (long)new_addr);
 	else
 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
+
+	if (ret)
+		module_put(mod);
 	return ret;
 }
 
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 34 lines · Jiri Olsa

Currently module can be unloaded even if there's a trampoline register in it. It's easily reproduced by running in parallel:

   # while :; do ./test_progs -t module_attach; done
   # while :; do rmmod bpf_testmod; sleep 0.5; done

Taking the module reference in case the trampoline's ip is within the module code. Releasing it when the trampoline's ip is unregistered.

---
v2 changes:
- fixed ip_module_put to do preempt_disable/preempt_enable

  kernel/bpf/trampoline.c | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)
kernel/bpf/trampoline.c +8 -0
index 1f3a4be4b175..39e4280f94e4 100644
@@ -87,6 +87,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
  	return tr;
  }
 
+static struct module *ip_module_get(unsigned long ip)
+{
+	struct module *mod;
+	int err = 0;
+
+	preempt_disable();
+	mod = __module_text_address(ip);
+	if (mod && !try_module_get(mod))

kernel/bpf/trampoline.c static struct bpf_trampoline *bpf_trampoline_lookup(u64 key) · line 107

Quoted 10 lines
kernel/bpf/trampoline.c +10 -0
+		err = -ENOENT;
+	preempt_enable();
+	return err ? ERR_PTR(err) : mod;
+}
+
+static void ip_module_put(unsigned long ip)
+{
+	preempt_disable();
+	module_put(__module_text_address(ip));
+	preempt_enable();

Could we cache the mod pointer in tr instead of doing another addr search for dropping the ref?

Quoted 42 lines
kernel/bpf/trampoline.c +13 -0
+}
+
  static int is_ftrace_location(void *ip)
  {
  	long addr;
@@ -108,6 +128,9 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
  		ret = unregister_ftrace_direct((long)ip, (long)old_addr);
  	else
  		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
+
+	if (!ret)
+		ip_module_put((unsigned long) ip);
  	return ret;
  }
 
@@ -126,6 +149,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad
  /* first time registering */
  static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
  {
+	struct module *mod;
  	void *ip = tr->func.addr;
  	int ret;
 
@@ -134,10 +158,17 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
  		return ret;
  	tr->func.ftrace_managed = ret;
 
+	mod = ip_module_get((unsigned long) ip);
+	if (IS_ERR(mod))
+		return -ENOENT;
+
  	if (tr->func.ftrace_managed)
  		ret = register_ftrace_direct((long)ip, (long)new_addr);
  	else
  		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
+
+	if (ret)
+		module_put(mod);
  	return ret;
  }
 
#2 lore raw
Quoted 39 lines · Daniel Borkmann
Quoted 38 lines · Jiri Olsa

Currently module can be unloaded even if there's a trampoline register in it. It's easily reproduced by running in parallel:

   # while :; do ./test_progs -t module_attach; done
   # while :; do rmmod bpf_testmod; sleep 0.5; done

Taking the module reference in case the trampoline's ip is within the module code. Releasing it when the trampoline's ip is unregistered.

---
v2 changes:
- fixed ip_module_put to do preempt_disable/preempt_enable

  kernel/bpf/trampoline.c | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)
kernel/bpf/trampoline.c +13 -0
index 1f3a4be4b175..39e4280f94e4 100644
@@ -87,6 +87,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
  	return tr;
  }
+static struct module *ip_module_get(unsigned long ip)
+{
+	struct module *mod;
+	int err = 0;
+
+	preempt_disable();
+	mod = __module_text_address(ip);
+	if (mod && !try_module_get(mod))
+		err = -ENOENT;
+	preempt_enable();
+	return err ? ERR_PTR(err) : mod;
+}
+
Quoted 8 lines

kernel/bpf/trampoline.c static struct bpf_trampoline *bpf_trampoline_lookup(u64 key) · line 107

Quoted 5 lines
kernel/bpf/trampoline.c +5 -0
+static void ip_module_put(unsigned long ip)
+{
+	preempt_disable();
+	module_put(__module_text_address(ip));
+	preempt_enable();

Could we cache the mod pointer in tr instead of doing another addr search for dropping the ref?

right.. I moved it from the ftrace layer where this was not an option, so I did not realize bpf_trampoline could get extended, will send new version

thanks,
jirka

Quoted 40 lines
Quoted 39 lines
kernel/bpf/trampoline.c +13 -0
+}
+
  static int is_ftrace_location(void *ip)
  {
  	long addr;
@@ -108,6 +128,9 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
  		ret = unregister_ftrace_direct((long)ip, (long)old_addr);
  	else
  		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
+
+	if (!ret)
+		ip_module_put((unsigned long) ip);
  	return ret;
  }
@@ -126,6 +149,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad
  /* first time registering */
  static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
  {
+	struct module *mod;
  	void *ip = tr->func.addr;
  	int ret;
@@ -134,10 +158,17 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
  		return ret;
  	tr->func.ftrace_managed = ret;
+	mod = ip_module_get((unsigned long) ip);
+	if (IS_ERR(mod))
+		return -ENOENT;
+
  	if (tr->func.ftrace_managed)
  		ret = register_ftrace_direct((long)ip, (long)new_addr);
  	else
  		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
+
+	if (ret)
+		module_put(mod);
  	return ret;
  }