From: "Pierre-Clément Tosi" <ptosi@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Andrey Konovalov <andreyknvl@gmail.com>
Cc: "Alexander Potapenko" <glider@google.com>,
"Dmitry Vyukov" <dvyukov@google.com>,
"Vincenzo Frascino" <vincenzo.frascino@arm.com>,
"Marco Elver" <elver@google.com>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Harry Yoo" <harry@kernel.org>,
kasan-dev@googlegroups.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
"Pierre-Clément Tosi" <ptosi@google.com>
Subject: [PATCH] kasan: fix resampling of mempool page unpoisoning
Date: Tue, 01 Dec 2026 16:22:45 +0100 [thread overview]
Message-ID: <20261201-fix-mempool-resample-v1-1-79bdf0278f68@google.com> (raw)
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>
reply other threads:[~2026-12-01 15:23 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=20261201-fix-mempool-resample-v1-1-79bdf0278f68@google.com \
--to=ptosi@google.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=harry@kernel.org \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryabinin.a.a@gmail.com \
--cc=stable@vger.kernel.org \
--cc=vbabka@kernel.org \
--cc=vincenzo.frascino@arm.com \
/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
all inboxes | Powered by JetHome®