Skip to contentSkip to conversation

audit: Use struct_size() helper in alloc_chunk

PATCH In review Gustavo A. R. Silva 2 participants 4 messages May 24, 2020 – Jun 17, 2020 Audit

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 audit: Use struct_size() helper in alloc_chunk

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 audit_chunk {
	...
        struct node {
                struct list_head list;
                struct audit_tree *owner;
                unsigned index;         /* index; upper bit indicates 'will prune' */
        } owners[];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

offsetof(struct audit_chunk, owners) + count * sizeof(struct node);

with:

struct_size(chunk, owners, count)

This code was detected with the help of Coccinelle.

---
 kernel/audit_tree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/audit_tree.c +1 -3
index e49c912f862d0..1b7a2f0417936 100644
@@ -188,11 +188,9 @@ static struct fsnotify_mark *alloc_mark(void)
 static struct audit_chunk *alloc_chunk(int count)
 {
 	struct audit_chunk *chunk;
-	size_t size;
 	int i;
 
-	size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
-	chunk = kzalloc(size, GFP_KERNEL);
+	chunk = kzalloc(struct_size(chunk, owners, count), GFP_KERNEL);
 	if (!chunk)
 		return NULL;
 
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 22 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 audit_chunk {
        ...
        struct node {
                struct list_head list;
                struct audit_tree *owner;
                unsigned index;         /* index; upper bit indicates 'will prune' */
        } owners[];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

offsetof(struct audit_chunk, owners) + count * sizeof(struct node);

with:

Quoted 8 lines

struct_size(chunk, owners, count)

This code was detected with the help of Coccinelle.

---
 kernel/audit_tree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Thanks, this looks reasonable to me, but it came in too late for the v5.8 merge window (I dislike taking changes past -rc5/6 unless critical). Once the merge window closes I'll merge this into audit/next.

Signature · 2 lines
paul moore
www.paul-moore.com
#2 lore raw
Quoted 8 lines · Paul Moore
Quoted 5 lines
---
 kernel/audit_tree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Thanks, this looks reasonable to me, but it came in too late for the v5.8 merge window (I dislike taking changes past -rc5/6 unless

Sounds sensible.

Quoted 3 lines

critical). Once the merge window closes I'll merge this into audit/next.

Thanks, Paul.
--
Gustavo

maintainer
#3 lore raw
In reply to Paul Moore
Quoted 33 lines · Paul Moore
Quoted 22 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 audit_chunk {
        ...
        struct node {
                struct list_head list;
                struct audit_tree *owner;
                unsigned index;         /* index; upper bit indicates 'will prune' */
        } owners[];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

offsetof(struct audit_chunk, owners) + count * sizeof(struct node);

with:

Quoted 8 lines

struct_size(chunk, owners, count)

This code was detected with the help of Coccinelle.

---
 kernel/audit_tree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 4 lines

Thanks, this looks reasonable to me, but it came in too late for the v5.8 merge window (I dislike taking changes past -rc5/6 unless critical). Once the merge window closes I'll merge this into audit/next.

FYI, I just merged this into audit/next. Thanks!

Signature · 2 lines
paul moore
www.paul-moore.com