btrfs: fix the resource leak issue in btrfs_iget()
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
@@ -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
Quoted 5 lines · Penglei Jiang
When
btrfs_iget()returns an error, it does not useiget_failed()to mark and release the inode. Now, we add the missingiget_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
Signed-off-by Penglei Jiang <[email protected]>--- fs/btrfs/inode.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)fs/btrfs/inode.c +0
-0@@ -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
-2path =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
-1btrfs_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
-0unlock_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,
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.
---
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
@@ -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
Quoted 8 lines · Penglei Jiang
When
btrfs_iget()returns an error, it does not useiget_failed()to mark and release the inode. Now, we add the missingiget_failed()call.Fixes 7c855e16ab72 ("btrfs: remove conditional path allocation in btrfs_read_locked_inode()")Reported-by Penglei Jiang <[email protected]>Signed-off-by Penglei Jiang <[email protected]>---
V1 -> V2: Fixed the issue with multiple calls tobtrfs_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@@ -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);
Quoted 6 lines · Penglei Jiang
When
btrfs_iget()returns an error, it does not useiget_failed()to mark and release the inode. Now, we add the missingiget_failed()call.Fixes 7c855e16ab72 ("btrfs: remove conditional path allocation in btrfs_read_locked_inode()")Reported-by Penglei Jiang <[email protected]>Signed-off-by Penglei Jiang <[email protected]>
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 tobtrfs_iget()fs/btrfs/inode.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/btrfs/inode.c +3
-1@@ -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);