Linux-Fsdevel Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] io_uring: fix a use after free in io_async_task_func()
@ 2020-07-08 18:47 Dan Carpenter
  2020-07-08 19:01 ` Pavel Begunkov
  2020-07-08 19:15 ` Jens Axboe
  0 siblings, 2 replies; 7+ messages in thread
From: Dan Carpenter @ 2020-07-08 18:47 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Pavel Begunkov, linux-fsdevel, io-uring, kernel-janitors

The "apoll" variable is freed and then used on the next line.  We need
to move the free down a few lines.

Fixes: 0be0b0e33b0b ("io_uring: simplify io_async_task_func()")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 fs/io_uring.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 70828e2470e2..f2993070a9e8 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -4652,12 +4652,13 @@ static void io_async_task_func(struct callback_head *cb)
 	/* restore ->work in case we need to retry again */
 	if (req->flags & REQ_F_WORK_INITIALIZED)
 		memcpy(&req->work, &apoll->work, sizeof(req->work));
-	kfree(apoll);
 
 	if (!READ_ONCE(apoll->poll.canceled))
 		__io_req_task_submit(req);
 	else
 		__io_req_task_cancel(req, -ECANCELED);
+
+	kfree(apoll);
 }
 
 static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
-- 
2.27.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] io_uring: fix a use after free in io_async_task_func()
  2020-07-08 18:47 [PATCH] io_uring: fix a use after free in io_async_task_func() Dan Carpenter
@ 2020-07-08 19:01 ` Pavel Begunkov
  2020-07-08 19:14   ` Jens Axboe
  2020-07-08 19:15 ` Jens Axboe
  1 sibling, 1 reply; 7+ messages in thread
From: Pavel Begunkov @ 2020-07-08 19:01 UTC (permalink / raw)
  To: Dan Carpenter, Jens Axboe; +Cc: linux-fsdevel, io-uring, kernel-janitors

On 08/07/2020 21:47, Dan Carpenter wrote:
> The "apoll" variable is freed and then used on the next line.  We need
> to move the free down a few lines.

My bad, thanks for spotting that!
A comment below

> 
> Fixes: 0be0b0e33b0b ("io_uring: simplify io_async_task_func()")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  fs/io_uring.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/io_uring.c b/fs/io_uring.c
> index 70828e2470e2..f2993070a9e8 100644
> --- a/fs/io_uring.c
> +++ b/fs/io_uring.c
> @@ -4652,12 +4652,13 @@ static void io_async_task_func(struct callback_head *cb)
>  	/* restore ->work in case we need to retry again */
>  	if (req->flags & REQ_F_WORK_INITIALIZED)
>  		memcpy(&req->work, &apoll->work, sizeof(req->work));
> -	kfree(apoll);

__io_req_task_submit() can take some time, e.g. waiting for a mutex, so
I'd rather free it before, but save req->poll.canceled in a local var.

>  
>  	if (!READ_ONCE(apoll->poll.canceled))
>  		__io_req_task_submit(req);
>  	else
>  		__io_req_task_cancel(req, -ECANCELED);
> +
> +	kfree(apoll);
>  }
>  
>  static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
> 

-- 
Pavel Begunkov

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] io_uring: fix a use after free in io_async_task_func()
  2020-07-08 19:01 ` Pavel Begunkov
@ 2020-07-08 19:14   ` Jens Axboe
  0 siblings, 0 replies; 7+ messages in thread
From: Jens Axboe @ 2020-07-08 19:14 UTC (permalink / raw)
  To: Pavel Begunkov, Dan Carpenter; +Cc: linux-fsdevel, io-uring, kernel-janitors

On 7/8/20 1:01 PM, Pavel Begunkov wrote:
> On 08/07/2020 21:47, Dan Carpenter wrote:
>> The "apoll" variable is freed and then used on the next line.  We need
>> to move the free down a few lines.
> 
> My bad, thanks for spotting that!
> A comment below
> 
>>
>> Fixes: 0be0b0e33b0b ("io_uring: simplify io_async_task_func()")
>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>> ---
>>  fs/io_uring.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/io_uring.c b/fs/io_uring.c
>> index 70828e2470e2..f2993070a9e8 100644
>> --- a/fs/io_uring.c
>> +++ b/fs/io_uring.c
>> @@ -4652,12 +4652,13 @@ static void io_async_task_func(struct callback_head *cb)
>>  	/* restore ->work in case we need to retry again */
>>  	if (req->flags & REQ_F_WORK_INITIALIZED)
>>  		memcpy(&req->work, &apoll->work, sizeof(req->work));
>> -	kfree(apoll);
> 
> __io_req_task_submit() can take some time, e.g. waiting for a mutex, so
> I'd rather free it before, but save req->poll.canceled in a local var.

It's 64 bytes of data, really doesn't matter. Let's just keep it simple.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] io_uring: fix a use after free in io_async_task_func()
  2020-07-08 18:47 [PATCH] io_uring: fix a use after free in io_async_task_func() Dan Carpenter
  2020-07-08 19:01 ` Pavel Begunkov
