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 fail_data.report_found = false;
57 kunit_add_named_resource(test, NULL, NULL, &resource,
58 "kasan_data", &fail_data);
62 static void kasan_test_exit(struct kunit *test)
64 kasan_restore_multi_shot(multishot);
65 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
69 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
70 * KASAN report; causes a test failure otherwise. This relies on a KUnit
71 * resource named "kasan_data". Do not use this name for KUnit resources
72 * outside of KASAN tests.
74 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
75 * checking is auto-disabled. When this happens, this test handler reenables
76 * tag checking. As tag checking can be only disabled or enabled per CPU,
77 * this handler disables migration (preemption).
79 * Since the compiler doesn't see that the expression can change the fail_data
80 * fields, it can reorder or optimize away the accesses to those fields.
81 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
82 * expression to prevent that.
84 * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
85 * false. This allows detecting KASAN reports that happen outside of the checks
86 * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
87 * and in kasan_test_exit.
89 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
90 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
91 kasan_sync_fault_possible()) \
93 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
97 if (!READ_ONCE(fail_data.report_found)) { \
98 KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure " \
99 "expected in \"" #expression \
100 "\", but none occurred"); \
102 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
103 if (READ_ONCE(fail_data.report_found)) \
104 kasan_enable_tagging_sync(); \
107 WRITE_ONCE(fail_data.report_found, false); \
110 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
111 if (!IS_ENABLED(config)) \
112 kunit_skip((test), "Test requires " #config "=y"); \
115 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
116 if (IS_ENABLED(config)) \
117 kunit_skip((test), "Test requires " #config "=n"); \
120 static void kmalloc_oob_right(struct kunit *test)
123 size_t size = 128 - KASAN_GRANULE_SIZE - 5;
125 ptr = kmalloc(size, GFP_KERNEL);
126 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
129 * An unaligned access past the requested kmalloc size.
130 * Only generic KASAN can precisely detect these.
132 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
133 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');
136 * An aligned access into the first out-of-bounds granule that falls
137 * within the aligned kmalloc object.
139 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');
141 /* Out-of-bounds access past the aligned kmalloc object. */
142 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] =
143 ptr[size + KASAN_GRANULE_SIZE + 5]);
148 static void kmalloc_oob_left(struct kunit *test)
153 ptr = kmalloc(size, GFP_KERNEL);
154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
156 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
160 static void kmalloc_node_oob_right(struct kunit *test)
165 ptr = kmalloc_node(size, GFP_KERNEL, 0);
166 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
168 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
173 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
174 * fit into a slab cache and therefore is allocated via the page allocator
175 * fallback. Since this kind of fallback is only implemented for SLUB, these
176 * tests are limited to that allocator.
178 static void kmalloc_pagealloc_oob_right(struct kunit *test)
181 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
183 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
185 ptr = kmalloc(size, GFP_KERNEL);
186 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
188 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
193 static void kmalloc_pagealloc_uaf(struct kunit *test)
196 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
198 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
200 ptr = kmalloc(size, GFP_KERNEL);
201 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
204 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
207 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
210 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
212 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
214 ptr = kmalloc(size, GFP_KERNEL);
215 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
217 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
220 static void pagealloc_oob_right(struct kunit *test)
225 size_t size = (1UL << (PAGE_SHIFT + order));
228 * With generic KASAN page allocations have no redzones, thus
229 * out-of-bounds detection is not guaranteed.
230 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
232 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
234 pages = alloc_pages(GFP_KERNEL, order);
235 ptr = page_address(pages);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
238 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
239 free_pages((unsigned long)ptr, order);
242 static void pagealloc_uaf(struct kunit *test)
248 pages = alloc_pages(GFP_KERNEL, order);
249 ptr = page_address(pages);
250 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
251 free_pages((unsigned long)ptr, order);
253 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
256 static void kmalloc_large_oob_right(struct kunit *test)
259 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
262 * Allocate a chunk that is large enough, but still fits into a slab
263 * and does not trigger the page allocator fallback in SLUB.
265 ptr = kmalloc(size, GFP_KERNEL);
266 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
268 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
272 static void krealloc_more_oob_helper(struct kunit *test,
273 size_t size1, size_t size2)
278 KUNIT_ASSERT_LT(test, size1, size2);
279 middle = size1 + (size2 - size1) / 2;
281 ptr1 = kmalloc(size1, GFP_KERNEL);
282 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
284 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
285 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
287 /* All offsets up to size2 must be accessible. */
288 ptr2[size1 - 1] = 'x';
291 ptr2[size2 - 1] = 'x';
293 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
294 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
295 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
297 /* For all modes first aligned offset after size2 must be inaccessible. */
298 KUNIT_EXPECT_KASAN_FAIL(test,
299 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
304 static void krealloc_less_oob_helper(struct kunit *test,
305 size_t size1, size_t size2)
310 KUNIT_ASSERT_LT(test, size2, size1);
311 middle = size2 + (size1 - size2) / 2;
313 ptr1 = kmalloc(size1, GFP_KERNEL);
314 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
316 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
317 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
319 /* Must be accessible for all modes. */
320 ptr2[size2 - 1] = 'x';
322 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
323 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
324 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
326 /* For all modes first aligned offset after size2 must be inaccessible. */
327 KUNIT_EXPECT_KASAN_FAIL(test,
328 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
331 * For all modes all size2, middle, and size1 should land in separate
332 * granules and thus the latter two offsets should be inaccessible.
334 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
335 round_down(middle, KASAN_GRANULE_SIZE));
336 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
337 round_down(size1, KASAN_GRANULE_SIZE));
338 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
339 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
340 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
345 static void krealloc_more_oob(struct kunit *test)
347 krealloc_more_oob_helper(test, 201, 235);
350 static void krealloc_less_oob(struct kunit *test)
352 krealloc_less_oob_helper(test, 235, 201);
355 static void krealloc_pagealloc_more_oob(struct kunit *test)
357 /* page_alloc fallback in only implemented for SLUB. */
358 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
360 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
361 KMALLOC_MAX_CACHE_SIZE + 235);
364 static void krealloc_pagealloc_less_oob(struct kunit *test)
366 /* page_alloc fallback in only implemented for SLUB. */
367 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
369 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
370 KMALLOC_MAX_CACHE_SIZE + 201);
374 * Check that krealloc() detects a use-after-free, returns NULL,
375 * and doesn't unpoison the freed object.
377 static void krealloc_uaf(struct kunit *test)
383 ptr1 = kmalloc(size1, GFP_KERNEL);
384 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
387 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
388 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
389 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
392 static void kmalloc_oob_16(struct kunit *test)
398 /* This test is specifically crafted for the generic mode. */
399 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
401 ptr1 = kmalloc(sizeof(*ptr1) - 3, 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);
407 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
412 static void kmalloc_uaf_16(struct kunit *test)
418 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
419 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
421 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
422 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
425 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
430 * Note: in the memset tests below, the written range touches both valid and
431 * invalid memory. This makes sure that the instrumentation does not only check
432 * the starting address but the whole range.
435 static void kmalloc_oob_memset_2(struct kunit *test)
438 size_t size = 128 - KASAN_GRANULE_SIZE;
440 ptr = kmalloc(size, GFP_KERNEL);
441 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
443 OPTIMIZER_HIDE_VAR(size);
444 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
448 static void kmalloc_oob_memset_4(struct kunit *test)
451 size_t size = 128 - KASAN_GRANULE_SIZE;
453 ptr = kmalloc(size, GFP_KERNEL);
454 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
456 OPTIMIZER_HIDE_VAR(size);
457 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
461 static void kmalloc_oob_memset_8(struct kunit *test)
464 size_t size = 128 - KASAN_GRANULE_SIZE;
466 ptr = kmalloc(size, GFP_KERNEL);
467 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
469 OPTIMIZER_HIDE_VAR(size);
470 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
474 static void kmalloc_oob_memset_16(struct kunit *test)
477 size_t size = 128 - KASAN_GRANULE_SIZE;
479 ptr = kmalloc(size, GFP_KERNEL);
480 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
482 OPTIMIZER_HIDE_VAR(size);
483 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
487 static void kmalloc_oob_in_memset(struct kunit *test)
490 size_t size = 128 - KASAN_GRANULE_SIZE;
492 ptr = kmalloc(size, GFP_KERNEL);
493 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
495 OPTIMIZER_HIDE_VAR(ptr);
496 OPTIMIZER_HIDE_VAR(size);
497 KUNIT_EXPECT_KASAN_FAIL(test,
498 memset(ptr, 0, size + KASAN_GRANULE_SIZE));
502 static void kmalloc_memmove_negative_size(struct kunit *test)
506 size_t invalid_size = -2;
509 * Hardware tag-based mode doesn't check memmove for negative size.
510 * As a result, this test introduces a side-effect memory corruption,
511 * which can result in a crash.
513 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_HW_TAGS);
515 ptr = kmalloc(size, GFP_KERNEL);
516 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
518 memset((char *)ptr, 0, 64);
519 OPTIMIZER_HIDE_VAR(ptr);
520 OPTIMIZER_HIDE_VAR(invalid_size);
521 KUNIT_EXPECT_KASAN_FAIL(test,
522 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
526 static void kmalloc_memmove_invalid_size(struct kunit *test)
530 volatile size_t invalid_size = size;
532 ptr = kmalloc(size, GFP_KERNEL);
533 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
535 memset((char *)ptr, 0, 64);
536 OPTIMIZER_HIDE_VAR(ptr);
537 KUNIT_EXPECT_KASAN_FAIL(test,
538 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
542 static void kmalloc_uaf(struct kunit *test)
547 ptr = kmalloc(size, GFP_KERNEL);
548 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
551 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[8]);
554 static void kmalloc_uaf_memset(struct kunit *test)
560 * Only generic KASAN uses quarantine, which is required to avoid a
561 * kernel memory corruption this test causes.
563 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
565 ptr = kmalloc(size, GFP_KERNEL);
566 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
569 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
572 static void kmalloc_uaf2(struct kunit *test)
579 ptr1 = kmalloc(size, GFP_KERNEL);
580 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
584 ptr2 = kmalloc(size, GFP_KERNEL);
585 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
588 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
589 * Allow up to 16 attempts at generating different tags.
591 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
596 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[40]);
597 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
602 static void kfree_via_page(struct kunit *test)
607 unsigned long offset;
609 ptr = kmalloc(size, GFP_KERNEL);
610 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
612 page = virt_to_page(ptr);
613 offset = offset_in_page(ptr);
614 kfree(page_address(page) + offset);
617 static void kfree_via_phys(struct kunit *test)
623 ptr = kmalloc(size, GFP_KERNEL);
624 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
626 phys = virt_to_phys(ptr);
627 kfree(phys_to_virt(phys));
630 static void kmem_cache_oob(struct kunit *test)
634 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 p = kmem_cache_alloc(cache, GFP_KERNEL);
641 kunit_err(test, "Allocation failed: %s\n", __func__);
642 kmem_cache_destroy(cache);
646 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
648 kmem_cache_free(cache, p);
649 kmem_cache_destroy(cache);
652 static void kmem_cache_accounted(struct kunit *test)
657 struct kmem_cache *cache;
659 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
660 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
663 * Several allocations with a delay to allow for lazy per memcg kmem
666 for (i = 0; i < 5; i++) {
667 p = kmem_cache_alloc(cache, GFP_KERNEL);
671 kmem_cache_free(cache, p);
676 kmem_cache_destroy(cache);
679 static void kmem_cache_bulk(struct kunit *test)
681 struct kmem_cache *cache;
687 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
688 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
690 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
692 kunit_err(test, "Allocation failed: %s\n", __func__);
693 kmem_cache_destroy(cache);
697 for (i = 0; i < ARRAY_SIZE(p); i++)
698 p[i][0] = p[i][size - 1] = 42;
700 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
701 kmem_cache_destroy(cache);
704 static char global_array[10];
706 static void kasan_global_oob_right(struct kunit *test)
709 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
710 * from failing here and panicking the kernel, access the array via a
711 * volatile pointer, which will prevent the compiler from being able to
712 * determine the array bounds.
714 * This access uses a volatile pointer to char (char *volatile) rather
715 * than the more conventional pointer to volatile char (volatile char *)
716 * because we want to prevent the compiler from making inferences about
717 * the pointer itself (i.e. its array bounds), not the data that it
720 char *volatile array = global_array;
721 char *p = &array[ARRAY_SIZE(global_array) + 3];
723 /* Only generic mode instruments globals. */
724 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
726 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
729 static void kasan_global_oob_left(struct kunit *test)
731 char *volatile array = global_array;
735 * GCC is known to fail this test, skip it.
736 * See https://bugzilla.kernel.org/show_bug.cgi?id=215051.
738 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_CC_IS_CLANG);
739 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
740 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
743 /* Check that ksize() makes the whole object accessible. */
744 static void ksize_unpoisons_memory(struct kunit *test)
747 size_t size = 123, real_size;
749 ptr = kmalloc(size, GFP_KERNEL);
750 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
751 real_size = ksize(ptr);
753 /* This access shouldn't trigger a KASAN report. */
757 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[real_size]);
763 * Check that a use-after-free is detected by ksize() and via normal accesses
766 static void ksize_uaf(struct kunit *test)
769 int size = 128 - KASAN_GRANULE_SIZE;
771 ptr = kmalloc(size, GFP_KERNEL);
772 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
775 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
776 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
777 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);
780 static void kasan_stack_oob(struct kunit *test)
782 char stack_array[10];
783 /* See comment in kasan_global_oob. */
784 char *volatile array = stack_array;
785 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
787 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
789 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
792 static void kasan_alloca_oob_left(struct kunit *test)
795 char alloca_array[i];
796 /* See comment in kasan_global_oob. */
797 char *volatile array = alloca_array;
800 /* Only generic mode instruments dynamic allocas. */
801 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
802 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
804 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
807 static void kasan_alloca_oob_right(struct kunit *test)
810 char alloca_array[i];
811 /* See comment in kasan_global_oob. */
812 char *volatile array = alloca_array;
815 /* Only generic mode instruments dynamic allocas. */
816 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
817 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
819 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
822 static void kmem_cache_double_free(struct kunit *test)
826 struct kmem_cache *cache;
828 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
829 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
831 p = kmem_cache_alloc(cache, GFP_KERNEL);
833 kunit_err(test, "Allocation failed: %s\n", __func__);
834 kmem_cache_destroy(cache);
838 kmem_cache_free(cache, p);
839 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
840 kmem_cache_destroy(cache);
843 static void kmem_cache_invalid_free(struct kunit *test)
847 struct kmem_cache *cache;
849 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
851 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
853 p = kmem_cache_alloc(cache, GFP_KERNEL);
855 kunit_err(test, "Allocation failed: %s\n", __func__);
856 kmem_cache_destroy(cache);
860 /* Trigger invalid free, the object doesn't get freed. */
861 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
864 * Properly free the object to prevent the "Objects remaining in
865 * test_cache on __kmem_cache_shutdown" BUG failure.
867 kmem_cache_free(cache, p);
869 kmem_cache_destroy(cache);
872 static void kmem_cache_double_destroy(struct kunit *test)
874 struct kmem_cache *cache;
876 cache = kmem_cache_create("test_cache", 200, 0, 0, NULL);
877 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
878 kmem_cache_destroy(cache);
879 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_destroy(cache));
882 static void kasan_memchr(struct kunit *test)
888 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
889 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
891 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
894 size = round_up(size, OOB_TAG_OFF);
896 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
897 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
899 OPTIMIZER_HIDE_VAR(ptr);
900 OPTIMIZER_HIDE_VAR(size);
901 KUNIT_EXPECT_KASAN_FAIL(test,
902 kasan_ptr_result = memchr(ptr, '1', size + 1));
907 static void kasan_memcmp(struct kunit *test)
914 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
915 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
917 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
920 size = round_up(size, OOB_TAG_OFF);
922 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
923 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
924 memset(arr, 0, sizeof(arr));
926 OPTIMIZER_HIDE_VAR(ptr);
927 OPTIMIZER_HIDE_VAR(size);
928 KUNIT_EXPECT_KASAN_FAIL(test,
929 kasan_int_result = memcmp(ptr, arr, size+1));
933 static void kasan_strings(struct kunit *test)
939 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
940 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
942 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
944 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
945 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
950 * Try to cause only 1 invalid access (less spam in dmesg).
951 * For that we need ptr to point to zeroed byte.
952 * Skip metadata that could be stored in freed object so ptr
953 * will likely point to zeroed byte.
956 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
958 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
960 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
962 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
964 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
966 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
969 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
971 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
972 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
973 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
974 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
975 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
976 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
977 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
978 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
981 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
983 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
984 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
985 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
986 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
987 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
988 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
989 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
990 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
992 #if defined(clear_bit_unlock_is_negative_byte)
993 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
994 clear_bit_unlock_is_negative_byte(nr, addr));
998 static void kasan_bitops_generic(struct kunit *test)
1002 /* This test is specifically crafted for the generic mode. */
1003 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
1006 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
1007 * this way we do not actually corrupt other memory.
1009 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
1010 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
1013 * Below calls try to access bit within allocated memory; however, the
1014 * below accesses are still out-of-bounds, since bitops are defined to
1015 * operate on the whole long the bit is in.
1017 kasan_bitops_modify(test, BITS_PER_LONG, bits);
1020 * Below calls try to access bit beyond allocated memory.
1022 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
1027 static void kasan_bitops_tags(struct kunit *test)
1031 /* This test is specifically crafted for tag-based modes. */
1032 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1034 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
1035 bits = kzalloc(48, GFP_KERNEL);
1036 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
1038 /* Do the accesses past the 48 allocated bytes, but within the redone. */
1039 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
1040 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
1045 static void kmalloc_double_kzfree(struct kunit *test)
1050 ptr = kmalloc(size, GFP_KERNEL);
1051 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1053 kfree_sensitive(ptr);
1054 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
1057 static void vmalloc_oob(struct kunit *test)
1061 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1064 * We have to be careful not to hit the guard page.
1065 * The MMU will catch that and crash us.
1067 area = vmalloc(3000);
1068 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
1070 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
1075 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
1076 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
1079 static void match_all_not_assigned(struct kunit *test)
1085 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1087 for (i = 0; i < 256; i++) {
1088 size = (get_random_int() % 1024) + 1;
1089 ptr = kmalloc(size, GFP_KERNEL);
1090 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1091 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1092 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1096 for (i = 0; i < 256; i++) {
1097 order = (get_random_int() % 4) + 1;
1098 pages = alloc_pages(GFP_KERNEL, order);
1099 ptr = page_address(pages);
1100 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1101 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1102 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1103 free_pages((unsigned long)ptr, order);
1107 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1108 static void match_all_ptr_tag(struct kunit *test)
1113 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1115 ptr = kmalloc(128, GFP_KERNEL);
1116 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1118 /* Backup the assigned tag. */
1120 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1122 /* Reset the tag to 0xff.*/
1123 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1125 /* This access shouldn't trigger a KASAN report. */
1128 /* Recover the pointer tag and free. */
1129 ptr = set_tag(ptr, tag);
1133 /* Check that there are no match-all memory tags for tag-based modes. */
1134 static void match_all_mem_tag(struct kunit *test)
1139 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1141 ptr = kmalloc(128, GFP_KERNEL);
1142 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1143 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1145 /* For each possible tag value not matching the pointer tag. */
1146 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1147 if (tag == get_tag(ptr))
1150 /* Mark the first memory granule with the chosen memory tag. */
1151 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1153 /* This access must cause a KASAN report. */
1154 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1157 /* Recover the memory tag and free. */
1158 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1162 static struct kunit_case kasan_kunit_test_cases[] = {
1163 KUNIT_CASE(kmalloc_oob_right),
1164 KUNIT_CASE(kmalloc_oob_left),
1165 KUNIT_CASE(kmalloc_node_oob_right),
1166 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1167 KUNIT_CASE(kmalloc_pagealloc_uaf),
1168 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1169 KUNIT_CASE(pagealloc_oob_right),
1170 KUNIT_CASE(pagealloc_uaf),
1171 KUNIT_CASE(kmalloc_large_oob_right),
1172 KUNIT_CASE(krealloc_more_oob),
1173 KUNIT_CASE(krealloc_less_oob),
1174 KUNIT_CASE(krealloc_pagealloc_more_oob),
1175 KUNIT_CASE(krealloc_pagealloc_less_oob),
1176 KUNIT_CASE(krealloc_uaf),
1177 KUNIT_CASE(kmalloc_oob_16),
1178 KUNIT_CASE(kmalloc_uaf_16),
1179 KUNIT_CASE(kmalloc_oob_in_memset),
1180 KUNIT_CASE(kmalloc_oob_memset_2),
1181 KUNIT_CASE(kmalloc_oob_memset_4),
1182 KUNIT_CASE(kmalloc_oob_memset_8),
1183 KUNIT_CASE(kmalloc_oob_memset_16),
1184 KUNIT_CASE(kmalloc_memmove_negative_size),
1185 KUNIT_CASE(kmalloc_memmove_invalid_size),
1186 KUNIT_CASE(kmalloc_uaf),
1187 KUNIT_CASE(kmalloc_uaf_memset),
1188 KUNIT_CASE(kmalloc_uaf2),
1189 KUNIT_CASE(kfree_via_page),
1190 KUNIT_CASE(kfree_via_phys),
1191 KUNIT_CASE(kmem_cache_oob),
1192 KUNIT_CASE(kmem_cache_accounted),
1193 KUNIT_CASE(kmem_cache_bulk),
1194 KUNIT_CASE(kasan_global_oob_right),
1195 KUNIT_CASE(kasan_global_oob_left),
1196 KUNIT_CASE(kasan_stack_oob),
1197 KUNIT_CASE(kasan_alloca_oob_left),
1198 KUNIT_CASE(kasan_alloca_oob_right),
1199 KUNIT_CASE(ksize_unpoisons_memory),
1200 KUNIT_CASE(ksize_uaf),
1201 KUNIT_CASE(kmem_cache_double_free),
1202 KUNIT_CASE(kmem_cache_invalid_free),
1203 KUNIT_CASE(kmem_cache_double_destroy),
1204 KUNIT_CASE(kasan_memchr),
1205 KUNIT_CASE(kasan_memcmp),
1206 KUNIT_CASE(kasan_strings),
1207 KUNIT_CASE(kasan_bitops_generic),
1208 KUNIT_CASE(kasan_bitops_tags),
1209 KUNIT_CASE(kmalloc_double_kzfree),
1210 KUNIT_CASE(vmalloc_oob),
1211 KUNIT_CASE(match_all_not_assigned),
1212 KUNIT_CASE(match_all_ptr_tag),
1213 KUNIT_CASE(match_all_mem_tag),
1217 static struct kunit_suite kasan_kunit_test_suite = {
1219 .init = kasan_test_init,
1220 .test_cases = kasan_kunit_test_cases,
1221 .exit = kasan_test_exit,
1224 kunit_test_suite(kasan_kunit_test_suite);
1226 MODULE_LICENSE("GPL");