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 char *p = &global_array[ARRAY_SIZE(global_array) + i];
660 /* Only generic mode instruments globals. */
661 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
663 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
666 /* Check that ksize() makes the whole object accessible. */
667 static void ksize_unpoisons_memory(struct kunit *test)
670 size_t size = 123, real_size;
672 ptr = kmalloc(size, GFP_KERNEL);
673 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
674 real_size = ksize(ptr);
676 /* This access shouldn't trigger a KASAN report. */
680 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
686 * Check that a use-after-free is detected by ksize() and via normal accesses
689 static void ksize_uaf(struct kunit *test)
692 int size = 128 - KASAN_GRANULE_SIZE;
694 ptr = kmalloc(size, GFP_KERNEL);
695 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
698 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
699 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
700 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
703 static void kasan_stack_oob(struct kunit *test)
705 char stack_array[10];
706 volatile int i = OOB_TAG_OFF;
707 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
709 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
711 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
714 static void kasan_alloca_oob_left(struct kunit *test)
717 char alloca_array[i];
718 char *p = alloca_array - 1;
720 /* Only generic mode instruments dynamic allocas. */
721 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
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_right(struct kunit *test)
730 char alloca_array[i];
731 char *p = alloca_array + i;
733 /* Only generic mode instruments dynamic allocas. */
734 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
735 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
737 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
740 static void kmem_cache_double_free(struct kunit *test)
744 struct kmem_cache *cache;
746 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
747 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
749 p = kmem_cache_alloc(cache, GFP_KERNEL);
751 kunit_err(test, "Allocation failed: %s\n", __func__);
752 kmem_cache_destroy(cache);
756 kmem_cache_free(cache, p);
757 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
758 kmem_cache_destroy(cache);
761 static void kmem_cache_invalid_free(struct kunit *test)
765 struct kmem_cache *cache;
767 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
769 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
771 p = kmem_cache_alloc(cache, GFP_KERNEL);
773 kunit_err(test, "Allocation failed: %s\n", __func__);
774 kmem_cache_destroy(cache);
778 /* Trigger invalid free, the object doesn't get freed. */
779 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
782 * Properly free the object to prevent the "Objects remaining in
783 * test_cache on __kmem_cache_shutdown" BUG failure.
785 kmem_cache_free(cache, p);
787 kmem_cache_destroy(cache);
790 static void kasan_memchr(struct kunit *test)
796 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
797 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
799 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
802 size = round_up(size, OOB_TAG_OFF);
804 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
805 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
807 KUNIT_EXPECT_KASAN_FAIL(test,
808 kasan_ptr_result = memchr(ptr, '1', size + 1));
813 static void kasan_memcmp(struct kunit *test)
820 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
821 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
823 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
826 size = round_up(size, OOB_TAG_OFF);
828 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
829 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
830 memset(arr, 0, sizeof(arr));
832 KUNIT_EXPECT_KASAN_FAIL(test,
833 kasan_int_result = memcmp(ptr, arr, size+1));
837 static void kasan_strings(struct kunit *test)
843 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
844 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
846 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
848 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
849 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
854 * Try to cause only 1 invalid access (less spam in dmesg).
855 * For that we need ptr to point to zeroed byte.
856 * Skip metadata that could be stored in freed object so ptr
857 * will likely point to zeroed byte.
860 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
862 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
864 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
866 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
868 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
870 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
873 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
875 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
876 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
877 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
878 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
879 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
880 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
881 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
882 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
885 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
887 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
888 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
889 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
890 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
891 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
892 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
893 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
894 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
896 #if defined(clear_bit_unlock_is_negative_byte)
897 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
898 clear_bit_unlock_is_negative_byte(nr, addr));
902 static void kasan_bitops_generic(struct kunit *test)
906 /* This test is specifically crafted for the generic mode. */
907 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
910 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
911 * this way we do not actually corrupt other memory.
913 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
914 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
917 * Below calls try to access bit within allocated memory; however, the
918 * below accesses are still out-of-bounds, since bitops are defined to
919 * operate on the whole long the bit is in.
921 kasan_bitops_modify(test, BITS_PER_LONG, bits);
924 * Below calls try to access bit beyond allocated memory.
926 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
931 static void kasan_bitops_tags(struct kunit *test)
935 /* This test is specifically crafted for tag-based modes. */
936 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
938 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
939 bits = kzalloc(48, GFP_KERNEL);
940 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
942 /* Do the accesses past the 48 allocated bytes, but within the redone. */
943 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
944 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
949 static void kmalloc_double_kzfree(struct kunit *test)
954 ptr = kmalloc(size, GFP_KERNEL);
955 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
957 kfree_sensitive(ptr);
958 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
961 static void vmalloc_oob(struct kunit *test)
965 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
968 * We have to be careful not to hit the guard page.
969 * The MMU will catch that and crash us.
971 area = vmalloc(3000);
972 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
974 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
979 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
980 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
983 static void match_all_not_assigned(struct kunit *test)
989 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
991 for (i = 0; i < 256; i++) {
992 size = (get_random_int() % 1024) + 1;
993 ptr = kmalloc(size, GFP_KERNEL);
994 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
995 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
996 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1000 for (i = 0; i < 256; i++) {
1001 order = (get_random_int() % 4) + 1;
1002 pages = alloc_pages(GFP_KERNEL, order);
1003 ptr = page_address(pages);
1004 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1005 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1006 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1007 free_pages((unsigned long)ptr, order);
1011 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1012 static void match_all_ptr_tag(struct kunit *test)
1017 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1019 ptr = kmalloc(128, GFP_KERNEL);
1020 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1022 /* Backup the assigned tag. */
1024 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1026 /* Reset the tag to 0xff.*/
1027 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1029 /* This access shouldn't trigger a KASAN report. */
1032 /* Recover the pointer tag and free. */
1033 ptr = set_tag(ptr, tag);
1037 /* Check that there are no match-all memory tags for tag-based modes. */
1038 static void match_all_mem_tag(struct kunit *test)
1043 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1045 ptr = kmalloc(128, GFP_KERNEL);
1046 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1047 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1049 /* For each possible tag value not matching the pointer tag. */
1050 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1051 if (tag == get_tag(ptr))
1054 /* Mark the first memory granule with the chosen memory tag. */
1055 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1057 /* This access must cause a KASAN report. */
1058 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1061 /* Recover the memory tag and free. */
1062 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1066 static struct kunit_case kasan_kunit_test_cases[] = {
1067 KUNIT_CASE(kmalloc_oob_right),
1068 KUNIT_CASE(kmalloc_oob_left),
1069 KUNIT_CASE(kmalloc_node_oob_right),
1070 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1071 KUNIT_CASE(kmalloc_pagealloc_uaf),
1072 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1073 KUNIT_CASE(pagealloc_oob_right),
1074 KUNIT_CASE(pagealloc_uaf),
1075 KUNIT_CASE(kmalloc_large_oob_right),
1076 KUNIT_CASE(krealloc_more_oob),
1077 KUNIT_CASE(krealloc_less_oob),
1078 KUNIT_CASE(krealloc_pagealloc_more_oob),
1079 KUNIT_CASE(krealloc_pagealloc_less_oob),
1080 KUNIT_CASE(krealloc_uaf),
1081 KUNIT_CASE(kmalloc_oob_16),
1082 KUNIT_CASE(kmalloc_uaf_16),
1083 KUNIT_CASE(kmalloc_oob_in_memset),
1084 KUNIT_CASE(kmalloc_oob_memset_2),
1085 KUNIT_CASE(kmalloc_oob_memset_4),
1086 KUNIT_CASE(kmalloc_oob_memset_8),
1087 KUNIT_CASE(kmalloc_oob_memset_16),
1088 KUNIT_CASE(kmalloc_memmove_invalid_size),
1089 KUNIT_CASE(kmalloc_uaf),
1090 KUNIT_CASE(kmalloc_uaf_memset),
1091 KUNIT_CASE(kmalloc_uaf2),
1092 KUNIT_CASE(kfree_via_page),
1093 KUNIT_CASE(kfree_via_phys),
1094 KUNIT_CASE(kmem_cache_oob),
1095 KUNIT_CASE(kmem_cache_accounted),
1096 KUNIT_CASE(kmem_cache_bulk),
1097 KUNIT_CASE(kasan_global_oob),
1098 KUNIT_CASE(kasan_stack_oob),
1099 KUNIT_CASE(kasan_alloca_oob_left),
1100 KUNIT_CASE(kasan_alloca_oob_right),
1101 KUNIT_CASE(ksize_unpoisons_memory),
1102 KUNIT_CASE(ksize_uaf),
1103 KUNIT_CASE(kmem_cache_double_free),
1104 KUNIT_CASE(kmem_cache_invalid_free),
1105 KUNIT_CASE(kasan_memchr),
1106 KUNIT_CASE(kasan_memcmp),
1107 KUNIT_CASE(kasan_strings),
1108 KUNIT_CASE(kasan_bitops_generic),
1109 KUNIT_CASE(kasan_bitops_tags),
1110 KUNIT_CASE(kmalloc_double_kzfree),
1111 KUNIT_CASE(vmalloc_oob),
1112 KUNIT_CASE(match_all_not_assigned),
1113 KUNIT_CASE(match_all_ptr_tag),
1114 KUNIT_CASE(match_all_mem_tag),
1118 static struct kunit_suite kasan_kunit_test_suite = {
1120 .init = kasan_test_init,
1121 .test_cases = kasan_kunit_test_cases,
1122 .exit = kasan_test_exit,
1125 kunit_test_suite(kasan_kunit_test_suite);
1127 MODULE_LICENSE("GPL");