Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp7312729ybh; Thu, 8 Aug 2019 13:35:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqz4X+lHKmr5gVneck99z7mWZncyPPA98x6NoGIK2yL+vgQLMiDigGiqJKG5Xuwzwkz0rN5+ X-Received: by 2002:a63:7d05:: with SMTP id y5mr14530030pgc.425.1565296555888; Thu, 08 Aug 2019 13:35:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565296555; cv=none; d=google.com; s=arc-20160816; b=kygxIjX8nA2n50DBydwPT5Fyd12PYFo4wHkuwyMHguYIPVPQxMKzxl0uiHqEnZO+Av mgUp+2ZXUTzXrV07vRcND6pJzKGrtEzGw4Vj52kgU5xQFqR0Z+Dv/biHMrMs8w/PQdXa 7ksPJHK1RBaZlham7/4AdoP04UP4iGQbrt9e9lXZneap9FTz/pGt0WoOgObyEyD2WFO+ fclyIUbFHjhR+coI11b/4M8bwtDinK9yJcbrM8ro7p7vwQfbDfKQqf3To+L8rT6PA8uk iN4ipK5Ea4IkILUYRyaPnHNG8c8qMIh49WQpPGJOSeJ5xOrS1F0E3gokBiIiMGDLx4jN Mt2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:dkim-signature:dkim-filter; bh=rdLSYTltPvgVpgyaIOKiiracGRusZLtBYcxcQ7fm2V8=; b=slVSY4Kf6uMWVGazZZ5lWcvEIuQq8OqvRq6cZcwwmcqy3bUCsu0LlKOcKiLSF1RKKT /1Wz+Mpt7s0sjXP9AwmCsLfJt90hOPjslON93rwKPQnTyrl+SFiBeyKvRdN1lzubiLcq 8TPe/Ok9P1Y/D1ie8vszmOC/BVLgRM20Lqh5oPYyzUB7nem62kWJZi0asGGOwud5o0df Xpz8CDNwYWtrjT3Fr7La4I7bAYVSNNVE6lfr09O2StcDnUiIpluf6U0SMQW8sOkAUI3N j0Vza+GVDHpKvUOgYuPCl3RELwj7WWC++8xpKQUmWB3lwrhq/riFQWjVpLLQkXSxNFqr BFSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2019071901 header.b="GyFq/1P/"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p27si48554603pli.411.2019.08.08.13.35.40; Thu, 08 Aug 2019 13:35:55 -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; dkim=pass header.i=@zytor.com header.s=2019071901 header.b="GyFq/1P/"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404711AbfHHUTf (ORCPT + 99 others); Thu, 8 Aug 2019 16:19:35 -0400 Received: from terminus.zytor.com ([198.137.202.136]:44911 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404590AbfHHUTf (ORCPT ); Thu, 8 Aug 2019 16:19:35 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x78KJLJD3321297 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Thu, 8 Aug 2019 13:19:21 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 terminus.zytor.com x78KJLJD3321297 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2019071901; t=1565295561; bh=rdLSYTltPvgVpgyaIOKiiracGRusZLtBYcxcQ7fm2V8=; h=Date:From:Cc:Reply-To:In-Reply-To:References:To:Subject:From; b=GyFq/1P/T5PH3rG6NsY5SGXkEL0fbvHNlIksWDeuTaHCAbPMVkx9K2OfdtTUEJ8HG Wzc/D+rarV2r/hzxmLvQQWXhkzrj9p3wOBspmPotqzDMSZl1h8+OkePVrZvEY+1B7f qMOfhiy5o2TkpfbqbJ8VeDtyq+xFBZeIE3mVAiVBPx12HVOtlkN438smH2ubGA5bea ajqon4KQo1UMVTxzCl95mVgqvB+cAt87S3NgWgvHWt50naJFPF97wmTNdK3/zMpfNQ iGbtcPY5PEKh230rC+fvVNG4iClZT/nSO9UrCDVnHlDEx2XBVatew8DJKu97VWLVbi 40QgdEjPxwZjQ== Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x78KJKPh3321280; Thu, 8 Aug 2019 13:19:20 -0700 Date: Thu, 8 Aug 2019 13:19:20 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for He Zhe Message-ID: Cc: namhyung@kernel.org, alexey.budankov@linux.intel.com, zhe.he@windriver.com, hpa@zytor.com, acme@redhat.com, peterz@infradead.org, mingo@kernel.org, linux-kernel@vger.kernel.org, alexander.shishkin@linux.intel.com, eranian@google.com, kan.liang@linux.intel.com, jolsa@redhat.com, tglx@linutronix.de Reply-To: alexander.shishkin@linux.intel.com, eranian@google.com, kan.liang@linux.intel.com, jolsa@redhat.com, tglx@linutronix.de, hpa@zytor.com, acme@redhat.com, peterz@infradead.org, mingo@kernel.org, linux-kernel@vger.kernel.org, zhe.he@windriver.com, namhyung@kernel.org, alexey.budankov@linux.intel.com In-Reply-To: <1564734592-15624-2-git-send-email-zhe.he@windriver.com> References: <1564734592-15624-2-git-send-email-zhe.he@windriver.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] perf cpumap: Fix writing to illegal memory in handling cpumap mask Git-Commit-ID: 5f5e25f1c7933a6e1673515c0b1d5acd82fea1ed X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-0.3 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_96_Q,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 5f5e25f1c7933a6e1673515c0b1d5acd82fea1ed Gitweb: https://git.kernel.org/tip/5f5e25f1c7933a6e1673515c0b1d5acd82fea1ed Author: He Zhe AuthorDate: Fri, 2 Aug 2019 16:29:52 +0800 Committer: Arnaldo Carvalho de Melo CommitDate: Thu, 8 Aug 2019 15:41:10 -0300 perf cpumap: Fix writing to illegal memory in handling cpumap mask cpu_map__snprint_mask() would write to illegal memory pointed by zalloc(0) when there is only one cpu. This patch fixes the calculation and adds sanity check against the input parameters. Signed-off-by: He Zhe Cc: Alexander Shishkin Cc: Alexey Budankov Cc: Jiri Olsa Cc: Kan Liang Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Fixes: 4400ac8a9a90 ("perf cpumap: Introduce cpu_map__snprint_mask()") Link: http://lkml.kernel.org/r/1564734592-15624-2-git-send-email-zhe.he@windriver.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/cpumap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 3acfbe34ebaf..39cce66b4ebc 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c @@ -751,7 +751,10 @@ size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size) unsigned char *bitmap; int last_cpu = cpu_map__cpu(map, map->nr - 1); - bitmap = zalloc((last_cpu + 7) / 8); + if (buf == NULL) + return 0; + + bitmap = zalloc(last_cpu / 8 + 1); if (bitmap == NULL) { buf[0] = '\0'; return 0;