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, dwarves@vger.kernel.org,
bpf@vger.kernel.org, ttreyer@meta.com,
mykyta.yatsenko5@gmail.com, martin.lau@kernel.org, clm@meta.com
Subject: Re: [PATCH v8 bpf-next 02/10] libbpf: Support kind layout section handling in BTF
Date: Mon, 15 Dec 2025 16:03:03 +0000 [thread overview]
Message-ID: <0d31010a-994f-444f-8c94-4316229db268@oracle.com> (raw)
In-Reply-To: <d965ba27da4b59ef69f94b335575827debba459e27c86a10e63d5edeeb155e97@mail.kernel.org>
On 15/12/2025 09:38, bot+bpf-ci@kernel.org wrote:
>> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
>> index b136572e889a..8835aee6ee84 100644
>> --- a/tools/lib/bpf/btf.c
>> +++ b/tools/lib/bpf/btf.c
>
> [ ... ]
>
>> @@ -214,7 +232,7 @@ static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
>> return 0;
>> }
>>
>> -static void btf_bswap_hdr(struct btf_header *h)
>> +static void btf_bswap_hdr(struct btf_header *h, __u32 hdr_len)
>> {
>> h->magic = bswap_16(h->magic);
>> h->hdr_len = bswap_32(h->hdr_len);
>> @@ -222,54 +240,87 @@ static void btf_bswap_hdr(struct btf_header *h)
>> h->type_len = bswap_32(h->type_len);
>> h->str_off = bswap_32(h->str_off);
>> h->str_len = bswap_32(h->str_len);
>> + /* May be operating on raw data with hdr_len that does not include below fields */
>> + if (hdr_len >= sizeof(struct btf_header)) {
>> + h->kind_layout_off = bswap_32(h->kind_layout_off);
>> + h->kind_layout_len = bswap_32(h->kind_layout_len);
>> + }
>> }
>>
>> 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;
>> __u32 meta_left;
>>
>> - if (btf->raw_size < sizeof(struct btf_header)) {
>> + if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
>> pr_debug("BTF header not found\n");
>> return -EINVAL;
>> }
>>
>> if (hdr->magic == bswap_16(BTF_MAGIC)) {
>> btf->swapped_endian = true;
>> - if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
>> + hdr_len = bswap_32(hdr->hdr_len);
>> + if (hdr_len < offsetofend(struct btf_header, str_len)) {
>> pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
>> - bswap_32(hdr->hdr_len));
>> + hdr_len);
>> return -ENOTSUP;
>> }
>> - btf_bswap_hdr(hdr);
>> + btf_bswap_hdr(hdr, hdr_len);
> ^^^
>
> Does this result in hdr->hdr_len being double-swapped? In the swapped
> endianness path, hdr_len is set to bswap_32(hdr->hdr_len) and then
> btf_bswap_hdr() is called, which does h->hdr_len = bswap_32(h->hdr_len)
> again. This means hdr->hdr_len in the structure ends up with its original
> (incorrect) endianness while the local hdr_len variable has the correct
> swapped value.
>
> Later code then copies the incorrectly-swapped header into btf->hdr:
>
I don't think this is right. We do
hdr_len = bswap_32(hdr->hdr_len);
and this will _return_ a bswapped value, but does not bswap hdr->hdr_len itself.
When we later bswap the header via btf_bswap_hdr(), we end up in a state where
both hdr_len and hdr->hdr_len will have the same (swapped once) value. If we
had
hdr->hdr_len = bswap_32(hdr->hdr_len);
...the concern would be valid. So AI wasn't far off here.
>> }
>>
>> - if (btf->raw_size < hdr->hdr_len) {
>> + if (btf->raw_size < hdr_len) {
>> pr_debug("BTF header len %u larger than data size %u\n",
>> - hdr->hdr_len, btf->raw_size);
>> + hdr_len, btf->raw_size);
>> return -EINVAL;
>> }
>
> [ ... ]
>
>> + /* At this point, we have basic header information, so allocate btf->hdr */
>> + btf->hdr = calloc(1, sizeof(struct btf_header));
>> + if (!btf->hdr) {
>> + pr_debug("BTF header allocation failed\n");
>> + return -ENOMEM;
>> + }
>> + memcpy(btf->hdr, hdr, min((size_t)hdr_len, sizeof(struct btf_header)));
> ^^^
>
> The memcpy copies from hdr, which contains the double-swapped (therefore
> incorrect) hdr_len field. So btf->hdr->hdr_len will have the wrong
> endianness when btf_parse_hdr() returns. This affects subsequent pointer
> arithmetic like:
>
> btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off
>
> causing types_data to point to the wrong location when loading BTF with
> non-native endianness.
>
AI has followed the chain of reasoning based on the non-existent byte-swap issue
above, so this isn't a concern either I believe.
next prev parent reply other threads:[~2025-12-15 16:05 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-15 9:17 [PATCH v8 bpf-next 00/10] Add kind layout to BTF Alan Maguire
2025-12-15 9:17 ` [PATCH v8 bpf-next 01/10] btf: add kind layout encoding to UAPI Alan Maguire
2025-12-15 9:38 ` bot+bpf-ci
2025-12-16 19:23 ` Andrii Nakryiko
2025-12-19 13:15 ` Alan Maguire
2025-12-19 17:53 ` Andrii Nakryiko
2025-12-19 18:13 ` Alan Maguire
2025-12-19 18:19 ` Andrii Nakryiko
2025-12-19 18:22 ` Alan Maguire
2025-12-20 0:05 ` Alexei Starovoitov
2025-12-22 8:58 ` Alan Maguire
2025-12-22 19:03 ` Alexei Starovoitov
2025-12-23 11:09 ` Alan Maguire
2026-01-06 0:11 ` Andrii Nakryiko
2026-01-06 0:51 ` Alexei Starovoitov
2026-01-06 1:19 ` Andrii Nakryiko
2026-01-08 18:55 ` Alan Maguire
2026-01-09 1:24 ` Andrii Nakryiko
2026-01-09 1:40 ` Alexei Starovoitov
2026-01-09 13:20 ` Alan Maguire
2026-01-09 18:34 ` Alexei Starovoitov
2026-01-12 17:47 ` Alan Maguire
2025-12-15 9:17 ` [PATCH v8 bpf-next 02/10] libbpf: Support kind layout section handling in BTF Alan Maguire
2025-12-15 9:38 ` bot+bpf-ci
2025-12-15 16:03 ` Alan Maguire [this message]
2025-12-16 0:08 ` Eduard Zingerman
2025-12-16 6:01 ` Eduard Zingerman
2025-12-16 14:58 ` Alan Maguire
2025-12-16 19:34 ` Andrii Nakryiko
2025-12-19 13:34 ` Alan Maguire
2025-12-19 17:58 ` Andrii Nakryiko
2025-12-19 18:18 ` Alan Maguire
2025-12-19 18:21 ` Andrii Nakryiko
2025-12-19 18:36 ` Eduard Zingerman
2025-12-19 18:41 ` Andrii Nakryiko
2025-12-19 18:44 ` Eduard Zingerman
2025-12-15 9:17 ` [PATCH v8 bpf-next 03/10] libbpf: use kind layout to compute an unknown kind size Alan Maguire
2025-12-16 6:07 ` Eduard Zingerman
2025-12-16 15:00 ` Alan Maguire
2025-12-16 19:42 ` Andrii Nakryiko
2025-12-16 19:58 ` Eduard Zingerman
2025-12-16 21:11 ` Andrii Nakryiko
2025-12-16 21:21 ` Eduard Zingerman
2025-12-16 22:23 ` Andrii Nakryiko
2025-12-16 22:35 ` Eduard Zingerman
2025-12-16 23:00 ` Andrii Nakryiko
2025-12-16 23:36 ` Eduard Zingerman
2025-12-17 0:30 ` Andrii Nakryiko
2025-12-17 0:38 ` Eduard Zingerman
2025-12-16 19:37 ` Andrii Nakryiko
2025-12-15 9:17 ` [PATCH v8 bpf-next 04/10] libbpf: Add kind layout encoding support Alan Maguire
2025-12-16 5:58 ` Eduard Zingerman
2025-12-16 21:04 ` Andrii Nakryiko
2025-12-15 9:17 ` [PATCH v8 bpf-next 05/10] libbpf: BTF validation can use kind layout for unknown kinds Alan Maguire
2025-12-15 9:17 ` [PATCH v8 bpf-next 06/10] btf: support kernel parsing of BTF with kind layout Alan Maguire
2025-12-16 6:51 ` Eduard Zingerman
2025-12-16 21:21 ` Andrii Nakryiko
2025-12-16 21:25 ` Eduard Zingerman
2025-12-16 22:09 ` Andrii Nakryiko
2025-12-16 22:12 ` Eduard Zingerman
2025-12-15 9:17 ` [PATCH v8 bpf-next 07/10] selftests/bpf: test kind encoding/decoding Alan Maguire
2025-12-15 9:17 ` [PATCH v8 bpf-next 08/10] bpftool: add BTF dump "format meta" to dump header/metadata Alan Maguire
2025-12-15 9:52 ` bot+bpf-ci
2025-12-15 9:17 ` [PATCH v8 bpf-next 09/10] bpftool: Update doc to describe bpftool btf dump .. format metadata Alan Maguire
2025-12-15 9:17 ` [PATCH v8 bpf-next 10/10] kbuild, bpf: Specify "kind_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=0d31010a-994f-444f-8c94-4316229db268@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=ttreyer@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