linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: yuyufen <yuyufen@huawei.com>
To: <axboe@kernel.dk>
Cc: <linux-block@vger.kernel.org>, <tj@kernel.org>, <hch@lst.de>,
	<viro@zeniv.linux.org.uk>, <bart.vanassche@wdc.com>,
	<jack@suse.cz>
Subject: Re: [PATCH 1/2] block: remove devt from ext_devt_idr when delete partition
Date: Sat, 23 Mar 2019 20:51:45 +0800	[thread overview]
Message-ID: <bb6f69c4-b8d6-32cb-3a90-40c2ad2bf0a7@huawei.com> (raw)
In-Reply-To: <20190318140703.28963-2-yuyufen@huawei.com>

This patch fix use-after-free on gendisk when open the disk partition.

Ping and Cc

Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Bart Van Assche <bart.vanassche@wdc.com>
Cc: Jan Kara <jack@suse.cz>

Yufen
Thanks

On 2019/3/18 22:07, Yufen Yu wrote:
> In part_release(), it will remove devt from ext_devt_idr and
> get_gendisk cannot find it. But, if disk_release() works before
> part_release, open device partition may cause use-after-free of
> disk in get_gendisk(). We use md device as example, the race sence:
>
> Process1	Worker			Process2
> md_free
> 					blkdev_open
> del_gendisk
>    add delete_partition_work_fn() to wq
>    					__blkdev_get
> 					get_gendisk
> put_disk
>    disk_release
>      kfree(disk)
>      					find part from ext_devt_idr
> 					get_disk_and_module(disk)
>      					  cause use after free
>
>      		delete_partition_work_fn
> 		put_device(part)
>      		  part_release
> 		    remove part from ext_devt_idr
>
> Before Woker thread removes part from ext_devt_idr, Process2 can find
> the part and access the disk, resulting use-after-free.
>
> We fix this by removing the devt from ext_devt_idr when delete partition.
>
> Signed-off-by: Yufen Yu <yuyufen@huawei.com>
> ---
>   block/partition-generic.c | 5 +++++
>   1 file changed, 5 insertions(+)
>
> diff --git a/block/partition-generic.c b/block/partition-generic.c
> index 1ee3e1d1bc2a..30d1039d5e8d 100644
> --- a/block/partition-generic.c
> +++ b/block/partition-generic.c
> @@ -288,6 +288,11 @@ void delete_partition(struct gendisk *disk, int partno)
>   	kobject_put(part->holder_dir);
>   	device_del(part_to_dev(part));
>   
> +	/*
> +	 * We should ensuere to delete part from idr before kfree(disk),
> +	 * avoiding use-after-free of disk.
> +	 */
> +	blk_free_devt(part_devt(part));
>   	hd_struct_kill(part);
>   }
>   



  reply	other threads:[~2019-03-23 12:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-18 14:07 [PATCH 0/2] fix use-after-free of gendisk Yufen Yu
2019-03-18 14:07 ` [PATCH 1/2] block: remove devt from ext_devt_idr when delete partition Yufen Yu
2019-03-23 12:51   ` yuyufen [this message]
2019-03-29 22:42     ` Jan Kara
2019-04-01  8:59       ` yuyufen
2019-03-18 14:07 ` [PATCH 2/2] block: remove unnecessary statement in blk_free_devt Yufen Yu

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=bb6f69c4-b8d6-32cb-3a90-40c2ad2bf0a7@huawei.com \
    --to=yuyufen@huawei.com \
    --cc=axboe@kernel.dk \
    --cc=bart.vanassche@wdc.com \
    --cc=hch@lst.de \
    --cc=jack@suse.cz \
    --cc=linux-block@vger.kernel.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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).