[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20220519075220.GD2578@worktop.programming.kicks-ass.net>
Date: Thu, 19 May 2022 09:52:20 +0200
From: Peter Zijlstra <peterz@...radead.org>
To: Lin Yujun <linyujun809@...wei.com>
Cc: mingo@...hat.com, acme@...nel.org, mark.rutland@....com,
alexander.shishkin@...ux.intel.com, jolsa@...nel.org,
namhyung@...nel.org, tglx@...utronix.de, bp@...en8.de,
dave.hansen@...ux.intel.com, x86@...nel.org, hpa@...or.com,
linux-perf-users@...r.kernel.org, linux-kernel@...r.kernel.org,
gustavoars@...nel.org, johnny.chenyi@...wei.com,
chenjiahao16@...wei.com, chenlifu@...wei.com,
lizhengyu3@...wei.com, liaochang1@...wei.com, wangzhu9@...wei.com,
xuyihang@...wei.com, chris.zjh@...wei.com, zouyipeng@...wei.com
Subject: Re: [PATCH -next v2] x86/events:Use struct_size() helper in kzalloc()
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@...wei.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.
Powered by blists - more mailing lists