mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-09-22 09:34:56 +02:00
libbpf: Prevent double close and leak of btf objects
[ Upstream commit380044c40b] Sashiko found possible double close of btf object fd [1], which happens when strdup in load_module_btfs fails at which point the obj->btf_module_cnt is already incremented. The error path close btf fd and so does later cleanup code in bpf_object_post_load_cleanup function. Also libbpf_ensure_mem failure leaves btf object not assigned and it's leaked. Replacing the err_out label with break to make the error path less confusing as suggested by Alan. Incrementing obj->btf_module_cnt only if there's no failure and releasing btf object in error path. Fixes:91abb4a6d7("libbpf: Support attachment of BPF tracing programs to kernel modules") [1] https://sashiko.dev/#/patchset/20260324081846.2334094-1-jolsa%40kernel.org Signed-off-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/r/20260416100034.1610852-1-jolsa@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
8abad856e2
commit
e8bd8274ae
+12
-11
@@ -5746,11 +5746,12 @@ static int load_module_btfs(struct bpf_object *obj)
|
||||
info.name = ptr_to_u64(name);
|
||||
info.name_len = sizeof(name);
|
||||
|
||||
btf = NULL;
|
||||
err = bpf_btf_get_info_by_fd(fd, &info, &len);
|
||||
if (err) {
|
||||
err = -errno;
|
||||
pr_warn("failed to get BTF object #%d info: %s\n", id, errstr(err));
|
||||
goto err_out;
|
||||
break;
|
||||
}
|
||||
|
||||
/* ignore non-module BTFs */
|
||||
@@ -5764,15 +5765,15 @@ static int load_module_btfs(struct bpf_object *obj)
|
||||
if (err) {
|
||||
pr_warn("failed to load module [%s]'s BTF object #%d: %s\n",
|
||||
name, id, errstr(err));
|
||||
goto err_out;
|
||||
break;
|
||||
}
|
||||
|
||||
err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
|
||||
sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
|
||||
if (err)
|
||||
goto err_out;
|
||||
break;
|
||||
|
||||
mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
|
||||
mod_btf = &obj->btf_modules[obj->btf_module_cnt];
|
||||
|
||||
mod_btf->btf = btf;
|
||||
mod_btf->id = id;
|
||||
@@ -5780,16 +5781,16 @@ static int load_module_btfs(struct bpf_object *obj)
|
||||
mod_btf->name = strdup(name);
|
||||
if (!mod_btf->name) {
|
||||
err = -ENOMEM;
|
||||
goto err_out;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
|
||||
err_out:
|
||||
close(fd);
|
||||
return err;
|
||||
obj->btf_module_cnt++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (err) {
|
||||
btf__free(btf);
|
||||
close(fd);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static struct bpf_core_cand_list *
|
||||
|
||||
Reference in New Issue
Block a user