Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp40363rdb; Wed, 21 Feb 2024 16:25:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWQxkchFJ9rZ0tufRxNf2yliO+aITLroMTUmsVtdj1IdFJqgoSY+MidGS50pFRcHgPWjlGrwdGgJnvSr21OZei1Q5T7l30GN+BOfEmihg== X-Google-Smtp-Source: AGHT+IFONHHYPGR4ZN6zJbwzUZ1+7OSf2XW1nSUhFk2znDbpoaQcq909kSGwHl4AxjNZzzDahHTE X-Received: by 2002:a05:6a21:9206:b0:19e:be5c:b7c6 with SMTP id tl6-20020a056a21920600b0019ebe5cb7c6mr25878567pzb.45.1708561528959; Wed, 21 Feb 2024 16:25:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708561528; cv=pass; d=google.com; s=arc-20160816; b=cw+zXMyZpNKUIfhPjvPKGcbFcLEaq2ZgJQkC+wB+mZu37u/Lpg8xBYGO8j0fIOsR3G Zbs3P+JcO8/a3rKoNlPC+UAcg9tyfEw35OVnM5J/raoBfOT5+sRPO+DF5f7Md7pKRD6K Vd72p1LICXxuvWqUIcQU0TWSvgDBswhF5KnQMx9q9YSXBUwidVyHYJtd4t4Z99ZTuPLt q0XwXJ83ch2LOazRysN1hs9YX94r4XAwK7LFo35HSEA3ED+P5n1QvcPK5aG8jtVY0DlI C2emJD2sDO2+NJIZj6AbZIGKyyHxgghSZdAYrREVLQ+jroVfT08JtVi9OcVTJ/m3pzxl 3QwQ== 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=WrVxGxFZ9H3+hlcYIVohDlCBqheEbLY5A8SDm36k8gU=; fh=DVJHRqTu41dMjn0MTMv6qwB5R9Ae087phz3ytsyq9qQ=; b=gDH1k2BcPzP9LCBZrmxoRl4PpT9/+P3n7PVYxbrQ6f4SQgXwSGoyg7H+VgpAJWt723 Mf4doejnS0Mqdc3wUMRo7A4Or4A0E9WJWqPWE8yEcIaXiYz5nKWTOKKNyNkXBngCqyUA dmW5qGXJ7DFIP+vUZqjnfpLIV+yMnTTKvTEY0Jk88wjf9DxxXmnbdmklhHw9IlFkVXfT /93uHxesOjtHz8snXXS+FGplsfFPl6VkNfbH2xoOb7TCqze8BJiEhjZ/8ftbNkAecN43 FIwyjjKldQGzv9Qyy0BVTiPOGoeHgilKvd75E6wW0A3qH/hR1p92xkwehP3Y5Ig/IGbX Rcdg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ofhFBy0g; 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-75721-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75721-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id z12-20020aa7990c000000b006e0385d7185si5991418pff.229.2024.02.21.16.25.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 16:25:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75721-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=@chromium.org header.s=google header.b=ofhFBy0g; 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-75721-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75721-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A25DD283555 for ; Thu, 22 Feb 2024 00:25:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A12BC10A3D; Thu, 22 Feb 2024 00:25:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ofhFBy0g" Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 18C70F9CE for ; Thu, 22 Feb 2024 00:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708561505; cv=none; b=rL81Ne9ux6bL+N0y+wXx37S0tCBI/t6DOEwWzsPE+PZtFstZ22qmi7npF3h8rqu0KEN9Ki/XuyhsXl5eZ5qqTxrii/m/EH26tYkix3iEEi7v1aet6WvBW8yj92cHiPuEZVjW4zzUMvxN8MqfKQno/nk5S3u+gPb0RyxdCK+6BLE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708561505; c=relaxed/simple; bh=Wl2VBsr8T1E9brAP9ZLk+di58Ii4yEnpHnIDqcL7Ys4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=IKY55UTbNXXIoWnsMiCRwUWKNL8gI9/pcqVSizNY+0DXGUP5RaTgu5k0OSGplRPzj5mCQjRIrTBEBwUyeYqXCCMp1A6Ac0IUYmgtSkn96MfoLO45/YKgbmq2QAaxVEidwBvTGYqQ4NuJ3ivQr/Iz6wGg47rKI20lz1FVN8eleko= 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=ofhFBy0g; arc=none smtp.client-ip=209.85.210.179 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-f179.google.com with SMTP id d2e1a72fcca58-6e34d12404eso3774058b3a.2 for ; Wed, 21 Feb 2024 16:25:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708561503; x=1709166303; 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=WrVxGxFZ9H3+hlcYIVohDlCBqheEbLY5A8SDm36k8gU=; b=ofhFBy0gzNz2gmMbsAtpvobfLetDEr2h/tPE+renP/99jyIWS80pMhgteraIKPRAuT dVlepU+AgxElGXhJTrV3XW5TKRRYghKYMkGBB6bsmf9UWTapjBEs+sytEdFYdPj195G4 U9m8fSBu+mk6/tjkHArePIX9vdpFML0ALGQ50= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708561503; x=1709166303; 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=WrVxGxFZ9H3+hlcYIVohDlCBqheEbLY5A8SDm36k8gU=; b=RLogXrWLQL9ZOBSg+pxuES6ZGk2yCnURko2anskhe3lpue7nTsWHQYkex0MezWaTNC KCutLW5Xtajt9TsvtJ9b6r8KzWdGCln/aDWrjCxe/7LgDROv17bfiM3R7+v38IneFdSO J6eUjRnZ+ruZECZ+s7ugXWqiMdOZlxk2zrLEQgvbrF1pgvBrcFqqFQfe0g2GSV8+c+24 0ArZ1ct8w1wB/XJBOup8brjB0kySP6gBa09U1gjQmiM68GxY37saw3Q2iZRliZfjfErK GtLh6mLfdEV7w6iV3k/5RQ9a332Gjt0TfHYFFXWlmUHSJR8yTRmFiOeX7TwMZReTTK0s 2GxA== X-Forwarded-Encrypted: i=1; AJvYcCXCIQuDh0LQLpvmvk+JgDzgc3Lkf82B7lVEfTYP8MPgQYXnUBo+QCnCZnpn0iMwdgvopuKKjcHOaD1oOBABflsez1nbeM+80pR3NBj8 X-Gm-Message-State: AOJu0YyMt2zu5W2m56qhyGCnznz4Il56EQtNQBEZCuAto/d2XHn1Rjah ZNcDjNBXifhNQx2jw1KFP2TzT+zk6Yxhabh+SOhx3D0bA9nOi2d6LuIK3QAmpA== X-Received: by 2002:aa7:8a06:0:b0:6e4:59d0:febe with SMTP id m6-20020aa78a06000000b006e459d0febemr11129392pfa.7.1708561503366; Wed, 21 Feb 2024 16:25:03 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id q13-20020a056a00088d00b006e05c801748sm9501136pfj.199.2024.02.21.16.25.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 16:25:03 -0800 (PST) Date: Wed, 21 Feb 2024 16:25:02 -0800 From: Kees Cook To: Kent Overstreet Cc: Suren Baghdasaryan , akpm@linux-foundation.org, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org Subject: Re: [PATCH v4 14/36] lib: add allocation tagging support for memory allocation profiling Message-ID: <202402211608.41AD94094@keescook> References: <20240221194052.927623-1-surenb@google.com> <20240221194052.927623-15-surenb@google.com> <202402211449.401382D2AF@keescook> <4vwiwgsemga7vmahgwsikbsawjq5xfskdsssmjsfe5hn7k2alk@b6ig5v2pxe5i> 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: <4vwiwgsemga7vmahgwsikbsawjq5xfskdsssmjsfe5hn7k2alk@b6ig5v2pxe5i> On Wed, Feb 21, 2024 at 06:29:17PM -0500, Kent Overstreet wrote: > On Wed, Feb 21, 2024 at 03:05:32PM -0800, Kees Cook wrote: > > On Wed, Feb 21, 2024 at 11:40:27AM -0800, Suren Baghdasaryan wrote: > > > [...] > > > +struct alloc_tag { > > > + struct codetag ct; > > > + struct alloc_tag_counters __percpu *counters; > > > +} __aligned(8); > > > [...] > > > +#define DEFINE_ALLOC_TAG(_alloc_tag) \ > > > + static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ > > > + static struct alloc_tag _alloc_tag __used __aligned(8) \ > > > + __section("alloc_tags") = { \ > > > + .ct = CODE_TAG_INIT, \ > > > + .counters = &_alloc_tag_cntr }; > > > [...] > > > +static inline struct alloc_tag *alloc_tag_save(struct alloc_tag *tag) > > > +{ > > > + swap(current->alloc_tag, tag); > > > + return tag; > > > +} > > > > Future security hardening improvement idea based on this infrastructure: > > it should be possible to implement per-allocation-site kmem caches. For > > example, we could create: > > > > struct alloc_details { > > u32 flags; > > union { > > u32 size; /* not valid after __init completes */ > > struct kmem_cache *cache; > > }; > > }; > > > > - add struct alloc_details to struct alloc_tag > > - move the tags section into .ro_after_init > > - extend alloc_hooks() to populate flags and size: > > .flags = __builtin_constant_p(size) ? KMALLOC_ALLOCATE_FIXED > > : KMALLOC_ALLOCATE_BUCKETS; > > .size = __builtin_constant_p(size) ? size : SIZE_MAX; > > - during kernel start or module init, walk the alloc_tag list > > and create either a fixed-size kmem_cache or to allocate a > > full set of kmalloc-buckets, and update the "cache" member. > > - adjust kmalloc core routines to use current->alloc_tag->cache instead > > of using the global buckets. > > > > This would get us fully separated allocations, producing better than > > type-based levels of granularity, exceeding what we have currently with > > CONFIG_RANDOM_KMALLOC_CACHES. > > > > Does this look possible, or am I misunderstanding something in the > > infrastructure being created here? > > Definitely possible, but... would we want this? Yes, very very much. One of the worst and mostly unaddressed weaknesses with the kernel right now is use-after-free based type confusion[0], which depends on merged caches (or cache reuse). This doesn't solve cross-allocator (kmalloc/page_alloc) type confusion (as terrifyingly demonstrated[1] by Jann Horn), but it does help with what has been a very common case of "use msg_msg to impersonate your target object"[2] exploitation. > That would produce a _lot_ of kmem caches Fewer than you'd expect, but yes, there is some overhead. However, out-of-tree forks of Linux have successfully experimented with this already and seen good results[3]. > and don't we already try to collapse those where possible to reduce > internal fragmentation? In the past, yes, but the desire for security has tended to have more people building with SLAB_MERGE_DEFAULT=n and/or CONFIG_RANDOM_KMALLOC_CACHES=y (or booting with "slab_nomerge"). Just doing the type safety isn't sufficient without the cross-allocator safety, but we've also had solutions for that proposed[4]. -Kees [0] https://github.com/KSPP/linux/issues/189 [1] https://googleprojectzero.blogspot.com/2021/10/how-simple-linux-kernel-memory.html [2] https://www.willsroot.io/2021/08/corctf-2021-fire-of-salvation-writeup.html https://google.github.io/security-research/pocs/linux/cve-2021-22555/writeup.html#exploring-struct-msg_msg [3] https://grsecurity.net/how_autoslab_changes_the_memory_unsafety_game [4] https://lore.kernel.org/linux-hardening/20230915105933.495735-1-matteorizzo@google.com/ -- Kees Cook