Skip to content

Commit 1357272

Browse files
idryomovJosef Bacik
authored andcommitted
Btrfs: fix a use-after-free bug in btrfs_dev_replace_finishing
free_device rcu callback, scheduled from btrfs_rm_dev_replace_srcdev, can be processed before btrfs_scratch_superblock is called, which would result in a use-after-free on btrfs_device contents. Fix this by zeroing the superblock before the rcu callback is registered. Cc: Stefan Behrens <[email protected]> Signed-off-by: Ilya Dryomov <[email protected]> Signed-off-by: Josef Bacik <[email protected]>
1 parent 964fb15 commit 1357272

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

fs/btrfs/dev-replace.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
535535
list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
536536

537537
btrfs_rm_dev_replace_srcdev(fs_info, src_device);
538-
if (src_device->bdev) {
539-
/* zero out the old super */
540-
btrfs_scratch_superblock(src_device);
541-
}
538+
542539
/*
543540
* this is again a consistent state where no dev_replace procedure
544541
* is running, the target device is part of the filesystem, the

fs/btrfs/volumes.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,7 @@ void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
17161716
struct btrfs_device *srcdev)
17171717
{
17181718
WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1719+
17191720
list_del_rcu(&srcdev->dev_list);
17201721
list_del_rcu(&srcdev->dev_alloc_list);
17211722
fs_info->fs_devices->num_devices--;
@@ -1725,9 +1726,13 @@ void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
17251726
}
17261727
if (srcdev->can_discard)
17271728
fs_info->fs_devices->num_can_discard--;
1728-
if (srcdev->bdev)
1729+
if (srcdev->bdev) {
17291730
fs_info->fs_devices->open_devices--;
17301731

1732+
/* zero out the old super */
1733+
btrfs_scratch_superblock(srcdev);
1734+
}
1735+
17311736
call_rcu(&srcdev->rcu, free_device);
17321737
}
17331738

0 commit comments

Comments
 (0)