public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Johannes Thumshirn <Johannes.Thumshirn@wdc.com>
To: Mark Harmstone <mark@harmstone.com>,
	"linux-btrfs@vger.kernel.org" <linux-btrfs@vger.kernel.org>,
	"boris@bur.io" <boris@bur.io>
Cc: Chris Mason <clm@fb.com>
Subject: Re: [PATCH] btrfs: fix use-after-free in move_existing_remap()
Date: Wed, 25 Feb 2026 11:22:42 +0000	[thread overview]
Message-ID: <470807f2-3e2c-49d0-8b74-e5a71b233ad4@wdc.com> (raw)
In-Reply-To: <20260225103557.18457-1-mark@harmstone.com>

On 2/25/26 11:36 AM, Mark Harmstone wrote:
> Fix a potential use-after-free in move_existing_remap(): we're calling
> btrfs_put_block_group() on dest_bg, then passing it to
> btrfs_add_block_group_free_space() a few lines later.
>
> Link: https://lore.kernel.org/linux-btrfs/20260125123908.2096548-1-clm@meta.com/
> Reported-by: Chris Mason <clm@fb.com>
> Fixes: bbea42dfb91f ("btrfs: move existing remaps before relocating block group")
> Signed-off-by: Mark Harmstone <mark@harmstone.com>
> ---
>   fs/btrfs/relocation.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index cc80760ba5e7..5a8644667620 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c
> @@ -4295,14 +4295,17 @@ static int move_existing_remap(struct btrfs_fs_info *fs_info,
>   	bg_needs_free_space = test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE,
>   				       &dest_bg->runtime_flags);
>   	mutex_unlock(&dest_bg->free_space_lock);
> -	btrfs_put_block_group(dest_bg);
>   
>   	if (bg_needs_free_space) {
>   		ret = btrfs_add_block_group_free_space(trans, dest_bg);
> -		if (unlikely(ret))
> +		if (unlikely(ret)) {
> +			btrfs_put_block_group(dest_bg);
>   			goto end;
> +		}
>   	}
>   
> +	btrfs_put_block_group(dest_bg);
> +
>   	ret = btrfs_remove_from_free_space_tree(trans, dest_addr, dest_length);
>   	if (unlikely(ret)) {
>   		btrfs_remove_from_free_space_tree(trans, new_addr, dest_length);
Why can't you move the put to the end of the function and skip the new 
lookup of the BG further down the function?

      reply	other threads:[~2026-02-25 11:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-25 10:35 [PATCH] btrfs: fix use-after-free in move_existing_remap() Mark Harmstone
2026-02-25 11:22 ` Johannes Thumshirn [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=470807f2-3e2c-49d0-8b74-e5a71b233ad4@wdc.com \
    --to=johannes.thumshirn@wdc.com \
    --cc=boris@bur.io \
    --cc=clm@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=mark@harmstone.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox