]> git.hungrycats.org Git - linux/commitdiff
btrfs: zoned: fix double free of bitmaps on clone failure
authorThomas Fourier <fourier.thomas@gmail.com>
Wed, 28 Oct 2026 09:50:51 +0000 (10:50 +0100)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:19 +0000 (21:36 +0100)
btrfs_clone_dev_zone_info() duplicates the original orig_dev->zone_info
with kmemdup(), which also copies the seq_zones, empty_zones and
active_zones pointers. The clone then replaces them one by one with
fresh bitmaps. If one of the allocations fails, the error path frees all
three pointers, including those not yet replaced, which still point to
the bitmaps owned by the original device.

Clear the three pointers right after kmemdup() so the error path only frees
what this function allocated.

Fixes: 21e61ec6d0bb ("btrfs: zoned: clone zoned device info when cloning a device")
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Thomas Fourier <fourier.thomas@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/zoned.c

index 09a81c463053dc205a6e36ebeebae514a11e5c39..3192b86669b9b3ffa8f82baa85397452fa08feab 100644 (file)
@@ -645,6 +645,11 @@ struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *o
        if (!zone_info)
                return NULL;
 
+       /* Don't let the error path free the bitmaps still owned by @orig_dev. */
+       zone_info->seq_zones = NULL;
+       zone_info->empty_zones = NULL;
+       zone_info->active_zones = NULL;
+
        zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
        if (!zone_info->seq_zones)
                goto out;