mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kasan: fix resampling of mempool page unpoisoning
@ 2026-12-01 15:22 Pierre-Clément Tosi
  0 siblings, 0 replies; only message in thread
From: Pierre-Clément Tosi @ 2026-12-01 15:22 UTC (permalink / raw)
  To: Andrew Morton, Andrey Ryabinin, Andrey Konovalov
  Cc: Alexander Potapenko, Dmitry Vyukov, Vincenzo Frascino,
	Marco Elver, Vlastimil Babka, Harry Yoo, kasan-dev, linux-mm,
	linux-kernel, stable, Pierre-Clément Tosi

With CONFIG_KASAN_HW_TAGS and kasan.page_alloc.sample > 1, alloc_pages()
tags sampled allocations with a random tag while skipped allocations
are marked with KASAN_TAG_KERNEL.

When a page-backed mempool caches an allocation in add_element(),
__kasan_mempool_poison_pages() already skips unsampled pages. However,
a later remove_element() ends up rolling kasan_sample_page_alloc() a
second time, from executing __kasan_unpoison_pages() through
__kasan_mempool_unpoison_pages().

When remove_element() rolls a "no-sample" on an element previously
sampled by alloc_pages(), it skips unpoisoning and the region stays
tagged with KASAN_PAGE_FREE while page_address(page) is tagged with
the random tag. Any subsequent access to the allocation (or returning it
to the mempool) triggers tag mismatches and false-positives such as

    "BUG: KASAN: double-free [...] Pointer tag: [fd], memory tag: [fe]"

during mempool_free(), under

    kasan.mode=sync
    kasan.page_alloc.sample=2
    kasan.page_alloc.sample.order=2

Note that in this illustration, mempool then drops the element (as it
should for genuine double-frees) which leaks its pages, permanently
shrinking the pool's reserve.

Therefore, skip unsampled allocs in __kasan_mempool_unpoison_pages() and
remove the double-sampling, as likely originally intended.

Fixes: 44383cef54c0 ("kasan: allow sampling page_alloc allocations for HW_TAGS")
Fixes: 9f41c59ae316 ("kasan: introduce kasan_mempool_unpoison_pages")
Cc: stable@vger.kernel.org
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
 mm/kasan/common.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 4187606a88cf..0c198ad895b4 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -137,22 +137,28 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
 }
 #endif /* CONFIG_KASAN_STACK */
 
-bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
+static void unpoison_page_alloc(struct page *page, unsigned int order,
+				bool init)
 {
 	u8 tag;
 	unsigned long i;
 
+	tag = kasan_random_tag();
+	kasan_unpoison(set_tag(page_address(page), tag),
+		       PAGE_SIZE << order, init);
+	for (i = 0; i < (1UL << order); i++)
+		page_kasan_tag_set(page + i, tag);
+}
+
+bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
+{
 	if (unlikely(PageHighMem(page)))
 		return false;
 
 	if (!kasan_sample_page_alloc(order))
 		return false;
 
-	tag = kasan_random_tag();
-	kasan_unpoison(set_tag(page_address(page), tag),
-		       PAGE_SIZE << order, init);
-	for (i = 0; i < (1 << order); i++)
-		page_kasan_tag_set(page + i, tag);
+	unpoison_page_alloc(page, order, init);
 
 	return true;
 }
@@ -527,7 +533,15 @@ bool __kasan_mempool_poison_pages(struct page *page, unsigned int order,
 void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order,
 				    unsigned long ip)
 {
-	__kasan_unpoison_pages(page, order, false);
+	if (unlikely(PageHighMem(page)))
+		return;
+
+	/* Bail out if allocation was excluded due to sampling. */
+	if (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
+	    page_kasan_tag(page) == KASAN_TAG_KERNEL)
+		return;
+
+	unpoison_page_alloc(page, order, false);
 }
 
 bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)

---
base-commit: 130b10745521b04ea857b9ac33580c9cb421143b
change-id: 20261127-fix-mempool-resample-ea561bae66ef

Best regards,
-- 
Pierre-Clément Tosi <ptosi@google.com>


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01 15:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 15:22 [PATCH] kasan: fix resampling of mempool page unpoisoning Pierre-Clément Tosi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®