From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Paul Moses" <p@1g4.org>, <martin.lau@linux.dev>,
<ast@kernel.org>, <daniel@iogearbox.net>, <andrii@kernel.org>,
<eddyz87@gmail.com>, <bpf@vger.kernel.org>
Cc: <song@kernel.org>, <yonghong.song@linux.dev>, <jolsa@kernel.org>,
<houtao1@huawei.com>, <linux-kernel@vger.kernel.org>,
<stable@vger.kernel.org>
Subject: Re: [PATCH bpf] bpf: Validate BTF repeated field counts before expansion
Date: Sun, 07 Jun 2026 09:55:47 -0700 [thread overview]
Message-ID: <DJ2Z46YDPFUR.3BDR6ZW4P912C@gmail.com> (raw)
In-Reply-To: <DJ2OZSCSEVEI.3APUCE7ML9X4Q@gmail.com>
On Sun Jun 7, 2026 at 1:59 AM PDT, Kumar Kartikeya Dwivedi wrote:
> On Sat Jun 6, 2026 at 1:43 AM CEST, Paul Moses wrote:
>> btf_parse_struct_metas() walks user-supplied BTF during BPF_BTF_LOAD,
>> and btf_repeat_fields() expands repeatable fields from array elements
>> into the fixed BTF_FIELDS_MAX scratch array used by btf_parse_fields().
>>
>> The remaining-capacity check performs the expanded field count calculation
>> in u32. A malformed BTF can wrap that calculation, causing the check to
>> pass even when the expanded field count exceeds the scratch array
>> capacity. The following memcpy() can then write past the end of the
>> array.
>>
>> Use checked addition and multiplication before copying repeated fields
>> and reject impossible counts.
>>
>> Fixes: 797d73ee232d ("bpf: Check the remaining info_cnt before repeating btf fields")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Paul Moses <p@1g4.org>
>> ---
>
> Do you have an example where this actually occurred in practice?
>
>> kernel/bpf/btf.c | 9 ++++-----
>> 1 file changed, 4 insertions(+), 5 deletions(-)
>>
>> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
>> index a62d78581207..510aa32847da 100644
>> --- a/kernel/bpf/btf.c
>> +++ b/kernel/bpf/btf.c
>> @@ -3668,7 +3668,7 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_
>> static int btf_repeat_fields(struct btf_field_info *info, int info_cnt,
>> u32 field_cnt, u32 repeat_cnt, u32 elem_size)
>> {
>> - u32 i, j;
>> + u32 i, j, total_cnt, total_repeats;
>> u32 cur;
>>
>> /* Ensure not repeating fields that should not be repeated. */
>> @@ -3686,10 +3686,9 @@ static int btf_repeat_fields(struct btf_field_info *info, int info_cnt,
>> }
>> }
>>
>> - /* The type of struct size or variable size is u32,
>> - * so the multiplication will not overflow.
>> - */
>> - if (field_cnt * (repeat_cnt + 1) > info_cnt)
>> + if (check_add_overflow(repeat_cnt, 1, &total_repeats) ||
>> + check_mul_overflow(field_cnt, total_repeats, &total_cnt) ||
>> + total_cnt > (u32)info_cnt)
>> return -E2BIG;
The callers of this function do:
if (nelems > 1) {
err = btf_repeat_fields(info, info_cnt, ret, nelems - 1, t->size);
so repeat_cnt cannot overflow.
'ret' (which is field_cnt) comes from btf_find_struct_field().
To overflow the struct needs to have 32k valid fields.
Is this really what is happening?
The issues is deeper. Please have a reliable reproducer first.
next prev parent reply other threads:[~2026-06-07 16:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-05 23:43 [PATCH bpf] bpf: Validate BTF repeated field counts before expansion Paul Moses
2026-06-07 8:59 ` Kumar Kartikeya Dwivedi
2026-06-07 10:11 ` Paul Moses
2026-06-07 11:08 ` Kumar Kartikeya Dwivedi
2026-06-07 17:53 ` Paul Moses
2026-06-08 19:59 ` Eduard Zingerman
2026-06-07 16:55 ` Alexei Starovoitov [this message]
2026-06-08 20:01 ` Eduard Zingerman
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=DJ2Z46YDPFUR.3BDR6ZW4P912C@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=houtao1@huawei.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=p@1g4.org \
--cc=song@kernel.org \
--cc=stable@vger.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