Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp4939548imm; Sun, 26 Aug 2018 07:07:12 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdafg9cc0d1hYOVIJYbLmmTnhM3MV6GKbpe243w0XSwpv3oXKTNDIJkC0C2LGwRQse2zsNE3 X-Received: by 2002:a17:902:622:: with SMTP id 31-v6mr9289914plg.153.1535292432377; Sun, 26 Aug 2018 07:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535292432; cv=none; d=google.com; s=arc-20160816; b=aHD2+Wx6QTd0UN2pKkpG+Ca/Zu4Y1Av9Y2yYwfQeegk/xjw0vflyn82AfSU2q1jYQa nLyDiJ5Rq9Uh/2XLf6U/Ga5dLmNbue1Z/y7RfAxD3osRNoqHU6SZ8DIZGJjs5YTKlD82 cvO6n5semjRtM1ZNZM32wD12onNbVytseuZQ1q54oLmNFKsgZrGqdhJnS/3M6kQO4zUC uAaNjQa2c9iFe+pvt9da4nH9xNOjZD6IcmBbWXeoRI96pRyvzMZCTsHpJIjYKYlaiSHU aV3e+JsIkSvTMptCjPzzfSeQSnC4RlR0/nWsiVGtJ5i6anLI1L/HjtGem7lqyvmP79jx ha1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=iPwbEDTPByu5LSTSxhIZyxzfliU7zI5i4FxRIj1eT3s=; b=Kq75aXJqWgvDP3WlRIjcPbuunFTYypqwW3MkTROr24WN8kFNWbW9qZsOlAodGAkHw9 YqJFrW7cHXD/0xbTyI+sivy1gNDuKwWnzLzAdL5h5IPF+UuLm4tCLsZp49GSpTJm9voN /xmeyRDkiJ1dEw+FHx7UrybWWlEsKlcdw0owY4cTHaKWnXLrSQkIh2CzAAIVNgv7BuxV FkDtUptHSVMOV9i2wxKDp7XzMSYMsV7K47s8LvQxcRqwUPp9yeIZI7lqM2M8cCXYqvJr 8LHnVjtlbW4wKSpJIK1oT0SHotnL8Wj0MjbiExuHwls4uTqnVNHtt6/nx9GmBn7bcbzY mrpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oLKPFqfa; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b129-v6si12588346pfa.12.2018.08.26.07.06.56; Sun, 26 Aug 2018 07:07:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oLKPFqfa; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726883AbeHZRqd (ORCPT + 99 others); Sun, 26 Aug 2018 13:46:33 -0400 Received: from mail-it0-f66.google.com ([209.85.214.66]:38860 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726478AbeHZRqd (ORCPT ); Sun, 26 Aug 2018 13:46:33 -0400 Received: by mail-it0-f66.google.com with SMTP id p129-v6so7415708ite.3; Sun, 26 Aug 2018 07:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iPwbEDTPByu5LSTSxhIZyxzfliU7zI5i4FxRIj1eT3s=; b=oLKPFqfaCcdQ7TBLlnHTTfxDm26qdDAIDlHVg+jrhpVpuepk9PA7wrc+0v9mnGzBGv 7nxuVqxq5ne1iWa4xF/FPWZQWsJfsZqyqsr/ti4LGOUROiURzAaPwW2EN7yaAWrMJ+ZP GWs8Ofk9sWTA7y7uco1QAdSKPlmNaZqxDTtDi96OG+wlfsqrwiOfJkEIetQh0dGVi1M3 efuhn7fg7ccjjKN/Hvyf5cJFSTuH9NnSi2InYs1PN6b/riAYMqFSuGuYZAo4nmb61DOa fI+hgBVTXHCNQUtGMWNBM6rMp6JANDexNQFx0W2k8pSYN198uF0kD0QpEXOaRDOPbLn3 xaMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iPwbEDTPByu5LSTSxhIZyxzfliU7zI5i4FxRIj1eT3s=; b=mUVz9ujly+CxJMq+m+txVXeBpsj60MvqKhzPny+QHvJ5xzOONTZKa0nduiwtITNuVy xKz58EW231bYLaDwKpML8fSrAUvybEZVWis4rvIId3Ix1LzNi0iFkEjsi1yKevQZBfNX gY5e7vaseDoHpBSyoCCyRfkAXwImetwP4dhGBOvsMKIiFiM9eLHTCjZBvdJlVWh6BZ/o E58N+mMNdFaKUo2BBxMC2QYLUF06VVmyEojIYEWMkpWShE9PRjNWG4h+nj1rPPCMX5oR WIVZZG+7rBu2nb+8qo1PQxBe1nT0IxVqGXmD4C8z9qEdmF/bojK1VWCNTynpZwLV/Nm1 JxNw== X-Gm-Message-State: APzg51D+QCzrdGqMe2Ic1cLjxd9UZjhSBBk5XeVFZ7SFaR1IjfP+TNnO F96ZUIkICVskRIQG+f+JtoQuQLWz3WtiGZJH9Q== X-Received: by 2002:a02:2a48:: with SMTP id w69-v6mr1298270jaw.106.1535292232934; Sun, 26 Aug 2018 07:03:52 -0700 (PDT) MIME-Version: 1.0 References: <20180823230654.GA11576@embeddedor.com> In-Reply-To: From: Avi Fishman Date: Sun, 26 Aug 2018 17:03:33 +0300 Message-ID: Subject: Re: [PATCH] clk: npcm7xx: fix memory allocation To: keescook@chromium.org Cc: gustavo@embeddedor.com, Tomer Maimon , Patrick Venture , Nancy Yuen , Brendan Higgins , mturquette@baylibre.com, sboyd@kernel.org, OpenBMC Maillist , linux-clk@vger.kernel.org, Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 24, 2018 at 2:18 AM Kees Cook wrote: > > On Thu, Aug 23, 2018 at 4:06 PM, Gustavo A. R. Silva > wrote: > > One of the more common cases of allocation size calculations is finding > > the size of a structure that has a zero-sized array at the end, along > > with memory for some number of elements for that array. For example: > > > > struct foo { > > int stuff; > > void *entry[]; > > }; > > > > instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, > > GFP_KERNEL); > > > > Instead of leaving these open-coded and prone to type mistakes, we can > > now use the new struct_size() helper: > > > > instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL); > > > > Notice that, currently, there is a bug during the allocation: > > > > sizeof(npcm7xx_clk_data) should be sizeof(*npcm7xx_clk_data) > > > > Fix this bug by using struct_size() in kzalloc() > > > > This issue was detected with the help of Coccinelle. > > > > Cc: stable@vger.kernel.org > > Signed-off-by: Gustavo A. R. Silva > > Reviewed-by: Kees Cook Reviewed-by: Avi Fishman > > -Kees > > > --- > > drivers/clk/clk-npcm7xx.c | 4 ++-- > > 1 file changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/clk/clk-npcm7xx.c b/drivers/clk/clk-npcm7xx.c > > index 740af90..c5edf8f 100644 > > --- a/drivers/clk/clk-npcm7xx.c > > +++ b/drivers/clk/clk-npcm7xx.c > > @@ -558,8 +558,8 @@ static void __init npcm7xx_clk_init(struct device_node *clk_np) > > if (!clk_base) > > goto npcm7xx_init_error; > > > > - npcm7xx_clk_data = kzalloc(sizeof(*npcm7xx_clk_data->hws) * > > - NPCM7XX_NUM_CLOCKS + sizeof(npcm7xx_clk_data), GFP_KERNEL); > > + npcm7xx_clk_data = kzalloc(struct_size(npcm7xx_clk_data, hws, > > + NPCM7XX_NUM_CLOCKS), GFP_KERNEL); > > if (!npcm7xx_clk_data) > > goto npcm7xx_init_np_err; > > > > -- > > 2.7.4 > > > > > > -- > Kees Cook > Pixel Security -- Regards, Avi