From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9280EC10F15 for ; Fri, 26 Apr 2024 10:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:To:Subject:CC: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=wGGbdIfm5YxZ72r4jaX2b9Gc11kCyjs27YQDLPxKGEQ=; b=fLkEvrLopdzLz/ DUP8AGQzLF9lvpoUgf/DTjY0TBBJ8puwfR4n06KOY57heJFzFP4HYxebIln/0fhT79SnEcKnUHLoX gJDwlFb8fBmqMlQyOsqkMIVplHXviG+lSVqe/4ehSFBFm8ORW7vilH/YyMhdFFeeXmanGLXX7PIyU vz9CNtyHibOdY6OQ+1Sl7UeLL+fY8g7cyLs4jKCptF7yw2f9dsO6k/XejeeNwIJT6v4Kbd/FQivvR /deIcfPoa6eNQx4iGE8kkOWMUrYwdOj2opsjJdblQXTPVf0hI+Mw/vto/Zi42wZFxtjPwYW08X3rk 6s4qXYHOoM1+6w00TJyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0Ifs-0000000C7KL-0V5D; Fri, 26 Apr 2024 10:19:24 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0Ifo-0000000C7JA-1YDZ for linux-arm-kernel@lists.infradead.org; Fri, 26 Apr 2024 10:19:21 +0000 Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VQpZf0pp7zcZxR; Fri, 26 Apr 2024 18:18:02 +0800 (CST) Received: from kwepemm600007.china.huawei.com (unknown [7.193.23.208]) by mail.maildlp.com (Postfix) with ESMTPS id 9ABDA180073; Fri, 26 Apr 2024 18:19:07 +0800 (CST) Received: from [10.67.120.192] (10.67.120.192) by kwepemm600007.china.huawei.com (7.193.23.208) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 26 Apr 2024 18:19:06 +0800 Message-ID: Date: Fri, 26 Apr 2024 18:19:06 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird CC: , , , , , Subject: Re: [PATCH 2/3] drivers/perf: hisi: hns3: Fix out-of-bound access when valid event group To: Junhao He , , , References: <20240425124627.13764-1-hejunhao3@huawei.com> <20240425124627.13764-3-hejunhao3@huawei.com> From: Jijie Shao In-Reply-To: <20240425124627.13764-3-hejunhao3@huawei.com> X-Originating-IP: [10.67.120.192] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600007.china.huawei.com (7.193.23.208) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240426_031920_621345_CAE3F3BE X-CRM114-Status: GOOD ( 22.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Reviewed-by: Jijie Shao on 2024/4/25 20:46, Junhao He wrote: > 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) _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 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 D339713F44F for ; Fri, 26 Apr 2024 10:19:09 +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=1714126751; cv=none; b=LRVNalUlfikSTBzp+PcVBg8CNnUWg06qrMV7pZK/Xbqdt7bLeNqIJUBtwzp+Ue2KM2wBvgdcLYPf6tBoxZAL8pbKe9/rNbqmDhkMj/2PA213W/mbtGZ33KixTPrRrcNHmkwhuf/nnz+xABoASmn1e/RiqQi9xFWv3qUrDPi79fk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714126751; c=relaxed/simple; bh=PvlqjU87/D+2kbb2605lAsy6oLHguWugqQjh+UKPNnQ=; h=Message-ID:Date:MIME-Version:CC:Subject:To:References:From: In-Reply-To:Content-Type; b=uoKv9VbIMQElX1mt97tTLxccmTwfaTeY1PBBgYx9tw4fcaSTWLFuNBldmO6BFFjSUOfHf85sdAEsGBuzEXc90ZE3+uByB+9eQpcMiNKcxKgBt810nJnhf4fIkUKi0ceQR4usJ/dU+mVhttFgWqgj1CqVr/C9wZy4Gn5gnmc1h10= 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.163.48]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VQpZf0pp7zcZxR; Fri, 26 Apr 2024 18:18:02 +0800 (CST) Received: from kwepemm600007.china.huawei.com (unknown [7.193.23.208]) by mail.maildlp.com (Postfix) with ESMTPS id 9ABDA180073; Fri, 26 Apr 2024 18:19:07 +0800 (CST) Received: from [10.67.120.192] (10.67.120.192) by kwepemm600007.china.huawei.com (7.193.23.208) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 26 Apr 2024 18:19:06 +0800 Message-ID: Date: Fri, 26 Apr 2024 18:19:06 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird CC: , , , , , Subject: Re: [PATCH 2/3] drivers/perf: hisi: hns3: Fix out-of-bound access when valid event group To: Junhao He , , , References: <20240425124627.13764-1-hejunhao3@huawei.com> <20240425124627.13764-3-hejunhao3@huawei.com> From: Jijie Shao In-Reply-To: <20240425124627.13764-3-hejunhao3@huawei.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600007.china.huawei.com (7.193.23.208) Reviewed-by: Jijie Shao on 2024/4/25 20:46, Junhao He wrote: > 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)