btrfs: ref-verify: fix memory leaks
PATCH 1/1 btrfs: ref-verify: fix memory leaks
In btrfs_ref_tree_mod(), when !parent 're' was allocated through kmalloc(). 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, 're' are not deallocated and may lead to memory leaks.
For example : lookup_block_entry() for 'be' returns null, the out label will be invoked. During that flow ref and ra was freed but not re, which can potentially lead to memleak
--- fs/btrfs/ref-verify.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/btrfs/ref-verify.c +5 -0
@@ -791,6 +791,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re); goto out_unlock; }else if (be->num_refs ==0 ) { btrfs_err(fs_info,
@@ -800,6 +801,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re); goto out_unlock; }
@@ -822,6 +824,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re); goto out_unlock; } exist->num_refs--;
@@ -838,6 +841,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re); goto out_unlock; }kfree (ref);
@@ -849,6 +853,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re); goto out_unlock; } }
Signature · 1 line
2.34.1
Quoted 23 lines · Bragatheswaran Manickavel
In
btrfs_ref_tree_mod(), when !parent 're' was allocated throughkmalloc(). 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, 're' are not deallocated and may lead to memory leaks.For example :
lookup_block_entry()for 'be' returns null, the out label will be invoked. During that flow ref and ra was freed but not re, which can potentially lead to memleakReported-and-tested-by [email protected]Signed-off-by Bragatheswaran Manickavel <[email protected]>--- fs/btrfs/ref-verify.c | 5 +++++ 1 file changed, 5 insertions(+)fs/btrfs/ref-verify.c +0
-0
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 794
Quoted 4 lines
fs/btrfs/ref-verify.c +1
-0@@ -791,6 +791,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);
Here it's fine, 're' was not yet added to the rbtree (be->roots).
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 804
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; }else if (be->num_refs ==0 ) { btrfs_err(fs_info,@@ -800,6 +801,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);
Same here.
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 827
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; }@@ -822,6 +824,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);
Here it's not ok. 're' was added to the rbtree, so you can't free it, as later when accessing the tree, it will trigger a use-after-free bug.
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 844
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; } exist->num_refs--;@@ -838,6 +841,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);
Same here, it will lead to a use-after-free.
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 856
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; }kfree (ref);@@ -849,6 +853,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);
Same here, it will lead to a use-after-free.
Thanks.
Quoted 7 lines
fs/btrfs/ref-verify.c +0
-1goto out_unlock; } } --2.34.1
Quoted 72 lines · Filipe Manana
Quoted 22 lines · Bragatheswaran Manickavel
In
btrfs_ref_tree_mod(), when !parent 're' was allocated throughkmalloc(). 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, 're' are not deallocated and may lead to memory leaks.For example :
lookup_block_entry()for 'be' returns null, the out label will be invoked. During that flow ref and ra was freed but not re, which can potentially lead to memleakReported-and-tested-by [email protected]Signed-off-by Bragatheswaran Manickavel <[email protected]>--- fs/btrfs/ref-verify.c | 5 +++++ 1 file changed, 5 insertions(+)fs/btrfs/ref-verify.c +0
-0fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 794
Quoted 4 lines
fs/btrfs/ref-verify.c +1
-0@@ -791,6 +791,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);Here it's fine, 're' was not yet added to the rbtree (
be->roots).fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 804
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; }else if (be->num_refs ==0 ) { btrfs_err(fs_info,@@ -800,6 +801,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);Same here.
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 827
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; }@@ -822,6 +824,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);Here it's not ok. 're' was added to the rbtree, so you can't free it, as later when accessing the tree, it will trigger a use-after-free bug.
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 844
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; } exist->num_refs--;@@ -838,6 +841,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);Same here, it will lead to a use-after-free.
fs/btrfs/ref-verify.c int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · line 856
Quoted 8 lines
fs/btrfs/ref-verify.c +1
-0goto out_unlock; }kfree (ref);@@ -849,6 +853,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,dump_ref_action (fs_info, ra);kfree (ref);kfree (ra); +kfree (re);Same here, it will lead to a use-after-free.
Quoted 6 lines
Thanks.
Quoted 5 lines
fs/btrfs/ref-verify.c +0
-1goto out_unlock; } } --2.34.1
Thanks Filipe for reviewing this!
Now, I understood why we shouldn't free 're' after it was added to the tree. In that case, can I send a new patch with first two changes.