Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1507474lqa; Mon, 29 Apr 2024 10:18:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU+clK/IJuR9YXLnnQUWkq/t1RxJcIh0Lwzq6LDlugNTUf+cjNMCCa+vWRKTq9QH/1B1fvADgfNsqjWcGrg6GGdHtU0c8uTgugdCplOSA== X-Google-Smtp-Source: AGHT+IE1KLM+uWd5w8DczrNjClHgujlOVNxWTcgvYkAHBTE948z1kr7Jwy9pMpiJKVhxY0YR7eGB X-Received: by 2002:a25:c74e:0:b0:dcb:ad22:1b1 with SMTP id w75-20020a25c74e000000b00dcbad2201b1mr12181873ybe.3.1714411092437; Mon, 29 Apr 2024 10:18:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714411092; cv=pass; d=google.com; s=arc-20160816; b=SqxQtcdI21FWLpb10F9EEOS9UOdMnflYterHOb5axm/wq0JfD/eTZVDGC1Qd+tdF3g AMbH1fkTNuuE9pphEoIPOwDE5TpsMK6SoctasSZCcI67YDOt5T5k6umhiYkVMyzl4aJZ YygXUmT7hm6kHpzQ2ojpBQwgcXFX9H03E0AzU+GR4JDS8SRzMCpLFwuqvRasQa1px9C9 Td/dX969rboFwJK2/BapKwe2zPwshGSLImzyHRGSmTr/40z5t4DcoXWxpVPAHFDQrTIv OFq0NZQROXYZShgys7261eBKNDUkNjbBvCD9989Jy70r3Zsy0x26nVVS0LBQPJ7F7RKf cKSA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=0Hd/wdvdADW+smZoeTYyFDi1rtzHcyz1g8xNiBj96c8=; fh=8VygZVybCUvTNA9dKBo6iR/CnTzAGHFnPxz3sT/V7zg=; b=BBmU5QDDBGRv5E1eIEkoE/Fiqcd5ClBdi2R40LEe2p/610MMp4QSZtGjMkr9VNHq6A FrM63xrgZBwiYi+IURaxcsP+IYIeNTxhICPh/k/uP17g6KSmOKsAU2MSCINrqXaFXYJk uM0VK9U7csznoRrriY6R/1o6tu8/punRC7nr98D80DxKz/MTj/zfexU8TwAPwylfK9Iv M/SBckL0CNkBzvkHMj/e5uDywvNB9+cP/V3ESIHjSIuwnZuyxO3bNW6PdQNKIHct/bZQ Au93/74fiXz7R/deQV1k+tirWEAgsvVoWq+xTpsnQDGIvVCftIulDug01vOKE9iifSMu sAuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OsZGsOFi; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-162740-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162740-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id fw13-20020a05622a4a8d00b00439b5de3156si19873534qtb.99.2024.04.29.10.18.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 10:18:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162740-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OsZGsOFi; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-162740-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162740-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0CB881C21D97 for ; Mon, 29 Apr 2024 17:18:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 303F886641; Mon, 29 Apr 2024 17:18:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="OsZGsOFi" Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E28158627A for ; Mon, 29 Apr 2024 17:18:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714411087; cv=none; b=f5w6KwTBQgB9QNdSroRqHAT7NkJarADxiEeTOjf4/U3ehWSS3T5aVd9blHL/l/rgeJS8uKTFm+Jcea7DxJTnv9KxUWhQ+QmndRvo5/UJXmCWnIodXZF7SdarmWoWRj2o78GdogAUvBhWLGfmyqZPAWZ9tewAXDXnF1Wt/Ig38kk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714411087; c=relaxed/simple; bh=qWnvkovjN0ozqbXF/oi4B7/RPRSQlKRASO5y4VDl8Xk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RqyCom8wdNehdPyaJYFU1//zt+aBie4QGhNIzP3wriyR3+qkONlXo8q07FHb3lM2r595JR4iPXtC+XkvEyVJmMbjI3G0pzGERpEFmBSuxMWsl55nuhMQtuOKHISCGC5d3VKs+GKuN6o/1Keodn/MO5DVMplNI3fj6v8vK9ywCgQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=OsZGsOFi; arc=none smtp.client-ip=209.85.210.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6f26588dd5eso4023797b3a.0 for ; Mon, 29 Apr 2024 10:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714411085; x=1715015885; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=0Hd/wdvdADW+smZoeTYyFDi1rtzHcyz1g8xNiBj96c8=; b=OsZGsOFiPCGntUzBHC54nROZSMu/FO2UlLJ2JnFI4YZKjLidJKCP9FlZIWN8fq+0qM QvTKCvHEhK/8xl/ofsigdGypjgWxIjIVWyqzc4CfpuP19bZJUs37WUmXlqPq+Dh8G+0g hSwqr7O/Oi7GhHmXLVPU14q/wDX9lOVLQkWio= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714411085; x=1715015885; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0Hd/wdvdADW+smZoeTYyFDi1rtzHcyz1g8xNiBj96c8=; b=DSyU2uHYvBdWQpSbx7uoMFnHyWuGhcEeCLzWqJsx4D7iHusoeQm5DEV8bzdt6/5nvF 4MYAQ71NUbtiEsDILzqXzGbqgsVU9WXiN+CPHUsN7hxyGGOmI5skC/9OxhbDGju3FVha cBTUDVnf9sk77kYawe1Eci3nqtVufDyjJI5Ql0VoPqEQ5CA4Me2xm8dC7IE3h8uYZVaS 4C0m+TbrdB2jSD+ENsRaj4+cDpMF+ZVhckYlkGx/1ehOaLvHupW97VTwK2lW8YIBSIps TBzeTLp2vVwNAxbHMwl005bk23MnEsM9c389OzsKNWAnNRcGLEXBFhv+HLpnIT4njj+t BEoA== X-Forwarded-Encrypted: i=1; AJvYcCVQKL79nDFfPi8JQ6oEJNx79tNuTr4UdKXTOoEJHywNfuy53kHk4VDJ42R4s+8UNiUWiAbhTQsohr5Uw1hvHElXHVjyqwcAJG6yD7J9 X-Gm-Message-State: AOJu0YwQkC+5ew+N38AMERFLuE2sQEB6Dgd3/mIZZOTlBKoNZirgBL9X hxvWjvaaK/BgVZp0NZlNjLwcn6n/UfH+PdsTi3eLQ0OEuPuV0tH79Rd/u0sNvQ== X-Received: by 2002:a05:6a20:9187:b0:1a7:52fa:7d6b with SMTP id v7-20020a056a20918700b001a752fa7d6bmr13417202pzd.43.1714411085239; Mon, 29 Apr 2024 10:18:05 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id a8-20020aa78648000000b006eade394613sm19515196pfo.198.2024.04.29.10.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 10:18:04 -0700 (PDT) Date: Mon, 29 Apr 2024 10:18:03 -0700 From: Kees Cook To: Peter Zijlstra , Ingo Molnar Cc: Erick Archer , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , "Gustavo A. R. Silva" , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] perf/x86/intel/uncore: Prefer struct_size over open coded arithmetic Message-ID: <202404291013.B21EADD4F@keescook> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Sat, Mar 30, 2024 at 03:32:59PM +0100, Erick Archer wrote: > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. > > As the "box" variable is a pointer to "struct intel_uncore_box" and > this structure ends in a flexible array: > > struct intel_uncore_box { > [...] > struct intel_uncore_extra_reg shared_regs[]; > }; > > the preferred way in the kernel is to use the struct_size() helper to > do the arithmetic instead of the calculation "size + count * size" in > the kzalloc_node() function. > > This way, the code is more readable and safer. > > This code was detected with the help of Coccinelle, and audited and > modified manually. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] > Link: https://github.com/KSPP/linux/issues/160 [2] > Signed-off-by: Erick Archer > --- > Hi, > > The Coccinelle script used to detect this code pattern is the following: > > virtual report > > @rule1@ > type t1; > type t2; > identifier i0; > identifier i1; > identifier i2; > identifier ALLOC =~ "kmalloc|kzalloc|kmalloc_node|kzalloc_node|vmalloc|vzalloc|kvmalloc|kvzalloc"; > position p1; > @@ > > i0 = sizeof(t1) + sizeof(t2) * i1; > ... > i2 = ALLOC@p1(..., i0, ...); > > @script:python depends on report@ > p1 << rule1.p1; > @@ > > msg = "WARNING: verify allocation on line %s" % (p1[0].line) > coccilib.report.print_report(p1[0],msg) > > Regards, > Erick > --- > arch/x86/events/intel/uncore.c | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c > index 258e2cdf28fa..ce756d24c370 100644 > --- a/arch/x86/events/intel/uncore.c > +++ b/arch/x86/events/intel/uncore.c > @@ -350,12 +350,11 @@ static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box) > static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type, > int node) > { > - int i, size, numshared = type->num_shared_regs ; > + int i, numshared = type->num_shared_regs; > struct intel_uncore_box *box; > > - size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg); > - > - box = kzalloc_node(size, GFP_KERNEL, node); > + box = kzalloc_node(struct_size(box, shared_regs, numshared), GFP_KERNEL, > + node); > if (!box) > return NULL; Thanks, yes, this looks correct to me. Reviewed-by: Kees Cook Peter and Ingo, you seem to traditionally take these changes (via -tip)? Can you please pick this up? -- Kees Cook