1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
8 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/kasan.h>
11 #include <linux/kernel.h>
13 #include <linux/mman.h>
14 #include <linux/module.h>
15 #include <linux/printk.h>
16 #include <linux/random.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/uaccess.h>
21 #include <linux/vmalloc.h>
25 #include <kunit/test.h>
27 #include "../mm/kasan/kasan.h"
29 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
32 * Some tests use these global variables to store return values from function
33 * calls that could otherwise be eliminated by the compiler as dead code.
35 void *kasan_ptr_result;
38 static struct kunit_resource resource;
39 static struct kunit_kasan_expectation fail_data;
40 static bool multishot;
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
44 * first detected bug and panic the kernel if panic_on_warn is enabled. For
45 * hardware tag-based KASAN also allow tag checking to be reenabled for each
46 * test, see the comment for KUNIT_EXPECT_KASAN_FAIL().
48 static int kasan_test_init(struct kunit *test)
50 if (!kasan_enabled()) {
51 kunit_err(test, "can't run KASAN tests with KASAN disabled");
55 multishot = kasan_save_enable_multi_shot();
56 kasan_set_tagging_report_once(false);
57 fail_data.report_found = false;
58 kunit_add_named_resource(test, NULL, NULL, &resource,
59 "kasan_data", &fail_data);
63 static void kasan_test_exit(struct kunit *test)
65 kasan_set_tagging_report_once(true);
66 kasan_restore_multi_shot(multishot);
67 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
71 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
72 * KASAN report; causes a test failure otherwise. This relies on a KUnit
73 * resource named "kasan_data". Do not use this name for KUnit resources
74 * outside of KASAN tests.
76 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
77 * checking is auto-disabled. When this happens, this test handler reenables
78 * tag checking. As tag checking can be only disabled or enabled per CPU,
79 * this handler disables migration (preemption).
81 * Since the compiler doesn't see that the expression can change the fail_data
82 * fields, it can reorder or optimize away the accesses to those fields.
83 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
84 * expression to prevent that.
86 * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
87 * false. This allows detecting KASAN reports that happen outside of the checks
88 * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
89 * and in kasan_test_exit.
91 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
92 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
93 !kasan_async_mode_enabled()) \
95 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
99 if (!READ_ONCE(fail_data.report_found)) { \
100 KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure " \
101 "expected in \"" #expression \
102 "\", but none occurred"); \
104 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
105 if (READ_ONCE(fail_data.report_found)) \
106 kasan_enable_tagging_sync(); \
109 WRITE_ONCE(fail_data.report_found, false); \
112 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
113 if (!IS_ENABLED(config)) \
114 kunit_skip((test), "Test requires " #config "=y"); \
117 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
118 if (IS_ENABLED(config)) \
119 kunit_skip((test), "Test requires " #config "=n"); \
122 static void kmalloc_oob_right(struct kunit *test)
127 ptr = kmalloc(size, GFP_KERNEL);
128 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
130 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
134 static void kmalloc_oob_left(struct kunit *test)
139 ptr = kmalloc(size, GFP_KERNEL);
140 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
142 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
146 static void kmalloc_node_oob_right(struct kunit *test)
151 ptr = kmalloc_node(size, GFP_KERNEL, 0);
152 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
154 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
159 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
160 * fit into a slab cache and therefore is allocated via the page allocator
161 * fallback. Since this kind of fallback is only implemented for SLUB, these
162 * tests are limited to that allocator.
164 static void kmalloc_pagealloc_oob_right(struct kunit *test)
167 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
169 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
171 ptr = kmalloc(size, GFP_KERNEL);
172 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
174 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
179 static void kmalloc_pagealloc_uaf(struct kunit *test)
182 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
184 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
186 ptr = kmalloc(size, GFP_KERNEL);
187 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
190 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
193 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
196 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
198 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
200 ptr = kmalloc(size, GFP_KERNEL);
201 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
203 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
206 static void pagealloc_oob_right(struct kunit *test)
211 size_t size = (1UL << (PAGE_SHIFT + order));
214 * With generic KASAN page allocations have no redzones, thus
215 * out-of-bounds detection is not guaranteed.
216 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
218 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
220 pages = alloc_pages(GFP_KERNEL, order);
221 ptr = page_address(pages);
222 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
224 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
225 free_pages((unsigned long)ptr, order);
228 static void pagealloc_uaf(struct kunit *test)
234 pages = alloc_pages(GFP_KERNEL, order);
235 ptr = page_address(pages);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
237 free_pages((unsigned long)ptr, order);
239 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
242 static void kmalloc_large_oob_right(struct kunit *test)
245 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
248 * Allocate a chunk that is large enough, but still fits into a slab
249 * and does not trigger the page allocator fallback in SLUB.
251 ptr = kmalloc(size, GFP_KERNEL);
252 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
254 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
258 static void krealloc_more_oob_helper(struct kunit *test,
259 size_t size1, size_t size2)
264 KUNIT_ASSERT_LT(test, size1, size2);
265 middle = size1 + (size2 - size1) / 2;
267 ptr1 = kmalloc(size1, GFP_KERNEL);
268 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
270 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
271 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
273 /* All offsets up to size2 must be accessible. */
274 ptr2[size1 - 1] = 'x';
277 ptr2[size2 - 1] = 'x';
279 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
280 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
281 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
283 /* For all modes first aligned offset after size2 must be inaccessible. */
284 KUNIT_EXPECT_KASAN_FAIL(test,
285 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
290 static void krealloc_less_oob_helper(struct kunit *test,
291 size_t size1, size_t size2)
296 KUNIT_ASSERT_LT(test, size2, size1);
297 middle = size2 + (size1 - size2) / 2;
299 ptr1 = kmalloc(size1, GFP_KERNEL);
300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
302 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
303 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
305 /* Must be accessible for all modes. */
306 ptr2[size2 - 1] = 'x';
308 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
309 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
310 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
312 /* For all modes first aligned offset after size2 must be inaccessible. */
313 KUNIT_EXPECT_KASAN_FAIL(test,
314 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
317 * For all modes all size2, middle, and size1 should land in separate
318 * granules and thus the latter two offsets should be inaccessible.
320 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
321 round_down(middle, KASAN_GRANULE_SIZE));
322 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
323 round_down(size1, KASAN_GRANULE_SIZE));
324 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
325 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
326 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
331 static void krealloc_more_oob(struct kunit *test)
333 krealloc_more_oob_helper(test, 201, 235);
336 static void krealloc_less_oob(struct kunit *test)
338 krealloc_less_oob_helper(test, 235, 201);
341 static void krealloc_pagealloc_more_oob(struct kunit *test)
343 /* page_alloc fallback in only implemented for SLUB. */
344 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
346 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
347 KMALLOC_MAX_CACHE_SIZE + 235);
350 static void krealloc_pagealloc_less_oob(struct kunit *test)
352 /* page_alloc fallback in only implemented for SLUB. */
353 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
355 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
356 KMALLOC_MAX_CACHE_SIZE + 201);
360 * Check that krealloc() detects a use-after-free, returns NULL,
361 * and doesn't unpoison the freed object.
363 static void krealloc_uaf(struct kunit *test)
369 ptr1 = kmalloc(size1, GFP_KERNEL);
370 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
373 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
374 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
375 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
378 static void kmalloc_oob_16(struct kunit *test)
384 /* This test is specifically crafted for the generic mode. */
385 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
387 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
388 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
390 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
391 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
393 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
398 static void kmalloc_uaf_16(struct kunit *test)
404 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
407 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
408 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
411 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
415 static void kmalloc_oob_memset_2(struct kunit *test)
420 ptr = kmalloc(size, GFP_KERNEL);
421 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
423 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
427 static void kmalloc_oob_memset_4(struct kunit *test)
432 ptr = kmalloc(size, GFP_KERNEL);
433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
435 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
440 static void kmalloc_oob_memset_8(struct kunit *test)
445 ptr = kmalloc(size, GFP_KERNEL);
446 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
448 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
452 static void kmalloc_oob_memset_16(struct kunit *test)
457 ptr = kmalloc(size, GFP_KERNEL);
458 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
460 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
464 static void kmalloc_oob_in_memset(struct kunit *test)
469 ptr = kmalloc(size, GFP_KERNEL);
470 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
472 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
476 static void kmalloc_memmove_invalid_size(struct kunit *test)
480 volatile size_t invalid_size = -2;
482 ptr = kmalloc(size, GFP_KERNEL);
483 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
485 memset((char *)ptr, 0, 64);
487 KUNIT_EXPECT_KASAN_FAIL(test,
488 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
492 static void kmalloc_uaf(struct kunit *test)
497 ptr = kmalloc(size, GFP_KERNEL);
498 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
501 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
504 static void kmalloc_uaf_memset(struct kunit *test)
509 ptr = kmalloc(size, GFP_KERNEL);
510 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
513 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
516 static void kmalloc_uaf2(struct kunit *test)
523 ptr1 = kmalloc(size, GFP_KERNEL);
524 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
528 ptr2 = kmalloc(size, GFP_KERNEL);
529 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
532 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
533 * Allow up to 16 attempts at generating different tags.
535 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
540 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
541 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
546 static void kfree_via_page(struct kunit *test)
551 unsigned long offset;
553 ptr = kmalloc(size, GFP_KERNEL);
554 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
556 page = virt_to_page(ptr);
557 offset = offset_in_page(ptr);
558 kfree(page_address(page) + offset);
561 static void kfree_via_phys(struct kunit *test)
567 ptr = kmalloc(size, GFP_KERNEL);
568 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
570 phys = virt_to_phys(ptr);
571 kfree(phys_to_virt(phys));
574 static void kmem_cache_oob(struct kunit *test)
578 struct kmem_cache *cache;
580 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
581 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
583 p = kmem_cache_alloc(cache, GFP_KERNEL);
585 kunit_err(test, "Allocation failed: %s\n", __func__);
586 kmem_cache_destroy(cache);
590 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
592 kmem_cache_free(cache, p);
593 kmem_cache_destroy(cache);
596 static void kmem_cache_accounted(struct kunit *test)
601 struct kmem_cache *cache;
603 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
604 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
607 * Several allocations with a delay to allow for lazy per memcg kmem
610 for (i = 0; i < 5; i++) {
611 p = kmem_cache_alloc(cache, GFP_KERNEL);
615 kmem_cache_free(cache, p);
620 kmem_cache_destroy(cache);
623 static void kmem_cache_bulk(struct kunit *test)
625 struct kmem_cache *cache;
631 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
632 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
634 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
636 kunit_err(test, "Allocation failed: %s\n", __func__);
637 kmem_cache_destroy(cache);
641 for (i = 0; i < ARRAY_SIZE(p); i++)
642 p[i][0] = p[i][size - 1] = 42;
644 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
645 kmem_cache_destroy(cache);
648 static char global_array[10];
650 static void kasan_global_oob(struct kunit *test)
653 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
654 * from failing here and panicking the kernel, access the array via a
655 * volatile pointer, which will prevent the compiler from being able to
656 * determine the array bounds.
658 * This access uses a volatile pointer to char (char *volatile) rather
659 * than the more conventional pointer to volatile char (volatile char *)
660 * because we want to prevent the compiler from making inferences about
661 * the pointer itself (i.e. its array bounds), not the data that it
664 char *volatile array = global_array;
665 char *p = &array[ARRAY_SIZE(global_array) + 3];
667 /* Only generic mode instruments globals. */
668 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
670 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
673 /* Check that ksize() makes the whole object accessible. */
674 static void ksize_unpoisons_memory(struct kunit *test)
677 size_t size = 123, real_size;
679 ptr = kmalloc(size, GFP_KERNEL);
680 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
681 real_size = ksize(ptr);
683 /* This access shouldn't trigger a KASAN report. */
687 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
693 * Check that a use-after-free is detected by ksize() and via normal accesses
696 static void ksize_uaf(struct kunit *test)
699 int size = 128 - KASAN_GRANULE_SIZE;
701 ptr = kmalloc(size, GFP_KERNEL);
702 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
705 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
706 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
707 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
710 static void kasan_stack_oob(struct kunit *test)
712 char stack_array[10];
713 /* See comment in kasan_global_oob. */
714 char *volatile array = stack_array;
715 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
717 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
719 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
722 static void kasan_alloca_oob_left(struct kunit *test)
725 char alloca_array[i];
726 /* See comment in kasan_global_oob. */
727 char *volatile array = alloca_array;
730 /* Only generic mode instruments dynamic allocas. */
731 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
732 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
734 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
737 static void kasan_alloca_oob_right(struct kunit *test)
740 char alloca_array[i];
741 /* See comment in kasan_global_oob. */
742 char *volatile array = alloca_array;
745 /* Only generic mode instruments dynamic allocas. */
746 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
747 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
749 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
752 static void kmem_cache_double_free(struct kunit *test)
756 struct kmem_cache *cache;
758 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
759 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
761 p = kmem_cache_alloc(cache, GFP_KERNEL);
763 kunit_err(test, "Allocation failed: %s\n", __func__);
764 kmem_cache_destroy(cache);
768 kmem_cache_free(cache, p);
769 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
770 kmem_cache_destroy(cache);
773 static void kmem_cache_invalid_free(struct kunit *test)
777 struct kmem_cache *cache;
779 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
781 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
783 p = kmem_cache_alloc(cache, GFP_KERNEL);
785 kunit_err(test, "Allocation failed: %s\n", __func__);
786 kmem_cache_destroy(cache);
790 /* Trigger invalid free, the object doesn't get freed. */
791 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
794 * Properly free the object to prevent the "Objects remaining in
795 * test_cache on __kmem_cache_shutdown" BUG failure.
797 kmem_cache_free(cache, p);
799 kmem_cache_destroy(cache);
802 static void kasan_memchr(struct kunit *test)
808 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
809 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
811 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
814 size = round_up(size, OOB_TAG_OFF);
816 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
817 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
819 KUNIT_EXPECT_KASAN_FAIL(test,
820 kasan_ptr_result = memchr(ptr, '1', size + 1));
825 static void kasan_memcmp(struct kunit *test)
832 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
833 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
835 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
838 size = round_up(size, OOB_TAG_OFF);
840 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
841 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
842 memset(arr, 0, sizeof(arr));
844 KUNIT_EXPECT_KASAN_FAIL(test,
845 kasan_int_result = memcmp(ptr, arr, size+1));
849 static void kasan_strings(struct kunit *test)
855 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
856 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
858 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
860 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
861 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
866 * Try to cause only 1 invalid access (less spam in dmesg).
867 * For that we need ptr to point to zeroed byte.
868 * Skip metadata that could be stored in freed object so ptr
869 * will likely point to zeroed byte.
872 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
874 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
876 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
878 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
880 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
882 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
885 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
887 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
888 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
889 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
890 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
891 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
892 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
893 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
894 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
897 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
899 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
900 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
901 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
902 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
903 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
904 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
905 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
906 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
908 #if defined(clear_bit_unlock_is_negative_byte)
909 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
910 clear_bit_unlock_is_negative_byte(nr, addr));
914 static void kasan_bitops_generic(struct kunit *test)
918 /* This test is specifically crafted for the generic mode. */
919 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
922 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
923 * this way we do not actually corrupt other memory.
925 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
926 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
929 * Below calls try to access bit within allocated memory; however, the
930 * below accesses are still out-of-bounds, since bitops are defined to
931 * operate on the whole long the bit is in.
933 kasan_bitops_modify(test, BITS_PER_LONG, bits);
936 * Below calls try to access bit beyond allocated memory.
938 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
943 static void kasan_bitops_tags(struct kunit *test)
947 /* This test is specifically crafted for tag-based modes. */
948 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
950 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
951 bits = kzalloc(48, GFP_KERNEL);
952 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
954 /* Do the accesses past the 48 allocated bytes, but within the redone. */
955 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
956 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
961 static void kmalloc_double_kzfree(struct kunit *test)
966 ptr = kmalloc(size, GFP_KERNEL);
967 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
969 kfree_sensitive(ptr);
970 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
973 static void vmalloc_oob(struct kunit *test)
977 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
980 * We have to be careful not to hit the guard page.
981 * The MMU will catch that and crash us.
983 area = vmalloc(3000);
984 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
986 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
991 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
992 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
995 static void match_all_not_assigned(struct kunit *test)
1001 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1003 for (i = 0; i < 256; i++) {
1004 size = (get_random_int() % 1024) + 1;
1005 ptr = kmalloc(size, GFP_KERNEL);
1006 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1007 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1008 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1012 for (i = 0; i < 256; i++) {
1013 order = (get_random_int() % 4) + 1;
1014 pages = alloc_pages(GFP_KERNEL, order);
1015 ptr = page_address(pages);
1016 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1017 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1018 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1019 free_pages((unsigned long)ptr, order);
1023 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1024 static void match_all_ptr_tag(struct kunit *test)
1029 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1031 ptr = kmalloc(128, GFP_KERNEL);
1032 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1034 /* Backup the assigned tag. */
1036 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1038 /* Reset the tag to 0xff.*/
1039 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1041 /* This access shouldn't trigger a KASAN report. */
1044 /* Recover the pointer tag and free. */
1045 ptr = set_tag(ptr, tag);
1049 /* Check that there are no match-all memory tags for tag-based modes. */
1050 static void match_all_mem_tag(struct kunit *test)
1055 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1057 ptr = kmalloc(128, GFP_KERNEL);
1058 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1059 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1061 /* For each possible tag value not matching the pointer tag. */
1062 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1063 if (tag == get_tag(ptr))
1066 /* Mark the first memory granule with the chosen memory tag. */
1067 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1069 /* This access must cause a KASAN report. */
1070 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1073 /* Recover the memory tag and free. */
1074 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1078 static struct kunit_case kasan_kunit_test_cases[] = {
1079 KUNIT_CASE(kmalloc_oob_right),
1080 KUNIT_CASE(kmalloc_oob_left),
1081 KUNIT_CASE(kmalloc_node_oob_right),
1082 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1083 KUNIT_CASE(kmalloc_pagealloc_uaf),
1084 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1085 KUNIT_CASE(pagealloc_oob_right),
1086 KUNIT_CASE(pagealloc_uaf),
1087 KUNIT_CASE(kmalloc_large_oob_right),
1088 KUNIT_CASE(krealloc_more_oob),
1089 KUNIT_CASE(krealloc_less_oob),
1090 KUNIT_CASE(krealloc_pagealloc_more_oob),
1091 KUNIT_CASE(krealloc_pagealloc_less_oob),
1092 KUNIT_CASE(krealloc_uaf),
1093 KUNIT_CASE(kmalloc_oob_16),
1094 KUNIT_CASE(kmalloc_uaf_16),
1095 KUNIT_CASE(kmalloc_oob_in_memset),
1096 KUNIT_CASE(kmalloc_oob_memset_2),
1097 KUNIT_CASE(kmalloc_oob_memset_4),
1098 KUNIT_CASE(kmalloc_oob_memset_8),
1099 KUNIT_CASE(kmalloc_oob_memset_16),
1100 KUNIT_CASE(kmalloc_memmove_invalid_size),
1101 KUNIT_CASE(kmalloc_uaf),
1102 KUNIT_CASE(kmalloc_uaf_memset),
1103 KUNIT_CASE(kmalloc_uaf2),
1104 KUNIT_CASE(kfree_via_page),
1105 KUNIT_CASE(kfree_via_phys),
1106 KUNIT_CASE(kmem_cache_oob),
1107 KUNIT_CASE(kmem_cache_accounted),
1108 KUNIT_CASE(kmem_cache_bulk),
1109 KUNIT_CASE(kasan_global_oob),
1110 KUNIT_CASE(kasan_stack_oob),
1111 KUNIT_CASE(kasan_alloca_oob_left),
1112 KUNIT_CASE(kasan_alloca_oob_right),
1113 KUNIT_CASE(ksize_unpoisons_memory),
1114 KUNIT_CASE(ksize_uaf),
1115 KUNIT_CASE(kmem_cache_double_free),
1116 KUNIT_CASE(kmem_cache_invalid_free),
1117 KUNIT_CASE(kasan_memchr),
1118 KUNIT_CASE(kasan_memcmp),
1119 KUNIT_CASE(kasan_strings),
1120 KUNIT_CASE(kasan_bitops_generic),
1121 KUNIT_CASE(kasan_bitops_tags),
1122 KUNIT_CASE(kmalloc_double_kzfree),
1123 KUNIT_CASE(vmalloc_oob),
1124 KUNIT_CASE(match_all_not_assigned),
1125 KUNIT_CASE(match_all_ptr_tag),
1126 KUNIT_CASE(match_all_mem_tag),
1130 static struct kunit_suite kasan_kunit_test_suite = {
1132 .init = kasan_test_init,
1133 .test_cases = kasan_kunit_test_cases,
1134 .exit = kasan_test_exit,
1137 kunit_test_suite(kasan_kunit_test_suite);
1139 MODULE_LICENSE("GPL");