On 25.01.21 г. 23:42 ч., Josef Bacik wrote:
> In __btrfs_return_cluster_to_free_space we will bail doing the cleanup
> of the cluster if the block group we passed in doesn't match the block
> group on the cluster.  However we drop a reference to block_group, as
> the cluster holds a reference to the block group while it's attached to
> the cluster.  If cluster->block_group != block_group however then this
> is an extra put, which means we'll go negative and free this block group
> down the line, leading to a UAF.

Was this found by code inspection or did you hit in production. Also why
in btrfs_remove_free_space_cache just before
__btrfs_return_cluster_to_free_space there is:

WARN_ON(cluster->block_group != block_group);

IMO this patch should also remove the WARN_ON if it's a valid condition
to have the passed bg be different than the one in the cluster. Also
that WARN_ON is likely racy since it's done outside of cluster->lock.

> 
> Fix this by simply bailing if the block group we passed in does not
> match the block group on the cluster.
> 
> CC: sta...@vger.kernel.org
> Fixes: fa9c0d795f7b ("Btrfs: rework allocation clustering")
> Signed-off-by: Josef Bacik <jo...@toxicpanda.com>
> ---
>  fs/btrfs/free-space-cache.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
> index 0d6dcb5ff963..8be36cc6cbd8 100644
> --- a/fs/btrfs/free-space-cache.c
> +++ b/fs/btrfs/free-space-cache.c
> @@ -2711,8 +2711,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;
> @@ -2750,8 +2752,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);
>  }
> 

Reply via email to