Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5213941ybl; Tue, 27 Aug 2019 00:54:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqxDiGs8m3vaAEpEc7KBVm952uaoXFV9T27eWMBzBy68ATaazWsPjiwGaTWn4UsGXUPbuUcS X-Received: by 2002:a63:2026:: with SMTP id g38mr19729927pgg.172.1566892477790; Tue, 27 Aug 2019 00:54:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566892477; cv=none; d=google.com; s=arc-20160816; b=B/5UXlosYUxD8GArJiMDWuxBWoVZhzZrPG3rFgppqq18lgkQPz7WNT6nncqV9vmQdS BPUfop5M1VuMIzEAoqC7YvBNcsbhpFrcG3Afy7GI04bh40ROg5DpdeTNgpoyVA0Ew994 VBUyekoDO/a5wVxi96x35ocDGNjgeePlfTqLSku7lO2qi3b7KQBJa2Tnmu4PMpyMdEQi NpIVrMo+NpuMv+et8+SoLs9ksjDB3xLM2TLiD72KKnJqkmjeN9VOqCgsQtrLRgH6+Y5t 70aHWlyRShDN6RbM7Ga2yyX5g1EhGoPGKX5rxCpHd40c0NUPSoVKrdVEeSeOCz/5k8gu LJcA== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2wHcfNjcJE8jduAceWqGTsO1o1sVuNU0qELldRecb8s=; b=RJJ78lR5G9aYw6ONOp4mmjQ6p8XxBqQQtNNF2JWiq9KR0/rXUzLrw7oFwJFuvEftAC zH6ZMbkjbE2MJrpx/mE7nklRW7iIHmWfuviNmn9QAES83358Q+CeQQFyeZ4/j/Lt67rp dkkW1THM2W3GFEb1fc5BuUXMU/FDEY7LXq9Qjkr+KOK6XbNlmSlMuk8toSactBu+/z47 eBgKafOcy20cLzf4r1gtrxKvzxDrhnlYI2f47p0dY9Tw2mDy/X8k53Fz4TwkDuhkhNuC YPeWYyHB2HTS8YecDrFo5LiPV3murCsCq0+CR9k8yJVFz5/vkqmcItPrc8k1O7ZKDTnf lBxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Bj2R8Qfe; 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 w9si1913425pjn.67.2019.08.27.00.54.22; Tue, 27 Aug 2019 00:54:37 -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=@kernel.org header.s=default header.b=Bj2R8Qfe; 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 S1729671AbfH0HxA (ORCPT + 99 others); Tue, 27 Aug 2019 03:53:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:44384 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729632AbfH0Hwz (ORCPT ); Tue, 27 Aug 2019 03:52:55 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 28C722173E; Tue, 27 Aug 2019 07:52:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566892374; bh=ufF3t1CWQkT46mQdMzFeTuoDkBirTgFqwuYqOxLy9ig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bj2R8QfejpLT9o0iRAFt0TUY7ORlgGUuTmdnGPm2QyGwpBdHW9YR1GT56Dhe+o4bZ xPVS6PdjWD1k7ZwTHxWwZcfAcnY28EaDVnPevyfrA2a46lz9OgboPsraNDxmgSEpGX L0Jp/1AXeVXJ7Fon/Febc1iX5bITt0ORu0YwQgIs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, He Zhe , Alexander Shishkin , Alexey Budankov , Jiri Olsa , Kan Liang , Namhyung Kim , Peter Zijlstra , Stephane Eranian , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 4.14 33/62] perf cpumap: Fix writing to illegal memory in handling cpumap mask Date: Tue, 27 Aug 2019 09:50:38 +0200 Message-Id: <20190827072702.651098086@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827072659.803647352@linuxfoundation.org> References: <20190827072659.803647352@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 5f5e25f1c7933a6e1673515c0b1d5acd82fea1ed ] 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 Signed-off-by: Sasha Levin --- 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 383674f448fcd..f93846edc1e0d 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c @@ -701,7 +701,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; -- 2.20.1