* [PATCH] audit: Use struct_size() helper in alloc_chunk
@ 2020-05-24 20:52 Gustavo A. R. Silva
2020-06-01 15:36 ` Paul Moore
0 siblings, 1 reply; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-05-24 20:52 UTC (permalink / raw)
To: Paul Moore, Eric Paris; +Cc: linux-audit, linux-kernel, 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:
struct_size(chunk, owners, count)
This code was detected with the help of Coccinelle.
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
kernel/audit_tree.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/kernel/audit_tree.c b/kernel/audit_tree.c
index e49c912f862d0..1b7a2f0417936 100644
--- a/kernel/audit_tree.c
+++ b/kernel/audit_tree.c
@@ -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;
--
2.26.2
--
Linux-audit mailing list
Linux-audit@redhat.com
https://www.redhat.com/mailman/listinfo/linux-audit
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] audit: Use struct_size() helper in alloc_chunk
2020-05-24 20:52 [PATCH] audit: Use struct_size() helper in alloc_chunk Gustavo A. R. Silva
@ 2020-06-01 15:36 ` Paul Moore
2020-06-01 15:51 ` Gustavo A. R. Silva
2020-06-17 20:45 ` Paul Moore
0 siblings, 2 replies; 4+ messages in thread
From: Paul Moore @ 2020-06-01 15:36 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: linux-audit, linux-kernel, Gustavo A. R. Silva
On Sun, May 24, 2020 at 4:47 PM Gustavo A. R. Silva
<gustavoars@kernel.org> wrote:
> 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.
>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
> 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.
--
paul moore
www.paul-moore.com
--
Linux-audit mailing list
Linux-audit@redhat.com
https://www.redhat.com/mailman/listinfo/linux-audit
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] audit: Use struct_size() helper in alloc_chunk
2020-06-01 15:36 ` Paul Moore
@ 2020-06-01 15:51 ` Gustavo A. R. Silva
2020-06-17 20:45 ` Paul Moore
1 sibling, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-01 15:51 UTC (permalink / raw)
To: Paul Moore; +Cc: linux-audit, linux-kernel, Gustavo A. R. Silva
On Mon, Jun 01, 2020 at 11:36:09AM -0400, Paul Moore wrote:
> >
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> > 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.
> critical). Once the merge window closes I'll merge this into
> audit/next.
>
Thanks, Paul.
--
Gustavo
--
Linux-audit mailing list
Linux-audit@redhat.com
https://www.redhat.com/mailman/listinfo/linux-audit
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] audit: Use struct_size() helper in alloc_chunk
2020-06-01 15:36 ` Paul Moore
2020-06-01 15:51 ` Gustavo A. R. Silva
@ 2020-06-17 20:45 ` Paul Moore
1 sibling, 0 replies; 4+ messages in thread
From: Paul Moore @ 2020-06-17 20:45 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: linux-audit, linux-kernel, Gustavo A. R. Silva
On Mon, Jun 1, 2020 at 11:36 AM Paul Moore <paul@paul-moore.com> wrote:
> On Sun, May 24, 2020 at 4:47 PM Gustavo A. R. Silva
> <gustavoars@kernel.org> wrote:
> > 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.
> >
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> > 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.
FYI, I just merged this into audit/next. Thanks!
--
paul moore
www.paul-moore.com
--
Linux-audit mailing list
Linux-audit@redhat.com
https://www.redhat.com/mailman/listinfo/linux-audit
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-06-17 20:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-24 20:52 [PATCH] audit: Use struct_size() helper in alloc_chunk Gustavo A. R. Silva
2020-06-01 15:36 ` Paul Moore
2020-06-01 15:51 ` Gustavo A. R. Silva
2020-06-17 20:45 ` Paul Moore
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).