Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3215101ybg; Mon, 28 Oct 2019 09:13:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxM8/ooxS8ULylAI63+sOhIi8HTlF9mTjtCzuPitUzJiwq7W+eqOb+F8XIT5K1yRKtr7Zs5 X-Received: by 2002:a05:6402:386:: with SMTP id o6mr20900617edv.148.1572279213677; Mon, 28 Oct 2019 09:13:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572279213; cv=none; d=google.com; s=arc-20160816; b=buCN0c5lxmuTOFhQdYMHyML6D88fwPsA7PwYRkQpCbUabdVujU8edaKUcIzPZ7ZgeZ 2kQYP76lybREENBkDEZVDnyz7x8hDY2Jb9WfczSlxAty71taw5vbQ1y4uM281Vux5ZMI jdy1ksgs7BHD1J4H/jiWPWkKA7ywH8gP65K28MP3NK0yaTbli4NiWVoSePsSCbU3zzMH 7afmHx+vwWfGeX7hdd9tNlISRuodtrfu5qoWsCgJs0umJxcBWfHmyEt9FmiLwnx+k0hn ybTVb6m9mwg6FoNgi+K2plEY4sd35QdtOL/FaOFbeXlorXvEhwJYEdhm0W/nnlGvEHQD JupQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:mime-version:user-agent:date:message-id:cc:to :subject:from; bh=Rem3kyM9rf8Bx8AJABwsJqrZNa2XpAOaiIk8g724Z4I=; b=drlf4G13OaTWh+aImk+vDFNiM3xhvksntlaRdiCuz2HE+maw9BPjUm0+TBUOYEBnZv Oz2y6TcQlngRLQ+IWWDsGyTKPxj2vhFhWmkJM+q7d6M9FA1k0hKZGm/dpYC4TrPD7V6a JBvEYygkO41WYdP1TElzlhF8++kZlIpn619TZa/Q6bzsb5kdkUSE2W4Svm7L51CDhn7L 0wh0fffZHDAb0G5aC0ge84r2eZc4P3C8100IburyuvjOm797r2FxbtT3v1Odu/5aRynS V/475IpH9r2vpqZL+PEpMBKZ0GAFRJPzlO93sPJJNTTzNLjZZEAvzAtqPcQsSGvblte6 ZMqw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 7si8870389edx.33.2019.10.28.09.12.57; Mon, 28 Oct 2019 09:13:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731954AbfJ1GrP (ORCPT + 99 others); Mon, 28 Oct 2019 02:47:15 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:43042 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730477AbfJ1GrP (ORCPT ); Mon, 28 Oct 2019 02:47:15 -0400 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 63B1A55C1466A571EBB9; Mon, 28 Oct 2019 14:47:13 +0800 (CST) Received: from [127.0.0.1] (10.177.251.225) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.439.0; Mon, 28 Oct 2019 14:47:03 +0800 From: Yunfeng Ye Subject: [PATCH] perf kmem: Fix memory leak in __cmd_record() To: , , , , , , CC: "linux-kernel@vger.kernel.org" , "hushiyuan@huawei.com" , "linfeilong@huawei.com" Message-ID: <81e3d338-dbf3-341b-431d-27bb51996e46@huawei.com> Date: Mon, 28 Oct 2019 14:46:35 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.251.225] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are memory leaks in __cmd_record() found by visual inspection. calloc() and strdup() are used to allocate memory for rec_argv pointer array and argv, which should be freed before the function returns. In addition, checking whether strdup() is success or not, if failure, then go to the failure path to free memory and return the function. Fixes: ba77c9e11111 ("perf: Add 'perf kmem' tool") Signed-off-by: Yunfeng Ye --- tools/perf/builtin-kmem.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 9661671cc26e..6a62acfc9470 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -1847,6 +1847,7 @@ static int __cmd_record(int argc, const char **argv) }; unsigned int rec_argc, i, j; const char **rec_argv; + int ret = -ENOMEM; rec_argc = ARRAY_SIZE(record_args) + argc - 1; if (kmem_slab) @@ -1873,10 +1874,20 @@ static int __cmd_record(int argc, const char **argv) rec_argv[i] = strdup(page_events[j]); } - for (j = 1; j < (unsigned int)argc; j++, i++) - rec_argv[i] = argv[j]; + for (j = 0; j < i; j++) + if (!rec_argv[j]) /* check strdup success or not */ + goto out; + + rec_argc = i; + for (j = 1; j < (unsigned int)argc; j++, rec_argc++) + rec_argv[rec_argc] = argv[j]; - return cmd_record(i, rec_argv); + ret = cmd_record(rec_argc, rec_argv); +out: + for (i--; (int)i >= 0; i--) + free((void *)rec_argv[i]); + free(rec_argv); + return ret; } static int kmem_config(const char *var, const char *value, void *cb __maybe_unused) -- 2.7.4