Skip to contentSkip to conversation

btrfs: fix the resource leak issue in btrfs_iget()

PATCH Superseded Penglei Jiang 3 participants 5 messages Apr 21, 2025 – Apr 22, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix the resource leak issue in btrfs_iget()

When btrfs_iget() returns an error, it does not use iget_failed() to mark and release the inode. Now, we add the missing iget_failed() call.

---
 fs/btrfs/inode.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/btrfs/inode.c +9 -3
index cc67d1a2d611..61d7f3f94090 100644
@@ -5681,16 +5681,22 @@ struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
 		return inode;
 
 	path = btrfs_alloc_path();
-	if (!path)
-		return ERR_PTR(-ENOMEM);
+	if (!path) {
+		ret = -ENOMEM;
+		goto bad_inode;
+	}
 
 	ret = btrfs_read_locked_inode(inode, path);
 	btrfs_free_path(path);
 	if (ret)
-		return ERR_PTR(ret);
+		goto bad_inode;
 
 	unlock_new_inode(&inode->vfs_inode);
 	return inode;
+
+bad_inode:
+	iget_failed(&inode->vfs_inode);
+	return ERR_PTR(ret);
 }
 
 static struct btrfs_inode *new_simple_dir(struct inode *dir,
Signature · 1 line
2.17.1
#1 lore raw
Quoted 5 lines · Penglei Jiang

When btrfs_iget() returns an error, it does not use iget_failed() to mark and release the inode. Now, we add the missing iget_failed() call.

IIRC this is not a syzbot report, although it's definitely a C reproducer from syzbot.

Thus I'm not sure if the closes: tag is correct.

Quoted 11 lines
---
  fs/btrfs/inode.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
fs/btrfs/inode.c +0 -0
index cc67d1a2d611..61d7f3f94090 100644
@@ -5681,16 +5681,22 @@ struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
  		return inode;

fs/btrfs/inode.c struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root) · line 5689

Quoted 10 lines
fs/btrfs/inode.c +4 -2
 
  	path = btrfs_alloc_path();
-	if (!path)
-		return ERR_PTR(-ENOMEM);
+	if (!path) {
+		ret = -ENOMEM;
+		goto bad_inode;
+	}
 
  	ret = btrfs_read_locked_inode(inode, path);

On error, btrfs_read_locked_inode() has already called iget_failed() at out: tag.

fs/btrfs/inode.c struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root) · line 5692

Quoted 4 lines
fs/btrfs/inode.c +1 -1
  	btrfs_free_path(path);
  	if (ret)
-		return ERR_PTR(ret);
+		goto bad_inode;

So we will either underflow or use-after-free the inode.

It looks like only the btrfs_alloc_path() failure is missing the handling.

And the error looks like a regression caused by commit 7c855e16ab72 ("btrfs: remove conditional path allocation in btrfs_read_locked_inode()"), please add a fixes: tag for it, which is more important than the syzbot tag.

Thanks,
Qu

Quoted 10 lines
fs/btrfs/inode.c +4 -0
  	unlock_new_inode(&inode->vfs_inode);
  	return inode;
+
+bad_inode:
+	iget_failed(&inode->vfs_inode);
+	return ERR_PTR(ret);
  }
 
  static struct btrfs_inode *new_simple_dir(struct inode *dir,
#2 lore raw

PATCH v2 1/1 btrfs: fix the resource leak issue in btrfs_iget()

When btrfs_iget() returns an error, it does not use iget_failed() to mark and release the inode. Now, we add the missing iget_failed() call.

Fixes 7c855e16ab72 ("btrfs: remove conditional path allocation in btrfs_read_locked_inode()")

---
V1 -> V2: Fixed the issue with multiple calls to btrfs_iget()

 fs/btrfs/inode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +3 -1
index cc67d1a2d611..1cbf92ca748d 100644
@@ -5681,8 +5681,10 @@ struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
 		return inode;
 
 	path = btrfs_alloc_path();
-	if (!path)
+	if (!path) {
+		iget_failed(&inode->vfs_inode);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	ret = btrfs_read_locked_inode(inode, path);
 	btrfs_free_path(path);
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#3 lore raw
Quoted 8 lines · Penglei Jiang

When btrfs_iget() returns an error, it does not use iget_failed() to mark and release the inode. Now, we add the missing iget_failed() call.

Fixes 7c855e16ab72 ("btrfs: remove conditional path allocation in btrfs_read_locked_inode()")

---
V1 -> V2: Fixed the issue with multiple calls to btrfs_iget()

Looks good to me.

Thanks,
Qu

Quoted 20 lines
  fs/btrfs/inode.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +3 -1
index cc67d1a2d611..1cbf92ca748d 100644
@@ -5681,8 +5681,10 @@ struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
  		return inode;
 
  	path = btrfs_alloc_path();
-	if (!path)
+	if (!path) {
+		iget_failed(&inode->vfs_inode);
  		return ERR_PTR(-ENOMEM);
+	}
 
  	ret = btrfs_read_locked_inode(inode, path);
  	btrfs_free_path(path);
#4 lore raw
In reply to Penglei Jiang
Quoted 6 lines · Penglei Jiang

When btrfs_iget() returns an error, it does not use iget_failed() to mark and release the inode. Now, we add the missing iget_failed() call.

Fixes 7c855e16ab72 ("btrfs: remove conditional path allocation in btrfs_read_locked_inode()")

Now pushed to for-next branch, with some updates on the commit message.

https://github.com/btrfs/linux/commit/cfcd9ed0108925c8071e27e6d5a300adb74c1839

Appreciate your bug report and fix. It would be even better if plan to continue your contribution to btrfs.

Thanks,
Qu

Quoted 22 lines

---
V1 -> V2: Fixed the issue with multiple calls to btrfs_iget()

  fs/btrfs/inode.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +3 -1
index cc67d1a2d611..1cbf92ca748d 100644
@@ -5681,8 +5681,10 @@ struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
  		return inode;
 
  	path = btrfs_alloc_path();
-	if (!path)
+	if (!path) {
+		iget_failed(&inode->vfs_inode);
  		return ERR_PTR(-ENOMEM);
+	}
 
  	ret = btrfs_read_locked_inode(inode, path);
  	btrfs_free_path(path);