public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next v2] x86/events:Use struct_size() helper in kzalloc()
@ 2022-05-19  2:36 Lin Yujun
  2022-05-19  7:52 ` Peter Zijlstra
  0 siblings, 1 reply; 2+ messages in thread
From: Lin Yujun @ 2022-05-19  2:36 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

Make use of the struct_size() helper instead of an open-coded version,
in order to avoid any potential type mistakes or integer overflows that,
in the worst scenario, could lead to heap overflows.

Signed-off-by: Lin Yujun <linyujun809@huawei.com>
---
 arch/x86/events/rapl.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
index 77e3a47af5ad..8da003e02010 100644
--- a/arch/x86/events/rapl.c
+++ b/arch/x86/events/rapl.c
@@ -683,10 +683,8 @@ static const struct attribute_group *rapl_attr_update[] = {
 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 *);
-	rapl_pmus = kzalloc(size, GFP_KERNEL);
+	rapl_pmus = kzalloc(struct_size(rapl_pmus, pmus, maxdie), GFP_KERNEL);
 	if (!rapl_pmus)
 		return -ENOMEM;
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH -next v2] x86/events:Use struct_size() helper in kzalloc()
  2022-05-19  2:36 [PATCH -next v2] x86/events:Use struct_size() helper in kzalloc() Lin Yujun
@ 2022-05-19  7:52 ` Peter Zijlstra
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Zijlstra @ 2022-05-19  7:52 UTC (permalink / raw)
  To: Lin Yujun
  Cc: mingo, acme, mark.rutland, alexander.shishkin, jolsa, namhyung,
	tglx, bp, dave.hansen, x86, hpa, linux-perf-users, linux-kernel,
	gustavoars, johnny.chenyi, chenjiahao16, chenlifu, lizhengyu3,
	liaochang1, wangzhu9, xuyihang, chris.zjh, zouyipeng

On Thu, May 19, 2022 at 10:36:00AM +0800, Lin Yujun wrote:
> Make use of the struct_size() helper instead of an open-coded version,
> in order to avoid any potential type mistakes or integer overflows that,
> in the worst scenario, could lead to heap overflows.
> 
> Signed-off-by: Lin Yujun <linyujun809@huawei.com>
> ---
>  arch/x86/events/rapl.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index 77e3a47af5ad..8da003e02010 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -683,10 +683,8 @@ static const struct attribute_group *rapl_attr_update[] = {
>  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 *);
> -	rapl_pmus = kzalloc(size, GFP_KERNEL);
> +	rapl_pmus = kzalloc(struct_size(rapl_pmus, pmus, maxdie), GFP_KERNEL);

So I really hate that thing; it's pointless obfuscation. If you're
really worried about the type confusion, write it like:

	size = sizeof(*rapl_pmus) + sizeof(rapl_pmus->pmus[0]) * maxdie;

or something. Otherwise, just go away.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-19  7:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-19  2:36 [PATCH -next v2] x86/events:Use struct_size() helper in kzalloc() Lin Yujun
2022-05-19  7:52 ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox