Skip to content

Commit fa5b571

Browse files
GustavoARSilvamimizohar
authored andcommitted
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. Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent cbc0425 commit fa5b571

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

security/integrity/ima/ima_template.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,8 @@ static int ima_restore_template_data(struct ima_template_desc *template_desc,
306306
int ret = 0;
307307
int i;
308308

309-
*entry = kzalloc(sizeof(**entry) +
310-
template_desc->num_fields * sizeof(struct ima_field_data),
311-
GFP_NOFS);
309+
*entry = kzalloc(struct_size(*entry, template_data,
310+
template_desc->num_fields), GFP_NOFS);
312311
if (!*entry)
313312
return -ENOMEM;
314313

0 commit comments

Comments
 (0)