From mboxrd@z Thu Jan 1 00:00:00 1970 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 1CC16CDB46E for ; Thu, 12 Oct 2023 16:22:01 +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:In-Reply-To:MIME-Version:References: 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: List-Owner; bh=eiLIrf6Zx264PywqsP4PUbWdwY8YsjZevTbnxWHxXSA=; b=i/sl4Vc+iu/Xu3 rKj87zFraHwK1svlirEO6p7VF/t2zIcqM8EKeuqGoaPC1s1zcr5VVhE5zfrDyD/PcKTUsW5pIoH/R sobmZEeu3X/gnA6KStgQin5sbMtJMSLUdGRn0/DtrNziF71LwZrY41WM8Ma81sEyagNHtPcrJ8nDE 5kVcYc7RwPvYSU7c8pYw8cDxPXcdsghrO5ThliskkrrKLPN/KRCDvIYwhQt8F98YoeOVQ3/SJIUPV +IETK7zx+icEdnMTdoxH81xfhRY9kkvTjowXOPnIBR9KSa2DTklm/6RhbStI7vKdg/ydUiRqmPlZT B0IsYR828NzKtlAkyl8A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qqyRK-001Ohz-0l; Thu, 12 Oct 2023 16:21:34 +0000 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qqyRH-001Ogu-1h for linux-arm-kernel@lists.infradead.org; Thu, 12 Oct 2023 16:21:32 +0000 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-58d26cfe863so846619a12.2 for ; Thu, 12 Oct 2023 09:21:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697127689; x=1697732489; darn=lists.infradead.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=kfZzbxyACyGprhFXrtZNBfWsxrOSC3ErxsTerbzJFPoKNgONpndNeSSBXKcGa1CeSx xsx7LlR58RWX7KKSkAGOCDs8H0ahvfEs2Lsh1GG6/iOtydR+L4VYQeytu9TttqVeUeSc d/VeqzofqrArg9Cbc2wpL0y/Mwi8AONdHzm3s= 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=Lw9AsY3t+UmpXJIdfGzVUTCrBzKRkqL8hQsJKAKyO5nEeDmLKGH3r4rqYWyuibUxAY WTeHX+vyE5AR5B28csbPPUAkDQvE3Fo7YG3KTHQY1L6FNZLny5FeqtILsJm0GMtDBNKr +Pric6jpe/EemyEp87S+EEHfDNNLR5mquMZOVMSCTM9IbOWH5hHUfKK61lUvCzGN6TAy Sk6FtaCrtv5UUTDj83dvXR+wb1jFkrQyya//Sa5MxilwXDFvYphE6V7RJr5Xmjm++kIP J2py7zn5K2WjpTeueG/aGLxJ/AsvYOhLIxzwSQnRHFpa3mr3UuHrRrfJumoAP/Z83JQt RfsQ== X-Gm-Message-State: AOJu0YykD2gSc9Y8U8cSbC7PYCacnnyDbBPoLlJxnVT/LJHA1jH+yxxi L1RHTA1nCFi3t3VdZKq4Wy3iKg== X-Google-Smtp-Source: AGHT+IFeOYmmrH1RYpChLn+mja+VTaPqsmPBs6JvFB7bcaiNtAxd4zJhq+olfYBAqqyB17X/qvWusw== 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-Disposition: inline In-Reply-To: <0a59a721d54b557076cc94eabeb694d463773204.1697076650.git.gustavoars@kernel.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231012_092131_593483_945B09D5 X-CRM114-Status: GOOD ( 20.63 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel