Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2198930imm; Thu, 23 Aug 2018 16:19:52 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxuG5oWmYc87AFQF+1YJ/G6owSO1U58QN1kNlGTvfeS2sqiYuQ4uBx2E3lb4+DWSsJayCiq X-Received: by 2002:a17:902:4324:: with SMTP id i33-v6mr59363089pld.43.1535066392081; Thu, 23 Aug 2018 16:19:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535066392; cv=none; d=google.com; s=arc-20160816; b=bHFuwkKoSAPWuKZEJ1WAT6ToKONkY7mYJ0tUHgXvG8fxuzN2qwAaVHZvmpmnzxoWSa 9f4lE8KP0uvRgrZLvBDyNqGJXhCpu5vuR5WURNSeJBKBNSi7H2tvO5+BVTey3YKMGh/S e5C6P/0BKb/diHK9S3wWat9n61oQDQhkG44NvRBkRZlp8o2gIG8NhJ+4yRmJCV3bRcaf mGBOwwM7apy6gqFRxXNhpKkl/yM779KZ6FOD4T0ctu4FaWO664uzgGCnQfVRSrK5BL9Y yS+uVVjyWvJCLS5XrIbJnvt8oX8Fb7gJsUfbYaKKJULQSUXoNotGulPRiqFo7GFV45z7 ZgEQ== 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=LAES9gX+SQq3A9ke8uOhWLm+0UsOm6t3NtXOL5NYcnI=; b=s7FNN4aIVx7W2TasfYOJck9t/YOR/5T4uaESyU+TaT7hcRwAhG3gWCw12QQEK9uqWm PKBELLGKfE3G2h2QUgIv2xtbfjXe0j+m2hL6McCJswBZhpikjFGiSFHj2tSFptpMADt/ +g/kXOvYRw6Qb9c/qVFhveYVlJOjUm9hjUalV3EjNLmyeIZutZeFQ9dNZbyyU3/qHCCo i/R+UlDrv3u3lN213qU6JSDwLK4Ah0vlZ6gGYIbukwiJSSurXO124E+zKia3GnnxFgnh UaHlX9c08Wujqi7lt5G1Rhn1qo/CYJcdA+5zTr87XjYV6HJY4VbZZtJ6Ri8jMqrPEZp1 M0cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZxoAYnI3; 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 d10-v6si5442352pgg.341.2018.08.23.16.19.35; Thu, 23 Aug 2018 16:19:52 -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=ZxoAYnI3; 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 S1726364AbeHXCt7 (ORCPT + 99 others); Thu, 23 Aug 2018 22:49:59 -0400 Received: from mail-yb0-f195.google.com ([209.85.213.195]:42488 "EHLO mail-yb0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726156AbeHXCt7 (ORCPT ); Thu, 23 Aug 2018 22:49:59 -0400 Received: by mail-yb0-f195.google.com with SMTP id z12-v6so2835765ybg.9 for ; Thu, 23 Aug 2018 16:18:03 -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=LAES9gX+SQq3A9ke8uOhWLm+0UsOm6t3NtXOL5NYcnI=; b=ZxoAYnI3EGLzTUdoHMyRso57uRnVnLAPGhO299G5ht4gtpAL5E8ABAyOJCer6dMF45 GB+7a9e1SvCd7UL4nxccrvruIgHP46Pv8aqMNR4G2V8ONGkTaH7L7BbKOm3nvmHK97DQ Jc7CtCN4jIaj6bBjKY9HN5Hm9lwVmVzj6nV1o= 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=LAES9gX+SQq3A9ke8uOhWLm+0UsOm6t3NtXOL5NYcnI=; b=NYRJR6wSlkx85DbfcFOY4//rSSsgPA09nvOOymUQJAvxwnWD1b0xeTGnjbs/ANVJ81 g/Sm9i22Ybq6Vx9CWO4mx0x36As1O0lIt7YsGWrERwhL3ieaytgCxwZSdX1sw5Ln+2qt X1LvB2XJCPLE0sbQU01LcnF5rPRtQHgJVYTQsqYeEwrYT4pmYgFBDOkMQtOirSeUwenV ymhL7dnWjac6GECpXtHh3M8Q+NaWISSFfPT3TmXAVf0dMRCStR7TcN5G7Ic8efMjlQ5k G7GhbtWvxcP3D7O7RvS+plnCLVPcQbnVYtABTTFmvpjNiHUz+IOJuCP0+SkLe5DPyHD8 uIKw== X-Gm-Message-State: AOUpUlEVNvqAzQhjlMt/8TaD85Ym+8rhw2qZiFbT7ALDkrfKfPvE10z6 iV9LFJ1LQTRYHHjguen8qjPw1qnQGtI= X-Received: by 2002:a25:8b85:: with SMTP id j5-v6mr34213526ybl.174.1535066283091; Thu, 23 Aug 2018 16:18:03 -0700 (PDT) Received: from mail-yb0-f169.google.com (mail-yb0-f169.google.com. [209.85.213.169]) by smtp.gmail.com with ESMTPSA id w80-v6sm1406495ywd.55.2018.08.23.16.18.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Aug 2018 16:18:00 -0700 (PDT) Received: by mail-yb0-f169.google.com with SMTP id o17-v6so2848774yba.2 for ; Thu, 23 Aug 2018 16:18:00 -0700 (PDT) X-Received: by 2002:a25:103:: with SMTP id 3-v6mr34452501ybb.421.1535066280062; Thu, 23 Aug 2018 16:18:00 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:2c11:0:0:0:0:0 with HTTP; Thu, 23 Aug 2018 16:17:59 -0700 (PDT) In-Reply-To: <20180823230654.GA11576@embeddedor.com> References: <20180823230654.GA11576@embeddedor.com> From: Kees Cook Date: Thu, 23 Aug 2018 16:17:59 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] clk: npcm7xx: fix memory allocation To: "Gustavo A. R. Silva" Cc: Avi Fishman , Tomer Maimon , Patrick Venture , Nancy Yuen , Brendan Higgins , Michael Turquette , Stephen Boyd , openbmc@lists.ozlabs.org, linux-clk@vger.kernel.org, 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 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 -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