* [PATCH bpf-next] bpf: Fix out-of-bounds btf->types write for split BTF
@ 2026-10-23 6:31 Feng Yang
2026-10-23 15:48 ` Mahe Tardy
0 siblings, 1 reply; 2+ messages in thread
From: Feng Yang @ 2026-10-23 6:31 UTC (permalink / raw)
To: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai
Cc: bpf, linux-kernel
From: Feng Yang <yangfeng@kylinos.cn>
btf_add_type() bounds the btf->types array by BTF_MAX_TYPE, but for a
split BTF the type ids it registers are start_id .. start_id +
nr_types - 1, so what is actually left of the id space is
BTF_MAX_TYPE - start_id. Two things follow from that:
- the limit check is an equality test,
'start_id + btf->types_size == BTF_MAX_TYPE'. It is evaluated only
when the array has to grow, with btf->types_size == btf->nr_types,
and the growth (T' = min(BTF_MAX_TYPE, T + max(T / 4, 16)), starting
from 0) is clamped to BTF_MAX_TYPE, which it lands on exactly (...,
928143, then min(BTF_MAX_TYPE, 1160178)). So unless
BTF_MAX_TYPE - start_id is one of the 48 sizes that sequence hits,
the check does not fire for a split BTF at all;
- new_size is clamped to BTF_MAX_TYPE instead of
BTF_MAX_TYPE - start_id, so the array can still grow to
BTF_MAX_TYPE entries even though only BTF_MAX_TYPE - start_id type
ids exist.
As a result nr_types can reach BTF_MAX_TYPE for a split BTF while its
type ids have already gone past BTF_MAX_TYPE; a type never validates its
own id, only the ids it references (btf_ref_type_check_meta()). At that
point btf->types_size == btf->nr_types == BTF_MAX_TYPE and the growth
branch computes the same size again: it allocates a fresh BTF_MAX_TYPE
entry array, copies the entries, and the loop is left with
btf->types_size != btf->nr_types, so that branch is not taken anymore.
Every type after this writes btf->types[btf->nr_types++] past the end of
the array: BTF_MAX_TYPE * 8 == 8388600 bytes are requested, so the first
such write lands in the 8 byte rounding slack of the 8MB allocation, and
each further type writes another 8 bytes beyond it.
Fix both by bounding the array with BTF_MAX_TYPE - start_id and by
turning the equality check into a '>=' one, so that the array stops
exactly where the id space ends. Base BTF behavior (start_id ==
0) is unchanged.
Fixes: 951bb64621b8 ("bpf: Add in-kernel split BTF support")
Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
kernel/bpf/btf.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 80b6f96263f1..6e54cd6c2eb5 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -1724,15 +1724,17 @@ static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
/* Expand 'types' array */
struct btf_type **new_types;
- u32 expand_by, new_size;
+ u32 expand_by, new_size, max_size;
- if (btf->start_id + btf->types_size == BTF_MAX_TYPE) {
+ if (btf->start_id >= BTF_MAX_TYPE ||
+ btf->start_id + btf->types_size >= BTF_MAX_TYPE) {
btf_verifier_log(env, "Exceeded max num of types");
return -E2BIG;
}
+ max_size = BTF_MAX_TYPE - btf->start_id;
expand_by = max_t(u32, btf->types_size >> 2, 16);
- new_size = min_t(u32, BTF_MAX_TYPE,
+ new_size = min_t(u32, max_size,
btf->types_size + expand_by);
new_types = kvzalloc_objs(*new_types, new_size,
--
2.25.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH bpf-next] bpf: Fix out-of-bounds btf->types write for split BTF
2026-10-23 6:31 [PATCH bpf-next] bpf: Fix out-of-bounds btf->types write for split BTF Feng Yang
@ 2026-10-23 15:48 ` Mahe Tardy
0 siblings, 0 replies; 2+ messages in thread
From: Mahe Tardy @ 2026-10-23 15:48 UTC (permalink / raw)
To: Feng Yang
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, bpf, linux-kernel
On Fri, Oct 23, 2026 at 02:31:33PM +0800, Feng Yang wrote:
> From: Feng Yang <yangfeng@kylinos.cn>
>
> btf_add_type() bounds the btf->types array by BTF_MAX_TYPE, but for a
> split BTF the type ids it registers are start_id .. start_id +
> nr_types - 1, so what is actually left of the id space is
> BTF_MAX_TYPE - start_id. Two things follow from that:
>
> - the limit check is an equality test,
> 'start_id + btf->types_size == BTF_MAX_TYPE'. It is evaluated only
> when the array has to grow, with btf->types_size == btf->nr_types,
> and the growth (T' = min(BTF_MAX_TYPE, T + max(T / 4, 16)), starting
> from 0) is clamped to BTF_MAX_TYPE, which it lands on exactly (...,
> 928143, then min(BTF_MAX_TYPE, 1160178)). So unless
> BTF_MAX_TYPE - start_id is one of the 48 sizes that sequence hits,
> the check does not fire for a split BTF at all;
>
> - new_size is clamped to BTF_MAX_TYPE instead of
> BTF_MAX_TYPE - start_id, so the array can still grow to
> BTF_MAX_TYPE entries even though only BTF_MAX_TYPE - start_id type
> ids exist.
>
> As a result nr_types can reach BTF_MAX_TYPE for a split BTF while its
> type ids have already gone past BTF_MAX_TYPE; a type never validates its
> own id, only the ids it references (btf_ref_type_check_meta()). At that
> point btf->types_size == btf->nr_types == BTF_MAX_TYPE and the growth
> branch computes the same size again: it allocates a fresh BTF_MAX_TYPE
> entry array, copies the entries, and the loop is left with
> btf->types_size != btf->nr_types, so that branch is not taken anymore.
> Every type after this writes btf->types[btf->nr_types++] past the end of
> the array: BTF_MAX_TYPE * 8 == 8388600 bytes are requested, so the first
> such write lands in the 8 byte rounding slack of the 8MB allocation, and
> each further type writes another 8 bytes beyond it.
>
> Fix both by bounding the array with BTF_MAX_TYPE - start_id and by
> turning the equality check into a '>=' one, so that the array stops
> exactly where the id space ends. Base BTF behavior (start_id ==
> 0) is unchanged.
>
> Fixes: 951bb64621b8 ("bpf: Add in-kernel split BTF support")
> Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
Reviewed-by: Mahe Tardy <mahe.tardy@gmail.com>
Indeed taking toy BTF_MAX_TYPE = 10 and start_id = 2 we can see the
array can grow from 0 to 10 directly, get filled, enter the growth
branch then the condition is for E2BIG is 2 + 10 which is already beyond
10 and thus never match: we re-allocate the array, copy and write out of
bounds. Fix looks reasonable to me (but disclaimer I just read that code
for the first time).
> ---
> kernel/bpf/btf.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 80b6f96263f1..6e54cd6c2eb5 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -1724,15 +1724,17 @@ static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
> /* Expand 'types' array */
>
> struct btf_type **new_types;
> - u32 expand_by, new_size;
> + u32 expand_by, new_size, max_size;
>
> - if (btf->start_id + btf->types_size == BTF_MAX_TYPE) {
> + if (btf->start_id >= BTF_MAX_TYPE ||
> + btf->start_id + btf->types_size >= BTF_MAX_TYPE) {
> btf_verifier_log(env, "Exceeded max num of types");
> return -E2BIG;
> }
>
> + max_size = BTF_MAX_TYPE - btf->start_id;
> expand_by = max_t(u32, btf->types_size >> 2, 16);
> - new_size = min_t(u32, BTF_MAX_TYPE,
> + new_size = min_t(u32, max_size,
> btf->types_size + expand_by);
>
> new_types = kvzalloc_objs(*new_types, new_size,
> --
> 2.25.1
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-23 15:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-23 6:31 [PATCH bpf-next] bpf: Fix out-of-bounds btf->types write for split BTF Feng Yang
2026-10-23 15:48 ` Mahe Tardy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®