From 3f5fc698af43ee5920d4d4857e20c054651f8bda Mon Sep 17 00:00:00 2001 From: Li Lingfeng Date: Thu, 11 Jan 2024 20:24:59 +0800 Subject: [PATCH 1/6] block: Record writing and mounting regardless of whether bdev_allow_write_mounted is set hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8S3GW CVE: NA --------------------------- Introduce bd_mounted to record the state of mount. The count of bd_writers and bd_mounted can have other uses and they doesn't block writes when bdev_allow_write_mounted is not set, so remove the restriction for recording them. Signed-off-by: Li Lingfeng --- block/bdev.c | 19 +++++++++---------- include/linux/blk_types.h | 3 +++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index dd39c26c44ad..b031c91efa0d 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -735,17 +735,22 @@ void blkdev_put_no_open(struct block_device *bdev) static bool bdev_writes_blocked(struct block_device *bdev) { - return bdev->bd_writers == -1; + return bdev->bd_mounted; } static void bdev_block_writes(struct block_device *bdev) { - bdev->bd_writers = -1; + bdev->bd_mounted = true; } static void bdev_unblock_writes(struct block_device *bdev) { - bdev->bd_writers = 0; + bdev->bd_mounted = false; +} + +static bool bdev_mount_blocked(struct block_device *bdev) +{ + return bdev->bd_writers > 0; } static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) @@ -755,16 +760,13 @@ static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) /* Writes blocked? */ if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) return false; - if (mode & BLK_OPEN_RESTRICT_WRITES && bdev->bd_writers > 0) + if (mode & BLK_OPEN_RESTRICT_WRITES && bdev_mount_blocked(bdev)) return false; return true; } static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) { - if (bdev_allow_write_mounted) - return; - /* Claim exclusive or shared write access. */ if (mode & BLK_OPEN_RESTRICT_WRITES) bdev_block_writes(bdev); @@ -774,9 +776,6 @@ static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) static void bdev_yield_write_access(struct block_device *bdev, blk_mode_t mode) { - if (bdev_allow_write_mounted) - return; - /* Yield exclusive or shared write access. */ if (mode & BLK_OPEN_RESTRICT_WRITES) bdev_unblock_writes(bdev); diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 262ae789726b..f571b45eba19 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -69,6 +69,9 @@ struct block_device { #ifdef CONFIG_FAIL_MAKE_REQUEST bool bd_make_it_fail; #endif + /* State of mounting */ + bool bd_mounted; + /* The counter of write opened */ int bd_writers; /* * keep this out-of-line as it's both big and not needed in the fast -- Gitee From e7e6768eb79a1dc2c022ee7c7b1639560cb7600f Mon Sep 17 00:00:00 2001 From: Li Lingfeng Date: Thu, 11 Jan 2024 20:25:00 +0800 Subject: [PATCH 2/6] block: Expand the meaning of bdev_allow_write_mounted hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8S3GW CVE: NA --------------------------- For bdev_allow_write_mounted, there will be more subdivided scenarios and different bits of it will be used to indicate different scenarios, so expand it now. Signed-off-by: Li Lingfeng --- block/bdev.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index b031c91efa0d..883ce162e9fc 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -30,8 +30,12 @@ #include "../fs/internal.h" #include "blk.h" +#define bdev_opt(opt) (bdev_allow_write_mounted & (1 << BLKDEV_##opt)) /* Should we allow writing to mounted block devices? */ -static bool bdev_allow_write_mounted = IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED); +#define BLKDEV_ALLOW_WRITE_MOUNTED 0 + +static u8 bdev_allow_write_mounted = + IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED) << BLKDEV_ALLOW_WRITE_MOUNTED; struct bdev_inode { struct block_device bdev; @@ -755,7 +759,7 @@ static bool bdev_mount_blocked(struct block_device *bdev) static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) { - if (bdev_allow_write_mounted) + if (bdev_opt(ALLOW_WRITE_MOUNTED)) return true; /* Writes blocked? */ if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) @@ -1134,7 +1138,7 @@ void bdev_statx_dioalign(struct inode *inode, struct kstat *stat) static int __init setup_bdev_allow_write_mounted(char *str) { - if (kstrtobool(str, &bdev_allow_write_mounted)) + if (kstrtou8(str, 0, &bdev_allow_write_mounted)) pr_warn("Invalid option string for bdev_allow_write_mounted:" " '%s'\n", str); return 1; -- Gitee From 39223da6e0d9a9ba0a2f01c52a3b2c1174806004 Mon Sep 17 00:00:00 2001 From: Li Lingfeng Date: Thu, 11 Jan 2024 20:25:01 +0800 Subject: [PATCH 3/6] block: Add config option to detect writing to part0 while partitions mounted 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 --- 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 7c7187366450..42d9e8f7eb54 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -249,6 +249,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 bea0b5fdac74..993eafd83027 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) -- Gitee From ba3189767b6ab7ef31cb11bf2eeede3c003980b6 Mon Sep 17 00:00:00 2001 From: Li Lingfeng Date: Thu, 11 Jan 2024 20:25:02 +0800 Subject: [PATCH 4/6] block: Add config option to show info about opening a mounted device for write hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8S3GW CVE: NA --------------------------- When writing to a mounted block device is allowed, we need to identify which processes may write to the block device to prevent the file system from being damaged in silence. Signed-off-by: Li Lingfeng --- block/Kconfig | 7 +++++++ block/bdev.c | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/block/Kconfig b/block/Kconfig index 42d9e8f7eb54..2e70a431d417 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -259,6 +259,13 @@ config BLK_DEV_DETECT_WRITING_PART0 As a supplement to BLK_DEV_WRITE_MOUNTED, enabling this to detect the scenario above. +config BLK_DEV_WRITE_MOUNTED_DUMP + bool "Dump info when detecting conflict of opening block device" + default n + help + Enabling this to dump info when opening mounted block devices + for write or trying to mount write opened block devices. + source "block/partitions/Kconfig" config BLK_MQ_PCI diff --git a/block/bdev.c b/block/bdev.c index 101bec422c0c..afa060807d4e 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -30,15 +30,20 @@ #include "../fs/internal.h" #include "blk.h" +#define OPEN_EXCLUSIVE "VFS: Open an exclusive opened block device for write" +#define OPEN_FOR_EXCLUSIVE "VFS: Open a write opened block device exclusively" #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 +/* Should we dump info when opening mounted block devices for write? */ +#define BLKDEV_WRITE_MOUNTED_DUMP 2 static u8 bdev_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; + IS_ENABLED(CONFIG_BLK_DEV_DETECT_WRITING_PART0) << BLKDEV_DETECT_WRITING_PART0 || + IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED_DUMP) << BLKDEV_WRITE_MOUNTED_DUMP; struct bdev_inode { struct block_device bdev; @@ -740,6 +745,29 @@ void blkdev_put_no_open(struct block_device *bdev) put_device(&bdev->bd_device); } +static void blkdev_dump_conflict_opener(struct block_device *bdev, char *msg) +{ + char name[BDEVNAME_SIZE]; + struct task_struct *p = NULL; + char comm_buf[TASK_COMM_LEN]; + pid_t p_pid; + + if (!bdev_opt(WRITE_MOUNTED_DUMP)) + return; + rcu_read_lock(); + p = rcu_dereference(current->real_parent); + task_lock(p); + strncpy(comm_buf, p->comm, TASK_COMM_LEN); + p_pid = p->pid; + task_unlock(p); + rcu_read_unlock(); + + snprintf(name, sizeof(name), "%pg", bdev); + pr_info_ratelimited("%s [%s]. current [%d %s]. parent [%d %s]\n", + msg, name, + current->pid, current->comm, p_pid, comm_buf); +} + static bool bdev_writes_blocked(struct block_device *bdev) { if (bdev->bd_mounted) @@ -792,14 +820,17 @@ static void bdev_unblock_mount(struct block_device *bdev) static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) { - if (bdev_opt(ALLOW_WRITE_MOUNTED)) + if (bdev_opt(ALLOW_WRITE_MOUNTED) && !bdev_opt(WRITE_MOUNTED_DUMP)) return true; /* Writes blocked? */ if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) - return false; - if (mode & BLK_OPEN_RESTRICT_WRITES && bdev_mount_blocked(bdev)) - return false; - return true; + blkdev_dump_conflict_opener(bdev, OPEN_EXCLUSIVE); + else if (mode & BLK_OPEN_RESTRICT_WRITES && bdev_mount_blocked(bdev)) + blkdev_dump_conflict_opener(bdev, OPEN_FOR_EXCLUSIVE); + else + return true; + + return bdev_opt(ALLOW_WRITE_MOUNTED); } static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) -- Gitee From cbe734b3ae193277025ab1994a1bee685eb7ead2 Mon Sep 17 00:00:00 2001 From: Li Lingfeng Date: Thu, 11 Jan 2024 20:25:03 +0800 Subject: [PATCH 5/6] block: Show info about opening a lower device for write while upper-layers mounted hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8S3GW CVE: NA --------------------------- Filesystem on logic devices may be corrupted when writing to a block device which is used to create logic devices, so show info to indicate this risk behavior. Signed-off-by: Li Lingfeng --- block/bdev.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/block/bdev.c b/block/bdev.c index afa060807d4e..21cbfa166ebe 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -818,12 +818,21 @@ static void bdev_unblock_mount(struct block_device *bdev) bdev->bd_disk->part_writers--; } +static bool bdev_lower_device_writes_blocked(struct block_device *bdev) +{ + if (bdev_opt(DETECT_WRITING_PART0)) + return !!bdev->bd_holder; + else + return !!bdev->bd_holder && bdev->bd_holder != bd_may_claim; +} + static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) { if (bdev_opt(ALLOW_WRITE_MOUNTED) && !bdev_opt(WRITE_MOUNTED_DUMP)) return true; /* Writes blocked? */ - if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) + if (mode & BLK_OPEN_WRITE && (bdev_writes_blocked(bdev) || + bdev_lower_device_writes_blocked(bdev))) blkdev_dump_conflict_opener(bdev, OPEN_EXCLUSIVE); else if (mode & BLK_OPEN_RESTRICT_WRITES && bdev_mount_blocked(bdev)) blkdev_dump_conflict_opener(bdev, OPEN_FOR_EXCLUSIVE); -- Gitee From b3469ce0a441819f92b28d8a4820a641e36d6cf1 Mon Sep 17 00:00:00 2001 From: Li Lingfeng Date: Thu, 11 Jan 2024 20:25:04 +0800 Subject: [PATCH 6/6] add config about writing mounted devices in openeuler_defconfig hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8S3GW CVE: NA --------------------------- OpenEuler need detect conflict of opening block device, so enable it as default. Signed-off-by: Li Lingfeng --- arch/arm64/configs/openeuler_defconfig | 3 +++ arch/x86/configs/openeuler_defconfig | 3 +++ 2 files changed, 6 insertions(+) diff --git a/arch/arm64/configs/openeuler_defconfig b/arch/arm64/configs/openeuler_defconfig index f130774b720c..1f10bdfb6502 100644 --- a/arch/arm64/configs/openeuler_defconfig +++ b/arch/arm64/configs/openeuler_defconfig @@ -901,6 +901,9 @@ CONFIG_BLK_DEBUG_FS=y CONFIG_BLK_DEBUG_FS_ZONED=y # CONFIG_BLK_SED_OPAL is not set # CONFIG_BLK_INLINE_ENCRYPTION is not set +CONFIG_BLK_DEV_WRITE_MOUNTED=y +CONFIG_BLK_DEV_DETECT_WRITING_PART0=y +CONFIG_BLK_DEV_WRITE_MOUNTED_DUMP=y # # Partition Types diff --git a/arch/x86/configs/openeuler_defconfig b/arch/x86/configs/openeuler_defconfig index c9e816e54003..54f0d478f614 100644 --- a/arch/x86/configs/openeuler_defconfig +++ b/arch/x86/configs/openeuler_defconfig @@ -965,6 +965,9 @@ CONFIG_BLK_DEBUG_FS=y CONFIG_BLK_DEBUG_FS_ZONED=y # CONFIG_BLK_SED_OPAL is not set # CONFIG_BLK_INLINE_ENCRYPTION is not set +CONFIG_BLK_DEV_WRITE_MOUNTED=y +CONFIG_BLK_DEV_DETECT_WRITING_PART0=y +CONFIG_BLK_DEV_WRITE_MOUNTED_DUMP=y # # Partition Types -- Gitee