From patchwork Fri Nov 6 09:25:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiqiang Liu X-Patchwork-Id: 11886481 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 D3A23697 for ; Fri, 6 Nov 2020 09:25:46 +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 A788A2087E for ; Fri, 6 Nov 2020 09:25:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A788A2087E 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 54A421673C07E; Fri, 6 Nov 2020 01:25:46 -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 3FF90163F9794 for ; Fri, 6 Nov 2020 01:25:43 -0800 (PST) Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4CSFNy1pp3zhdbZ for ; Fri, 6 Nov 2020 17:25:38 +0800 (CST) Received: from [127.0.0.1] (10.174.176.238) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Fri, 6 Nov 2020 17:25:32 +0800 Subject: [ndctl PATCH 3/8] libdaxctl: fix memory leakage in add_dax_region() To: References: From: Zhiqiang Liu Message-ID: <7ae72048-0c26-9da2-41a9-c1b63a8db117@huawei.com> Date: Fri, 6 Nov 2020 17:25:32 +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: CIDKJXRYSFXY6YUOGO5YMZFIMKPNPPYX X-Message-ID-Hash: CIDKJXRYSFXY6YUOGO5YMZFIMKPNPPYX 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 add_dax_region(), region->devname is allocated by calling strdup(), which may return NULL. So, we need to check whether region->devname is NULL, and free region->devname in err_read tag. Signed-off-by: Zhiqiang Liu Acked-by: Jeff Moyer --- daxctl/lib/libdaxctl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c index ee4a069..d841b78 100644 --- a/daxctl/lib/libdaxctl.c +++ b/daxctl/lib/libdaxctl.c @@ -287,6 +287,8 @@ static struct daxctl_region *add_dax_region(void *parent, int id, region->refcount = 1; list_head_init(®ion->devices); region->devname = strdup(devpath_to_devname(base)); + if (!region->devname) + goto err_read; sprintf(path, "%s/%s/size", base, attrs); if (sysfs_read_attr(ctx, path, buf) == 0) @@ -314,6 +316,7 @@ static struct daxctl_region *add_dax_region(void *parent, int id, err_read: free(region->region_buf); free(region->region_path); + free(region->devname); free(region); err_region: free(path);