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);
60 static void kasan_test_exit(struct kunit *test)
62 kasan_set_tagging_report_once(true);
63 kasan_restore_multi_shot(multishot);
67 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
68 * KASAN report; causes a test failure otherwise. This relies on a KUnit
69 * resource named "kasan_data". Do not use this name for KUnit resources
70 * outside of KASAN tests.
72 * For hardware tag-based KASAN, when a tag fault happens, tag checking is
73 * normally auto-disabled. When this happens, this test handler reenables
74 * tag checking. As tag checking can be only disabled or enabled per CPU, this
75 * handler disables migration (preemption).
77 * Since the compiler doesn't see that the expression can change the fail_data
78 * fields, it can reorder or optimize away the accesses to those fields.
79 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
80 * expression to prevent that.
82 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
83 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
85 WRITE_ONCE(fail_data.report_expected, true); \
86 WRITE_ONCE(fail_data.report_found, false); \
87 kunit_add_named_resource(test, \
91 "kasan_data", &fail_data); \
95 KUNIT_EXPECT_EQ(test, \
96 READ_ONCE(fail_data.report_expected), \
97 READ_ONCE(fail_data.report_found)); \
98 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
99 if (READ_ONCE(fail_data.report_found)) \
100 kasan_enable_tagging(); \
105 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
106 if (!IS_ENABLED(config)) { \
107 kunit_info((test), "skipping, " #config " required"); \
112 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
113 if (IS_ENABLED(config)) { \
114 kunit_info((test), "skipping, " #config " enabled"); \
119 static void kmalloc_oob_right(struct kunit *test)
124 ptr = kmalloc(size, GFP_KERNEL);
125 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
127 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
131 static void kmalloc_oob_left(struct kunit *test)
136 ptr = kmalloc(size, GFP_KERNEL);
137 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
139 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
143 static void kmalloc_node_oob_right(struct kunit *test)
148 ptr = kmalloc_node(size, GFP_KERNEL, 0);
149 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
151 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
156 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
157 * fit into a slab cache and therefore is allocated via the page allocator
158 * fallback. Since this kind of fallback is only implemented for SLUB, these
159 * tests are limited to that allocator.
161 static void kmalloc_pagealloc_oob_right(struct kunit *test)
164 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
166 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
168 ptr = kmalloc(size, GFP_KERNEL);
169 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
171 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
176 static void kmalloc_pagealloc_uaf(struct kunit *test)
179 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
181 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
183 ptr = kmalloc(size, GFP_KERNEL);
184 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
187 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
190 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
193 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
195 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
197 ptr = kmalloc(size, GFP_KERNEL);
198 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
200 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
203 static void pagealloc_oob_right(struct kunit *test)
208 size_t size = (1UL << (PAGE_SHIFT + order));
211 * With generic KASAN page allocations have no redzones, thus
212 * out-of-bounds detection is not guaranteed.
213 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
215 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
217 pages = alloc_pages(GFP_KERNEL, order);
218 ptr = page_address(pages);
219 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
221 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
222 free_pages((unsigned long)ptr, order);
225 static void pagealloc_uaf(struct kunit *test)
231 pages = alloc_pages(GFP_KERNEL, order);
232 ptr = page_address(pages);
233 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
234 free_pages((unsigned long)ptr, order);
236 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
239 static void kmalloc_large_oob_right(struct kunit *test)
242 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
245 * Allocate a chunk that is large enough, but still fits into a slab
246 * and does not trigger the page allocator fallback in SLUB.
248 ptr = kmalloc(size, GFP_KERNEL);
249 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
251 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
255 static void krealloc_more_oob_helper(struct kunit *test,
256 size_t size1, size_t size2)
261 KUNIT_ASSERT_LT(test, size1, size2);
262 middle = size1 + (size2 - size1) / 2;
264 ptr1 = kmalloc(size1, GFP_KERNEL);
265 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
267 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
268 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
270 /* All offsets up to size2 must be accessible. */
271 ptr2[size1 - 1] = 'x';
274 ptr2[size2 - 1] = 'x';
276 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
277 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
278 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
280 /* For all modes first aligned offset after size2 must be inaccessible. */
281 KUNIT_EXPECT_KASAN_FAIL(test,
282 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
287 static void krealloc_less_oob_helper(struct kunit *test,
288 size_t size1, size_t size2)
293 KUNIT_ASSERT_LT(test, size2, size1);
294 middle = size2 + (size1 - size2) / 2;
296 ptr1 = kmalloc(size1, GFP_KERNEL);
297 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
299 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
302 /* Must be accessible for all modes. */
303 ptr2[size2 - 1] = 'x';
305 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
306 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
307 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
309 /* For all modes first aligned offset after size2 must be inaccessible. */
310 KUNIT_EXPECT_KASAN_FAIL(test,
311 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
314 * For all modes all size2, middle, and size1 should land in separate
315 * granules and thus the latter two offsets should be inaccessible.
317 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
318 round_down(middle, KASAN_GRANULE_SIZE));
319 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
320 round_down(size1, KASAN_GRANULE_SIZE));
321 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
322 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
323 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
328 static void krealloc_more_oob(struct kunit *test)
330 krealloc_more_oob_helper(test, 201, 235);
333 static void krealloc_less_oob(struct kunit *test)
335 krealloc_less_oob_helper(test, 235, 201);
338 static void krealloc_pagealloc_more_oob(struct kunit *test)
340 /* page_alloc fallback in only implemented for SLUB. */
341 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
343 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
344 KMALLOC_MAX_CACHE_SIZE + 235);
347 static void krealloc_pagealloc_less_oob(struct kunit *test)
349 /* page_alloc fallback in only implemented for SLUB. */
350 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
352 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
353 KMALLOC_MAX_CACHE_SIZE + 201);
357 * Check that krealloc() detects a use-after-free, returns NULL,
358 * and doesn't unpoison the freed object.
360 static void krealloc_uaf(struct kunit *test)
366 ptr1 = kmalloc(size1, GFP_KERNEL);
367 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
370 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
371 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
372 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
375 static void kmalloc_oob_16(struct kunit *test)
381 /* This test is specifically crafted for the generic mode. */
382 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
384 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
385 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
387 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
388 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
390 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
395 static void kmalloc_uaf_16(struct kunit *test)
401 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
402 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
404 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
408 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
412 static void kmalloc_oob_memset_2(struct kunit *test)
417 ptr = kmalloc(size, GFP_KERNEL);
418 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
420 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
424 static void kmalloc_oob_memset_4(struct kunit *test)
429 ptr = kmalloc(size, GFP_KERNEL);
430 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
432 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
437 static void kmalloc_oob_memset_8(struct kunit *test)
442 ptr = kmalloc(size, GFP_KERNEL);
443 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
445 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
449 static void kmalloc_oob_memset_16(struct kunit *test)
454 ptr = kmalloc(size, GFP_KERNEL);
455 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
457 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
461 static void kmalloc_oob_in_memset(struct kunit *test)
466 ptr = kmalloc(size, GFP_KERNEL);
467 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
469 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
473 static void kmalloc_memmove_invalid_size(struct kunit *test)
477 volatile size_t invalid_size = -2;
479 ptr = kmalloc(size, GFP_KERNEL);
480 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
482 memset((char *)ptr, 0, 64);
484 KUNIT_EXPECT_KASAN_FAIL(test,
485 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
489 static void kmalloc_uaf(struct kunit *test)
494 ptr = kmalloc(size, GFP_KERNEL);
495 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
498 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
501 static void kmalloc_uaf_memset(struct kunit *test)
506 ptr = kmalloc(size, GFP_KERNEL);
507 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
510 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
513 static void kmalloc_uaf2(struct kunit *test)
520 ptr1 = kmalloc(size, GFP_KERNEL);
521 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
525 ptr2 = kmalloc(size, GFP_KERNEL);
526 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
529 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
530 * Allow up to 16 attempts at generating different tags.
532 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
537 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
538 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
543 static void kfree_via_page(struct kunit *test)
548 unsigned long offset;
550 ptr = kmalloc(size, GFP_KERNEL);
551 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
553 page = virt_to_page(ptr);
554 offset = offset_in_page(ptr);
555 kfree(page_address(page) + offset);
558 static void kfree_via_phys(struct kunit *test)
564 ptr = kmalloc(size, GFP_KERNEL);
565 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
567 phys = virt_to_phys(ptr);
568 kfree(phys_to_virt(phys));
571 static void kmem_cache_oob(struct kunit *test)
575 struct kmem_cache *cache;
577 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
578 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
580 p = kmem_cache_alloc(cache, GFP_KERNEL);
582 kunit_err(test, "Allocation failed: %s\n", __func__);
583 kmem_cache_destroy(cache);
587 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
589 kmem_cache_free(cache, p);
590 kmem_cache_destroy(cache);
593 static void kmem_cache_accounted(struct kunit *test)
598 struct kmem_cache *cache;
600 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
601 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
604 * Several allocations with a delay to allow for lazy per memcg kmem
607 for (i = 0; i < 5; i++) {
608 p = kmem_cache_alloc(cache, GFP_KERNEL);
612 kmem_cache_free(cache, p);
617 kmem_cache_destroy(cache);
620 static void kmem_cache_bulk(struct kunit *test)
622 struct kmem_cache *cache;
628 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
629 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
631 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
633 kunit_err(test, "Allocation failed: %s\n", __func__);
634 kmem_cache_destroy(cache);
638 for (i = 0; i < ARRAY_SIZE(p); i++)
639 p[i][0] = p[i][size - 1] = 42;
641 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
642 kmem_cache_destroy(cache);
645 static char global_array[10];
647 static void kasan_global_oob(struct kunit *test)
650 char *p = &global_array[ARRAY_SIZE(global_array) + i];
652 /* Only generic mode instruments globals. */
653 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
655 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
658 /* Check that ksize() makes the whole object accessible. */
659 static void ksize_unpoisons_memory(struct kunit *test)
662 size_t size = 123, real_size;
664 ptr = kmalloc(size, GFP_KERNEL);
665 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
666 real_size = ksize(ptr);
668 /* This access shouldn't trigger a KASAN report. */
672 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
678 * Check that a use-after-free is detected by ksize() and via normal accesses
681 static void ksize_uaf(struct kunit *test)
684 int size = 128 - KASAN_GRANULE_SIZE;
686 ptr = kmalloc(size, GFP_KERNEL);
687 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
690 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
691 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
692 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
695 static void kasan_stack_oob(struct kunit *test)
697 char stack_array[10];
698 volatile int i = OOB_TAG_OFF;
699 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
701 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
703 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
706 static void kasan_alloca_oob_left(struct kunit *test)
709 char alloca_array[i];
710 char *p = alloca_array - 1;
712 /* Only generic mode instruments dynamic allocas. */
713 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
714 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
716 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
719 static void kasan_alloca_oob_right(struct kunit *test)
722 char alloca_array[i];
723 char *p = alloca_array + i;
725 /* Only generic mode instruments dynamic allocas. */
726 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
727 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
729 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
732 static void kmem_cache_double_free(struct kunit *test)
736 struct kmem_cache *cache;
738 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
739 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
741 p = kmem_cache_alloc(cache, GFP_KERNEL);
743 kunit_err(test, "Allocation failed: %s\n", __func__);
744 kmem_cache_destroy(cache);
748 kmem_cache_free(cache, p);
749 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
750 kmem_cache_destroy(cache);
753 static void kmem_cache_invalid_free(struct kunit *test)
757 struct kmem_cache *cache;
759 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
761 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
763 p = kmem_cache_alloc(cache, GFP_KERNEL);
765 kunit_err(test, "Allocation failed: %s\n", __func__);
766 kmem_cache_destroy(cache);
770 /* Trigger invalid free, the object doesn't get freed. */
771 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
774 * Properly free the object to prevent the "Objects remaining in
775 * test_cache on __kmem_cache_shutdown" BUG failure.
777 kmem_cache_free(cache, p);
779 kmem_cache_destroy(cache);
782 static void kasan_memchr(struct kunit *test)
788 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
789 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
791 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
794 size = round_up(size, OOB_TAG_OFF);
796 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
797 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
799 KUNIT_EXPECT_KASAN_FAIL(test,
800 kasan_ptr_result = memchr(ptr, '1', size + 1));
805 static void kasan_memcmp(struct kunit *test)
812 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
813 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
815 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
818 size = round_up(size, OOB_TAG_OFF);
820 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
821 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
822 memset(arr, 0, sizeof(arr));
824 KUNIT_EXPECT_KASAN_FAIL(test,
825 kasan_int_result = memcmp(ptr, arr, size+1));
829 static void kasan_strings(struct kunit *test)
835 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
836 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
838 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
840 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
841 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
846 * Try to cause only 1 invalid access (less spam in dmesg).
847 * For that we need ptr to point to zeroed byte.
848 * Skip metadata that could be stored in freed object so ptr
849 * will likely point to zeroed byte.
852 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
854 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
856 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
858 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
860 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
862 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
865 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
867 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
868 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
869 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
870 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
871 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
872 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
873 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
874 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
877 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
879 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
880 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
881 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
882 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
883 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
884 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
885 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
886 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
888 #if defined(clear_bit_unlock_is_negative_byte)
889 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
890 clear_bit_unlock_is_negative_byte(nr, addr));
894 static void kasan_bitops_generic(struct kunit *test)
898 /* This test is specifically crafted for the generic mode. */
899 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
902 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
903 * this way we do not actually corrupt other memory.
905 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
906 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
909 * Below calls try to access bit within allocated memory; however, the
910 * below accesses are still out-of-bounds, since bitops are defined to
911 * operate on the whole long the bit is in.
913 kasan_bitops_modify(test, BITS_PER_LONG, bits);
916 * Below calls try to access bit beyond allocated memory.
918 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
923 static void kasan_bitops_tags(struct kunit *test)
927 /* This test is specifically crafted for tag-based modes. */
928 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
930 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
931 bits = kzalloc(48, GFP_KERNEL);
932 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
934 /* Do the accesses past the 48 allocated bytes, but within the redone. */
935 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
936 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
941 static void kmalloc_double_kzfree(struct kunit *test)
946 ptr = kmalloc(size, GFP_KERNEL);
947 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
949 kfree_sensitive(ptr);
950 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
953 static void vmalloc_oob(struct kunit *test)
957 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
960 * We have to be careful not to hit the guard page.
961 * The MMU will catch that and crash us.
963 area = vmalloc(3000);
964 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
966 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
971 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
972 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
975 static void match_all_not_assigned(struct kunit *test)
981 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
983 for (i = 0; i < 256; i++) {
984 size = (get_random_int() % 1024) + 1;
985 ptr = kmalloc(size, GFP_KERNEL);
986 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
987 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
988 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
992 for (i = 0; i < 256; i++) {
993 order = (get_random_int() % 4) + 1;
994 pages = alloc_pages(GFP_KERNEL, order);
995 ptr = page_address(pages);
996 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
997 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
998 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
999 free_pages((unsigned long)ptr, order);
1003 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1004 static void match_all_ptr_tag(struct kunit *test)
1009 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1011 ptr = kmalloc(128, GFP_KERNEL);
1012 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1014 /* Backup the assigned tag. */
1016 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1018 /* Reset the tag to 0xff.*/
1019 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1021 /* This access shouldn't trigger a KASAN report. */
1024 /* Recover the pointer tag and free. */
1025 ptr = set_tag(ptr, tag);
1029 /* Check that there are no match-all memory tags for tag-based modes. */
1030 static void match_all_mem_tag(struct kunit *test)
1035 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1037 ptr = kmalloc(128, GFP_KERNEL);
1038 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1039 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1041 /* For each possible tag value not matching the pointer tag. */
1042 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1043 if (tag == get_tag(ptr))
1046 /* Mark the first memory granule with the chosen memory tag. */
1047 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
1049 /* This access must cause a KASAN report. */
1050 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1053 /* Recover the memory tag and free. */
1054 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
1058 static struct kunit_case kasan_kunit_test_cases[] = {
1059 KUNIT_CASE(kmalloc_oob_right),
1060 KUNIT_CASE(kmalloc_oob_left),
1061 KUNIT_CASE(kmalloc_node_oob_right),
1062 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1063 KUNIT_CASE(kmalloc_pagealloc_uaf),
1064 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1065 KUNIT_CASE(pagealloc_oob_right),
1066 KUNIT_CASE(pagealloc_uaf),
1067 KUNIT_CASE(kmalloc_large_oob_right),
1068 KUNIT_CASE(krealloc_more_oob),
1069 KUNIT_CASE(krealloc_less_oob),
1070 KUNIT_CASE(krealloc_pagealloc_more_oob),
1071 KUNIT_CASE(krealloc_pagealloc_less_oob),
1072 KUNIT_CASE(krealloc_uaf),
1073 KUNIT_CASE(kmalloc_oob_16),
1074 KUNIT_CASE(kmalloc_uaf_16),
1075 KUNIT_CASE(kmalloc_oob_in_memset),
1076 KUNIT_CASE(kmalloc_oob_memset_2),
1077 KUNIT_CASE(kmalloc_oob_memset_4),
1078 KUNIT_CASE(kmalloc_oob_memset_8),
1079 KUNIT_CASE(kmalloc_oob_memset_16),
1080 KUNIT_CASE(kmalloc_memmove_invalid_size),
1081 KUNIT_CASE(kmalloc_uaf),
1082 KUNIT_CASE(kmalloc_uaf_memset),
1083 KUNIT_CASE(kmalloc_uaf2),
1084 KUNIT_CASE(kfree_via_page),
1085 KUNIT_CASE(kfree_via_phys),
1086 KUNIT_CASE(kmem_cache_oob),
1087 KUNIT_CASE(kmem_cache_accounted),
1088 KUNIT_CASE(kmem_cache_bulk),
1089 KUNIT_CASE(kasan_global_oob),
1090 KUNIT_CASE(kasan_stack_oob),
1091 KUNIT_CASE(kasan_alloca_oob_left),
1092 KUNIT_CASE(kasan_alloca_oob_right),
1093 KUNIT_CASE(ksize_unpoisons_memory),
1094 KUNIT_CASE(ksize_uaf),
1095 KUNIT_CASE(kmem_cache_double_free),
1096 KUNIT_CASE(kmem_cache_invalid_free),
1097 KUNIT_CASE(kasan_memchr),
1098 KUNIT_CASE(kasan_memcmp),
1099 KUNIT_CASE(kasan_strings),
1100 KUNIT_CASE(kasan_bitops_generic),
1101 KUNIT_CASE(kasan_bitops_tags),
1102 KUNIT_CASE(kmalloc_double_kzfree),
1103 KUNIT_CASE(vmalloc_oob),
1104 KUNIT_CASE(match_all_not_assigned),
1105 KUNIT_CASE(match_all_ptr_tag),
1106 KUNIT_CASE(match_all_mem_tag),
1110 static struct kunit_suite kasan_kunit_test_suite = {
1112 .init = kasan_test_init,
1113 .test_cases = kasan_kunit_test_cases,
1114 .exit = kasan_test_exit,
1117 kunit_test_suite(kasan_kunit_test_suite);
1119 MODULE_LICENSE("GPL");