Re: [PATCH bpf-next v5 8/8] resolve_btfids: Change in-place update with raw binary output

bot+bpf-ci
2025-12-19T00:53:13Z

[-- Attachment #1: Type: text/plain, Size: 2072 bytes --]

> diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
> index e721e20a2bbd..7127396c5f50 100644
> --- a/tools/bpf/resolve_btfids/main.c
> +++ b/tools/bpf/resolve_btfids/main.c

[ ... ]

> @@ -587,6 +563,19 @@ static int load_btf(struct object *obj)
> obj->base_btf = base_btf;
> obj->btf = btf;
>
> + if (obj->base_btf && obj->distill_base) {
> + err = btf__distill_base(obj->btf, &base_btf, &btf);
> + if (err) {
> + pr_err("FAILED to distill base BTF: %s\n", strerror(errno));
> + goto out_err;
> + }
> +
> + btf__free(obj->btf);
> + btf__free(obj->base_btf);
> + obj->btf = btf;
> + obj->base_btf = base_btf;
> + }
> +
> return 0;
>
> out_err:
> + btf__free(base_btf);
> + btf__free(btf);
> return err;
> }

Can this lead to a double free if btf__distill_base() fails?

At the point of calling btf__distill_base(), the local variables base_btf
and btf have already been assigned to obj->base_btf and obj->btf. If
btf__distill_base() fails, it does not modify its output parameters, so
base_btf and btf still point to the same memory as obj->base_btf and
obj->btf.

The out_err path then frees base_btf and btf (the original BTF objects).
But obj->base_btf and obj->btf still point to this freed memory. When
load_btf() returns an error to main(), the cleanup at the out: label
calls btf__free(obj.base_btf) and btf__free(obj.btf), which would free
the same memory a second time.

The execution path would be:
main() -> load_btf() -> btf__distill_base() fails
-> out_err frees base_btf and btf
-> return err to main()
-> main() goto out
-> btf__free(obj.base_btf) and btf__free(obj.btf) -> double free

Perhaps the out_err path should set obj->base_btf = NULL and obj->btf =
NULL after freeing, or load_btf() should clear obj->base_btf/obj->btf
before going to out_err?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20355860150