btrfs: fix use-after-free of reloc_control in btrfs_reloc_cow_block
PATCH 1/1 btrfs: fix use-after-free of reloc_control in btrfs_reloc_cow_block
btrfs_reloc_cow_block() reads fs_info->reloc_ctl without holding any lock and then uses the obtained rc pointer throughout the function (including replace_file_extents which may sleep). Meanwhile, the balance error path can concurrently call unset_reloc_control() followed by free_reloc_control(), freeing rc while btrfs_reloc_cow_block() is still using it.
The race window exists because btrfs_commit_current_transaction() in relocate_block_group() may return immediately when the filesystem is in error state (no running transaction to wait for), allowing free_reloc_control() to execute while other threads still hold references to rc obtained before unset_reloc_control().
Fix this by adding a btrfs_commit_current_transaction() call in btrfs_relocate_block_group() right before free_reloc_control(). This ensures that any concurrent thread that obtained rc via reloc_ctl (which requires being in a transaction context) has completed its transaction before rc is freed.
--- fs/btrfs/relocation.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/btrfs/relocation.c +8 -0
@@ -5443,6 +5443,14 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,reloc_chunk_end (fs_info); out_put_bg:btrfs_put_block_group (bg); +/* +* Ensure no concurrent btrfs_reloc_cow_block() is still using rc. +* After unset_reloc_control() new callers will see reloc_ctl == NULL +* and return immediately. But callers that read reloc_ctl before unset +* are still in a transaction. Wait for the current transaction to +* complete so all such callers have finished using rc. +*/ +btrfs_commit_current_transaction (fs_info->tree_root);free_reloc_control (rc);return ret; }
Signature · 1 line
2.43.0
Quoted 33 lines · Yun Zhou
btrfs_reloc_cow_block()readsfs_info->reloc_ctlwithout holding any lock and then uses the obtained rc pointer throughout the function (includingreplace_file_extentswhich may sleep). Meanwhile, the balance error path can concurrently callunset_reloc_control()followed byfree_reloc_control(), freeing rc whilebtrfs_reloc_cow_block()is still using it.The race window exists because
btrfs_commit_current_transaction()inrelocate_block_group()may return immediately when the filesystem is in error state (no running transaction to wait for), allowingfree_reloc_control()to execute while other threads still hold references to rc obtained beforeunset_reloc_control().Fix this by adding a
btrfs_commit_current_transaction()call inbtrfs_relocate_block_group()right beforefree_reloc_control(). This ensures that any concurrent thread that obtained rc viareloc_ctl(which requires being in a transaction context) has completed its transaction before rc is freed.Reported-by [email protected]Signed-off-by Yun Zhou <[email protected]>--- fs/btrfs/relocation.c | 8 ++++++++ 1 file changed, 8 insertions(+)fs/btrfs/relocation.c +0
-0@@ -5443,6 +5443,14 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,reloc_chunk_end (fs_info);
fs/btrfs/relocation.c int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start, · line 5453
Quoted 10 lines
fs/btrfs/relocation.c +8
-0out_put_bg:btrfs_put_block_group (bg); +/* +* Ensure no concurrent btrfs_reloc_cow_block() is still using rc. +* After unset_reloc_control() new callers will see reloc_ctl == NULL +* and return immediately. But callers that read reloc_ctl before unset +* are still in a transaction. Wait for the current transaction to +* complete so all such callers have finished using rc. +*/ +btrfs_commit_current_transaction (fs_info->tree_root);
This does not work at all:
btrfs_commit_current_transaction() can not join the current transaction if the fs is in error state. start_transaction() will return immediately with -EROFS if a transaction abort happened before.
Second, the rc structure points to the block group for which the put was done just above, so there's a potential for another use-after-free here.
Finally, always calling btrfs_commit_current_transaction() creates unnecessary overhead when no errors occur (the expected case). This is due not only to the IO involved, but also because it results in an unnecessary rotation of the backup roots in the super block.
Finally there's a similar race in btrfs_init_reloc_root() too.
And there's already a proper way to fix this in progress:
Thanks.
Quoted 7 lines
fs/btrfs/relocation.c +0
-1free_reloc_control (rc);return ret; } --2.43.0