2018-11-24 08:22:59

by Pan Bian

[permalink] [raw]
Subject: [PATCH] btrfs: relocation: set trans to be NULL after free

The function relocate_block_group calls btrfs_end_transaction to release
trans when update_backref_cache returns 1, and then continues the loop
body. If btrfs_block_rsv_refill fails this time, it will jump out the
loop and the freed trans will be accessed. This may result in a
use-after-free bug. The patch assigns NULL to trans after trans is
released so that it will not be accessed.

Fixes: 0647bf564f1("Btrfs: improve forever loop when doing balance
relocation")

Signed-off-by: Pan Bian <[email protected]>
---
fs/btrfs/relocation.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 924116f..a3f75b8 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -3959,6 +3959,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
restart:
if (update_backref_cache(trans, &rc->backref_cache)) {
btrfs_end_transaction(trans);
+ trans = NULL;
continue;
}

--
2.7.4




2018-11-24 08:27:41

by Qu Wenruo

[permalink] [raw]
Subject: Re: [PATCH] btrfs: relocation: set trans to be NULL after free



On 2018/11/23 下午6:10, Pan Bian wrote:
> The function relocate_block_group calls btrfs_end_transaction to release
> trans when update_backref_cache returns 1, and then continues the loop
> body. If btrfs_block_rsv_refill fails this time, it will jump out the
> loop and the freed trans will be accessed. This may result in a
> use-after-free bug. The patch assigns NULL to trans after trans is
> released so that it will not be accessed.
>
> Fixes: 0647bf564f1("Btrfs: improve forever loop when doing balance
> relocation")
>
> Signed-off-by: Pan Bian <[email protected]>

Reviewed-by: Qu Wenruo <[email protected]>

Thanks,
Qu

> ---
> fs/btrfs/relocation.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index 924116f..a3f75b8 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c
> @@ -3959,6 +3959,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
> restart:
> if (update_backref_cache(trans, &rc->backref_cache)) {
> btrfs_end_transaction(trans);
> + trans = NULL;
> continue;
> }
>
>


Attachments:
signature.asc (499.00 B)
OpenPGP digital signature

2018-11-24 08:32:46

by David Sterba

[permalink] [raw]
Subject: Re: [PATCH] btrfs: relocation: set trans to be NULL after free

On Fri, Nov 23, 2018 at 06:10:15PM +0800, Pan Bian wrote:
> The function relocate_block_group calls btrfs_end_transaction to release
> trans when update_backref_cache returns 1, and then continues the loop
> body. If btrfs_block_rsv_refill fails this time, it will jump out the
> loop and the freed trans will be accessed. This may result in a
> use-after-free bug. The patch assigns NULL to trans after trans is
> released so that it will not be accessed.
>
> Fixes: 0647bf564f1("Btrfs: improve forever loop when doing balance
> relocation")
>
> Signed-off-by: Pan Bian <[email protected]>

Good catch, thanks.

Reviewed-by: David Sterba <[email protected]>