diff options
author | Naohiro Aota <naota@elisp.net> | 2018-07-26 20:04:55 -0400 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2018-08-06 07:13:04 -0400 |
commit | 39379faaad79e3cf403a6904a08676b7850043ae (patch) | |
tree | 6b7f07df694ff8e502a024f71a2dd0020f23e5c3 | |
parent | 64f64f43c89aca1782aa672e0586f6903c5d8979 (diff) |
btrfs: revert fs_devices state on error of btrfs_init_new_device
When btrfs hits error after modifying fs_devices in
btrfs_init_new_device() (such as btrfs_add_dev_item() returns error), it
leaves everything as is, but frees allocated btrfs_device. As a result,
fs_devices->devices and fs_devices->alloc_list contain already freed
btrfs_device, leading to later use-after-free bug.
Error path also messes the things like ->num_devices. While they go back
to the original value by unscanning btrfs devices, it is safe to revert
them here.
Fixes: 79787eaab461 ("btrfs: replace many BUG_ONs with proper error handling")
Signed-off-by: Naohiro Aota <naota@elisp.net>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r-- | fs/btrfs/volumes.c | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 7218a79a0e57..da86706123ff 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c | |||
@@ -2321,7 +2321,8 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path | |||
2321 | struct super_block *sb = fs_info->sb; | 2321 | struct super_block *sb = fs_info->sb; |
2322 | struct rcu_string *name; | 2322 | struct rcu_string *name; |
2323 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | 2323 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
2324 | u64 tmp; | 2324 | u64 orig_super_total_bytes; |
2325 | u64 orig_super_num_devices; | ||
2325 | int seeding_dev = 0; | 2326 | int seeding_dev = 0; |
2326 | int ret = 0; | 2327 | int ret = 0; |
2327 | bool unlocked = false; | 2328 | bool unlocked = false; |
@@ -2417,12 +2418,14 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path | |||
2417 | if (!blk_queue_nonrot(q)) | 2418 | if (!blk_queue_nonrot(q)) |
2418 | fs_devices->rotating = 1; | 2419 | fs_devices->rotating = 1; |
2419 | 2420 | ||
2420 | tmp = btrfs_super_total_bytes(fs_info->super_copy); | 2421 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
2421 | btrfs_set_super_total_bytes(fs_info->super_copy, | 2422 | btrfs_set_super_total_bytes(fs_info->super_copy, |
2422 | round_down(tmp + device->total_bytes, fs_info->sectorsize)); | 2423 | round_down(orig_super_total_bytes + device->total_bytes, |
2424 | fs_info->sectorsize)); | ||
2423 | 2425 | ||
2424 | tmp = btrfs_super_num_devices(fs_info->super_copy); | 2426 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
2425 | btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1); | 2427 | btrfs_set_super_num_devices(fs_info->super_copy, |
2428 | orig_super_num_devices + 1); | ||
2426 | 2429 | ||
2427 | /* add sysfs device entry */ | 2430 | /* add sysfs device entry */ |
2428 | btrfs_sysfs_add_device_link(fs_devices, device); | 2431 | btrfs_sysfs_add_device_link(fs_devices, device); |
@@ -2502,6 +2505,22 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path | |||
2502 | 2505 | ||
2503 | error_sysfs: | 2506 | error_sysfs: |
2504 | btrfs_sysfs_rm_device_link(fs_devices, device); | 2507 | btrfs_sysfs_rm_device_link(fs_devices, device); |
2508 | mutex_lock(&fs_info->fs_devices->device_list_mutex); | ||
2509 | mutex_lock(&fs_info->chunk_mutex); | ||
2510 | list_del_rcu(&device->dev_list); | ||
2511 | list_del(&device->dev_alloc_list); | ||
2512 | fs_info->fs_devices->num_devices--; | ||
2513 | fs_info->fs_devices->open_devices--; | ||
2514 | fs_info->fs_devices->rw_devices--; | ||
2515 | fs_info->fs_devices->total_devices--; | ||
2516 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; | ||
2517 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); | ||
2518 | btrfs_set_super_total_bytes(fs_info->super_copy, | ||
2519 | orig_super_total_bytes); | ||
2520 | btrfs_set_super_num_devices(fs_info->super_copy, | ||
2521 | orig_super_num_devices); | ||
2522 | mutex_unlock(&fs_info->chunk_mutex); | ||
2523 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | ||
2505 | error_trans: | 2524 | error_trans: |
2506 | if (seeding_dev) | 2525 | if (seeding_dev) |
2507 | sb->s_flags |= SB_RDONLY; | 2526 | sb->s_flags |= SB_RDONLY; |