@ 2020-07-08 19:15 ` Jens Axboe
  2020-07-08 19:28   ` Pavel Begunkov
  1 sibling, 1 reply; 7+ messages in thread
From: Jens Axboe @ 2020-07-08 19:15 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: Pavel Begunkov, linux-fsdevel, io-uring, kernel-janitors

On 7/8/20 12:47 PM, Dan Carpenter wrote:
> The "apoll" variable is freed and then used on the next line.  We need
> to move the free down a few lines.

Thanks for spotting this Dan, applied.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] io_uring: fix a use after free in io_async_task_func()
  2020-07-08 19:15 ` Jens Axboe
@ 2020-07-08 19:28   ` Pavel Begunkov
  2020-07-08 19:56     ` Matthew Wilcox
  0 siblings, 1 reply; 7+ messages in thread
From: Pavel Begunkov @ 2020-07-08 19:28 UTC (permalink / raw)
  To: Jens Axboe, Dan Carpenter; +Cc: linux-fsdevel, io-uring, kernel-janitors

On 08/07/2020 22:15, Jens Axboe wrote:
> On 7/8/20 12:47 PM, Dan Carpenter wrote:
>> The "apoll" variable is freed and then used on the next line.  We need
>> to move the free down a few lines.
> 
> Thanks for spotting this Dan, applied.

I wonder why gcc can't find it... It shouldn't be hard to do after
marking free-like functions with an attribute.

Are there such tools for the kernel?

-- 
Pavel Begunkov

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] io_uring: fix a use after free in io_async_task_func()
  2020-07-08 19:28   ` Pavel Begunkov
@ 2020-07-08 19:56     ` Matthew Wilcox
  2020-07-09  9:47       ` Pavel Begunkov
  0 siblings, 1 reply; 7+ messages in thread
From: Matthew Wilcox @ 2020-07-08 19:56 UTC (permalink / raw)
  To: Pavel Begunkov
  Cc: Jens Axboe, Dan Carpenter, linux-fsdevel, io-uring, kernel-janitors

On Wed, Jul 08, 2020 at 10:28:51PM +0300, Pavel Begunkov wrote:
> On 08/07/2020 22:15, Jens Axboe wrote:
> > On 7/8/20 12:47 PM, Dan Carpenter wrote:
> >> The "apoll" variable is freed and then used on the next line.  We need
> >> to move the free down a few lines.
> > 
> > Thanks for spotting this Dan, applied.
> 
> I wonder why gcc can't find it... It shouldn't be hard to do after
> marking free-like functions with an attribute.
> 
> Are there such tools for the kernel?

GCC doesn't have an __attribute__((free)) yet.  Martin Sebor is working on
it: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87736
also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94527

(I just confirmed with him on IRC that he's still working on it; it's
part of an ongoing larger project)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] io_uring: fix a use after free in io_async_task_func()
  2020-07-08 19:56     ` Matthew Wilcox
@ 2020-07-09  9:47       ` Pavel Begunkov
  0 siblings, 0 replies; 7+ messages in thread
From: Pavel Begunkov @ 2020-07-09  9:47 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Dan Carpenter, linux-fsdevel, io-uring, kernel-janitors

On 08/07/2020 22:56, Matthew Wilcox wrote:
> On Wed, Jul 08, 2020 at 10:28:51PM +0300, Pavel Begunkov wrote:
>> On 08/07/2020 22:15, Jens Axboe wrote:
>>> On 7/8/20 12:47 PM, Dan Carpenter wrote:
>>>> The "apoll" variable is freed and then used on the next line.  We need
>>>> to move the free down a few lines.
>>>
>>> Thanks for spotting this Dan, applied.
>>
>> I wonder why gcc can't find it... It shouldn't be hard to do after
>> marking free-like functions with an attribute.
>>
>> Are there such tools for the kernel?
> 
> GCC doesn't have an __attribute__((free)) yet.  Martin Sebor is working on
> it: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87736
> also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94527
> 
> (I just confirmed with him on IRC that he's still working on it; it's
> part of an ongoing larger project)

Good to know. It looks broader indeed, hence won't probably be here too soon.

-- 
Pavel Begunkov

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-07-09  9:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-08 18:47 [PATCH] io_uring: fix a use after free in io_async_task_func() Dan Carpenter
2020-07-08 19:01 ` Pavel Begunkov
2020-07-08 19:14   ` Jens Axboe
2020-07-08 19:15 ` Jens Axboe
2020-07-08 19:28   ` Pavel Begunkov
2020-07-08 19:56     ` Matthew Wilcox
2020-07-09  9:47       ` Pavel Begunkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).