Skip to contentSkip to conversation

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

PATCH Applied Zhiqiang Liu 2 participants 4 messages Aug 6, 2019 – Aug 8, 2019

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

PATCH v2 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")

---
v1->v2:
- free orgoptions in main func as suggested by Aurélien Aptel
- free mountpoint in acquire_mountpoint func as suggested by Aurélien Aptel

 mount.cifs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mount.cifs.c +10 -2
index ae7a899..656d353 100644
@@ -1891,7 +1891,10 @@ restore_privs:
 		uid_t __attribute__((unused)) uignore = setfsuid(oldfsuid);
 		gid_t __attribute__((unused)) gignore = setfsgid(oldfsgid);
 	}
-
+	
+	if (rc) {
+		free(*mountpointp);
+	}
 	return rc;
 }
 
@@ -1994,8 +1997,10 @@ int main(int argc, char **argv)
 
 	/* chdir into mountpoint as soon as possible */
 	rc = acquire_mountpoint(&mountpoint);
-	if (rc)
+	if (rc) {
+		free(orgoptions);
 		return rc;
+	}
 
 	/*
 	 * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2019,8 @@ int main(int argc, char **argv)
 		/* child */
 		rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
 					orig_dev, orgoptions);
+		free(orgoptions);
+		free(mountpoint);
 		return rc;
 	} else {
 		/* parent */
@@ -2149,5 +2156,6 @@ mount_exit:
 	}
 	free(options);
 	free(orgoptions);
+	free(mountpoint);
 	return rc;
 }
Signature · 1 line
2.7.4
#1 lore raw

пн, 5 авг. 2019 г. в 19:36, Zhiqiang Liu <[email protected]>:

Quoted 72 lines

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")

---
v1->v2:
- free orgoptions in main func as suggested by Aurélien Aptel
- free mountpoint in acquire_mountpoint func as suggested by Aurélien Aptel

 mount.cifs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mount.cifs.c +10 -3
index ae7a899..656d353 100644
@@ -1891,7 +1891,10 @@ restore_privs:
                uid_t __attribute__((unused)) uignore = setfsuid(oldfsuid);
                gid_t __attribute__((unused)) gignore = setfsgid(oldfsgid);
        }
-
+
+       if (rc) {
+               free(*mountpointp);
+       }
        return rc;
 }
 
@@ -1994,8 +1997,10 @@ int main(int argc, char **argv)
 
        /* chdir into mountpoint as soon as possible */
        rc = acquire_mountpoint(&mountpoint);
-       if (rc)
+       if (rc) {
+               free(orgoptions);
                return rc;
+       }
 
        /*
         * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2019,8 @@ int main(int argc, char **argv)
                /* child */
                rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
                                        orig_dev, orgoptions);
+               free(orgoptions);
+               free(mountpoint);
                return rc;
        } else {
                /* parent */
@@ -2149,5 +2156,6 @@ mount_exit:
        }
        free(options);
        free(orgoptions);
+       free(mountpoint);
        return rc;
 }
--
Quoted 2 lines

2.7.4

Thanks for the patch! I will apply it to my github tree shortly.

--
Best regards,
Pavel Shilovsky

#2 lore raw

Merged into "next" with one minor change - removed a trailing white space. Thanks.

--
Best regards,
Pavel Shilovsky

вт, 6 авг. 2019 г. в 09:49, Pavel Shilovsky <[email protected]>:

Quoted 82 lines

пн, 5 авг. 2019 г. в 19:36, Zhiqiang Liu <[email protected]>:

Quoted 72 lines

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")

---
v1->v2:
- free orgoptions in main func as suggested by Aurélien Aptel
- free mountpoint in acquire_mountpoint func as suggested by Aurélien Aptel

 mount.cifs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mount.cifs.c +10 -3
index ae7a899..656d353 100644
@@ -1891,7 +1891,10 @@ restore_privs:
                uid_t __attribute__((unused)) uignore = setfsuid(oldfsuid);
                gid_t __attribute__((unused)) gignore = setfsgid(oldfsgid);
        }
-
+
+       if (rc) {
+               free(*mountpointp);
+       }
        return rc;
 }
 
@@ -1994,8 +1997,10 @@ int main(int argc, char **argv)
 
        /* chdir into mountpoint as soon as possible */
        rc = acquire_mountpoint(&mountpoint);
-       if (rc)
+       if (rc) {
+               free(orgoptions);
                return rc;
+       }
 
        /*
         * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2019,8 @@ int main(int argc, char **argv)
                /* child */
                rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
                                        orig_dev, orgoptions);
+               free(orgoptions);
+               free(mountpoint);
                return rc;
        } else {
                /* parent */
@@ -2149,5 +2156,6 @@ mount_exit:
        }
        free(options);
        free(orgoptions);
+       free(mountpoint);
        return rc;
 }
--
Quoted 2 lines

2.7.4

Thanks for the patch! I will apply it to my github tree shortly.

--
Best regards,
Pavel Shilovsky

#3 lore raw
Quoted 3 lines · Pavel Shilovsky

Merged into "next" with one minor change - removed a trailing white space. Thanks.

That is ok. Thank you very much.

Quoted 91 lines

--
Best regards,
Pavel Shilovsky

вт, 6 авг. 2019 г. в 09:49, Pavel Shilovsky <[email protected]>:

Quoted 79 lines

пн, 5 авг. 2019 г. в 19:36, Zhiqiang Liu <[email protected]>:

Quoted 72 lines

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")

---
v1->v2:
- free orgoptions in main func as suggested by Aurélien Aptel
- free mountpoint in acquire_mountpoint func as suggested by Aurélien Aptel

 mount.cifs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mount.cifs.c +10 -3
index ae7a899..656d353 100644
@@ -1891,7 +1891,10 @@ restore_privs:
                uid_t __attribute__((unused)) uignore = setfsuid(oldfsuid);
                gid_t __attribute__((unused)) gignore = setfsgid(oldfsgid);
        }
-
+
+       if (rc) {
+               free(*mountpointp);
+       }
        return rc;
 }
 
@@ -1994,8 +1997,10 @@ int main(int argc, char **argv)
 
        /* chdir into mountpoint as soon as possible */
        rc = acquire_mountpoint(&mountpoint);
-       if (rc)
+       if (rc) {
+               free(orgoptions);
                return rc;
+       }
 
        /*
         * mount.cifs does privilege separation. Most of the code to handle
@@ -2014,6 +2019,8 @@ int main(int argc, char **argv)
                /* child */
                rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
                                        orig_dev, orgoptions);
+               free(orgoptions);
+               free(mountpoint);
                return rc;
        } else {
                /* parent */
@@ -2149,5 +2156,6 @@ mount_exit:
        }
        free(options);
        free(orgoptions);
+       free(mountpoint);
        return rc;
 }
--
Quoted 2 lines

2.7.4

Thanks for the patch! I will apply it to my github tree shortly.

Quoted 3 lines

--
Best regards,
Pavel Shilovsky

.