All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v1 13/17] bpf: Report Program Structure CFG errors
Date: Fri, 05 Jun 2026 06:51:30 +0000	[thread overview]
Message-ID: <20260605065130.BB6CA1F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260605063412.974640-14-memxor@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The `reason` stack buffer in `check_subprogs()` is undersized and will truncate the diagnostic message for moderately sized BPF programs.
- [Medium] The newly added diagnostic suggestion explicitly advises users to use `throw` to end subprograms, contradicting the verifier's actual enforcement logic.
--

commit 18889a5137c9a55cdba7f071532572199ed7120c
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>

bpf: Report Program Structure CFG errors

This commit augments selected subprogram CFG validation failures with
Program Structure reports. It covers jumps leaving the current subprogram
and subprograms falling through into the next.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index e30d79149cc7d..325c162e3df0d 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
[ ... ]
> @@ -2879,6 +2879,7 @@ static int check_subprogs(struct bpf_verifier_env *env)
>  	int i, subprog_start, subprog_end, off, cur_subprog = 0;
>  	struct bpf_subprog_info *subprog = env->subprog_info;
>  	struct bpf_insn *insn = env->prog->insnsi;
> +	char reason[160];
>  	int insn_cnt = env->prog->len;
>  
>  	/* now check that all jumps are within the same subprog */
> @@ -2907,6 +2908,15 @@ static int check_subprogs(struct bpf_verifier_env *env)
>  		off = i + bpf_jmp_offset(&insn[i]) + 1;
>  		if (off < subprog_start || off >= subprog_end) {
>  			verbose(env, "jump out of range from insn %d to %d\n", i, off);
> +			scnprintf(reason, sizeof(reason),
> +				  "Instruction %d jumps to instruction %d, but subprogram %d only contains instructions %d through %d. A branch target must stay inside "
> +				  "the same subprogram",
> +				  i, off, cur_subprog, subprog_start,
> +				  subprog_end - 1);

[Severity: Medium]
Does this code risk truncating the diagnostic message?

The format string contains 142 characters of static text and accepts 5 integer
arguments. Because BPF programs can contain up to 1,000,000 instructions,
instruction indices can take up to 7 digits.

Formatting these integers can add up to 31 characters, resulting in a string
length of around 173 bytes.

Since the reason buffer is limited to 160 bytes, scnprintf() will silently
truncate the output and the user will see a cut-off diagnostic message.

> +			bpf_diag_report_program_structure(env, i,
> +							  "jump out of range",
> +							  reason,
> +							  "Keep branch targets within the same subprogram, or use an explicit subprogram call.");
>  			return -EINVAL;
>  		}
>  next:
> @@ -2919,6 +2929,13 @@ static int check_subprogs(struct bpf_verifier_env *env)
>  			    code != (BPF_JMP32 | BPF_JA) &&
>  			    code != (BPF_JMP | BPF_JA)) {
>  				verbose(env, "last insn is not an exit or jmp\n");
> +				scnprintf(reason, sizeof(reason),
> +					  "Subprogram %d reaches its last instruction %d without an exit or jump, so control could continue into the next subprogram",
> +					  cur_subprog, i);
> +				bpf_diag_report_program_structure(env, i,
> +								  "subprogram can fall through",
> +								  reason,
> +								  "End each subprogram with exit, throw, or an explicit jump that keeps control flow inside the subprogram.");

[Severity: Medium]
Will this suggestion mislead users trying to end their subprogram with a throw?

The newly added suggestion advises users to use throw to end subprograms.

However, the immediately preceding logic in check_subprogs() verifies the final
instruction using:

    if (code != (BPF_JMP | BPF_EXIT) &&
        code != (BPF_JMP32 | BPF_JA) &&
        code != (BPF_JMP | BPF_JA)) {

This condition does not account for bpf_throw(), which is a kfunc and evaluates
as a BPF_JMP | BPF_CALL instruction.

If a user writes assembly following the newly provided suggestion and places a
bpf_throw call as the absolute end of their subprogram, won't the verifier
still reject it and print this exact error?

>  				return -EINVAL;
>  			}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260605063412.974640-1-memxor@gmail.com?part=13

  reply	other threads:[~2026-06-05  6:51 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-05  6:33 [PATCH bpf-next v1 00/17] Redesign Verification Errors Kumar Kartikeya Dwivedi
2026-06-05  6:33 ` [PATCH bpf-next v1 01/17] bpf: Add verifier diagnostics report helpers Kumar Kartikeya Dwivedi
2026-06-05  6:42   ` sashiko-bot
2026-06-05  7:40   ` bot+bpf-ci
2026-06-05 18:58   ` Eduard Zingerman
2026-06-05  6:33 ` [PATCH bpf-next v1 02/17] bpf: Define verifier diagnostic categories Kumar Kartikeya Dwivedi
2026-06-05 19:10   ` Eduard Zingerman
2026-06-05  6:33 ` [PATCH bpf-next v1 03/17] bpf: Add source and instruction diagnostic context Kumar Kartikeya Dwivedi
2026-06-05  8:48   ` sashiko-bot
2026-06-05 20:22   ` Eduard Zingerman
2026-06-05 20:55     ` Kumar Kartikeya Dwivedi
2026-06-05 21:07       ` Eduard Zingerman
2026-06-05  6:33 ` [PATCH bpf-next v1 04/17] bpf: Track verifier branch diagnostic history Kumar Kartikeya Dwivedi
2026-06-05  6:50   ` sashiko-bot
2026-06-05  7:57   ` bot+bpf-ci
2026-06-05 21:41     ` Eduard Zingerman
2026-06-05 21:37   ` Eduard Zingerman
2026-06-05  6:33 ` [PATCH bpf-next v1 05/17] bpf: Track verifier register " Kumar Kartikeya Dwivedi
2026-06-05  6:53   ` sashiko-bot
2026-06-05  7:40   ` bot+bpf-ci
2026-06-05 22:31   ` Eduard Zingerman
2026-06-05  6:33 ` [PATCH bpf-next v1 06/17] bpf: Track verifier reference " Kumar Kartikeya Dwivedi
2026-06-05  6:33 ` [PATCH bpf-next v1 07/17] bpf: Track verifier context " Kumar Kartikeya Dwivedi
2026-06-05  6:46   ` sashiko-bot
2026-06-05  7:22   ` bot+bpf-ci
2026-06-05  6:33 ` [PATCH bpf-next v1 08/17] bpf: Report Register Type Safety errors Kumar Kartikeya Dwivedi
2026-06-05  6:57   ` sashiko-bot
2026-06-05  7:23   ` bot+bpf-ci
2026-06-05  6:33 ` [PATCH bpf-next v1 09/17] bpf: Report Memory Safety bounds errors Kumar Kartikeya Dwivedi
2026-06-05  6:45   ` sashiko-bot
2026-06-05  7:57   ` bot+bpf-ci
2026-06-05  6:34 ` [PATCH bpf-next v1 10/17] bpf: Report Resource Lifetime reference leaks Kumar Kartikeya Dwivedi
2026-06-05  6:45   ` sashiko-bot
2026-06-05  7:22   ` bot+bpf-ci
2026-06-05  6:34 ` [PATCH bpf-next v1 11/17] bpf: Report Call Type Safety argument errors Kumar Kartikeya Dwivedi
2026-06-05  6:47   ` sashiko-bot
2026-06-05  7:23   ` bot+bpf-ci
2026-06-05  6:34 ` [PATCH bpf-next v1 12/17] bpf: Report Execution Context Safety errors Kumar Kartikeya Dwivedi
2026-06-05  6:46   ` sashiko-bot
2026-06-05  7:23   ` bot+bpf-ci
2026-06-05  6:34 ` [PATCH bpf-next v1 13/17] bpf: Report Program Structure CFG errors Kumar Kartikeya Dwivedi
2026-06-05  6:51   ` sashiko-bot [this message]
2026-06-05  7:22   ` bot+bpf-ci
2026-06-05  6:34 ` [PATCH bpf-next v1 14/17] bpf: Report Policy helper and kfunc errors Kumar Kartikeya Dwivedi
2026-06-05  7:02   ` sashiko-bot
2026-06-05  6:34 ` [PATCH bpf-next v1 15/17] bpf: Report Verifier Limit errors Kumar Kartikeya Dwivedi
2026-06-05  6:53   ` sashiko-bot
2026-06-05  7:40   ` bot+bpf-ci
2026-06-05  6:34 ` [PATCH bpf-next v1 16/17] bpf: Report Verifier Internal errors Kumar Kartikeya Dwivedi
2026-06-05  6:34 ` [PATCH bpf-next v1 17/17] bpf: Gate verifier diagnostics on log level Kumar Kartikeya Dwivedi
2026-06-05  6:58   ` sashiko-bot
2026-06-05  7:40   ` bot+bpf-ci

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=20260605065130.BB6CA1F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=memxor@gmail.com \
    --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.