Skip to content

Commit

Permalink
btrfs: avoid double put of block group when emptying cluster
Browse files Browse the repository at this point in the history
It's wrong calling btrfs_put_block_group in
__btrfs_return_cluster_to_free_space if the block group passed is
different than the block group the cluster represents. As this means the
cluster doesn't have a reference to the passed block group. This results
in double put and a use-after-free bug.

Fix this by simply bailing if the block group we passed in does not
match the block group on the cluster.

Fixes: fa9c0d7 ("Btrfs: rework allocation clustering")
CC: stable@vger.kernel.org # 4.4+
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
Josef Bacik authored and David Sterba committed Feb 22, 2021
1 parent 3660d0b commit 95c85fb
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions fs/btrfs/free-space-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -2801,8 +2801,10 @@ static void __btrfs_return_cluster_to_free_space(
struct rb_node *node;

spin_lock(&cluster->lock);
if (cluster->block_group != block_group)
goto out;
if (cluster->block_group != block_group) {
spin_unlock(&cluster->lock);
return;
}

cluster->block_group = NULL;
cluster->window_start = 0;
Expand Down Expand Up @@ -2840,8 +2842,6 @@ static void __btrfs_return_cluster_to_free_space(
entry->offset, &entry->offset_index, bitmap);
}
cluster->root = RB_ROOT;

out:
spin_unlock(&cluster->lock);
btrfs_put_block_group(block_group);
}
Expand Down

0 comments on commit 95c85fb

Please sign in to comment.