Linux-CIFS Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func
@ 2019-08-06  2:35 Zhiqiang Liu
  2019-08-06 16:49 ` Pavel Shilovsky
  0 siblings, 1 reply; 4+ messages in thread
From: Zhiqiang Liu @ 2019-08-06  2:35 UTC (permalink / raw)
  To: linux-cifs, Aurélien Aptel, liujiawen10, Steve French,
	pshilov, lsahlber, kdsouza, ab, palcantara
  Cc: dujin1, Mingfangsen, zhangsaisai

[-- Warning: decoded text below may be mangled --]
[-- Attachment #0: Type: text/plain; charset="gb18030", Size: 1958 bytes --]

From: Jiawen Liu <liujiawen10@huawei.com>

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")
Signed-off-by: Jiawen Liu <liujiawen10@huawei.com>
Reported-by: Jin Du <dujin1@huawei.com>
Reviewed-by: Saisai Zhang <zhangsaisai@huawei.com>
Reviewed-by: Aur¨¦lien Aptel <aaptel@suse.com>
---
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(-)

diff --git a/mount.cifs.c b/mount.cifs.c
index ae7a899..656d353 100644
--- a/mount.cifs.c
+++ b/mount.cifs.c
@@ -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;
 }
-- 
2.7.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func
  2019-08-06  2:35 [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func Zhiqiang Liu
@ 2019-08-06 16:49 ` Pavel Shilovsky
  2019-08-07 21:44   ` Pavel Shilovsky
  0 siblings, 1 reply; 4+ messages in thread
From: Pavel Shilovsky @ 2019-08-06 16:49 UTC (permalink / raw)
  To: Zhiqiang Liu
  Cc: linux-cifs, Aurélien Aptel, liujiawen10, Steve French,
	Pavel Shilovskiy, Ronnie Sahlberg, Kenneth Dsouza,
	Alexander Bokovoy, Paulo Alcantara, dujin1, Mingfangsen,
	zhangsaisai

пн, 5 авг. 2019 г. в 19:36, Zhiqiang Liu <liuzhiqiang26@huawei.com>:
>
> From: Jiawen Liu <liujiawen10@huawei.com>
>
> 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")
> Signed-off-by: Jiawen Liu <liujiawen10@huawei.com>
> Reported-by: Jin Du <dujin1@huawei.com>
> Reviewed-by: Saisai Zhang <zhangsaisai@huawei.com>
> Reviewed-by: Aurélien Aptel <aaptel@suse.com>
> ---
> 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(-)
>
> diff --git a/mount.cifs.c b/mount.cifs.c
> index ae7a899..656d353 100644
> --- a/mount.cifs.c
> +++ b/mount.cifs.c
> @@ -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;
>  }
> --
> 2.7.4
>

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

--
Best regards,
Pavel Shilovsky

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func
  2019-08-06 16:49 ` Pavel Shilovsky
@ 2019-08-07 21:44   ` Pavel Shilovsky
  2019-08-08  1:06     ` Zhiqiang Liu
  0 siblings, 1 reply; 4+ messages in thread
From: Pavel Shilovsky @ 2019-08-07 21:44 UTC (permalink / raw)
  To: Zhiqiang Liu
  Cc: linux-cifs, Aurélien Aptel, liujiawen10, Steve French,
	Pavel Shilovskiy, Ronnie Sahlberg, Kenneth Dsouza,
	Alexander Bokovoy, Paulo Alcantara, dujin1, Mingfangsen,
	zhangsaisai

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

--
Best regards,
Pavel Shilovsky

вт, 6 авг. 2019 г. в 09:49, Pavel Shilovsky <piastryyy@gmail.com>:

>
> пн, 5 авг. 2019 г. в 19:36, Zhiqiang Liu <liuzhiqiang26@huawei.com>:
> >
> > From: Jiawen Liu <liujiawen10@huawei.com>
> >
> > 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")
> > Signed-off-by: Jiawen Liu <liujiawen10@huawei.com>
> > Reported-by: Jin Du <dujin1@huawei.com>
> > Reviewed-by: Saisai Zhang <zhangsaisai@huawei.com>
> > Reviewed-by: Aurélien Aptel <aaptel@suse.com>
> > ---
> > 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(-)
> >
> > diff --git a/mount.cifs.c b/mount.cifs.c
> > index ae7a899..656d353 100644
> > --- a/mount.cifs.c
> > +++ b/mount.cifs.c
> > @@ -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;
> >  }
> > --
> > 2.7.4
> >
>
> Thanks for the patch! I will apply it to my github tree shortly.
>
> --
> Best regards,
> Pavel Shilovsky

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func
  2019-08-07 21:44   ` Pavel Shilovsky
@ 2019-08-08  1:06     ` Zhiqiang Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Zhiqiang Liu @ 2019-08-08  1:06 UTC (permalink / raw)
  To: Pavel Shilovsky
  Cc: linux-cifs, Aurélien Aptel, liujiawen10, Steve French,
	Pavel Shilovskiy, Ronnie Sahlberg, Kenneth Dsouza,
	Alexander Bokovoy, Paulo Alcantara, dujin1, Mingfangsen,
	zhangsaisai

On 2019/8/8 5:44, Pavel Shilovsky wrote:
> Merged into "next" with one minor change - removed a trailing white
> space. Thanks.
> 
That is ok. Thank you very much.

> --
> Best regards,
> Pavel Shilovsky
> 
> вт, 6 авг. 2019 г. в 09:49, Pavel Shilovsky <piastryyy@gmail.com>:
> 
>>
>> пн, 5 авг. 2019 г. в 19:36, Zhiqiang Liu <liuzhiqiang26@huawei.com>:
>>>
>>> From: Jiawen Liu <liujiawen10@huawei.com>
>>>
>>> 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")
>>> Signed-off-by: Jiawen Liu <liujiawen10@huawei.com>
>>> Reported-by: Jin Du <dujin1@huawei.com>
>>> Reviewed-by: Saisai Zhang <zhangsaisai@huawei.com>
>>> Reviewed-by: Aurélien Aptel <aaptel@suse.com>
>>> ---
>>> 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(-)
>>>
>>> diff --git a/mount.cifs.c b/mount.cifs.c
>>> index ae7a899..656d353 100644
>>> --- a/mount.cifs.c
>>> +++ b/mount.cifs.c
>>> @@ -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;
>>>  }
>>> --
>>> 2.7.4
>>>
>>
>> Thanks for the patch! I will apply it to my github tree shortly.
>>
>> --
>> Best regards,
>> Pavel Shilovsky
> 
> .
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-06  2:35 [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func Zhiqiang Liu
2019-08-06 16:49 ` Pavel Shilovsky
2019-08-07 21:44   ` Pavel Shilovsky
2019-08-08  1:06     ` Zhiqiang Liu

Linux-CIFS Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-cifs/0 linux-cifs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-cifs linux-cifs/ https://lore.kernel.org/linux-cifs \
		linux-cifs@vger.kernel.org linux-cifs@archiver.kernel.org
	public-inbox-index linux-cifs


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-cifs


AGPL code for this site: git clone https://public-inbox.org/ public-inbox