Received: by 2002:ab2:715a:0:b0:1fd:c064:50c with SMTP id l26csp9477lqm; Mon, 10 Jun 2024 10:59:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUu3LiHmhBNfoWqlafI4Jfm8s9l5RV56qsJ8LzGitHC0SfpDi/hinfnj33zjGTY3lRhZXn309NMzZNt3kpvxMDMe3zTN9ZtA3jKxWwZrA== X-Google-Smtp-Source: AGHT+IEWOFCIXMO8uYAyq+iA6+QlYto/78GfznlqcFdkNNlkgs4KMR+J6fFpIPw25YBrOONhkvg5 X-Received: by 2002:a17:90a:ea92:b0:2c2:dfbe:7318 with SMTP id 98e67ed59e1d1-2c2dfbe7742mr5948406a91.26.1718042380059; Mon, 10 Jun 2024 10:59:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718042380; cv=pass; d=google.com; s=arc-20160816; b=DbLuOylIeDXMfD9ahQJ7JdOm/4enkvydh0nF7tundD2nN//CZ7Mh1ifruK3mdlD+HG BIdPabdc7YF/KZoRPInkSY9uvi2ZCaz3BP3gVMhDtPJcm89oF3rPrckKKPr582RaGcNU EVaTRATz/BHfWQxfy+p8GeIf8vWpbqDRwRbfXS5y23omMEQaEgmzB0G6j77CypCN+262 1TfMK8d4ltXzFqOCYlF3m9FM0OeldiY6iAY/NdyhfgF2InK9Tzm2Yb+r9nxO4FT/twIu nfGeXn/0/Qp4Fq3Ij8RpKZwsDXXxSV8Lxx4Oraby/cpnpOvG7hhMmm8hKu4Rmw3gg7vv NUQA== 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=EivJPT9dRurUBy3LzshwcSFcz2TsLs90RXYFd2ebWKM=; fh=dqFlBHNLwFhe0mK/gMe2/e0bJ/F+7tQQKGpHurKpkA4=; b=y5w9nHts4OTd/ApV5CSHi5jowxQwyfbO6Vw4nHsjD1e1LP9XA/TDrX47LSuiZif2Nn 5tGbqZhujrGaEFaQJgAZsc6V0AMBLuy6biUhRYjba34HfEORc58kA2tJWh2ulnV9aXqG p8stbfCYuwLv2muF1SBhvvuuVSG1xWtvAkV6OBz6+LfcSlEa9NWmnYj4BRpKt90nPVCT 4nj/LmNNGICQJJGvNQr0AvmrW5atMKcHGyXp55mZFZEY1lh4kwi90TljvQ7EYGLVQVk+ rjR/mug5jZt4LBRBE4qmbx91wSSST98HB6FqqtmkHOZiMXOGkFC9PseJmgfqnYCn8BvW FeCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aNpdFrgy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-208652-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208652-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c2d113c2ddsi5373383a91.163.2024.06.10.10.59.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 10:59:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208652-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aNpdFrgy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-208652-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208652-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 4BADBB23B53 for ; Mon, 10 Jun 2024 17:29:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B561F1482EE; Mon, 10 Jun 2024 17:28:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="aNpdFrgy" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7271145FEF; Mon, 10 Jun 2024 17:28:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718040533; cv=none; b=aohNyqK1Xy37WfTp00oCeRsml1JLSme8FiGO5XBDBjfOYcAsUQZStPJIrPDhL2T02zJtN5d+ED8EXyxGPPq2QDk2d6QjUpO9a4Zt09ehNEK0EC7hRoyadSFANzixoBtY/A1pi/cc/blfiZwMjyHvPtOI5V9ql1idTJazEBwWBik= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718040533; c=relaxed/simple; bh=hsXdp2QwbthzBhOSFd6YbbgLNTqyXSDvwcDh62NNNiI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uNpc9Vhw66yefjY3JIvi0ZZ5Q3xqfmTSvDagfbYrAFQVXC/utjgbhH54e0Jckhg1qa0Ev7uqIzK9lSBxmwMZk/1pAsosZJl6w4tMC52WivIwr8LxU/2hY6RBMnTmBJQCrP5TTPNFDxpfaIOG4y8owxz46XQWeiCz/TmADJSZPKc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aNpdFrgy; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52D87C2BBFC; Mon, 10 Jun 2024 17:28:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718040533; bh=hsXdp2QwbthzBhOSFd6YbbgLNTqyXSDvwcDh62NNNiI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=aNpdFrgy5Zza1DDVY23kZJNbw/0JA56+1KNloDqPZnNNfPbTnlj+hn4pOK4Czyakl /mOUHYorQi9awPhEnKK70KmfiZViAMbYcH7p/uuDpVqq+RsHyylTJp1EIZDPGb4n7M L2UZeq/Vv0BDdJwq9PsThFKWQX7c9U0hzvbtX8AoIMUEMKXqHuyfx82Ltmhb2ql5Qw rwP7azFkzipdwQanc/NhpRb7JaqUFsQhRjsVZiYPXxzQoRmJZFC732CmFuKeuubuI5 zdBKP4p1y+1aWACQ75TI5Ta2ITCt4yWds0rtsqyvm+HYlZedAG8K5ODb1zqGI6Z5hy MY6Yi5AEWDmng== Date: Mon, 10 Jun 2024 10:28:52 -0700 From: Kees Cook To: Erick Archer Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Christophe JAILLET , Matthew Wilcox , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH v4 0/3] Hardening perf subsystem Message-ID: <202406101010.E1C77AE9D@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, Jun 01, 2024 at 06:56:15PM +0200, Erick Archer wrote: > Hi everyone, > > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. I didn't actually see these 3 patches in this thread nor via lore. > In the first patch, the "struct amd_uncore_ctx" can be refactored to > use a flex array for the "events" member. This way, the allocation/ > freeing of the memory can be simplified. Then, the struct_size() > helper can be used to do the arithmetic calculation for the memory > to be allocated. I like this patch because it reduces the allocation from 2 to 1. This isn't what Peter might see as "churn": this is an improvement in resource utilization. I think it's here: https://lore.kernel.org/linux-hardening/AS8PR02MB7237E4848B44A5226BD3551C8BE02@AS8PR02MB7237.eurprd02.prod.outlook.com/ > In the second patch, as the "struct intel_uncore_box" ends in a > flexible array, 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 is https://lore.kernel.org/linux-hardening/AS8PR02MB7237F4D39BF6AA0FF40E91638B392@AS8PR02MB7237.eurprd02.prod.outlook.com/ I prefer this style, as it makes things unambiguous ("this will never wrap around") without having to check the associated types and doesn't make the resulting binary code different in the "can never overflow" case. In this particular case: int size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg); "int numshared" comes from struct intel_uncore_type::num_shared_regs, which is: unsigned num_shared_regs:8; And the struct sizes are: $ pahole -C intel_uncore_box gcc-boot/vmlinux | grep size: /* size: 488, cachelines: 8, members: 19 */ $ pahole -C intel_uncore_extra_reg gcc-boot/vmlinux | grep size: /* size: 96, cachelines: 2, members: 5 */ So we have: s32 size = 488 + u8 * 96 Max size here is 24968 so it can never overflow an s32, so I can see why Peter views this as "churn". I still think the patch is a coding style improvement, but okay. > In the third patch, as the "struct perf_buffer" also ends in a > flexible array, 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() functions. At the same > time, prepare for the coming implementation by GCC and Clang of the > __counted_by attribute. This is https://lore.kernel.org/linux-hardening/AS8PR02MB72379B4807F3951A1B926BA58BE02@AS8PR02MB7237.eurprd02.prod.outlook.com/ This provides __counted_by coverage, and I think this is important to gain in ever place we can. Given that this is part of a ring buffer implementation that is arbitrarily sized, this is exactly the kind of place I'd like to see __counted_by used. This is a runtime robustness improvement, so I don't see this a "churn" at all. Peter, for patches 1 and 3, if you'd prefer not to carry them, I could put them in the hardening tree to keep them out of your way. It seems clear you don't want patch 2 at all. -Kees -- Kees Cook