ima: use struct_size() in kzalloc()
Subsystems (4)
PATCH 1/1 ima: use struct_size() in kzalloc()
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;
struct boo entry[];
};
instance = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), 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);
This code was detected with the help of Coccinelle.
--- security/integrity/ima/ima_template.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
security/integrity/ima/ima_template.c +2 -3
@@ -281,9 +281,8 @@ static int ima_restore_template_data(struct ima_template_desc *template_desc,int ret =0 ;int i; - *entry =kzalloc (sizeof (**entry) + - template_desc->num_fields *sizeof (struct ima_field_data ), -GFP_NOFS ); + *entry =kzalloc (struct_size (*entry, template_data, + template_desc->num_fields),GFP_NOFS );if (!*entry)return -ENOMEM ;
Signature · 1 line
2.21.0
Mimi Zohar
maintainerQuoted 5 lines · Gustavo A. R. Silva
Hi all,
Friendly ping:
Who can take this, please?
Thank you for the reminder. I'm just getting back from LSS and a very short vacation. I'll look at it shortly.
Mimi
Hi all,
Friendly ping:
Who can take this, please?
Thanks
--
Gustavo
Quoted 40 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; struct boo entry[]; };instance =
kzalloc(sizeof(struct foo) + count * sizeof(struct boo), 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);This code was detected with the help of Coccinelle.
Signed-off-by Gustavo A. R. Silva <[email protected]>--- security/integrity/ima/ima_template.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)security/integrity/ima/ima_template.c +2
-3@@ -281,9 +281,8 @@ static int ima_restore_template_data(struct ima_template_desc *template_desc,int ret =0 ;int i; - *entry =kzalloc (sizeof (**entry) + - template_desc->num_fields *sizeof (struct ima_field_data ), -GFP_NOFS ); + *entry =kzalloc (struct_size (*entry, template_data, + template_desc->num_fields),GFP_NOFS );if (!*entry)return -ENOMEM ;
Quoted 10 lines · Mimi Zohar
Quoted 5 lines · Gustavo A. R. Silva
Hi all,
Friendly ping:
Who can take this, please?
Thank you for the reminder. I'm just getting back from LSS and a very short vacation. I'll look at it shortly.
Thanks, Mimi.
--
Gustavo
Mimi Zohar
maintainerHi Gustavo,
Quoted 32 lines · Gustavo A. R. Silva
Quoted 31 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; struct boo entry[]; };instance =
kzalloc(sizeof(struct foo) + count * sizeof(struct boo), 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);This code was detected with the help of Coccinelle.
Signed-off-by Gustavo A. R. Silva <[email protected]>--- security/integrity/ima/ima_template.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)security/integrity/ima/ima_template.c +0
-0@@ -281,9 +281,8 @@ static int ima_restore_template_data(struct ima_template_desc *template_desc,int ret =0 ;int i;
Quoted 9 lines
security/integrity/ima/ima_template.c static int ima_restore_template_data(struct ima_template_desc *template_desc, · line 288
Quoted 9 lines
security/integrity/ima/ima_template.c +2
-3- *entry =kzalloc (sizeof (**entry) + - template_desc->num_fields *sizeof (struct ima_field_data ), -GFP_NOFS ); + *entry =kzalloc (struct_size (*entry, template_data, + template_desc->num_fields),GFP_NOFS );if (!*entry)return -ENOMEM ;
The same usage exists in ima_api.c: ima_alloc_init_template(). Did you want to make the change there as well?
thanks,
Mimi
Hi Mimi,
Quoted 4 lines · Mimi Zohar
The same usage exists in
ima_api.c:ima_alloc_init_template(). Did you want to make the change there as well?
Yep. I'll write a patch for that one too.
Thanks
--
Gustavo