From: Alan Maguire <alan.maguire@oracle.com>
To: bot+bpf-ci@kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, eddyz87@gmail.com
Cc: jolsa@kernel.org, ihor.solodrai@linux.dev,
yonghong.song@linux.dev, song@kernel.org, qmo@kernel.org,
martin.lau@linux.dev, memxor@gmail.com, emil@etsalapatis.com,
mcgrof@kernel.org, petr.pavlu@suse.com, tj@kernel.org,
kees@kernel.org, nathan@kernel.org, arnd@arndb.de,
puranjay@kernel.org, yatsenko@meta.com, atenart@kernel.org,
ojeda@kernel.org, linux-modules@vger.kernel.org,
arnaldo.melo@gmail.com, bpf@vger.kernel.org,
martin.lau@kernel.org, mason@kernel.org
Subject: Re: [PATCH v3 bpf-next 1/7] resolve_btfids: Build separate main and inline BTF objects
Date: Tue, 6 Oct 2026 18:30:50 +0100 [thread overview]
Message-ID: <2734b5d8-b16f-41c9-bd34-467f15cf4e78@oracle.com> (raw)
In-Reply-To: <56cb8412ae0f25a42289986ef6005a01b17dcc487692d9d21ae6de977eaee66e@mail.kernel.org>
On 03/10/2026 21:27, bot+bpf-ci@kernel.org wrote:
>> resolve_btfids: Build separate main and inline BTF objects
>
> TL;DR: In resolve_btfids --inline, extract_inline_btf() may free a base BTF
> that the new main BTF still points to, causing a possible use-after-free for
> modules with .BTF.base; in-tree builds seem unaffected.
>
>> diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
>> index 37d7e7224207..dcd289d16faf 100644
>> --- a/tools/bpf/resolve_btfids/main.c
>> +++ b/tools/bpf/resolve_btfids/main.c
>
> [ ... ]
>
>> @@ -1510,6 +1569,85 @@ static int btf2btf(struct object *obj)
>> return err;
>> }
>>
>
> [ ... ]
>
>> +static int extract_inline_btf(struct object *obj)
>> +{
>> + struct btf *btf = obj->btf, *main_btf, *inline_btf;
>> + __u32 type_cnt = btf__type_cnt(btf);
>> + __u8 *colors;
>> + int err;
>> +
>> + colors = calloc(type_cnt, sizeof(*colors));
>> + if (!colors)
>> + return -ENOMEM;
>> + err = color_btf(obj, colors);
>> + if (err)
>> + goto out;
>> + err = btf_split_by_color(btf, colors, &main_btf, &inline_btf);
>> + if (!err) {
>> + obj->btf = main_btf;
>> + obj->inline_btf = inline_btf;
>> + btf__free(btf);
> ^^^^
>
> Can this btf__free(btf) free a base BTF that the new obj->btf still points
> to?
>
> btf_split_by_color() creates main_btf with .base_btf set to
> btf__base_btf(src), and btf_new_empty() stores that pointer without taking
> ownership. btf__free() on the source also frees its base when the source
> owns it:
>
> if (btf->owns_base)
> btf__free(btf->base_btf);
>
> libbpf sets owns_base in btf_parse_elf() when the ELF passed to
> btf__parse_split() has a .BTF.base section and no base_btf was given.
>
> This looks reachable with "resolve_btfids --inline mod.ko" (no --btf_base,
> no --btf) on an out-of-tree module that already has .BTF, .BTF.base and
> .BTF_ids:
>
> load_btf()
> btf__parse_split(obj->path, NULL)
> owns_base = true, obj->base_btf stays NULL
> finalize_btf()
> distill is skipped because obj->base_btf is NULL
> sort_btf_by_name()
> extract_inline_btf()
> btf__free(btf)
> also frees the distilled base that main_btf->base_btf points to
> main()
> symbols_resolve()
> btf__type_by_id(obj->btf, ...)
> btf_type_by_id(btf->base_btf, type_id)
> reads freed memory
>
> Without --inline the same command works, because obj->btf keeps owning its
> base until main() frees it.
>
> The in-tree build does not appear to be affected, since gen-btf.sh passes a
> raw --btf file together with --btf_base, which leaves owns_base false. I did
> not find a fix for this later in the series.
>
> Should extract_inline_btf() keep the original BTF alive, for example in a
> field of struct object that main() frees after obj->btf? Alternatively,
> could it reject or handle the case where btf__base_btf(obj->btf) is set but
> obj->base_btf is NULL?
>
>> + }
>> +out:
>> + free(colors);
>> + return err;
>> +}
>
> [ ... ]
>
>
legit issue; we need to keep around obj.orig_btf for after coloring to be
safe. Also worth centralizing all the freeing of btfs in the main out:
path to remove a bunch of goto labels and adding a comment to note the
importance of btf__free() order. Will fix..
next prev parent reply other threads:[~2026-10-06 17:31 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-03 19:43 [PATCH v3 bpf-next 0/7] kbuild, bpf: Support inline info in BTF Alan Maguire
2026-10-03 19:43 ` [PATCH v3 bpf-next 1/7] resolve_btfids: Build separate main and inline BTF objects Alan Maguire
2026-10-03 20:27 ` bot+bpf-ci
2026-10-06 17:30 ` Alan Maguire [this message]
2026-10-03 19:43 ` [PATCH v3 bpf-next 2/7] resolve_btfids: Populate BTF link section Alan Maguire
2026-10-03 20:27 ` bot+bpf-ci
2026-10-03 19:43 ` [PATCH v3 bpf-next 3/7] kbuild, btf: Expose vmlinux inline BTF through sysfs Alan Maguire
2026-10-03 19:56 ` sashiko-bot
2026-10-06 18:12 ` Alan Maguire
2026-10-03 20:41 ` bot+bpf-ci
2026-10-06 18:42 ` Alan Maguire
2026-10-03 19:43 ` [PATCH v3 bpf-next 4/7] btf: Preserve string relocation maps for split BTF Alan Maguire
2026-10-03 19:43 ` [PATCH v3 bpf-next 5/7] btf: Relocate and expose module inline BTF Alan Maguire
2026-10-03 19:59 ` sashiko-bot
2026-10-06 18:56 ` Alan Maguire
2026-10-03 20:27 ` bot+bpf-ci
2026-10-03 19:43 ` [PATCH v3 bpf-next 6/7] selftests/bpf: Test BTF sysfs inline representations Alan Maguire
2026-10-03 19:54 ` sashiko-bot
2026-10-03 19:43 ` [PATCH v3 bpf-next 7/7] selftests/bpf: Add a test verifying inline information Alan Maguire
2026-10-03 19:54 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2734b5d8-b16f-41c9-bd34-467f15cf4e78@oracle.com \
--to=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=arnaldo.melo@gmail.com \
--cc=arnd@arndb.de \
--cc=ast@kernel.org \
--cc=atenart@kernel.org \
--cc=bot+bpf-ci@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=kees@kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=martin.lau@kernel.org \
--cc=martin.lau@linux.dev \
--cc=mason@kernel.org \
--cc=mcgrof@kernel.org \
--cc=memxor@gmail.com \
--cc=nathan@kernel.org \
--cc=ojeda@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=puranjay@kernel.org \
--cc=qmo@kernel.org \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=yatsenko@meta.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox