Skip to contentSkip to conversation

mount.cifs.c: fix memory leaks in main func

PATCH Superseded Zhiqiang Liu 2 participants 3 messages Aug 1, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mount.cifs.c: fix memory leaks in main func

From: Jiawen Liu <[email protected]>

In mount.cifs module, orgoptions and mountpoint in the main func point to the memory allocated by func realpath and strndup respectively. However, they are not freed before the main func returns so that the memory leaks occurred.

The memory leak problem is reported by LeakSanitizer tool. LeakSanitizer url: "https://github.com/google/sanitizers"

Here I free the pointers orgoptions and mountpoint before main func returns.

Fixes:7549ad5e7126 ("memory leaks: caused by func realpath and strndup")

---
 mount.cifs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mount.cifs.c +7 -1
index ae7a899..029f01a 100644
@@ -1830,6 +1830,7 @@ assemble_mountinfo(struct parsed_mount_info *parsed_info,
 	}
 
 assemble_exit:
+	free(orgoptions);
 	return rc;
 }
 
@@ -1994,8 +1995,11 @@ int main(int argc, char **argv)
 
 	/* chdir into mountpoint as soon as possible */
 	rc = acquire_mountpoint(&mountpoint);
-	if (rc)
+	if (rc) {
+		free(mountpoint);
+		free(orgoptions);
 		return rc;
+	}
 
 	/*
 	 * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2018,7 @@ int main(int argc, char **argv)
 		/* child */
 		rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
 					orig_dev, orgoptions);
+		free(mountpoint);
 		return rc;
 	} else {
 		/* parent */
@@ -2149,5 +2154,6 @@ mount_exit:
 	}
 	free(options);
 	free(orgoptions);
+	free(mountpoint);
 	return rc;
 }
Signature · 1 line
2.7.4
#1 lore raw

Hi Zhiqiang,

You are on the right list :)

Unfortunately it seems you have sent the exact same patch as before so I'll post my comments again:

mount.cifs.c assemble_mountinfo(struct parsed_mount_info *parsed_info, · line 1835

Quoted 10 lines · Zhiqiang Liu

index ae7a899..029f01a 100644

mount.cifs.c +1 -0
@@ -1830,6 +1830,7 @@ assemble_mountinfo(struct parsed_mount_info *parsed_info,
 	}
 
 assemble_exit:
+	free(orgoptions);
 	return rc;
 }

Since orgoptions is allocated in main() you should also free it there. In fact it is already freed there so the return have to be changed to goto.

mount.cifs.c int main(int argc, char **argv) · line 2002

Quoted 11 lines
mount.cifs.c +4 -1
@@ -1994,8 +1995,11 @@ int main(int argc, char **argv)
 
 	/* chdir into mountpoint as soon as possible */
 	rc = acquire_mountpoint(&mountpoint);
-	if (rc)
+	if (rc) {
+		free(mountpoint);
+		free(orgoptions);
 		return rc;
+	}

Since mountpoint is allocated in acquire_mountpoint() you should free it there if there's an error.

mount.cifs.c int main(int argc, char **argv) · line 2021

Quoted 7 lines
mount.cifs.c +1 -0
 	/*
 	 * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2018,7 @@ int main(int argc, char **argv)
 		/* child */
 		rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
 					orig_dev, orgoptions);
+		free(mountpoint);

Since this code block is only run by the child I think it's ok to not use goto. Don't forget to free(orgoptions) if you remove it from assemble_mountinfo()

mount.cifs.c mount_exit: · line 2157

Quoted 8 lines
mount.cifs.c +1 -0
 		return rc;
 	} else {
 		/* parent */
@@ -2149,5 +2154,6 @@ mount_exit:
 	}
 	free(options);
 	free(orgoptions);
+	free(mountpoint);

Cheers,

Signature · 4 lines
Aurélien Aptel / SUSE Labs Samba Team
GPG: 1839 CB5F 9F5B FB9B AA97  8C99 03C8 A49B 521B D5D3
SUSE Linux GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah HRB 21284 (AG Nürnberg)
#2 lore raw
Quoted 7 lines · Aurélien Aptel

Hi Zhiqiang,

You are on the right list :)

Unfortunately it seems you have sent the exact same patch as before so I'll post my comments again:

Thanks for your reply.
I have just missed your mail with comments. Sorry for that. I will modify the patch as your suggestion, then post the v2 patch.

Quoted 54 lines

mount.cifs.c assemble_mountinfo(struct parsed_mount_info *parsed_info, · line 1835

Quoted 10 lines · Zhiqiang Liu

index ae7a899..029f01a 100644

mount.cifs.c +1 -0
@@ -1830,6 +1830,7 @@ assemble_mountinfo(struct parsed_mount_info *parsed_info,
 	}
 
 assemble_exit:
+	free(orgoptions);
 	return rc;
 }

Since orgoptions is allocated in main() you should also free it there. In fact it is already freed there so the return have to be changed to goto.

mount.cifs.c int main(int argc, char **argv) · line 2002

Quoted 11 lines
mount.cifs.c +4 -1
@@ -1994,8 +1995,11 @@ int main(int argc, char **argv)
 
 	/* chdir into mountpoint as soon as possible */
 	rc = acquire_mountpoint(&mountpoint);
-	if (rc)
+	if (rc) {
+		free(mountpoint);
+		free(orgoptions);
 		return rc;
+	}

Since mountpoint is allocated in acquire_mountpoint() you should free it there if there's an error.

mount.cifs.c int main(int argc, char **argv) · line 2021

Quoted 7 lines
mount.cifs.c +1 -0
 	/*
 	 * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2018,7 @@ int main(int argc, char **argv)
 		/* child */
 		rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
 					orig_dev, orgoptions);
+		free(mountpoint);

Since this code block is only run by the child I think it's ok to not use goto. Don't forget to free(orgoptions) if you remove it from assemble_mountinfo()

mount.cifs.c mount_exit: · line 2157

Quoted 8 lines
mount.cifs.c +1 -0
 		return rc;
 	} else {
 		/* parent */
@@ -2149,5 +2154,6 @@ mount_exit:
 	}
 	free(options);
 	free(orgoptions);
+	free(mountpoint);

Cheers,