• Filipe Manana's avatar
    btrfs: zoned: fix use-after-free due to race with dev replace · 0090d6e1
    Filipe Manana authored
    While loading a zone's info during creation of a block group, we can race
    with a device replace operation and then trigger a use-after-free on the
    device that was just replaced (source device of the replace operation).
    
    This happens because at btrfs_load_zone_info() we extract a device from
    the chunk map into a local variable and then use the device while not
    under the protection of the device replace rwsem. So if there's a device
    replace operation happening when we extract the device and that device
    is the source of the replace operation, we will trigger a use-after-free
    if before we finish using the device the replace operation finishes and
    frees the device.
    
    Fix this by enlarging the critical section under the protection of the
    device replace rwsem so that all uses of the device are done inside the
    critical section.
    
    CC: stable@vger.kernel.org # 6.1.x: 15c12fcc: btrfs: zoned: introduce a zone_info struct in btrfs_load_block_group_zone_info
    CC: stable@vger.kernel.org # 6.1.x: 09a46725: btrfs: zoned: factor out per-zone logic from btrfs_load_block_group_zone_info
    CC: stable@vger.kernel.org # 6.1.x: 9e0e3e74: btrfs: zoned: factor out single bg handling from btrfs_load_block_group_zone_info
    CC: stable@vger.kernel.org # 6.1.x: 87463f7e: btrfs: zoned: factor out DUP bg handling from btrfs_load_block_group_zone_info
    CC: stable@vger.kernel.org # 6.1.x
    Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
    Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
    Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    0090d6e1
zoned.c 69.5 KB