From patchwork Fri Nov 6 09:26:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiqiang Liu X-Patchwork-Id: 11886483 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 555F316C1 for ; Fri, 6 Nov 2020 09:26:35 +0000 (UTC) Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 24F7D208B3 for ; Fri, 6 Nov 2020 09:26:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 24F7D208B3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-nvdimm-bounces@lists.01.org Received: from ml01.vlan13.01.org (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 821F41674C0EE; Fri, 6 Nov 2020 01:26:34 -0800 (PST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=45.249.212.32; helo=szxga06-in.huawei.com; envelope-from=liuzhiqiang26@huawei.com; receiver= Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 660B31673C07E for ; Fri, 6 Nov 2020 01:26:32 -0800 (PST) Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4CSFPm54vvzhgxr for ; Fri, 6 Nov 2020 17:26:20 +0800 (CST) Received: from [127.0.0.1] (10.174.176.238) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.487.0; Fri, 6 Nov 2020 17:26:13 +0800 Subject: [ndctl PATCH 4/8] dimm: fix potential fd leakage in dimm_action() To: References: From: Zhiqiang Liu Message-ID: <1ca17cf4-a5fd-786d-fa50-8ed09ccd55e4@huawei.com> Date: Fri, 6 Nov 2020 17:26:12 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-Originating-IP: [10.174.176.238] X-CFilter-Loop: Reflected Message-ID-Hash: K53J3EMKFP456D6ANRJ5DNDS2VZZQIV2 X-Message-ID-Hash: K53J3EMKFP456D6ANRJ5DNDS2VZZQIV2 X-MailFrom: liuzhiqiang26@huawei.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: linux-nvdimm@lists.01.org, linfeilong , liuzhiqiang26@huawei.com X-Mailman-Version: 3.1.1 Precedence: list List-Id: "Linux-nvdimm developer list." Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: In dimm_action(), actx.f_out and actx.f_in may be set by calling fopen(). If exceptions occur, we will directly goto out tag. However, we did not close actx.f_out|actx.f_in in out tag, which will cause fd leakage. Signed-off-by: Zhiqiang Liu Acked-by: Jeff Moyer --- ndctl/dimm.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ndctl/dimm.c b/ndctl/dimm.c index 90eb0b8..2f52cda 100644 --- a/ndctl/dimm.c +++ b/ndctl/dimm.c @@ -1352,7 +1352,7 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx, fprintf(stderr, "failed to open: %s: (%s)\n", param.infile, strerror(errno)); rc = -errno; - goto out; + goto out_close_fout; } } @@ -1371,7 +1371,7 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx, fprintf(stderr, "'%s' is not a valid label version\n", param.labelversion); rc = -EINVAL; - goto out; + goto out_close_fin_fout; } rc = 0; @@ -1423,12 +1423,14 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx, util_display_json_array(actx.f_out, actx.jdimms, flags); } - if (actx.f_out != stdout) - fclose(actx.f_out); - + out_close_fin_fout: if (actx.f_in != stdin) fclose(actx.f_in); + out_close_fout: + if (actx.f_out != stdout) + fclose(actx.f_out); + out: /* * count if some actions succeeded, 0 if none were attempted,