Skip to content

Commit 67a2c45

Browse files
Miao Xiemasoncl
authored andcommitted
Btrfs: fix use-after-free problem of the device during device replace
The problem is: Task0(device scan task) Task1(device replace task) scan_one_device() mutex_lock(&uuid_mutex) device = find_device() mutex_lock(&device_list_mutex) lock_chunk() rm_and_free_source_device unlock_chunk() mutex_unlock(&device_list_mutex) check device Destroying the target device if device replace fails also has the same problem. We fix this problem by locking uuid_mutex during destroying source device or target device, just like the device remove operation. It is a temporary solution, we can fix this problem and make the code more clear by atomic counter in the future. Signed-off-by: Miao Xie <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent adbbb86 commit 67a2c45

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

fs/btrfs/dev-replace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
509509
ret = btrfs_commit_transaction(trans, root);
510510
WARN_ON(ret);
511511

512+
mutex_lock(&uuid_mutex);
512513
/* keep away write_all_supers() during the finishing procedure */
513514
mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
514515
mutex_lock(&root->fs_info->chunk_mutex);
@@ -536,6 +537,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
536537
btrfs_dev_replace_unlock(dev_replace);
537538
mutex_unlock(&root->fs_info->chunk_mutex);
538539
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
540+
mutex_unlock(&uuid_mutex);
539541
if (tgt_device)
540542
btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
541543
mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
@@ -591,6 +593,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
591593
*/
592594
mutex_unlock(&root->fs_info->chunk_mutex);
593595
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
596+
mutex_unlock(&uuid_mutex);
594597

595598
/* write back the superblocks */
596599
trans = btrfs_start_transaction(root, 0);

fs/btrfs/volumes.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
5050
static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
5151
static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
5252

53-
static DEFINE_MUTEX(uuid_mutex);
53+
DEFINE_MUTEX(uuid_mutex);
5454
static LIST_HEAD(fs_uuids);
5555

5656
static void lock_chunks(struct btrfs_root *root)
@@ -1867,6 +1867,7 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
18671867
{
18681868
struct btrfs_device *next_device;
18691869

1870+
mutex_lock(&uuid_mutex);
18701871
WARN_ON(!tgtdev);
18711872
mutex_lock(&fs_info->fs_devices->device_list_mutex);
18721873
if (tgtdev->bdev) {
@@ -1886,6 +1887,7 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
18861887
call_rcu(&tgtdev->rcu, free_device);
18871888

18881889
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1890+
mutex_unlock(&uuid_mutex);
18891891
}
18901892

18911893
static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,

fs/btrfs/volumes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <linux/btrfs.h>
2525
#include "async-thread.h"
2626

27+
extern struct mutex uuid_mutex;
28+
2729
#define BTRFS_STRIPE_LEN (64 * 1024)
2830

2931
struct buffer_head;

0 commit comments

Comments
 (0)