From: Christoph Hellwig <hch@infradead.org>
To: Yufen Yu <yuyufen@huawei.com>
Cc: axboe@kernel.dk, linux-block@vger.kernel.org,
ming.lei@redhat.com, houtao1@huawei.com, hch@lst.de,
yi.zhang@huawei.com, zhengchuan@huawei.com
Subject: Re: [PATCH] block: cache index instead of part self to avoid use-after-free
Date: Wed, 8 Jan 2020 07:01:06 -0800 [thread overview]
Message-ID: <20200108150106.GA18991@infradead.org> (raw)
In-Reply-To: <20200106073510.10825-1-yuyufen@huawei.com>
On Mon, Jan 06, 2020 at 03:35:10PM +0800, Yufen Yu wrote:
> When delete partition executes concurrently with IOs issue,
> it may cause use-after-free on part in disk_map_sector_rcu()
> as following:
>
> blk_account_io_start(req1) delete_partition blk_account_io_start(req2)
>
> rcu_read_lock()
> disk_map_sector_rcu
> part = rcu_dereference(ptbl->part[4])
> rcu_assign_pointer(ptbl->part[4], NULL);
> rcu_assign_pointer(ptbl->last_lookup, NULL);
> rcu_assign_pointer(ptbl->last_lookup, part);
>
> hd_struct_kill(part)
> !hd_struct_try_get
> part = &rq->rq_disk->part0;
> rcu_read_unlock()
> __delete_partition
> call_rcu
> rcu_read_lock
> disk_map_sector_rcu
> part = rcu_dereference(ptbl->last_lookup);
>
> delete_partition_work_fn
> free(part)
> hd_struct_try_get(part)
> BUG_ON use-after-free
>
> req1 try to get 'ptbl->part[4]', while the part is beening
> deleted. Although the delete_partition() will set last_lookup
> as NULL, req1 can overwrite it as 'part[4]' again.
>
> After calling call_rcu() and free() for the part, req2 can
> access the part by last_lookup, resulting in use after free.
>
> In fact, this bug has been reported by syzbot:
> https://lkml.org/lkml/2019/1/4/357
>
> To fix the bug, we try to cache index of part[] instead of
> part[i] itself in last_lookup. Even if the index may been
> re-assign, others can either get part[i] as value of NULL,
> or get the new allocated part[i] after call_rcu. Both of
> them is okay.
>
> Signed-off-by: Yufen Yu <yuyufen@huawei.com>
Looks good,
Reviewed-by: Christoph Hellwig <hch@lst.de>
next prev parent reply other threads:[~2020-01-08 15:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-06 7:35 [PATCH] block: cache index instead of part self to avoid use-after-free Yufen Yu
2020-01-08 15:01 ` Christoph Hellwig [this message]
2020-01-09 1:35 ` 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=20200108150106.GA18991@infradead.org \
--to=hch@infradead.org \
--cc=axboe@kernel.dk \
--cc=hch@lst.de \
--cc=houtao1@huawei.com \
--cc=linux-block@vger.kernel.org \
--cc=ming.lei@redhat.com \
--cc=yi.zhang@huawei.com \
--cc=yuyufen@huawei.com \
--cc=zhengchuan@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox