[PATCH 2/2] io_uring/kbuf: fix infinite loop in io_kbuf_inc_commit()
From: Qingyue Zhang
Date: Wed Aug 27 2025 - 07:51:33 EST
In io_kbuf_inc_commit(), buf points to a user-mapped memory region,
which means buf->len might be changed between importing and committing.
Add a check to avoid infinite loop when sum of buf->len is less than
len.
Co-developed-by: Suoxing Zhang <aftern00n@xxxxxx>
Signed-off-by: Suoxing Zhang <aftern00n@xxxxxx>
Signed-off-by: Qingyue Zhang <chunzhennn@xxxxxx>
---
io_uring/kbuf.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
index 81a13338dfab..80ffe6755598 100644
--- a/io_uring/kbuf.c
+++ b/io_uring/kbuf.c
@@ -34,11 +34,12 @@ struct io_provide_buf {
static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
{
+ struct io_uring_buf *buf, *buf_start;
+
+ buf_start = buf = io_ring_head_to_buf(bl->buf_ring, bl->head, bl->mask);
while (len) {
- struct io_uring_buf *buf;
u32 this_len;
- buf = io_ring_head_to_buf(bl->buf_ring, bl->head, bl->mask);
this_len = min_t(u32, len, buf->len);
buf->len -= this_len;
if (buf->len) {
@@ -47,6 +48,10 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
}
bl->head++;
len -= this_len;
+
+ buf = io_ring_head_to_buf(bl->buf_ring, bl->head, bl->mask);
+ if (unlikely(buf == buf_start))
+ break;
}
return true;
}
--
2.48.1