From patchwork Thu Aug 1 02:02:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhiqiang Liu X-Patchwork-Id: 11069919 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 64E6A1395 for ; Thu, 1 Aug 2019 02:03:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5033128390 for ; Thu, 1 Aug 2019 02:03:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 43E6B283A8; Thu, 1 Aug 2019 02:03:06 +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 E281728396 for ; Thu, 1 Aug 2019 02:02:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726334AbfHACC6 (ORCPT ); Wed, 31 Jul 2019 22:02:58 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:3286 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726185AbfHACC6 (ORCPT ); Wed, 31 Jul 2019 22:02:58 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id AF281736122E02F675B2; Thu, 1 Aug 2019 10:02:55 +0800 (CST) Received: from [127.0.0.1] (10.184.225.177) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Thu, 1 Aug 2019 10:02:43 +0800 To: , =?utf-8?q?Aur=C3=A9lien_Aptel?= , , , , , , , , From: Zhiqiang Liu Subject: [PATCH cifs-utils] mount.cifs.c: fix memory leaks in main func CC: , Mingfangsen , zhangsaisai Message-ID: Date: Thu, 1 Aug 2019 10:02:24 +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 --- mount.cifs.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mount.cifs.c b/mount.cifs.c index ae7a899..029f01a 100644 --- a/mount.cifs.c +++ b/mount.cifs.c @@ -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; }