From: Alan Maguire <alan.maguire@oracle.com>
To: bot+bpf-ci@kernel.org, andrii@kernel.org, ast@kernel.org
Cc: daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com,
song@kernel.org, yonghong.song@linux.dev,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me,
haoluo@google.com, jolsa@kernel.org, qmo@kernel.org,
ihor.solodrai@linux.dev, mykyta.yatsenko5@gmail.com,
dwarves@vger.kernel.org, bpf@vger.kernel.org,
martin.lau@kernel.org, clm@meta.com
Subject: Re: [PATCH v9 bpf-next 02/10] libbpf: Support layout section handling in BTF
Date: Thu, 26 Feb 2026 15:55:56 +0000 [thread overview]
Message-ID: <37ec740d-b7d1-410a-a2ea-331d19b59f5e@oracle.com> (raw)
In-Reply-To: <1e030700935a6f99789250a8841c908cfda89f4e5cd215ada2485b02c07994c7@mail.kernel.org>
On 26/02/2026 10:05, bot+bpf-ci@kernel.org wrote:
>> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
>> index 83fe79ffcb8f..3438cc15bb7a 100644
>> --- a/tools/lib/bpf/btf.c
>> +++ b/tools/lib/bpf/btf.c
>
> [ ... ]
>
>> @@ -224,66 +242,95 @@ static void btf_bswap_hdr(struct btf_header *h)
>
> [ ... ]
>
>> static int btf_parse_hdr(struct btf *btf)
>> {
>> - struct btf_header *hdr = btf->hdr;
>> + struct btf_header *hdr = btf->raw_data;
>> + __u32 hdr_len = hdr->hdr_len;
>
> [ ... ]
>
>> + memcpy(&btf->hdr, hdr, min((size_t)hdr_len, sizeof(struct btf_header)));
>
> The memcpy here correctly caps the copy at sizeof(struct btf_header),
> but btf->hdr.hdr_len retains the original value from the raw data.
> If the input BTF has a future-extended header where hdr_len exceeds
> sizeof(struct btf_header), only sizeof(struct btf_header) bytes are
> stored, yet hdr_len remembers the larger original value. This
> matters in btf_get_raw_data() below.
>
> [ ... ]
>
>> @@ -1601,7 +1664,7 @@ static const void *btf_strs_data(const struct btf *btf)
>>
>> static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
>> {
>> - struct btf_header *hdr = btf->hdr;
>> + const struct btf_header *hdr = &btf->hdr;
>> struct btf_type *t;
>> void *data, *p;
>> __u32 data_sz;
>
> [ ... ]
>
>> memcpy(p, hdr, hdr->hdr_len);
>
> When hdr_len > sizeof(struct btf_header), does this read past the
> end of the embedded btf->hdr struct? hdr points to &btf->hdr which
> is sizeof(struct btf_header) bytes, but hdr->hdr_len can be larger
> if the parsed BTF had a future-extended header. The extra bytes
> would come from adjacent struct btf fields (types_data, etc.),
> leaking internal state into the serialized output.
>
> Previously, btf->hdr pointed directly into raw_data where the full
> header (including any extension bytes) was available. The switch to
> an embedded struct loses those extra bytes, but the serialization
> still tries to write hdr_len bytes.
>
> One possible fix:
>
> memcpy(p, hdr, min((__u32)sizeof(struct btf_header), hdr->hdr_len));
> if (hdr->hdr_len > sizeof(struct btf_header))
> memset(p + sizeof(struct btf_header), 0,
> hdr->hdr_len - sizeof(struct btf_header));
>
yep, this is a good catch, will fix.
>> if (swap_endian)
>> - btf_bswap_hdr(p);
>> + btf_bswap_hdr(p, hdr->hdr_len);
>> p += hdr->hdr_len;
>
> [ ... ]
>
>
> ---
> 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/22436234361
>
> AI-authorship-score: low
> AI-authorship-explanation: The patch went through nine review iterations on lore with detailed human reviewer interactions, follows established libbpf patterns, and the subtle header over-read is characteristic of human-authored refactoring.
> issues-found: 1
> issue-severity-score: medium
> issue-severity-explanation: btf_get_raw_data() reads past the embedded btf->hdr struct into adjacent fields when hdr_len exceeds sizeof(struct btf_header), silently leaking internal state into serialized BTF output.
next prev parent reply other threads:[~2026-02-26 15:56 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-26 8:56 [PATCH v9 bpf-next 00/10] Add BTF layout to BTF Alan Maguire
2026-02-26 8:56 ` [PATCH v9 bpf-next 01/10] btf: add BTF kind layout encoding to UAPI Alan Maguire
2026-02-26 10:06 ` bot+bpf-ci
2026-02-26 8:56 ` [PATCH v9 bpf-next 02/10] libbpf: Support layout section handling in BTF Alan Maguire
2026-02-26 10:05 ` bot+bpf-ci
2026-02-26 15:55 ` Alan Maguire [this message]
2026-02-26 8:56 ` [PATCH v9 bpf-next 03/10] libbpf: use layout to compute an unknown kind size Alan Maguire
2026-02-26 10:05 ` bot+bpf-ci
2026-02-26 8:56 ` [PATCH v9 bpf-next 04/10] libbpf: Add layout encoding support Alan Maguire
2026-02-26 10:05 ` bot+bpf-ci
2026-02-26 14:46 ` Alan Maguire
2026-02-26 8:56 ` [PATCH v9 bpf-next 05/10] libbpf: BTF validation can use layout for unknown kinds Alan Maguire
2026-02-26 8:56 ` [PATCH v9 bpf-next 06/10] btf: support kernel parsing of BTF with layout info Alan Maguire
2026-02-26 10:05 ` bot+bpf-ci
2026-02-26 16:25 ` Alan Maguire
2026-02-26 16:34 ` Alexei Starovoitov
2026-02-26 16:39 ` Alan Maguire
2026-02-26 8:56 ` [PATCH v9 bpf-next 07/10] selftests/bpf: test kind encoding/decoding Alan Maguire
2026-02-26 8:56 ` [PATCH v9 bpf-next 08/10] bpftool: add BTF dump "format meta" to dump header/metadata Alan Maguire
2026-02-26 10:05 ` bot+bpf-ci
2026-02-26 8:56 ` [PATCH v9 bpf-next 09/10] bpftool: Update doc to describe bpftool btf dump .. format metadata Alan Maguire
2026-02-26 10:05 ` bot+bpf-ci
2026-02-26 8:56 ` [PATCH v9 bpf-next 10/10] kbuild, bpf: Specify "layout" optional feature Alan Maguire
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=37ec740d-b7d1-410a-a2ea-331d19b59f5e@oracle.com \
--to=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bot+bpf-ci@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=clm@meta.com \
--cc=daniel@iogearbox.net \
--cc=dwarves@vger.kernel.org \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykyta.yatsenko5@gmail.com \
--cc=qmo@kernel.org \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--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