hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8S3GW CVE: NA
---------------------------
When a partition of a block device is mounted, opening the entire block device for write operations may also damage the file system. Identify this situation.
Signed-off-by: Li Lingfeng lilingfeng3@huawei.com --- block/Kconfig | 10 ++++++++++ block/bdev.c | 43 +++++++++++++++++++++++++++++++++++++----- include/linux/blkdev.h | 2 ++ 3 files changed, 50 insertions(+), 5 deletions(-)
diff --git a/block/Kconfig b/block/Kconfig index 03b53a50939f..45044f1d2942 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -239,6 +239,16 @@ config BLK_INLINE_ENCRYPTION_FALLBACK by falling back to the kernel crypto API when inline encryption hardware is not present.
+config BLK_DEV_DETECT_WRITING_PART0 + bool "Detect writing to part0 when partitions mounted" + default n + help + When partitions of a block device are mounted, writing to part0's + buffer cache is likely going to cause filesystem corruption on + each partition. + As a supplement to BLK_DEV_WRITE_MOUNTED, enabling this + to detect the scenario above. + source "block/partitions/Kconfig"
config BLK_MQ_PCI diff --git a/block/bdev.c b/block/bdev.c index 883ce162e9fc..101bec422c0c 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -33,9 +33,12 @@ #define bdev_opt(opt) (bdev_allow_write_mounted & (1 << BLKDEV_##opt)) /* Should we allow writing to mounted block devices? */ #define BLKDEV_ALLOW_WRITE_MOUNTED 0 +/* Should we detect writing to part0 when partitions mounted */ +#define BLKDEV_DETECT_WRITING_PART0 1
static u8 bdev_allow_write_mounted = - IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED) << BLKDEV_ALLOW_WRITE_MOUNTED; + IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED) << BLKDEV_ALLOW_WRITE_MOUNTED || + IS_ENABLED(CONFIG_BLK_DEV_DETECT_WRITING_PART0) << BLKDEV_DETECT_WRITING_PART0;
struct bdev_inode { struct block_device bdev; @@ -739,22 +742,52 @@ void blkdev_put_no_open(struct block_device *bdev)
static bool bdev_writes_blocked(struct block_device *bdev) { - return bdev->bd_mounted; + if (bdev->bd_mounted) + return true; + if (bdev_opt(DETECT_WRITING_PART0)) + return bdev_is_partition(bdev) ? bdev_whole(bdev)->bd_mounted : + bdev->bd_disk->part_mounters; + + return false; }
static void bdev_block_writes(struct block_device *bdev) { bdev->bd_mounted = true; + if (bdev_is_partition(bdev)) + bdev->bd_disk->part_mounters++; }
static void bdev_unblock_writes(struct block_device *bdev) { bdev->bd_mounted = false; + if (bdev_is_partition(bdev)) + bdev->bd_disk->part_mounters--; }
static bool bdev_mount_blocked(struct block_device *bdev) { - return bdev->bd_writers > 0; + if (bdev->bd_writers) + return true; + if (bdev_opt(DETECT_WRITING_PART0)) + return bdev_is_partition(bdev) ? bdev_whole(bdev)->bd_writers : + bdev->bd_disk->part_writers; + + return false; +} + +static void bdev_block_mount(struct block_device *bdev) +{ + bdev->bd_writers++; + if (bdev_is_partition(bdev)) + bdev->bd_disk->part_writers++; +} + +static void bdev_unblock_mount(struct block_device *bdev) +{ + bdev->bd_writers--; + if (bdev_is_partition(bdev)) + bdev->bd_disk->part_writers--; }
static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) @@ -775,7 +808,7 @@ static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) if (mode & BLK_OPEN_RESTRICT_WRITES) bdev_block_writes(bdev); else if (mode & BLK_OPEN_WRITE) - bdev->bd_writers++; + bdev_block_mount(bdev); }
static void bdev_yield_write_access(struct block_device *bdev, blk_mode_t mode) @@ -784,7 +817,7 @@ static void bdev_yield_write_access(struct block_device *bdev, blk_mode_t mode) if (mode & BLK_OPEN_RESTRICT_WRITES) bdev_unblock_writes(bdev); else if (mode & BLK_OPEN_WRITE) - bdev->bd_writers--; + bdev_unblock_mount(bdev); }
/** diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 9f3bcbcb156d..162027385e1b 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -209,6 +209,8 @@ struct gendisk { * devices that do not have multiple independent access ranges. */ struct blk_independent_access_ranges *ia_ranges; + int part_mounters; + int part_writers; };
static inline bool disk_live(struct gendisk *disk)