1 // SPDX-License-Identifier: GPL-2.0
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_tracing.h>
6 const volatile struct {
7 /* thread to activate trace programs for */
9 /* return error from __init function */
11 /* uffd monitored range start address */
13 } bpf_mod_race_config = { -1 };
16 int res_try_get_module = -1;
18 static __always_inline bool check_thread_id(void)
20 struct task_struct *task = bpf_get_current_task_btf();
22 return task->tgid == bpf_mod_race_config.tgid;
25 /* The trace of execution is something like this:
29 * prepare_coming_module()
30 * notifier_call(MODULE_STATE_COMING)
32 * btf_alloc_id() // Visible to userspace at this point
33 * list_add(btf_mod->list, &btf_modules)
35 * freeinit = kmalloc()
37 * bpf_prog_widen_race()
38 * bpf_copy_from_user()
45 * At this point, module loading thread is blocked, we now load the program:
48 * add_kfunc_call/check_pseudo_btf_id
50 * try_get_module_live == false
53 * Without the fix (try_get_module_live in btf_try_get_module):
56 * add_kfunc_call/check_pseudo_btf_id
58 * try_get_module == true
59 * <store module reference in btf_kfunc_tab or used_btf array>
63 * Now, if we inject an error in the blocked program, our module will be freed
64 * (going straight from MODULE_STATE_COMING to MODULE_STATE_GOING).
65 * Later, when bpf program is freed, it will try to module_put already freed
66 * module. This is why try_get_module_live returns false if mod->state is not
70 SEC("fmod_ret.s/bpf_fentry_test1")
71 int BPF_PROG(widen_race, int a, int ret)
75 if (!check_thread_id())
77 /* Indicate that we will attempt to block */
79 bpf_copy_from_user(&dst, 1, bpf_mod_race_config.fault_addr);
80 return bpf_mod_race_config.inject_error;
83 SEC("fexit/do_init_module")
84 int BPF_PROG(fexit_init_module, struct module *mod, int ret)
86 if (!check_thread_id())
88 /* Indicate that we finished blocking */
93 SEC("fexit/btf_try_get_module")
94 int BPF_PROG(fexit_module_get, const struct btf *btf, struct module *mod)
96 res_try_get_module = !!mod;
100 char _license[] SEC("license") = "GPL";