Skip to content

Commit 861de02

Browse files
olsajiriAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d6fe1cf commit 861de02

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct bpf_local_storage;
4040
struct bpf_local_storage_map;
4141
struct kobject;
4242
struct mem_cgroup;
43+
struct module;
4344

4445
extern struct idr btf_idr;
4546
extern spinlock_t btf_idr_lock;
@@ -623,6 +624,7 @@ struct bpf_trampoline {
623624
/* Executable image of trampoline */
624625
struct bpf_tramp_image *cur_image;
625626
u64 selector;
627+
struct module *mod;
626628
};
627629

628630
struct bpf_attach_target_info {

kernel/bpf/trampoline.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/btf.h>
1010
#include <linux/rcupdate_trace.h>
1111
#include <linux/rcupdate_wait.h>
12+
#include <linux/module.h>
1213

1314
/* dummy _ops. The verifier will operate on target program's ops. */
1415
const struct bpf_verifier_ops bpf_extension_verifier_ops = {
@@ -87,6 +88,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
8788
return tr;
8889
}
8990

91+
static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
92+
{
93+
struct module *mod;
94+
int err = 0;
95+
96+
preempt_disable();
97+
mod = __module_text_address((unsigned long) tr->func.addr);
98+
if (mod && !try_module_get(mod))
99+
err = -ENOENT;
100+
preempt_enable();
101+
tr->mod = mod;
102+
return err;
103+
}
104+
105+
static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
106+
{
107+
module_put(tr->mod);
108+
tr->mod = NULL;
109+
}
110+
90111
static int is_ftrace_location(void *ip)
91112
{
92113
long addr;
@@ -108,6 +129,9 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
108129
ret = unregister_ftrace_direct((long)ip, (long)old_addr);
109130
else
110131
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
132+
133+
if (!ret)
134+
bpf_trampoline_module_put(tr);
111135
return ret;
112136
}
113137

@@ -134,10 +158,16 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
134158
return ret;
135159
tr->func.ftrace_managed = ret;
136160

161+
if (bpf_trampoline_module_get(tr))
162+
return -ENOENT;
163+
137164
if (tr->func.ftrace_managed)
138165
ret = register_ftrace_direct((long)ip, (long)new_addr);
139166
else
140167
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
168+
169+
if (ret)
170+
bpf_trampoline_module_put(tr);
141171
return ret;
142172
}
143173

0 commit comments

Comments
 (0)