[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <36cd75fea9d630152704e29bd21054aba72dcb459a7b9d40b5d979313b1fe3a5@mail.kernel.org>
Date: Thu, 6 Nov 2025 13:47:40 +0000 (UTC)
From: bot+bpf-ci@...nel.org
To: dolinux.peng@...il.com,ast@...nel.org
Cc: eddyz87@...il.com,andrii.nakryiko@...il.com,zhangxiaoqin@...omi.com,linux-kernel@...r.kernel.org,bpf@...r.kernel.org,dolinux.peng@...il.com,alan.maguire@...cle.com,song@...nel.org,pengdonglin@...omi.com,ast@...nel.org,andrii@...nel.org,daniel@...earbox.net,martin.lau@...nel.org,eddyz87@...il.com,yonghong.song@...ux.dev,clm@...a.com,ihor.solodrai@...ux.dev
Subject: Re: [PATCH v5 2/7] libbpf: Add BTF permutation support for type reordering
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 9c389b2c8..97451a929 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
[ ... ]
> +int btf__permute(struct btf *btf, __u32 *ids, __u32 ids_sz, const struct btf_permute_opts *opts)
> +{
> + struct btf_permute p;
> + int err = 0;
> + __u32 *ids_map = NULL;
> +
> + if (!OPTS_VALID(opts, btf_permute_opts) || (ids_sz > btf->nr_types))
> + return libbpf_err(-EINVAL);
> +
> + ids_map = calloc(ids_sz, sizeof(*ids_map));
Can ids_map overflow here in btf_permute_shuffle_types()?
The ids_map array is allocated with ids_sz elements, giving valid
indices from 0 to ids_sz-1. However, in btf_permute_shuffle_types()
the code accesses:
ids_map = &p->ids_map[id - btf->start_id]
where 'id' is validated only to be in range [btf->start_id,
btf->start_id + btf->nr_types). When ids_sz < btf->nr_types
(truncation case) and a user provides a type ID where
(id - btf->start_id) >= ids_sz, this results in an out-of-bounds
write to ids_map.
For example, with start_id=1, nr_types=100, and ids_sz=50:
- ids_map has 50 elements (indices 0-49)
- If user includes type ID 75 in the ids array
- btf__type_by_id(btf, 75) succeeds (75 < 101)
- ids_map[75-1] = ids_map[74] writes beyond the array
The same issue occurs in btf_permute_remap_type_id() where the code
reads from ids_map without validating the index is within ids_sz:
new_type_id = p->ids_map[*type_id - p->btf->start_id]
If any included type references a type ID beyond ids_sz, this reads
uninitialized memory (or out-of-bounds if the ID is >= ids_sz +
start_id).
> + if (!ids_map) {
> + err = -ENOMEM;
> + goto done;
> + }
> +
> + p.btf = btf;
> + p.btf_ext = OPTS_GET(opts, btf_ext, NULL);
> + p.ids = ids;
> + p.ids_map = ids_map;
> + p.ids_sz = ids_sz;
> +
> + if (btf_ensure_modifiable(btf)) {
> + err = -ENOMEM;
> + goto done;
> + }
> + err = btf_permute_shuffle_types(&p);
> + if (err < 0) {
> + goto done;
> + }
> + err = btf_permute_remap_types(&p);
> + if (err < 0) {
> + goto done;
> + }
> +
> +done:
> + free(ids_map);
> + return libbpf_err(err);
> +}
[ ... ]
> +static int btf_permute_shuffle_types(struct btf_permute *p)
> +{
> + struct btf *btf = p->btf;
> + const struct btf_type *t;
> + __u32 *new_offs = NULL, *ids_map;
> + void *nt, *new_types = NULL;
> + int i, id, len, err;
> +
> + new_offs = calloc(p->ids_sz, sizeof(*new_offs));
> + new_types = calloc(btf->hdr->type_len, 1);
> + if (!new_offs || !new_types) {
> + err = -ENOMEM;
> + goto out_err;
> + }
> +
> + nt = new_types;
> + for (i = 0; i < p->ids_sz; i++) {
> + id = p->ids[i];
> + /* type IDs from base_btf and the VOID type are not allowed */
> + if (id < btf->start_id) {
> + err = -EINVAL;
> + goto out_err;
> + }
> + /* must be a valid type ID */
> + t = btf__type_by_id(btf, id);
> + if (!t) {
> + err = -EINVAL;
> + goto out_err;
> + }
> + ids_map = &p->ids_map[id - btf->start_id];
> + /* duplicate type IDs are not allowed */
> + if (*ids_map) {
> + err = -EINVAL;
> + goto out_err;
> + }
> + len = btf_type_size(t);
> + memcpy(nt, t, len);
> + new_offs[i] = nt - new_types;
> + *ids_map = btf->start_id + i;
> + nt += len;
> + }
[ ... ]
> +static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)
> +{
> + struct btf_permute *p = ctx;
> + __u32 new_type_id = *type_id;
> +
> + /* skip references that point into the base BTF */
> + if (new_type_id < p->btf->start_id)
> + return 0;
> +
> + new_type_id = p->ids_map[*type_id - p->btf->start_id];
> + if (new_type_id > BTF_MAX_NR_TYPES)
> + return -EINVAL;
> +
> + *type_id = new_type_id;
> + return 0;
> +}
---
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/19137195500
Powered by blists - more mailing lists