Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2344969imm; Thu, 23 Aug 2018 19:59:53 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYvGT5lcWut1dnGnfj/5w0Eh286+MbZGf0ZmMq8kBudH2UJbzhfkZjbzGYexI93SsmpgDqb X-Received: by 2002:a63:1f13:: with SMTP id f19-v6mr7794890pgf.166.1535079593073; Thu, 23 Aug 2018 19:59:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535079593; cv=none; d=google.com; s=arc-20160816; b=VfFHN65j0Ulofal4VIj5zcNyONXOZEw8H3mo6qRAMBZ5wNE726f8dQz3cA5lA5038S o/uvfBnP+S3D/6WipQiDdhIg0IWQ/TK9LvfSzsqzyBGd2H7qa3OB0LjrbGuVraOuwJIn iMUNObGooMwc42paLaZcY/EZHKKB+kkn56Erp2nEFPFsiN/ye6ZXgWeeiqGDA2pH5bL/ u536J07bPE5VijQ6cohS+1eyvZ15RBDQReP5Yux3szCP2mH3WdvmmWDlOQa568QhGaA1 jKNuKDzsJ32Y1FXaQG+wR6ph9ZwtwyNxG7kjfWAyyhQxBawWwX7KIY4IGhJdFqvuYH7l X7TQ== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=S1Ln40Qo/8/OYYmu4Bg/icBVUigxwP0yBnsvo4Qnj6E=; b=sNjlLq0E1XsJKZ2Bq/NyOExeCsqxBZgitQC+eemAfMo2OEm9IpbDzTI/ITEVNx4yMM ebhQkuKbTd4o0KprYXVR1dBu49VWWFNS1z31NHYSr+5ZhzYpSOP0Br1pSWi8FQwzhc9V k7nw7dAPJe91SAELBzReNK0uMyLH3ATCwWTYTcDR0+7fjBhMOp+nWiJDdGvK+Z56OBAL 5q8DQ11TCD7Iu6f3BsOZynhnmwEZcgptwd1OXvclRj9m8v6oCxVNsUd8GYG17UV2hZnS vJBttP3PQ2UE84UQN4DhcuXx3XAbHXdGiGIJY2AX9gsvMWrm8POnEO9qV18SWUYGKXyn Ao8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XsWeknpg; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3-v6si5602748plx.173.2018.08.23.19.59.38; Thu, 23 Aug 2018 19:59:53 -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=@chromium.org header.s=google header.b=XsWeknpg; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727405AbeHXGaO (ORCPT + 99 others); Fri, 24 Aug 2018 02:30:14 -0400 Received: from mail-yb0-f196.google.com ([209.85.213.196]:38131 "EHLO mail-yb0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726174AbeHXGaO (ORCPT ); Fri, 24 Aug 2018 02:30:14 -0400 Received: by mail-yb0-f196.google.com with SMTP id c1-v6so2990765ybq.5 for ; Thu, 23 Aug 2018 19:57:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=S1Ln40Qo/8/OYYmu4Bg/icBVUigxwP0yBnsvo4Qnj6E=; b=XsWeknpgKicWAVxHmqROS7UdnCIJAZVA+Vh/1RWDCQmfPJePK53dZWkRYCn1osRNOI cOGcaeQDMnBU+5GURiSdKx5Hg/ltuo876FsRk0ks5ObDR1niGJYSaHsbSE6JqGXHymUO 5rIOpe9zS+sU2yZCPISEK7HgOpY67RnRTsFNA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=S1Ln40Qo/8/OYYmu4Bg/icBVUigxwP0yBnsvo4Qnj6E=; b=PjstuqOBjooO51vqwHdc/d3Bat7uWMZ/6TSv24qY+f9/wlhrQeXN5x5epOsulBQX9R 9272/enJcYlw/uXWR04lFIK+w16U3ATHn7PvY3rXbZzXbU5VTZO9kRfJtZdgkKmF1A5t YJdCdF3+vOR/oILyXvNW3N9pgzeTGOt+t0ELosQd7vZ8f6R8bKs8oNM3wOIdMzqin+Gs eKjXhb6FNDYMgcL8I8G9nRckkO2B0RdsR4wtUboCGUEcAXubh6pSijS9/ezmyVYjsUJD a4GkNagoP4y2/7ZuP/Rv64dmC/3ulnLQwIb8aTTr7WgWHi5y5sRkL1dCeHuctevF7zkf pqtQ== X-Gm-Message-State: AOUpUlGF0nAgzVFDyFEajIIp2wKV9G3Q4jrNkhEpUpY7nUzR+o8h5TE1 KV0N16dIjl6H3ZIHlkQCyLNXfIlbiDA= X-Received: by 2002:a25:9909:: with SMTP id z9-v6mr12772785ybn.509.1535079458718; Thu, 23 Aug 2018 19:57:38 -0700 (PDT) Received: from mail-yb0-f177.google.com (mail-yb0-f177.google.com. [209.85.213.177]) by smtp.gmail.com with ESMTPSA id u191-v6sm2911817ywc.0.2018.08.23.19.57.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Aug 2018 19:57:37 -0700 (PDT) Received: by mail-yb0-f177.google.com with SMTP id f4-v6so2993556ybp.7 for ; Thu, 23 Aug 2018 19:57:36 -0700 (PDT) X-Received: by 2002:a25:103:: with SMTP id 3-v6mr34771409ybb.421.1535079456148; Thu, 23 Aug 2018 19:57:36 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:2c11:0:0:0:0:0 with HTTP; Thu, 23 Aug 2018 19:57:35 -0700 (PDT) In-Reply-To: <20180824010938.GA25581@embeddedor.com> References: <20180824010938.GA25581@embeddedor.com> From: Kees Cook Date: Thu, 23 Aug 2018 19:57:35 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] mtd: rawnand: atmel: use struct_size() in devm_kzalloc() To: "Gustavo A. R. Silva" Cc: Josh Wu , Boris Brezillon , Miquel Raynal , Richard Weinberger , David Woodhouse , Brian Norris , Marek Vasut , Nicolas Ferre , Alexandre Belloni , Linux mtd , linux-arm-kernel , LKML 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 Thu, Aug 23, 2018 at 6:09 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 = devm_kzalloc(dev, 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 = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL); > > This issue was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva Reviewed-by: Kees Cook -Kees > --- > drivers/mtd/nand/raw/atmel/nand-controller.c | 4 +--- > 1 file changed, 1 insertion(+), 3 deletions(-) > > diff --git a/drivers/mtd/nand/raw/atmel/nand-controller.c b/drivers/mtd/nand/raw/atmel/nand-controller.c > index a068b21..b17cfd0 100644 > --- a/drivers/mtd/nand/raw/atmel/nand-controller.c > +++ b/drivers/mtd/nand/raw/atmel/nand-controller.c > @@ -1586,9 +1586,7 @@ static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc, > return ERR_PTR(-EINVAL); > } > > - nand = devm_kzalloc(nc->dev, > - sizeof(*nand) + (numcs * sizeof(*nand->cs)), > - GFP_KERNEL); > + nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL); > if (!nand) { > dev_err(nc->dev, "Failed to allocate NAND object\n"); > return ERR_PTR(-ENOMEM); > -- > 2.7.4 > -- Kees Cook Pixel Security