From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D015CF8853 for ; Fri, 4 Oct 2024 17:23:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7DA498E0005; Fri, 4 Oct 2024 13:23:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 761DC8D0003; Fri, 4 Oct 2024 13:23:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E6B58E0005; Fri, 4 Oct 2024 13:23:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3BFBC8D0003 for ; Fri, 4 Oct 2024 13:23:53 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AD51B161585 for ; Fri, 4 Oct 2024 17:23:52 +0000 (UTC) X-FDA: 82636592304.24.922A410 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf05.hostedemail.com (Postfix) with ESMTP id 1213510000F for ; Fri, 4 Oct 2024 17:23:50 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qRnK+Hc8; spf=pass (imf05.hostedemail.com: domain of kees@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728062590; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RMS/Ffeg2GdvCUNCTTQ6KTGOTLO4ZgjbauNEmudOTJM=; b=FV1OkA+4QVfAUJi0zhKVMXnPiGh6pXJ+ny+35TPJNFzJl7xz9YaI7JTQxcM1ceOi4p9zw9 rGovDivSx6mmc+ySrvuWPcRMEisVtlh1KqTG4N6OFJwBNONoUyEKzaFlcSyEoSMT13d6pf H7epqhmiJdiVeHMLj6ZTGRle0Q3W5RM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qRnK+Hc8; spf=pass (imf05.hostedemail.com: domain of kees@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728062590; a=rsa-sha256; cv=none; b=tHkYBQ7RO06wIPg5AnVOKjnOMAa6vdtS3xtCt4Hiji87Wf52TyeUPdQIwpXo006u6Zhw65 3Tas9JeuzgzTXYg+8CxKZxEVBKK1g2wvnT57GULyNuAqsLDY8jGSin6j9xnyojFEmJa6z6 YdYMJw2S/AVGcGkuFzU0QykXmb7IQIY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 7DBE1A44C16; Fri, 4 Oct 2024 17:23:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 459D2C4CEC6; Fri, 4 Oct 2024 17:23:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728062629; bh=Ck04ZwAJLUC52xG8xW/cvhMb6CgIDBwDNggWk8T5Z6M=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qRnK+Hc8fbZvqSga/zsdsMP74yY2Pz6jVlLqCCHwKxGGmm8KNdEr4zdpu2IAhCLPb lRaZbchE7zyY27Ol9Hnw1uWRbliPl8I/+/zilZPSKXUzKJC12bSV7HSlWpl5DiSMLB HEtpGPskvn2MfPZvV+vmD81CeSXVEr7PaNhTPCm2Rkd2jjSIGGFWJfsUKASwbMQNHs uP2JSq3E6dNWwubN3+b+dfKgeVeIX661LoHrOpwP6Zyl5LE3pLKdri5Mk8VL0E2nli COSe9o3f5MkB154n+3I+MJydU/ZNq9U612HB6Fs44DljKJXML+LxDLyynurnyC1mM5 lmi9vJ1Zilt0g== Date: Fri, 4 Oct 2024 10:23:46 -0700 From: Kees Cook To: Przemek Kitszel Cc: Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, "Gustavo A . R . Silva" , Bill Wendling , Justin Stitt , Jann Horn , Marco Elver , linux-mm@kvack.org, Nathan Chancellor , Nick Desaulniers , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: Re: [PATCH v3] slab: Introduce kmalloc_obj() and family Message-ID: <202410041014.7DE8981@keescook> References: <20240822231324.make.666-kees@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Stat-Signature: ujf864b6wograsn13qk3czrj74nsar9w X-Rspamd-Queue-Id: 1213510000F X-Rspamd-Server: rspam11 X-HE-Tag: 1728062630-35408 X-HE-Meta: U2FsdGVkX19UVEdnCnyX6V38ODqaEw7b2n9WDW/YPVbmufi3aGJBGHMBHY1iC4FImcivJd0Rqu1Chl/ZnjO6I0Fgx8bsVBOZ1KIDkxsFTZGuHogCyp1rKLfV6M+0gAwLP+Q/jYKImcPzQS6yPEacjTV5X+owzyLM0rFLCn+escc0pJqSFXrzGINE5Z1eYanPiFmYHqnFpG7uczRvWLynCFR74gegyRZtNL2RGCwRbqGhGdhrOZqCVW8GotiTX25/5ImT92CdVMSYOhDCxC3sGNs8JBivVa/XvwJBYpP2g9uBQQL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ter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, Aug 23, 2024 at 06:27:58AM +0200, Przemek Kitszel wrote: > On 8/23/24 01:13, Kees Cook wrote: > > > (...) For cases where the total size of the allocation is needed, > > the kmalloc_obj_sz(), kmalloc_objs_sz(), and kmalloc_flex_sz() family > > of macros can be used. For example: > > > > info->size = struct_size(ptr, flex_member, count); > > ptr = kmalloc(info->size, gfp); > > > > becomes: > > > > kmalloc_flex_sz(ptr, flex_member, count, gfp, &info->size); > > > > Internal introspection of allocated type now becomes possible, allowing > > for future alignment-aware choices and hardening work. For example, > > adding __alignof(*ptr) as an argument to the internal allocators so that > > appropriate/efficient alignment choices can be made, or being able to > > correctly choose per-allocation offset randomization within a bucket > > that does not break alignment requirements. > > > > Introduces __flex_count() for when __builtin_get_counted_by() is added > > by GCC[1] and Clang[2]. The internal use of __flex_count() allows for > > automatically setting the counter member of a struct's flexible array > > member when it has been annotated with __counted_by(), avoiding any > > missed early size initializations while __counted_by() annotations are > > added to the kernel. Additionally, this also checks for "too large" > > allocations based on the type size of the counter variable. For example: > > > > if (count > type_max(ptr->flex_count)) > > fail...; > > info->size = struct_size(ptr, flex_member, count); > > ptr = kmalloc(info->size, gfp); > > ptr->flex_count = count; > > > > becomes (i.e. unchanged from earlier example): > > > > kmalloc_flex_sz(ptr, flex_member, count, gfp, &info->size); > > As there could be no __builtin_get_counted_by() available, caller still > needs to fill the counted-by variable, right? So it is possible to just > pass the in the struct pointer to fill? (last argument "&f->cnt" of the > snippet below): > > struct foo { > int cnt; > struct bar[] __counted_by(cnt); > }; > > //... > struct foo *f; > > kmalloc_flex_sz(f, cnt, 42, gfp, &f->cnt); I specifically want to avoid this because it makes adding the counted_by attribute more difficult -- requiring manual auditing of all allocation sites, even if we switch all the alloc macros. But if allocation macros are all replaced with a treewide change, it becomes trivial to add counted_by annotations without missing "too late" counter assignments. (And note that the "too late" counter assignments are only a problem for code built with compilers that support counted_by, so there's no problem that __builtin_get_counted_by() isn't available.) Right now we have two cases in kernel code: case 1: - allocate - assign counter - access array case 2: - allocate - access array - assign counter When we add a counted_by annotation, all "case 2" code but be found and refactored into "case 1". This has proven error-prone already, and we're still pretty early in adding annotations. The reason refactoring is needed is because when the compiler supports counted_by instrumentation, at run-time, we get: case 1: - allocate - assign counter - access array // no problem! case 2: - allocate - access array // trap! - assign counter I want to change this to be: case 1: - allocate & assign counter - assign counter - access array case 2: - allocate & assign counter - access array - assign counter Once the kernel reaches a minimum compiler version where counted_by is universally available, we can remove all the "open coded" counter assignments. -Kees -- Kees Cook