9a227d7e06d65a2687d9c540ec66b3137247b444
[linux-2.6-microblaze.git] / lib / test_kasan.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
6  */
7
8 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/kasan.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.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>
20 #include <linux/io.h>
21 #include <linux/vmalloc.h>
22
23 #include <asm/page.h>
24
25 #include <kunit/test.h>
26
27 #include "../mm/kasan/kasan.h"
28
29 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
30
31 /*
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.
34  */
35 void *kasan_ptr_result;
36 int kasan_int_result;
37
38 static struct kunit_resource resource;
39 static struct kunit_kasan_expectation fail_data;
40 static bool multishot;
41
42 /*
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().
47  */
48 static int kasan_test_init(struct kunit *test)
49 {
50         multishot = kasan_save_enable_multi_shot();
51         kasan_set_tagging_report_once(false);
52         return 0;
53 }
54
55 static void kasan_test_exit(struct kunit *test)
56 {
57         kasan_set_tagging_report_once(true);
58         kasan_restore_multi_shot(multishot);
59 }
60
61 /**
62  * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
63  * KASAN report; causes a test failure otherwise. This relies on a KUnit
64  * resource named "kasan_data". Do not use this name for KUnit resources
65  * outside of KASAN tests.
66  *
67  * For hardware tag-based KASAN, when a tag fault happens, tag checking is
68  * normally auto-disabled. When this happens, this test handler reenables
69  * tag checking. As tag checking can be only disabled or enabled per CPU, this
70  * handler disables migration (preemption).
71  *
72  * Since the compiler doesn't see that the expression can change the fail_data
73  * fields, it can reorder or optimize away the accesses to those fields.
74  * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
75  * expression to prevent that.
76  */
77 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do {          \
78         if (IS_ENABLED(CONFIG_KASAN_HW_TAGS))                   \
79                 migrate_disable();                              \
80         WRITE_ONCE(fail_data.report_expected, true);            \
81         WRITE_ONCE(fail_data.report_found, false);              \
82         kunit_add_named_resource(test,                          \
83                                 NULL,                           \
84                                 NULL,                           \
85                                 &resource,                      \
86                                 "kasan_data", &fail_data);      \
87         barrier();                                              \
88         expression;                                             \
89         barrier();                                              \
90         KUNIT_EXPECT_EQ(test,                                   \
91                         READ_ONCE(fail_data.report_expected),   \
92                         READ_ONCE(fail_data.report_found));     \
93         if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) {                 \
94                 if (READ_ONCE(fail_data.report_found))          \
95                         kasan_enable_tagging();                 \
96                 migrate_enable();                               \
97         }                                                       \
98 } while (0)
99
100 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do {                   \
101         if (!IS_ENABLED(config)) {                                      \
102                 kunit_info((test), "skipping, " #config " required");   \
103                 return;                                                 \
104         }                                                               \
105 } while (0)
106
107 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do {                  \
108         if (IS_ENABLED(config)) {                                       \
109                 kunit_info((test), "skipping, " #config " enabled");    \
110                 return;                                                 \
111         }                                                               \
112 } while (0)
113
114 static void kmalloc_oob_right(struct kunit *test)
115 {
116         char *ptr;
117         size_t size = 123;
118
119         ptr = kmalloc(size, GFP_KERNEL);
120         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
121
122         KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
123         kfree(ptr);
124 }
125
126 static void kmalloc_oob_left(struct kunit *test)
127 {
128         char *ptr;
129         size_t size = 15;
130
131         ptr = kmalloc(size, GFP_KERNEL);
132         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
133
134         KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
135         kfree(ptr);
136 }
137
138 static void kmalloc_node_oob_right(struct kunit *test)
139 {
140         char *ptr;
141         size_t size = 4096;
142
143         ptr = kmalloc_node(size, GFP_KERNEL, 0);
144         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
145
146         KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
147         kfree(ptr);
148 }
149
150 static void kmalloc_pagealloc_oob_right(struct kunit *test)
151 {
152         char *ptr;
153         size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
154
155         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
156
157         /*
158          * Allocate a chunk that does not fit into a SLUB cache to trigger
159          * the page allocator fallback.
160          */
161         ptr = kmalloc(size, GFP_KERNEL);
162         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
163
164         KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
165         kfree(ptr);
166 }
167
168 static void kmalloc_pagealloc_uaf(struct kunit *test)
169 {
170         char *ptr;
171         size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
172
173         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
174
175         ptr = kmalloc(size, GFP_KERNEL);
176         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
177
178         kfree(ptr);
179         KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
180 }
181
182 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
183 {
184         char *ptr;
185         size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
186
187         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
188
189         ptr = kmalloc(size, GFP_KERNEL);
190         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
191
192         KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
193 }
194
195 static void kmalloc_large_oob_right(struct kunit *test)
196 {
197         char *ptr;
198         size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
199
200         /*
201          * Allocate a chunk that is large enough, but still fits into a slab
202          * and does not trigger the page allocator fallback in SLUB.
203          */
204         ptr = kmalloc(size, GFP_KERNEL);
205         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
206
207         KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
208         kfree(ptr);
209 }
210
211 static void kmalloc_oob_krealloc_more(struct kunit *test)
212 {
213         char *ptr1, *ptr2;
214         size_t size1 = 17;
215         size_t size2 = 19;
216
217         ptr1 = kmalloc(size1, GFP_KERNEL);
218         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
219
220         ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
221         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
222
223         KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
224         kfree(ptr2);
225 }
226
227 static void kmalloc_oob_krealloc_less(struct kunit *test)
228 {
229         char *ptr1, *ptr2;
230         size_t size1 = 17;
231         size_t size2 = 15;
232
233         ptr1 = kmalloc(size1, GFP_KERNEL);
234         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
235
236         ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
237         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
238
239         KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
240         kfree(ptr2);
241 }
242
243 static void kmalloc_oob_16(struct kunit *test)
244 {
245         struct {
246                 u64 words[2];
247         } *ptr1, *ptr2;
248
249         /* This test is specifically crafted for the generic mode. */
250         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
251
252         ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
253         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
254
255         ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
256         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
257
258         KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
259         kfree(ptr1);
260         kfree(ptr2);
261 }
262
263 static void kmalloc_uaf_16(struct kunit *test)
264 {
265         struct {
266                 u64 words[2];
267         } *ptr1, *ptr2;
268
269         ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
270         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
271
272         ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
273         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
274         kfree(ptr2);
275
276         KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
277         kfree(ptr1);
278 }
279
280 static void kmalloc_oob_memset_2(struct kunit *test)
281 {
282         char *ptr;
283         size_t size = 8;
284
285         ptr = kmalloc(size, GFP_KERNEL);
286         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
287
288         KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
289         kfree(ptr);
290 }
291
292 static void kmalloc_oob_memset_4(struct kunit *test)
293 {
294         char *ptr;
295         size_t size = 8;
296
297         ptr = kmalloc(size, GFP_KERNEL);
298         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
299
300         KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
301         kfree(ptr);
302 }
303
304
305 static void kmalloc_oob_memset_8(struct kunit *test)
306 {
307         char *ptr;
308         size_t size = 8;
309
310         ptr = kmalloc(size, GFP_KERNEL);
311         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
312
313         KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
314         kfree(ptr);
315 }
316
317 static void kmalloc_oob_memset_16(struct kunit *test)
318 {
319         char *ptr;
320         size_t size = 16;
321
322         ptr = kmalloc(size, GFP_KERNEL);
323         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
324
325         KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
326         kfree(ptr);
327 }
328
329 static void kmalloc_oob_in_memset(struct kunit *test)
330 {
331         char *ptr;
332         size_t size = 666;
333
334         ptr = kmalloc(size, GFP_KERNEL);
335         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
336
337         KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
338         kfree(ptr);
339 }
340
341 static void kmalloc_memmove_invalid_size(struct kunit *test)
342 {
343         char *ptr;
344         size_t size = 64;
345         volatile size_t invalid_size = -2;
346
347         ptr = kmalloc(size, GFP_KERNEL);
348         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
349
350         memset((char *)ptr, 0, 64);
351
352         KUNIT_EXPECT_KASAN_FAIL(test,
353                 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
354         kfree(ptr);
355 }
356
357 static void kmalloc_uaf(struct kunit *test)
358 {
359         char *ptr;
360         size_t size = 10;
361
362         ptr = kmalloc(size, GFP_KERNEL);
363         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
364
365         kfree(ptr);
366         KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
367 }
368
369 static void kmalloc_uaf_memset(struct kunit *test)
370 {
371         char *ptr;
372         size_t size = 33;
373
374         ptr = kmalloc(size, GFP_KERNEL);
375         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
376
377         kfree(ptr);
378         KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
379 }
380
381 static void kmalloc_uaf2(struct kunit *test)
382 {
383         char *ptr1, *ptr2;
384         size_t size = 43;
385         int counter = 0;
386
387 again:
388         ptr1 = kmalloc(size, GFP_KERNEL);
389         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
390
391         kfree(ptr1);
392
393         ptr2 = kmalloc(size, GFP_KERNEL);
394         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
395
396         /*
397          * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
398          * Allow up to 16 attempts at generating different tags.
399          */
400         if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
401                 kfree(ptr2);
402                 goto again;
403         }
404
405         KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
406         KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
407
408         kfree(ptr2);
409 }
410
411 static void kfree_via_page(struct kunit *test)
412 {
413         char *ptr;
414         size_t size = 8;
415         struct page *page;
416         unsigned long offset;
417
418         ptr = kmalloc(size, GFP_KERNEL);
419         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
420
421         page = virt_to_page(ptr);
422         offset = offset_in_page(ptr);
423         kfree(page_address(page) + offset);
424 }
425
426 static void kfree_via_phys(struct kunit *test)
427 {
428         char *ptr;
429         size_t size = 8;
430         phys_addr_t phys;
431
432         ptr = kmalloc(size, GFP_KERNEL);
433         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
434
435         phys = virt_to_phys(ptr);
436         kfree(phys_to_virt(phys));
437 }
438
439 static void kmem_cache_oob(struct kunit *test)
440 {
441         char *p;
442         size_t size = 200;
443         struct kmem_cache *cache = kmem_cache_create("test_cache",
444                                                 size, 0,
445                                                 0, NULL);
446         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
447         p = kmem_cache_alloc(cache, GFP_KERNEL);
448         if (!p) {
449                 kunit_err(test, "Allocation failed: %s\n", __func__);
450                 kmem_cache_destroy(cache);
451                 return;
452         }
453
454         KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
455         kmem_cache_free(cache, p);
456         kmem_cache_destroy(cache);
457 }
458
459 static void memcg_accounted_kmem_cache(struct kunit *test)
460 {
461         int i;
462         char *p;
463         size_t size = 200;
464         struct kmem_cache *cache;
465
466         cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
467         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
468
469         /*
470          * Several allocations with a delay to allow for lazy per memcg kmem
471          * cache creation.
472          */
473         for (i = 0; i < 5; i++) {
474                 p = kmem_cache_alloc(cache, GFP_KERNEL);
475                 if (!p)
476                         goto free_cache;
477
478                 kmem_cache_free(cache, p);
479                 msleep(100);
480         }
481
482 free_cache:
483         kmem_cache_destroy(cache);
484 }
485
486 static char global_array[10];
487
488 static void kasan_global_oob(struct kunit *test)
489 {
490         volatile int i = 3;
491         char *p = &global_array[ARRAY_SIZE(global_array) + i];
492
493         /* Only generic mode instruments globals. */
494         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
495
496         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
497 }
498
499 static void ksize_unpoisons_memory(struct kunit *test)
500 {
501         char *ptr;
502         size_t size = 123, real_size;
503
504         ptr = kmalloc(size, GFP_KERNEL);
505         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
506         real_size = ksize(ptr);
507
508         /* This access shouldn't trigger a KASAN report. */
509         ptr[size] = 'x';
510
511         /* This one must. */
512         KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
513
514         kfree(ptr);
515 }
516
517 static void kasan_stack_oob(struct kunit *test)
518 {
519         char stack_array[10];
520         volatile int i = OOB_TAG_OFF;
521         char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
522
523         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
524
525         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
526 }
527
528 static void kasan_alloca_oob_left(struct kunit *test)
529 {
530         volatile int i = 10;
531         char alloca_array[i];
532         char *p = alloca_array - 1;
533
534         /* Only generic mode instruments dynamic allocas. */
535         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
536         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
537
538         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
539 }
540
541 static void kasan_alloca_oob_right(struct kunit *test)
542 {
543         volatile int i = 10;
544         char alloca_array[i];
545         char *p = alloca_array + i;
546
547         /* Only generic mode instruments dynamic allocas. */
548         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
549         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
550
551         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
552 }
553
554 static void kmem_cache_double_free(struct kunit *test)
555 {
556         char *p;
557         size_t size = 200;
558         struct kmem_cache *cache;
559
560         cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
561         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
562
563         p = kmem_cache_alloc(cache, GFP_KERNEL);
564         if (!p) {
565                 kunit_err(test, "Allocation failed: %s\n", __func__);
566                 kmem_cache_destroy(cache);
567                 return;
568         }
569
570         kmem_cache_free(cache, p);
571         KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
572         kmem_cache_destroy(cache);
573 }
574
575 static void kmem_cache_invalid_free(struct kunit *test)
576 {
577         char *p;
578         size_t size = 200;
579         struct kmem_cache *cache;
580
581         cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
582                                   NULL);
583         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
584
585         p = kmem_cache_alloc(cache, GFP_KERNEL);
586         if (!p) {
587                 kunit_err(test, "Allocation failed: %s\n", __func__);
588                 kmem_cache_destroy(cache);
589                 return;
590         }
591
592         /* Trigger invalid free, the object doesn't get freed. */
593         KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
594
595         /*
596          * Properly free the object to prevent the "Objects remaining in
597          * test_cache on __kmem_cache_shutdown" BUG failure.
598          */
599         kmem_cache_free(cache, p);
600
601         kmem_cache_destroy(cache);
602 }
603
604 static void kasan_memchr(struct kunit *test)
605 {
606         char *ptr;
607         size_t size = 24;
608
609         /*
610          * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
611          * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
612          */
613         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
614
615         if (OOB_TAG_OFF)
616                 size = round_up(size, OOB_TAG_OFF);
617
618         ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
619         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
620
621         KUNIT_EXPECT_KASAN_FAIL(test,
622                 kasan_ptr_result = memchr(ptr, '1', size + 1));
623
624         kfree(ptr);
625 }
626
627 static void kasan_memcmp(struct kunit *test)
628 {
629         char *ptr;
630         size_t size = 24;
631         int arr[9];
632
633         /*
634          * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
635          * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
636          */
637         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
638
639         if (OOB_TAG_OFF)
640                 size = round_up(size, OOB_TAG_OFF);
641
642         ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
643         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
644         memset(arr, 0, sizeof(arr));
645
646         KUNIT_EXPECT_KASAN_FAIL(test,
647                 kasan_int_result = memcmp(ptr, arr, size+1));
648         kfree(ptr);
649 }
650
651 static void kasan_strings(struct kunit *test)
652 {
653         char *ptr;
654         size_t size = 24;
655
656         /*
657          * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
658          * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
659          */
660         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
661
662         ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
663         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
664
665         kfree(ptr);
666
667         /*
668          * Try to cause only 1 invalid access (less spam in dmesg).
669          * For that we need ptr to point to zeroed byte.
670          * Skip metadata that could be stored in freed object so ptr
671          * will likely point to zeroed byte.
672          */
673         ptr += 16;
674         KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
675
676         KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
677
678         KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
679
680         KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
681
682         KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
683
684         KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
685 }
686
687 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
688 {
689         KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
690         KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
691         KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
692         KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
693         KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
694         KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
695         KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
696         KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
697 }
698
699 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
700 {
701         KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
702         KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
703         KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
704         KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
705         KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
706         KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
707         KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
708         KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
709
710 #if defined(clear_bit_unlock_is_negative_byte)
711         KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
712                                 clear_bit_unlock_is_negative_byte(nr, addr));
713 #endif
714 }
715
716 static void kasan_bitops_generic(struct kunit *test)
717 {
718         long *bits;
719
720         /* This test is specifically crafted for the generic mode. */
721         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
722
723         /*
724          * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
725          * this way we do not actually corrupt other memory.
726          */
727         bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
728         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
729
730         /*
731          * Below calls try to access bit within allocated memory; however, the
732          * below accesses are still out-of-bounds, since bitops are defined to
733          * operate on the whole long the bit is in.
734          */
735         kasan_bitops_modify(test, BITS_PER_LONG, bits);
736
737         /*
738          * Below calls try to access bit beyond allocated memory.
739          */
740         kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
741
742         kfree(bits);
743 }
744
745 static void kasan_bitops_tags(struct kunit *test)
746 {
747         long *bits;
748
749         /* This test is specifically crafted for tag-based modes. */
750         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
751
752         /* Allocation size will be rounded to up granule size, which is 16. */
753         bits = kzalloc(sizeof(*bits), GFP_KERNEL);
754         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
755
756         /* Do the accesses past the 16 allocated bytes. */
757         kasan_bitops_modify(test, BITS_PER_LONG, &bits[1]);
758         kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, &bits[1]);
759
760         kfree(bits);
761 }
762
763 static void kmalloc_double_kzfree(struct kunit *test)
764 {
765         char *ptr;
766         size_t size = 16;
767
768         ptr = kmalloc(size, GFP_KERNEL);
769         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
770
771         kfree_sensitive(ptr);
772         KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
773 }
774
775 static void vmalloc_oob(struct kunit *test)
776 {
777         void *area;
778
779         KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
780
781         /*
782          * We have to be careful not to hit the guard page.
783          * The MMU will catch that and crash us.
784          */
785         area = vmalloc(3000);
786         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
787
788         KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
789         vfree(area);
790 }
791
792 /*
793  * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
794  * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
795  * modes.
796  */
797 static void match_all_not_assigned(struct kunit *test)
798 {
799         char *ptr;
800         struct page *pages;
801         int i, size, order;
802
803         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
804
805         for (i = 0; i < 256; i++) {
806                 size = (get_random_int() % 1024) + 1;
807                 ptr = kmalloc(size, GFP_KERNEL);
808                 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
809                 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
810                 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
811                 kfree(ptr);
812         }
813
814         for (i = 0; i < 256; i++) {
815                 order = (get_random_int() % 4) + 1;
816                 pages = alloc_pages(GFP_KERNEL, order);
817                 ptr = page_address(pages);
818                 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
819                 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
820                 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
821                 free_pages((unsigned long)ptr, order);
822         }
823 }
824
825 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
826 static void match_all_ptr_tag(struct kunit *test)
827 {
828         char *ptr;
829         u8 tag;
830
831         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
832
833         ptr = kmalloc(128, GFP_KERNEL);
834         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
835
836         /* Backup the assigned tag. */
837         tag = get_tag(ptr);
838         KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
839
840         /* Reset the tag to 0xff.*/
841         ptr = set_tag(ptr, KASAN_TAG_KERNEL);
842
843         /* This access shouldn't trigger a KASAN report. */
844         *ptr = 0;
845
846         /* Recover the pointer tag and free. */
847         ptr = set_tag(ptr, tag);
848         kfree(ptr);
849 }
850
851 /* Check that there are no match-all memory tags for tag-based modes. */
852 static void match_all_mem_tag(struct kunit *test)
853 {
854         char *ptr;
855         int tag;
856
857         KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
858
859         ptr = kmalloc(128, GFP_KERNEL);
860         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
861         KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
862
863         /* For each possible tag value not matching the pointer tag. */
864         for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
865                 if (tag == get_tag(ptr))
866                         continue;
867
868                 /* Mark the first memory granule with the chosen memory tag. */
869                 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
870
871                 /* This access must cause a KASAN report. */
872                 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
873         }
874
875         /* Recover the memory tag and free. */
876         kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
877         kfree(ptr);
878 }
879
880 static struct kunit_case kasan_kunit_test_cases[] = {
881         KUNIT_CASE(kmalloc_oob_right),
882         KUNIT_CASE(kmalloc_oob_left),
883         KUNIT_CASE(kmalloc_node_oob_right),
884         KUNIT_CASE(kmalloc_pagealloc_oob_right),
885         KUNIT_CASE(kmalloc_pagealloc_uaf),
886         KUNIT_CASE(kmalloc_pagealloc_invalid_free),
887         KUNIT_CASE(kmalloc_large_oob_right),
888         KUNIT_CASE(kmalloc_oob_krealloc_more),
889         KUNIT_CASE(kmalloc_oob_krealloc_less),
890         KUNIT_CASE(kmalloc_oob_16),
891         KUNIT_CASE(kmalloc_uaf_16),
892         KUNIT_CASE(kmalloc_oob_in_memset),
893         KUNIT_CASE(kmalloc_oob_memset_2),
894         KUNIT_CASE(kmalloc_oob_memset_4),
895         KUNIT_CASE(kmalloc_oob_memset_8),
896         KUNIT_CASE(kmalloc_oob_memset_16),
897         KUNIT_CASE(kmalloc_memmove_invalid_size),
898         KUNIT_CASE(kmalloc_uaf),
899         KUNIT_CASE(kmalloc_uaf_memset),
900         KUNIT_CASE(kmalloc_uaf2),
901         KUNIT_CASE(kfree_via_page),
902         KUNIT_CASE(kfree_via_phys),
903         KUNIT_CASE(kmem_cache_oob),
904         KUNIT_CASE(memcg_accounted_kmem_cache),
905         KUNIT_CASE(kasan_global_oob),
906         KUNIT_CASE(kasan_stack_oob),
907         KUNIT_CASE(kasan_alloca_oob_left),
908         KUNIT_CASE(kasan_alloca_oob_right),
909         KUNIT_CASE(ksize_unpoisons_memory),
910         KUNIT_CASE(kmem_cache_double_free),
911         KUNIT_CASE(kmem_cache_invalid_free),
912         KUNIT_CASE(kasan_memchr),
913         KUNIT_CASE(kasan_memcmp),
914         KUNIT_CASE(kasan_strings),
915         KUNIT_CASE(kasan_bitops_generic),
916         KUNIT_CASE(kasan_bitops_tags),
917         KUNIT_CASE(kmalloc_double_kzfree),
918         KUNIT_CASE(vmalloc_oob),
919         KUNIT_CASE(match_all_not_assigned),
920         KUNIT_CASE(match_all_ptr_tag),
921         KUNIT_CASE(match_all_mem_tag),
922         {}
923 };
924
925 static struct kunit_suite kasan_kunit_test_suite = {
926         .name = "kasan",
927         .init = kasan_test_init,
928         .test_cases = kasan_kunit_test_cases,
929         .exit = kasan_test_exit,
930 };
931
932 kunit_test_suite(kasan_kunit_test_suite);
933
934 MODULE_LICENSE("GPL");