~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/bpf_mod_race.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <vmlinux.h>
  3 #include <bpf/bpf_helpers.h>
  4 #include <bpf/bpf_tracing.h>
  5 
  6 const volatile struct {
  7         /* thread to activate trace programs for */
  8         pid_t tgid;
  9         /* return error from __init function */
 10         int inject_error;
 11         /* uffd monitored range start address */
 12         void *fault_addr;
 13 } bpf_mod_race_config = { -1 };
 14 
 15 int bpf_blocking = 0;
 16 int res_try_get_module = -1;
 17 
 18 static __always_inline bool check_thread_id(void)
 19 {
 20         struct task_struct *task = bpf_get_current_task_btf();
 21 
 22         return task->tgid == bpf_mod_race_config.tgid;
 23 }
 24 
 25 /* The trace of execution is something like this:
 26  *
 27  * finit_module()
 28  *   load_module()
 29  *     prepare_coming_module()
 30  *       notifier_call(MODULE_STATE_COMING)
 31  *         btf_parse_module()
 32  *         btf_alloc_id()               // Visible to userspace at this point
 33  *         list_add(btf_mod->list, &btf_modules)
 34  *     do_init_module()
 35  *       freeinit = kmalloc()
 36  *       ret = mod->init()
 37  *         bpf_prog_widen_race()
 38  *           bpf_copy_from_user()
 39  *             ...<sleep>...
 40  *       if (ret < 0)
 41  *         ...
 42  *         free_module()
 43  * return ret
 44  *
 45  * At this point, module loading thread is blocked, we now load the program:
 46  *
 47  * bpf_check
 48  *   add_kfunc_call/check_pseudo_btf_id
 49  *     btf_try_get_module
 50  *       try_get_module_live == false
 51  *     return -ENXIO
 52  *
 53  * Without the fix (try_get_module_live in btf_try_get_module):
 54  *
 55  * bpf_check
 56  *   add_kfunc_call/check_pseudo_btf_id
 57  *     btf_try_get_module
 58  *       try_get_module == true
 59  *     <store module reference in btf_kfunc_tab or used_btf array>
 60  *   ...
 61  * return fd
 62  *
 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
 67  * MODULE_STATE_LIVE.
 68  */
 69 
 70 SEC("fmod_ret.s/bpf_fentry_test1")
 71 int BPF_PROG(widen_race, int a, int ret)
 72 {
 73         char dst;
 74 
 75         if (!check_thread_id())
 76                 return 0;
 77         /* Indicate that we will attempt to block */
 78         bpf_blocking = 1;
 79         bpf_copy_from_user(&dst, 1, bpf_mod_race_config.fault_addr);
 80         return bpf_mod_race_config.inject_error;
 81 }
 82 
 83 SEC("fexit/do_init_module")
 84 int BPF_PROG(fexit_init_module, struct module *mod, int ret)
 85 {
 86         if (!check_thread_id())
 87                 return 0;
 88         /* Indicate that we finished blocking */
 89         bpf_blocking = 2;
 90         return 0;
 91 }
 92 
 93 SEC("fexit/btf_try_get_module")
 94 int BPF_PROG(fexit_module_get, const struct btf *btf, struct module *mod)
 95 {
 96         res_try_get_module = !!mod;
 97         return 0;
 98 }
 99 
100 char _license[] SEC("license") = "GPL";
101 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php