Message ID | ZMgiiEmBORZRaobc@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp2276919vqg; Mon, 31 Jul 2023 14:32:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlHb6kPaVwheAihMHtpkkn7+NYUWTalIhNxW9kDkSOCFYp1sNIaMACa+ejaSNY16sy/O2iBX X-Received: by 2002:a05:6512:ea1:b0:4fb:8cd1:1679 with SMTP id bi33-20020a0565120ea100b004fb8cd11679mr719533lfb.44.1690839130146; Mon, 31 Jul 2023 14:32:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690839130; cv=none; d=google.com; s=arc-20160816; b=cdU8MImsKFLlbnoPHG15kEq9OEwNnU3Kl23SD5cW8NHYh/ktgGKn5WCAH7zWQG7YNY je3VPJrwo/UWVKMkQGMWcsP+1lbRETiCEpwoq8u6jpjWo4eyfYmwXnLxxVGvc/mLS8WW p5Xf09EcJ8Rrh6cbiBnqPv9fAki0gRl28bPqgugqRSzV6pMAYEfdT2yreEz2TJV90M2F bYYKPCoQnJGN4Usak+5D+EiZBrlFQX6RlfnI5pHU6sLjdThEWkWfHQLrE/ApQZv+BFAc 92Bq+l0w5P1Zcp0oRq+lHbREV5zEwFyoNt5NSmxwole0+pZW9k5SXFLEzWWMMuaCYAYz JZqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=akXlsyDT+DogdAo6VZjVkJ9g6gm1NiKxqsA4jEsIVbI=; fh=HkdaBjX0iZS7RwtLCzLEGE6+Tl1BtmIGSHaAETqBdDw=; b=e5zZIgn3sIgYcQRV9Xd5e0cZEjRD6UrJk+ELeSIiHLQEr058n8ZTIPkqOSAM2SRX8m YmDtk8FSfyUstPRAbBilGm18fekL4tQeJrYc8oRF4VaWew27DwzekDXs+OYcK8PoMQPN dUytl+XYBU4B7Fg3vj5QPL2mMjdaHCOBP09OKFRVjiUFkBJiqAo4DNluVz9Q0ySHymym BFuEsncnM+0lNzGdQLwvTv1iIFyh9bgE09sLAalWnRfe+GM6VAtX53H4sZVJwtDM0VZd 368F6B0OfeQmny3aV27ae2MHqDUFj8g4mcjAT1ciit0ei8uwlmwADkLC8uS5BW2jtPMw uppQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=mrH744iQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j12-20020aa7c40c000000b0051e2a1ef7dbsi6747783edq.495.2023.07.31.14.31.46; Mon, 31 Jul 2023 14:32:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=mrH744iQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230219AbjGaVGS (ORCPT <rfc822;dengxinlin2429@gmail.com> + 99 others); Mon, 31 Jul 2023 17:06:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229843AbjGaVGR (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 31 Jul 2023 17:06:17 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB2F4194; Mon, 31 Jul 2023 14:06:16 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 65687612DD; Mon, 31 Jul 2023 21:06:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE2DDC433C8; Mon, 31 Jul 2023 21:06:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690837575; bh=uFmV+Z9IEZrlWvkA9y0SJYB4T9OHFEmD0Fdew8OiXcs=; h=Date:From:To:Cc:Subject:From; b=mrH744iQrz7H9/m2jMOkpp+sRDM/1X4YqksMXBA8LCgMQY5YbV4fybbNg3IaYLGKM 5+A1YzHtc3n4Kf6PbHETyDFT5o6x7m2JVuKlSqq0+272oHJ7Ir4Jp72rx8ElT2GWxd Yo41hVxoS6xL+TGS1WDKtj+TYqWE/VYIrbRLLN6hmWbOEfxV8Uzn2/8fuTxx6vCXVN Xg/8stS3DgMQ0rbmzutxZv4FCO5An08+w9GTNWiTWIbKaZapx8qD6rxBjG8eM7sQVm YfKr9aVn2BIzRwMN6b9wNdUVPI7zF1tZpXABmwJKGTHQTOaZ8raJURDgTEL78B/pi/ xzGk2wffY3U5w== Date: Mon, 31 Jul 2023 15:07:20 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Markus Mayer <mmayer@broadcom.com>, Broadcom internal kernel review list <bcm-kernel-feedback-list@broadcom.com>, "Rafael J. Wysocki" <rafael@kernel.org>, Viresh Kumar <viresh.kumar@linaro.org>, Florian Fainelli <florian.fainelli@broadcom.com> Cc: linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH v2] cpufreq: brcmstb-avs-cpufreq: Fix -Warray-bounds bug Message-ID: <ZMgiiEmBORZRaobc@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772972536089233932 X-GMAIL-MSGID: 1772973331752667073 |
Series |
[v2] cpufreq: brcmstb-avs-cpufreq: Fix -Warray-bounds bug
|
|
Commit Message
Gustavo A. R. Silva
July 31, 2023, 9:07 p.m. UTC
Update the iteration conditions in the for() loop to avoid writing in
array `table` beyond its allocated size at:
drivers/cpufreq/brcmstb-avs-cpufreq.c:
449 table[i].frequency = CPUFREQ_TABLE_END;
This fixes the following -Warray-bounds warning seen after building
ARM with multi_v7_defconfig (GCC 13):
In function 'brcm_avs_get_freq_table',
inlined from 'brcm_avs_cpufreq_init' at drivers/cpufreq/brcmstb-avs-cpufreq.c:623:15:
drivers/cpufreq/brcmstb-avs-cpufreq.c:449:28: warning: array subscript 5 is outside array bounds of 'void[60]' [-Warray-bounds=]
449 | table[i].frequency = CPUFREQ_TABLE_END;
In file included from include/linux/node.h:18,
from include/linux/cpu.h:17,
from include/linux/cpufreq.h:12,
from drivers/cpufreq/brcmstb-avs-cpufreq.c:44:
In function 'devm_kmalloc_array',
inlined from 'devm_kcalloc' at include/linux/device.h:328:9,
inlined from 'brcm_avs_get_freq_table' at drivers/cpufreq/brcmstb-avs-cpufreq.c:437:10,
inlined from 'brcm_avs_cpufreq_init' at drivers/cpufreq/brcmstb-avs-cpufreq.c:623:15:
include/linux/device.h:323:16: note: at offset 60 into object of size 60 allocated by 'devm_kmalloc'
323 | return devm_kmalloc(dev, bytes, flags);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This helps with the ongoing efforts to tighten the FORTIFY_SOURCE
routines on memcpy() and help us make progress towards globally
enabling -Warray-bounds.
Link: https://github.com/KSPP/linux/issues/324
Fixes: de322e085995 ("cpufreq: brcmstb-avs-cpufreq: AVS CPUfreq driver for Broadcom STB SoCs")
Cc: stable@vger.kernel.org
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
Changes in v2:
- Update changelog text. Add more details.
v1:
- Link: https://lore.kernel.org/linux-hardening/ZMgfWEA0GAN%2FRog8@work/
drivers/cpufreq/brcmstb-avs-cpufreq.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Mon, Jul 31, 2023 at 03:07:20PM -0600, Gustavo A. R. Silva wrote: > Update the iteration conditions in the for() loop to avoid writing in > array `table` beyond its allocated size at: > > drivers/cpufreq/brcmstb-avs-cpufreq.c: > 449 table[i].frequency = CPUFREQ_TABLE_END; > > This fixes the following -Warray-bounds warning seen after building > ARM with multi_v7_defconfig (GCC 13): > In function 'brcm_avs_get_freq_table', > inlined from 'brcm_avs_cpufreq_init' at drivers/cpufreq/brcmstb-avs-cpufreq.c:623:15: > drivers/cpufreq/brcmstb-avs-cpufreq.c:449:28: warning: array subscript 5 is outside array bounds of 'void[60]' [-Warray-bounds=] > 449 | table[i].frequency = CPUFREQ_TABLE_END; #define AVS_PSTATE_P0 0x0 #define AVS_PSTATE_P1 0x1 #define AVS_PSTATE_P2 0x2 #define AVS_PSTATE_P3 0x3 #define AVS_PSTATE_P4 0x4 #define AVS_PSTATE_MAX AVS_PSTATE_P4 table = devm_kcalloc(dev, AVS_PSTATE_MAX + 1, sizeof(*table), GFP_KERNEL); ... for (i = AVS_PSTATE_P0; i <= AVS_PSTATE_MAX; i++) { ... } table[i].frequency = CPUFREQ_TABLE_END; I see "AVS_PSTATE_MAX + 1" being used for the allocation, and so the loop is likely correctly doing P0 through P4. If there is supposed to be a terminating element in the table, I think the correct fix would be to allocate an additional element, not stop the loop from processing P4. > [...] > - for (i = AVS_PSTATE_P0; i <= AVS_PSTATE_MAX; i++) { > + for (i = AVS_PSTATE_P0; i < AVS_PSTATE_MAX; i++) { > ret = brcm_avs_set_pstate(priv, i); > if (ret) > return ERR_PTR(ret); -Kees
On 7/31/23 17:16, Kees Cook wrote: > On Mon, Jul 31, 2023 at 03:07:20PM -0600, Gustavo A. R. Silva wrote: >> Update the iteration conditions in the for() loop to avoid writing in >> array `table` beyond its allocated size at: >> >> drivers/cpufreq/brcmstb-avs-cpufreq.c: >> 449 table[i].frequency = CPUFREQ_TABLE_END; >> >> This fixes the following -Warray-bounds warning seen after building >> ARM with multi_v7_defconfig (GCC 13): >> In function 'brcm_avs_get_freq_table', >> inlined from 'brcm_avs_cpufreq_init' at drivers/cpufreq/brcmstb-avs-cpufreq.c:623:15: >> drivers/cpufreq/brcmstb-avs-cpufreq.c:449:28: warning: array subscript 5 is outside array bounds of 'void[60]' [-Warray-bounds=] >> 449 | table[i].frequency = CPUFREQ_TABLE_END; > > #define AVS_PSTATE_P0 0x0 > #define AVS_PSTATE_P1 0x1 > #define AVS_PSTATE_P2 0x2 > #define AVS_PSTATE_P3 0x3 > #define AVS_PSTATE_P4 0x4 > #define AVS_PSTATE_MAX AVS_PSTATE_P4 > > table = devm_kcalloc(dev, AVS_PSTATE_MAX + 1, sizeof(*table), > GFP_KERNEL); > ... > for (i = AVS_PSTATE_P0; i <= AVS_PSTATE_MAX; i++) { > ... > } > table[i].frequency = CPUFREQ_TABLE_END; > > I see "AVS_PSTATE_MAX + 1" being used for the allocation, and so the > loop is likely correctly doing P0 through P4. If there is supposed to be > a terminating element in the table, I think the correct fix would be to > allocate an additional element, not stop the loop from processing P4. Yeah; I think you're right. And it seems that this function header makes it clear, too: drivers/cpufreq/brcmstb-avs-cpufreq.c: 421 /* 422 * We determine which frequencies are supported by cycling through all P-states 423 * and reading back what frequency we are running at for each P-state. 424 */ 425 static struct cpufreq_frequency_table * 426 brcm_avs_get_freq_table(struct device *dev, struct private_data *priv) I just sent v3: https://lore.kernel.org/linux-hardening/ZMh45KH2iPIpNktr@work/ Let's see what the maintainers say. Thanks for the feedback! -- Gustavo
diff --git a/drivers/cpufreq/brcmstb-avs-cpufreq.c b/drivers/cpufreq/brcmstb-avs-cpufreq.c index 1bdd513bcd19..99ba2d707eff 100644 --- a/drivers/cpufreq/brcmstb-avs-cpufreq.c +++ b/drivers/cpufreq/brcmstb-avs-cpufreq.c @@ -439,7 +439,7 @@ brcm_avs_get_freq_table(struct device *dev, struct private_data *priv) if (!table) return ERR_PTR(-ENOMEM); - for (i = AVS_PSTATE_P0; i <= AVS_PSTATE_MAX; i++) { + for (i = AVS_PSTATE_P0; i < AVS_PSTATE_MAX; i++) { ret = brcm_avs_set_pstate(priv, i); if (ret) return ERR_PTR(ret);