io-uring.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefano Garzarella <sgarzare@redhat.com>
To: Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
Cc: io-uring@vger.kernel.org, axboe@kernel.dk, joseph.qi@linux.alibaba.com
Subject: Re: [PATCH] io_uring: fix possible use after free to sqd
Date: Thu, 15 Oct 2020 12:01:42 +0200	[thread overview]
Message-ID: <20201015100142.k2uylzcwy6pu6vzw@steredhat> (raw)
In-Reply-To: <20201015091335.1667-1-xiaoguang.wang@linux.alibaba.com>

On Thu, Oct 15, 2020 at 05:13:35PM +0800, Xiaoguang Wang wrote:
> Reading codes finds a possible use after free issue to sqd:
>           thread1              |       thread2
> ==> io_attach_sq_data()        |
> ===> sqd = ctx_attach->sq_data;|
>                                | ==> io_put_sq_data()
>                                | ===> refcount_dec_and_test(&sqd->refs)
>                                |     If sqd->refs is zero, will free sqd.
>                                |
> ===> refcount_inc(&sqd->refs); |
>                                |
>                                | ====> kfree(sqd);
> ===> now use after free to sqd |
> 

IIUC the io_attach_sq_data() is called only during the setup of an
io_uring context, before that the fd is returned to the user space.

So, I'm not sure a second thread can call io_put_sq_data() while the
first thread is in io_attach_sq_data().

Can you check if this situation can really happen?

Thanks,
Stefano

> Use refcount_inc_not_zero() to fix this issue.
> 
> Signed-off-by: Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
> ---
>  fs/io_uring.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/io_uring.c b/fs/io_uring.c
> index 33b5cf18bb51..48e230feb704 100644
> --- a/fs/io_uring.c
> +++ b/fs/io_uring.c
> @@ -6868,7 +6868,11 @@ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
>  		return ERR_PTR(-EINVAL);
>  	}
>  
> -	refcount_inc(&sqd->refs);
> +	if (!refcount_inc_not_zero(&sqd->refs)) {
> +		fdput(f);
> +		return ERR_PTR(-EINVAL);
> +	}
> +
>  	fdput(f);
>  	return sqd;
>  }
> -- 
> 2.17.2
> 


  reply	other threads:[~2020-10-15 10:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-15  9:13 [PATCH] io_uring: fix possible use after free to sqd Xiaoguang Wang
2020-10-15 10:01 ` Stefano Garzarella [this message]
2020-10-15 10:46   ` Xiaoguang Wang
2020-10-15 11:07     ` Stefano Garzarella
2020-10-15 11:24       ` Xiaoguang Wang

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=20201015100142.k2uylzcwy6pu6vzw@steredhat \
    --to=sgarzare@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=joseph.qi@linux.alibaba.com \
    --cc=xiaoguang.wang@linux.alibaba.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;
as well as URLs for NNTP newsgroup(s).