From patchwork Tue Aug 1 03:15:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 13335730 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 AC8C4C001DF for ; Tue, 1 Aug 2023 03:15:19 +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-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:Cc:To: From:Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=5kVPnXMv9lS62vUBeJSyD6uWFoApY2cfXuPGRZ8egyQ=; b=tYg78NRjn7cIoq 9dt9o5GUm8EjogWTTn9O/uPt8UiGeGI98DYXr3t9rVUTphZt5AMG+fNdc1oo12+DB5yLXeOVzmvff bOWPYfevwrffHKnffvb0ggF2/MhYyB6Ex+QhjeT3MVg6tO9WwdeSgAAQFvSnWsedwyyH5t93HiwPe y94nwtNLKiaDQS8gkJrRGwrHYX+mtzV4tSubYC7BBrBfSJLf0e7JFc+mv4xhHNxPgex3J0494KM8X I3LDysA1XInoDHhgMBj9EPlkUkUC8mwFbSkfhK2rIpl2fbbSglTpss0uex9+/1qcJWC/EBpBN+Tiy 5GubvU9nSAGU47IUFyMg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQfqT-000G0J-0B; Tue, 01 Aug 2023 03:14:49 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQfqQ-000Fzv-0G for linux-arm-kernel@lists.infradead.org; Tue, 01 Aug 2023 03:14:47 +0000 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 49072613E9; Tue, 1 Aug 2023 03:14:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 471FCC433C7; Tue, 1 Aug 2023 03:14:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690859684; bh=2oC1axryfLbP+TCpiGARmMNZzsq4UdhTAH2upeuRKAQ=; h=Date:From:To:Cc:Subject:From; b=jhgy7qjl3QW+L8CbNw51tigS3tZSBhG/HYf3GSvbGrG3bvuUXIa/O+KxEDzuqxlod P8B56Hd4b+zcdPWEl5IIGHS6UoGsWvbbuG9jthVc6U5+CKsvVXqkXKVRTkA8gx81p9 ahh31yEXdFxO045zP3A+quMQTM+aM9TI4K/rkEgpe1E6RdmezYfXDdO4Tn+w5m9Bas XFVjng3yIRffZ5v7IeaTcSHDdcv2ek+Jyb9ZrfH66rbmdDSSOiB0PTJkkHSvCMG6hd qy96KAHE9irZRKpqFW2bwKRYnYYizmAT4y/7O1TtLwzNm2i7b+KWFr6aA5gdk/fycJ fvHH2fzbvPD/Q== Date: Mon, 31 Jul 2023 21:15:48 -0600 From: "Gustavo A. R. Silva" To: Markus Mayer , Broadcom internal kernel review list , "Rafael J. Wysocki" , Viresh Kumar , Florian Fainelli Cc: linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org, Kees Cook Subject: [PATCH v3] cpufreq: brcmstb-avs-cpufreq: Fix -Warray-bounds bug Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230731_201446_220399_C568A049 X-CRM114-Status: GOOD ( 14.59 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Allocate extra space for terminating element at: drivers/cpufreq/brcmstb-avs-cpufreq.c: 449 table[i].frequency = CPUFREQ_TABLE_END; and add code comment to make this clear. 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 Reviewed-by: Florian Fainelli --- Changes in v3: - Allocate extra space for a terminating element. (Kees Cook) - Update changelog text. Changes in v2: - Update changelog text. Add more details. - Link: https://lore.kernel.org/linux-hardening/202307311610.B1EB796684@keescook/ v1: - Link: https://lore.kernel.org/linux-hardening/ZMgfWEA0GAN%2FRog8@work/ drivers/cpufreq/brcmstb-avs-cpufreq.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/cpufreq/brcmstb-avs-cpufreq.c b/drivers/cpufreq/brcmstb-avs-cpufreq.c index ffea6402189d..3052949aebbc 100644 --- a/drivers/cpufreq/brcmstb-avs-cpufreq.c +++ b/drivers/cpufreq/brcmstb-avs-cpufreq.c @@ -434,7 +434,11 @@ brcm_avs_get_freq_table(struct device *dev, struct private_data *priv) if (ret) return ERR_PTR(ret); - table = devm_kcalloc(dev, AVS_PSTATE_MAX + 1, sizeof(*table), + /* + * We allocate space for the 5 different P-STATES AVS, + * plus extra space for a terminating element. + */ + table = devm_kcalloc(dev, AVS_PSTATE_MAX + 1 + 1, sizeof(*table), GFP_KERNEL); if (!table) return ERR_PTR(-ENOMEM);