Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1084144lqd; Thu, 25 Apr 2024 05:49:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUq2Roic/8bWnhJUI82XpIg8nso5QBBFnb+6fXf+dvoU2NqPqwNWZjDsFujK7yG+oURR+5a9djS0dBT/rJP2VpkbrU88/EGGFx0KY6KHQ== X-Google-Smtp-Source: AGHT+IEyt1pvADEMAs6PpPFTXjDG7w0nGK85WNz/jcxbB5Xv89Ubh1q5NSuHHXUA677T3gU/fY0W X-Received: by 2002:a17:907:6d17:b0:a58:aea7:df78 with SMTP id sa23-20020a1709076d1700b00a58aea7df78mr2114398ejc.6.1714049349125; Thu, 25 Apr 2024 05:49:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714049349; cv=pass; d=google.com; s=arc-20160816; b=RO1+JhzPeRqxLp7MsFHDUltD3SlIiCByJfFL9KVjp4z0StD5ynL6PIrg5ucgbT3I1B uQgIYxg5ZG4mSz/ySYEdTVh9FtKZzCdMSuiT9r6DFdjmcd2/Y5viGwOiFK87mTPMBMuo +5D0E6LhtQjr7Gpj9+hgvyinuMoDfTfCUr81+GHvIO5mtzCVXt15sR/yGo4iVyRE6rsc pI4nodjxBfDG00yrcyv9iYTH8OL3PbU9I7vp8vmoiE1cJBzWV69yZRt2xaivB71r9Cwc UYep6oQT+gs3ccSko1OMgxUldwdy4eYhKudPFc07tQpn1xVJ9T4Z/eD7yv1RmarsEH6n McQw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=hsAeYNceXL6eAND7diZUJk4WCiODqHQaZQYg3fdMIoQ=; fh=svueJjvnNVRhQUONq/E+d+p+xL4JvjPnKzv7bb7GZV4=; b=bIZNlasDx6Folzluoy4EB/51LUEl1+Rr+7Q6Ky7It5ZTFYE5XeTqMNFFaeIxoo9eEG RtMQ/VBfypB2jQJNEbdNQbVuZhXmteqfPb8Gps8K5M0KY2grkx6MjsG+cLS7G1UY7QNh s1snK3ukLAR2HwBjh1CcJpg/SiSZJ31W0jmmgp7QKBgvaMYTp/EF2JlpHIACx2wUfvdL CjzMuljp1/XbTAo1Hkk8yZK6lrXhGYV4leaafGCtP8ZmUSq5Rdj0fgUivt6TosKeIdpR kB6KM27M8WIEYclpCGbpcfF/v0UdfQ1yTgVgzJZm8y4GEqm9e9jtocPrQVfcWJ2yOc5n Ln3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-158547-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158547-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i22-20020a1709064fd600b00a52385c9a7fsi10815402ejw.681.2024.04.25.05.49.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 05:49:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158547-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-158547-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158547-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D6AD01F22F6D for ; Thu, 25 Apr 2024 12:49:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C3E01149C54; Thu, 25 Apr 2024 12:48:49 +0000 (UTC) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 696221494B1 for ; Thu, 25 Apr 2024 12:48:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714049329; cv=none; b=O7v+XP8RJckIc5QGMpghlYt4YappQKmYLnQL6eCx0YUUYEWicBtu7pFFi2dEDqXPT1psZnlflcotAfe32sXWuvREMQ7/wdTxnBDTp99ZHgBDuHmWNGcNbngxi5EkdPsXdh+JTMqrIsmd1J9Dm7PJ1/RNPVX3OPnhJ/BZyQfDjEU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714049329; c=relaxed/simple; bh=slRugwnP921pIxWBVI6GNhjGlZd6X4SBQW6Z9Mm1TIY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rYS8kAefwcFqiUjF9L5i5V4SirVRZNh7G2KAkhB09Su7jV98+Ata0TTamMMV39+WC7I6rA5CdXEknA0DOYi2Oo9S1yiP820wgHaSw2dfbr8CVmk2Kpq4MBMqmVTFaIszXM5CPi9/fv8Mi3Jta+OyyuHLjTeyO/Hu73Sa8EPBYW8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VQFxk1HNzzcc1t; Thu, 25 Apr 2024 20:47:38 +0800 (CST) Received: from dggpeml500002.china.huawei.com (unknown [7.185.36.158]) by mail.maildlp.com (Postfix) with ESMTPS id 493AA1401E9; Thu, 25 Apr 2024 20:48:43 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpeml500002.china.huawei.com (7.185.36.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 25 Apr 2024 20:48:43 +0800 From: Junhao He To: , , , CC: , , , , , Subject: [PATCH 2/3] drivers/perf: hisi: hns3: Fix out-of-bound access when valid event group Date: Thu, 25 Apr 2024 20:46:26 +0800 Message-ID: <20240425124627.13764-3-hejunhao3@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240425124627.13764-1-hejunhao3@huawei.com> References: <20240425124627.13764-1-hejunhao3@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500002.china.huawei.com (7.185.36.158) The perf tool allows users to create event groups through following cmd [1], but the driver does not check whether the array index is out of bounds when writing data to the event_group array. If the number of events in an event_group is greater than HNS3_PMU_MAX_HW_EVENTS, the memory write overflow of event_group array occurs. Add array index check to fix the possible array out of bounds violation, and return directly when write new events are written to array bounds. There are 9 different events in an event_group. [1] perf stat -e '{pmu/event1/, ... ,pmu/event9/} Fixes: 66637ab137b4 ("drivers/perf: hisi: add driver for HNS3 PMU") Signed-off-by: Junhao He Signed-off-by: Hao Chen --- drivers/perf/hisilicon/hns3_pmu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/perf/hisilicon/hns3_pmu.c b/drivers/perf/hisilicon/hns3_pmu.c index 16869bf5bf4c..cbdd53b0a034 100644 --- a/drivers/perf/hisilicon/hns3_pmu.c +++ b/drivers/perf/hisilicon/hns3_pmu.c @@ -1085,15 +1085,27 @@ static bool hns3_pmu_validate_event_group(struct perf_event *event) return false; for (num = 0; num < counters; num++) { + /* + * If we find a related event, then it's a valid group + * since we don't need to allocate a new counter for it. + */ if (hns3_pmu_cmp_event(event_group[num], sibling)) break; } + /* + * Otherwise it's a new event but if there's no available counter, + * fail the check since we cannot schedule all the events in + * the group simultaneously. + */ + if (num == HNS3_PMU_MAX_HW_EVENTS) + return false; + if (num == counters) event_group[counters++] = sibling; } - return counters <= HNS3_PMU_MAX_HW_EVENTS; + return true; } static u32 hns3_pmu_get_filter_condition(struct perf_event *event) -- 2.33.0