All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
Cc: Avi Fishman <avifishman70@gmail.com>,
	Tomer Maimon <tmaimon77@gmail.com>,
	Patrick Venture <venture@google.com>,
	Nancy Yuen <yuenn@google.com>,
	Brendan Higgins <brendanhiggins@google.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>,
	openbmc@lists.ozlabs.org, linux-clk@vger.kernel.org,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] clk: npcm7xx: fix memory allocation
Date: Thu, 23 Aug 2018 16:17:59 -0700	[thread overview]
Message-ID: <CAGXu5jK7VQSo=xsYVp5DPmUgULNt+wopyAXnK20z+LxZtyPfZA@mail.gmail.com> (raw)
In-Reply-To: <20180823230654.GA11576@embeddedor.com>

On Thu, Aug 23, 2018 at 4:06 PM, Gustavo A. R. Silva
<gustavo@embeddedor.com> 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 <gustavo@embeddedor.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-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

  reply	other threads:[~2018-08-23 23:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-23 23:06 [PATCH] clk: npcm7xx: fix memory allocation Gustavo A. R. Silva
2018-08-23 23:17 ` Kees Cook [this message]
2018-08-23 23:17   ` Kees Cook
2018-08-26 14:03   ` Avi Fishman
2018-08-26 14:03     ` Avi Fishman
2018-08-28 22:13 ` Stephen Boyd
2018-08-28 22:13   ` Stephen Boyd

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGXu5jK7VQSo=xsYVp5DPmUgULNt+wopyAXnK20z+LxZtyPfZA@mail.gmail.com' \
    --to=keescook@chromium.org \
    --cc=avifishman70@gmail.com \
    --cc=brendanhiggins@google.com \
    --cc=gustavo@embeddedor.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=openbmc@lists.ozlabs.org \
    --cc=sboyd@kernel.org \
    --cc=tmaimon77@gmail.com \
    --cc=venture@google.com \
    --cc=yuenn@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.