io_uring/kbuf: fix use-after-free of new iovec on bundle grow
PATCH 1/1 io_uring/kbuf: fix use-after-free of new iovec on bundle grow
When io_ring_buffers_peek() grows a provided-buffer bundle, it allocates a new iovec array and points arg->iovs at it. The KBUF_MODE_FREE cleanup added at the end of the function then does kfree(arg->iovs), which frees this freshly allocated array that is about to be returned to and used by the caller, instead of the old cached iovec (org_iovs) it was meant to release. The caller reads the now-freed array, resulting in a use-after-free, easily triggered by the liburing recv-bundle-short-ooo test:
BUG: KASAN: slab-use-after-free in io_recv+0x4bc/0xc60 Read of size 8 at addr ffff00037b20c240 by task recv-bundle-sho io_recv Allocated by task: __kmalloc_noprof io_ring_buffers_peek io_buffers_peek io_recv Freed by task: kfree io_ring_buffers_peek io_buffers_peek io_recv
Free org_iovs instead, and only when it was actually replaced by a new allocation. On the access_ok() failure path the new array is already freed and the request is left pointing at the original iovec, so nothing needs to be released at this point in that case.
--- io_uring/kbuf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
io_uring/kbuf.c +2 -2
@@ -328,8 +328,8 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg, buf =io_ring_head_to_buf (br, ++head, bl->mask); }while (--nr_iovs); -if (arg->mode &KBUF_MODE_FREE ) -kfree (arg->iovs); +if ((arg->mode &KBUF_MODE_FREE ) && arg->iovs != org_iovs) +kfree (org_iovs);if (head == tail) req->flags |=REQ_F_BL_EMPTY ;
---
base-commit: bee763d5f341b99cf472afeb508d4988f62a6ca1
change-id: 20260713-io_uring_dangling-87c23d568135
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Quoted 23 lines · Breno Leitao
When
io_ring_buffers_peek()grows a provided-buffer bundle, it allocates a new iovec array and pointsarg->iovsat it. TheKBUF_MODE_FREEcleanup added at the end of the function then doeskfree(arg->iovs), which frees this freshly allocated array that is about to be returned to and used by the caller, instead of the old cached iovec (org_iovs) it was meant to release. The caller reads the now-freed array, resulting in a use-after-free, easily triggered by the liburing recv-bundle-short-ooo test:BUG: KASAN: slab-use-after-free in io_recv+0x4bc/0xc60 Read of size 8 at addr ffff00037b20c240 by task recv-bundle-sho io_recv Allocated by task: __kmalloc_noprof io_ring_buffers_peek io_buffers_peek io_recv Freed by task: kfree io_ring_buffers_peek io_buffers_peek io_recv
Quoted 7 lines
Free
org_iovsinstead, and only when it was actually replaced by a new allocation. On theaccess_ok()failure path the new array is already freed and the request is left pointing at the original iovec, so nothing needs to be released at this point in that case.Fixes cd053d788c3f ("io_uring: fix dangling iovec after provided-buffer bundle grow failure")Signed-off-by Breno Leitao <[email protected]>
Already fixed here
https://lore.kernel.org/io-uring/[email protected]/T/#u
here:
and here:
https://lore.kernel.org/io-uring/[email protected]/T/#u
Aren't LLMs fun?
Signature · 1 line
Gabriel Krisman Bertazi
Quoted 37 lines · Gabriel Krisman Bertazi
Quoted 23 lines · Breno Leitao
When
io_ring_buffers_peek()grows a provided-buffer bundle, it allocates a new iovec array and pointsarg->iovsat it. TheKBUF_MODE_FREEcleanup added at the end of the function then doeskfree(arg->iovs), which frees this freshly allocated array that is about to be returned to and used by the caller, instead of the old cached iovec (org_iovs) it was meant to release. The caller reads the now-freed array, resulting in a use-after-free, easily triggered by the liburing recv-bundle-short-ooo test:BUG: KASAN: slab-use-after-free in io_recv+0x4bc/0xc60 Read of size 8 at addr ffff00037b20c240 by task recv-bundle-sho io_recv Allocated by task: __kmalloc_noprof io_ring_buffers_peek io_buffers_peek io_recv Freed by task: kfree io_ring_buffers_peek io_buffers_peek io_recvQuoted 7 lines
Free
org_iovsinstead, and only when it was actually replaced by a new allocation. On theaccess_ok()failure path the new array is already freed and the request is left pointing at the original iovec, so nothing needs to be released at this point in that case.Fixes cd053d788c3f ("io_uring: fix dangling iovec after provided-buffer bundle grow failure")Signed-off-by Breno Leitao <[email protected]>Already fixed here
Quoted 7 lines
here:
and here:
Oh, -ETOOMANY fixes.
Quoted 1 line
Aren't LLMs fun?
Oh yes, It is easier to send the fix than to check in the mailing list if someone has fixed it already.