* [PATCH] bpf: Add nelems overflow check in btf_find_field_one() btf_find_field_one() multiplies a u32 nelems accumulator by each nested array level's element count without checking for overflow. The sibling function __btf_resolve_size() already guards against the same overflow pattern (btf.c line 2110). Currently the BTF array verifier (btf_array_resolve) rejects BTF blobs whose total array size would overflow u32, so this code path is not reachable with crafted BTF input on kernels that include that check. Add check_mul_overflow() anyway to keep btf_find_field_one() self-consistent with __btf_resolve_size() and to guard against future changes in the validation ordering. Fixes: 994796c0256c ("bpf: create repeated fields for arrays.")
@ 2026-05-22 12:41 Himanshu Anand
2026-05-22 20:44 ` Yonghong Song
0 siblings, 1 reply; 2+ messages in thread
From: Himanshu Anand @ 2026-05-22 12:41 UTC (permalink / raw)
To: martin.lau, ast, daniel, andrii; +Cc: bpf, linux-kernel, Himanshu Anand
Signed-off-by: Himanshu Anand <anand.himanshu17@gmail.com>
---
kernel/bpf/btf.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index a62d78581207..b767a9fcf095 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3765,7 +3765,8 @@ static int btf_find_field_one(const struct btf *btf,
*/
for (i = 0; i < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); i++) {
array = btf_array(var_type);
- nelems *= array->nelems;
+ if (check_mul_overflow(nelems, array->nelems, &nelems))
+ return -E2BIG;
var_type = btf_type_by_id(btf, array->type);
}
if (i == MAX_RESOLVE_DEPTH)
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] bpf: Add nelems overflow check in btf_find_field_one() btf_find_field_one() multiplies a u32 nelems accumulator by each nested array level's element count without checking for overflow. The sibling function __btf_resolve_size() already guards against the same overflow pattern (btf.c line 2110). Currently the BTF array verifier (btf_array_resolve) rejects BTF blobs whose total array size would overflow u32, so this code path is not reachable with crafted BTF input on kernels that include that check. Add check_mul_overflow() anyway to keep btf_find_field_one() self-consistent with __btf_resolve_size() and to guard against future changes in the validation ordering. Fixes: 994796c0256c ("bpf: create repeated fields for arrays.")
2026-05-22 12:41 [PATCH] bpf: Add nelems overflow check in btf_find_field_one() btf_find_field_one() multiplies a u32 nelems accumulator by each nested array level's element count without checking for overflow. The sibling function __btf_resolve_size() already guards against the same overflow pattern (btf.c line 2110). Currently the BTF array verifier (btf_array_resolve) rejects BTF blobs whose total array size would overflow u32, so this code path is not reachable with crafted BTF input on kernels that include that check. Add check_mul_overflow() anyway to keep btf_find_field_one() self-consistent with __btf_resolve_size() and to guard against future changes in the validation ordering. Fixes: 994796c0256c ("bpf: create repeated fields for arrays.") Himanshu Anand
@ 2026-05-22 20:44 ` Yonghong Song
0 siblings, 0 replies; 2+ messages in thread
From: Yonghong Song @ 2026-05-22 20:44 UTC (permalink / raw)
To: Himanshu Anand, martin.lau, ast, daniel, andrii; +Cc: bpf, linux-kernel
On 5/22/26 5:41 AM, Himanshu Anand wrote:
> Signed-off-by: Himanshu Anand <anand.himanshu17@gmail.com>
Looks like patch subject and commit message are messed up. Please
fix it in the next revision.
Please use [PATCH bpf v2] as the tag.
> ---
> kernel/bpf/btf.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index a62d78581207..b767a9fcf095 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -3765,7 +3765,8 @@ static int btf_find_field_one(const struct btf *btf,
> */
> for (i = 0; i < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); i++) {
> array = btf_array(var_type);
> - nelems *= array->nelems;
> + if (check_mul_overflow(nelems, array->nelems, &nelems))
> + return -E2BIG;
Looks correct to me.
> var_type = btf_type_by_id(btf, array->type);
> }
> if (i == MAX_RESOLVE_DEPTH)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-22 20:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-22 12:41 [PATCH] bpf: Add nelems overflow check in btf_find_field_one() btf_find_field_one() multiplies a u32 nelems accumulator by each nested array level's element count without checking for overflow. The sibling function __btf_resolve_size() already guards against the same overflow pattern (btf.c line 2110). Currently the BTF array verifier (btf_array_resolve) rejects BTF blobs whose total array size would overflow u32, so this code path is not reachable with crafted BTF input on kernels that include that check. Add check_mul_overflow() anyway to keep btf_find_field_one() self-consistent with __btf_resolve_size() and to guard against future changes in the validation ordering. Fixes: 994796c0256c ("bpf: create repeated fields for arrays.") Himanshu Anand
2026-05-22 20:44 ` Yonghong Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox