BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hasan Basbunar" <basbunarhasan@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v2] bpf: bpf_dbg: fix off-by-one in cmd_select
Date: Wed, 29 Apr 2026 09:41:23 +0000	[thread overview]
Message-ID: <20260429094124.8AF41C2BCB3@smtp.kernel.org> (raw)
In-Reply-To: <20260429084441.22089-1-basbunarhasan@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch introduces a logic regression in `cmd_select` that skips bounds-checking for the newly selected packet, allowing subsequent commands to blindly dereference invalid or out-of-bounds packets and crash the debugger.
--

commit 34793f5a31a990a20ff41f65cd4c82f4419849d0
Author: Hasan Basbunar <basbunarhasan@gmail.com>

bpf: bpf_dbg: fix off-by-one in cmd_select

This commit changes the loop in cmd_select() to start from 1 instead of 0.
The intent is to fix an off-by-one error where selecting a packet advances
the cursor one packet too far.

> After this fix, 'select K' shows packet K for all K in 1..N, and
> 'select N+1' correctly errors with "no packet #N+1 available!".

Is this description accurate?

For select N+1, the loop will run N times and attempt to validate the
first N packets. If the Nth packet is valid, pcap_next_pkt() returns
true and parks the cursor at the end of the file. Because have_next remains
true, it appears the function will return CMD_OK rather than an error.

Does it only error during testing due to a pre-existing off-by-one check in
pcap_next_pkt() that falsely rejects valid packets aligning exactly with the
file's end? If the PCAP has any trailing garbage bytes, it seems select N+1
would silently succeed.

> diff --git a/tools/bpf/bpf_dbg.c b/tools/bpf/bpf_dbg.c
> index 00e560a17baf..4895602ab37d 100644
> --- a/tools/bpf/bpf_dbg.c
> +++ b/tools/bpf/bpf_dbg.c
> @@ -1141,7 +1141,7 @@ static int cmd_select(char *num)
>  	pcap_reset_pkt();
>  	bpf_reset();
>  
> -	for (i = 0; i < which && (have_next = pcap_next_pkt()); i++)
> +	for (i = 1; i < which && (have_next = pcap_next_pkt()); i++)

Does this change skip bounds checking for the newly selected packet?

Looking at pcap_next_pkt(), it validates the current packet and then
unconditionally advances the cursor. By reducing the loop to which - 1
iterations, it seems cmd_select() positions the cursor on the target packet
but never passes that packet to pcap_next_pkt() for validation.

For example, if which is 1, the loop executes 0 times and have_next remains
true. Could this cause cmd_select() to return CMD_OK even if the PCAP is
empty or the first packet is truncated?

If so, could subsequent commands like run or step blindly dereference the
unvalidated packet from pcap_curr_pkt(), potentially causing an
out-of-bounds memory read?

>  		/* noop */;
>  	if (!have_next || pcap_curr_pkt() == NULL) {
>  		rl_printf("no packet #%u available!\n", which);

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

  reply	other threads:[~2026-04-29  9:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-28 10:01 [PATCH] bpf: bpf_dbg: fix off-by-one in cmd_select and pcap_next_pkt Hasan Basbunar
2026-04-28 21:03 ` sashiko-bot
2026-04-29  8:44 ` [PATCH v2] bpf: bpf_dbg: fix off-by-one in cmd_select Hasan Basbunar
2026-04-29  9:41   ` sashiko-bot [this message]
2026-04-29 12:35   ` [PATCH v3] bpf: bpf_dbg: split pcap_next_pkt() validation/advance, " Hasan Basbunar
2026-04-29 13:13     ` 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=20260429094124.8AF41C2BCB3@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=basbunarhasan@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko@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