From: Yonghong Song <yonghong.song@linux.dev>
To: sashiko-reviews@lists.linux.dev
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 15/20] libbpf: Collect .bpf_cleanup records and pass them to the kernel
Date: Sat, 19 Sep 2026 13:21:21 -0700 [thread overview]
Message-ID: <5e04d0f0-52d7-4e78-a608-7acae44f6908@linux.dev> (raw)
In-Reply-To: <20260918050054.BAFFB1F000FF@smtp.kernel.org>
On 9/17/26 10:00 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [Medium] bpf: empty cleanup range check in bpf_prog_collect_cleanup_info() only catches offset 0
> - [Medium] bpf: bpf_object__init_cleanup_info() rejects valid objects with 0-sized .bpf_cleanup sections
> --
>
> commit a645d3bd249aceee4909edcc78e5fe4247fd3432
> Author: Yonghong Song <yonghong.song@linux.dev>
>
> libbpf: Collect .bpf_cleanup records and pass them to the kernel
>
> Parse the compiler-emitted .bpf_cleanup section and hand the resulting
> table to BPF_PROG_LOAD.
>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 27f2eec13f76f..1b8b978e7b7b9 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
> [ ... ]
>
>> @@ -4852,6 +4890,221 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
>> return NULL;
>> }
>>
>> +static int bpf_object__init_cleanup_info(struct bpf_object *obj)
>> +{
>> + Elf_Data *data = obj->efile.cleanup_data;
>> + Elf_Data *relo = NULL;
>> + size_t i, nrels, nslots, nrecs;
>> + struct cleanup_raw_rec *recs;
>> + int *slot_sec, ret = 0;
>> + size_t *slot_val;
>> + const __u32 *vals;
>> + bool native;
>> +
>> + if (!data || obj->efile.cleanup_shndx < 0)
>> + return 0;
>> +
>> + native = is_native_endianness(obj);
>> +
>> + for (i = 0; i < obj->efile.sec_cnt; i++) {
>> + struct elf_sec_desc *sd = &obj->efile.secs[i];
>> +
>> + if (sd->sec_type == SEC_RELO && sd->shdr &&
>> + sd->shdr->sh_info == (Elf64_Word)obj->efile.cleanup_shndx) {
>> + relo = sd->data;
>> + break;
>> + }
>> + }
>> + if (!relo) {
>> + pr_warn("%s present without relocations\n", CLEANUP_SEC);
>> + return -LIBBPF_ERRNO__FORMAT;
>> + }
> [Severity: Medium]
> Could this reject valid objects if the compiler emits an empty 0-byte
> .bpf_cleanup section?
>
> If the section is empty (e.g., exceptions were optimized out), no relocations
> are emitted and there is no corresponding .rel.bpf_cleanup section. However,
> bpf_object__init_cleanup_info() requires the relocation section to exist if
> .bpf_cleanup is present.
>
> Would it be better to return early if data->d_size is 0 before checking for
> relocations?
I think it is okay, as you mentioned, there will be one error message later
when relocation section checks.
>
> [ ... ]
>
>> +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[CLEANUP_REC_FIELDS];
>> + void *tmp;
>> +
>> + for (j = 0; j < CLEANUP_REC_FIELDS; j++) {
>> + size_t idx = raw->insn_idx[j], final;
>> + struct bpf_program *p;
>> +
>> + /* The end of a range is exclusive, so it may name the
>> + * instruction just past the last one of a function,
>> + * which belongs to the next function or to nothing at
>> + * all. Ask about the last instruction the range covers,
>> + * the way the kernel does.
>> + */
>> + if (j == CLEANUP_REC_END) {
>> + if (!idx) {
>> + pr_warn("%s: record %zu is an empty range\n",
>> + CLEANUP_SEC, i);
>> + return -LIBBPF_ERRNO__FORMAT;
>> + }
>> + idx--;
>> + }
> [Severity: Medium]
> Will this check catch empty ranges emitted by the compiler at non-zero
> offsets?
>
> If the compiler emits a .bpf_cleanup record where begin and end are both 5,
> the check for !idx evaluates to false. The code will then decrement idx to 4
> and look up the program for the previous instruction.
>
> If this empty range is located at the start of a function, looking up the
> previous instruction will fall into the preceding function, potentially
> triggering a spurious "spans functions" error or silently accepting invalid
> records.
Good point. Will fix this corner case.
>
next prev parent reply other threads:[~2026-09-19 20:21 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 4:41 [PATCH bpf-next v2 00/20] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 01/20] bpf: Accept the compiler's exception cleanup table at program load Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 02/20] bpf: Add the bpf_unwind_resume() kfunc Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 03/20] bpf: Add lookups for exception cleanup resumes and landing pads Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 4:55 ` Alexei Starovoitov
2026-09-19 17:42 ` Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 04/20] bpf: Mark the call sites an exception cleanup table covers Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 05/20] bpf: Make exception landing pads reachable in the CFG Yonghong Song
2026-09-18 4:59 ` sashiko-bot
2026-09-19 19:17 ` Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 19:32 ` Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 06/20] bpf: Explore the landing pads no call site reaches Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 19:32 ` Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 07/20] bpf: Refuse exception cleanup shapes bpf_throw() cannot dispatch Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 08/20] bpf: Walk the exception unwind in the verifier Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 09/20] bpf: Refuse a private stack for a program with an exception cleanup table Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 19:37 ` Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 10/20] bpf: Dispatch exception cleanup pads from bpf_throw() Yonghong Song
2026-09-18 5:58 ` bot+bpf-ci
2026-09-19 19:54 ` Yonghong Song
2026-09-18 4:42 ` [PATCH bpf-next v2 11/20] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
2026-09-18 5:03 ` sashiko-bot
2026-09-19 20:00 ` Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 20:04 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 12/20] bpf, arm64: " Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 20:07 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 13/20] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 14/20] libbpf: Add cleanup_info to bpf_prog_load_opts Yonghong Song
2026-09-18 4:57 ` sashiko-bot
2026-09-19 20:18 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 15/20] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
2026-09-18 5:00 ` sashiko-bot
2026-09-19 20:21 ` Yonghong Song [this message]
2026-09-18 4:43 ` [PATCH bpf-next v2 16/20] libbpf: Carry the exception cleanup table through the light skeleton Yonghong Song
2026-09-18 5:02 ` sashiko-bot
2026-09-19 20:27 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 17/20] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
2026-09-18 5:01 ` sashiko-bot
2026-09-19 20:31 ` Yonghong Song
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 20:32 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 18/20] selftests/bpf: Add an end-to-end .bpf_cleanup exception test Yonghong Song
2026-09-18 4:59 ` sashiko-bot
2026-09-18 5:58 ` bot+bpf-ci
2026-09-19 20:34 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 19/20] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
2026-09-18 5:01 ` sashiko-bot
2026-09-18 5:44 ` bot+bpf-ci
2026-09-19 21:13 ` Yonghong Song
2026-09-18 4:43 ` [PATCH bpf-next v2 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=5e04d0f0-52d7-4e78-a608-7acae44f6908@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox