io_uring: fix resource leak issues
Subsystems (4)
cover io_uring: fix resource leak issues
From: Koba Ko <[email protected]>
Three resource leak fixes found by code audit:
1. memmap: pinned pages and pages array leak on WARN_ON path in io_region_pin_pages() — mr->pages is never assigned so the caller's cleanup is a no-op.
2. rsrc: kfree() used instead of io_cache_free() in io_buffer_register_bvec() error path — bypasses cache return.
3. zcrx: io_import_umem() leaves live pinned pages in a partially initialized struct on io_account_mem() failure, and io_release_area_mem() is not idempotent (missing pages = NULL), creating a double-free hazard.
Koba Ko (3):
io_uring: fix pinned pages and pages array leak in
io_region_pin_pages()
io_uring/rsrc: use io_cache_free for node in io_buffer_register_bvec
error path
io_uring/zcrx: fix resource leak and double-free hazard in
io_import_umem
io_uring/memmap.c | 5 ++++- io_uring/rsrc.c | 2 +- io_uring/zcrx.c | 19 +++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 io_uring: fix pinned pages and pages array leak in io_region_pin_pages()
From: Koba Ko <[email protected]>
When io_pin_pages() succeeds but the subsequent nr_pages sanity check fires (WARN_ON_ONCE), the function returns -EFAULT without unpinning the user pages or freeing the kvmalloc'd pages array. The caller's cleanup via io_free_region() won't help either, because mr->pages was never assigned — so the entire cleanup block is skipped.
Add unpin_user_pages() and kvfree() before the error return to prevent the leak.
--- io_uring/memmap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
io_uring/memmap.c +4 -1
@@ -141,8 +141,11 @@ static int io_region_pin_pages(struct io_mapped_region *mr, pages =io_pin_pages (reg->user_addr, size, &nr_pages);if (IS_ERR (pages))return PTR_ERR (pages); -if (WARN_ON_ONCE (nr_pages != mr->nr_pages)) +if (WARN_ON_ONCE (nr_pages != mr->nr_pages)) { +unpin_user_pages (pages, nr_pages); +kvfree (pages);return -EFAULT ; + } mr->pages = pages; mr->flags |=IO_REGION_F_USER_PROVIDED ;
Signature · 1 line
2.43.0
Pavel Begunkov
maintainerRe: [PATCH 1/3] io_uring: fix pinned pages and pages array leak in io_region_pin_pages()
Quoted 8 lines · KobaK
From: Koba Ko <[email protected]>
When
io_pin_pages()succeeds but the subsequentnr_pagessanity check fires (WARN_ON_ONCE), the function returns-EFAULTwithout unpinning the user pages or freeing the kvmalloc'd pages array. The caller's cleanup viaio_free_region()won't help either, becausemr->pageswas never assigned — so the entire cleanup block is skipped.
Quoted 5 lines
Add
unpin_user_pages()andkvfree()before the error return to prevent the leak.Fixes a90558b36ccee ("io_uring/memmap: helper for pinning region pages")Signed-off-by Koba Ko <[email protected]>
It's a WARN path, it should never happen, but if it does, that means io_pin_pages() is buggy, and it's better to leak rather than risk something nastier.
Quoted 21 lines
--- io_uring/memmap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)io_uring/memmap.c +4
-1@@ -141,8 +141,11 @@ static int io_region_pin_pages(struct io_mapped_region *mr, pages =io_pin_pages (reg->user_addr, size, &nr_pages);if (IS_ERR (pages))return PTR_ERR (pages); -if (WARN_ON_ONCE (nr_pages != mr->nr_pages)) +if (WARN_ON_ONCE (nr_pages != mr->nr_pages)) { +unpin_user_pages (pages, nr_pages); +kvfree (pages);return -EFAULT ; + } mr->pages = pages; mr->flags |=IO_REGION_F_USER_PROVIDED ;
Signature · 1 line
Pavel Begunkov
PATCH 2/3 io_uring/rsrc: use io_cache_free for node in io_buffer_register_bvec error path
In reply to KobaKFrom: Koba Ko <[email protected]>
io_buffer_register_bvec() allocates the rsrc node via io_rsrc_node_alloc() which pulls from ctx->node_cache. On imu allocation failure, the node is freed with raw kfree() instead of io_cache_free(&ctx->node_cache, node), bypassing the cache return path and wasting a reuse opportunity. Every other error path in this file correctly uses io_cache_free for nodes.
--- io_uring/rsrc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/rsrc.c +1 -1
@@ -961,7 +961,7 @@ int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,*/ imu =io_alloc_imu (ctx,blk_rq_nr_phys_segments (rq));if (!imu) { -kfree (node); +io_cache_free (&ctx->node_cache, node); ret = -ENOMEM ; goto unlock; }
Signature · 1 line
2.43.0
Pavel Begunkov
maintainerRe: [PATCH 2/3] io_uring/rsrc: use io_cache_free for node in io_buffer_register_bvec error path
Quoted 20 lines · KobaK
From: Koba Ko <[email protected]>
io_buffer_register_bvec()allocates the rsrc node viaio_rsrc_node_alloc()which pulls fromctx->node_cache. On imu allocation failure, the node is freed with rawkfree()instead ofio_cache_free(&ctx->node_cache, node), bypassing the cache return path and wasting a reuse opportunity. Every other error path in this file correctly usesio_cache_freefor nodes.Fixes 27cb27b6d5ea4 ("io_uring: add support for kernel registered bvecs")Signed-off-by Koba Ko <[email protected]>--- io_uring/rsrc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)io_uring/rsrc.c +0
-0
io_uring/rsrc.c int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq, · line 964
Quoted 5 lines
io_uring/rsrc.c +1
-1@@ -961,7 +961,7 @@ int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,*/ imu =io_alloc_imu (ctx,blk_rq_nr_phys_segments (rq));if (!imu) { -kfree (node); +io_cache_free (&ctx->node_cache, node);
Looks like it was already patched a week ago
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [PATCH 2/3] io_uring/rsrc: use io_cache_free for node in io_buffer_register_bvec error path
Quoted 16 lines · Pavel Begunkov
Quoted 15 lines · KobaK
From: Koba Ko <[email protected]>
io_buffer_register_bvec()allocates the rsrc node viaio_rsrc_node_alloc()which pulls fromctx->node_cache. On imu allocation failure, the node is freed with rawkfree()instead ofio_cache_free(&ctx->node_cache, node), bypassing the cache return path and wasting a reuse opportunity. Every other error path in this file correctly usesio_cache_freefor nodes.Fixes 27cb27b6d5ea4 ("io_uring: add support for kernel registered bvecs")Signed-off-by Koba Ko <[email protected]>--- io_uring/rsrc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
io_uring/rsrc.c int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq, · line 964
Quoted 10 lines
io_uring/rsrc.c +1
-1@@ -961,7 +961,7 @@ int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,*/ imu =io_alloc_imu (ctx,blk_rq_nr_phys_segments (rq));if (!imu) { -kfree (node); +io_cache_free (&ctx->node_cache, node);Looks like it was already patched a week ago
Indeed, and main motivation was to eliminate reports like this, where (clearly) an LLM spotted the "problem" even if there is none. Guess that was a good move, just need it to land so that we don't need to waste time on this again.
Nobody cares about "wasting a reuse opportunity" in an error path, any human would recognize that.
Signature · 1 line
Jens Axboe
PATCH 3/3 io_uring/zcrx: fix resource leak and double-free hazard in io_import_umem
In reply to KobaKFrom: Koba Ko <[email protected]>
io_import_umem() has two problems:
1. When io_account_mem() fails, the function returns an error but leaves live pinned pages and sg_table in the mem struct without cleaning them up. The caller happens to handle this today via io_zcrx_free_area() -> io_release_area_mem(), but the contract is fragile.
2. io_release_area_mem() doesn't NULL out mem->pages after kvfree(), making it unsafe to call twice. Since io_zcrx_free_area() always calls it during teardown, any earlier cleanup call would cause a double-free.
Fix both: populate mem fields before io_account_mem() so io_release_area_mem() can do a proper cleanup on failure, and add mem->pages = NULL in io_release_area_mem() to make it idempotent.
--- io_uring/zcrx.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
io_uring/zcrx.c +13 -6
@@ -188,6 +188,8 @@ static unsigned long io_count_account_pages(struct page **pages, unsigned nr_pagreturn res; } +static void io_release_area_mem (struct io_zcrx_mem *mem); +static int io_import_umem (struct io_zcrx_ifq *ifq,struct io_zcrx_mem *mem,struct io_uring_zcrx_area_reg *area_reg)
@@ -213,16 +215,20 @@ static int io_import_umem(struct io_zcrx_ifq *ifq,return ret; } - mem->account_pages =io_count_account_pages (pages, nr_pages); - ret =io_account_mem (ifq->user, ifq->mm_account, mem->account_pages); -if (ret <0 ) - mem->account_pages =0 ; - mem->sgt = &mem->page_sg_table; mem->pages = pages; mem->nr_folios = nr_pages; mem->size = area_reg->len; -return ret; + + mem->account_pages =io_count_account_pages (pages, nr_pages); + ret =io_account_mem (ifq->user, ifq->mm_account, mem->account_pages); +if (ret <0 ) { + mem->account_pages =0 ; +io_release_area_mem (mem); +return ret; + } + +return 0 ; }static void io_release_area_mem (struct io_zcrx_mem *mem)
@@ -236,6 +242,7 @@ static void io_release_area_mem(struct io_zcrx_mem *mem)sg_free_table (mem->sgt); mem->sgt =NULL ;kvfree (mem->pages); + mem->pages =NULL ; } }
Signature · 1 line
2.43.0
Pavel Begunkov
maintainerRe: [PATCH 3/3] io_uring/zcrx: fix resource leak and double-free hazard in io_import_umem
Quoted 8 lines · KobaK
From: Koba Ko <[email protected]>
io_import_umem()has two problems:1. When
io_account_mem()fails, the function returns an error but leaves live pinned pages andsg_tablein the mem struct without cleaning them up. The caller happens to handle this today viaio_zcrx_free_area()->io_release_area_mem(), but the contract is fragile.
That was the intention for the caller to clean it up, but in either case the function has already been rewritten. In general, it seems you based your patches on top of an outdated tree.
Quoted 12 lines
2.
io_release_area_mem()doesn't NULL outmem->pagesafterkvfree(), making it unsafe to call twice. Sinceio_zcrx_free_area()always calls it during teardown, any earlier cleanup call would cause a double-free.Fix both: populate mem fields before
io_account_mem()soio_release_area_mem()can do a proper cleanup on failure, and addmem->pages= NULL inio_release_area_mem()to make it idempotent.Fixes 262ab205180d2 ("io_uring/zcrx: account area memory")Signed-off-by Koba Ko <[email protected]>---
...
io_uring/zcrx.c static void io_release_area_mem(struct io_zcrx_mem *mem) · line 245
Quoted 7 lines
static void
io_release_area_mem(struct io_zcrx_mem *mem)io_uring/zcrx.c +1
-0@@ -236,6 +242,7 @@ static void io_release_area_mem(struct io_zcrx_mem *mem)sg_free_table (mem->sgt); mem->sgt =NULL ;kvfree (mem->pages); + mem->pages =NULL ;
The entire struct io_zcrx_mem / area is freed right after, calling io_zcrx_free_area() multiple times for the same area is not allowed.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerQuoted 8 lines · KobaK
From: Koba Ko <[email protected]>
Three resource leak fixes found by code audit:
1. memmap: pinned pages and pages array leak on
WARN_ONpath inio_region_pin_pages()?mr->pagesis never assigned so the caller's cleanup is a no-op.
Quoted 7 lines
2. rsrc:
kfree()used instead ofio_cache_free()inio_buffer_register_bvec()error path ? bypasses cache return.3. zcrx:
io_import_umem()leaves live pinned pages in a partially initialized struct onio_account_mem()failure, andio_release_area_mem()is not idempotent (missing pages = NULL), creating a double-free hazard.
General advice - anyone can point an LLM at a code base and get some reports, but please apply some actual critical thinking to the "issues" found before blindly sending them out.
Signature · 1 line
Jens Axboe