Skip to contentSkip to conversation

fs/cifs: Fix resource leak

PATCH-next Khaled ROMDHANI 2 participants 3 messages May 4, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

The -EIO error return path is leaking memory allocated to page. Fix this by invoking the free_dentry_path before the return.

Addresses-Coverity: ("Resource leak")

---
 fs/cifs/link.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/cifs/link.c +3 -1
index 1cbe7ec73728..1485c6095ba1 100644
@@ -686,8 +686,10 @@ cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
 	void *page = alloc_dentry_path();
 	struct inode *newinode = NULL;
 
-	if (unlikely(cifs_forced_shutdown(cifs_sb)))
+	if (unlikely(cifs_forced_shutdown(cifs_sb))) {
+		free_dentry_path(page);
 		return -EIO;
+	}
 
 	xid = get_xid();
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 6 lines · Khaled ROMDHANI

The -EIO error return path is leaking memory allocated to page. Fix this by invoking the free_dentry_path before the return.

Addresses-Coverity: ("Resource leak")

Add a Fixes tag.

Fixes 583248493f78 ("cifs: add shutdown support")
Quoted 9 lines
---
 fs/cifs/link.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/cifs/link.c +0 -0
index 1cbe7ec73728..1485c6095ba1 100644

fs/cifs/link.c cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode, · line 692

Quoted 8 lines
fs/cifs/link.c +3 -1
@@ -686,8 +686,10 @@ cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
 	void *page = alloc_dentry_path();
 	struct inode *newinode = NULL;
 
-	if (unlikely(cifs_forced_shutdown(cifs_sb)))
+	if (unlikely(cifs_forced_shutdown(cifs_sb))) {
+		free_dentry_path(page);
 		return -EIO;
+	}

Better to move the allocation here. Avoid calling functions which can fail in the declaration block. Better to test for NULL directly instead of hiding the test inside the build_path_from_dentry() function.

	page = alloc_dentry_path();
	if (!page)
		return -ENOMEM;

The error handling in this function is slightly unweildy...

regards,
dan carpenter

#2 lore raw
Quoted 5 lines · Dan Carpenter
Quoted 4 lines · Khaled ROMDHANI

The -EIO error return path is leaking memory allocated to page. Fix this by invoking the free_dentry_path before the return.

Quoted 7 lines
Quoted 2 lines

Addresses-Coverity: ("Resource leak")

Add a Fixes tag.

Fixes 583248493f78 ("cifs: add shutdown support")

Yes, I will add a Fixes tag.

Quoted 22 lines
Quoted 9 lines
---
 fs/cifs/link.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/cifs/link.c +0 -0
index 1cbe7ec73728..1485c6095ba1 100644

fs/cifs/link.c cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode, · line 692

Quoted 8 lines
fs/cifs/link.c +3 -1
@@ -686,8 +686,10 @@ cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
 	void *page = alloc_dentry_path();
 	struct inode *newinode = NULL;
 
-	if (unlikely(cifs_forced_shutdown(cifs_sb)))
+	if (unlikely(cifs_forced_shutdown(cifs_sb))) {
+		free_dentry_path(page);
 		return -EIO;
+	}

Better to move the allocation here. Avoid calling functions which can fail in the declaration block. Better to test for NULL directly instead of hiding the test inside the build_path_from_dentry() function.

Quoted 9 lines
	page = alloc_dentry_path();
	if (!page)
		return -ENOMEM;

The error handling in this function is slightly unweildy...

regards,
dan carpenter

Yes, it would be better to move the allocation...
I will send a V2.

Thanks.