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 C5BB5C2BD09 for ; Tue, 9 Jul 2024 18:57:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DA1E6B0096; Tue, 9 Jul 2024 14:57:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 289286B0099; Tue, 9 Jul 2024 14:57:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 150FC6B009A; Tue, 9 Jul 2024 14:57:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EAB776B0096 for ; Tue, 9 Jul 2024 14:57:23 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 636A04166B for ; Tue, 9 Jul 2024 18:57:23 +0000 (UTC) X-FDA: 82321122366.04.67821E3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id AD3161A0011 for ; Tue, 9 Jul 2024 18:57:21 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qjA4DhPT; spf=pass (imf19.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720551411; 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=HNWHilpzNaY1u+qr2Sm9pBePYdsiCObjHwWhCE5l+vY=; b=Io3WLgYncj/up6EFlGKTFXm2IIihA9ReNfTRQCkmvo5qo7IXOX7QFLKN6ifvEF1VqrTjvj tri9pyfd0ls1861iwIqv25luc3R3MfvsYB4X3IIZrAKYx+ayHE0r56YjMyPwBqASKTMr0x jiAkYywDeZM74cJ32YtPRwr1Ke1h4M0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720551411; a=rsa-sha256; cv=none; b=rG6cNmEzwgla+KZxOoAakI/SGh1SWHqsfkaGwuoUglVBlvhyEkoiSiNrzy1e2VEXX2iluC upJtA3/J0b+GsM2P6IQqoc9naajEgkCknuleY4Umahwr+gIL9QNekUr98wgWDVo0FTWN2V ZTD8G9k1od3TX2wkkdYs1IxAvDB3ApY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qjA4DhPT; spf=pass (imf19.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9339E61668; Tue, 9 Jul 2024 18:57:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D266C3277B; Tue, 9 Jul 2024 18:57:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720551440; bh=BwqLwtrExU9DV27J8IQjRT372nw9zEI5Ny0egiaWJK0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qjA4DhPTcZp05JxJcSy1L2Va3w0tst+HrK3SIOmEDlwoPPukKJpcmhbgEmohx7JsX s1GE0DRFDYeR6retitf/mErPEDNWRiFd4yLAPR2bQh7q8/y9CPtakLg5/4GYyZdjQo UDCzjHdU1PDeaQIhO70yosfSXscEwARJFFOpTkw8cqJlhKB6LBYY8uFt2H+wykGGCT qawYbjR3V4BCYphgrCMMBDvORtJAWiQV7Cfylu1ifVMMSR/DEBLEGt8kVdAGOCMexV p2wa7q4hTZ0vUvvQzcrYIc3m+fI56nzLlhWx8xn95VtnohVJ0EllmlzLTeqNrWjJh2 LzPYSJRa4R5HQ== Date: Tue, 9 Jul 2024 11:57:19 -0700 From: Kees Cook To: Roman Gushchin Cc: Vlastimil Babka , Jann Horn , Tony Luck , Nick Desaulniers , Miguel Ojeda , Marco Elver , Nathan Chancellor , Hao Luo , Przemek Kitszel , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Hyeonggon Yoo <42.hyeyoo@gmail.com>, "Guilherme G. Piccoli" , Mark Rutland , Jakub Kicinski , Petr Pavlu , Alexander Lobakin , Tony Ambardar , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: Re: [RFC][PATCH 0/4] slab: Allow for type introspection during allocation Message-ID: <202407091155.1D0D7584@keescook> References: <20240708190924.work.846-kees@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AD3161A0011 X-Stat-Signature: enoxhjp4kxezk6htip5ofdnb8hgonk3p X-HE-Tag: 1720551441-499587 X-HE-Meta: U2FsdGVkX18dmpfHHPxcqqSoYbUt0TaIn7IhktkO/3w8MonNEKhIt9GeGJgziNbxt8/oKFrfFRIEXsLHKZVNByNnQJdxE9s/Ls0zTa0i3iMkfm7XEesydwuLpi7I6LXEhyt9rc9tqtFDmKLw+spkVipo/kAMnYQryjVQDoFfvJRZLhBG13X7rkBEBDXRCtw7lGT1ENvViXX1o+F5+KjuHOA/qepIU332+QeamXKA5kstiBJGf6GvYujMyOeaYC6ylf+ddXKxC5j2i/VNtxsLZKdoiXXH5hLB4G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k X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, Jul 09, 2024 at 04:57:38PM +0000, Roman Gushchin wrote: > On Mon, Jul 08, 2024 at 12:18:34PM -0700, Kees Cook wrote: > > Hi, > > > > This is an RFC for some changes I'd like to make to the kernel's > > allocators (starting with slab) that allow for type introspection, which > > has been a long-time gap in potential analysis capabilities available > > at compile-time. The changes here are just a "first step" example that > > updates kmalloc() and kzalloc() to show what I'm thinking we can do, > > and shows an example conversion within the fs/pstore tree. > > > > Repeating patch 3's commit log here: > > > > There is currently no way for the slab to know what type is being > > allocated, and this hampers the development of any logic that would need > > this information including basic type checking, alignment need analysis, > > etc. > > > > Allow the size argument to optionally be a variable, from which the > > type (and there by the size, alignment, or any other features) can be > > determined at compile-time. This allows for the incremental replacement > > of the classic code pattern: > > > > obj = kmalloc(sizeof(*obj), gfp); > > > > into: > > > > obj = kmalloc(obj, gfp); > > > > As an additional build-time safety feature, the return value of kmalloc() > > also becomes typed so that the assignment and first argument cannot drift, > > doing away with the other, more fragile, classic code pattern: > > > > obj = kmalloc(sizeof(struct the_object), gfp); > > > > into: > > > > obj = kmalloc(obj, gfp); > > I like the idea, however it's not as simple and straightforward because > it's common for structures to have a variable part (usually at the end) > and also allocate more than one structure at once. > > There are many allocations which look like > kmalloc(sizeof(my_struct) * 2 + SOME_MAGIC_LENGTH, GFP_...) > or something like this, which you can't easily convert to your scheme. Right -- and with this we can leave those as-is initially (since a size argument will still work). > The only option I see is to introduce the new set of functions/macros, > something like kmalloc_obj() or kmalloc_struct(). Or maybe tmalloc()? > (t for typed) Yeah, in a neighboring thread I was talking about a kmalloc_obj that would handle fixed-sized structs, flexible array structs, and arrays. I need to prove out the array part, but the first two should be trivial to implement. -- Kees Cook