Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp875981pxk; Thu, 17 Sep 2020 20:02:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJ2FMyBc/5M9jtIQ90OCuF2i3luyPWp45TPn84TzDvVuTDDYmnuGYNMIfK89IpDtVP4TJI X-Received: by 2002:a17:906:ca4f:: with SMTP id jx15mr33414725ejb.454.1600398135719; Thu, 17 Sep 2020 20:02:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398135; cv=none; d=google.com; s=arc-20160816; b=SVIqT+crjZ8noHMwFMCkPWh5JjH+6F+YGy5YyyqqsdDBXBdiGnaDzUREHWQtyrSm+M KZeDngf74ungZlgFgR3k/oeDr1XHpLIEsMmD7D8lThY6za9YKwdW/nbdMNBTr8JtpLbr Cwv6RIKLVk0TN7ShllczvjBM7sup7CnFtZxfZHECREPJIXji/wualPNz0GzQV6ffpkIV RZkfW8Y2NV6y6NGMvcyXaDC+Nk9leH5HFa/M3OIp+ZNbx6CPMKUjE+llcz765NxYxe7E c9LATlNGmIRywb2YLWUBxFzH8YUEkBJMq11eDG4VYoyCn+hpyCm/4GpvA9GhpXwkrl34 EFSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JMeihtiiq6ixqNRZM47zc13v3tJ71X2MM+6a2sFnv2c=; b=T+3uiPIYaU/RopUrd0poBSbafkRfkhKKBg+CzvYJqYdJA5Y56qB5Eb8y0CFQGum64n 9wLIW/f8XCtR0X2pFSKxC63JbWy31p3DT/14q47302KENZ5alnm0yizE9wfbzqR7TEZs nHJ5JCRfOIbCoJRq1sAKUUt8vpNwEVEAhJWNaFoczmXZcT1WslbYjsLrTRMZ8LbT3YOm F8tyu7v6rZoHUK7VNAksIoPuGsHCWWQNTvtJeqrjfWybDvjm+VspBypoLpdxRNBqkE1A WN/iKIlGgc6i0ugCHQccVM3yvH+SDs/1c1N24/PLoUnnFvlaUOL6XrA35sAJ0CglsPMM 4YXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dm7dCAby; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a4si1060808edt.550.2020.09.17.20.01.52; Thu, 17 Sep 2020 20:02:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dm7dCAby; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729515AbgIRC5y (ORCPT + 99 others); Thu, 17 Sep 2020 22:57:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:55740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727751AbgIRCGZ (ORCPT ); Thu, 17 Sep 2020 22:06:25 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3ECFC23977; Fri, 18 Sep 2020 02:06:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394784; bh=v1AshLKQV0xOuh+08E+uQcfHlVlSimqpQhxYu6ZqGQI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dm7dCAbyRxk1BKj9PjEIred0wbXSeujWvVpJZSssXeftOzge1gXjR7Kgwh1rCkZKP 1EjPXr8w7C6oWgTI0Zv9lcDfFtFpTWT5u+kZu9fZYUo15hM926AN+tO1u/ACaK6SOe xJQFDd2R0YNAngz+5zyPXDAGScjb85LoTAypHo80= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ian Rogers , Jiri Olsa , Alexander Shishkin , Mark Rutland , Namhyung Kim , Peter Zijlstra , Stephane Eranian , clang-built-linux@googlegroups.com, Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH AUTOSEL 5.4 256/330] perf mem2node: Avoid double free related to realloc Date: Thu, 17 Sep 2020 21:59:56 -0400 Message-Id: <20200918020110.2063155-256-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ian Rogers [ Upstream commit 266150c94c69429cf6d18e130237224a047f5061 ] Realloc of size zero is a free not an error, avoid this causing a double free. Caught by clang's address sanitizer: ==2634==ERROR: AddressSanitizer: attempting double-free on 0x6020000015f0 in thread T0: #0 0x5649659297fd in free llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:123:3 #1 0x5649659e9251 in __zfree tools/lib/zalloc.c:13:2 #2 0x564965c0f92c in mem2node__exit tools/perf/util/mem2node.c:114:2 #3 0x564965a08b4c in perf_c2c__report tools/perf/builtin-c2c.c:2867:2 #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10 #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11 #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8 #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2 #8 0x564965942e41 in main tools/perf/perf.c:538:3 0x6020000015f0 is located 0 bytes inside of 1-byte region [0x6020000015f0,0x6020000015f1) freed by thread T0 here: #0 0x564965929da3 in realloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0x564965c0f55e in mem2node__init tools/perf/util/mem2node.c:97:16 #2 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8 #3 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10 #4 0x564965944348 in run_builtin tools/perf/perf.c:312:11 #5 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8 #6 0x5649659440c4 in run_argv tools/perf/perf.c:408:2 #7 0x564965942e41 in main tools/perf/perf.c:538:3 previously allocated by thread T0 here: #0 0x564965929c42 in calloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3 #1 0x5649659e9220 in zalloc tools/lib/zalloc.c:8:9 #2 0x564965c0f32d in mem2node__init tools/perf/util/mem2node.c:61:12 #3 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8 #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10 #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11 #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8 #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2 #8 0x564965942e41 in main tools/perf/perf.c:538:3 v2: add a WARN_ON_ONCE when the free condition arises. Signed-off-by: Ian Rogers Acked-by: Jiri Olsa Cc: Alexander Shishkin Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Cc: clang-built-linux@googlegroups.com Link: http://lore.kernel.org/lkml/20200320182347.87675-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/mem2node.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/mem2node.c b/tools/perf/util/mem2node.c index 797d86a1ab095..c84f5841c7abd 100644 --- a/tools/perf/util/mem2node.c +++ b/tools/perf/util/mem2node.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -95,7 +96,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env) /* Cut unused entries, due to merging. */ tmp_entries = realloc(entries, sizeof(*entries) * j); - if (tmp_entries) + if (tmp_entries || WARN_ON_ONCE(j == 0)) entries = tmp_entries; for (i = 0; i < j; i++) { -- 2.25.1