Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp423279rdg; Thu, 12 Oct 2023 09:21:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGAxPSb1GzK+XFtHHT9eQ/3F4ObZ6oJZvClFjuB2X6Wls427N/mCwNuBGAS1/rDxVi8PdRv X-Received: by 2002:a05:6e02:1b09:b0:351:ab2:2621 with SMTP id i9-20020a056e021b0900b003510ab22621mr36697930ilv.15.1697127705667; Thu, 12 Oct 2023 09:21:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697127705; cv=none; d=google.com; s=arc-20160816; b=Y4pnSwyQL/LHi4f63oYyRRM11GONk9WBpvMpaOu2XkPU0LDKSniB+ewgWYlXokVRky PQyfaqyOTKhEwOadeUpNYrUUlioDTBaxx19iWXVEda8RSJBx05tYv6zMQpnYG/4JDlgd tTT2vaRI6KqqviOHouA+9GMl9Yi8KE817MoXTGybNZWTKAWjvSaCRsAkFVtmDvl7FlgA Dv4sWpf3N2u76qER1I9NebPYuxdJqFFVjGC9g0ESX1JZgLPJFkFpbfKcKFADcE/oQo3/ 0KQeWmHmuAwQPPt34PN6Ff8GVLfzCPML/2uW815a85wPon48gigPOOBgwjU/J/3WguY0 Yb1Q== 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=yNWVejFTVcyzLtYuIOHAfjVjETmGqKuawTBPQ3WoJZ8=; fh=ODFDY2mJBwtLoOiyjFZppYqin3iZTagZvz5i9p4l5F4=; b=D1jaomvYmSS4oOp3bVlUIiow9vdMDnoY8zuHOtaf6cShHojvWpZJ2rY93EGWPyonlc GjchBQEgL0RchXZnpOybmFUZ07yn9n8bTTMqczcri0YRIF4aXHapUNT1++EHcpa9tmXm Kbqu70ek2Rkyh2/Yre+tsK54bSHbia+TEXEywG2Um7od/s+2/NuH4uP4IYannmgzCU2Z F89D/klSVrHEgusA9vYpo81HamkBVbupcp6eLYubOe3bkJKaTy43pHnXKy0UiwsuV80g HM2PzTEcla7vu3OBPWlW8ORh/56R1oF0/HrLYRTi2+EMcDUbPsfETm5tW9R6r9EkUT+1 DVjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XLNQFC9U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id bv8-20020a632e08000000b005a1d8816addsi2474607pgb.592.2023.10.12.09.21.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 09:21:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XLNQFC9U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 094A38212C69; Thu, 12 Oct 2023 09:21:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379523AbjJLQVf (ORCPT + 99 others); Thu, 12 Oct 2023 12:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235778AbjJLQVc (ORCPT ); Thu, 12 Oct 2023 12:21:32 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10E95F3 for ; Thu, 12 Oct 2023 09:21:30 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-690bc3f82a7so999685b3a.0 for ; Thu, 12 Oct 2023 09:21:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697127689; x=1697732489; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yNWVejFTVcyzLtYuIOHAfjVjETmGqKuawTBPQ3WoJZ8=; b=XLNQFC9UBO3HfdR3oXalLULMpw9+BsoxH1aiFtdfAI6w0Q+80xrIAwFjEnrKfpDvZr bfXnL2ejKZB7ZA/yi7S9xI0FYni/5L8R+VTaeMuxqldpFW6MvgVruqZc52hKgIggmwni x5HcEGpWOnPAU6YnYs9EY8WDroOpKwZm9TQ4g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697127689; x=1697732489; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yNWVejFTVcyzLtYuIOHAfjVjETmGqKuawTBPQ3WoJZ8=; b=KZlskyK2e3hDmpWdbOeXeyFcQLCuLk0TXK9KI3XYl/g/F1dVzaClAunFWVr94WTYyL IxROkHUwQgG9r4DifAArVznF+XH4IptnsAoRSZX1XOqEGveJMTvFxwcZLdajeyclA93O rdjbgd+JDqKUSTuZOpSUWcP9QqHcnlYI3WUyGrxCX+R94HX5AygwPguDE3r+HgSSk6Hi FvOzCGneUM80QeYJardlvQZdJ/F1o3Zd7dgcwByitBxtzxQC84yPyp08L59Xl/eFHLgP I94ZpKNjowfWHTPbgPtGMNL9WqYaOhCbNsmBuH/U92pGv78k2+ToVT2+N2OxnCQpZ4sH ReLw== X-Gm-Message-State: AOJu0Yzd7SlQ8j+hxO6O+i0iGpk265Le+Z57O9GISmuiNa+2uxCezNuq 9h2BQj6hLC6vf2pR11CDnzx9Ww== X-Received: by 2002:a05:6a20:7291:b0:157:b453:dbb9 with SMTP id o17-20020a056a20729100b00157b453dbb9mr26465216pzk.6.1697127689467; Thu, 12 Oct 2023 09:21:29 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id s5-20020a637705000000b00578b40a4903sm1929961pgc.22.2023.10.12.09.21.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 09:21:29 -0700 (PDT) Date: Thu, 12 Oct 2023 09:21:28 -0700 From: Kees Cook To: "Gustavo A. R. Silva" Cc: Michael Turquette , Stephen Boyd , Nobuhiro Iwamatsu , linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 1/2][next] clk: visconti: Fix undefined behavior bug in struct visconti_pll_provider Message-ID: <202310120921.08062349C7@keescook> References: <0a59a721d54b557076cc94eabeb694d463773204.1697076650.git.gustavoars@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0a59a721d54b557076cc94eabeb694d463773204.1697076650.git.gustavoars@kernel.org> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 12 Oct 2023 09:21:43 -0700 (PDT) On Wed, Oct 11, 2023 at 08:17:36PM -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 visconti_pll_provider`, in this case > `struct device_node *node;`, at run-time: > > drivers/clk/visconti/pll.h: > 16 struct visconti_pll_provider { > 17 void __iomem *reg_base; > 18 struct clk_hw_onecell_data clk_data; > 19 struct device_node *node; > 20 }; > > Notice that a total of 56 bytes are allocated for flexible-array `hws` > at line 328. See below: > > include/dt-bindings/clock/toshiba,tmpv770x.h: > 14 #define TMPV770X_NR_PLL 7 > > drivers/clk/visconti/pll-tmpv770x.c: > 69 ctx = visconti_init_pll(np, reg_base, TMPV770X_NR_PLL); > > drivers/clk/visconti/pll.c: > 321 struct visconti_pll_provider * __init visconti_init_pll(struct device_node *np, > 322 void __iomem *base, > 323 unsigned long nr_plls) > 324 { > 325 struct visconti_pll_provider *ctx; > ... > 328 ctx = kzalloc(struct_size(ctx, clk_data.hws, nr_plls), GFP_KERNEL); > > `struct_size(ctx, clk_data.hws, nr_plls)` above translates to > sizeof(struct visconti_pll_provider) + sizeof(struct clk_hw *) * 7 == > 24 + 8 * 7 == 24 + 56 > ^^^^ > | > allocated bytes for flex array `hws` > > $ pahole -C visconti_pll_provider drivers/clk/visconti/pll.o > struct visconti_pll_provider { > void * reg_base; /* 0 8 */ > struct clk_hw_onecell_data clk_data; /* 8 8 */ > struct device_node * node; /* 16 8 */ > > /* size: 24, cachelines: 1, members: 3 */ > /* last cacheline: 24 bytes */ > }; > > And then, after the allocation, some data is written into all members > of `struct visconti_pll_provider`: > > 332 for (i = 0; i < nr_plls; ++i) > 333 ctx->clk_data.hws[i] = ERR_PTR(-ENOENT); > 334 > 335 ctx->node = np; > 336 ctx->reg_base = base; > 337 ctx->clk_data.num = nr_plls; > > Fix all these by placing the declaration of object `clk_data` at the > end of `struct visconti_pll_provider`. Also, add a comment to make it > clear that this object must always be last in the structure, and > prevent this bug from being introduced again in the future. > > Fixes: b4cbe606dc36 ("clk: visconti: Add support common clock driver and reset driver") > Cc: stable@vger.kernel.org > Signed-off-by: Gustavo A. R. Silva Another good catch. :) Reviewed-by: Kees Cook -- Kees Cook