block: Add config option to not allow writing to mounted devices
Subsystems (5)
Jan Kara
reviewerv3 cover block: Add config option to not allow writing to mounted devices
Hello!
This is the third version of the patches to add config option to not allow writing to mounted block devices. The new API for block device opening has been merged so hopefully this patchset can progress towards being merged. We face some issues with necessary btrfs changes (review bandwidth) so this series is modified to enable restricting of writes for all other filesystems. Once btrfs can merge necessary device scanning changes, enabling the support for restricting writes for it is trivial.
For motivation why restricting writes to mounted block devices is interesting see patch 3/7. I've been testing the patches more extensively and I've found couple of things that get broken by disallowing writes to mounted block devices:
1) "mount -o loop" gets broken because util-linux keeps the loop device open read-write when attempting to mount it. Hopefully fixable within util-linux.
2) resize2fs online resizing gets broken because it tries to open the block device read-write only to call resizing ioctl. Trivial to fix within e2fsprogs.
3) Online e2label will break because it directly writes to the ext2/3/4 superblock while the FS is mounted to set the new label. Ext4 driver will have to implement the SETFSLABEL ioctl() and e2label will have to use it, matching what happens for online labelling of btrfs and xfs.
Likely there will be other breakage I didn't find yet but overall the breakage looks minor enough that the option might be useful. Definitely good enough for syzbot fuzzing and likely good enough for hardening of systems with more tightened security.
This patch set is based on the VFS tree as of yesterday.
Changes since v2:
* Rebased on top of current VFS tree
* Added missing conversion of bcachefs to new bdev opening API
* Added patch to drop old bdev opening API
* Dropped support for restricting writes to btrfs to avoid patch dependencies and unblock merging of the patches
Changes since v1:
* Added kernel cmdline argument to toggle whether writing to mounted block devices is allowed or not
* Fixed handling of partitions
* Limit write blocking only to devices open with explicit BLK_OPEN_BLOCK_WRITES flag
Honza
Previous versions:
Jan Kara
reviewerPATCH v3 1/7 bcachefs: Convert to bdev_open_by_path()
Convert bcachefs to use bdev_open_by_path() and pass the handle around.
--- fs/bcachefs/super-io.c | 19 ++++++++++--------- fs/bcachefs/super_types.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-)
fs/bcachefs/super-io.c +10 -9
@@ -162,8 +162,8 @@ void bch2_sb_field_delete(struct bch_sb_handle *sb,void bch2_free_super (struct bch_sb_handle *sb) {kfree (sb->bio); -if (!IS_ERR_OR_NULL (sb->bdev)) -blkdev_put (sb->bdev, sb->holder); +if (!IS_ERR_OR_NULL (sb->bdev_handle)) +bdev_release (sb->bdev_handle);kfree (sb->holder);kfree (sb->sb);
@@ -685,21 +685,22 @@ int bch2_read_super(const char *path, struct bch_opts *opts,if (!opt_get (*opts, nochanges)) sb->mode |=BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (IS_ERR (sb->bdev) && -PTR_ERR (sb->bdev) == -EACCES && + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (IS_ERR (sb->bdev_handle) && +PTR_ERR (sb->bdev_handle) == -EACCES &&opt_get (*opts, read_only)) { sb->mode &= ~BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (!IS_ERR (sb->bdev)) + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (!IS_ERR (sb->bdev_handle))opt_set (*opts, nochanges, true); } -if (IS_ERR (sb->bdev)) { - ret =PTR_ERR (sb->bdev); +if (IS_ERR (sb->bdev_handle)) { + ret =PTR_ERR (sb->bdev_handle); goto out; } + sb->bdev = sb->bdev_handle->bdev; ret =bch2_sb_realloc (sb,0 );if (ret) {
fs/bcachefs/super_types.h +1 -0
@@ -4,6 +4,7 @@struct bch_sb_handle {struct bch_sb *sb; +struct bdev_handle *bdev_handle;struct block_device *bdev;struct bio *bio;void *holder;
Signature · 1 line
2.35.3
Re: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path()
Quoted 15 lines · Jan Kara
Convert bcachefs to use
bdev_open_by_path()and pass the handle around.CC Kent Overstreet <[email protected]>CC Brian Foster <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/bcachefs/super-io.c | 19 ++++++++++--------- fs/bcachefs/super_types.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-)fs/bcachefs/super-io.c +0
-0
...
Quoted 20 lines
fs/bcachefs/super-io.c +5
-5@@ -685,21 +685,22 @@ int bch2_read_super(const char *path, struct bch_opts *opts,if (!opt_get (*opts, nochanges)) sb->mode |=BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (IS_ERR (sb->bdev) && -PTR_ERR (sb->bdev) == -EACCES && + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (IS_ERR (sb->bdev_handle) && +PTR_ERR (sb->bdev_handle) == -EACCES &&opt_get (*opts, read_only)) { sb->mode &= ~BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (!IS_ERR (sb->bdev)) + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (!IS_ERR (sb->bdev_handle))opt_set (*opts, nochanges, true); }
fs/bcachefs/super-io.c int bch2_read_super(const char *path, struct bch_opts *opts, · line 706
Quoted 10 lines
fs/bcachefs/super-io.c +3
-2-if (IS_ERR (sb->bdev)) { - ret =PTR_ERR (sb->bdev); +if (IS_ERR (sb->bdev_handle)) { + ret =PTR_ERR (sb->bdev_handle); goto out; } + sb->bdev = sb->bdev_handle->bdev; ret =bch2_sb_realloc (sb,0 );if (ret) {
Hi Jan,
This all seems reasonable to me, but should bcachefs use sb_open_mode() somewhere in here to involve use of the restrict writes flag in the first place? It looks like bcachefs sort of open codes bits of mount_bdev() so it isn't clear the flag would be used anywhere...
Brian
Quoted 15 lines
fs/bcachefs/super_types.h +1
-0@@ -4,6 +4,7 @@struct bch_sb_handle {struct bch_sb *sb; +struct bdev_handle *bdev_handle;struct block_device *bdev;struct bio *bio;void *holder;Signature · 1 line
2.35.3
Re: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path()
Quoted 50 lines · Brian Foster
Quoted 15 lines · Jan Kara
Convert bcachefs to use
bdev_open_by_path()and pass the handle around.CC Kent Overstreet <[email protected]>CC Brian Foster <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/bcachefs/super-io.c | 19 ++++++++++--------- fs/bcachefs/super_types.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-)fs/bcachefs/super-io.c +0
-0...
Quoted 20 lines
fs/bcachefs/super-io.c +5
-5@@ -685,21 +685,22 @@ int bch2_read_super(const char *path, struct bch_opts *opts,if (!opt_get (*opts, nochanges)) sb->mode |=BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (IS_ERR (sb->bdev) && -PTR_ERR (sb->bdev) == -EACCES && + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (IS_ERR (sb->bdev_handle) && +PTR_ERR (sb->bdev_handle) == -EACCES &&opt_get (*opts, read_only)) { sb->mode &= ~BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (!IS_ERR (sb->bdev)) + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (!IS_ERR (sb->bdev_handle))opt_set (*opts, nochanges, true); }fs/bcachefs/super-io.c int bch2_read_super(const char *path, struct bch_opts *opts, · line 706
Quoted 10 lines
fs/bcachefs/super-io.c +3
-2-if (IS_ERR (sb->bdev)) { - ret =PTR_ERR (sb->bdev); +if (IS_ERR (sb->bdev_handle)) { + ret =PTR_ERR (sb->bdev_handle); goto out; } + sb->bdev = sb->bdev_handle->bdev; ret =bch2_sb_realloc (sb,0 );if (ret) {Hi Jan,
Quoted 4 lines
This all seems reasonable to me, but should bcachefs use
sb_open_mode()somewhere in here to involve use of the restrict writes flag in the first place? It looks like bcachefs sort of open codes bits ofmount_bdev()so it isn't clear the flag would be used anywhere...
Yeah, but that should be a separate patch :)
Jan Kara
reviewerRe: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path()
In reply to Brian FosterHi Brian,
Quoted 45 lines · Brian Foster
Quoted 15 lines · Jan Kara
Convert bcachefs to use
bdev_open_by_path()and pass the handle around.CC Kent Overstreet <[email protected]>CC Brian Foster <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/bcachefs/super-io.c | 19 ++++++++++--------- fs/bcachefs/super_types.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-)fs/bcachefs/super-io.c +0
-0...
Quoted 28 lines
fs/bcachefs/super-io.c +8
-7@@ -685,21 +685,22 @@ int bch2_read_super(const char *path, struct bch_opts *opts,if (!opt_get (*opts, nochanges)) sb->mode |=BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (IS_ERR (sb->bdev) && -PTR_ERR (sb->bdev) == -EACCES && + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (IS_ERR (sb->bdev_handle) && +PTR_ERR (sb->bdev_handle) == -EACCES &&opt_get (*opts, read_only)) { sb->mode &= ~BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (!IS_ERR (sb->bdev)) + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (!IS_ERR (sb->bdev_handle))opt_set (*opts, nochanges, true); } -if (IS_ERR (sb->bdev)) { - ret =PTR_ERR (sb->bdev); +if (IS_ERR (sb->bdev_handle)) { + ret =PTR_ERR (sb->bdev_handle); goto out; } + sb->bdev = sb->bdev_handle->bdev;
Quoted 7 lines
fs/bcachefs/super-io.c int bch2_read_super(const char *path, struct bch_opts *opts, · line 706
Quoted 2 lines
fs/bcachefs/super-io.c +0
-0ret =bch2_sb_realloc (sb,0 );if (ret) {This all seems reasonable to me, but should bcachefs use
sb_open_mode()somewhere in here to involve use of the restrict writes flag in the first place? It looks like bcachefs sort of open codes bits ofmount_bdev()so it isn't clear the flag would be used anywhere...
Yes, so AFAICS bcachefs will not get the write restriction from the changes in the generic code. Using sb_open_mode() in bcachefs would fix that but given the 'noexcl' and 'nochanges' mount options it will not be completely seamless anyway. I guess once the generic changes are in, bcachefs can decide how exactly it wants to set the BLK_OPEN_RESTRICT_WRITES flag. Or if you already have an opinion, we can just add the patch to this series.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path()
Quoted 56 lines · Jan Kara
Hi Brian,
Quoted 45 lines · Brian Foster
Quoted 15 lines · Jan Kara
Convert bcachefs to use
bdev_open_by_path()and pass the handle around.CC Kent Overstreet <[email protected]>CC Brian Foster <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/bcachefs/super-io.c | 19 ++++++++++--------- fs/bcachefs/super_types.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-)fs/bcachefs/super-io.c +0
-0...
Quoted 28 lines
fs/bcachefs/super-io.c +8
-7@@ -685,21 +685,22 @@ int bch2_read_super(const char *path, struct bch_opts *opts,if (!opt_get (*opts, nochanges)) sb->mode |=BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (IS_ERR (sb->bdev) && -PTR_ERR (sb->bdev) == -EACCES && + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (IS_ERR (sb->bdev_handle) && +PTR_ERR (sb->bdev_handle) == -EACCES &&opt_get (*opts, read_only)) { sb->mode &= ~BLK_OPEN_WRITE ; - sb->bdev =blkdev_get_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); -if (!IS_ERR (sb->bdev)) + sb->bdev_handle =bdev_open_by_path (path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); +if (!IS_ERR (sb->bdev_handle))opt_set (*opts, nochanges, true); } -if (IS_ERR (sb->bdev)) { - ret =PTR_ERR (sb->bdev); +if (IS_ERR (sb->bdev_handle)) { + ret =PTR_ERR (sb->bdev_handle); goto out; } + sb->bdev = sb->bdev_handle->bdev;Quoted 7 lines
fs/bcachefs/super-io.c int bch2_read_super(const char *path, struct bch_opts *opts, · line 706
Quoted 2 lines
fs/bcachefs/super-io.c +0
-0ret =bch2_sb_realloc (sb,0 );if (ret) {This all seems reasonable to me, but should bcachefs use
sb_open_mode()somewhere in here to involve use of the restrict writes flag in the first place? It looks like bcachefs sort of open codes bits ofmount_bdev()so it isn't clear the flag would be used anywhere...
Quoted 7 lines
Yes, so AFAICS bcachefs will not get the write restriction from the changes in the generic code. Using
sb_open_mode()in bcachefs would fix that but given the 'noexcl' and 'nochanges' mount options it will not be completely seamless anyway. I guess once the generic changes are in, bcachefs can decide how exactly it wants to set theBLK_OPEN_RESTRICT_WRITESflag. Or if you already have an opinion, we can just add the patch to this series.
Yep, makes sense, and I agree with Kent this should be a separate patch anyways. Just wanted to make sure I wasn't missing something or otherwise it was known that bcachefs needs a bit more work to turn this on... thanks.
Brian
Quoted 5 lines
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path()
In reply to Jan KaraQuoted 6 lines · Jan Kara
Convert bcachefs to use
bdev_open_by_path()and pass the handle around.CC Kent Overstreet <[email protected]>CC Brian Foster <[email protected]>Signed-off-by Jan Kara <[email protected]>
Re: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path()
In reply to Jan KaraQuoted 3 lines · Jan Kara
Convert bcachefs to use
bdev_open_by_path()and pass the handle around.
Applied to the vfs.super branch of the vfs/vfs.git tree. Patches in the vfs.super branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.super
[1/7] bcachefs: Convert to bdev_open_by_path()
https://git.kernel.org/vfs/vfs/c/8e897399352c [2/7] block: Remove blkdev_get_by_*() functions https://git.kernel.org/vfs/vfs/c/1dc2789bf2d9 [3/7] block: Add config option to not allow writing to mounted devices https://git.kernel.org/vfs/vfs/c/708e8ecda49e [4/7] btrfs: Do not restrict writes to btrfs devices https://git.kernel.org/vfs/vfs/c/b6b2f4843264 [5/7] fs: Block writes to mounted block devices https://git.kernel.org/vfs/vfs/c/48ce483465bb [6/7] xfs: Block writes to log device https://git.kernel.org/vfs/vfs/c/dae1e956882c [7/7] ext4: Block writes to journal device https://git.kernel.org/vfs/vfs/c/a8a97da12393
Jan Kara
reviewerPATCH v3 2/7 block: Remove blkdev_get_by_*() functions
In reply to Jan Karablkdev_get_by_*() and blkdev_put() functions are now unused. Remove them.
--- block/bdev.c | 94 ++++++++++++++---------------------------- include/linux/blkdev.h | 5 --- 2 files changed, 30 insertions(+), 69 deletions(-)
block/bdev.c +30 -64
@@ -732,7 +732,7 @@ void blkdev_put_no_open(struct block_device *bdev) }/** -* blkdev_get_by_dev - open a block device by device number +* bdev_open_by_dev - open a block device by device number * @dev: device number of block device to open * @mode: open mode (BLK_OPEN_*) * @holder: exclusive holder identifier
@@ -744,32 +744,40 @@ void blkdev_put_no_open(struct block_device *bdev)* * Use this interface ONLY if you really do not have anything better - i.e. when * you are behind a truly sucky interface and all you are given is a device -* number. Everything else should use blkdev_get_by_path(). +* number. Everything else should use bdev_open_by_path(). * * CONTEXT: * Might sleep. * * RETURNS: -* Reference to the block_device on success, ERR_PTR(-errno) on failure. +* Handle with a reference to the block_device on success, ERR_PTR(-errno) on +* failure. */ -struct block_device *blkdev_get_by_dev (dev_t dev,blk_mode_t mode,void *holder, -const struct blk_holder_ops *hops) +struct bdev_handle *bdev_open_by_dev (dev_t dev,blk_mode_t mode,void *holder, +const struct blk_holder_ops *hops) { -bool unblock_events = true; +struct bdev_handle *handle =kmalloc (sizeof (struct bdev_handle ), +GFP_KERNEL );struct block_device *bdev; +bool unblock_events = true;struct gendisk *disk;int ret; +if (!handle) +return ERR_PTR (-ENOMEM ); + ret =devcgroup_check_permission (DEVCG_DEV_BLOCK ,MAJOR (dev),MINOR (dev), ((mode &BLK_OPEN_READ ) ?DEVCG_ACC_READ :0 ) | ((mode &BLK_OPEN_WRITE ) ?DEVCG_ACC_WRITE :0 ));if (ret) -return ERR_PTR (ret); + goto free_handle; bdev =blkdev_get_no_open (dev); -if (!bdev) -return ERR_PTR (-ENXIO ); +if (!bdev) { + ret = -ENXIO ; + goto free_handle; + } disk = bdev->bd_disk;if (holder) {
@@ -818,7 +826,10 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,if (unblock_events)disk_unblock_events (disk); -return bdev; + handle->bdev = bdev; + handle->holder = holder; + handle->mode = mode; +return handle; put_module:module_put (disk->fops->owner); abort_claiming:
@@ -828,34 +839,14 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,disk_unblock_events (disk); put_blkdev:blkdev_put_no_open (bdev); +free_handle: +kfree (handle);return ERR_PTR (ret); } -EXPORT_SYMBOL (blkdev_get_by_dev); - -struct bdev_handle *bdev_open_by_dev (dev_t dev,blk_mode_t mode,void *holder, -const struct blk_holder_ops *hops) -{ -struct bdev_handle *handle =kmalloc (sizeof (*handle),GFP_KERNEL ); -struct block_device *bdev; - -if (!handle) -return ERR_PTR (-ENOMEM ); - bdev =blkdev_get_by_dev (dev, mode, holder, hops); -if (IS_ERR (bdev)) { -kfree (handle); -return ERR_CAST (bdev); - } - handle->bdev = bdev; - handle->holder = holder; -if (holder) - mode |=BLK_OPEN_EXCL ; - handle->mode = mode; -return handle; -}EXPORT_SYMBOL (bdev_open_by_dev);/** -* blkdev_get_by_path - open a block device by name +* bdev_open_by_path - open a block device by name * @path: path to the block device to open * @mode: open mode (BLK_OPEN_*) * @holder: exclusive holder identifier
@@ -869,29 +860,9 @@ EXPORT_SYMBOL(bdev_open_by_dev);* Might sleep. * * RETURNS: -* Reference to the block_device on success, ERR_PTR(-errno) on failure. +* Handle with a reference to the block_device on success, ERR_PTR(-errno) on +* failure. */ -struct block_device *blkdev_get_by_path (const char *path,blk_mode_t mode, -void *holder,const struct blk_holder_ops *hops) -{ -struct block_device *bdev; -dev_t dev; -int error; - - error =lookup_bdev (path, &dev); -if (error) -return ERR_PTR (error); - - bdev =blkdev_get_by_dev (dev, mode, holder, hops); -if (!IS_ERR (bdev) && (mode &BLK_OPEN_WRITE ) &&bdev_read_only (bdev)) { -blkdev_put (bdev, holder); -return ERR_PTR (-EACCES ); - } - -return bdev; -} -EXPORT_SYMBOL (blkdev_get_by_path); -struct bdev_handle *bdev_open_by_path (const char *path,blk_mode_t mode,void *holder,const struct blk_holder_ops *hops) {
@@ -914,8 +885,9 @@ struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, }EXPORT_SYMBOL (bdev_open_by_path); -void blkdev_put (struct block_device *bdev,void *holder) +void bdev_release (struct bdev_handle *handle) { +struct block_device *bdev = handle->bdev;struct gendisk *disk = bdev->bd_disk;/*
@@ -929,8 +901,8 @@ void blkdev_put(struct block_device *bdev, void *holder)sync_blockdev (bdev);mutex_lock (&disk->open_mutex); -if (holder) -bd_end_claim (bdev, holder); +if (handle->holder) +bd_end_claim (bdev, handle->holder);/* * Trigger event checking and tell drivers to flush MEDIA_CHANGE
@@ -947,12 +919,6 @@ void blkdev_put(struct block_device *bdev, void *holder)module_put (disk->fops->owner);blkdev_put_no_open (bdev); -} -EXPORT_SYMBOL (blkdev_put); - -void bdev_release (struct bdev_handle *handle) -{ -blkdev_put (handle->bdev, handle->holder);kfree (handle); }EXPORT_SYMBOL (bdev_release);
include/linux/blkdev.h +0 -5
@@ -1500,10 +1500,6 @@ struct bdev_handle {blk_mode_t mode; }; -struct block_device *blkdev_get_by_dev (dev_t dev,blk_mode_t mode,void *holder, -const struct blk_holder_ops *hops); -struct block_device *blkdev_get_by_path (const char *path,blk_mode_t mode, -void *holder,const struct blk_holder_ops *hops);struct bdev_handle *bdev_open_by_dev (dev_t dev,blk_mode_t mode,void *holder,const struct blk_holder_ops *hops); struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode,
@@ -1511,7 +1507,6 @@ struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode,int bd_prepare_to_claim (struct block_device *bdev,void *holder,const struct blk_holder_ops *hops);void bd_abort_claiming (struct block_device *bdev,void *holder); -void blkdev_put (struct block_device *bdev,void *holder);void bdev_release (struct bdev_handle *handle);/* just for blk-cgroup, don't use elsewhere */
Signature · 1 line
2.35.3
Re: [PATCH 2/7] block: Remove blkdev_get_by_*() functions
Quoted 6 lines · Jan Kara
blkdev_get_by_*() and
blkdev_put()functions are now unused. Remove them.Signed-off-by Jan Kara <[email protected]>---
Yes, very nice!
Jan Kara
reviewerPATCH v3 3/7 block: Add config option to not allow writing to mounted devices
In reply to Jan KaraWriting to mounted devices is dangerous and can lead to filesystem corruption as well as crashes. Furthermore syzbot comes with more and more involved examples how to corrupt block device under a mounted filesystem leading to kernel crashes and reports we can do nothing about. Add tracking of writers to each block device and a kernel cmdline argument which controls whether other writeable opens to block devices open with BLK_OPEN_RESTRICT_WRITES flag are allowed. We will make filesystems use this flag for used devices.
Note that this effectively only prevents modification of the particular block device's page cache by other writers. The actual device content can still be modified by other means - e.g. by issuing direct scsi commands, by doing writes through devices lower in the storage stack (e.g. in case loop devices, DM, or MD are involved) etc. But blocking direct modifications of the block device page cache is enough to give filesystems a chance to perform data validation when loading data from the underlying storage and thus prevent kernel crashes.
Syzbot can use this cmdline argument option to avoid uninteresting crashes. Also users whose userspace setup does not need writing to mounted block devices can set this option for hardening.
--- block/Kconfig | 20 +++++++++++++ block/bdev.c | 62 ++++++++++++++++++++++++++++++++++++++- include/linux/blk_types.h | 1 + include/linux/blkdev.h | 2 ++ 4 files changed, 84 insertions(+), 1 deletion(-)
block/Kconfig +20 -0
@@ -78,6 +78,26 @@ config BLK_DEV_INTEGRITY_T10 select CRC_T10DIF select CRC64_ROCKSOFT +config BLK_DEV_WRITE_MOUNTED + bool "Allow writing to mounted block devices" + default y + help + When a block device is mounted, writing to its buffer cache is very + likely going to cause filesystem corruption. It is also rather easy to + crash the kernel in this way since the filesystem has no practical way + of detecting these writes to buffer cache and verifying its metadata + integrity. However there are some setups that need this capability + like running fsck on read-only mounted root device, modifying some + features on mounted ext4 filesystem, and similar. If you say N, the + kernel will prevent processes from writing to block devices that are + mounted by filesystems which provides some more protection from runaway + privileged processes and generally makes it much harder to crash + filesystem drivers. Note however that this does not prevent + underlying device(s) from being modified by other means, e.g. by + directly submitting SCSI commands or through access to lower layers of + storage stack. If in doubt, say Y. The configuration can be overridden + with the bdev_allow_write_mounted boot option. + config BLK_DEV_ZONED bool "Zoned block device support" select MQ_IOSCHED_DEADLINE
block/bdev.c +61 -1
101 lines not on this page · raw message
include/linux/blk_types.h +1 -0
@@ -66,6 +66,7 @@ struct block_device {#ifdef CONFIG_FAIL_MAKE_REQUEST bool bd_make_it_fail;#endif +int bd_writers;/* * keep this out-of-line as it's both big and not needed in the fast * path
include/linux/blkdev.h +2 -0
@@ -124,6 +124,8 @@ typedef unsigned int __bitwise blk_mode_t;#define BLK_OPEN_NDELAY ((__force blk_mode_t)(1 << 3))/* open for "writes" only for ioctls (specialy hack for floppy.c) */ #define BLK_OPEN_WRITE_IOCTL ((__force blk_mode_t)(1 << 4)) +/* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */ +#define BLK_OPEN_RESTRICT_WRITES ((__force blk_mode_t)(1 << 5))struct gendisk {/*
Signature · 1 line
2.35.3
Christian Brauner
maintainerQuoted 18 lines · Jan Kara
Writing to mounted devices is dangerous and can lead to filesystem corruption as well as crashes. Furthermore syzbot comes with more and more involved examples how to corrupt block device under a mounted filesystem leading to kernel crashes and reports we can do nothing about. Add tracking of writers to each block device and a kernel cmdline argument which controls whether other writeable opens to block devices open with
BLK_OPEN_RESTRICT_WRITESflag are allowed. We will make filesystems use this flag for used devices.Note that this effectively only prevents modification of the particular block device's page cache by other writers. The actual device content can still be modified by other means - e.g. by issuing direct scsi commands, by doing writes through devices lower in the storage stack (e.g. in case loop devices, DM, or MD are involved) etc. But blocking direct modifications of the block device page cache is enough to give filesystems a chance to perform data validation when loading data from the underlying storage and thus prevent kernel crashes.
Quoted 7 lines
Syzbot can use this cmdline argument option to avoid uninteresting crashes. Also users whose userspace setup does not need writing to mounted block devices can set this option for hardening.
Signed-off-by Jan Kara <[email protected]>---
A few minor tweaks I would do in-tree. Please see below. I know it's mostly stylistic that's why I would do it so there's no resend dance for non-technical reasons.
Quoted 11 lines
block/Kconfig | 20 +++++++++++++ block/bdev.c | 62 ++++++++++++++++++++++++++++++++++++++- include/linux/blk_types.h | 1 + include/linux/blkdev.h | 2 ++ 4 files changed, 84 insertions(+), 1 deletion(-)block/Kconfig +0
-0
block/Kconfig config BLK_DEV_INTEGRITY_T10 · line 83
Quoted 6 lines
block/Kconfig +3
-0@@ -78,6 +78,26 @@ config BLK_DEV_INTEGRITY_T10 select CRC_T10DIF select CRC64_ROCKSOFT +config BLK_DEV_WRITE_MOUNTED + bool "Allow writing to mounted block devices" + default y
Let's hope that this can become the default one day.
block/Kconfig config BLK_DEV_INTEGRITY_T10 · line 89
Quoted 6 lines
block/Kconfig +6
-0+static void bdev_unblock_writes(struct block_device *bdev) +{ + bdev->bd_writers = 0; +} + +static bool blkdev_open_compatible(struct block_device *bdev, blk_mode_t mode)
I would like to mirror our may_{open,create}() routines here and call this:
bdev_may_open()
This is a well-known vfs pattern and also easy to understand for block devs as well.
Quoted 9 lines
block/Kconfig +3
-0@@ -800,12 +834,21 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, goto abort_claiming; if (!try_module_get(disk->fops->owner)) goto abort_claiming; + ret = -EBUSY; + if (!blkdev_open_compatible(bdev, mode)) + goto abort_claiming; if (bdev_is_partition(bdev)) ret = blkdev_get_part(bdev, mode);
block/Kconfig struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, · line 851
Quoted 10 lines
block/Kconfig +6
-0else ret = blkdev_get_whole(bdev, mode); if (ret) goto put_module; + if (!bdev_allow_write_mounted) { + if (mode & BLK_OPEN_RESTRICT_WRITES) + bdev_block_writes(bdev); + else if (mode & BLK_OPEN_WRITE) + bdev->bd_writers++; + }
I would like to move this to a tiny helper for clarity:
static void bdev_claim_write_access(struct block_device *bdev)
{
if (!bdev_allow_write_mounted)
return;
/* Claim exclusive or shared write access to the block device. */
if (mode & BLK_OPEN_RESTRICT_WRITES)
bdev_block_writes(bdev);
else if (mode & BLK_OPEN_WRITE)
bdev->bd_writers++;
}Quoted 4 lines
block/Kconfig +0
-0if (holder) { bd_finish_claiming(bdev, holder, hops);
block/Kconfig void bdev_release(struct bdev_handle *handle) · line 953
Quoted 10 lines
block/Kconfig +7
-0@@ -901,6 +944,14 @@ void bdev_release(struct bdev_handle *handle) sync_blockdev(bdev); mutex_lock(&disk->open_mutex); + if (!bdev_allow_write_mounted) { + /* The exclusive opener was blocking writes? Unblock them. */ + if (handle->mode & BLK_OPEN_RESTRICT_WRITES) + bdev_unblock_writes(bdev); + else if (handle->mode & BLK_OPEN_WRITE) + bdev->bd_writers--; + }
static void bdev_yield_write_access(struct block_device *bdev)
{
if (!bdev_allow_write_mounted)
return;
/* Yield exclusive or shared write access. */
if (handle->mode & BLK_OPEN_RESTRICT_WRITES)
bdev_unblock_writes(bdev);
else if (handle->mode & BLK_OPEN_WRITE)
bdev->bd_writers--;
}Jan Kara
reviewerQuoted 23 lines · Christian Brauner
Quoted 22 lines · Jan Kara
Writing to mounted devices is dangerous and can lead to filesystem corruption as well as crashes. Furthermore syzbot comes with more and more involved examples how to corrupt block device under a mounted filesystem leading to kernel crashes and reports we can do nothing about. Add tracking of writers to each block device and a kernel cmdline argument which controls whether other writeable opens to block devices open with
BLK_OPEN_RESTRICT_WRITESflag are allowed. We will make filesystems use this flag for used devices.Note that this effectively only prevents modification of the particular block device's page cache by other writers. The actual device content can still be modified by other means - e.g. by issuing direct scsi commands, by doing writes through devices lower in the storage stack (e.g. in case loop devices, DM, or MD are involved) etc. But blocking direct modifications of the block device page cache is enough to give filesystems a chance to perform data validation when loading data from the underlying storage and thus prevent kernel crashes.
Syzbot can use this cmdline argument option to avoid uninteresting crashes. Also users whose userspace setup does not need writing to mounted block devices can set this option for hardening.
Quoted 7 lines
Quoted 3 lines
Signed-off-by Jan Kara <[email protected]>---
A few minor tweaks I would do in-tree. Please see below. I know it's mostly stylistic that's why I would do it so there's no resend dance for non-technical reasons.
Whatever works best for you. I agree with the changes but please see my comments below for some fixes.
Quoted 14 lines
Quoted 14 lines
block/Kconfig | 20 +++++++++++++ block/bdev.c | 62 ++++++++++++++++++++++++++++++++++++++- include/linux/blk_types.h | 1 + include/linux/blkdev.h | 2 ++ 4 files changed, 84 insertions(+), 1 deletion(-)block/Kconfig +0
-0@@ -78,6 +78,26 @@ config BLK_DEV_INTEGRITY_T10 select CRC_T10DIF select CRC64_ROCKSOFT
Quoted 5 lines
block/Kconfig config BLK_DEV_INTEGRITY_T10 · line 83
Quoted 3 lines
block/Kconfig +3
-0+config BLK_DEV_WRITE_MOUNTED + bool "Allow writing to mounted block devices" + default yLet's hope that this can become the default one day.
Yes, I'd hope as well but we need some tooling work (util-linux, e2fsprogs) before that can happen.
Quoted 10 lines
block/Kconfig config BLK_DEV_INTEGRITY_T10 · line 89
Quoted 6 lines
block/Kconfig +6
-0+static void bdev_unblock_writes(struct block_device *bdev) +{ + bdev->bd_writers = 0; +} + +static bool blkdev_open_compatible(struct block_device *bdev, blk_mode_t mode)I would like to mirror our may_{open,create}() routines here and call this:
Quoted 4 lines
bdev_may_open()This is a well-known vfs pattern and also easy to understand for block devs as well.
Sure.
Quoted 22 lines
Quoted 9 lines
block/Kconfig +3
-0@@ -800,12 +834,21 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, goto abort_claiming; if (!try_module_get(disk->fops->owner)) goto abort_claiming; + ret = -EBUSY; + if (!blkdev_open_compatible(bdev, mode)) + goto abort_claiming; if (bdev_is_partition(bdev)) ret = blkdev_get_part(bdev, mode);block/Kconfig struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, · line 851
Quoted 10 lines
block/Kconfig +6
-0else ret = blkdev_get_whole(bdev, mode); if (ret) goto put_module; + if (!bdev_allow_write_mounted) { + if (mode & BLK_OPEN_RESTRICT_WRITES) + bdev_block_writes(bdev); + else if (mode & BLK_OPEN_WRITE) + bdev->bd_writers++; + }I would like to move this to a tiny helper for clarity:
Quoted 3 lines
static void
bdev_claim_write_access(struct block_device *bdev){
if (!bdev_allow_write_mounted)
This should be the other way around.
Quoted 15 lines
return; /* Claim exclusive or shared write access to the block device. */ if (mode & BLK_OPEN_RESTRICT_WRITES) bdev_block_writes(bdev); else if (mode & BLK_OPEN_WRITE) bdev->bd_writers++; }Quoted 6 lines
block/Kconfig +0
-0if (holder) { bd_finish_claiming(bdev, holder, hops);@@ -901,6 +944,14 @@ void bdev_release(struct bdev_handle *handle) sync_blockdev(bdev);
Quoted 12 lines
block/Kconfig void bdev_release(struct bdev_handle *handle) · line 953
Quoted 8 lines
block/Kconfig +7
-0mutex_lock(&disk->open_mutex); + if (!bdev_allow_write_mounted) { + /* The exclusive opener was blocking writes? Unblock them. */ + if (handle->mode & BLK_OPEN_RESTRICT_WRITES) + bdev_unblock_writes(bdev); + else if (handle->mode & BLK_OPEN_WRITE) + bdev->bd_writers--; + }static void
bdev_yield_write_access(struct block_device *bdev){
if (!bdev_allow_write_mounted)
And this as well.
Quoted 8 lines
return;
/* Yield exclusive or shared write access. */
if (handle->mode & BLK_OPEN_RESTRICT_WRITES)
bdev_unblock_writes(bdev);
else if (handle->mode & BLK_OPEN_WRITE)
bdev->bd_writers--;
}Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 4 lines
Quoted 1 line
Let's hope that this can become the default one day.
Yes, I'd hope as well but we need some tooling work (util-linux, e2fsprogs) before that can happen.
Yeah, absolutely. I'm moderately confident we'll have fairly quick adoption of this though.
Quoted 129 lines · Jan Kara
Writing to mounted devices is dangerous and can lead to filesystem corruption as well as crashes. Furthermore syzbot comes with more and more involved examples how to corrupt block device under a mounted filesystem leading to kernel crashes and reports we can do nothing about. Add tracking of writers to each block device and a kernel cmdline argument which controls whether other writeable opens to block devices open with
BLK_OPEN_RESTRICT_WRITESflag are allowed. We will make filesystems use this flag for used devices.Note that this effectively only prevents modification of the particular block device's page cache by other writers. The actual device content can still be modified by other means - e.g. by issuing direct scsi commands, by doing writes through devices lower in the storage stack (e.g. in case loop devices, DM, or MD are involved) etc. But blocking direct modifications of the block device page cache is enough to give filesystems a chance to perform data validation when loading data from the underlying storage and thus prevent kernel crashes.
Syzbot can use this cmdline argument option to avoid uninteresting crashes. Also users whose userspace setup does not need writing to mounted block devices can set this option for hardening.
Signed-off-by Jan Kara <[email protected]>--- block/Kconfig | 20 +++++++++++++ block/bdev.c | 62 ++++++++++++++++++++++++++++++++++++++- include/linux/blk_types.h | 1 + include/linux/blkdev.h | 2 ++ 4 files changed, 84 insertions(+), 1 deletion(-)block/Kconfig +20
-0@@ -78,6 +78,26 @@ config BLK_DEV_INTEGRITY_T10 select CRC_T10DIF select CRC64_ROCKSOFT +config BLK_DEV_WRITE_MOUNTED + bool "Allow writing to mounted block devices" + default y + help + When a block device is mounted, writing to its buffer cache is very + likely going to cause filesystem corruption. It is also rather easy to + crash the kernel in this way since the filesystem has no practical way + of detecting these writes to buffer cache and verifying its metadata + integrity. However there are some setups that need this capability + like running fsck on read-only mounted root device, modifying some + features on mounted ext4 filesystem, and similar. If you say N, the + kernel will prevent processes from writing to block devices that are + mounted by filesystems which provides some more protection from runaway + privileged processes and generally makes it much harder to crash + filesystem drivers. Note however that this does not prevent + underlying device(s) from being modified by other means, e.g. by + directly submitting SCSI commands or through access to lower layers of + storage stack. If in doubt, say Y. The configuration can be overridden + with the bdev_allow_write_mounted boot option. + config BLK_DEV_ZONED bool "Zoned block device support" select MQ_IOSCHED_DEADLINEblock/bdev.c +37
-1@@ -30,6 +30,9 @@#include "../fs/internal.h" #include "blk.h" +/* Should we allow writing to mounted block devices? */ +static bool bdev_allow_write_mounted =IS_ENABLED (CONFIG_BLK_DEV_WRITE_MOUNTED ); +struct bdev_inode {struct block_device bdev;struct inode vfs_inode;@@ -730,7 +733,34 @@ void blkdev_put_no_open(struct block_device *bdev) {put_device (&bdev->bd_device); } - + +static bool bdev_writes_blocked (struct block_device *bdev) +{ +return bdev->bd_writers ==-1 ; +} + +static void bdev_block_writes (struct block_device *bdev) +{ + bdev->bd_writers =-1 ; +} + +static void bdev_unblock_writes (struct block_device *bdev) +{ + bdev->bd_writers =0 ; +} + +static bool blkdev_open_compatible (struct block_device *bdev,blk_mode_t mode) +{ +if (!bdev_allow_write_mounted) { +/* Writes blocked? */ +if (mode &BLK_OPEN_WRITE &&bdev_writes_blocked (bdev)) +return false; +if (mode &BLK_OPEN_RESTRICT_WRITES && bdev->bd_writers >0 ) +return false; + } +return true; +} +/** * bdev_open_by_dev - open a block device by device number * @dev: device number of block device to open @@ -773,6 +803,10 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,if (ret) goto free_handle; +/* Blocking writes requires exclusive opener */ +if (mode &BLK_OPEN_RESTRICT_WRITES && !holder) +return ERR_PTR (-EINVAL ); + bdev =blkdev_get_no_open (dev);if (!bdev) { ret = -ENXIO ;@@ -800,12 +834,21 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, goto abort_claiming;if (!try_module_get (disk->fops->owner)) goto abort_claiming; + ret = -EBUSY ; +if (!blkdev_open_compatible (bdev, mode))
block/bdev.c struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, · line 848
Quoted 10 lines
block/bdev.c +4
-0+ goto abort_claiming;if (bdev_is_partition (bdev)) ret =blkdev_get_part (bdev, mode);else ret =blkdev_get_whole (bdev, mode);if (ret) goto put_module; +if (!bdev_allow_write_mounted) { +if (mode &BLK_OPEN_RESTRICT_WRITES ) +bdev_block_writes (bdev);
Hi, Jan
When a partition device is mounted, I think maybe it's better to block writes on the whole device at same time.
Allowing the whole device to be opened for writing when mounting a partition device, did you have any special considerations before?
Thanks.
Quoted 59 lines
block/bdev.c +20
-0+else if (mode &BLK_OPEN_WRITE ) + bdev->bd_writers++; + }if (holder) {bd_finish_claiming (bdev, holder, hops);@@ -901,6 +944,14 @@ void bdev_release(struct bdev_handle *handle)sync_blockdev (bdev);mutex_lock (&disk->open_mutex); +if (!bdev_allow_write_mounted) { +/* The exclusive opener was blocking writes? Unblock them. */ +if (handle->mode &BLK_OPEN_RESTRICT_WRITES ) +bdev_unblock_writes (bdev); +else if (handle->mode &BLK_OPEN_WRITE ) + bdev->bd_writers--; + } +if (handle->holder)bd_end_claim (bdev, handle->holder);@@ -1069,3 +1120,12 @@ void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)blkdev_put_no_open (bdev); } + +static int __init setup_bdev_allow_write_mounted (char *str) +{ +if (kstrtobool (str, &bdev_allow_write_mounted)) +pr_warn ("Invalid option string for bdev_allow_write_mounted:" +" '%s'\n" , str); +return 1 ; +} +__setup ("bdev_allow_write_mounted=" , setup_bdev_allow_write_mounted);include/linux/blk_types.h +1
-0@@ -66,6 +66,7 @@ struct block_device {#ifdef CONFIG_FAIL_MAKE_REQUEST bool bd_make_it_fail;#endif +int bd_writers;/* * keep this out-of-line as it's both big and not needed in the fast * path include/linux/blkdev.h +2
-0@@ -124,6 +124,8 @@ typedef unsigned int __bitwise blk_mode_t;#define BLK_OPEN_NDELAY ((__force blk_mode_t)(1 << 3))/* open for "writes" only for ioctls (specialy hack for floppy.c) */ #define BLK_OPEN_WRITE_IOCTL ((__force blk_mode_t)(1 << 4)) +/* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */ +#define BLK_OPEN_RESTRICT_WRITES ((__force blk_mode_t)(1 << 5))struct gendisk {/*
Jan Kara
reviewerQuoted 30 lines · Li Lingfeng
Quoted 16 lines
block/bdev.c +6
-0@@ -773,6 +803,10 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,if (ret) goto free_handle; +/* Blocking writes requires exclusive opener */ +if (mode &BLK_OPEN_RESTRICT_WRITES && !holder) +return ERR_PTR (-EINVAL ); + bdev =blkdev_get_no_open (dev);if (!bdev) { ret = -ENXIO ;@@ -800,12 +834,21 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, goto abort_claiming;if (!try_module_get (disk->fops->owner)) goto abort_claiming; + ret = -EBUSY ; +if (!blkdev_open_compatible (bdev, mode))block/bdev.c struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, · line 848
Quoted 10 lines
block/bdev.c +4
-0+ goto abort_claiming;if (bdev_is_partition (bdev)) ret =blkdev_get_part (bdev, mode);else ret =blkdev_get_whole (bdev, mode);if (ret) goto put_module; +if (!bdev_allow_write_mounted) { +if (mode &BLK_OPEN_RESTRICT_WRITES ) +bdev_block_writes (bdev);When a partition device is mounted, I think maybe it's better to block writes on the whole device at same time.
Quoted 2 lines
Allowing the whole device to be opened for writing when mounting a partition device, did you have any special considerations before?
Yes, we were considering this. But the truth is that:
a) It is *very* hard to stop all the possibilities of corrupting data on the device - e.g. with device mapper / loop device / malicious partition table you can construct many block devices pointing to the same storage, you can use e.g. SG_IO to corrupt on disk data etc. So special-casing partitions is providing little additional benefit.
b) It is difficult to then correctly handle valid cases of multiple writeably mounted partitions on the same device - you'd need to track used block numbers for each device which gets difficult in presence of device mapper etc.
c) To stop filesystem crashes, it is enough to stop modifications of buffer cache of that one block device. Because filesystems have to validate data they are loading into buffer cache anyway to handle faulty device, fs corruption etc.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerPATCH v3 4/7 btrfs: Do not restrict writes to btrfs devices
In reply to Jan KaraBtrfs device probing code needs adaptation so that it works when writes are restricted to its mounted devices. Since btrfs maintainer wants to merge these changes through btrfs tree and there are review bandwidth issues with that, let's not block all other filesystems and just not restrict writes to btrfs devices for now.
--- fs/btrfs/super.c | 2 ++ 1 file changed, 2 insertions(+)
fs/btrfs/super.c +2 -0
@@ -1403,6 +1403,8 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,return ERR_PTR (error); } +/* No support for restricting writes to btrfs devices yet... */ + mode &= ~BLK_OPEN_RESTRICT_WRITES ;/* * Setup a dummy root and fs_info for test/set super. This is because * we don't actually fill this stuff out until open_ctree, but we need
Signature · 1 line
2.35.3
Re: [PATCH 4/7] btrfs: Do not restrict writes to btrfs devices
Quoted 11 lines · Jan Kara
Btrfs device probing code needs adaptation so that it works when writes are restricted to its mounted devices. Since btrfs maintainer wants to merge these changes through btrfs tree and there are review bandwidth issues with that, let's not block all other filesystems and just not restrict writes to btrfs devices for now.
CC David Sterba <[email protected]>CC Josef Bacik <[email protected]>CC Chris Mason <[email protected]>Signed-off-by Jan Kara <[email protected]>
Jan Kara
reviewerPATCH v3 5/7 fs: Block writes to mounted block devices
In reply to Jan KaraAsk block layer to block writes to block devices mounted by filesystems.
--- include/linux/blkdev.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/blkdev.h +2 -1
@@ -1494,7 +1494,8 @@ extern const struct blk_holder_ops fs_holder_ops;* as stored in sb->s_flags. */ #define sb_open_mode (flags) \ - (BLK_OPEN_READ | (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE)) + (BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \ + (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))struct bdev_handle {struct block_device *bdev;
Signature · 1 line
2.35.3
Re: [PATCH 5/7] fs: Block writes to mounted block devices
Quoted 4 lines · Jan Kara
Ask block layer to block writes to block devices mounted by filesystems.
Signed-off-by Jan Kara <[email protected]>---
Looks good to me,
Jan Kara
reviewerPATCH v3 6/7 xfs: Block writes to log device
In reply to Jan KaraAsk block layer to not allow other writers to open block devices used for xfs log and realtime devices.
--- fs/xfs/xfs_super.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/xfs/xfs_super.c +3 -2
@@ -365,8 +365,9 @@ xfs_blkdev_get( {int error =0 ; - *handlep =bdev_open_by_path (name,BLK_OPEN_READ |BLK_OPEN_WRITE , - mp->m_super, &fs_holder_ops); + *handlep =bdev_open_by_path (name, +BLK_OPEN_READ |BLK_OPEN_WRITE |BLK_OPEN_RESTRICT_WRITES , + mp->m_super, &fs_holder_ops);if (IS_ERR (*handlep)) { error =PTR_ERR (*handlep); *handlep =NULL ;
Signature · 1 line
2.35.3
Jan Kara
reviewerPATCH v3 7/7 ext4: Block writes to journal device
In reply to Jan KaraAsk block layer to not allow other writers to open block device used for ext4 journal.
--- fs/ext4/super.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ext4/super.c +3 -2
@@ -5855,8 +5855,9 @@ static struct bdev_handle *ext4_get_journal_blkdev(struct super_block *sb,struct ext4_super_block *es;int errno; - bdev_handle =bdev_open_by_dev (j_dev,BLK_OPEN_READ |BLK_OPEN_WRITE , - sb, &fs_holder_ops); + bdev_handle =bdev_open_by_dev (j_dev, +BLK_OPEN_READ |BLK_OPEN_WRITE |BLK_OPEN_RESTRICT_WRITES , + sb, &fs_holder_ops);if (IS_ERR (bdev_handle)) { ext4_msg(sb, KERN_ERR, "failed to open journal device unknown-block(%u,%u) %ld",
Signature · 1 line
2.35.3
Quoted 26 lines · Jan Kara
Hello!
This is the third version of the patches to add config option to not allow writing to mounted block devices. The new API for block device opening has been merged so hopefully this patchset can progress towards being merged. We face some issues with necessary btrfs changes (review bandwidth) so this series is modified to enable restricting of writes for all other filesystems. Once btrfs can merge necessary device scanning changes, enabling the support for restricting writes for it is trivial.
For motivation why restricting writes to mounted block devices is interesting see patch 3/7. I've been testing the patches more extensively and I've found couple of things that get broken by disallowing writes to mounted block devices:
1) "mount -o loop" gets broken because util-linux keeps the loop device open read-write when attempting to mount it. Hopefully fixable within util-linux.
2) resize2fs online resizing gets broken because it tries to open the block device read-write only to call resizing ioctl. Trivial to fix within e2fsprogs.
3) Online e2label will break because it directly writes to the ext2/3/4 superblock while the FS is mounted to set the new label. Ext4 driver will have to implement the SETFSLABELioctl()and e2label will have to use it, matching what happens for online labelling of btrfs and xfs.
Quoted 4 lines
Likely there will be other breakage I didn't find yet but overall the breakage looks minor enough that the option might be useful. Definitely good enough for syzbot fuzzing and likely good enough for hardening of systems with more tightened security.
For the series:
Signature · 1 line
Jens Axboe