Skip to contentSkip to conversation

ima: use struct_size() in kzalloc()

PATCH In review Gustavo A. R. Silva 2 participants 6 messages May 29, 2019 – Aug 29, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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
index b631b8bc7624..b945dff2ed14 100644
@@ -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
maintainer
#2 lore raw
In reply to Gustavo A. R. Silva
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.

Mimi

#1 lore raw
In reply to Gustavo A. R. Silva

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.

---
 security/integrity/ima/ima_template.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
security/integrity/ima/ima_template.c +2 -3
index b631b8bc7624..b945dff2ed14 100644
@@ -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;
 
#3 lore raw
In reply to Mimi Zohar
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

maintainer
#4 lore raw
In reply to Gustavo A. R. Silva

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

---
 security/integrity/ima/ima_template.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
security/integrity/ima/ima_template.c +0 -0
index b631b8bc7624..b945dff2ed14 100644
@@ -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

#5 lore raw

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