diff mbox series

io_uring: check for rollover of buffer ID when providing buffers

Message ID 82171764-77aa-0f8a-a9c7-3c465ffe51a5@kernel.dk (mailing list archive)
State New
Headers show
Series io_uring: check for rollover of buffer ID when providing buffers | expand

Commit Message

Jens Axboe Nov. 10, 2022, 5:55 p.m. UTC
We already check if the chosen starting offset for the buffer IDs fit
within an unsigned short, as 65535 is the maximum value for a provided
buffer. But if the caller asks to add N buffers at offset M, and M + N
would exceed the size of the unsigned short, we simply add buffers with
wrapping around the ID.

This is not necessarily a bug and could in fact be a valid use case, but
it seems confusing and inconsistent with the initial check for starting
offset. Let's check for wrap consistently, and error the addition if we
do need to wrap.

Reported-by: Oliver Lang <Oliver.Lang@gossenmetrawatt.com>
Link: https://github.com/axboe/liburing/issues/726
Cc: stable@vger.kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>

---

Comments

Jens Axboe Nov. 10, 2022, 6:08 p.m. UTC | #1
On 11/10/22 10:55 AM, Jens Axboe wrote:
> We already check if the chosen starting offset for the buffer IDs fit
> within an unsigned short, as 65535 is the maximum value for a provided
> buffer. But if the caller asks to add N buffers at offset M, and M + N
> would exceed the size of the unsigned short, we simply add buffers with
> wrapping around the ID.
> 
> This is not necessarily a bug and could in fact be a valid use case, but
> it seems confusing and inconsistent with the initial check for starting
> offset. Let's check for wrap consistently, and error the addition if we
> do need to wrap.
> 
> Reported-by: Oliver Lang <Oliver.Lang@gossenmetrawatt.com>

Sorry, that was the wrong email, I have updated the commit locally.
diff mbox series

Patch

diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
index 25cd724ade18..e2c46889d5fa 100644
--- a/io_uring/kbuf.c
+++ b/io_uring/kbuf.c
@@ -346,6 +346,8 @@  int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe
 	tmp = READ_ONCE(sqe->off);
 	if (tmp > USHRT_MAX)
 		return -E2BIG;
+	if (tmp + p->nbufs >= USHRT_MAX)
+		return -EINVAL;
 	p->bid = tmp;
 	return 0;
 }