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);
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.
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),