Skip to contentSkip to conversation

btrfs: fix memory leak of path on error return path

PATCH Superseded Colin King 2 participants 3 messages Jul 4, 2019 – Jul 5, 2019

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH v2 1/1 btrfs: fix memory leak of path on error return path

From: Colin Ian King <[email protected]>

Currently if the allocation of roots or tmp_ulist fails the error handling does not free up the allocation of path causing a memory leak. Fix this and other similar leaks by moving the call of btrfs_free_path from label out to label out_free_ulist.

Kudos to David Sterba for spotting the issue in my original fix and providing the correct way to fix the leak.

Addresses-Coverity: ("Resource leak")

Fixes 5911c8fe05c5 ("btrfs: fiemap: preallocate ulists for btrfs_check_shared")

---

V2: move the btrfs_free_path to the out_free_ulist label as suggested by
David Sterba as the correct fix.

---
 fs/btrfs/extent_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_io.c +1 -1
index 1eb671c16ff1..31127f6d2971 100644
@@ -4766,11 +4766,11 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 		ret = emit_last_fiemap_cache(fieinfo, &cache);
 	free_extent_map(em);
 out:
-	btrfs_free_path(path);
 	unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
 			     &cached_state);
 
 out_free_ulist:
+	btrfs_free_path(path);
 	ulist_free(roots);
 	ulist_free(tmp_ulist);
 	return ret;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 30 lines · Colin King

From: Colin Ian King <[email protected]>

Currently if the allocation of roots or tmp_ulist fails the error handling does not free up the allocation of path causing a memory leak. Fix this and other similar leaks by moving the call of btrfs_free_path from label out to label out_free_ulist.

Kudos to David Sterba for spotting the issue in my original fix and providing the correct way to fix the leak.

Addresses-Coverity: ("Resource leak")

Fixes 5911c8fe05c5 ("btrfs: fiemap: preallocate ulists for btrfs_check_shared")

---

V2: move the btrfs_free_path to the out_free_ulist label as suggested by
David Sterba as the correct fix.

---
  fs/btrfs/extent_io.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_io.c +0 -0
index 1eb671c16ff1..31127f6d2971 100644
@@ -4766,11 +4766,11 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  		ret = emit_last_fiemap_cache(fieinfo, &cache);
  	free_extent_map(em);

fs/btrfs/extent_io.c int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4776

Quoted 10 lines
fs/btrfs/extent_io.c +1 -1
  out:
-	btrfs_free_path(path);
  	unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  			     &cached_state);
 
  out_free_ulist:
+	btrfs_free_path(path);
  	ulist_free(roots);
  	ulist_free(tmp_ulist);
  	return ret;

Now this causes double free.

$ git diff
fs/btrfs/extent_io.c +0 -0
index 6b154bce5687..61ebdccca04b 100644
@@ -4611,7 +4611,6 @@ int extent_fiemap(struct inode *inode, struct 

fiemap_extent_info *fieinfo,

         ret = btrfs_lookup_file_extent(NULL, root, path,
                         btrfs_ino(BTRFS_I(inode)), -1, 0);
         if (ret < 0) {
-               btrfs_free_path(path);
                 goto out_free_ulist;
         } else {
                 WARN_ON(!ret);
#2 lore raw
Quoted 39 lines · Anand Jain
Quoted 38 lines · Colin King

From: Colin Ian King <[email protected]>

Currently if the allocation of roots or tmp_ulist fails the error handling
does not free up the allocation of path causing a memory leak. Fix this and
other similar leaks by moving the call of btrfs_free_path from label out to label out_free_ulist.

Kudos to David Sterba for spotting the issue in my original fix and providing the correct way to fix the leak.

Addresses-Coverity: ("Resource leak")

Fixes 5911c8fe05c5 ("btrfs: fiemap: preallocate ulists for

btrfs_check_shared")

---

V2: move the btrfs_free_path to the out_free_ulist label as suggested by David Sterba as the correct fix.

---
  fs/btrfs/extent_io.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_io.c +0 -0
index 1eb671c16ff1..31127f6d2971 100644
@@ -4766,11 +4766,11 @@ int extent_fiemap(struct inode *inode, struct
 fiemap_extent_info *fieinfo,
           ret = emit_last_fiemap_cache(fieinfo, &cache);
       free_extent_map(em);

  out:
-    btrfs_free_path(path); unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
                   &cached_state);

Quoted 8 lines
Quoted 6 lines

    out_free_ulist:
+    btrfs_free_path(path);
      ulist_free(roots);
      ulist_free(tmp_ulist);
      return ret;

Now this causes double free.

Thanks for spotting that, I'll send a V3

Quoted 17 lines
$ git diff
fs/btrfs/extent_io.c +0 -1
index 6b154bce5687..61ebdccca04b 100644
@@ -4611,7 +4611,6 @@ int extent_fiemap(struct inode *inode, struct
 fiemap_extent_info *fieinfo,
         ret = btrfs_lookup_file_extent(NULL, root, path,
                         btrfs_ino(BTRFS_I(inode)), -1, 0);
         if (ret < 0) {
-               btrfs_free_path(path);
                 goto out_free_ulist;
         } else {

                WARN_ON(!ret);