BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Israel Téllez García" <i.tellez@btesa.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v2 bpf 3/4] bpf: Read producer_pos before overwrite_pos in ringbuf_avail_data_sz()
Date: Fri, 14 Aug 2026 13:01:04 +0000	[thread overview]
Message-ID: <20260814130105.0AC8D1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260814124843.22041-4-i.tellez@btesa.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Swapping the load order of producer_pos and overwrite_pos introduces a TOCTOU race that causes an integer underflow in `prod_pos - over_pos`, resulting in a massively overstated available data size.
--

--- Patch [3]: [PATCH v2 bpf 3/4] bpf: Read producer_pos before overwrite_pos in ringbuf_avail_data_sz() ---
commit efaae45af0e3891c65188df6e4677342c6fb0c41
Author: Israel Téllez García <i.tellez@btesa.com>

bpf: Read producer_pos before overwrite_pos in ringbuf_avail_data_sz()

This commit changes the order in which producer_pos and overwrite_pos are
read in ringbuf_avail_data_sz() to match the documented memory ordering
contract. It swaps the reads so that the acquire load of producer_pos
orders the subsequent load of overwrite_pos.

> diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> index 0fefa89039beb..89a9c15a260f3 100644
> --- a/kernel/bpf/ringbuf.c
> +++ b/kernel/bpf/ringbuf.c
> @@ -319,8 +319,14 @@ static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb)
>  	cons_pos = smp_load_acquire(&rb->consumer_pos);
>  
>  	if (unlikely(rb->overwrite_mode)) {
> -		over_pos = smp_load_acquire(&rb->overwrite_pos);
> +		/*
> +		 * Read producer_pos first: its release store in
> +		 * __bpf_ringbuf_reserve() is what publishes the matching
> +		 * overwrite_pos.
> +		 */
>  		prod_pos = smp_load_acquire(&rb->producer_pos);
> +		/* Ordered after the acquire load above, per that contract. */
> +		over_pos = smp_load_acquire(&rb->overwrite_pos);
>  		return min(prod_pos - cons_pos, prod_pos - over_pos);

[Severity: High]
Does this sequence allow an integer underflow in prod_pos - over_pos?

If the reader is preempted after reading prod_pos, and a concurrent producer
advances overwrite_pos beyond the stale prod_pos snapshot:

Reader:
    prod_pos = smp_load_acquire(&rb->producer_pos);
    <preempted>

Producer:
    // advances overwrite_pos beyond the snapshotted prod_pos
    rb->overwrite_pos = ...;
    smp_store_release(&rb->producer_pos, ...);

Reader resumes:
    over_pos = smp_load_acquire(&rb->overwrite_pos);
    return min(prod_pos - cons_pos, prod_pos - over_pos);

Could over_pos now be greater than prod_pos, causing the subtraction to
underflow to a massive unsigned value near ULONG_MAX?

This would result in min() returning prod_pos - cons_pos, which could be
extremely large and break EPOLLOUT for user-space producers by returning
incorrectly large available data sizes.

>  	} else {
>  		prod_pos = smp_load_acquire(&rb->producer_pos);
>  		return prod_pos - cons_pos;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260814124843.22041-1-i.tellez@btesa.com?part=3

  reply	other threads:[~2026-08-14 13:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-14 12:48 [PATCH v2 bpf 0/4] bpf: Fix ring buffer position wrap handling on 32-bit Israel Téllez García
2026-08-14 12:48 ` [PATCH v2 bpf 1/4] bpf: Fix pending_pos walk on 32-bit ring position wrap Israel Téllez García
2026-08-14 13:05   ` sashiko-bot
2026-08-14 13:31   ` bot+bpf-ci
2026-08-14 12:48 ` [PATCH v2 bpf 2/4] bpf: Fix available-data accounting on 32-bit wrap in overwrite mode Israel Téllez García
2026-08-14 13:04   ` sashiko-bot
2026-08-14 12:48 ` [PATCH v2 bpf 3/4] bpf: Read producer_pos before overwrite_pos in ringbuf_avail_data_sz() Israel Téllez García
2026-08-14 13:01   ` sashiko-bot [this message]
2026-08-14 13:30   ` bot+bpf-ci
2026-08-14 14:09   ` Israel Téllez
2026-08-14 12:48 ` [PATCH v2 bpf 4/4] libbpf: Fix ring buffer consumer loop on 32-bit position wrap Israel Téllez García
2026-08-14 13:07   ` sashiko-bot
2026-08-14 13:31   ` 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=20260814130105.0AC8D1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=i.tellez@btesa.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox