From patchwork Thu Apr 25 16:55:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Chancellor X-Patchwork-Id: 13643565 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 8631DC4345F for ; Thu, 25 Apr 2024 16:56:49 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=mC0dVQo8ZJnnOIvgY8v4NlC9W7o2SBINId38mZWF/a4=; b=KzqcrsTEs0Z9yR b/LJrn8oJAAMBfcQhr1/l0tbPQNwPPIUOcPYnnASdZQPEyXPju5PY83D3JsWtHkJPN+qepgY9JfNj FX+ZaueJ9XWv68L1jgPJlpzSwbsWALhW87k5SIxaksUrFXJ89mbug5Ss0eq6FvtGOeFkvz5sfHOQ+ 2BGnbCHb5iQ+xiIrPG+0BHanXVTVQWEWLbjYxL5VE5GagnGbkbbKEhadWDDegYrK6lDDUhfFN2Coh 79VF57eWIHlMqdKGvZgbIztbvNN8UUbEH10gAj8lnzSFUFbZTKPJQ4RPSd0fAdt0Ur7zevhGDju7n TFGRc65sOhduNIu4ioOg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s02Oj-00000009TDd-0wpd; Thu, 25 Apr 2024 16:56:37 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s02OW-00000009T5I-252w; Thu, 25 Apr 2024 16:56:26 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D1A1261C4F; Thu, 25 Apr 2024 16:56:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F6F4C2BD10; Thu, 25 Apr 2024 16:56:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714064183; bh=SXGhBnlcLWFdwE2aSUT7wEcsxWYKFItR1ihqD0HbrNk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SXedzSuhCjqQhQnjFPccHZuxE/yKDIwxVATHRECZco3Ko4VUsEv60lSE+TOye2eYu QqwyN4G9LJ3CluI9Vy+tMrTImddixFCOmcu0SlWNM4cjUfbGEzJGh4yPXiR1TPIZnm S+ncfXh9u2E3hEPp8NZmUxAn3mS7kUOrSHJoAXLlgOklYC0LTCVNDkP353s4hN2y4A Qb8u+qas5+ppBneMSTMei4tbkhYLG11I6G8gs2yuQqLeqL6X+NtsEsqs62/RAdA6oN B6f/6ul9VM+H1I98hIF9GM9GuI0UvXqD4GmlE5CE7rn/LEbmv2FxFacIsa8+NfxL2X tB7LMbWiOSH5Q== From: Nathan Chancellor Date: Thu, 25 Apr 2024 09:55:51 -0700 Subject: [PATCH 1/2] clk: bcm: dvp: Assign ->num before accessing ->hws MIME-Version: 1.0 Message-Id: <20240425-cbl-bcm-assign-counted-by-val-before-access-v1-1-e2db3b82d5ef@kernel.org> References: <20240425-cbl-bcm-assign-counted-by-val-before-access-v1-0-e2db3b82d5ef@kernel.org> In-Reply-To: <20240425-cbl-bcm-assign-counted-by-val-before-access-v1-0-e2db3b82d5ef@kernel.org> To: Michael Turquette , Stephen Boyd , Florian Fainelli Cc: Kees Cook , "Gustavo A. R. Silva" , bcm-kernel-feedback-list@broadcom.com, linux-clk@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-hardening@vger.kernel.org, patches@lists.linux.dev, llvm@lists.linux.dev, stable@vger.kernel.org, Nathan Chancellor X-Mailer: b4 0.14-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1901; i=nathan@kernel.org; h=from:subject:message-id; bh=SXGhBnlcLWFdwE2aSUT7wEcsxWYKFItR1ihqD0HbrNk=; b=owGbwMvMwCUmm602sfCA1DTG02pJDGla3aYz4qUdrfPXzgicc4iB6eMTl9Seha2nKj8tnvs76 c/5sxzmHaUsDGJcDLJiiizVj1WPGxrOOct449QkmDmsTCBDGLg4BWAi/jcYGZZutL6S0KV8ZM8O Ebvl7kstffrsutoP+bAfn86/L7G5m5vhv/uLuwc3ZTDy7elOXtTQ3P7Wi22vYYusWfX2Yq8Tj9c 0sgMA X-Developer-Key: i=nathan@kernel.org; a=openpgp; fpr=2437CB76E544CB6AB3D9DFD399739260CB6CB716 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240425_095624_671050_F356CCE9 X-CRM114-Status: GOOD ( 11.79 ) 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 Commit f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by") annotated the hws member of 'struct clk_hw_onecell_data' with __counted_by, which informs the bounds sanitizer about the number of elements in hws, so that it can warn when hws is accessed out of bounds. As noted in that change, the __counted_by member must be initialized with the number of elements before the first array access happens, otherwise there will be a warning from each access prior to the initialization because the number of elements is zero. This occurs in clk_dvp_probe() due to ->num being assigned after ->hws has been accessed: UBSAN: array-index-out-of-bounds in drivers/clk/bcm/clk-bcm2711-dvp.c:59:2 index 0 is out of range for type 'struct clk_hw *[] __counted_by(num)' (aka 'struct clk_hw *[]') Move the ->num initialization to before the first access of ->hws, which clears up the warning. Cc: stable@vger.kernel.org Fixes: f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by") Signed-off-by: Nathan Chancellor Reviewed-by: Kees Cook Reviewed-by: Florian Fainelli --- drivers/clk/bcm/clk-bcm2711-dvp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/clk/bcm/clk-bcm2711-dvp.c b/drivers/clk/bcm/clk-bcm2711-dvp.c index e4fbbf3c40fe..3cb235df9d37 100644 --- a/drivers/clk/bcm/clk-bcm2711-dvp.c +++ b/drivers/clk/bcm/clk-bcm2711-dvp.c @@ -56,6 +56,8 @@ static int clk_dvp_probe(struct platform_device *pdev) if (ret) return ret; + data->num = NR_CLOCKS; + data->hws[0] = clk_hw_register_gate_parent_data(&pdev->dev, "hdmi0-108MHz", &clk_dvp_parent, 0, @@ -76,7 +78,6 @@ static int clk_dvp_probe(struct platform_device *pdev) goto unregister_clk0; } - data->num = NR_CLOCKS; ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get, data); if (ret) From patchwork Thu Apr 25 16:55:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Chancellor X-Patchwork-Id: 13643566 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 971E9C10F15 for ; Thu, 25 Apr 2024 16:56:51 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KmsWQGyeEp9mFwRKXvWy3gd1v8Bs+YWLUpPWnTYWoZ4=; b=U76+iLKUzeRMpT KlSAXizRuIQKdI9CmSj3rYEwS82c/jgZb8zJvjQt5zVDlFvpkW7wiwLeuzAFds+L3X4pSUFbgaTRf 3j4AXVx2EMo2uxvo1F1/FbW3pJOMOxUJglDIijTTVD9Zn8+pxmRz5tgJAb7ySUu78CXJOh1Fa6N4c XyDD0TzsVrGS5lmUDpQ6+Kj/hWcfjMHJRJ5xWMNm3r+dM+EOUA5u0iXJ+0wiVcKUfjqqgM9lf7A1S LyG0bNRFN0XnspeJh8GzjGyjetMgtAvt8GUbcSJaVle6aEs7dcWsIHpzGTg5KVnUhfeuTlCKQCLGL WmPLWGq/v7T/0SXzUwkA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s02Ol-00000009TEn-2lyc; Thu, 25 Apr 2024 16:56:39 +0000 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s02OY-00000009T6J-1wM6; Thu, 25 Apr 2024 16:56:28 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A80FACE14A3; Thu, 25 Apr 2024 16:56:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86EC4C4AF0D; Thu, 25 Apr 2024 16:56:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714064184; bh=xOZM1ewaEE8webcrxae6fRZ3lapbeFGOs6WCFr8o9mU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=aBmRapsv6IaKBvdiQYhwCmEf62JM5xoaIdiZiuzQMg+cDdNZ5fUPBS0dvn02e3av9 zI2nOSy20qcJz3Vvu6so2xEacDjHl5ORFeYDK8ApeaQwQJkHbbQ8Bf4QDslxjjJv+Y oLDNM5DU10WXbvFj1NVqBfIxdsS7RjR25TBTrRN5h7Avx0QCbfSFuzbilZlZ3xr82q y13aputd6YFD4hjuFFtnmMn9BNWvTaMgZ6K+Y/I/ZlpdYlOhKVjAewRdxcu/yBXU+9 O2Ga2ovz2u7oU/CviciWQV/ZmGoZ6mwAqQnM6aHY3q94S0XD0Gwv4eKiMzUYVsLSgB c1kRGKIZXJn/Q== From: Nathan Chancellor Date: Thu, 25 Apr 2024 09:55:52 -0700 Subject: [PATCH 2/2] clk: bcm: rpi: Assign ->num before accessing ->hws MIME-Version: 1.0 Message-Id: <20240425-cbl-bcm-assign-counted-by-val-before-access-v1-2-e2db3b82d5ef@kernel.org> References: <20240425-cbl-bcm-assign-counted-by-val-before-access-v1-0-e2db3b82d5ef@kernel.org> In-Reply-To: <20240425-cbl-bcm-assign-counted-by-val-before-access-v1-0-e2db3b82d5ef@kernel.org> To: Michael Turquette , Stephen Boyd , Florian Fainelli Cc: Kees Cook , "Gustavo A. R. Silva" , bcm-kernel-feedback-list@broadcom.com, linux-clk@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-hardening@vger.kernel.org, patches@lists.linux.dev, llvm@lists.linux.dev, stable@vger.kernel.org, Nathan Chancellor X-Mailer: b4 0.14-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1678; i=nathan@kernel.org; h=from:subject:message-id; bh=xOZM1ewaEE8webcrxae6fRZ3lapbeFGOs6WCFr8o9mU=; b=owGbwMvMwCUmm602sfCA1DTG02pJDGla3abTn9y8tqDHMf/Qn/N85dnXVnov523rf7SlcaFbR 2b1jttqHaUsDGJcDLJiiizVj1WPGxrOOct449QkmDmsTCBDGLg4BWAiPKUM/1MLZtRPneq48r76 vu2NLyLVtxv1zLwQaPcvwZZv+ulgOV1GhgXn/XQKJgfuK7uWwHxp29VlqtZzb4XqTdmabG4lH3B DkwMA X-Developer-Key: i=nathan@kernel.org; a=openpgp; fpr=2437CB76E544CB6AB3D9DFD399739260CB6CB716 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240425_095627_104770_C333AB17 X-CRM114-Status: GOOD ( 12.56 ) 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 Commit f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by") annotated the hws member of 'struct clk_hw_onecell_data' with __counted_by, which informs the bounds sanitizer about the number of elements in hws, so that it can warn when hws is accessed out of bounds. As noted in that change, the __counted_by member must be initialized with the number of elements before the first array access happens, otherwise there will be a warning from each access prior to the initialization because the number of elements is zero. This occurs in raspberrypi_discover_clocks() due to ->num being assigned after ->hws has been accessed: UBSAN: array-index-out-of-bounds in drivers/clk/bcm/clk-raspberrypi.c:374:4 index 3 is out of range for type 'struct clk_hw *[] __counted_by(num)' (aka 'struct clk_hw *[]') Move the ->num initialization to before the first access of ->hws, which clears up the warning. Cc: stable@vger.kernel.org Fixes: f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by") Signed-off-by: Nathan Chancellor Reviewed-by: Kees Cook Reviewed-by: Florian Fainelli --- drivers/clk/bcm/clk-raspberrypi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/clk/bcm/clk-raspberrypi.c b/drivers/clk/bcm/clk-raspberrypi.c index 829406dc44a2..4d411408e4af 100644 --- a/drivers/clk/bcm/clk-raspberrypi.c +++ b/drivers/clk/bcm/clk-raspberrypi.c @@ -371,8 +371,8 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, if (IS_ERR(hw)) return PTR_ERR(hw); - data->hws[clks->id] = hw; data->num = clks->id + 1; + data->hws[clks->id] = hw; } clks++;