btrfs: revert fs_devices state on error of btrfs_init_new_device
authorNaohiro Aota <naota@elisp.net>
Fri, 27 Jul 2018 00:04:55 +0000 (09:04 +0900)
committerDavid Sterba <dsterba@suse.com>
Mon, 6 Aug 2018 11:13:04 +0000 (13:13 +0200)
commit39379faaad79e3cf403a6904a08676b7850043ae
tree6b7f07df694ff8e502a024f71a2dd0020f23e5c3
parent64f64f43c89aca1782aa672e0586f6903c5d8979
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>
fs/btrfs/volumes.c