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 #define pr_fmt(fmt) "kasan_test: " fmt
10 #include <kunit/test.h>
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
14 #include <linux/kasan.h>
15 #include <linux/kernel.h>
17 #include <linux/mman.h>
18 #include <linux/module.h>
19 #include <linux/printk.h>
20 #include <linux/random.h>
21 #include <linux/set_memory.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/tracepoint.h>
25 #include <linux/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <trace/events/printk.h>
33 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
35 static bool multishot;
37 /* Fields set based on lines observed in the console. */
44 * Some tests use these global variables to store return values from function
45 * calls that could otherwise be eliminated by the compiler as dead code.
47 void *kasan_ptr_result;
50 /* Probe for console output: obtains test_status lines of interest. */
51 static void probe_console(void *ignore, const char *buf, size_t len)
53 if (strnstr(buf, "BUG: KASAN: ", len))
54 WRITE_ONCE(test_status.report_found, true);
55 else if (strnstr(buf, "Asynchronous fault: ", len))
56 WRITE_ONCE(test_status.async_fault, true);
59 static int kasan_suite_init(struct kunit_suite *suite)
61 if (!kasan_enabled()) {
62 pr_err("Can't run KASAN tests with KASAN disabled");
66 /* Stop failing KUnit tests on KASAN reports. */
67 kasan_kunit_test_suite_start();
70 * Temporarily enable multi-shot mode. Otherwise, KASAN would only
71 * report the first detected bug and panic the kernel if panic_on_warn
74 multishot = kasan_save_enable_multi_shot();
76 register_trace_console(probe_console, NULL);
80 static void kasan_suite_exit(struct kunit_suite *suite)
82 kasan_kunit_test_suite_end();
83 kasan_restore_multi_shot(multishot);
84 unregister_trace_console(probe_console, NULL);
85 tracepoint_synchronize_unregister();
88 static void kasan_test_exit(struct kunit *test)
90 KUNIT_EXPECT_FALSE(test, READ_ONCE(test_status.report_found));
94 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
95 * KASAN report; causes a test failure otherwise. This relies on a KUnit
96 * resource named "kasan_status". Do not use this name for KUnit resources
97 * outside of KASAN tests.
99 * For hardware tag-based KASAN, when a synchronous tag fault happens, tag
100 * checking is auto-disabled. When this happens, this test handler reenables
101 * tag checking. As tag checking can be only disabled or enabled per CPU,
102 * this handler disables migration (preemption).
104 * Since the compiler doesn't see that the expression can change the test_status
105 * fields, it can reorder or optimize away the accesses to those fields.
106 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
107 * expression to prevent that.
109 * In between KUNIT_EXPECT_KASAN_FAIL checks, test_status.report_found is kept
110 * as false. This allows detecting KASAN reports that happen outside of the
111 * checks by asserting !test_status.report_found at the start of
112 * KUNIT_EXPECT_KASAN_FAIL and in kasan_test_exit.
114 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
115 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
116 kasan_sync_fault_possible()) \
118 KUNIT_EXPECT_FALSE(test, READ_ONCE(test_status.report_found)); \
122 if (kasan_async_fault_possible()) \
123 kasan_force_async_fault(); \
124 if (!READ_ONCE(test_status.report_found)) { \
125 KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure " \
126 "expected in \"" #expression \
127 "\", but none occurred"); \
129 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
130 kasan_sync_fault_possible()) { \
131 if (READ_ONCE(test_status.report_found) && \
132 !READ_ONCE(test_status.async_fault)) \
133 kasan_enable_hw_tags(); \
136 WRITE_ONCE(test_status.report_found, false); \
137 WRITE_ONCE(test_status.async_fault, false); \
140 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
141 if (!IS_ENABLED(config)) \
142 kunit_skip((test), "Test requires " #config "=y"); \
145 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
146 if (IS_ENABLED(config)) \
147 kunit_skip((test), "Test requires " #config "=n"); \
150 #define KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test) do { \
151 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
152 break; /* No compiler instrumentation. */ \
153 if (IS_ENABLED(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX)) \
154 break; /* Should always be instrumented! */ \
155 if (IS_ENABLED(CONFIG_GENERIC_ENTRY)) \
156 kunit_skip((test), "Test requires checked mem*()"); \
159 static void kmalloc_oob_right(struct kunit *test)
162 size_t size = 128 - KASAN_GRANULE_SIZE - 5;
164 ptr = kmalloc(size, GFP_KERNEL);
165 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
167 OPTIMIZER_HIDE_VAR(ptr);
169 * An unaligned access past the requested kmalloc size.
170 * Only generic KASAN can precisely detect these.
172 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
173 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');
176 * An aligned access into the first out-of-bounds granule that falls
177 * within the aligned kmalloc object.
179 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');
181 /* Out-of-bounds access past the aligned kmalloc object. */
182 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] =
183 ptr[size + KASAN_GRANULE_SIZE + 5]);
188 static void kmalloc_oob_left(struct kunit *test)
193 ptr = kmalloc(size, GFP_KERNEL);
194 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
196 OPTIMIZER_HIDE_VAR(ptr);
197 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
201 static void kmalloc_node_oob_right(struct kunit *test)
206 ptr = kmalloc_node(size, GFP_KERNEL, 0);
207 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
209 OPTIMIZER_HIDE_VAR(ptr);
210 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
215 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
216 * fit into a slab cache and therefore is allocated via the page allocator
217 * fallback. Since this kind of fallback is only implemented for SLUB, these
218 * tests are limited to that allocator.
220 static void kmalloc_pagealloc_oob_right(struct kunit *test)
223 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
225 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
227 ptr = kmalloc(size, GFP_KERNEL);
228 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
230 OPTIMIZER_HIDE_VAR(ptr);
231 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
236 static void kmalloc_pagealloc_uaf(struct kunit *test)
239 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
241 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
243 ptr = kmalloc(size, GFP_KERNEL);
244 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
247 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
250 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
253 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
255 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
257 ptr = kmalloc(size, GFP_KERNEL);
258 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
260 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
263 static void pagealloc_oob_right(struct kunit *test)
268 size_t size = (1UL << (PAGE_SHIFT + order));
271 * With generic KASAN page allocations have no redzones, thus
272 * out-of-bounds detection is not guaranteed.
273 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
275 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
277 pages = alloc_pages(GFP_KERNEL, order);
278 ptr = page_address(pages);
279 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
281 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
282 free_pages((unsigned long)ptr, order);
285 static void pagealloc_uaf(struct kunit *test)
291 pages = alloc_pages(GFP_KERNEL, order);
292 ptr = page_address(pages);
293 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
294 free_pages((unsigned long)ptr, order);
296 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
299 static void kmalloc_large_oob_right(struct kunit *test)
302 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
305 * Allocate a chunk that is large enough, but still fits into a slab
306 * and does not trigger the page allocator fallback in SLUB.
308 ptr = kmalloc(size, GFP_KERNEL);
309 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
311 OPTIMIZER_HIDE_VAR(ptr);
312 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
316 static void krealloc_more_oob_helper(struct kunit *test,
317 size_t size1, size_t size2)
322 KUNIT_ASSERT_LT(test, size1, size2);
323 middle = size1 + (size2 - size1) / 2;
325 ptr1 = kmalloc(size1, GFP_KERNEL);
326 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
328 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
329 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
331 /* Suppress -Warray-bounds warnings. */
332 OPTIMIZER_HIDE_VAR(ptr2);
334 /* All offsets up to size2 must be accessible. */
335 ptr2[size1 - 1] = 'x';
338 ptr2[size2 - 1] = 'x';
340 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
341 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
342 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
344 /* For all modes first aligned offset after size2 must be inaccessible. */
345 KUNIT_EXPECT_KASAN_FAIL(test,
346 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
351 static void krealloc_less_oob_helper(struct kunit *test,
352 size_t size1, size_t size2)
357 KUNIT_ASSERT_LT(test, size2, size1);
358 middle = size2 + (size1 - size2) / 2;
360 ptr1 = kmalloc(size1, GFP_KERNEL);
361 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
363 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
364 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
366 /* Suppress -Warray-bounds warnings. */
367 OPTIMIZER_HIDE_VAR(ptr2);
369 /* Must be accessible for all modes. */
370 ptr2[size2 - 1] = 'x';
372 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
373 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
374 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
376 /* For all modes first aligned offset after size2 must be inaccessible. */
377 KUNIT_EXPECT_KASAN_FAIL(test,
378 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
381 * For all modes all size2, middle, and size1 should land in separate
382 * granules and thus the latter two offsets should be inaccessible.
384 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
385 round_down(middle, KASAN_GRANULE_SIZE));
386 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
387 round_down(size1, KASAN_GRANULE_SIZE));
388 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
389 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
390 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
395 static void krealloc_more_oob(struct kunit *test)
397 krealloc_more_oob_helper(test, 201, 235);
400 static void krealloc_less_oob(struct kunit *test)
402 krealloc_less_oob_helper(test, 235, 201);
405 static void krealloc_pagealloc_more_oob(struct kunit *test)
407 /* page_alloc fallback in only implemented for SLUB. */
408 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
410 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
411 KMALLOC_MAX_CACHE_SIZE + 235);
414 static void krealloc_pagealloc_less_oob(struct kunit *test)
416 /* page_alloc fallback in only implemented for SLUB. */
417 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
419 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
420 KMALLOC_MAX_CACHE_SIZE + 201);
424 * Check that krealloc() detects a use-after-free, returns NULL,
425 * and doesn't unpoison the freed object.
427 static void krealloc_uaf(struct kunit *test)
433 ptr1 = kmalloc(size1, GFP_KERNEL);
434 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
437 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
438 KUNIT_ASSERT_NULL(test, ptr2);
439 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
442 static void kmalloc_oob_16(struct kunit *test)
448 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
450 /* This test is specifically crafted for the generic mode. */
451 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
453 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
454 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
456 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
457 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
459 OPTIMIZER_HIDE_VAR(ptr1);
460 OPTIMIZER_HIDE_VAR(ptr2);
461 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
466 static void kmalloc_uaf_16(struct kunit *test)
472 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
474 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
475 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
477 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
478 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
481 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
486 * Note: in the memset tests below, the written range touches both valid and
487 * invalid memory. This makes sure that the instrumentation does not only check
488 * the starting address but the whole range.
491 static void kmalloc_oob_memset_2(struct kunit *test)
494 size_t size = 128 - KASAN_GRANULE_SIZE;
496 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
498 ptr = kmalloc(size, GFP_KERNEL);
499 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
501 OPTIMIZER_HIDE_VAR(size);
502 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
506 static void kmalloc_oob_memset_4(struct kunit *test)
509 size_t size = 128 - KASAN_GRANULE_SIZE;
511 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
513 ptr = kmalloc(size, GFP_KERNEL);
514 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
516 OPTIMIZER_HIDE_VAR(size);
517 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
521 static void kmalloc_oob_memset_8(struct kunit *test)
524 size_t size = 128 - KASAN_GRANULE_SIZE;
526 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
528 ptr = kmalloc(size, GFP_KERNEL);
529 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
531 OPTIMIZER_HIDE_VAR(size);
532 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
536 static void kmalloc_oob_memset_16(struct kunit *test)
539 size_t size = 128 - KASAN_GRANULE_SIZE;
541 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
543 ptr = kmalloc(size, GFP_KERNEL);
544 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
546 OPTIMIZER_HIDE_VAR(size);
547 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
551 static void kmalloc_oob_in_memset(struct kunit *test)
554 size_t size = 128 - KASAN_GRANULE_SIZE;
556 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
558 ptr = kmalloc(size, GFP_KERNEL);
559 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
561 OPTIMIZER_HIDE_VAR(ptr);
562 OPTIMIZER_HIDE_VAR(size);
563 KUNIT_EXPECT_KASAN_FAIL(test,
564 memset(ptr, 0, size + KASAN_GRANULE_SIZE));
568 static void kmalloc_memmove_negative_size(struct kunit *test)
572 size_t invalid_size = -2;
574 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
577 * Hardware tag-based mode doesn't check memmove for negative size.
578 * As a result, this test introduces a side-effect memory corruption,
579 * which can result in a crash.
581 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_HW_TAGS);
583 ptr = kmalloc(size, GFP_KERNEL);
584 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
586 memset((char *)ptr, 0, 64);
587 OPTIMIZER_HIDE_VAR(ptr);
588 OPTIMIZER_HIDE_VAR(invalid_size);
589 KUNIT_EXPECT_KASAN_FAIL(test,
590 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
594 static void kmalloc_memmove_invalid_size(struct kunit *test)
598 size_t invalid_size = size;
600 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
602 ptr = kmalloc(size, GFP_KERNEL);
603 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
605 memset((char *)ptr, 0, 64);
606 OPTIMIZER_HIDE_VAR(ptr);
607 OPTIMIZER_HIDE_VAR(invalid_size);
608 KUNIT_EXPECT_KASAN_FAIL(test,
609 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
613 static void kmalloc_uaf(struct kunit *test)
618 ptr = kmalloc(size, GFP_KERNEL);
619 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
622 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[8]);
625 static void kmalloc_uaf_memset(struct kunit *test)
630 KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
633 * Only generic KASAN uses quarantine, which is required to avoid a
634 * kernel memory corruption this test causes.
636 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
638 ptr = kmalloc(size, GFP_KERNEL);
639 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
642 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
645 static void kmalloc_uaf2(struct kunit *test)
652 ptr1 = kmalloc(size, GFP_KERNEL);
653 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
657 ptr2 = kmalloc(size, GFP_KERNEL);
658 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
661 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
662 * Allow up to 16 attempts at generating different tags.
664 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
669 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[40]);
670 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
676 * Check that KASAN detects use-after-free when another object was allocated in
677 * the same slot. Relevant for the tag-based modes, which do not use quarantine.
679 static void kmalloc_uaf3(struct kunit *test)
684 /* This test is specifically crafted for tag-based modes. */
685 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
687 ptr1 = kmalloc(size, GFP_KERNEL);
688 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
691 ptr2 = kmalloc(size, GFP_KERNEL);
692 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
695 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[8]);
698 static void kfree_via_page(struct kunit *test)
703 unsigned long offset;
705 ptr = kmalloc(size, GFP_KERNEL);
706 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
708 page = virt_to_page(ptr);
709 offset = offset_in_page(ptr);
710 kfree(page_address(page) + offset);
713 static void kfree_via_phys(struct kunit *test)
719 ptr = kmalloc(size, GFP_KERNEL);
720 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
722 phys = virt_to_phys(ptr);
723 kfree(phys_to_virt(phys));
726 static void kmem_cache_oob(struct kunit *test)
730 struct kmem_cache *cache;
732 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
733 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
735 p = kmem_cache_alloc(cache, GFP_KERNEL);
737 kunit_err(test, "Allocation failed: %s\n", __func__);
738 kmem_cache_destroy(cache);
742 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
744 kmem_cache_free(cache, p);
745 kmem_cache_destroy(cache);
748 static void kmem_cache_accounted(struct kunit *test)
753 struct kmem_cache *cache;
755 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
756 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
759 * Several allocations with a delay to allow for lazy per memcg kmem
762 for (i = 0; i < 5; i++) {
763 p = kmem_cache_alloc(cache, GFP_KERNEL);
767 kmem_cache_free(cache, p);
772 kmem_cache_destroy(cache);
775 static void kmem_cache_bulk(struct kunit *test)
777 struct kmem_cache *cache;
783 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
784 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
786 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
788 kunit_err(test, "Allocation failed: %s\n", __func__);
789 kmem_cache_destroy(cache);
793 for (i = 0; i < ARRAY_SIZE(p); i++)
794 p[i][0] = p[i][size - 1] = 42;
796 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
797 kmem_cache_destroy(cache);
800 static char global_array[10];
802 static void kasan_global_oob_right(struct kunit *test)
805 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
806 * from failing here and panicking the kernel, access the array via a
807 * volatile pointer, which will prevent the compiler from being able to
808 * determine the array bounds.
810 * This access uses a volatile pointer to char (char *volatile) rather
811 * than the more conventional pointer to volatile char (volatile char *)
812 * because we want to prevent the compiler from making inferences about
813 * the pointer itself (i.e. its array bounds), not the data that it
816 char *volatile array = global_array;
817 char *p = &array[ARRAY_SIZE(global_array) + 3];
819 /* Only generic mode instruments globals. */
820 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
822 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
825 static void kasan_global_oob_left(struct kunit *test)
827 char *volatile array = global_array;
831 * GCC is known to fail this test, skip it.
832 * See https://bugzilla.kernel.org/show_bug.cgi?id=215051.
834 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_CC_IS_CLANG);
835 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
836 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
839 /* Check that ksize() does NOT unpoison whole object. */
840 static void ksize_unpoisons_memory(struct kunit *test)
843 size_t size = 128 - KASAN_GRANULE_SIZE - 5;
846 ptr = kmalloc(size, GFP_KERNEL);
847 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
849 real_size = ksize(ptr);
850 KUNIT_EXPECT_GT(test, real_size, size);
852 OPTIMIZER_HIDE_VAR(ptr);
854 /* These accesses shouldn't trigger a KASAN report. */
858 /* These must trigger a KASAN report. */
859 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
860 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);
861 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size + 5]);
862 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[real_size - 1]);
868 * Check that a use-after-free is detected by ksize() and via normal accesses
871 static void ksize_uaf(struct kunit *test)
874 int size = 128 - KASAN_GRANULE_SIZE;
876 ptr = kmalloc(size, GFP_KERNEL);
877 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
880 OPTIMIZER_HIDE_VAR(ptr);
881 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
882 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
883 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);
886 static void kasan_stack_oob(struct kunit *test)
888 char stack_array[10];
889 /* See comment in kasan_global_oob_right. */
890 char *volatile array = stack_array;
891 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
893 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
895 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
898 static void kasan_alloca_oob_left(struct kunit *test)
901 char alloca_array[i];
902 /* See comment in kasan_global_oob_right. */
903 char *volatile array = alloca_array;
906 /* Only generic mode instruments dynamic allocas. */
907 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
908 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
910 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
913 static void kasan_alloca_oob_right(struct kunit *test)
916 char alloca_array[i];
917 /* See comment in kasan_global_oob_right. */
918 char *volatile array = alloca_array;
921 /* Only generic mode instruments dynamic allocas. */
922 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
923 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
925 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
928 static void kmem_cache_double_free(struct kunit *test)
932 struct kmem_cache *cache;
934 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
935 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
937 p = kmem_cache_alloc(cache, GFP_KERNEL);
939 kunit_err(test, "Allocation failed: %s\n", __func__);
940 kmem_cache_destroy(cache);
944 kmem_cache_free(cache, p);
945 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
946 kmem_cache_destroy(cache);
949 static void kmem_cache_invalid_free(struct kunit *test)
953 struct kmem_cache *cache;
955 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
957 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
959 p = kmem_cache_alloc(cache, GFP_KERNEL);
961 kunit_err(test, "Allocation failed: %s\n", __func__);
962 kmem_cache_destroy(cache);
966 /* Trigger invalid free, the object doesn't get freed. */
967 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
970 * Properly free the object to prevent the "Objects remaining in
971 * test_cache on __kmem_cache_shutdown" BUG failure.
973 kmem_cache_free(cache, p);
975 kmem_cache_destroy(cache);
978 static void empty_cache_ctor(void *object) { }
980 static void kmem_cache_double_destroy(struct kunit *test)
982 struct kmem_cache *cache;
984 /* Provide a constructor to prevent cache merging. */
985 cache = kmem_cache_create("test_cache", 200, 0, 0, empty_cache_ctor);
986 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
987 kmem_cache_destroy(cache);
988 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_destroy(cache));
991 static void kasan_memchr(struct kunit *test)
997 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
998 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
1000 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
1003 size = round_up(size, OOB_TAG_OFF);
1005 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
1006 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1008 OPTIMIZER_HIDE_VAR(ptr);
1009 OPTIMIZER_HIDE_VAR(size);
1010 KUNIT_EXPECT_KASAN_FAIL(test,
1011 kasan_ptr_result = memchr(ptr, '1', size + 1));
1016 static void kasan_memcmp(struct kunit *test)
1023 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
1024 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
1026 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
1029 size = round_up(size, OOB_TAG_OFF);
1031 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
1032 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1033 memset(arr, 0, sizeof(arr));
1035 OPTIMIZER_HIDE_VAR(ptr);
1036 OPTIMIZER_HIDE_VAR(size);
1037 KUNIT_EXPECT_KASAN_FAIL(test,
1038 kasan_int_result = memcmp(ptr, arr, size+1));
1042 static void kasan_strings(struct kunit *test)
1048 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
1049 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
1051 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
1053 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
1054 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1059 * Try to cause only 1 invalid access (less spam in dmesg).
1060 * For that we need ptr to point to zeroed byte.
1061 * Skip metadata that could be stored in freed object so ptr
1062 * will likely point to zeroed byte.
1065 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
1067 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
1069 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
1071 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
1073 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
1075 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
1078 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
1080 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
1081 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
1082 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
1083 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
1084 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
1085 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
1086 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
1087 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
1090 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
1092 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
1093 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
1094 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
1095 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
1096 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
1097 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
1098 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
1099 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
1101 #if defined(clear_bit_unlock_is_negative_byte)
1102 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
1103 clear_bit_unlock_is_negative_byte(nr, addr));
1107 static void kasan_bitops_generic(struct kunit *test)
1111 /* This test is specifically crafted for the generic mode. */
1112 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
1115 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
1116 * this way we do not actually corrupt other memory.
1118 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
1119 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
1122 * Below calls try to access bit within allocated memory; however, the
1123 * below accesses are still out-of-bounds, since bitops are defined to
1124 * operate on the whole long the bit is in.
1126 kasan_bitops_modify(test, BITS_PER_LONG, bits);
1129 * Below calls try to access bit beyond allocated memory.
1131 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
1136 static void kasan_bitops_tags(struct kunit *test)
1140 /* This test is specifically crafted for tag-based modes. */
1141 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1143 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
1144 bits = kzalloc(48, GFP_KERNEL);
1145 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
1147 /* Do the accesses past the 48 allocated bytes, but within the redone. */
1148 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
1149 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
1154 static void kmalloc_double_kzfree(struct kunit *test)
1159 ptr = kmalloc(size, GFP_KERNEL);
1160 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1162 kfree_sensitive(ptr);
1163 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
1167 * The two tests below check that Generic KASAN prints auxiliary stack traces
1168 * for RCU callbacks and workqueues. The reports need to be inspected manually.
1170 * These tests are still enabled for other KASAN modes to make sure that all
1171 * modes report bad accesses in tested scenarios.
1174 static struct kasan_rcu_info {
1176 struct rcu_head rcu;
1179 static void rcu_uaf_reclaim(struct rcu_head *rp)
1181 struct kasan_rcu_info *fp =
1182 container_of(rp, struct kasan_rcu_info, rcu);
1185 ((volatile struct kasan_rcu_info *)fp)->i;
1188 static void rcu_uaf(struct kunit *test)
1190 struct kasan_rcu_info *ptr;
1192 ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL);
1193 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1195 global_rcu_ptr = rcu_dereference_protected(
1196 (struct kasan_rcu_info __rcu *)ptr, NULL);
1198 KUNIT_EXPECT_KASAN_FAIL(test,
1199 call_rcu(&global_rcu_ptr->rcu, rcu_uaf_reclaim);
1203 static void workqueue_uaf_work(struct work_struct *work)
1208 static void workqueue_uaf(struct kunit *test)
1210 struct workqueue_struct *workqueue;
1211 struct work_struct *work;
1213 workqueue = create_workqueue("kasan_workqueue_test");
1214 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, workqueue);
1216 work = kmalloc(sizeof(struct work_struct), GFP_KERNEL);
1217 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, work);
1219 INIT_WORK(work, workqueue_uaf_work);
1220 queue_work(workqueue, work);
1221 destroy_workqueue(workqueue);
1223 KUNIT_EXPECT_KASAN_FAIL(test,
1224 ((volatile struct work_struct *)work)->data);
1227 static void vmalloc_helpers_tags(struct kunit *test)
1231 /* This test is intended for tag-based modes. */
1232 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1234 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1236 ptr = vmalloc(PAGE_SIZE);
1237 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1239 /* Check that the returned pointer is tagged. */
1240 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1241 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1243 /* Make sure exported vmalloc helpers handle tagged pointers. */
1244 KUNIT_ASSERT_TRUE(test, is_vmalloc_addr(ptr));
1245 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, vmalloc_to_page(ptr));
1247 #if !IS_MODULE(CONFIG_KASAN_KUNIT_TEST)
1251 /* Make sure vmalloc'ed memory permissions can be changed. */
1252 rv = set_memory_ro((unsigned long)ptr, 1);
1253 KUNIT_ASSERT_GE(test, rv, 0);
1254 rv = set_memory_rw((unsigned long)ptr, 1);
1255 KUNIT_ASSERT_GE(test, rv, 0);
1262 static void vmalloc_oob(struct kunit *test)
1264 char *v_ptr, *p_ptr;
1266 size_t size = PAGE_SIZE / 2 - KASAN_GRANULE_SIZE - 5;
1268 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1270 v_ptr = vmalloc(size);
1271 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1273 OPTIMIZER_HIDE_VAR(v_ptr);
1276 * We have to be careful not to hit the guard page in vmalloc tests.
1277 * The MMU will catch that and crash us.
1280 /* Make sure in-bounds accesses are valid. */
1282 v_ptr[size - 1] = 0;
1285 * An unaligned access past the requested vmalloc size.
1286 * Only generic KASAN can precisely detect these.
1288 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
1289 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)v_ptr)[size]);
1291 /* An aligned access into the first out-of-bounds granule. */
1292 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)v_ptr)[size + 5]);
1294 /* Check that in-bounds accesses to the physical page are valid. */
1295 page = vmalloc_to_page(v_ptr);
1296 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, page);
1297 p_ptr = page_address(page);
1298 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_ptr);
1304 * We can't check for use-after-unmap bugs in this nor in the following
1305 * vmalloc tests, as the page might be fully unmapped and accessing it
1306 * will crash the kernel.
1310 static void vmap_tags(struct kunit *test)
1312 char *p_ptr, *v_ptr;
1313 struct page *p_page, *v_page;
1316 * This test is specifically crafted for the software tag-based mode,
1317 * the only tag-based mode that poisons vmap mappings.
1319 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_SW_TAGS);
1321 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1323 p_page = alloc_pages(GFP_KERNEL, 1);
1324 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_page);
1325 p_ptr = page_address(p_page);
1326 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_ptr);
1328 v_ptr = vmap(&p_page, 1, VM_MAP, PAGE_KERNEL);
1329 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1332 * We can't check for out-of-bounds bugs in this nor in the following
1333 * vmalloc tests, as allocations have page granularity and accessing
1334 * the guard page will crash the kernel.
1337 KUNIT_EXPECT_GE(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_MIN);
1338 KUNIT_EXPECT_LT(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_KERNEL);
1340 /* Make sure that in-bounds accesses through both pointers work. */
1344 /* Make sure vmalloc_to_page() correctly recovers the page pointer. */
1345 v_page = vmalloc_to_page(v_ptr);
1346 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_page);
1347 KUNIT_EXPECT_PTR_EQ(test, p_page, v_page);
1350 free_pages((unsigned long)p_ptr, 1);
1353 static void vm_map_ram_tags(struct kunit *test)
1355 char *p_ptr, *v_ptr;
1359 * This test is specifically crafted for the software tag-based mode,
1360 * the only tag-based mode that poisons vm_map_ram mappings.
1362 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_SW_TAGS);
1364 page = alloc_pages(GFP_KERNEL, 1);
1365 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, page);
1366 p_ptr = page_address(page);
1367 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_ptr);
1369 v_ptr = vm_map_ram(&page, 1, -1);
1370 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1372 KUNIT_EXPECT_GE(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_MIN);
1373 KUNIT_EXPECT_LT(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_KERNEL);
1375 /* Make sure that in-bounds accesses through both pointers work. */
1379 vm_unmap_ram(v_ptr, 1);
1380 free_pages((unsigned long)p_ptr, 1);
1383 static void vmalloc_percpu(struct kunit *test)
1389 * This test is specifically crafted for the software tag-based mode,
1390 * the only tag-based mode that poisons percpu mappings.
1392 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_SW_TAGS);
1394 ptr = __alloc_percpu(PAGE_SIZE, PAGE_SIZE);
1396 for_each_possible_cpu(cpu) {
1397 char *c_ptr = per_cpu_ptr(ptr, cpu);
1399 KUNIT_EXPECT_GE(test, (u8)get_tag(c_ptr), (u8)KASAN_TAG_MIN);
1400 KUNIT_EXPECT_LT(test, (u8)get_tag(c_ptr), (u8)KASAN_TAG_KERNEL);
1402 /* Make sure that in-bounds accesses don't crash the kernel. */
1410 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
1411 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
1414 static void match_all_not_assigned(struct kunit *test)
1420 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1422 for (i = 0; i < 256; i++) {
1423 size = get_random_u32_inclusive(1, 1024);
1424 ptr = kmalloc(size, GFP_KERNEL);
1425 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1426 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1427 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1431 for (i = 0; i < 256; i++) {
1432 order = get_random_u32_inclusive(1, 4);
1433 pages = alloc_pages(GFP_KERNEL, order);
1434 ptr = page_address(pages);
1435 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1436 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1437 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1438 free_pages((unsigned long)ptr, order);
1441 if (!IS_ENABLED(CONFIG_KASAN_VMALLOC))
1444 for (i = 0; i < 256; i++) {
1445 size = get_random_u32_inclusive(1, 1024);
1446 ptr = vmalloc(size);
1447 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1448 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1449 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1454 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1455 static void match_all_ptr_tag(struct kunit *test)
1460 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1462 ptr = kmalloc(128, GFP_KERNEL);
1463 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1465 /* Backup the assigned tag. */
1467 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1469 /* Reset the tag to 0xff.*/
1470 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1472 /* This access shouldn't trigger a KASAN report. */
1475 /* Recover the pointer tag and free. */
1476 ptr = set_tag(ptr, tag);
1480 /* Check that there are no match-all memory tags for tag-based modes. */
1481 static void match_all_mem_tag(struct kunit *test)
1486 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1488 ptr = kmalloc(128, GFP_KERNEL);
1489 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1490 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1492 /* For each possible tag value not matching the pointer tag. */
1493 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1494 if (tag == get_tag(ptr))
1497 /* Mark the first memory granule with the chosen memory tag. */
1498 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1500 /* This access must cause a KASAN report. */
1501 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1504 /* Recover the memory tag and free. */
1505 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1509 static struct kunit_case kasan_kunit_test_cases[] = {
1510 KUNIT_CASE(kmalloc_oob_right),
1511 KUNIT_CASE(kmalloc_oob_left),
1512 KUNIT_CASE(kmalloc_node_oob_right),
1513 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1514 KUNIT_CASE(kmalloc_pagealloc_uaf),
1515 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1516 KUNIT_CASE(pagealloc_oob_right),
1517 KUNIT_CASE(pagealloc_uaf),
1518 KUNIT_CASE(kmalloc_large_oob_right),
1519 KUNIT_CASE(krealloc_more_oob),
1520 KUNIT_CASE(krealloc_less_oob),
1521 KUNIT_CASE(krealloc_pagealloc_more_oob),
1522 KUNIT_CASE(krealloc_pagealloc_less_oob),
1523 KUNIT_CASE(krealloc_uaf),
1524 KUNIT_CASE(kmalloc_oob_16),
1525 KUNIT_CASE(kmalloc_uaf_16),
1526 KUNIT_CASE(kmalloc_oob_in_memset),
1527 KUNIT_CASE(kmalloc_oob_memset_2),
1528 KUNIT_CASE(kmalloc_oob_memset_4),
1529 KUNIT_CASE(kmalloc_oob_memset_8),
1530 KUNIT_CASE(kmalloc_oob_memset_16),
1531 KUNIT_CASE(kmalloc_memmove_negative_size),
1532 KUNIT_CASE(kmalloc_memmove_invalid_size),
1533 KUNIT_CASE(kmalloc_uaf),
1534 KUNIT_CASE(kmalloc_uaf_memset),
1535 KUNIT_CASE(kmalloc_uaf2),
1536 KUNIT_CASE(kmalloc_uaf3),
1537 KUNIT_CASE(kfree_via_page),
1538 KUNIT_CASE(kfree_via_phys),
1539 KUNIT_CASE(kmem_cache_oob),
1540 KUNIT_CASE(kmem_cache_accounted),
1541 KUNIT_CASE(kmem_cache_bulk),
1542 KUNIT_CASE(kasan_global_oob_right),
1543 KUNIT_CASE(kasan_global_oob_left),
1544 KUNIT_CASE(kasan_stack_oob),
1545 KUNIT_CASE(kasan_alloca_oob_left),
1546 KUNIT_CASE(kasan_alloca_oob_right),
1547 KUNIT_CASE(ksize_unpoisons_memory),
1548 KUNIT_CASE(ksize_uaf),
1549 KUNIT_CASE(kmem_cache_double_free),
1550 KUNIT_CASE(kmem_cache_invalid_free),
1551 KUNIT_CASE(kmem_cache_double_destroy),
1552 KUNIT_CASE(kasan_memchr),
1553 KUNIT_CASE(kasan_memcmp),
1554 KUNIT_CASE(kasan_strings),
1555 KUNIT_CASE(kasan_bitops_generic),
1556 KUNIT_CASE(kasan_bitops_tags),
1557 KUNIT_CASE(kmalloc_double_kzfree),
1558 KUNIT_CASE(rcu_uaf),
1559 KUNIT_CASE(workqueue_uaf),
1560 KUNIT_CASE(vmalloc_helpers_tags),
1561 KUNIT_CASE(vmalloc_oob),
1562 KUNIT_CASE(vmap_tags),
1563 KUNIT_CASE(vm_map_ram_tags),
1564 KUNIT_CASE(vmalloc_percpu),
1565 KUNIT_CASE(match_all_not_assigned),
1566 KUNIT_CASE(match_all_ptr_tag),
1567 KUNIT_CASE(match_all_mem_tag),
1571 static struct kunit_suite kasan_kunit_test_suite = {
1573 .test_cases = kasan_kunit_test_cases,
1574 .exit = kasan_test_exit,
1575 .suite_init = kasan_suite_init,
1576 .suite_exit = kasan_suite_exit,
1579 kunit_test_suite(kasan_kunit_test_suite);
1581 MODULE_LICENSE("GPL");