Commit e1994e4d authored by Nikolay Borisov's avatar Nikolay Borisov Committed by Greg Kroah-Hartman

btrfs: Fix use-after-free when cleaning up fs_devs with a single stale device

commit fd649f10 upstream.

Commit 4fde46f0 ("Btrfs: free the stale device") introduced
btrfs_free_stale_device which iterates the device lists for all
registered btrfs filesystems and deletes those devices which aren't
mounted. In a btrfs_devices structure has only 1 device attached to it
and it is unused then btrfs_free_stale_devices will proceed to also free
the btrfs_fs_devices struct itself. Currently this leads to a use after
free since list_for_each_entry will try to perform a check on the
already freed memory to see if it has to terminate the loop.

The fix is to use 'break' when we know we are freeing the current
fs_devs.

Fixes: 4fde46f0 ("Btrfs: free the stale device")
Signed-off-by: default avatarNikolay Borisov <nborisov@suse.com>
Reviewed-by: default avatarAnand Jain <anand.jain@oracle.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 97c3e46e
...@@ -568,6 +568,7 @@ void btrfs_free_stale_device(struct btrfs_device *cur_dev) ...@@ -568,6 +568,7 @@ void btrfs_free_stale_device(struct btrfs_device *cur_dev)
btrfs_sysfs_remove_fsid(fs_devs); btrfs_sysfs_remove_fsid(fs_devs);
list_del(&fs_devs->list); list_del(&fs_devs->list);
free_fs_devices(fs_devs); free_fs_devices(fs_devs);
break;
} else { } else {
fs_devs->num_devices--; fs_devs->num_devices--;
list_del(&dev->dev_list); list_del(&dev->dev_list);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment