From: Zhiqiang Liu <liuzhiqiang26@huawei.com> Date: Thu, 5 Nov 2020 09:24:12 +0000 (+0800) Subject: mount.fuse.c: fix potential memory leak in main func X-Git-Tag: fuse-3.10.1~7 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=5670dde86ce311bfc68b0398cda545b3230787e8;p=qemu-gpiodev%2Flibfuse.git mount.fuse.c: fix potential memory leak in main func In mount.fuse.c, there are several memory leak problems in main func. For example, setuid_name is allocated by calling xstrdup func, however it is not freed before calling execl func. Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com> Signed-off-by: Haotian Li <lihaotian9@huawei.com> --- diff --git a/util/mount.fuse.c b/util/mount.fuse.c index 1fc98f1..f7e60c7 100644 --- a/util/mount.fuse.c +++ b/util/mount.fuse.c @@ -233,6 +233,7 @@ int main(int argc, char *argv[]) { char *type = NULL; char *source; + char *dup_source = NULL; const char *mountpoint; char *basename; char *options = NULL; @@ -243,6 +244,7 @@ int main(int argc, char *argv[]) int suid = 1; int pass_fuse_fd = 0; int drop_privileges = 0; + char *dev_fd_mountpoint = NULL; progname = argv[0]; basename = strrchr(argv[0], '/'); @@ -340,6 +342,7 @@ int main(int argc, char *argv[]) } opt = strtok(NULL, ","); } + free(opts); } } @@ -360,7 +363,8 @@ int main(int argc, char *argv[]) if (!type) { if (source) { - type = xstrdup(source); + dup_source = xstrdup(source); + type = dup_source; source = strchr(type, '#'); if (source) *source++ = '\0'; @@ -410,7 +414,7 @@ int main(int argc, char *argv[]) if (pass_fuse_fd) { int fuse_fd = prepare_fuse_fd(mountpoint, type, options); - char *dev_fd_mountpoint = xrealloc(NULL, 20); + dev_fd_mountpoint = xrealloc(NULL, 20); snprintf(dev_fd_mountpoint, 20, "/dev/fd/%u", fuse_fd); mountpoint = dev_fd_mountpoint; } @@ -429,6 +433,11 @@ int main(int argc, char *argv[]) add_arg(&command, options); } + free(options); + free(dev_fd_mountpoint); + free(dup_source); + free(setuid_name); + execl("/bin/sh", "/bin/sh", "-c", command, NULL); fprintf(stderr, "%s: failed to execute /bin/sh: %s\n", progname, strerror(errno));