From patchwork Thu Aug 30 09:23:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Honggang LI X-Patchwork-Id: 10581385 X-Patchwork-Delegate: hal@mellanox.com 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 172D5920 for ; Thu, 30 Aug 2018 09:23:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 066A527CF9 for ; Thu, 30 Aug 2018 09:23:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ECBC927F8F; Thu, 30 Aug 2018 09:23:11 +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 96EEF27CF9 for ; Thu, 30 Aug 2018 09:23:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728024AbeH3NYW (ORCPT ); Thu, 30 Aug 2018 09:24:22 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:60936 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727979AbeH3NYW (ORCPT ); Thu, 30 Aug 2018 09:24:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4E01940241C3; Thu, 30 Aug 2018 09:23:10 +0000 (UTC) Received: from lhg.nay.redhat.com (unknown [10.66.129.124]) by smtp.corp.redhat.com (Postfix) with ESMTP id B3263202704E; Thu, 30 Aug 2018 09:23:08 +0000 (UTC) From: Honggang LI To: hal@dev.mellanox.co.il Cc: linux-rdma@vger.kernel.org, Honggang Li Subject: [PATCH] osm_console.c: fix resource leak in dump_portguid_parse Date: Thu, 30 Aug 2018 17:23:00 +0800 Message-Id: <20180830092300.10931-1-honli@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Thu, 30 Aug 2018 09:23:10 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Thu, 30 Aug 2018 09:23:10 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'honli@redhat.com' RCPT:'' Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Honggang Li Issue was found by Coverity. To observer this resource leak, just input this command in the console: OpenSM $ dump_portguid file /tmp/c file /tmp/d . then: ls -l /proc//fd You will find /tmp/c was not closed. Error: RESOURCE_LEAK (CWE-772): [#def5] opensm-3.3.20/opensm/osm_console.c:1682: alloc_fn: Storage is returned from allocation function "fopen". opensm-3.3.20/opensm/osm_console.c:1682: var_assign: Assigning: "output" = storage returned from "fopen(p_cmd, "w+")". opensm-3.3.20/opensm/osm_console.c:1682: overwrite_var: Overwriting "output" in "output = fopen(p_cmd, "w+")" leaks the storage that "output" points to. |# 1680| p_cmd = next_token(p_last); |# 1681| if (p_cmd) { |# 1682|-> output = fopen(p_cmd, "w+"); |# 1683| if (output == NULL) { |# 1684| fprintf(out, Signed-off-by: Honggang Li --- opensm/osm_console.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/opensm/osm_console.c b/opensm/osm_console.c index 9b91a42a..641bdcd5 100644 --- a/opensm/osm_console.c +++ b/opensm/osm_console.c @@ -1679,6 +1679,8 @@ static void dump_portguid_parse(char **p_last, osm_opensm_t * p_osm, FILE * out) if (strcmp(p_cmd, "file") == 0) { p_cmd = next_token(p_last); if (p_cmd) { + if (output != out) + fclose(output); output = fopen(p_cmd, "w+"); if (output == NULL) { fprintf(out,