block: unhash blkdev part inode when the part is deleted
Subsystems (2)
PATCH v2 1/1 block: unhash blkdev part inode when the part is deleted
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/[email protected]/
So fixes it by unhashing part bdev in delete_partition(), and this way is actually aligned with v5.11+'s behavior.
---
V2:
- fix one typo and Shiwei's email format
block/partitions/core.c | 7 +++++++ 1 file changed, 7 insertions(+)
block/partitions/core.c +7 -0
@@ -329,6 +329,7 @@ void delete_partition(struct hd_struct *part)struct gendisk *disk =part_to_disk (part);struct disk_part_tbl *ptbl =rcu_dereference_protected (disk->part_tbl,1 ); +struct block_device *bdev;/* * ->part_tbl is referenced in this part's release handler, so
@@ -346,6 +347,12 @@ void delete_partition(struct hd_struct *part)* "in-use" until we really free the gendisk. */ blk_invalidate_devt (part_devt (part)); + + bdev =bdget_part (part); +if (bdev) { +remove_inode_hash (bdev->bd_inode); +bdput (bdev); + }percpu_ref_kill (&part->ref); }
Signature · 1 line
2.37.3
kernel test robot
build botFYI: kernel test robot notices the stable kernel rule is not satisfied.
Hi,
Thanks for your patch.
FYI: kernel test robot notices the stable kernel rule is not satisfied.
Rule: 'Cc: [email protected]' or 'commit <sha1> upstream.'
Subject: [PATCH 5.10.y stable v2] block: unhash blkdev part inode when the part is deleted
The check is based on https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
Greg Kroah-Hartman
maintainerQuoted 28 lines · Ming Lei
v5.11 changes the blkdev lookup mechanism completely since commit
22ae8ce8b892("block: simplify bdev/disk lookup inblkdev_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 ofBLOCK_EXT_MAJOR:1) when one partition is deleted & closed,
disk_put_part()is always called before bdput(bdev), seeblkdev_put(); so the part's devt can be freed & re-used before the inode is dropped2) 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_deviceand structhd_structv4" for addressing this issue.https://lore.kernel.org/linux-block/[email protected]/
So fixes it by unhashing part bdev in
delete_partition(), and this way is actually aligned with v5.11+'s behavior.Reported-by Shiwei Cui <[email protected]>Tested-by Shiwei Cui <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Ming Lei <[email protected]>
Quoted 5 lines
---
V2:
- fix one typo and Shiwei's email formatblock/partitions/core.c | 7 +++++++
I need an ack from the block maintainers/developers to be able to take this.
thanks,
greg k-h
Quoted 33 lines · Greg Kroah-Hartman
Quoted 32 lines · Ming Lei
v5.11 changes the blkdev lookup mechanism completely since commit
22ae8ce8b892("block: simplify bdev/disk lookup inblkdev_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 ofBLOCK_EXT_MAJOR:1) when one partition is deleted & closed,
disk_put_part()is always called before bdput(bdev), seeblkdev_put(); so the part's devt can be freed & re-used before the inode is dropped2) 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_deviceand structhd_structv4" for addressing this issue.https://lore.kernel.org/linux-block/[email protected]/
So fixes it by unhashing part bdev in
delete_partition(), and this way is actually aligned with v5.11+'s behavior.Reported-by Shiwei Cui <[email protected]>Tested-by Shiwei Cui <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Ming Lei <[email protected]>---
V2:
- fix one typo and Shiwei's email format
Quoted 4 lines
Quoted 1 line
block/partitions/core.c | 7 +++++++I need an ack from the block maintainers/developers to be able to take this.
Signature · 1 line
Jens Axboe
Greg Kroah-Hartman
maintainerQuoted 34 lines · Jens Axboe
Quoted 33 lines · Greg Kroah-Hartman
Quoted 32 lines · Ming Lei
v5.11 changes the blkdev lookup mechanism completely since commit
22ae8ce8b892("block: simplify bdev/disk lookup inblkdev_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 ofBLOCK_EXT_MAJOR:1) when one partition is deleted & closed,
disk_put_part()is always called before bdput(bdev), seeblkdev_put(); so the part's devt can be freed & re-used before the inode is dropped2) 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_deviceand structhd_structv4" for addressing this issue.https://lore.kernel.org/linux-block/[email protected]/
So fixes it by unhashing part bdev in
delete_partition(), and this way is actually aligned with v5.11+'s behavior.Reported-by Shiwei Cui <[email protected]>Tested-by Shiwei Cui <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Ming Lei <[email protected]>---
V2:
- fix one typo and Shiwei's email format
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
block/partitions/core.c | 7 +++++++I need an ack from the block maintainers/developers to be able to take this.
Acked-by Jens Axboe <[email protected]>
Thanks, now queued up for 5.10.y
greg k-h