io_uring: fix page leak in io_sqe_buffer_register()
PATCH 1/1 io_uring: fix page leak in io_sqe_buffer_register()
Add missing unpin_user_pages() in the error path
--- io_uring/rsrc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
io_uring/rsrc.c +3 -1
@@ -804,8 +804,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } imu =io_alloc_imu (ctx, nr_pages); -if (!imu) +if (!imu) { +unpin_user_pages (pages, nr_pages); goto done; + } imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage);
Signature · 1 line
2.17.1
Jens Axboe
maintainerQuoted 14 lines · Penglei Jiang
Add missing
unpin_user_pages()in the error pathFixes d8c2237d0aa9 ("io_uring: add io_pin_pages() helper")Signed-off-by Penglei Jiang <[email protected]>--- io_uring/rsrc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)io_uring/rsrc.c +0
-0@@ -804,8 +804,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, }
io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 813
Quoted 10 lines
io_uring/rsrc.c +3
-1imu =io_alloc_imu (ctx, nr_pages); -if (!imu) +if (!imu) { +unpin_user_pages (pages, nr_pages); goto done; + } imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage);
Wouldn't it be better to have the unpin be part of the normal error handling? Not sure why the pin accounting failure doesn't do that already.
Totally untested...
io_uring/rsrc.c +2 -3
@@ -809,10 +809,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage); -if (ret) { -unpin_user_pages (pages, nr_pages); +if (ret) goto done; - } size = iov->iov_len;/* store original address for later verification */
@@ -840,6 +838,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } done:if (ret) { +unpin_user_pages (pages, nr_pages);if (imu)io_free_imu (ctx, imu);io_cache_free (&ctx->node_cache, node);
Signature · 1 line
Jens Axboe
Quoted 49 lines · Jens Axboe
Quoted 14 lines · Penglei Jiang
Add missing
unpin_user_pages()in the error pathFixes d8c2237d0aa9 ("io_uring: add io_pin_pages() helper")Signed-off-by Penglei Jiang <[email protected]>--- io_uring/rsrc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)io_uring/rsrc.c +0
-0@@ -804,8 +804,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, }io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 813
Quoted 10 lines
io_uring/rsrc.c +3
-1imu =io_alloc_imu (ctx, nr_pages); -if (!imu) +if (!imu) { +unpin_user_pages (pages, nr_pages); goto done; + } imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage);Wouldn't it be better to have the unpin be part of the normal error handling? Not sure why the pin accounting failure doesn't do that already.
Totally untested...
io_uring/rsrc.c +1
-3@@ -809,10 +809,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage); -if (ret) { -unpin_user_pages (pages, nr_pages); +if (ret) goto done; - } size = iov->iov_len;/* store original address for later verification */
io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 844
Quoted 7 lines
io_uring/rsrc.c +1
-0@@ -840,6 +838,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } done:if (ret) { +unpin_user_pages (pages, nr_pages);if (imu)io_free_imu (ctx, imu);io_cache_free (&ctx->node_cache, node);
Thank you for taking the time to address this issue!
However, if io_pin_pages() fails, it will also jump to the done label, but at that point, the value of nr_pages is undefined because nr_pages is only assigned a value inside io_pin_pages() if it succeeds.
pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
&nr_pages);
if (IS_ERR(pages)) {
ret = PTR_ERR(pages);
pages = NULL;
goto done;
}
...
done:
if (ret) {
unpin_user_pages(NULL, undefined-value);
...I'm not sure what the impact of calling unpin_user_pages() in this way would be.
Jens Axboe
maintainerQuoted 79 lines · Penglei Jiang
Quoted 49 lines · Jens Axboe
Quoted 14 lines · Penglei Jiang
Add missing
unpin_user_pages()in the error pathFixes d8c2237d0aa9 ("io_uring: add io_pin_pages() helper")Signed-off-by Penglei Jiang <[email protected]>--- io_uring/rsrc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)io_uring/rsrc.c +0
-0@@ -804,8 +804,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, }io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 813
Quoted 10 lines
io_uring/rsrc.c +3
-1imu =io_alloc_imu (ctx, nr_pages); -if (!imu) +if (!imu) { +unpin_user_pages (pages, nr_pages); goto done; + } imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage);Wouldn't it be better to have the unpin be part of the normal error handling? Not sure why the pin accounting failure doesn't do that already.
Totally untested...
io_uring/rsrc.c +1
-3@@ -809,10 +809,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage); -if (ret) { -unpin_user_pages (pages, nr_pages); +if (ret) goto done; - } size = iov->iov_len;/* store original address for later verification */ io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 844
Quoted 7 lines
io_uring/rsrc.c +1
-0@@ -840,6 +838,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } done:if (ret) { +unpin_user_pages (pages, nr_pages);if (imu)io_free_imu (ctx, imu);io_cache_free (&ctx->node_cache, node);Thank you for taking the time to address this issue!
However, if
io_pin_pages()fails, it will also jump to the done label, but at that point, the value ofnr_pagesis undefined becausenr_pagesis only assigned a value insideio_pin_pages()if it succeeds.pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len, &nr_pages); if (IS_ERR(pages)) { ret = PTR_ERR(pages); pages = NULL; goto done; } ... done: if (ret) { unpin_user_pages(NULL, undefined-value); ...
Quoted 1 line
I'm not sure what the impact of calling
unpin_user_pages()in this way would be.
We should just check for 'pages' being valid first. Updated below. If you want to test and send a v2 based on that, I do think that's the better approach as it keeps all the error handling consistent.
io_uring/rsrc.c +3 -3
@@ -809,10 +809,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, imu->nr_bvecs = nr_pages; ret =io_buffer_account_pin (ctx, pages, nr_pages, imu, last_hpage); -if (ret) { -unpin_user_pages (pages, nr_pages); +if (ret) goto done; - } size = iov->iov_len;/* store original address for later verification */
@@ -840,6 +838,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } done:if (ret) { +if (pages) +unpin_user_pages (pages, nr_pages);if (imu)io_free_imu (ctx, imu);io_cache_free (&ctx->node_cache, node);
Signature · 1 line
Jens Axboe