From: Luxiao Xu In __bpf_user_ringbuf_peek(), the kernel ensures that a sample length does not exceed the advertised unread window (prod_pos - cons_pos) and does not exceed the total data capacity of the ring buffer (ringbuf_total_data_sz(rb)). However, the kernel does not verify that the entire advertised unread window (prod_pos - cons_pos) fits within the ring buffer capacity. Because the producer position page is mapped writable to user space, a malicious or buggy user-space producer can advance producer_pos arbitrarily far ahead of consumer_pos while populating only one physical lap of data. When bpf_user_ringbuf_drain() drains the ring buffer, cons_pos wraps around the physical buffer repeatedly within the oversized unread window, replaying stale or already-consumed samples without new writes from user space. In the worst case, this leads to excessive CPU consumption and watchdog softlockups. Validate that prod_pos - cons_pos <= ringbuf_total_data_sz(rb) before attempting to peek samples. If the advertised backlog exceeds the ring size, return -EINVAL immediately. Fixes: 205715673844 ("bpf: Add bpf_user_ringbuf_drain() helper") Cc: stable@vger.kernel.org Reported-by: VEGA Signed-off-by: Luxiao Xu Signed-off-by: Ren Wei --- kernel/bpf/ringbuf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 447863ece26a..cfd4fe4e85ee 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -761,6 +761,9 @@ static int __bpf_user_ringbuf_peek(struct bpf_ringbuf *rb, void **sample, u32 *s if (cons_pos >= prod_pos) return -ENODATA; + if (prod_pos - cons_pos > ringbuf_total_data_sz(rb)) + return -EINVAL; + hdr = (u32 *)((uintptr_t)rb->data + (uintptr_t)(cons_pos & rb->mask)); /* Synchronizes with smp_store_release() in user-space producer. */ hdr_len = smp_load_acquire(hdr); -- 2.43.0