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 fail_data.report_expected = false;
59 kunit_add_named_resource(test, NULL, NULL, &resource,
60 "kasan_data", &fail_data);
64 static void kasan_test_exit(struct kunit *test)
66 kasan_set_tagging_report_once(true);
67 kasan_restore_multi_shot(multishot);
68 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
72 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
73 * KASAN report; causes a test failure otherwise. This relies on a KUnit
74 * resource named "kasan_data". Do not use this name for KUnit resources
75 * outside of KASAN tests.
77 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
78 * checking is auto-disabled. When this happens, this test handler reenables
79 * tag checking. As tag checking can be only disabled or enabled per CPU,
80 * this handler disables migration (preemption).
82 * Since the compiler doesn't see that the expression can change the fail_data
83 * fields, it can reorder or optimize away the accesses to those fields.
84 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
85 * expression to prevent that.
87 * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
88 * false. This allows detecting KASAN reports that happen outside of the checks
89 * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
90 * and in kasan_test_exit.
92 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
93 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
94 !kasan_async_mode_enabled()) \
96 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
97 WRITE_ONCE(fail_data.report_expected, true); \
101 KUNIT_EXPECT_EQ(test, \
102 READ_ONCE(fail_data.report_expected), \
103 READ_ONCE(fail_data.report_found)); \
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); \
110 WRITE_ONCE(fail_data.report_expected, false); \
113 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
114 if (!IS_ENABLED(config)) { \
115 kunit_info((test), "skipping, " #config " required"); \
120 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
121 if (IS_ENABLED(config)) { \
122 kunit_info((test), "skipping, " #config " enabled"); \
127 static void kmalloc_oob_right(struct kunit *test)
132 ptr = kmalloc(size, GFP_KERNEL);
133 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
135 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
139 static void kmalloc_oob_left(struct kunit *test)
144 ptr = kmalloc(size, GFP_KERNEL);
145 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
147 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
151 static void kmalloc_node_oob_right(struct kunit *test)
156 ptr = kmalloc_node(size, GFP_KERNEL, 0);
157 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
159 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
164 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
165 * fit into a slab cache and therefore is allocated via the page allocator
166 * fallback. Since this kind of fallback is only implemented for SLUB, these
167 * tests are limited to that allocator.
169 static void kmalloc_pagealloc_oob_right(struct kunit *test)
172 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
174 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
176 ptr = kmalloc(size, GFP_KERNEL);
177 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
179 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
184 static void kmalloc_pagealloc_uaf(struct kunit *test)
187 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
189 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
191 ptr = kmalloc(size, GFP_KERNEL);
192 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
195 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
198 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
201 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
203 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
205 ptr = kmalloc(size, GFP_KERNEL);
206 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
208 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
211 static void pagealloc_oob_right(struct kunit *test)
216 size_t size = (1UL << (PAGE_SHIFT + order));
219 * With generic KASAN page allocations have no redzones, thus
220 * out-of-bounds detection is not guaranteed.
221 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
223 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
225 pages = alloc_pages(GFP_KERNEL, order);
226 ptr = page_address(pages);
227 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
229 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
230 free_pages((unsigned long)ptr, order);
233 static void pagealloc_uaf(struct kunit *test)
239 pages = alloc_pages(GFP_KERNEL, order);
240 ptr = page_address(pages);
241 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
242 free_pages((unsigned long)ptr, order);
244 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
247 static void kmalloc_large_oob_right(struct kunit *test)
250 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
253 * Allocate a chunk that is large enough, but still fits into a slab
254 * and does not trigger the page allocator fallback in SLUB.
256 ptr = kmalloc(size, GFP_KERNEL);
257 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
259 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
263 static void krealloc_more_oob_helper(struct kunit *test,
264 size_t size1, size_t size2)
269 KUNIT_ASSERT_LT(test, size1, size2);
270 middle = size1 + (size2 - size1) / 2;
272 ptr1 = kmalloc(size1, GFP_KERNEL);
273 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
275 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
276 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
278 /* All offsets up to size2 must be accessible. */
279 ptr2[size1 - 1] = 'x';
282 ptr2[size2 - 1] = 'x';
284 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
285 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
286 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
288 /* For all modes first aligned offset after size2 must be inaccessible. */
289 KUNIT_EXPECT_KASAN_FAIL(test,
290 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
295 static void krealloc_less_oob_helper(struct kunit *test,
296 size_t size1, size_t size2)
301 KUNIT_ASSERT_LT(test, size2, size1);
302 middle = size2 + (size1 - size2) / 2;
304 ptr1 = kmalloc(size1, GFP_KERNEL);
305 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
307 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
308 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
310 /* Must be accessible for all modes. */
311 ptr2[size2 - 1] = 'x';
313 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
314 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
315 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
317 /* For all modes first aligned offset after size2 must be inaccessible. */
318 KUNIT_EXPECT_KASAN_FAIL(test,
319 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
322 * For all modes all size2, middle, and size1 should land in separate
323 * granules and thus the latter two offsets should be inaccessible.
325 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
326 round_down(middle, KASAN_GRANULE_SIZE));
327 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
328 round_down(size1, KASAN_GRANULE_SIZE));
329 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
330 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
331 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
336 static void krealloc_more_oob(struct kunit *test)
338 krealloc_more_oob_helper(test, 201, 235);
341 static void krealloc_less_oob(struct kunit *test)
343 krealloc_less_oob_helper(test, 235, 201);
346 static void krealloc_pagealloc_more_oob(struct kunit *test)
348 /* page_alloc fallback in only implemented for SLUB. */
349 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
351 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
352 KMALLOC_MAX_CACHE_SIZE + 235);
355 static void krealloc_pagealloc_less_oob(struct kunit *test)
357 /* page_alloc fallback in only implemented for SLUB. */
358 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
360 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
361 KMALLOC_MAX_CACHE_SIZE + 201);
365 * Check that krealloc() detects a use-after-free, returns NULL,
366 * and doesn't unpoison the freed object.
368 static void krealloc_uaf(struct kunit *test)
374 ptr1 = kmalloc(size1, GFP_KERNEL);
375 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
378 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
379 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
380 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
383 static void kmalloc_oob_16(struct kunit *test)
389 /* This test is specifically crafted for the generic mode. */
390 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
392 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
393 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
395 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
396 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
398 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
403 static void kmalloc_uaf_16(struct kunit *test)
409 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
410 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
412 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
413 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
416 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
420 static void kmalloc_oob_memset_2(struct kunit *test)
425 ptr = kmalloc(size, GFP_KERNEL);
426 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
428 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
432 static void kmalloc_oob_memset_4(struct kunit *test)
437 ptr = kmalloc(size, GFP_KERNEL);
438 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
440 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
445 static void kmalloc_oob_memset_8(struct kunit *test)
450 ptr = kmalloc(size, GFP_KERNEL);
451 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
453 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
457 static void kmalloc_oob_memset_16(struct kunit *test)
462 ptr = kmalloc(size, GFP_KERNEL);
463 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
465 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
469 static void kmalloc_oob_in_memset(struct kunit *test)
474 ptr = kmalloc(size, GFP_KERNEL);
475 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
477 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
481 static void kmalloc_memmove_invalid_size(struct kunit *test)
485 volatile size_t invalid_size = -2;
487 ptr = kmalloc(size, GFP_KERNEL);
488 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
490 memset((char *)ptr, 0, 64);
492 KUNIT_EXPECT_KASAN_FAIL(test,
493 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
497 static void kmalloc_uaf(struct kunit *test)
502 ptr = kmalloc(size, GFP_KERNEL);
503 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
506 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
509 static void kmalloc_uaf_memset(struct kunit *test)
514 ptr = kmalloc(size, GFP_KERNEL);
515 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
518 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
521 static void kmalloc_uaf2(struct kunit *test)
528 ptr1 = kmalloc(size, GFP_KERNEL);
529 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
533 ptr2 = kmalloc(size, GFP_KERNEL);
534 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
537 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
538 * Allow up to 16 attempts at generating different tags.
540 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
545 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
546 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
551 static void kfree_via_page(struct kunit *test)
556 unsigned long offset;
558 ptr = kmalloc(size, GFP_KERNEL);
559 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
561 page = virt_to_page(ptr);
562 offset = offset_in_page(ptr);
563 kfree(page_address(page) + offset);
566 static void kfree_via_phys(struct kunit *test)
572 ptr = kmalloc(size, GFP_KERNEL);
573 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
575 phys = virt_to_phys(ptr);
576 kfree(phys_to_virt(phys));
579 static void kmem_cache_oob(struct kunit *test)
583 struct kmem_cache *cache;
585 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
586 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
588 p = kmem_cache_alloc(cache, GFP_KERNEL);
590 kunit_err(test, "Allocation failed: %s\n", __func__);
591 kmem_cache_destroy(cache);
595 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
597 kmem_cache_free(cache, p);
598 kmem_cache_destroy(cache);
601 static void kmem_cache_accounted(struct kunit *test)
606 struct kmem_cache *cache;
608 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
609 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
612 * Several allocations with a delay to allow for lazy per memcg kmem
615 for (i = 0; i < 5; i++) {
616 p = kmem_cache_alloc(cache, GFP_KERNEL);
620 kmem_cache_free(cache, p);
625 kmem_cache_destroy(cache);
628 static void kmem_cache_bulk(struct kunit *test)
630 struct kmem_cache *cache;
636 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
637 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
639 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
641 kunit_err(test, "Allocation failed: %s\n", __func__);
642 kmem_cache_destroy(cache);
646 for (i = 0; i < ARRAY_SIZE(p); i++)
647 p[i][0] = p[i][size - 1] = 42;
649 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
650 kmem_cache_destroy(cache);
653 static char global_array[10];
655 static void kasan_global_oob(struct kunit *test)
658 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
659 * from failing here and panicing the kernel, access the array via a
660 * volatile pointer, which will prevent the compiler from being able to
661 * determine the array bounds.
663 * This access uses a volatile pointer to char (char *volatile) rather
664 * than the more conventional pointer to volatile char (volatile char *)
665 * because we want to prevent the compiler from making inferences about
666 * the pointer itself (i.e. its array bounds), not the data that it
669 char *volatile array = global_array;
670 char *p = &array[ARRAY_SIZE(global_array) + 3];
672 /* Only generic mode instruments globals. */
673 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
675 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
678 /* Check that ksize() makes the whole object accessible. */
679 static void ksize_unpoisons_memory(struct kunit *test)
682 size_t size = 123, real_size;
684 ptr = kmalloc(size, GFP_KERNEL);
685 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
686 real_size = ksize(ptr);
688 /* This access shouldn't trigger a KASAN report. */
692 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
698 * Check that a use-after-free is detected by ksize() and via normal accesses
701 static void ksize_uaf(struct kunit *test)
704 int size = 128 - KASAN_GRANULE_SIZE;
706 ptr = kmalloc(size, GFP_KERNEL);
707 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
710 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
711 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
712 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
715 static void kasan_stack_oob(struct kunit *test)
717 char stack_array[10];
718 /* See comment in kasan_global_oob. */
719 char *volatile array = stack_array;
720 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
722 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
724 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
727 static void kasan_alloca_oob_left(struct kunit *test)
730 char alloca_array[i];
731 /* See comment in kasan_global_oob. */
732 char *volatile array = alloca_array;
735 /* Only generic mode instruments dynamic allocas. */
736 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
737 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
739 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
742 static void kasan_alloca_oob_right(struct kunit *test)
745 char alloca_array[i];
746 /* See comment in kasan_global_oob. */
747 char *volatile array = alloca_array;
750 /* Only generic mode instruments dynamic allocas. */
751 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
752 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
754 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
757 static void kmem_cache_double_free(struct kunit *test)
761 struct kmem_cache *cache;
763 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
764 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
766 p = kmem_cache_alloc(cache, GFP_KERNEL);
768 kunit_err(test, "Allocation failed: %s\n", __func__);
769 kmem_cache_destroy(cache);
773 kmem_cache_free(cache, p);
774 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
775 kmem_cache_destroy(cache);
778 static void kmem_cache_invalid_free(struct kunit *test)
782 struct kmem_cache *cache;
784 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
786 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
788 p = kmem_cache_alloc(cache, GFP_KERNEL);
790 kunit_err(test, "Allocation failed: %s\n", __func__);
791 kmem_cache_destroy(cache);
795 /* Trigger invalid free, the object doesn't get freed. */
796 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
799 * Properly free the object to prevent the "Objects remaining in
800 * test_cache on __kmem_cache_shutdown" BUG failure.
802 kmem_cache_free(cache, p);
804 kmem_cache_destroy(cache);
807 static void kasan_memchr(struct kunit *test)
813 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
814 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
816 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
819 size = round_up(size, OOB_TAG_OFF);
821 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
822 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
824 KUNIT_EXPECT_KASAN_FAIL(test,
825 kasan_ptr_result = memchr(ptr, '1', size + 1));
830 static void kasan_memcmp(struct kunit *test)
837 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
838 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
840 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
843 size = round_up(size, OOB_TAG_OFF);
845 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
846 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
847 memset(arr, 0, sizeof(arr));
849 KUNIT_EXPECT_KASAN_FAIL(test,
850 kasan_int_result = memcmp(ptr, arr, size+1));
854 static void kasan_strings(struct kunit *test)
860 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
861 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
863 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
865 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
866 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
871 * Try to cause only 1 invalid access (less spam in dmesg).
872 * For that we need ptr to point to zeroed byte.
873 * Skip metadata that could be stored in freed object so ptr
874 * will likely point to zeroed byte.
877 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
879 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
881 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
883 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
885 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
887 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
890 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
892 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
893 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
894 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
895 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
896 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
897 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
898 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
899 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
902 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
904 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
905 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
906 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
907 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
908 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
909 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
910 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
911 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
913 #if defined(clear_bit_unlock_is_negative_byte)
914 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
915 clear_bit_unlock_is_negative_byte(nr, addr));
919 static void kasan_bitops_generic(struct kunit *test)
923 /* This test is specifically crafted for the generic mode. */
924 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
927 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
928 * this way we do not actually corrupt other memory.
930 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
931 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
934 * Below calls try to access bit within allocated memory; however, the
935 * below accesses are still out-of-bounds, since bitops are defined to
936 * operate on the whole long the bit is in.
938 kasan_bitops_modify(test, BITS_PER_LONG, bits);
941 * Below calls try to access bit beyond allocated memory.
943 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
948 static void kasan_bitops_tags(struct kunit *test)
952 /* This test is specifically crafted for tag-based modes. */
953 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
955 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
956 bits = kzalloc(48, GFP_KERNEL);
957 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
959 /* Do the accesses past the 48 allocated bytes, but within the redone. */
960 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
961 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
966 static void kmalloc_double_kzfree(struct kunit *test)
971 ptr = kmalloc(size, GFP_KERNEL);
972 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
974 kfree_sensitive(ptr);
975 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
978 static void vmalloc_oob(struct kunit *test)
982 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
985 * We have to be careful not to hit the guard page.
986 * The MMU will catch that and crash us.
988 area = vmalloc(3000);
989 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
991 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
996 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
997 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
1000 static void match_all_not_assigned(struct kunit *test)
1006 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1008 for (i = 0; i < 256; i++) {
1009 size = (get_random_int() % 1024) + 1;
1010 ptr = kmalloc(size, GFP_KERNEL);
1011 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1012 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1013 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1017 for (i = 0; i < 256; i++) {
1018 order = (get_random_int() % 4) + 1;
1019 pages = alloc_pages(GFP_KERNEL, order);
1020 ptr = page_address(pages);
1021 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1022 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1023 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1024 free_pages((unsigned long)ptr, order);
1028 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1029 static void match_all_ptr_tag(struct kunit *test)
1034 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1036 ptr = kmalloc(128, GFP_KERNEL);
1037 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1039 /* Backup the assigned tag. */
1041 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1043 /* Reset the tag to 0xff.*/
1044 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1046 /* This access shouldn't trigger a KASAN report. */
1049 /* Recover the pointer tag and free. */
1050 ptr = set_tag(ptr, tag);
1054 /* Check that there are no match-all memory tags for tag-based modes. */
1055 static void match_all_mem_tag(struct kunit *test)
1060 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1062 ptr = kmalloc(128, GFP_KERNEL);
1063 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1064 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1066 /* For each possible tag value not matching the pointer tag. */
1067 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1068 if (tag == get_tag(ptr))
1071 /* Mark the first memory granule with the chosen memory tag. */
1072 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1074 /* This access must cause a KASAN report. */
1075 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1078 /* Recover the memory tag and free. */
1079 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1083 static struct kunit_case kasan_kunit_test_cases[] = {
1084 KUNIT_CASE(kmalloc_oob_right),
1085 KUNIT_CASE(kmalloc_oob_left),
1086 KUNIT_CASE(kmalloc_node_oob_right),
1087 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1088 KUNIT_CASE(kmalloc_pagealloc_uaf),
1089 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1090 KUNIT_CASE(pagealloc_oob_right),
1091 KUNIT_CASE(pagealloc_uaf),
1092 KUNIT_CASE(kmalloc_large_oob_right),
1093 KUNIT_CASE(krealloc_more_oob),
1094 KUNIT_CASE(krealloc_less_oob),
1095 KUNIT_CASE(krealloc_pagealloc_more_oob),
1096 KUNIT_CASE(krealloc_pagealloc_less_oob),
1097 KUNIT_CASE(krealloc_uaf),
1098 KUNIT_CASE(kmalloc_oob_16),
1099 KUNIT_CASE(kmalloc_uaf_16),
1100 KUNIT_CASE(kmalloc_oob_in_memset),
1101 KUNIT_CASE(kmalloc_oob_memset_2),
1102 KUNIT_CASE(kmalloc_oob_memset_4),
1103 KUNIT_CASE(kmalloc_oob_memset_8),
1104 KUNIT_CASE(kmalloc_oob_memset_16),
1105 KUNIT_CASE(kmalloc_memmove_invalid_size),
1106 KUNIT_CASE(kmalloc_uaf),
1107 KUNIT_CASE(kmalloc_uaf_memset),
1108 KUNIT_CASE(kmalloc_uaf2),
1109 KUNIT_CASE(kfree_via_page),
1110 KUNIT_CASE(kfree_via_phys),
1111 KUNIT_CASE(kmem_cache_oob),
1112 KUNIT_CASE(kmem_cache_accounted),
1113 KUNIT_CASE(kmem_cache_bulk),
1114 KUNIT_CASE(kasan_global_oob),
1115 KUNIT_CASE(kasan_stack_oob),
1116 KUNIT_CASE(kasan_alloca_oob_left),
1117 KUNIT_CASE(kasan_alloca_oob_right),
1118 KUNIT_CASE(ksize_unpoisons_memory),
1119 KUNIT_CASE(ksize_uaf),
1120 KUNIT_CASE(kmem_cache_double_free),
1121 KUNIT_CASE(kmem_cache_invalid_free),
1122 KUNIT_CASE(kasan_memchr),
1123 KUNIT_CASE(kasan_memcmp),
1124 KUNIT_CASE(kasan_strings),
1125 KUNIT_CASE(kasan_bitops_generic),
1126 KUNIT_CASE(kasan_bitops_tags),
1127 KUNIT_CASE(kmalloc_double_kzfree),
1128 KUNIT_CASE(vmalloc_oob),
1129 KUNIT_CASE(match_all_not_assigned),
1130 KUNIT_CASE(match_all_ptr_tag),
1131 KUNIT_CASE(match_all_mem_tag),
1135 static struct kunit_suite kasan_kunit_test_suite = {
1137 .init = kasan_test_init,
1138 .test_cases = kasan_kunit_test_cases,
1139 .exit = kasan_test_exit,
1142 kunit_test_suite(kasan_kunit_test_suite);
1144 MODULE_LICENSE("GPL");