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 From: Luxiao Xu Add a test case validating that bpf_user_ringbuf_drain() rejects an advertised unread backlog that exceeds the ring buffer capacity. A valid sample header is written to the ring buffer, but producer_pos is advanced past consumer_pos by twice the ring size. Verify that the kernel returns -EINVAL and drains 0 samples. Signed-off-by: Luxiao Xu Signed-off-by: Ren Wei --- .../selftests/bpf/prog_tests/user_ringbuf.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/user_ringbuf.c b/tools/testing/selftests/bpf/prog_tests/user_ringbuf.c index 9fd3ae987321..b455e35d616b 100644 --- a/tools/testing/selftests/bpf/prog_tests/user_ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/user_ringbuf.c @@ -280,6 +280,22 @@ static void test_user_ringbuf_post_larger_than_ringbuf_sz(void) user_ringbuf_success__destroy(skel); } +static void test_user_ringbuf_post_backlog_larger_than_ringbuf_sz(void) +{ + struct user_ringbuf_success *skel; + struct user_ring_buffer *ringbuf; + int err; + __u32 size = sizeof(struct sample); + + err = load_skel_create_user_ringbuf(&skel, &ringbuf); + if (!ASSERT_OK(err, "oversized_backlog_skel")) + return; + + manually_write_test_invalid_sample(skel, size, c_ringbuf_size * 2, -EINVAL); + user_ring_buffer__free(ringbuf); + user_ringbuf_success__destroy(skel); +} + static void test_user_ringbuf_basic(void) { struct user_ringbuf_success *skel; @@ -673,6 +689,7 @@ static struct { SUCCESS_TEST(test_user_ringbuf_post_misaligned), SUCCESS_TEST(test_user_ringbuf_post_producer_wrong_offset), SUCCESS_TEST(test_user_ringbuf_post_larger_than_ringbuf_sz), + SUCCESS_TEST(test_user_ringbuf_post_backlog_larger_than_ringbuf_sz), SUCCESS_TEST(test_user_ringbuf_basic), SUCCESS_TEST(test_user_ringbuf_sample_full_ring_buffer), SUCCESS_TEST(test_user_ringbuf_post_alignment_autoadjust), -- 2.43.0