Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1088382lqd; Thu, 25 Apr 2024 05:57:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUfwyUv1XYf2Y1Ebm4ah05+R7hVrOQlaYbLa8+EHVRRFGWmkjAOFEY6Itc1s4EROjQQT9OFevYb2GNUq19ROw1uxV9q23/UcfxelgNPZg== X-Google-Smtp-Source: AGHT+IF9zAxe4csODvtjHKNg7UkBH9pu7mgjg5RYkhHbk5c1eUUDFipuodAG2HPcu1Y2kgivTU2X X-Received: by 2002:a05:6808:18a1:b0:3c8:32b1:f8e9 with SMTP id bi33-20020a05680818a100b003c832b1f8e9mr7710730oib.14.1714049844160; Thu, 25 Apr 2024 05:57:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714049844; cv=pass; d=google.com; s=arc-20160816; b=kOVmdye4psJtY98+reDqWFIEJw2jljq2aCCPlpT8SR7fe0Sz3jFZPsCJOOHQ3XHm7P jAd1AE2yr3xNeyUQJNIyOqz0jx1Ug//9VRy+PYHzJXsPWc7UDZ81TiMVGL06exezxze7 cr8qKrON+EVx2Igq1LJU297afo2RSV7GTMbCuYz53u5DtokNFXb673O3Fb1BNrLRZ0DM akdfhBDdubJKvQN9WzZceKrQsd5S+C3UUhQ4/zPd/NSRLIOT9y9fMd1WvqNzwsFtTdSW YJtbDY7UtqQREeyNNMAHm5cYtuDEwYFjz+piXt3dPlnhW9/5E5Fc/vhWJKW6M24lvzdV A9Kg== 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=InL+DnMbkcpJDpPVU5sRM0o8vBGkgJ7D3hGbgXWgZ+Q=; fh=svueJjvnNVRhQUONq/E+d+p+xL4JvjPnKzv7bb7GZV4=; b=x4fP7eYI+0mQO2GMUZYhDjCrPzDEMwj5PfmR2a18spjLFA9wPX8r/q0tdVqVt2eMIC PsLhbkwJEN1qjbYUGhGMkXst8uoU1dfXK/GyOdjAaopffFIvzwb8l8OO0NQL9kt9RT0T SvTQ9BXzBsdr2KdCP9qFSMG29TQxOoittWPCg6Vyxdt5X0ICuGB4/bqM05KUz/FMw3gK KaAwt7ldlQIoV3jrJLNLIL2wAP+NyXJBmRVumNcl3QXfSAJNNrJaCfcS5sMJRb+Olf6c cLZ2OKHxei72BlFXdx8rtpbZoJ2dkJiNhr5n2UGY8OdSGZ8x/igskiIWX3CBzcFxTUx0 b0cA==; 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-158545-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158545-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t185-20020a6381c2000000b005f7fcd32070si11202167pgd.23.2024.04.25.05.57.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 05:57:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158545-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; 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-158545-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158545-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 81C9EB2689B for ; Thu, 25 Apr 2024 12:48:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A5861494CC; Thu, 25 Apr 2024 12:48:48 +0000 (UTC) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (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 5736827715 for ; Thu, 25 Apr 2024 12:48:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714049327; cv=none; b=WoBWZBRvdv8RoyyPvZ+bMRu1GeVUZv7q1oayj3TPLg14tcLcwKbQaDQ6z8qhhl3teYggoHlVoWZ46WOEYhmRvLkazRiwfbIT7Hvb2C++N1ksqme9hPaCjcsau/lmpGBa3WLFAPd+1dm1J33ecSjsILHDCHx3cS9sqOP4BmuzPlg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714049327; c=relaxed/simple; bh=gXeNCjxT3gJZfn41DbjCUhvZyt8PxO6qbccvtxOwzgA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JTVbycanbahQAX/bp8lHnXKZVgxeP7/WQGAqsBh7h3aJaDoYjwQmn5yCgUD8G1Jo9yT2OGDOhKFYZTlDNvMnuLMC0+Xsnikm9aj2a0wqr+5FJYzhP+eGDpwJCHnr+2EFaxKIai2ofExHRBNisD82IRfaog6/OW8zLA6FeMgIZMY= 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.187 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.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VQFvH16Y8zwTrt; Thu, 25 Apr 2024 20:45:31 +0800 (CST) Received: from dggpeml500002.china.huawei.com (unknown [7.185.36.158]) by mail.maildlp.com (Postfix) with ESMTPS id 21A23140258; 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:42 +0800 From: Junhao He To: , , , CC: , , , , , Subject: [PATCH 1/3] drivers/perf: hisi_pcie: Fix out-of-bound access when valid event group Date: Thu, 25 Apr 2024 20:46:25 +0800 Message-ID: <20240425124627.13764-2-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 HISI_PCIE_MAX_COUNTERS, 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: 8404b0fbc7fb ("drivers/perf: hisi: Add driver for HiSilicon PCIe PMU") Signed-off-by: Junhao He --- drivers/perf/hisilicon/hisi_pcie_pmu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/perf/hisilicon/hisi_pcie_pmu.c b/drivers/perf/hisilicon/hisi_pcie_pmu.c index 5d1f0e9fdb08..dba399125658 100644 --- a/drivers/perf/hisilicon/hisi_pcie_pmu.c +++ b/drivers/perf/hisilicon/hisi_pcie_pmu.c @@ -350,15 +350,27 @@ static bool hisi_pcie_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 (hisi_pcie_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 == HISI_PCIE_MAX_COUNTERS) + return false; + if (num == counters) event_group[counters++] = sibling; } - return counters <= HISI_PCIE_MAX_COUNTERS; + return true; } static int hisi_pcie_pmu_event_init(struct perf_event *event) -- 2.33.0