[PATCH v1] mm: kmsan: add tests for high-order page freeing

0 views
Skip to first unread message

Alexander Potapenko

unread,
Jan 12, 2026, 9:51:58 AM (6 days ago) Jan 12
to gli...@google.com, ak...@linux-foundation.org, ryan.r...@arm.com, linu...@kvack.org, linux-...@vger.kernel.org, el...@google.com, dvy...@google.com, kasa...@googlegroups.com
Add regression tests to verify that KMSAN correctly poisons the full memory
range when freeing pages.

Specifically, verify that accessing the tail pages of a high-order
non-compound allocation triggers a use-after-free report. This ensures
that the fix "mm: kmsan: Fix poisoning of high-order non-compound pages"
is working as expected.

Also add a test for standard order-0 pages for completeness.

Link: https://lore.kernel.org/all/20260104134348.354...@arm.com/
Signed-off-by: Alexander Potapenko <gli...@google.com>
---
mm/kmsan/kmsan_test.c | 48 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
index 902ec48b1e3e6..25cfba0db2cfb 100644
--- a/mm/kmsan/kmsan_test.c
+++ b/mm/kmsan/kmsan_test.c
@@ -361,7 +361,7 @@ static void test_init_vmalloc(struct kunit *test)
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

-/* Test case: ensure that use-after-free reporting works. */
+/* Test case: ensure that use-after-free reporting works for kmalloc. */
static void test_uaf(struct kunit *test)
{
EXPECTATION_USE_AFTER_FREE(expect);
@@ -378,6 +378,50 @@ static void test_uaf(struct kunit *test)
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

+/* Test case: ensure that use-after-free reporting works for freed pages. */
+static void test_uaf_pages(struct kunit *test)
+{
+ EXPECTATION_USE_AFTER_FREE(expect);
+ const int order = 0;
+ volatile char value;
+ struct page *page;
+ volatile char *var;
+
+ kunit_info(test, "use-after-free on a freed page (UMR report)\n");
+
+ /* Memory is initialized up until __free_pages() thanks to __GFP_ZERO. */
+ page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
+ var = page_address(page);
+ __free_pages(page, order);
+
+ /* Copy the invalid value before checking it. */
+ value = var[3];
+ USE(value);
+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
+}
+
+/* Test case: ensure that use-after-free reporting works for alloc_pages. */
+static void test_uaf_high_order_pages(struct kunit *test)
+{
+ EXPECTATION_USE_AFTER_FREE(expect);
+ const int order = 1;
+ volatile char value;
+ struct page *page;
+ volatile char *var;
+
+ kunit_info(test,
+ "use-after-free on a freed high-order page (UMR report)\n");
+
+ page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
+ var = page_address(page) + PAGE_SIZE;
+ __free_pages(page, order);
+
+ /* Copy the invalid value before checking it. */
+ value = var[3];
+ USE(value);
+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
+}
+
/*
* Test case: ensure that uninitialized values are propagated through per-CPU
* memory.
@@ -683,6 +727,8 @@ static struct kunit_case kmsan_test_cases[] = {
KUNIT_CASE(test_init_kmsan_vmap_vunmap),
KUNIT_CASE(test_init_vmalloc),
KUNIT_CASE(test_uaf),
+ KUNIT_CASE(test_uaf_pages),
+ KUNIT_CASE(test_uaf_high_order_pages),
KUNIT_CASE(test_percpu_propagate),
KUNIT_CASE(test_printk),
KUNIT_CASE(test_init_memcpy),
--
2.52.0.457.g6b5491de43-goog

Ryan Roberts

unread,
Jan 12, 2026, 10:18:05 AM (6 days ago) Jan 12
to Alexander Potapenko, ak...@linux-foundation.org, linu...@kvack.org, linux-...@vger.kernel.org, el...@google.com, dvy...@google.com, kasa...@googlegroups.com
test_uaf_pages() and test_uaf_high_order_pages() are the same except for the
value of order. Does it make sense to create a single parameterized helper that
gets called from the 2 tests wrappers?

Functionally looks correct though, so either way, feel free to add:

Reviewed-by: Ryan Roberts <ryan.r...@arm.com>

Alexander Potapenko

unread,
Jan 13, 2026, 4:12:03 AM (5 days ago) Jan 13
to gli...@google.com, ak...@linux-foundation.org, ryan.r...@arm.com, linu...@kvack.org, linux-...@vger.kernel.org, el...@google.com, dvy...@google.com, kasa...@googlegroups.com
Add regression tests to verify that KMSAN correctly poisons the full memory
range when freeing pages.

Specifically, verify that accessing the tail pages of a high-order
non-compound allocation triggers a use-after-free report. This ensures
that the fix "mm: kmsan: Fix poisoning of high-order non-compound pages"
is working as expected.

Also add a test for standard order-0 pages for completeness.

Link: https://lore.kernel.org/all/20260104134348.354...@arm.com/
Signed-off-by: Alexander Potapenko <gli...@google.com>
Reviewed-by: Ryan Roberts <ryan.r...@arm.com>

---
v2: factored out the common part of the two tests
---
mm/kmsan/kmsan_test.c | 49 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
index 902ec48b1e3e6..ba44bf2072bbe 100644
--- a/mm/kmsan/kmsan_test.c
+++ b/mm/kmsan/kmsan_test.c
@@ -361,7 +361,7 @@ static void test_init_vmalloc(struct kunit *test)
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

-/* Test case: ensure that use-after-free reporting works. */
+/* Test case: ensure that use-after-free reporting works for kmalloc. */
static void test_uaf(struct kunit *test)
{
EXPECTATION_USE_AFTER_FREE(expect);
@@ -378,6 +378,51 @@ static void test_uaf(struct kunit *test)
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

+static volatile char *test_uaf_pages_helper(int order, int offset)
+{
+ struct page *page;
+ volatile char *var;
+
+ /* Memory is initialized up until __free_pages() thanks to __GFP_ZERO. */
+ page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
+ var = page_address(page) + offset;
+ __free_pages(page, order);
+
+ return var;
+}
+
+/* Test case: ensure that use-after-free reporting works for a freed page. */
+static void test_uaf_pages(struct kunit *test)
+{
+ EXPECTATION_USE_AFTER_FREE(expect);
+ volatile char value;
+
+ kunit_info(test, "use-after-free on a freed page (UMR report)\n");
+ /* Allocate a single page, free it, then try to access it. */
+ value = *test_uaf_pages_helper(0, 3);
+ USE(value);
+
+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
+}
+
+/* Test case: ensure that UAF reporting works for high order pages. */
+static void test_uaf_high_order_pages(struct kunit *test)
+{
+ EXPECTATION_USE_AFTER_FREE(expect);
+ volatile char value;
+
+ kunit_info(test,
+ "use-after-free on a freed high-order page (UMR report)\n");
+ /*
+ * Create a high-order non-compound page, free it, then try to access
+ * its tail page.
+ */
+ value = *test_uaf_pages_helper(1, PAGE_SIZE + 3);
+ USE(value);
+
+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
+}
+
/*
* Test case: ensure that uninitialized values are propagated through per-CPU
* memory.
@@ -683,6 +728,8 @@ static struct kunit_case kmsan_test_cases[] = {
KUNIT_CASE(test_init_kmsan_vmap_vunmap),
KUNIT_CASE(test_init_vmalloc),
KUNIT_CASE(test_uaf),
+ KUNIT_CASE(test_uaf_pages),
+ KUNIT_CASE(test_uaf_high_order_pages),
KUNIT_CASE(test_percpu_propagate),
KUNIT_CASE(test_printk),
KUNIT_CASE(test_init_memcpy),
--
2.52.0.457.g6b5491de43-goog

Alexander Potapenko

unread,
Jan 13, 2026, 4:12:07 AM (5 days ago) Jan 13
to gli...@google.com, ak...@linux-foundation.org, ryan.r...@arm.com, linu...@kvack.org, linux-...@vger.kernel.org, el...@google.com, dvy...@google.com, kasa...@googlegroups.com
Test that pages allocated with alloc_page() are uninitialized
by default.

Signed-off-by: Alexander Potapenko <gli...@google.com>
---
mm/kmsan/kmsan_test.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
index ba44bf2072bbe..81e642db6e239 100644
--- a/mm/kmsan/kmsan_test.c
+++ b/mm/kmsan/kmsan_test.c
@@ -378,6 +378,20 @@ static void test_uaf(struct kunit *test)
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

+static void test_uninit_page(struct kunit *test)
+{
+ EXPECTATION_UNINIT_VALUE(expect);
+ struct page *page;
+ int *ptr;
+
+ kunit_info(test, "uninitialized page allocation (UMR report)\n");
+ page = alloc_pages(GFP_KERNEL, 0);
+ ptr = page_address(page);
+ USE(*ptr);
+ __free_pages(page, 0);
+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
+}
+
static volatile char *test_uaf_pages_helper(int order, int offset)
{
struct page *page;
@@ -727,6 +741,7 @@ static struct kunit_case kmsan_test_cases[] = {
KUNIT_CASE(test_uninit_kmsan_check_memory),
KUNIT_CASE(test_init_kmsan_vmap_vunmap),
KUNIT_CASE(test_init_vmalloc),
+ KUNIT_CASE(test_uninit_page),
KUNIT_CASE(test_uaf),
KUNIT_CASE(test_uaf_pages),
KUNIT_CASE(test_uaf_high_order_pages),
--
2.52.0.457.g6b5491de43-goog

Reply all
Reply to author
Forward
0 new messages