From: Ming Lei <ming.lei@redhat.com>
To: "yukuai (C)" <yukuai3@huawei.com>
Cc: axboe@kernel.dk, josef@toxicpanda.com, bvanassche@acm.org,
linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
nbd@other.debian.org, yi.zhang@huawei.com
Subject: Re: [PATCH v2 2/2] nbd: convert to use blk_mq_get_rq_by_tag()
Date: Mon, 9 Aug 2021 17:46:58 +0800 [thread overview]
Message-ID: <YRD5krmF/C7JxchE@T590> (raw)
In-Reply-To: <47e5faa8-f8e5-86db-05a1-559e3b3c04b5@huawei.com>
On Mon, Aug 09, 2021 at 03:08:26PM +0800, yukuai (C) wrote:
> On 2021/08/09 14:28, Ming Lei wrote:
> > On Mon, Aug 09, 2021 at 11:09:27AM +0800, Yu Kuai wrote:
> > > blk_mq_tag_to_rq() might return freed request, use
> > > blk_mq_get_rq_by_tag() instead.
> > >
> > > Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> > > ---
> > > drivers/block/nbd.c | 11 ++++++-----
> > > 1 file changed, 6 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> > > index c38317979f74..9e56975a8eee 100644
> > > --- a/drivers/block/nbd.c
> > > +++ b/drivers/block/nbd.c
> > > @@ -713,11 +713,10 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
> > > tag = nbd_handle_to_tag(handle);
> > > hwq = blk_mq_unique_tag_to_hwq(tag);
> > > if (hwq < nbd->tag_set.nr_hw_queues)
> > > - req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
> > > - blk_mq_unique_tag_to_tag(tag));
> > > - if (!req || !blk_mq_request_started(req)) {
> > > - dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
> > > - tag, req);
> > > + req = blk_mq_get_rq_by_tag(nbd->tag_set.tags[hwq],
> > > + blk_mq_unique_tag_to_tag(tag));
> > > + if (!req) {
> > > + dev_err(disk_to_dev(nbd->disk), "Unexpected reply %d\n", tag);
> > > return ERR_PTR(-ENOENT);
> > > }
> > > trace_nbd_header_received(req, handle);
> > > @@ -779,6 +778,8 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
> > > }
> > > out:
> > > trace_nbd_payload_received(req, handle);
> > > + if (req)
> > > + blk_mq_put_rq_ref(req);
> > > mutex_unlock(&cmd->lock);
> > > return ret ? ERR_PTR(ret) : cmd;
> >
> > After blk_mq_put_rq_ref() returns, this request may have been freed,
> > so the returned 'cmd' may have been freed too.
> >
> > As I replied in your another thread, it is driver's responsibility to
> > cover race between normal completion and timeout/error handling, that
> > means the caller of blk_mq_tag_to_rq need to make sure that the request
> > represented by the passed 'tag' can't be freed.
>
> Hi, Ming
>
> There are two problems here in nbd, both reported by our syzkaller.
>
> The first is that blk_mq_tag_to_rq() returned a freed request, which is
> because tags->static_rq[] is freed without clearing tags->rq[].
> Syzkaller log shows that a reply package is sent to client without
> the client's request package. And this patch is trying to solve this
> problem.
It is still driver's problem:
->static_rq is freed in blk_mq_free_tag_set() which is called after
blk_cleanup_disk() returns. Once blk_cleanup_disk() returns, there
shouldn't be any driver activity, including calling blk_mq_tag_to_rq()
by passing one invalid tag.
Thanks,
Ming
next prev parent reply other threads:[~2021-08-09 9:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-09 3:09 [PATCH v2 0/2] fix request uaf in nbd_read_stat() Yu Kuai
2021-08-09 3:09 ` [PATCH v2 1/2] blk-mq: add a new interface to get request by tag Yu Kuai
2021-08-09 3:09 ` [PATCH v2 2/2] nbd: convert to use blk_mq_get_rq_by_tag() Yu Kuai
2021-08-09 6:28 ` Ming Lei
2021-08-09 7:08 ` yukuai (C)
2021-08-09 9:46 ` Ming Lei [this message]
2021-08-09 14:04 ` yukuai (C)
2021-08-10 1:48 ` Ming Lei
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=YRD5krmF/C7JxchE@T590 \
--to=ming.lei@redhat.com \
--cc=axboe@kernel.dk \
--cc=bvanassche@acm.org \
--cc=josef@toxicpanda.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nbd@other.debian.org \
--cc=yi.zhang@huawei.com \
--cc=yukuai3@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.