Skip to content

Commit f311ade

Browse files
wenwenwang1kdave
authored andcommitted
btrfs: ref-verify: fix memory leaks
In btrfs_ref_tree_mod(), 'ref' and 'ra' are allocated through kzalloc() and kmalloc(), respectively. In the following code, if an error occurs, the execution will be redirected to 'out' or 'out_unlock' and the function will be exited. However, on some of the paths, 'ref' and 'ra' are not deallocated, leading to memory leaks. For example, if 'action' is BTRFS_ADD_DELAYED_EXTENT, add_block_entry() will be invoked. If the return value indicates an error, the execution will be redirected to 'out'. But, 'ref' is not deallocated on this path, causing a memory leak. To fix the above issues, deallocate both 'ref' and 'ra' before exiting from the function when an error is encountered. CC: [email protected] # 4.15+ Signed-off-by: Wenwen Wang <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent d55966c commit f311ade

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

fs/btrfs/ref-verify.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
744744
*/
745745
be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
746746
if (IS_ERR(be)) {
747+
kfree(ref);
747748
kfree(ra);
748749
ret = PTR_ERR(be);
749750
goto out;
@@ -757,6 +758,8 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
757758
"re-allocated a block that still has references to it!");
758759
dump_block_entry(fs_info, be);
759760
dump_ref_action(fs_info, ra);
761+
kfree(ref);
762+
kfree(ra);
760763
goto out_unlock;
761764
}
762765

@@ -819,6 +822,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
819822
"dropping a ref for a existing root that doesn't have a ref on the block");
820823
dump_block_entry(fs_info, be);
821824
dump_ref_action(fs_info, ra);
825+
kfree(ref);
822826
kfree(ra);
823827
goto out_unlock;
824828
}
@@ -834,6 +838,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
834838
"attempting to add another ref for an existing ref on a tree block");
835839
dump_block_entry(fs_info, be);
836840
dump_ref_action(fs_info, ra);
841+
kfree(ref);
837842
kfree(ra);
838843
goto out_unlock;
839844
}

0 commit comments

Comments
 (0)