Add a KUnit test (page_alloc_highuser_kasan) to verify that, unlike GFP_HIGHUSER, page allocations using GFP_HIGHUSER_KASAN under CONFIG_KASAN_HW_TAGS: 1. Get a KASAN tag in page_kasan_tag() across all sub-pages of a high-order allocation, and the same tag in the pointer returned by page_address(), and 2. Are poisoned on free, so that a use-after-free through the stale pointer is reported. The test fails if __GFP_SKIP_KASAN is added to GFP_HIGHUSER_KASAN. Tested on arm64 with CONFIG_KASAN_HW_TAGS under QEMU (virt,mte=on) with kasan.mode=sync, async and asymm. Assisted-by: LLM Signed-off-by: Jeff Xu --- mm/kasan/kasan_test_c.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index 72342137bb18..285b82793bdd 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -423,6 +423,33 @@ static void page_alloc_skip_kasan(struct kunit *test) page_alloc_skip_kasan_helper(test, GFP_HIGHUSER_MOVABLE); } +static void page_alloc_highuser_kasan(struct kunit *test) +{ + struct page *pages; + size_t order = 2; + char *ptr; + size_t i; + + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_HW_TAGS); + + pages = alloc_pages(GFP_HIGHUSER_KASAN, order); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pages); + ptr = page_address(pages); + + /* Unlike GFP_HIGHUSER, these pages must get a KASAN tag. */ + for (i = 0; i < (1UL << order); i++) { + KUNIT_EXPECT_GE(test, page_kasan_tag(pages + i), + (u8)KASAN_TAG_MIN); + KUNIT_EXPECT_LT(test, page_kasan_tag(pages + i), + (u8)KASAN_TAG_KERNEL); + } + KUNIT_EXPECT_EQ(test, (u8)get_tag(ptr), page_kasan_tag(pages)); + + /* They are poisoned on free, so a use-after-free is reported. */ + __free_pages(pages, order); + KUNIT_EXPECT_KASAN_FAIL_READ(test, READ_ONCE(*ptr)); +} + static void krealloc_more_oob_helper(struct kunit *test, size_t size1, size_t size2) { @@ -2413,6 +2440,7 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(page_alloc_oob_right), KUNIT_CASE(page_alloc_uaf), KUNIT_CASE(page_alloc_skip_kasan), + KUNIT_CASE(page_alloc_highuser_kasan), KUNIT_CASE(krealloc_more_oob), KUNIT_CASE(krealloc_less_oob), KUNIT_CASE(krealloc_large_more_oob), -- 2.56.0.611.g1030f0b940-goog