Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5225074ybl; Tue, 27 Aug 2019 01:05:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqxvVC1nr7d6A6VC5s7daLjrWJZr779QjQXGOqO28U0BM5lR4cRW6sJraodlssuDiKebautV X-Received: by 2002:a62:8343:: with SMTP id h64mr23790211pfe.170.1566893158533; Tue, 27 Aug 2019 01:05:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566893158; cv=none; d=google.com; s=arc-20160816; b=lJ0V+MpXz33o3CJvC/Mg66u36DgI51eIKRFzlda/tsPybNS8RA8Mo0r+87Dwi1rUsa BodsRPKHSN82Ky0QkGCV6pi3R9eB8DfT5LrBSqwZ1e4WwEHDpazUC6XWr0HNVYRZBi7Y o9nNTc99Dow1vatACmffBOtN6gPHtHq4uN3nFWFEVZywBWH3UNYkJJSqfM+cFKbAqMEA DzVHTx3LLEewgL4FivTARozpPoIt57CkvyEOOwkytWFt1fD04YDJjkVAh2i3EM6fP6Pk kmC7NKkRqE2S+pHhip+OsLDMcMT1pO4Xqwc+1I1Yj+A1SmInjXcNRMBKExH/tT5T6atY 2n5g== 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=1HEwrl0wQ5X6vJWB0D9B7W3TPN8JcwMtCGC3FfjS7NU=; b=hEKvNjhbLIPq27Rw+iuIVzfHBHidz5qloPyqq7LZ1Fo3FLanVWtul46Y3pPspvbI2p XMk5ueKdAo+PiYEklYimPvRYuqHSHbZuCEJbFF7thquylKrT4895WkcIpy/LTNwdVwVm 2x25smPplMcy7VGYnM9e8mzFUhmqxpssUDGiZqVDDwc7NKaHh80kkas2DIqXO2r3yHEU IkvgmELFkE+uEu7F5653PLpJ1PNzWqttXK+aoh8eV1i+14DXorSkbmG0ElMnhyDcIsiQ uRu/DWephqgO6sZ83Ri0eG1u24hGBZcQ9w0s8+5L150sEj+JiilpnGBBu6o3p4+HaCOj EJPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jFNBns9u; 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 l70si11146826pge.446.2019.08.27.01.05.43; Tue, 27 Aug 2019 01:05:58 -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=jFNBns9u; 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 S1732591AbfH0IEe (ORCPT + 99 others); Tue, 27 Aug 2019 04:04:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:34066 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730668AbfH0IEd (ORCPT ); Tue, 27 Aug 2019 04:04:33 -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 D0E42206BA; Tue, 27 Aug 2019 08:04:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566893072; bh=hNum6xY1ZxdFXvz+d1ludW6VEn5ALBpiGz7sSOzKs3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jFNBns9uZKA5Kgppjdze725rl9cp5N8GEewZgZCbo4bFicrxmNcLWP1yNPwYGEj3p Y5C61Tojgzx1mYu8fBQicLT0EIvB0DWF7fbcsZxXOsuF2245zevEOzMHjroScUHTNb ir5berFvKvvLPiztZLrone+cgvs52OuYIEiv+eLs= 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 5.2 104/162] perf cpumap: Fix writing to illegal memory in handling cpumap mask Date: Tue, 27 Aug 2019 09:50:32 +0200 Message-Id: <20190827072741.914554536@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827072738.093683223@linuxfoundation.org> References: <20190827072738.093683223@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 0b599229bc7e9..0aba5b39c21ef 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