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 C442DC35FFC for ; Sat, 22 Mar 2025 07:18:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF396280003; Sat, 22 Mar 2025 03:18:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA46D280001; Sat, 22 Mar 2025 03:18:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6C52280003; Sat, 22 Mar 2025 03:18:54 -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 A8E7F280001 for ; Sat, 22 Mar 2025 03:18:54 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B096A1CC23F for ; Sat, 22 Mar 2025 07:18:55 +0000 (UTC) X-FDA: 83248335030.25.280B368 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf13.hostedemail.com (Postfix) with ESMTP id 1D05320003 for ; Sat, 22 Mar 2025 07:18:53 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nMFiqeNI; spf=pass (imf13.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=1742627934; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MZ8sPSrWnnM4piXSiD6x0kM9HEX6kZNLkmO/7aXzH2U=; b=elZPmyB6ka3IAptctuN4enlHCGybxe8NS5NWUVJhftQxuKZESxSbpBixCHhI9ky0/jpyBX DptzCsJpC5/zf0g5y7Kd7E8fRjAWhoQ7flxlvB2WzztiUHDmGOuxwmE5dc7ujCyYpUiMaB Jt/w9BKoben0dcsSVrCKm0Nt9x4+ILA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nMFiqeNI; spf=pass (imf13.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=1742627934; a=rsa-sha256; cv=none; b=8lGzn1reHg544oRyodlcc0YPSdT4ahvFaa1PSVampnmSpuF5YeovKNT1DQb5njo7GRJX2U E4rRZEfo4yOGNOls1q6ISRW8uNlIZfTLmJpoZDZSYubnPWKZdnDneGasMRb7QK9OsOsqoK /O4wXdrr3r+JX1PAEZ+NdUZ0OTZ+Nzg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 2FE2AA4A1C3; Sat, 22 Mar 2025 07:13:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 169ADC4CEDD; Sat, 22 Mar 2025 07:18:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742627933; bh=5YMW/DlS0ebGtlxpPMHrs7Dv7gA/oYjUad8cwYfX9Ys=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nMFiqeNIksNdGGRfCHxNVLnfEM0y+dtvL4aPrDhAWdnK6/kfk0QxvmdrO3iv9fpxw DLNpWIooMxIjV+zHXcTTay4U3gjiAEeBCJD2MQXxEP/473xpYLhrM4F25Axravembf cATBSlxgy67RYIpjrpUHOH/C9mbvKiQvHNg1kbGTz8eEq/P8jMbOILzy18Amt0BKX8 +NoGdA80UA8YGxGZk0gJT5LSqDKAqqvsmaGT57kWxGvRgmwy21RWxDlKFlVIFsnKap 0RP11hAGgBowov3NI0SRqsBJUY2RmS8Sqx5N6y8doSBWSgpGROgYKgilW4jURCBv+Y u8oR0afv5C95A== Date: Sat, 22 Mar 2025 00:18:49 -0700 From: Kees Cook To: Jann Horn Cc: Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, Miguel Ojeda , Nathan Chancellor , Marco Elver , Nick Desaulniers , Przemek Kitszel , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 4/5] slab: Set freed variables to NULL by default Message-ID: <202503220003.FABF5E82D1@keescook> References: <20250321202620.work.175-kees@kernel.org> <20250321204105.1898507-4-kees@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1D05320003 X-Stat-Signature: gwm1byqjhggfo1amqzb5je1z9pdiz44y X-HE-Tag: 1742627933-73210 X-HE-Meta: U2FsdGVkX18DHDJ8dDyYBKvS1P0unKZKdqiGtlWHxL+iFSN5tW1+uRYjG3ClPDMFjL1bIZrRhlBmv+IwrIm5O9QB5I33+ke13as9z7xUbTcWronvx4L+WYu8cMj2A4dDnzK6WlsT+8C0cIJwVYQCKrEqVjncK7FOjzwbIc9xKc2oKImPRcNGcIQu49NEltvz0SMIldYu92+U6oEJG0N5QJJGJ1iQYjalLVQ/jcQIlzJl1qv45DWfWBXMprs1hpM59IScA4VNmp2g8KFg/8aEmgT5lm4r2oSgVF5dKKmGblhES/hh5ctR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ayYDkRIFConzBkJ375+/Ejv0ag7Hg== X-Bogosity: Ham, tests=bogofilter, 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 Sat, Mar 22, 2025 at 02:50:15AM +0100, Jann Horn wrote: > On Fri, Mar 21, 2025 at 9:41 PM Kees Cook wrote: > > To defang a subset of "dangling pointer" use-after-free flaws[1], take the > > address of any lvalues passed to kfree() and set them to NULL after > > freeing. > > > > To do this manually, kfree_and_null() (and the "sensitive" variant) > > are introduced. > > Unless callers of kfree() are allowed to rely on this behavior, we > might want to have an option to use a poison value instead of NULL for > this in debug builds. Sure -- we have many to choose from. Is there a specific one you think would be good? > > Hmm... in theory, we could have an object that points to its own type, like so: > > struct foo { > struct foo *ptr; > }; > > And if that was initialized like so: > > struct foo *obj = kmalloc(sizeof(struct foo)); > obj->ptr = obj; > > Then the following is currently fine, but would turn into UAF with this patch: > > kfree(obj->ptr); > > That is a fairly contrived example; but maybe it would make sense to > reorder the NULL assignment and the actual freeing to avoid this > particular case? Ew. Yeah. Reordering this looks okay, yes: diff --git a/include/linux/slab.h b/include/linux/slab.h index 3f8fb60963e3..8913b05eca33 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -473,13 +473,15 @@ extern atomic_t count_nulled; static inline void kfree_and_null(void **ptr) { - __kfree(*ptr); + void *addr = *ptr; *ptr = NULL; + __kfree(addr); } static inline void kfree_sensitive_and_null(void **ptr) { - __kfree_sensitive(*ptr); + void *addr = *ptr; *ptr = NULL; + __kfree_sensitive(addr); } #define __force_assignable(x) \ > Another pattern that is theoretically currently fine but would be > problematic after this would be if code continues to access a pointer > after the pointed-to object has been freed, but just to check for > NULL-ness to infer something about the state of the object containing > the pointer, or to check pointer identity, or something like that. But > I guess that's probably not very common? Something like: > > if (ptr) { > mutex_lock(&some_lock); > ... > kfree(ptr); > } > ... > if (ptr) { > ... > mutex_unlock(&some_lock); > } Yeah, this would be bad form IMO. But it's not impossible. This idea was mentioned on the Fediverse thread for this RFC too. https://fosstodon.org/@kees/114202495615591299 > But from scrolling through the kernel sources and glancing at a few > hundred kfree() calls (not a lot compared to the ~40000 callsites we > have), I haven't actually found any obvious instances like that. There > is KMSAN test code that intentionally does a UAF, which might need to > be adjusted to not hit a NULL deref instead. > (And just an irrelevant sidenote: snd_gf1_dma_interrupt() has > commented-out debugging code that would UAF the "block" variable if it > was not commented out.) Yeah, the heap LKDTM tests need to switch to __kfree() too. :) I'm going to see if I can build a sensible Coccinelle script to look for this. It's currently getting very confused by the may "for_each" helpers, but here's what I've got currently: @use_freed@ expression E, RHS; @@ ( kfree(E); ... when != E ? E = RHS | kfree(E); ... * E ) It is finding a few, though. For example: --- ./drivers/md/dm-ima.c +++ /tmp/nothing/drivers/md/dm-ima.c @@ -584,13 +584,11 @@ error: exit: kfree(md->ima.active_table.device_metadata); - if (md->ima.active_table.device_metadata != md->ima.inactive_table.device_metadata) kfree(md->ima.inactive_table.device_metadata); -- Kees Cook