[2/2,next] clk: visconti: Add bounds-checking coverage for struct visconti_pll_provider
Message ID | 675633f7ae9893371d35b238f06fd02a3acffebb.1697076650.git.gustavoars@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp936808vqb; Wed, 11 Oct 2023 19:19:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF2L/jx2M9EVXNVy/dEKebl/KvRuMz2NxSsNJ/0boFQCisjQgGF1rslOYrBLhET/DcVbtW9 X-Received: by 2002:a17:90a:17c5:b0:27d:3b8:a08a with SMTP id q63-20020a17090a17c500b0027d03b8a08amr3340154pja.1.1697077163982; Wed, 11 Oct 2023 19:19:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697077163; cv=none; d=google.com; s=arc-20160816; b=qT/yYpmdwlmAOtPKj9pG0mD8V6IlwrukbCq3DzoPAOabkLQQdFCf39VvZEsWX9nAtT dHcnm5g2sxoyWEw5qL9vPUSIeC7beXB4oPlnVj5J/tM3c2eAABRl4R+2zKjWcDUU8UgY 7l2ale5/irQjuZCFSiWSgd+KZbNs3aJtd1Pjr6ojp/bFDUqFGtkzbajLjcw+qItl8oK0 uVhFRb53Mv598GtD14YiPztjUlTsbEa8J1B5aMrwtStV/5CrALnOvBDCez42PJOc5c4U yoRauLpVzEMwxj+dsFUda/5JXyf1QjwyFDF2ZsI3SnOpIspHhULmUGmD/cYvyzPXRRYE JC/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=i0liUqH09Hmc5Cr/glSr/854lsIVL4XHDYWeqUoshrs=; fh=gk24IOEycqD+6u11idkE8VXvRMctSQA5r+DBdFry3D0=; b=tAayXQMg1W7KyEmp5kmm2XsEl43JD7VlEqpdwJfCzI/5I1wSLXwSVxh2BuT1FFBsKp A6Da04NfVE9vHc3qqvhVL6Nmi765BZVUai+u27fjFKoiddkLWEH8w4zGOEhG2uw5VaMJ zDBq/1dWQjN1pqRJFYdzUZzuq5AMxkGItt0Sim8YRuZqQvfKogZn8vfWu4cBDDtsXpep CeHw/BieWWi2Hc2ecRdJhnpNR3gwQIsy7y28gsxPHCau/wuO5x2DN2NXO1eLP6fmnAbm i7EjRoP7Y8m2fNMibImCbTqk3law+YwoH9XZh3beW+NrmHPH2b/NfF1ZzK3m8U4fTN4k rwTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="UVhiz6z/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id v14-20020a17090ac90e00b0027743a2eeb9si1101481pjt.124.2023.10.11.19.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 19:19:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="UVhiz6z/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 20E1E8226F1E; Wed, 11 Oct 2023 19:19:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234085AbjJLCTE (ORCPT <rfc822;kartikey406@gmail.com> + 18 others); Wed, 11 Oct 2023 22:19:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233797AbjJLCSu (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 11 Oct 2023 22:18:50 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6949FB6; Wed, 11 Oct 2023 19:18:49 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5BE0C433C8; Thu, 12 Oct 2023 02:18:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697077129; bh=NePGQwBKuxNkoa39buLkrVoRes9zHH25Dkf23OgpCjM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=UVhiz6z/97OOSugRxlauh+TMHfJufV+mWRoRMD2YXt8tyY1UPkDiYTakb/Bx/GamR MigEfZyD9OMed1dDbdfM87yNyzEAliLbMH98/oI6B1+taXYd2rbvFvrdy9xWhe2+A+ SIGWUgGYJx7/XsjBDTpzj3OY2ODYJ1iPYFsHmZjRTqM+78PHUrfH2QcgRaxU/dKcv0 vpsC8wp2NPgL8NzJigOm+oTjYH/L459BACaMAgKkPqX6tj7kiFvdTzQ+ckpjSM53O8 kzGsqRjWfpOSEngRJbl4wenw7qBM6A3F+VxRRGTuEJr+f7faCg2DKIFtiR+ZnyPFff ysV2ArQKTDviA== Date: Wed, 11 Oct 2023 20:18:45 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Michael Turquette <mturquette@baylibre.com>, Stephen Boyd <sboyd@kernel.org>, Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp> Cc: Kees Cook <keescook@chromium.org>, linux-clk@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 2/2][next] clk: visconti: Add bounds-checking coverage for struct visconti_pll_provider Message-ID: <675633f7ae9893371d35b238f06fd02a3acffebb.1697076650.git.gustavoars@kernel.org> References: <cover.1697076650.git.gustavoars@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <cover.1697076650.git.gustavoars@kernel.org> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 11 Oct 2023 19:19:22 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779514383857632739 X-GMAIL-MSGID: 1779514383857632739 |
Series |
clk: visconti: Fix undefined behavior bug and add bounds-checking coverage
|
|
Commit Message
Gustavo A. R. Silva
Oct. 12, 2023, 2:18 a.m. UTC
In order to gain the bounds-checking coverage that __counted_by provides
to flexible-array members at run-time via CONFIG_UBSAN_BOUNDS (for array
indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions),
we must make sure that the counter member, in this particular case `num`,
is updated before the first access to the flex-array member, in this
particular case array `hws`. See below:
commit f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with
__counted_by") introduced `__counted_by` for `struct clk_hw_onecell_data`
together with changes to relocate some of assignments of counter `num`
before `hws` is accessed:
include/linux/clk-provider.h:
1380 struct clk_hw_onecell_data {
1381 unsigned int num;
1382 struct clk_hw *hws[] __counted_by(num);
1383 };
However, this structure is used as a member in other structs, in this
case in `struct visconti_pll_provider`:
drivers/clk/visconti/pll.h:
16 struct visconti_pll_provider {
17 void __iomem *reg_base;
18 struct device_node *node;
19
20 /* Must be last */
21 struct clk_hw_onecell_data clk_data;
22 };
Hence, we need to move the assignments to `ctx->clk_data.num` after
allocation for `struct visconti_pll_provider` and before accessing the
flexible array `ctx->clk_data.hws`. And, as assignments for all members
in `struct visconti_pll_provider` are originally adjacent to each other,
relocate all assignments together, so we don't split up
`ctx->clk_data.hws = nr_plls` from the rest. :)
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
drivers/clk/visconti/pll.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
Comments
On Wed, Oct 11, 2023 at 08:18:45PM -0600, Gustavo A. R. Silva wrote: > In order to gain the bounds-checking coverage that __counted_by provides > to flexible-array members at run-time via CONFIG_UBSAN_BOUNDS (for array > indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions), > we must make sure that the counter member, in this particular case `num`, > is updated before the first access to the flex-array member, in this > particular case array `hws`. See below: > > commit f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with > __counted_by") introduced `__counted_by` for `struct clk_hw_onecell_data` > together with changes to relocate some of assignments of counter `num` > before `hws` is accessed: > > include/linux/clk-provider.h: > 1380 struct clk_hw_onecell_data { > 1381 unsigned int num; > 1382 struct clk_hw *hws[] __counted_by(num); > 1383 }; > > However, this structure is used as a member in other structs, in this > case in `struct visconti_pll_provider`: > > drivers/clk/visconti/pll.h: > 16 struct visconti_pll_provider { > 17 void __iomem *reg_base; > 18 struct device_node *node; > 19 > 20 /* Must be last */ > 21 struct clk_hw_onecell_data clk_data; > 22 }; > > Hence, we need to move the assignments to `ctx->clk_data.num` after > allocation for `struct visconti_pll_provider` and before accessing the > flexible array `ctx->clk_data.hws`. And, as assignments for all members > in `struct visconti_pll_provider` are originally adjacent to each other, > relocate all assignments together, so we don't split up > `ctx->clk_data.hws = nr_plls` from the rest. :) > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Looks good; thanks! Reviewed-by: Kees Cook <keescook@chromium.org>
Hi Gustavo A. R. Silva, Thanks for your patch! > -----Original Message----- > From: Gustavo A. R. Silva <gustavoars@kernel.org> > Sent: Thursday, October 12, 2023 11:19 AM > To: Michael Turquette <mturquette@baylibre.com>; Stephen Boyd > <sboyd@kernel.org>; iwamatsu nobuhiro(岩松 信洋 ○DITC□DIT○O > ST) <nobuhiro1.iwamatsu@toshiba.co.jp> > Cc: Kees Cook <keescook@chromium.org>; linux-clk@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 2/2][next] clk: visconti: Add bounds-checking coverage for > struct visconti_pll_provider > > In order to gain the bounds-checking coverage that __counted_by provides to > flexible-array members at run-time via CONFIG_UBSAN_BOUNDS (for array > indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family > functions), we must make sure that the counter member, in this particular case > `num`, is updated before the first access to the flex-array member, in this > particular case array `hws`. See below: > > commit f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with > __counted_by") introduced `__counted_by` for `struct clk_hw_onecell_data` > together with changes to relocate some of assignments of counter `num` > before `hws` is accessed: > > include/linux/clk-provider.h: > 1380 struct clk_hw_onecell_data { > 1381 unsigned int num; > 1382 struct clk_hw *hws[] __counted_by(num); > 1383 }; > > However, this structure is used as a member in other structs, in this case in > `struct visconti_pll_provider`: > > drivers/clk/visconti/pll.h: > 16 struct visconti_pll_provider { > 17 void __iomem *reg_base; > 18 struct device_node *node; > 19 > 20 /* Must be last */ > 21 struct clk_hw_onecell_data clk_data; > 22 }; > > Hence, we need to move the assignments to `ctx->clk_data.num` after > allocation for `struct visconti_pll_provider` and before accessing the flexible > array `ctx->clk_data.hws`. And, as assignments for all members in `struct > visconti_pll_provider` are originally adjacent to each other, relocate all > assignments together, so we don't split up `ctx->clk_data.hws = nr_plls` from > the rest. :) > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Acked-by: Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp> Best regards, Nobuhiro
diff --git a/drivers/clk/visconti/pll.c b/drivers/clk/visconti/pll.c index 1f3234f22667..e9cd80e085dc 100644 --- a/drivers/clk/visconti/pll.c +++ b/drivers/clk/visconti/pll.c @@ -329,12 +329,12 @@ struct visconti_pll_provider * __init visconti_init_pll(struct device_node *np, if (!ctx) return ERR_PTR(-ENOMEM); - for (i = 0; i < nr_plls; ++i) - ctx->clk_data.hws[i] = ERR_PTR(-ENOENT); - ctx->node = np; ctx->reg_base = base; ctx->clk_data.num = nr_plls; + for (i = 0; i < nr_plls; ++i) + ctx->clk_data.hws[i] = ERR_PTR(-ENOENT); + return ctx; }