Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp680613lqz; Sat, 30 Mar 2024 14:11:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXDVaGVfN7xYKQ725X2BovhsOeTEQHXWDRIjNUuin23z8swJo460DQhw7x+Cux+u5u9nupgUdAda1qsaIcB7moppMAKURr08mz3NnhCvg== X-Google-Smtp-Source: AGHT+IG+7cKNNFyRh42qqAxq9TtoBBg4AbiK6S1dCSOIxeRFSB7Ww1reTh/T1tdpg/Jrce9li/7n X-Received: by 2002:a17:902:f082:b0:1e0:b5d4:9f61 with SMTP id p2-20020a170902f08200b001e0b5d49f61mr4002571pla.36.1711833097238; Sat, 30 Mar 2024 14:11:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711833097; cv=pass; d=google.com; s=arc-20160816; b=yLqUPv4JQC96wxzc4VUjw2TrSIhuTrFmI94YJsjt/g2UhLcMHOSjYwhYDvFecznvSl 7pgBMu8JMIusRVMrELT725Jkw5Kh2Y8aS0U1lRhmAd05LTI7ZXKKMGQlKYJe9n2xfPip P0TddmH8u98EAmE2IebEkNRco8hB54enS36u887DTbcAZRznWpWbritKnuqsK2Rey96u d0VxukuYeO8KlvR+eHc6OccEsKjwf5lYIGXI2VXjQ25VB4pietFQxsrra53ZcQZffDa/ piAzYg6+AN+BFTfVoPeVKpFXHo4yNsP1imMnofQRJYmE/CNx7FRn73YK/EtOa9Xd2CUj +l/A== 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:sender:dkim-signature; bh=6FfCXTwbIMZ0/LT2fPdhGW3nMXxEswf6TerbPzBdQ6o=; fh=k05s7ZZA0V4SaennMCNh0ChVIWX/VmownZtXN14nSAw=; b=EVC07tK5QCDBMay4iVrIte9Q43ojwW3U2RDDVZYUG0oqJi+zhuzJYp0tyn6bIVvv3q KjYW35OSeDYouC0Iq+FWtpY82vxLKtCy199N4C1vC6gZPwDy8ckOgS8mJOwoJVFPCv74 lZz6dZK0StvgLjW+wA5wlIsfWuAA0/n0RHE+o3kPGwVbdfH9G6TLNelWedt7sdWIEPOZ OSlKspNc7DfTmXQjn9gOkyR6Ceexb7PLhX8c+OzD9J4xNpIGHesvEWI21m1rgNe+15l0 vKR4H7mowBgWspy+U5PNW0tl3BOUZtMde8QsE6WdivGxG6nIQltUMB/9alpIyY37FAVx ltiQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nYp7vNrP; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-125890-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125890-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k10-20020a170902f28a00b001e0f302a06asi5691660plc.461.2024.03.30.14.11.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 14:11:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125890-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nYp7vNrP; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-125890-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125890-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DFEBD283155 for ; Sat, 30 Mar 2024 21:11:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 700474AEDA; Sat, 30 Mar 2024 21:11:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nYp7vNrP" Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (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 A0B3E43144; Sat, 30 Mar 2024 21:11:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711833089; cv=none; b=Fc4WTDiN3fafyRFs4xubbxt2hTvFHTPjZbEo8aMhrIA67SjR200TI+09y7xLxICqzb9vXl2/ZX0WTtFXr6kGuGjj3HINgYSdEfLxPod6TgyHXyf/rlUvg0IDgvhvcPU8nHr14vzG+KXDMW9G6J1y25n4q5wnEZI4wiB8AbtqjDs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711833089; c=relaxed/simple; bh=CHcXrxf2ENpQddGykj9rk3TKmc0vJHsnIQfhoa/d1sw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=bDGMJgbw7uUTNcNvsRUJHkbGwVXrjcm3akhXq1A1xoQNkEifMCRhpiMmmub9EPrq13C8sxRMXBErgJmUM0GU0cBb/YBWPwpbxFyzuiH/h/wTXcvMaBA24NVXRdhh5PpfjDUCGCrsKkdWI7upcjSWjNPgz0Ch36q1dTsXZKzcFgs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nYp7vNrP; arc=none smtp.client-ip=209.85.208.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-56c0bd07e7bso3198910a12.1; Sat, 30 Mar 2024 14:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711833086; x=1712437886; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=6FfCXTwbIMZ0/LT2fPdhGW3nMXxEswf6TerbPzBdQ6o=; b=nYp7vNrP4H6zxpt+VWqWiLxFuhvnUn9PhikSF+Qd+wu/XbGaVHX+zgk5bAK2paJCSY dOdYpNkm1AGS4d4Tpho22pJoWmOvKkkNJ5PXVoegiClRps/KLCCUok8FUJdMXDRT1zbb nGQrMRIa0FETxYrGP9bDwXl83ukC1B+wCFduUhI/1eauVrH3GO/ggMZOoCrafy4o+M/7 REV38wFjl/6dBlMO8VgS6gDdl0071CQ4SclNRbbZtS+7plD2Gc8srVdXLBU16VXRUKpV gk4FQ5689wtStLlKlsCA7TYk6g2zMKkyWMIj0b8qV/wMpGOD21rizhMCm/30pajQuh/r H6oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711833086; x=1712437886; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6FfCXTwbIMZ0/LT2fPdhGW3nMXxEswf6TerbPzBdQ6o=; b=SsP4hsVgfU+L7txdZ6IfDh/h4AGfKCJ67EXJ92vTdC74U0DlwicS2r+Ol5wC7X1oDc r0qq/3HSSuxToUoqwMC0gbVHgW5qYIbfxMsoVQ4WyfRh+77cLCW0HpeoaJJIIwoHG+Cr zkDCNjQIiOCg/+xJ5MMDECCAjfRIcRaXQNZWAKxxceyFelMBtexh+8mI/xQXeiDWBNgH ohG1hruVb++72ckz2XGmtE4OdUr5QpTh1tVEQF5FvHTV1WYcLJqi3kCrO2GQO6/8OBVW 07pZ+xwl2xrVFuuPND7kJfDaUXz+z/HQOXeAUJr39IJ4BRCp7i/OnSLiQsFEIKRk1D5o cNjQ== X-Forwarded-Encrypted: i=1; AJvYcCXnBrSOYtl/MJ1PE+Gp6dEqlXYvZ+pQ4g0fNTuOdWvHWI2P/o1emDoRT5a1PSoNCVHHTLKI7/4BTM85IQVAOeLeCG0R37cuOmu1nl1wwRQqkQ0dVUL63Rzu76YEwID/ekSEa4LICkuw0Y5k6ORSchQRcOs8iXGHXX+GSdMw1+G/iNrnss0w6X9QIdWGwuZ2mg== X-Gm-Message-State: AOJu0YxW7mTqLTZN2dMZLnw7G8df9ZtDhAqUzePPbK18LkJ8d45kSIPG F5Bm8gHx7QRnNlOW7iMzgya9p1Q/vjgerVUVouDPVXsZVQy4lUU3 X-Received: by 2002:a17:907:78d7:b0:a4e:5a23:24d with SMTP id kv23-20020a17090778d700b00a4e5a23024dmr470387ejc.38.1711833085563; Sat, 30 Mar 2024 14:11:25 -0700 (PDT) Received: from gmail.com (84-236-113-97.pool.digikabel.hu. [84.236.113.97]) by smtp.gmail.com with ESMTPSA id xc4-20020a170907074400b00a4e5ab88803sm314414ejb.183.2024.03.30.14.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 14:11:25 -0700 (PDT) Sender: Ingo Molnar Date: Sat, 30 Mar 2024 22:11:23 +0100 From: Ingo Molnar To: Erick Archer Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Borislav Petkov , Dave Hansen , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "H. Peter Anvin" , Kees Cook , "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 v2] perf/x86/amd/uncore: Use kcalloc*() instead of kzalloc*() Message-ID: 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: * Erick Archer wrote: > As noted in the "Deprecated Interfaces, Language Features, Attributes, > and Conventions" documentation [1], size calculations (especially > multiplication) should not be performed in memory allocator (or similar) > function arguments due to the risk of them overflowing. This could lead > to values wrapping around and a smaller allocation being made than the > caller was expecting. Using those allocations could lead to linear > overflows of heap memory and other misbehaviors. > > So, use the purpose specific kcalloc*() function instead of the argument > size * count in the kzalloc*() function. > > [1] https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments > > Link: https://github.com/KSPP/linux/issues/162 > Reviewed-by: Gustavo A. R. Silva > Signed-off-by: Erick Archer > --- > Changes in v2: > - Add the "Reviewed-by:" tag. > - Rebase against linux-next. > > Previous versions: > v1 -> https://lore.kernel.org/linux-hardening/20240116125813.3754-1-erick.archer@gmx.com/ > > Hi everyone, > > This patch seems to be lost. Gustavo reviewed it on January 16, 2024 > but the patch has not been applied since. > > Thanks, > Erick > --- > arch/x86/events/amd/uncore.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c > index 4ccb8fa483e6..61c0a2114183 100644 > --- a/arch/x86/events/amd/uncore.c > +++ b/arch/x86/events/amd/uncore.c > @@ -479,8 +479,8 @@ static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu) > goto fail; > > curr->cpu = cpu; > - curr->events = kzalloc_node(sizeof(*curr->events) * > - pmu->num_counters, > + curr->events = kcalloc_node(pmu->num_counters, > + sizeof(*curr->events), > GFP_KERNEL, node); > if (!curr->events) { > kfree(curr); > @@ -928,7 +928,7 @@ int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu) > uncore->num_pmus += group_num_pmus[gid]; > } > > - uncore->pmus = kzalloc(sizeof(*uncore->pmus) * uncore->num_pmus, > + uncore->pmus = kcalloc(uncore->num_pmus, sizeof(*uncore->pmus), > GFP_KERNEL); > if (!uncore->pmus) { > uncore->num_pmus = 0; This change is nonsense, kzalloc() is a perfectly usable interface, and none of the arguments are user-controlled, so I don't see how there could be a real overflow bug here. Thanks, Ingo