Skip to content

Commit 78385c7

Browse files
calebsanderaxboe
authored andcommitted
io_uring/kbuf: use READ_ONCE() for userspace-mapped memory
The struct io_uring_buf elements in a buffer ring are in a memory region accessible from userspace. A malicious/buggy userspace program could therefore write to them at any time, so they should be accessed with READ_ONCE() in the kernel. Commit 98b6fa6 ("io_uring/kbuf: always use READ_ONCE() to read ring provided buffer lengths") already switched the reads of the len field to READ_ONCE(). Do the same for bid and addr. Signed-off-by: Caleb Sander Mateos <[email protected]> Fixes: c7fb194 ("io_uring: add support for ring mapped supplied buffers") Cc: Joanne Koong <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 525916c commit 78385c7

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

io_uring/kbuf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
4444
buf_len -= this_len;
4545
/* Stop looping for invalid buffer length of 0 */
4646
if (buf_len || !this_len) {
47-
buf->addr += this_len;
47+
buf->addr = READ_ONCE(buf->addr) + this_len;
4848
buf->len = buf_len;
4949
return false;
5050
}
@@ -198,9 +198,9 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,
198198
if (*len == 0 || *len > buf_len)
199199
*len = buf_len;
200200
req->flags |= REQ_F_BUFFER_RING | REQ_F_BUFFERS_COMMIT;
201-
req->buf_index = buf->bid;
201+
req->buf_index = READ_ONCE(buf->bid);
202202
sel.buf_list = bl;
203-
sel.addr = u64_to_user_ptr(buf->addr);
203+
sel.addr = u64_to_user_ptr(READ_ONCE(buf->addr));
204204

205205
if (io_should_commit(req, issue_flags)) {
206206
io_kbuf_commit(req, sel.buf_list, *len, 1);
@@ -280,7 +280,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
280280
if (!arg->max_len)
281281
arg->max_len = INT_MAX;
282282

283-
req->buf_index = buf->bid;
283+
req->buf_index = READ_ONCE(buf->bid);
284284
do {
285285
u32 len = READ_ONCE(buf->len);
286286

@@ -295,7 +295,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
295295
}
296296
}
297297

298-
iov->iov_base = u64_to_user_ptr(buf->addr);
298+
iov->iov_base = u64_to_user_ptr(READ_ONCE(buf->addr));
299299
iov->iov_len = len;
300300
iov++;
301301

0 commit comments

Comments
 (0)