* [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
@ 2022-05-18 13:16 Lin Yujun
2022-05-18 13:49 ` Gustavo A. R. Silva
0 siblings, 1 reply; 6+ messages in thread
From: Lin Yujun @ 2022-05-18 13:16 UTC (permalink / raw)
To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
namhyung, tglx, bp, dave.hansen, x86, hpa
Cc: linux-perf-users, linux-kernel, gustavoars, johnny.chenyi,
chenjiahao16, chenlifu, lizhengyu3, liaochang1, linyujun809,
wangzhu9, xuyihang, chris.zjh, zouyipeng
Replace sizeof() with struct_size() to avoid potential integer
or heap overflow.
Also, address the following sparse warnings:
arch/x86/events/rapl.c:685:16: warning: using sizeof on a
flexible structure
Signed-off-by: Lin Yujun <linyujun809@huawei.com>
---
arch/x86/events/rapl.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
index 77e3a47af5ad..c7e79f0ac04f 100644
--- a/arch/x86/events/rapl.c
+++ b/arch/x86/events/rapl.c
@@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
int maxdie = topology_max_packages() * topology_max_die_per_package();
size_t size;
- size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
+ size = struct_size(rapl_pmus, pmus, maxdie);
rapl_pmus = kzalloc(size, GFP_KERNEL);
if (!rapl_pmus)
return -ENOMEM;
--
2.17.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
2022-05-18 13:16 [PATCH -next] x86/events:Use struct_size() helper in kzalloc() Lin Yujun
@ 2022-05-18 13:49 ` Gustavo A. R. Silva
2022-05-18 13:55 ` Peter Zijlstra
2022-05-19 2:30 ` linyujun (C)
0 siblings, 2 replies; 6+ messages in thread
From: Gustavo A. R. Silva @ 2022-05-18 13:49 UTC (permalink / raw)
To: Lin Yujun
Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
namhyung, tglx, bp, dave.hansen, x86, hpa, linux-perf-users,
linux-kernel, johnny.chenyi, chenjiahao16, chenlifu, lizhengyu3,
liaochang1, wangzhu9, xuyihang, chris.zjh, zouyipeng
On Wed, May 18, 2022 at 09:16:26PM +0800, Lin Yujun wrote:
> Replace sizeof() with struct_size() to avoid potential integer
> or heap overflow.
This is not exactly what the patch does.
Your patch is replacing the open-coded calculation of the total amount
of memory to be allocated for rapl_pmus with struct_size().
>
> Also, address the following sparse warnings:
> arch/x86/events/rapl.c:685:16: warning: using sizeof on a
> flexible structure
This does not address this warning because struct_size()
still make use of sizeof on the struct-with-flex-array,
internally.
So, the mention to the sparse warning should be removed from
this changelog text.
>
> Signed-off-by: Lin Yujun <linyujun809@huawei.com>
> ---
> arch/x86/events/rapl.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index 77e3a47af5ad..c7e79f0ac04f 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
> int maxdie = topology_max_packages() * topology_max_die_per_package();
> size_t size;
>
> - size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
> + size = struct_size(rapl_pmus, pmus, maxdie);
> rapl_pmus = kzalloc(size, GFP_KERNEL);
It seems that in this case, size could be entirely replaced by
struct_size().
Thanks
--
Gustavo
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
2022-05-18 13:49 ` Gustavo A. R. Silva
@ 2022-05-18 13:55 ` Peter Zijlstra
2022-05-18 16:03 ` Gustavo A. R. Silva
2022-05-19 2:30 ` linyujun (C)
1 sibling, 1 reply; 6+ messages in thread
From: Peter Zijlstra @ 2022-05-18 13:55 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: Lin Yujun, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
namhyung, tglx, bp, dave.hansen, x86, hpa, linux-perf-users,
linux-kernel, johnny.chenyi, chenjiahao16, chenlifu, lizhengyu3,
liaochang1, wangzhu9, xuyihang, chris.zjh, zouyipeng
On Wed, May 18, 2022 at 08:49:20AM -0500, Gustavo A. R. Silva wrote:
> > Signed-off-by: Lin Yujun <linyujun809@huawei.com>
> > ---
> > arch/x86/events/rapl.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> > index 77e3a47af5ad..c7e79f0ac04f 100644
> > --- a/arch/x86/events/rapl.c
> > +++ b/arch/x86/events/rapl.c
> > @@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
> > int maxdie = topology_max_packages() * topology_max_die_per_package();
> > size_t size;
> >
> > - size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
> > + size = struct_size(rapl_pmus, pmus, maxdie);
> > rapl_pmus = kzalloc(size, GFP_KERNEL);
>
> It seems that in this case, size could be entirely replaced by
> struct_size().
Except every time I look at struct_size() I go: WTF does that do; while
the code as-is is crystal clear, no de-obfucstaion required.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
2022-05-18 13:55 ` Peter Zijlstra
@ 2022-05-18 16:03 ` Gustavo A. R. Silva
2022-05-18 16:54 ` Borislav Petkov
0 siblings, 1 reply; 6+ messages in thread
From: Gustavo A. R. Silva @ 2022-05-18 16:03 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Lin Yujun, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
namhyung, tglx, bp, dave.hansen, x86, hpa, linux-perf-users,
linux-kernel, johnny.chenyi, chenjiahao16, chenlifu, lizhengyu3,
liaochang1, wangzhu9, xuyihang, chris.zjh, zouyipeng
On Wed, May 18, 2022 at 03:55:00PM +0200, Peter Zijlstra wrote:
> On Wed, May 18, 2022 at 08:49:20AM -0500, Gustavo A. R. Silva wrote:
>
> > > Signed-off-by: Lin Yujun <linyujun809@huawei.com>
> > > ---
> > > arch/x86/events/rapl.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> > > index 77e3a47af5ad..c7e79f0ac04f 100644
> > > --- a/arch/x86/events/rapl.c
> > > +++ b/arch/x86/events/rapl.c
> > > @@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
> > > int maxdie = topology_max_packages() * topology_max_die_per_package();
> > > size_t size;
> > >
> > > - size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
> > > + size = struct_size(rapl_pmus, pmus, maxdie);
> > > rapl_pmus = kzalloc(size, GFP_KERNEL);
> >
> > It seems that in this case, size could be entirely replaced by
> > struct_size().
>
> Except every time I look at struct_size() I go: WTF does that do; while
> the code as-is is crystal clear, no de-obfucstaion required.
Naming it flex_struct_size() would be more informative?
--
Gustavo
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
2022-05-18 16:03 ` Gustavo A. R. Silva
@ 2022-05-18 16:54 ` Borislav Petkov
0 siblings, 0 replies; 6+ messages in thread
From: Borislav Petkov @ 2022-05-18 16:54 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: Peter Zijlstra, Lin Yujun, mingo, acme, mark.rutland,
alexander.shishkin, jolsa, namhyung, tglx, dave.hansen, x86, hpa,
linux-perf-users, linux-kernel, johnny.chenyi, chenjiahao16,
chenlifu, lizhengyu3, liaochang1, wangzhu9, xuyihang, chris.zjh,
zouyipeng
On Wed, May 18, 2022 at 11:03:45AM -0500, Gustavo A. R. Silva wrote:
> Naming it flex_struct_size() would be more informative?
Not use it at all if it makes the code less readable.
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
2022-05-18 13:49 ` Gustavo A. R. Silva
2022-05-18 13:55 ` Peter Zijlstra
@ 2022-05-19 2:30 ` linyujun (C)
1 sibling, 0 replies; 6+ messages in thread
From: linyujun (C) @ 2022-05-19 2:30 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
namhyung, tglx, bp, dave.hansen, x86, hpa, linux-perf-users,
linux-kernel, johnny.chenyi, chenjiahao16, chenlifu, lizhengyu3,
liaochang1, wangzhu9, xuyihang, chris.zjh, zouyipeng
Thank you for your suggestion. I will modify the patch according
to your suggestion and make the v2 version.
Thanks
--
LinYujun
在 2022/5/18 21:49, Gustavo A. R. Silva 写道:
> On Wed, May 18, 2022 at 09:16:26PM +0800, Lin Yujun wrote:
>> Replace sizeof() with struct_size() to avoid potential integer
>> or heap overflow.
> This is not exactly what the patch does.
>
> Your patch is replacing the open-coded calculation of the total amount
> of memory to be allocated for rapl_pmus with struct_size().
>
>> Also, address the following sparse warnings:
>> arch/x86/events/rapl.c:685:16: warning: using sizeof on a
>> flexible structure
> This does not address this warning because struct_size()
> still make use of sizeof on the struct-with-flex-array,
> internally.
>
> So, the mention to the sparse warning should be removed from
> this changelog text.
>
>> Signed-off-by: Lin Yujun <linyujun809@huawei.com>
>> ---
>> arch/x86/events/rapl.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
>> index 77e3a47af5ad..c7e79f0ac04f 100644
>> --- a/arch/x86/events/rapl.c
>> +++ b/arch/x86/events/rapl.c
>> @@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
>> int maxdie = topology_max_packages() * topology_max_die_per_package();
>> size_t size;
>>
>> - size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
>> + size = struct_size(rapl_pmus, pmus, maxdie);
>> rapl_pmus = kzalloc(size, GFP_KERNEL);
> It seems that in this case, size could be entirely replaced by
> struct_size().
>
> Thanks
> --
> Gustavo
> .
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-05-19 2:31 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 13:16 [PATCH -next] x86/events:Use struct_size() helper in kzalloc() Lin Yujun
2022-05-18 13:49 ` Gustavo A. R. Silva
2022-05-18 13:55 ` Peter Zijlstra
2022-05-18 16:03 ` Gustavo A. R. Silva
2022-05-18 16:54 ` Borislav Petkov
2022-05-19 2:30 ` linyujun (C)
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).