public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: Penglei Jiang <superman.xpt@gmail.com>
Cc: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] io_uring: fix page leak in io_sqe_buffer_register()
Date: Tue, 17 Jun 2025 08:29:59 -0600	[thread overview]
Message-ID: <06a466e2-0904-447e-a0d7-73ddc2da937f@kernel.dk> (raw)
In-Reply-To: <20250617140234.40664-1-superman.xpt@gmail.com>

On 6/17/25 8:02 AM, Penglei Jiang wrote:
> On Tue, 17 Jun 2025 06:53:04 -0600, Jens Axboe wrote:
>> On 6/17/25 6:39 AM, Penglei Jiang wrote:
>>> Add missing unpin_user_pages() in the error path
>>>
>>> Fixes: d8c2237d0aa9 ("io_uring: add io_pin_pages() helper")
>>> Signed-off-by: Penglei Jiang <superman.xpt@gmail.com>
>>> ---
>>>  io_uring/rsrc.c | 4 +++-
>>>  1 file changed, 3 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
>>> index c592ceace97d..f5ac1b530e21 100644
>>> --- a/io_uring/rsrc.c
>>> +++ b/io_uring/rsrc.c
>>> @@ -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);
>>
>> 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...
>>
>> diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
>> index 94a9db030e0e..a68f0cd677a3 100644
>> --- a/io_uring/rsrc.c
>> +++ b/io_uring/rsrc.c
>> @@ -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);
> 
> 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.

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.


diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 94a9db030e0e..454cd8855c6c 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -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);

-- 
Jens Axboe

      reply	other threads:[~2025-06-17 14:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-17 12:39 [PATCH] io_uring: fix page leak in io_sqe_buffer_register() Penglei Jiang
2025-06-17 12:53 ` Jens Axboe
2025-06-17 14:02   ` Penglei Jiang
2025-06-17 14:29     ` Jens Axboe [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=06a466e2-0904-447e-a0d7-73ddc2da937f@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=superman.xpt@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox