From: Yonghong Song <yonghong.song@linux.dev>
To: sashiko-reviews@lists.linux.dev
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 15/20] libbpf: Collect .bpf_cleanup records and pass them to the kernel
Date: Thu, 17 Sep 2026 20:44:39 -0700 [thread overview]
Message-ID: <48fd0275-b710-40de-890b-df81882e1b2e@linux.dev> (raw)
In-Reply-To: <20260917061212.515111F000FF@smtp.kernel.org>
On 9/16/26 11:12 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [Medium] bpf: undefined behavior when calling qsort with a NULL pointer
> - [Low] bpf: array bounds violation via type-punned pointer arithmetic
> --
>
> commit 1e591245d7ec663b872eae44c8fa7f02cfd956a4
> Author: Yonghong Song <yonghong.song@linux.dev>
>
> libbpf: Collect .bpf_cleanup records and pass them to the kernel
>
> This commit parses the compiler-emitted .bpf_cleanup section and hands
> the resulting table to BPF_PROG_LOAD so the kernel can resolve exceptions.
>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index ea1c09fa37939..d7cb93a70f58d 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
> [ ... ]
>
>> @@ -4847,6 +4885,214 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
> [ ... ]
>
>> +static int bpf_prog_collect_cleanup_info(struct bpf_object *obj,
>> + struct bpf_program *prog)
>> +{
>> + size_t i;
>> + int j;
>> +
>> + for (i = 0; i < obj->cleanup_rec_cnt; i++) {
>> + struct cleanup_raw_rec *raw = &obj->cleanup_recs[i];
>> + struct bpf_program *owner = NULL;
>> + struct bpf_cleanup_info ci = {};
>> + __u32 *fields = (__u32 *)&ci;
>> + void *tmp;
>> +
>> + for (j = 0; j < CLEANUP_REC_FIELDS; j++) {
>> + size_t idx = raw->insn_idx[j], final;
>> + struct bpf_program *p;
> [ ... ]
>
>> + if (owner == prog) {
>> + final = raw->insn_idx[j] - prog->sec_insn_off;
>> + } else if (prog_is_subprog(obj, owner) && owner->sub_insn_off) {
>> + /* sub_insn_off is where this subprogram was
>> + * appended to the main program being relocated;
>> + * zero means it is not part of it.
>> + */
>> + final = owner->sub_insn_off +
>> + raw->insn_idx[j] - owner->sec_insn_off;
>> + } else {
>> + owner = NULL;
>> + break;
>> + }
>> + fields[j] = final;
> [Severity: Low]
> Is it safe to cast a pointer to a struct field into an array pointer and access
> subsequent members via pointer arithmetic in this loop?
>
> The struct bpf_cleanup_info fields are populated by casting a pointer to the
> struct to a __u32 * array and indexing into it in a loop.
>
> According to C11 6.5.6, pointer arithmetic on a pointer to a scalar (the first
> struct field) only allows accessing up to one element past it, making fields[1]
> and fields[2] undefined behavior.
>
> This can trigger compiler warnings (-Warray-bounds) and potentially cause
> aggressive optimizers to generate incorrect code.
Yes, the code is correct but it is awkward and my trigger compiler warnings with
additional compiler flag. Will make it explicit with bpf_cleanup_info fields.
>
>> + }
>> + if (!owner)
>> + continue;
>> +
>> + tmp = libbpf_reallocarray(prog->cleanup_info, prog->cleanup_info_cnt + 1,
>> + sizeof(*prog->cleanup_info));
>> + if (!tmp)
>> + return -ENOMEM;
>> + prog->cleanup_info = tmp;
>> + prog->cleanup_info_rec_size = sizeof(struct bpf_cleanup_info);
>> + prog->cleanup_info[prog->cleanup_info_cnt++] = ci;
>> +
>> + pr_debug("prog '%s': cleanup region [%u,%u) -> landing pad %u\n",
>> + prog->name, ci.begin_off, ci.end_off, ci.landing_pad_off);
>> + }
>> +
>> + qsort(prog->cleanup_info, prog->cleanup_info_cnt,
>> + sizeof(*prog->cleanup_info), cmp_cleanup_info);
> [Severity: Medium]
> Can this unconditionally call qsort() with a NULL base pointer?
You are right. It is possible prog->cleanup_info_cnt is 0 and in such cases,
cleanup_info will be NULL. Will add a condition like 'prog->cleanup_info_cnt > 0'.
>
> If obj->cleanup_rec_cnt is 0, or if none of the records match the program,
> prog->cleanup_info_cnt remains 0 and prog->cleanup_info will be NULL.
>
> Calling qsort() with a NULL pointer invokes undefined behavior under C11 7.1.4,
> even if the element count is 0. This triggers UBSAN errors (nonnull attribute
> violation in libc headers) and can fail runtime checks.
>
next prev parent reply other threads:[~2026-09-18 3:44 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-17 5:56 [PATCH bpf-next 00/20] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
2026-09-17 5:56 ` [PATCH bpf-next 01/20] bpf: Accept the compiler's exception cleanup table at program load Yonghong Song
2026-09-17 5:56 ` [PATCH bpf-next 02/20] bpf: Add the bpf_unwind_resume() kfunc Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 03/20] bpf: Add lookups for exception cleanup resumes and landing pads Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 04/20] bpf: Mark the call sites an exception cleanup table covers Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 05/20] bpf: Make exception landing pads reachable in the CFG Yonghong Song
2026-09-17 6:22 ` sashiko-bot
2026-09-18 3:36 ` Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 06/20] bpf: Explore the landing pads no call site reaches Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 07/20] bpf: Refuse exception cleanup shapes bpf_throw() cannot dispatch Yonghong Song
2026-09-19 4:57 ` Alexei Starovoitov
2026-09-19 17:32 ` Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 08/20] bpf: Walk the exception unwind in the verifier Yonghong Song
2026-09-19 4:57 ` Alexei Starovoitov
2026-09-19 17:36 ` Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 09/20] bpf: Refuse a private stack for a program with an exception cleanup table Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 10/20] bpf: Dispatch exception cleanup pads from bpf_throw() Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 11/20] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
2026-09-19 5:02 ` Alexei Starovoitov
2026-09-19 19:13 ` Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 12/20] bpf, arm64: " Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 13/20] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
2026-09-17 6:08 ` sashiko-bot
2026-09-17 7:00 ` bot+bpf-ci
2026-09-18 3:40 ` Yonghong Song
2026-09-17 5:57 ` [PATCH bpf-next 14/20] libbpf: Add cleanup_info to bpf_prog_load_opts Yonghong Song
2026-09-17 5:58 ` [PATCH bpf-next 15/20] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
2026-09-17 6:12 ` sashiko-bot
2026-09-18 3:44 ` Yonghong Song [this message]
2026-09-17 5:58 ` [PATCH bpf-next 16/20] libbpf: Carry the exception cleanup table through the light skeleton Yonghong Song
2026-09-17 6:18 ` sashiko-bot
2026-09-18 3:52 ` Yonghong Song
2026-09-17 5:58 ` [PATCH bpf-next 17/20] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
2026-09-17 6:16 ` sashiko-bot
2026-09-18 3:58 ` Yonghong Song
2026-09-17 5:58 ` [PATCH bpf-next 18/20] selftests/bpf: Add an end-to-end .bpf_cleanup exception test Yonghong Song
2026-09-17 6:12 ` sashiko-bot
2026-09-17 5:58 ` [PATCH bpf-next 19/20] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
2026-09-17 6:16 ` sashiko-bot
2026-09-17 5:58 ` [PATCH bpf-next 20/20] selftests/bpf: Load an exception cleanup program from a light skeleton Yonghong Song
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=48fd0275-b710-40de-890b-df81882e1b2e@linux.dev \
--to=yonghong.song@linux.dev \
--cc=bpf@vger.kernel.org \
--cc=sashiko-reviews@lists.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.