Message ID | 5dd8483177dc8cd91d021170b6717f2e570bab03.1697059539.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 ib8csp828164vqb; Wed, 11 Oct 2023 14:34:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHG4ulSIqYkcs8Xi5/hZ6ibZzrTgoaIL9EEv7p/rdCBq+Y5wgoDfSVLiEyHJpDbURA3plIm X-Received: by 2002:a17:902:dac8:b0:1bf:1a9e:85f7 with SMTP id q8-20020a170902dac800b001bf1a9e85f7mr25530473plx.1.1697060076208; Wed, 11 Oct 2023 14:34:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697060076; cv=none; d=google.com; s=arc-20160816; b=0Mwz8hcfyW2hNUY1pX3e1kqeucGwh8LgiB9WT75d7h74jE8DOjhPt7PNvzAuzQbDl3 2y+DvFrCThJI5FsfstkEtm/KgdgTxJY5mWYT+jkT53hwIoIG5bC3lPZoJCx6AZzTptTq QRl82jIJvQeV7o4YcFG+eM75RFb3KPm4qEaT4gxpkwSUl3Ry1Qgl82UVQ0XlD9t6uVmz YNFdSA877bXUmfkeLx4iVinp8fySjUNXuGBjRJQ4B8PdqHL4110uTsj9gkBJGOceDi32 XrNy0rbJesxaq755Xe3dLkviwHHR+FKv1l8olinkgEr2BljcFR7oN9alnsk+LpMUkZgd YacA== 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=72qoNRPA51RrNutmMc4gSYb9yUZFETq0clpw8FRduq8=; fh=uBtqv/kPFyoqWehSaASRS515pQ2wFqsb3kRG8pu85CU=; b=AZRfo/8KxEhQmckIxtS89uoIHH0mzIyS/XkTSzk3TezJZJC86VbAXFL+8a+L+JfG4Q DVZ1hwM9iMnKEMf9Xd3rGqQuUhBBChNAQneaa8xCREqQMRJ109P85E4aEGCjUIVN85xg Tmckk8jLVwlPk61sw6iaFOPjnPGGWIMk8lgWa1d0A5cHnZ/hYBwTDXK22lz75q1guvD7 0vWGM+ODT68tf9MuFPpZx4QQrFsBsS6QkahMK2iyugSbPjyOdzMuBR9wpq6yReqUMc9N fjUrwYyK4pv2YNC9TaSLY6dpiVdFIu0/6vD7nrSYtJLpGgpuRiFivJKE7F6kgviqBjGV LQLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u+nF+k1S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id ik24-20020a170902ab1800b001b8922e82e3si553387plb.297.2023.10.11.14.34.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 14:34:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u+nF+k1S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id 29FB38120446; Wed, 11 Oct 2023 14:34:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233625AbjJKVeL (ORCPT <rfc822;kartikey406@gmail.com> + 18 others); Wed, 11 Oct 2023 17:34:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233321AbjJKVeI (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 11 Oct 2023 17:34:08 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49F99A9 for <linux-kernel@vger.kernel.org>; Wed, 11 Oct 2023 14:34:07 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16C73C433C9; Wed, 11 Oct 2023 21:34:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697060046; bh=y/Fx1W1Dn15dJIy+URSYMgLaWxu8v3pvpDRK48RdAb0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=u+nF+k1SL6CHMixjHxnimulZbbZAFPaR3NXAiaVg8Vdo/6cENXSW233w5FmBuM2Qf FRp7kU6PaI8MHcYGobSkUnDdAGlNT5KnanhxcxDaK3XqOGoNh4jz0bjx5bv2MYNj1D IFJahx5m1FkNZeKyEV0JeprMMRLFg5JdfwnmDDg7lslsNT+GkQeJ+bZBNlPp1OsEAo pr12Loo2r1/DLle1IFW6IGai95whr+vUj2CocSMHTvm01+FNs4Uazmm7+C4jQ3Ztq2 KTBrDEaaC5jEQXVl8HNGCGCJsEZk9fz1LSdk70bAuF80grZIhFeMGasg1qzmLhNDu4 xYCigAlhZty6w== Date: Wed, 11 Oct 2023 15:34:03 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Dinh Nguyen <dinguyen@kernel.org>, Michael Turquette <mturquette@baylibre.com>, Stephen Boyd <sboyd@kernel.org> Cc: Kees Cook <keescook@chromium.org>, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH 1/2][next] clk: socfpga: Fix undefined behavior bug in struct stratix10_clock_data Message-ID: <5dd8483177dc8cd91d021170b6717f2e570bab03.1697059539.git.gustavoars@kernel.org> References: <cover.1697059539.git.gustavoars@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <cover.1697059539.git.gustavoars@kernel.org> X-Spam-Status: No, score=2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Wed, 11 Oct 2023 14:34:34 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779496466749332660 X-GMAIL-MSGID: 1779496466749332660 |
Series |
Fix undefined behavior bug and add bounds-checking coverage
|
|
Commit Message
Gustavo A. R. Silva
Oct. 11, 2023, 9:34 p.m. UTC
`struct clk_hw_onecell_data` is a flexible structure, which means that
it contains flexible-array member at the bottom, in this case array
`hws`:
include/linux/clk-provider.h:
1380 struct clk_hw_onecell_data {
1381 unsigned int num;
1382 struct clk_hw *hws[] __counted_by(num);
1383 };
This could potentially lead to an overwrite of the objects following
`clk_data` in `struct stratix10_clock_data`, in this case
`void __iomem *base;` at run-time:
drivers/clk/socfpga/stratix10-clk.h:
9 struct stratix10_clock_data {
10 struct clk_hw_onecell_data clk_data;
11 void __iomem *base;
12 };
There are currently three different places where memory is allocated for
`struct stratix10_clock_data`, including the flex-array `hws` in
`struct clk_hw_onecell_data`:
drivers/clk/socfpga/clk-agilex.c:
469 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
470 num_clks), GFP_KERNEL);
drivers/clk/socfpga/clk-agilex.c:
509 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
510 num_clks), GFP_KERNEL);
drivers/clk/socfpga/clk-s10.c:
400 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
401 num_clks), GFP_KERNEL);
I'll use just one of them to describe the issue. See below.
Notice that a total of 440 bytes are allocated for flexible-array member
`hws` at line 469:
include/dt-bindings/clock/agilex-clock.h:
70 #define AGILEX_NUM_CLKS 55
drivers/clk/socfpga/clk-agilex.c:
459 struct stratix10_clock_data *clk_data;
460 void __iomem *base;
...
466
467 num_clks = AGILEX_NUM_CLKS;
468
469 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
470 num_clks), GFP_KERNEL);
`struct_size(clk_data, clk_data.hws, num_clks)` above translates to
sizeof(struct stratix10_clock_data) + sizeof(struct clk_hw *) * 55 ==
16 + 8 * 55 == 16 + 440
^^^
|
allocated bytes for flex-array `hws`
474 for (i = 0; i < num_clks; i++)
475 clk_data->clk_data.hws[i] = ERR_PTR(-ENOENT);
476
477 clk_data->base = base;
and then some data is written into both `hws` and `base` objects.
Fix this by placing the declaration of object `clk_data` at the end of
`struct stratix10_clock_data`. Also, add a comment to make it clear
that this object must always be last in the structure.
Fixes: ba7e258425ac ("clk: socfpga: Convert to s10/agilex/n5x to use clk_hw")
Cc: stable@vger.kernel.org
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
drivers/clk/socfpga/stratix10-clk.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
Comments
On Wed, Oct 11, 2023 at 03:34:03PM -0600, Gustavo A. R. Silva wrote: > `struct clk_hw_onecell_data` is a flexible structure, which means that > it contains flexible-array member at the bottom, in this case array > `hws`: > > include/linux/clk-provider.h: > 1380 struct clk_hw_onecell_data { > 1381 unsigned int num; > 1382 struct clk_hw *hws[] __counted_by(num); > 1383 }; > > This could potentially lead to an overwrite of the objects following > `clk_data` in `struct stratix10_clock_data`, in this case > `void __iomem *base;` at run-time: > > drivers/clk/socfpga/stratix10-clk.h: > 9 struct stratix10_clock_data { > 10 struct clk_hw_onecell_data clk_data; > 11 void __iomem *base; > 12 }; > > There are currently three different places where memory is allocated for > `struct stratix10_clock_data`, including the flex-array `hws` in > `struct clk_hw_onecell_data`: > > drivers/clk/socfpga/clk-agilex.c: > 469 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws, > 470 num_clks), GFP_KERNEL); > > drivers/clk/socfpga/clk-agilex.c: > 509 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws, > 510 num_clks), GFP_KERNEL); > > drivers/clk/socfpga/clk-s10.c: > 400 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws, > 401 num_clks), GFP_KERNEL); > > I'll use just one of them to describe the issue. See below. > > Notice that a total of 440 bytes are allocated for flexible-array member > `hws` at line 469: > > include/dt-bindings/clock/agilex-clock.h: > 70 #define AGILEX_NUM_CLKS 55 > > drivers/clk/socfpga/clk-agilex.c: > 459 struct stratix10_clock_data *clk_data; > 460 void __iomem *base; > ... > 466 > 467 num_clks = AGILEX_NUM_CLKS; > 468 > 469 clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws, > 470 num_clks), GFP_KERNEL); > > `struct_size(clk_data, clk_data.hws, num_clks)` above translates to > sizeof(struct stratix10_clock_data) + sizeof(struct clk_hw *) * 55 == > 16 + 8 * 55 == 16 + 440 > ^^^ > | > allocated bytes for flex-array `hws` > > 474 for (i = 0; i < num_clks; i++) > 475 clk_data->clk_data.hws[i] = ERR_PTR(-ENOENT); > 476 > 477 clk_data->base = base; > > and then some data is written into both `hws` and `base` objects. > > Fix this by placing the declaration of object `clk_data` at the end of > `struct stratix10_clock_data`. Also, add a comment to make it clear > that this object must always be last in the structure. > > Fixes: ba7e258425ac ("clk: socfpga: Convert to s10/agilex/n5x to use clk_hw") > Cc: stable@vger.kernel.org > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Nice find! Reviewed-by: Kees Cook <keescook@chromium.org>
>> Fixes: ba7e258425ac ("clk: socfpga: Convert to s10/agilex/n5x to use clk_hw") >> Cc: stable@vger.kernel.org >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > > Nice find! :D > > Reviewed-by: Kees Cook <keescook@chromium.org> > Thanks! -- Gustavo
diff --git a/drivers/clk/socfpga/stratix10-clk.h b/drivers/clk/socfpga/stratix10-clk.h index 75234e0783e1..83fe4eb3133c 100644 --- a/drivers/clk/socfpga/stratix10-clk.h +++ b/drivers/clk/socfpga/stratix10-clk.h @@ -7,8 +7,10 @@ #define __STRATIX10_CLK_H struct stratix10_clock_data { - struct clk_hw_onecell_data clk_data; void __iomem *base; + + /* Must be last */ + struct clk_hw_onecell_data clk_data; }; struct stratix10_pll_clock {