[PATCH] io_uring/kbuf: keep ring buffer selection length within MAX_RW_COUNT

From: Hang Zhengyang

Date: Sun Dec 06 2026 - 04:35:14 EST


io_ring_buffer_select() can select a length of 2G or more: ring entries
have a 32-bit length, and a request length of 0 takes the whole entry.
When the buffer is committed right away, for a file that can't be polled
or from io-wq, io_kbuf_commit() takes that length as a negative int. It
then clears REQ_F_BUFFERS_COMMIT without moving the ring head, and the
next selection hands out the same buffer again. With one 0x80000000-byte
buffer in the ring, two reads of a tmpfs file with a length of 0 both
get the same buffer ID.

The recv and recvmsg paths clamp the length only after the selection,
which doesn't help a recv from io-wq. Clamp it to MAX_RW_COUNT in
io_ring_buffer_select() instead, as io_ring_buffers_peek() already does.
An incremental entry longer than MAX_RW_COUNT is then consumed
MAX_RW_COUNT at a time by an early commit, as io_buffers_select() does
for sends.

Found by Andres Berbescu with AI assistance; fixed and tested by Claude
Code, I handled the review process.

Suggested-by: Andres Berbescu <aberbescu@xxxxxxxxx>
Reported-by: Andres Berbescu <aberbescu@xxxxxxxxx>
Closes: https://lore.kernel.org/io-uring/CAAOipkZW2sfxsnbXemEdxENHr4a0rKOBGPkUn44xsNbmarZpBw@xxxxxxxxxxxxxx/
Fixes: ae98dbf43d75 ("io_uring/kbuf: add support for incremental buffer consumption")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---
Tested on for-7.5/io_uring in a KVM guest, as an unprivileged user, with
one buffer of 0x1000, 0x7fffffff, 0x80000000 or 0xffffffff bytes in the
ring and two requests with a length of 0: reads of a tmpfs file on a
plain and on an incremental ring, and recvs on a plain ring, each with
and without IOSQE_ASYNC. Without the patch, 10 of the 24 runs hand the
same buffer out twice or leave the incremental entry unconsumed: all
reads and the recvs with IOSQE_ASYNC at 0x80000000 and 0xffffffff bytes.
With it all 24 pass. The full liburing suite shows no new failures. A
liburing test follows separately.

The clamps in net.c are now redundant but harmless; I left them alone.

The patch applies to 7.4-rc and 7.3.y. 6.12.y and 6.18.y don't have the
access_ok() check that follows the new line; there the line goes right
after "*len = buf_len;".

io_uring/kbuf.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
index 6e4c119709de..c149d6d9c9dd 100644
--- a/io_uring/kbuf.c
+++ b/io_uring/kbuf.c
@@ -212,6 +212,8 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,
buf_len = READ_ONCE(buf->len);
if (*len == 0 || *len > buf_len)
*len = buf_len;
+ /* ring provided buffers can be up to 4G, the commit takes an int */
+ *len = min_t(size_t, *len, MAX_RW_COUNT);
sel.addr = u64_to_user_ptr(READ_ONCE(buf->addr));
if (unlikely(!access_ok(sel.addr, *len))) {
sel.addr = NULL;

base-commit: 416f1d31a8acea8b45b5da50e78147b83ccf63a8
--
2.53.0