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: 1142542 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-cifs-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 462f0J2m02z9sBF for ; Tue, 6 Aug 2019 12:36:31 +1000 (AEST) 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 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; }