From: sashiko-bot@kernel.org
To: "Alan Maguire" <alan.maguire@oracle.com>
Cc: bpf@vger.kernel.org, linux-modules@vger.kernel.org,
"Petr Pavlu" <petr.pavlu@suse.com>
Subject: Re: [PATCH v4 bpf-next 4/7] btf: Preserve string relocation maps for split BTF
Date: Fri, 09 Oct 2026 12:01:47 +0000 [thread overview]
Message-ID: <sashiko-outbox-165464@kernel.org> (raw)
In-Reply-To: <20261009112324.1442319-5-alan.maguire@oracle.com>
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] btf: memory corruption and invalid string offsets with split base BTF
- [High] bpf: heap buffer overflow in btf_relocate_map_distilled_base() on split BTF
- [Medium] libbpf: NULL pointer dereference in btf_relocate() when btf has no base
- [Low] bpf: btf_relocate_rewrite_strs prints empty string in warning
--
commit 5e8ac630aea03f4aa4b3a237792c60ad3c7a0956
Author: Alan Maguire <alan.maguire@oracle.com>
btf: Preserve string relocation maps for split BTF
Make btf_relocate() optionally return the string-offset map it builds
while relocating a BTF relative to a distilled base. A subsequent split
BTF object can use that map to rebase references to base strings.
Also account for the accumulated base string offset when assigning a
new BTF base. This is required when the base is itself split.
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 592b71a838410..22dd123450c3e 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -1936,7 +1936,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
> {
> btf->base_btf = (struct btf *)base_btf;
> btf->start_id = btf_nr_types(base_btf);
> - btf->start_str_off = base_btf->hdr.str_len;
> + btf->start_str_off = base_btf->start_str_off + base_btf->hdr.str_len;
> }
[Severity: High]
Since this allows using a split BTF as the base, will it cause out-of-bounds
heap writes in tools/lib/bpf/btf_relocate.c:btf_relocate_map_distilled_base()?
When relocating against a split base BTF, the function allocates an array
using only the local string section length of the target base BTF:
base_name_cnt = calloc(r->base_str_len, sizeof(*base_name_cnt));
It then iterates over all types in the BTF hierarchy, indexing into this
array using the absolute name_off of inherited types:
if (base_name_cnt[base_t->name_off] < 255)
base_name_cnt[base_t->name_off]++;
Because the absolute name_off can span up to the total accumulated string
length (up to millions of bytes for vmlinux), this index can far exceed
the local string length and corrupt the heap.
Additionally, in btf_relocate_rewrite_strs(), local string offsets are
incremented using only the local string section length:
*str_off += r->base_str_len - r->dist_str_len;
Does this need to account for base_btf->start_str_off as well, to prevent
the rewritten offsets from incorrectly pointing into the base BTF's bounds?
[ ... ]
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 512b3962f75f8..d2ff6f5d6d5b6 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -6640,7 +6640,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
>
> int btf__relocate(struct btf *btf, const struct btf *base_btf)
> {
> - int err = btf_relocate(btf, base_btf, NULL);
> + int err = btf_relocate(btf, base_btf, NULL, NULL);
[Severity: Medium]
If a user calls btf__relocate() with a root BTF (which has no base BTF),
will this cause a NULL pointer dereference?
Inside btf_relocate(), r.dist_base_btf is populated using btf__base_btf(btf),
which returns NULL for a root BTF. However, the function proceeds to
dereference it without checking:
r.str_map = calloc(btf_header(r.dist_base_btf)->str_len,
sizeof(*r.str_map));
Since btf_header(NULL) returns a near-NULL pointer, accessing str_len will
trigger a page fault and crash the caller.
[ ... ]
> diff --git a/tools/lib/bpf/btf_relocate.c b/tools/lib/bpf/btf_relocate.c
> index df5fa4bd87d69..e55dd75f6c950 100644
> --- a/tools/lib/bpf/btf_relocate.c
> +++ b/tools/lib/bpf/btf_relocate.c
> @@ -441,7 +441,8 @@ static int btf_relocate_rewrite_strs(struct btf_relocate *r, __u32 i)
> /* If successful, output of relocation is updated BTF with base BTF pointing
> * at base_btf, and type ids, strings adjusted accordingly.
> */
> -int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)
> +int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,
> + __u32 **str_map)
> {
[Severity: Low]
When strings fail to map in btf_relocate_rewrite_strs() during relocation,
does the warning message print an empty string instead of the actual unmapped
string?
off = r->str_map[*str_off];
if (!off) {
pr_warn("string '%s' [offset %u] is not mapped to base BTF\n",
btf__str_by_offset(r->btf, off), *str_off);
Since off is guaranteed to be 0 here, and offset 0 in BTF always corresponds
to the empty string, the warning will always print "string '' [offset X]".
Should this use *str_off to retrieve and print the correct unmapped string?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261009112324.1442319-1-alan.maguire@oracle.com?part=4
next prev parent reply other threads:[~2026-10-09 12:01 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-09 11:23 [PATCH v4 bpf-next 0/7] kbuild, bpf: Support inline info in BTF Alan Maguire
2026-10-09 11:23 ` [PATCH v4 bpf-next 1/7] resolve_btfids: Build separate main and inline BTF objects Alan Maguire
2026-10-09 11:23 ` [PATCH v4 bpf-next 2/7] resolve_btfids: Populate BTF link section Alan Maguire
2026-10-09 12:24 ` bot+bpf-ci
2026-10-09 11:23 ` [PATCH v4 bpf-next 3/7] kbuild, btf: Expose vmlinux inline BTF through sysfs Alan Maguire
2026-10-09 11:23 ` [PATCH v4 bpf-next 4/7] btf: Preserve string relocation maps for split BTF Alan Maguire
2026-10-09 12:01 ` sashiko-bot [this message]
2026-10-09 14:00 ` Alan Maguire
2026-10-09 11:23 ` [PATCH v4 bpf-next 5/7] btf: Relocate and expose module inline BTF Alan Maguire
2026-10-09 11:23 ` [PATCH v4 bpf-next 6/7] selftests/bpf: Test BTF sysfs inline representations Alan Maguire
2026-10-09 12:25 ` sashiko-bot
2026-10-11 14:29 ` Alexei Starovoitov
2026-10-09 11:23 ` [PATCH v4 bpf-next 7/7] selftests/bpf: Add a test verifying inline information Alan Maguire
2026-10-09 12:33 ` sashiko-bot
2026-10-11 14:40 ` [PATCH v4 bpf-next 0/7] kbuild, bpf: Support inline info in BTF patchwork-bot+netdevbpf
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=sashiko-outbox-165464@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=alan.maguire@oracle.com \
--cc=bpf@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=petr.pavlu@suse.com \
--cc=sashiko-reviews@lists.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