Skip to content

Commit 18688de

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Fix UAF due to race between btf_try_get_module and load_module
While working on code to populate kfunc BTF ID sets for module BTF from its initcall, I noticed that by the time the initcall is invoked, the module BTF can already be seen by userspace (and the BPF verifier). The existing btf_try_get_module calls try_module_get which only fails if mod->state == MODULE_STATE_GOING, i.e. it can increment module reference when module initcall is happening in parallel. Currently, BTF parsing happens from MODULE_STATE_COMING notifier callback. At this point, the module initcalls have not been invoked. The notifier callback parses and prepares the module BTF, allocates an ID, which publishes it to userspace, and then adds it to the btf_modules list allowing the kernel to invoke btf_try_get_module for the BTF. However, at this point, the module has not been fully initialized (i.e. its initcalls have not finished). The code in module.c can still fail and free the module, without caring for other users. However, nothing stops btf_try_get_module from succeeding between the state transition from MODULE_STATE_COMING to MODULE_STATE_LIVE. This leads to a use-after-free issue when BPF program loads successfully in the state transition, load_module's do_init_module call fails and frees the module, and BPF program fd on close calls module_put for the freed module. Future patch has test case to verify we don't regress in this area in future. There are multiple points after prepare_coming_module (in load_module) where failure can occur and module loading can return error. We illustrate and test for the race using the last point where it can practically occur (in module __init function). An illustration of the race: CPU 0 CPU 1 load_module notifier_call(MODULE_STATE_COMING) btf_parse_module btf_alloc_id // Published to userspace list_add(&btf_mod->list, btf_modules) mod->init(...) ... ^ bpf_check | check_pseudo_btf_id | btf_try_get_module | returns true | ... ... | module __init in progress return prog_fd | ... ... V if (ret < 0) free_module(mod) ... close(prog_fd) ... bpf_prog_free_deferred module_put(used_btf.mod) // use-after-free We fix this issue by setting a flag BTF_MODULE_F_LIVE, from the notifier callback when MODULE_STATE_LIVE state is reached for the module, so that we return NULL from btf_try_get_module for modules that are not fully formed. Since try_module_get already checks that module is not in MODULE_STATE_GOING state, and that is the only transition a live module can make before being removed from btf_modules list, this is enough to close the race and prevent the bug. A later selftest patch crafts the race condition artifically to verify that it has been fixed, and that verifier fails to load program (with ENXIO). Lastly, a couple of comments: 1. Even if this race didn't exist, it seems more appropriate to only access resources (ksyms and kfuncs) of a fully formed module which has been initialized completely. 2. This patch was born out of need for synchronization against module initcall for the next patch, so it is needed for correctness even without the aforementioned race condition. The BTF resources initialized by module initcall are set up once and then only looked up, so just waiting until the initcall has finished ensures correct behavior. Fixes: 541c3ba ("bpf: Support BPF ksym variables in kernel modules") Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e80f2a0 commit 18688de

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

kernel/bpf/btf.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6200,12 +6200,17 @@ bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
62006200
return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
62016201
}
62026202

6203+
enum {
6204+
BTF_MODULE_F_LIVE = (1 << 0),
6205+
};
6206+
62036207
#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
62046208
struct btf_module {
62056209
struct list_head list;
62066210
struct module *module;
62076211
struct btf *btf;
62086212
struct bin_attribute *sysfs_attr;
6213+
int flags;
62096214
};
62106215

62116216
static LIST_HEAD(btf_modules);
@@ -6233,7 +6238,8 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
62336238
int err = 0;
62346239

62356240
if (mod->btf_data_size == 0 ||
6236-
(op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
6241+
(op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE &&
6242+
op != MODULE_STATE_GOING))
62376243
goto out;
62386244

62396245
switch (op) {
@@ -6291,6 +6297,17 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
62916297
btf_mod->sysfs_attr = attr;
62926298
}
62936299

6300+
break;
6301+
case MODULE_STATE_LIVE:
6302+
mutex_lock(&btf_module_mutex);
6303+
list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
6304+
if (btf_mod->module != module)
6305+
continue;
6306+
6307+
btf_mod->flags |= BTF_MODULE_F_LIVE;
6308+
break;
6309+
}
6310+
mutex_unlock(&btf_module_mutex);
62946311
break;
62956312
case MODULE_STATE_GOING:
62966313
mutex_lock(&btf_module_mutex);
@@ -6338,7 +6355,12 @@ struct module *btf_try_get_module(const struct btf *btf)
63386355
if (btf_mod->btf != btf)
63396356
continue;
63406357

6341-
if (try_module_get(btf_mod->module))
6358+
/* We must only consider module whose __init routine has
6359+
* finished, hence we must check for BTF_MODULE_F_LIVE flag,
6360+
* which is set from the notifier callback for
6361+
* MODULE_STATE_LIVE.
6362+
*/
6363+
if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module))
63426364
res = btf_mod->module;
63436365

63446366
break;

0 commit comments

Comments
 (0)