From patchwork Tue Aug 6 02:35:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhiqiang Liu X-Patchwork-Id: 11078147 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 47E2613A4 for ; Tue, 6 Aug 2019 02:36:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2C73622B26 for ; Tue, 6 Aug 2019 02:36:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1A08C2890A; Tue, 6 Aug 2019 02:36:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CD3122B26 for ; Tue, 6 Aug 2019 02:36:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731259AbfHFCga (ORCPT ); Mon, 5 Aug 2019 22:36:30 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:57222 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729334AbfHFCga (ORCPT ); Mon, 5 Aug 2019 22:36:30 -0400 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 76C3C323F3DAD912D613; Tue, 6 Aug 2019 10:36:28 +0800 (CST) Received: from [127.0.0.1] (10.184.225.177) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.439.0; Tue, 6 Aug 2019 10:36:19 +0800 To: , =?utf-8?q?Aur=C3=A9lien_Aptel?= , , Steve French , , , , , CC: , Mingfangsen , zhangsaisai From: Zhiqiang Liu Subject: [PATCH cifs-utils v2] mount.cifs.c: fix memory leaks in main func Message-ID: <0f780b18-0b1c-e2ff-31b1-1d697becd142@huawei.com> Date: Tue, 6 Aug 2019 10:35:29 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 X-Originating-IP: [10.184.225.177] X-CFilter-Loop: Reflected Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jiawen Liu 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 Reported-by: Jin Du Reviewed-by: Saisai Zhang Reviewed-by: Aurélien Aptel --- 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; }