Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Alexander Potapenko <glider@google.com>,
	Marco Elver <elver@google.com>,
	 Dmitry Vyukov <dvyukov@google.com>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org,
	 Chris Mason <mason@kernel.org>,
	 "David Hildenbrand (Arm)" <david@kernel.org>
Subject: [PATCH] kfence: use a nofault store to zero the guard page
Date: Fri, 27 Nov 2026 11:04:22 +0100	[thread overview]
Message-ID: <20261127-kfence_guard_zero-v1-1-cb33d3ee653b@kernel.org> (raw)

From: Chris Mason <mason@kernel.org>

After an out-of-bounds report, freeing the reported object can deadlock
on its own meta->lock with interrupts disabled, or trigger a false
out-of-bounds report on a neighbouring object.

With kfence.fault=oops, this can happen more reliably, without races.

kfence_guarded_free() zeroes the guard page that meta->unprotected_page
points at while holding meta->lock, assuming that the page is
unprotected. However, the page might still be protected:

 - kfence_handle_page_fault() calls kfence_unprotect() only after
   dropping the lock, so a concurrent free is possible.

 - With kfence.fault=oops, kfence_handle_fault() calls BUG(), so we
   never get to kfence_unprotect().

 - If both neighbours of a guard page recorded it, freeing the first one
   protects it again.

The store then faults, and kfence_handle_page_fault() either tries to
take the meta->lock we are holding, or reports a bogus out-of-bounds
access on the other neighbour.

Let's fix it by zeroing the page with copy_to_kernel_nofault(), so that
a store to a protected page simply fails. All architectures that
support KFENCE try exception fixups before they call
kfence_handle_page_fault().

Skipping the zeroing is fine: either the other neighbour zeroed the page
before protecting it, or it was never unprotected.

This is a quick fix: nothing serializes the protection of a guard page
against the fault handler, so after a racing free the page can stay
unprotected and we will not report later out-of-bounds accesses to it.
Add a TODO as a reminder to sort that out properly.

Fixes: 94868a1e127b ("kfence: zero guard page after out-of-bounds access")
Fixes: da735962d05c ("kfence: add kfence.fault parameter")
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust comment and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
I think the underlying problem should be fixed at some point. If this
quick fix here is not acceptable, kfence people please take over and
fix it properly right away :)
---
 mm/kfence/core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index bae787fd9baf7..02a07676ac7aa 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -33,6 +33,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
+#include <linux/uaccess.h>
 
 #include <asm/kfence.h>
 
@@ -553,7 +554,16 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool z
 
 	/* Restore page protection if there was an OOB access. */
 	if (meta->unprotected_page) {
-		memzero_explicit((void *)ALIGN_DOWN(meta->unprotected_page, PAGE_SIZE), PAGE_SIZE);
+		/*
+		 * The fault handler may not have unprotected the page, or
+		 * freeing a neighbouring object may have re-protected it. Don't
+		 * fault, otherwise we can deadlock on meta->lock.
+		 *
+		 * TODO: Serialize the protection of guard pages against the
+		 * fault handler, so that we always find the page unprotected.
+		 */
+		copy_to_kernel_nofault((void *)ALIGN_DOWN(meta->unprotected_page, PAGE_SIZE),
+				       page_address(ZERO_PAGE(0)), PAGE_SIZE);
 		kfence_protect(meta->unprotected_page);
 		meta->unprotected_page = 0;
 	}

---
base-commit: 739e2d979b66be560b608a38c4ff86150ec090be
change-id: 20261127-kfence_guard_zero-fda6d21938a4

Best regards,
--  
David Hildenbrand (Arm) <david@kernel.org>



                 reply	other threads:[~2026-11-27 10:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127-kfence_guard_zero-v1-1-cb33d3ee653b@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox