clk: npcm7xx: fix memory allocation
Subsystems (2)
PATCH 1/1 clk: npcm7xx: fix memory allocation
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.
--- drivers/clk/clk-npcm7xx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk-npcm7xx.c +2 -2
@@ -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;
Signature · 1 line
2.7.4
Quoted 19 lines · 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:
Quoted 8 lines
sizeof(npcm7xx_clk_data)should besizeof(*npcm7xx_clk_data)Fix this bug by using
struct_size()inkzalloc()This issue was detected with the help of Coccinelle.
Signed-off-by Gustavo A. R. Silva <[email protected]>
-Kees
Quoted 22 lines
--- drivers/clk/clk-npcm7xx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/clk-npcm7xx.c +2
-3@@ -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
Signature · 2 lines
Kees Cook Pixel Security
Quoted 24 lines · Kees Cook
Quoted 21 lines · 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 besizeof(*npcm7xx_clk_data)
Quoted 8 lines
Quoted 6 lines
Fix this bug by using
struct_size()inkzalloc()This issue was detected with the help of Coccinelle.
Signed-off-by Gustavo A. R. Silva <[email protected]>Reviewed-by Kees Cook <[email protected]>
Quoted 31 lines
-Kees
Quoted 20 lines
--- drivers/clk/clk-npcm7xx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/clk-npcm7xx.c +2
-3@@ -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; --Quoted 2 lines
2.7.4
--
Kees Cook
Pixel Security
Signature · 2 lines
Regards, Avi
Quoting Gustavo A. R. Silva (2018-08-23 16:06:54)
Quoted 31 lines
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 =3D
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 =3D
kzalloc(struct_size(instance, entry, count), GFP_KERNEL); =Notice that, currently, there is a bug during the allocation:
=
sizeof(npcm7xx_clk_data)should besizeof(*npcm7xx_clk_data)=Fix this bug by using
struct_size()inkzalloc()
=
Quoted 6 lines
This issue was detected with the help of Coccinelle. =
Signed-off-by Gustavo A. R. Silva <[email protected]>---
Applied to clk-fixes