linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH stable 4.9.y] block: unhash blkdev part inode when the part is deleted
@ 2022-12-13  7:16 Ming Lei
  2022-12-15  6:50 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 2+ messages in thread
From: Ming Lei @ 2022-12-13  7:16 UTC (permalink / raw)
  To: Greg Kroah-Hartman, stable
  Cc: Jens Axboe, linux-block, Ming Lei, Shiwei Cui, Christoph Hellwig,
	Jan Kara

v5.11 changes the blkdev lookup mechanism completely since commit
22ae8ce8b892 ("block: simplify bdev/disk lookup in blkdev_get"),
and small part of the change is to unhash part bdev inode when
deleting partition. Turns out this kind of change does fix one
nasty issue in case of BLOCK_EXT_MAJOR:

1) when one partition is deleted & closed, disk_put_part() is always
called before bdput(bdev), see blkdev_put(); so the part's devt can
be freed & re-used before the inode is dropped

2) then new partition with same devt can be created just before the
inode in 1) is dropped, then the old inode/bdev structurein 1) is
re-used for this new partition, this way causes use-after-free and
kernel panic.

It isn't possible to backport the whole big patchset of "merge struct
block_device and struct hd_struct v4" for addressing this issue.

https://lore.kernel.org/linux-block/20201128161510.347752-1-hch@lst.de/

So fixes it by unhashing part bdev in delete_partition(), and this way
is actually aligned with v5.11+'s behavior.

Backported from the following 5.10.y commit:

5f2f77560591 ("block: unhash blkdev part inode when the part is deleted")

Reported-by: Shiwei Cui <cuishw@inspur.com>
Tested-by: Shiwei Cui <cuishw@inspur.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 block/partition-generic.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/block/partition-generic.c b/block/partition-generic.c
index 298c05f8b5e3..434c122cb958 100644
--- a/block/partition-generic.c
+++ b/block/partition-generic.c
@@ -254,6 +254,7 @@ void delete_partition(struct gendisk *disk, int partno)
 {
 	struct disk_part_tbl *ptbl = disk->part_tbl;
 	struct hd_struct *part;
+	struct block_device *bdev;
 
 	if (partno >= ptbl->len)
 		return;
@@ -267,6 +268,11 @@ void delete_partition(struct gendisk *disk, int partno)
 	kobject_put(part->holder_dir);
 	device_del(part_to_dev(part));
 
+	bdev = bdget(part_devt(part));
+	if (bdev) {
+		remove_inode_hash(bdev->bd_inode);
+		bdput(bdev);
+	}
 	hd_struct_kill(part);
 }
 
-- 
2.38.1


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

* Re: [PATCH stable 4.9.y] block: unhash blkdev part inode when the part is deleted
  2022-12-13  7:16 [PATCH stable 4.9.y] block: unhash blkdev part inode when the part is deleted Ming Lei
@ 2022-12-15  6:50 ` Greg Kroah-Hartman
  0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2022-12-15  6:50 UTC (permalink / raw)
  To: Ming Lei
  Cc: stable, Jens Axboe, linux-block, Shiwei Cui, Christoph Hellwig, Jan Kara

On Tue, Dec 13, 2022 at 03:16:55PM +0800, Ming Lei wrote:
> v5.11 changes the blkdev lookup mechanism completely since commit
> 22ae8ce8b892 ("block: simplify bdev/disk lookup in blkdev_get"),
> and small part of the change is to unhash part bdev inode when
> deleting partition. Turns out this kind of change does fix one
> nasty issue in case of BLOCK_EXT_MAJOR:
> 
> 1) when one partition is deleted & closed, disk_put_part() is always
> called before bdput(bdev), see blkdev_put(); so the part's devt can
> be freed & re-used before the inode is dropped
> 
> 2) then new partition with same devt can be created just before the
> inode in 1) is dropped, then the old inode/bdev structurein 1) is
> re-used for this new partition, this way causes use-after-free and
> kernel panic.
> 
> It isn't possible to backport the whole big patchset of "merge struct
> block_device and struct hd_struct v4" for addressing this issue.
> 
> https://lore.kernel.org/linux-block/20201128161510.347752-1-hch@lst.de/
> 
> So fixes it by unhashing part bdev in delete_partition(), and this way
> is actually aligned with v5.11+'s behavior.
> 
> Backported from the following 5.10.y commit:
> 
> 5f2f77560591 ("block: unhash blkdev part inode when the part is deleted")
> 
> Reported-by: Shiwei Cui <cuishw@inspur.com>
> Tested-by: Shiwei Cui <cuishw@inspur.com>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Jan Kara <jack@suse.cz>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
>  block/partition-generic.c | 6 ++++++
>  1 file changed, 6 insertions(+)

All now queued up, thanks!

greg k-h

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

end of thread, other threads:[~2022-12-15  6:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-13  7:16 [PATCH stable 4.9.y] block: unhash blkdev part inode when the part is deleted Ming Lei
2022-12-15  6:50 ` Greg Kroah-Hartman

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).