linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clk: npcm7xx: fix memory allocation
@ 2018-08-23 23:06 Gustavo A. R. Silva
  2018-08-23 23:17 ` Kees Cook
  2018-08-28 22:13 ` Stephen Boyd
  0 siblings, 2 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2018-08-23 23:06 UTC (permalink / raw)
  To: Avi Fishman, Tomer Maimon, Patrick Venture, Nancy Yuen,
	Brendan Higgins, Michael Turquette, Stephen Boyd
  Cc: openbmc, linux-clk, linux-kernel, Kees Cook, Gustavo A. R. Silva

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>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] clk: npcm7xx: fix memory allocation
  2018-08-23 23:06 [PATCH] clk: npcm7xx: fix memory allocation Gustavo A. R. Silva
@ 2018-08-23 23:17 ` Kees Cook
  2018-08-26 14:03   ` Avi Fishman
  2018-08-28 22:13 ` Stephen Boyd
  1 sibling, 1 reply; 4+ messages in thread
From: Kees Cook @ 2018-08-23 23:17 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Avi Fishman, Tomer Maimon, Patrick Venture, Nancy Yuen,
	Brendan Higgins, Michael Turquette, Stephen Boyd, openbmc,
	linux-clk, LKML

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] clk: npcm7xx: fix memory allocation
  2018-08-23 23:17 ` Kees Cook
@ 2018-08-26 14:03   ` Avi Fishman
  0 siblings, 0 replies; 4+ messages in thread
From: Avi Fishman @ 2018-08-26 14:03 UTC (permalink / raw)
  To: keescook
  Cc: gustavo, Tomer Maimon, Patrick Venture, Nancy Yuen,
	Brendan Higgins, mturquette, sboyd, OpenBMC Maillist, linux-clk,
	Linux Kernel Mailing List

On Fri, Aug 24, 2018 at 2:18 AM Kees Cook <keescook@chromium.org> wrote:
>
> 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>
Reviewed-by: Avi Fishman <avifishman70@gmail.com>
>
> -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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] clk: npcm7xx: fix memory allocation
  2018-08-23 23:06 [PATCH] clk: npcm7xx: fix memory allocation Gustavo A. R. Silva
  2018-08-23 23:17 ` Kees Cook
@ 2018-08-28 22:13 ` Stephen Boyd
  1 sibling, 0 replies; 4+ messages in thread
From: Stephen Boyd @ 2018-08-28 22:13 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Avi Fishman, Brendan Higgins,
	Michael Turquette, Nancy Yuen, Patrick Venture, Tomer Maimon
  Cc: openbmc, linux-clk, linux-kernel, Kees Cook, Gustavo A. R. Silva

Quoting Gustavo A. R. Silva (2018-08-23 16:06:54)
> 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>
> ---

Applied to clk-fixes


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-08-28 22:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-23 23:06 [PATCH] clk: npcm7xx: fix memory allocation Gustavo A. R. Silva
2018-08-23 23:17 ` Kees Cook
2018-08-26 14:03   ` Avi Fishman
2018-08-28 22:13 ` Stephen Boyd

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).