]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - lib/test_kasan.c
x86/speculation/mmio: Enable CPU Fill buffer clearing on idle
[mirror_ubuntu-jammy-kernel.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 if (!kasan_enabled()) {
51 kunit_err(test, "can't run KASAN tests with KASAN disabled");
52 return -1;
53 }
54
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);
59 return 0;
60 }
61
62 static void kasan_test_exit(struct kunit *test)
63 {
64 kasan_restore_multi_shot(multishot);
65 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
66 }
67
68 /**
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.
73 *
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).
78 *
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.
83 *
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.
88 */
89 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
90 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
91 !kasan_async_mode_enabled()) \
92 migrate_disable(); \
93 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
94 barrier(); \
95 expression; \
96 barrier(); \
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"); \
101 } \
102 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
103 if (READ_ONCE(fail_data.report_found)) \
104 kasan_enable_tagging_sync(); \
105 migrate_enable(); \
106 } \
107 WRITE_ONCE(fail_data.report_found, false); \
108 } while (0)
109
110 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
111 if (!IS_ENABLED(config)) \
112 kunit_skip((test), "Test requires " #config "=y"); \
113 } while (0)
114
115 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
116 if (IS_ENABLED(config)) \
117 kunit_skip((test), "Test requires " #config "=n"); \
118 } while (0)
119
120 static void kmalloc_oob_right(struct kunit *test)
121 {
122 char *ptr;
123 size_t size = 128 - KASAN_GRANULE_SIZE - 5;
124
125 ptr = kmalloc(size, GFP_KERNEL);
126 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
127
128 /*
129 * An unaligned access past the requested kmalloc size.
130 * Only generic KASAN can precisely detect these.
131 */
132 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
133 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');
134
135 /*
136 * An aligned access into the first out-of-bounds granule that falls
137 * within the aligned kmalloc object.
138 */
139 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');
140
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]);
144
145 kfree(ptr);
146 }
147
148 static void kmalloc_oob_left(struct kunit *test)
149 {
150 char *ptr;
151 size_t size = 15;
152
153 ptr = kmalloc(size, GFP_KERNEL);
154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
155
156 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
157 kfree(ptr);
158 }
159
160 static void kmalloc_node_oob_right(struct kunit *test)
161 {
162 char *ptr;
163 size_t size = 4096;
164
165 ptr = kmalloc_node(size, GFP_KERNEL, 0);
166 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
167
168 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
169 kfree(ptr);
170 }
171
172 /*
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.
177 */
178 static void kmalloc_pagealloc_oob_right(struct kunit *test)
179 {
180 char *ptr;
181 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
182
183 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
184
185 ptr = kmalloc(size, GFP_KERNEL);
186 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
187
188 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
189
190 kfree(ptr);
191 }
192
193 static void kmalloc_pagealloc_uaf(struct kunit *test)
194 {
195 char *ptr;
196 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
197
198 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
199
200 ptr = kmalloc(size, GFP_KERNEL);
201 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
202 kfree(ptr);
203
204 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
205 }
206
207 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
208 {
209 char *ptr;
210 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
211
212 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
213
214 ptr = kmalloc(size, GFP_KERNEL);
215 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
216
217 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
218 }
219
220 static void pagealloc_oob_right(struct kunit *test)
221 {
222 char *ptr;
223 struct page *pages;
224 size_t order = 4;
225 size_t size = (1UL << (PAGE_SHIFT + order));
226
227 /*
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.
231 */
232 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
233
234 pages = alloc_pages(GFP_KERNEL, order);
235 ptr = page_address(pages);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
237
238 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
239 free_pages((unsigned long)ptr, order);
240 }
241
242 static void pagealloc_uaf(struct kunit *test)
243 {
244 char *ptr;
245 struct page *pages;
246 size_t order = 4;
247
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);
252
253 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
254 }
255
256 static void kmalloc_large_oob_right(struct kunit *test)
257 {
258 char *ptr;
259 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
260
261 /*
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.
264 */
265 ptr = kmalloc(size, GFP_KERNEL);
266 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
267
268 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
269 kfree(ptr);
270 }
271
272 static void krealloc_more_oob_helper(struct kunit *test,
273 size_t size1, size_t size2)
274 {
275 char *ptr1, *ptr2;
276 size_t middle;
277
278 KUNIT_ASSERT_LT(test, size1, size2);
279 middle = size1 + (size2 - size1) / 2;
280
281 ptr1 = kmalloc(size1, GFP_KERNEL);
282 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
283
284 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
285 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
286
287 /* All offsets up to size2 must be accessible. */
288 ptr2[size1 - 1] = 'x';
289 ptr2[size1] = 'x';
290 ptr2[middle] = 'x';
291 ptr2[size2 - 1] = 'x';
292
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');
296
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');
300
301 kfree(ptr2);
302 }
303
304 static void krealloc_less_oob_helper(struct kunit *test,
305 size_t size1, size_t size2)
306 {
307 char *ptr1, *ptr2;
308 size_t middle;
309
310 KUNIT_ASSERT_LT(test, size2, size1);
311 middle = size2 + (size1 - size2) / 2;
312
313 ptr1 = kmalloc(size1, GFP_KERNEL);
314 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
315
316 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
317 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
318
319 /* Must be accessible for all modes. */
320 ptr2[size2 - 1] = 'x';
321
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');
325
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');
329
330 /*
331 * For all modes all size2, middle, and size1 should land in separate
332 * granules and thus the latter two offsets should be inaccessible.
333 */
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');
341
342 kfree(ptr2);
343 }
344
345 static void krealloc_more_oob(struct kunit *test)
346 {
347 krealloc_more_oob_helper(test, 201, 235);
348 }
349
350 static void krealloc_less_oob(struct kunit *test)
351 {
352 krealloc_less_oob_helper(test, 235, 201);
353 }
354
355 static void krealloc_pagealloc_more_oob(struct kunit *test)
356 {
357 /* page_alloc fallback in only implemented for SLUB. */
358 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
359
360 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
361 KMALLOC_MAX_CACHE_SIZE + 235);
362 }
363
364 static void krealloc_pagealloc_less_oob(struct kunit *test)
365 {
366 /* page_alloc fallback in only implemented for SLUB. */
367 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
368
369 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
370 KMALLOC_MAX_CACHE_SIZE + 201);
371 }
372
373 /*
374 * Check that krealloc() detects a use-after-free, returns NULL,
375 * and doesn't unpoison the freed object.
376 */
377 static void krealloc_uaf(struct kunit *test)
378 {
379 char *ptr1, *ptr2;
380 int size1 = 201;
381 int size2 = 235;
382
383 ptr1 = kmalloc(size1, GFP_KERNEL);
384 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
385 kfree(ptr1);
386
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);
390 }
391
392 static void kmalloc_oob_16(struct kunit *test)
393 {
394 struct {
395 u64 words[2];
396 } *ptr1, *ptr2;
397
398 /* This test is specifically crafted for the generic mode. */
399 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
400
401 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
402 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
403
404 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
406
407 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
408 kfree(ptr1);
409 kfree(ptr2);
410 }
411
412 static void kmalloc_uaf_16(struct kunit *test)
413 {
414 struct {
415 u64 words[2];
416 } *ptr1, *ptr2;
417
418 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
419 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
420
421 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
422 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
423 kfree(ptr2);
424
425 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
426 kfree(ptr1);
427 }
428
429 /*
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.
433 */
434
435 static void kmalloc_oob_memset_2(struct kunit *test)
436 {
437 char *ptr;
438 size_t size = 128 - KASAN_GRANULE_SIZE;
439
440 ptr = kmalloc(size, GFP_KERNEL);
441 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
442
443 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
444 kfree(ptr);
445 }
446
447 static void kmalloc_oob_memset_4(struct kunit *test)
448 {
449 char *ptr;
450 size_t size = 128 - KASAN_GRANULE_SIZE;
451
452 ptr = kmalloc(size, GFP_KERNEL);
453 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
454
455 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
456 kfree(ptr);
457 }
458
459 static void kmalloc_oob_memset_8(struct kunit *test)
460 {
461 char *ptr;
462 size_t size = 128 - KASAN_GRANULE_SIZE;
463
464 ptr = kmalloc(size, GFP_KERNEL);
465 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
466
467 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
468 kfree(ptr);
469 }
470
471 static void kmalloc_oob_memset_16(struct kunit *test)
472 {
473 char *ptr;
474 size_t size = 128 - KASAN_GRANULE_SIZE;
475
476 ptr = kmalloc(size, GFP_KERNEL);
477 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
478
479 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
480 kfree(ptr);
481 }
482
483 static void kmalloc_oob_in_memset(struct kunit *test)
484 {
485 char *ptr;
486 size_t size = 128 - KASAN_GRANULE_SIZE;
487
488 ptr = kmalloc(size, GFP_KERNEL);
489 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
490
491 KUNIT_EXPECT_KASAN_FAIL(test,
492 memset(ptr, 0, size + KASAN_GRANULE_SIZE));
493 kfree(ptr);
494 }
495
496 static void kmalloc_memmove_invalid_size(struct kunit *test)
497 {
498 char *ptr;
499 size_t size = 64;
500 volatile size_t invalid_size = -2;
501
502 /*
503 * Hardware tag-based mode doesn't check memmove for negative size.
504 * As a result, this test introduces a side-effect memory corruption,
505 * which can result in a crash.
506 */
507 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_HW_TAGS);
508
509 ptr = kmalloc(size, GFP_KERNEL);
510 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
511
512 memset((char *)ptr, 0, 64);
513 KUNIT_EXPECT_KASAN_FAIL(test,
514 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
515 kfree(ptr);
516 }
517
518 static void kmalloc_uaf(struct kunit *test)
519 {
520 char *ptr;
521 size_t size = 10;
522
523 ptr = kmalloc(size, GFP_KERNEL);
524 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
525
526 kfree(ptr);
527 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[8]);
528 }
529
530 static void kmalloc_uaf_memset(struct kunit *test)
531 {
532 char *ptr;
533 size_t size = 33;
534
535 /*
536 * Only generic KASAN uses quarantine, which is required to avoid a
537 * kernel memory corruption this test causes.
538 */
539 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
540
541 ptr = kmalloc(size, GFP_KERNEL);
542 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
543
544 kfree(ptr);
545 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
546 }
547
548 static void kmalloc_uaf2(struct kunit *test)
549 {
550 char *ptr1, *ptr2;
551 size_t size = 43;
552 int counter = 0;
553
554 again:
555 ptr1 = kmalloc(size, GFP_KERNEL);
556 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
557
558 kfree(ptr1);
559
560 ptr2 = kmalloc(size, GFP_KERNEL);
561 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
562
563 /*
564 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
565 * Allow up to 16 attempts at generating different tags.
566 */
567 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
568 kfree(ptr2);
569 goto again;
570 }
571
572 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[40]);
573 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
574
575 kfree(ptr2);
576 }
577
578 static void kfree_via_page(struct kunit *test)
579 {
580 char *ptr;
581 size_t size = 8;
582 struct page *page;
583 unsigned long offset;
584
585 ptr = kmalloc(size, GFP_KERNEL);
586 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
587
588 page = virt_to_page(ptr);
589 offset = offset_in_page(ptr);
590 kfree(page_address(page) + offset);
591 }
592
593 static void kfree_via_phys(struct kunit *test)
594 {
595 char *ptr;
596 size_t size = 8;
597 phys_addr_t phys;
598
599 ptr = kmalloc(size, GFP_KERNEL);
600 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
601
602 phys = virt_to_phys(ptr);
603 kfree(phys_to_virt(phys));
604 }
605
606 static void kmem_cache_oob(struct kunit *test)
607 {
608 char *p;
609 size_t size = 200;
610 struct kmem_cache *cache;
611
612 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
613 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
614
615 p = kmem_cache_alloc(cache, GFP_KERNEL);
616 if (!p) {
617 kunit_err(test, "Allocation failed: %s\n", __func__);
618 kmem_cache_destroy(cache);
619 return;
620 }
621
622 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
623
624 kmem_cache_free(cache, p);
625 kmem_cache_destroy(cache);
626 }
627
628 static void kmem_cache_accounted(struct kunit *test)
629 {
630 int i;
631 char *p;
632 size_t size = 200;
633 struct kmem_cache *cache;
634
635 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
636 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
637
638 /*
639 * Several allocations with a delay to allow for lazy per memcg kmem
640 * cache creation.
641 */
642 for (i = 0; i < 5; i++) {
643 p = kmem_cache_alloc(cache, GFP_KERNEL);
644 if (!p)
645 goto free_cache;
646
647 kmem_cache_free(cache, p);
648 msleep(100);
649 }
650
651 free_cache:
652 kmem_cache_destroy(cache);
653 }
654
655 static void kmem_cache_bulk(struct kunit *test)
656 {
657 struct kmem_cache *cache;
658 size_t size = 200;
659 char *p[10];
660 bool ret;
661 int i;
662
663 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
664 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
665
666 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
667 if (!ret) {
668 kunit_err(test, "Allocation failed: %s\n", __func__);
669 kmem_cache_destroy(cache);
670 return;
671 }
672
673 for (i = 0; i < ARRAY_SIZE(p); i++)
674 p[i][0] = p[i][size - 1] = 42;
675
676 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
677 kmem_cache_destroy(cache);
678 }
679
680 static char global_array[10];
681
682 static void kasan_global_oob(struct kunit *test)
683 {
684 /*
685 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
686 * from failing here and panicking the kernel, access the array via a
687 * volatile pointer, which will prevent the compiler from being able to
688 * determine the array bounds.
689 *
690 * This access uses a volatile pointer to char (char *volatile) rather
691 * than the more conventional pointer to volatile char (volatile char *)
692 * because we want to prevent the compiler from making inferences about
693 * the pointer itself (i.e. its array bounds), not the data that it
694 * refers to.
695 */
696 char *volatile array = global_array;
697 char *p = &array[ARRAY_SIZE(global_array) + 3];
698
699 /* Only generic mode instruments globals. */
700 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
701
702 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
703 }
704
705 /* Check that ksize() makes the whole object accessible. */
706 static void ksize_unpoisons_memory(struct kunit *test)
707 {
708 char *ptr;
709 size_t size = 123, real_size;
710
711 ptr = kmalloc(size, GFP_KERNEL);
712 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
713 real_size = ksize(ptr);
714
715 /* This access shouldn't trigger a KASAN report. */
716 ptr[size] = 'x';
717
718 /* This one must. */
719 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[real_size]);
720
721 kfree(ptr);
722 }
723
724 /*
725 * Check that a use-after-free is detected by ksize() and via normal accesses
726 * after it.
727 */
728 static void ksize_uaf(struct kunit *test)
729 {
730 char *ptr;
731 int size = 128 - KASAN_GRANULE_SIZE;
732
733 ptr = kmalloc(size, GFP_KERNEL);
734 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
735 kfree(ptr);
736
737 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
738 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
739 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);
740 }
741
742 static void kasan_stack_oob(struct kunit *test)
743 {
744 char stack_array[10];
745 /* See comment in kasan_global_oob. */
746 char *volatile array = stack_array;
747 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
748
749 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
750
751 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
752 }
753
754 static void kasan_alloca_oob_left(struct kunit *test)
755 {
756 volatile int i = 10;
757 char alloca_array[i];
758 /* See comment in kasan_global_oob. */
759 char *volatile array = alloca_array;
760 char *p = array - 1;
761
762 /* Only generic mode instruments dynamic allocas. */
763 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
764 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
765
766 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
767 }
768
769 static void kasan_alloca_oob_right(struct kunit *test)
770 {
771 volatile int i = 10;
772 char alloca_array[i];
773 /* See comment in kasan_global_oob. */
774 char *volatile array = alloca_array;
775 char *p = array + i;
776
777 /* Only generic mode instruments dynamic allocas. */
778 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
779 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
780
781 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
782 }
783
784 static void kmem_cache_double_free(struct kunit *test)
785 {
786 char *p;
787 size_t size = 200;
788 struct kmem_cache *cache;
789
790 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
791 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
792
793 p = kmem_cache_alloc(cache, GFP_KERNEL);
794 if (!p) {
795 kunit_err(test, "Allocation failed: %s\n", __func__);
796 kmem_cache_destroy(cache);
797 return;
798 }
799
800 kmem_cache_free(cache, p);
801 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
802 kmem_cache_destroy(cache);
803 }
804
805 static void kmem_cache_invalid_free(struct kunit *test)
806 {
807 char *p;
808 size_t size = 200;
809 struct kmem_cache *cache;
810
811 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
812 NULL);
813 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
814
815 p = kmem_cache_alloc(cache, GFP_KERNEL);
816 if (!p) {
817 kunit_err(test, "Allocation failed: %s\n", __func__);
818 kmem_cache_destroy(cache);
819 return;
820 }
821
822 /* Trigger invalid free, the object doesn't get freed. */
823 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
824
825 /*
826 * Properly free the object to prevent the "Objects remaining in
827 * test_cache on __kmem_cache_shutdown" BUG failure.
828 */
829 kmem_cache_free(cache, p);
830
831 kmem_cache_destroy(cache);
832 }
833
834 static void kasan_memchr(struct kunit *test)
835 {
836 char *ptr;
837 size_t size = 24;
838
839 /*
840 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
841 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
842 */
843 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
844
845 if (OOB_TAG_OFF)
846 size = round_up(size, OOB_TAG_OFF);
847
848 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
849 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
850
851 KUNIT_EXPECT_KASAN_FAIL(test,
852 kasan_ptr_result = memchr(ptr, '1', size + 1));
853
854 kfree(ptr);
855 }
856
857 static void kasan_memcmp(struct kunit *test)
858 {
859 char *ptr;
860 size_t size = 24;
861 int arr[9];
862
863 /*
864 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
865 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
866 */
867 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
868
869 if (OOB_TAG_OFF)
870 size = round_up(size, OOB_TAG_OFF);
871
872 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
873 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
874 memset(arr, 0, sizeof(arr));
875
876 KUNIT_EXPECT_KASAN_FAIL(test,
877 kasan_int_result = memcmp(ptr, arr, size+1));
878 kfree(ptr);
879 }
880
881 static void kasan_strings(struct kunit *test)
882 {
883 char *ptr;
884 size_t size = 24;
885
886 /*
887 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
888 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
889 */
890 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
891
892 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
893 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
894
895 kfree(ptr);
896
897 /*
898 * Try to cause only 1 invalid access (less spam in dmesg).
899 * For that we need ptr to point to zeroed byte.
900 * Skip metadata that could be stored in freed object so ptr
901 * will likely point to zeroed byte.
902 */
903 ptr += 16;
904 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
905
906 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
907
908 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
909
910 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
911
912 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
913
914 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
915 }
916
917 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
918 {
919 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
920 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
921 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
922 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
923 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
924 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
925 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
926 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
927 }
928
929 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
930 {
931 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
932 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
933 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
934 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
935 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
936 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
937 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
938 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
939
940 #if defined(clear_bit_unlock_is_negative_byte)
941 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
942 clear_bit_unlock_is_negative_byte(nr, addr));
943 #endif
944 }
945
946 static void kasan_bitops_generic(struct kunit *test)
947 {
948 long *bits;
949
950 /* This test is specifically crafted for the generic mode. */
951 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
952
953 /*
954 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
955 * this way we do not actually corrupt other memory.
956 */
957 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
958 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
959
960 /*
961 * Below calls try to access bit within allocated memory; however, the
962 * below accesses are still out-of-bounds, since bitops are defined to
963 * operate on the whole long the bit is in.
964 */
965 kasan_bitops_modify(test, BITS_PER_LONG, bits);
966
967 /*
968 * Below calls try to access bit beyond allocated memory.
969 */
970 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
971
972 kfree(bits);
973 }
974
975 static void kasan_bitops_tags(struct kunit *test)
976 {
977 long *bits;
978
979 /* This test is specifically crafted for tag-based modes. */
980 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
981
982 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
983 bits = kzalloc(48, GFP_KERNEL);
984 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
985
986 /* Do the accesses past the 48 allocated bytes, but within the redone. */
987 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
988 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
989
990 kfree(bits);
991 }
992
993 static void kmalloc_double_kzfree(struct kunit *test)
994 {
995 char *ptr;
996 size_t size = 16;
997
998 ptr = kmalloc(size, GFP_KERNEL);
999 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1000
1001 kfree_sensitive(ptr);
1002 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
1003 }
1004
1005 static void vmalloc_oob(struct kunit *test)
1006 {
1007 void *area;
1008
1009 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1010
1011 /*
1012 * We have to be careful not to hit the guard page.
1013 * The MMU will catch that and crash us.
1014 */
1015 area = vmalloc(3000);
1016 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
1017
1018 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
1019 vfree(area);
1020 }
1021
1022 /*
1023 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
1024 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
1025 * modes.
1026 */
1027 static void match_all_not_assigned(struct kunit *test)
1028 {
1029 char *ptr;
1030 struct page *pages;
1031 int i, size, order;
1032
1033 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1034
1035 for (i = 0; i < 256; i++) {
1036 size = (get_random_int() % 1024) + 1;
1037 ptr = kmalloc(size, GFP_KERNEL);
1038 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1039 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1040 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1041 kfree(ptr);
1042 }
1043
1044 for (i = 0; i < 256; i++) {
1045 order = (get_random_int() % 4) + 1;
1046 pages = alloc_pages(GFP_KERNEL, order);
1047 ptr = page_address(pages);
1048 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1049 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1050 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1051 free_pages((unsigned long)ptr, order);
1052 }
1053 }
1054
1055 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1056 static void match_all_ptr_tag(struct kunit *test)
1057 {
1058 char *ptr;
1059 u8 tag;
1060
1061 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1062
1063 ptr = kmalloc(128, GFP_KERNEL);
1064 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1065
1066 /* Backup the assigned tag. */
1067 tag = get_tag(ptr);
1068 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1069
1070 /* Reset the tag to 0xff.*/
1071 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1072
1073 /* This access shouldn't trigger a KASAN report. */
1074 *ptr = 0;
1075
1076 /* Recover the pointer tag and free. */
1077 ptr = set_tag(ptr, tag);
1078 kfree(ptr);
1079 }
1080
1081 /* Check that there are no match-all memory tags for tag-based modes. */
1082 static void match_all_mem_tag(struct kunit *test)
1083 {
1084 char *ptr;
1085 int tag;
1086
1087 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1088
1089 ptr = kmalloc(128, GFP_KERNEL);
1090 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1091 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1092
1093 /* For each possible tag value not matching the pointer tag. */
1094 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1095 if (tag == get_tag(ptr))
1096 continue;
1097
1098 /* Mark the first memory granule with the chosen memory tag. */
1099 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1100
1101 /* This access must cause a KASAN report. */
1102 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1103 }
1104
1105 /* Recover the memory tag and free. */
1106 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1107 kfree(ptr);
1108 }
1109
1110 static struct kunit_case kasan_kunit_test_cases[] = {
1111 KUNIT_CASE(kmalloc_oob_right),
1112 KUNIT_CASE(kmalloc_oob_left),
1113 KUNIT_CASE(kmalloc_node_oob_right),
1114 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1115 KUNIT_CASE(kmalloc_pagealloc_uaf),
1116 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1117 KUNIT_CASE(pagealloc_oob_right),
1118 KUNIT_CASE(pagealloc_uaf),
1119 KUNIT_CASE(kmalloc_large_oob_right),
1120 KUNIT_CASE(krealloc_more_oob),
1121 KUNIT_CASE(krealloc_less_oob),
1122 KUNIT_CASE(krealloc_pagealloc_more_oob),
1123 KUNIT_CASE(krealloc_pagealloc_less_oob),
1124 KUNIT_CASE(krealloc_uaf),
1125 KUNIT_CASE(kmalloc_oob_16),
1126 KUNIT_CASE(kmalloc_uaf_16),
1127 KUNIT_CASE(kmalloc_oob_in_memset),
1128 KUNIT_CASE(kmalloc_oob_memset_2),
1129 KUNIT_CASE(kmalloc_oob_memset_4),
1130 KUNIT_CASE(kmalloc_oob_memset_8),
1131 KUNIT_CASE(kmalloc_oob_memset_16),
1132 KUNIT_CASE(kmalloc_memmove_invalid_size),
1133 KUNIT_CASE(kmalloc_uaf),
1134 KUNIT_CASE(kmalloc_uaf_memset),
1135 KUNIT_CASE(kmalloc_uaf2),
1136 KUNIT_CASE(kfree_via_page),
1137 KUNIT_CASE(kfree_via_phys),
1138 KUNIT_CASE(kmem_cache_oob),
1139 KUNIT_CASE(kmem_cache_accounted),
1140 KUNIT_CASE(kmem_cache_bulk),
1141 KUNIT_CASE(kasan_global_oob),
1142 KUNIT_CASE(kasan_stack_oob),
1143 KUNIT_CASE(kasan_alloca_oob_left),
1144 KUNIT_CASE(kasan_alloca_oob_right),
1145 KUNIT_CASE(ksize_unpoisons_memory),
1146 KUNIT_CASE(ksize_uaf),
1147 KUNIT_CASE(kmem_cache_double_free),
1148 KUNIT_CASE(kmem_cache_invalid_free),
1149 KUNIT_CASE(kasan_memchr),
1150 KUNIT_CASE(kasan_memcmp),
1151 KUNIT_CASE(kasan_strings),
1152 KUNIT_CASE(kasan_bitops_generic),
1153 KUNIT_CASE(kasan_bitops_tags),
1154 KUNIT_CASE(kmalloc_double_kzfree),
1155 KUNIT_CASE(vmalloc_oob),
1156 KUNIT_CASE(match_all_not_assigned),
1157 KUNIT_CASE(match_all_ptr_tag),
1158 KUNIT_CASE(match_all_mem_tag),
1159 {}
1160 };
1161
1162 static struct kunit_suite kasan_kunit_test_suite = {
1163 .name = "kasan",
1164 .init = kasan_test_init,
1165 .test_cases = kasan_kunit_test_cases,
1166 .exit = kasan_test_exit,
1167 };
1168
1169 kunit_test_suite(kasan_kunit_test_suite);
1170
1171 MODULE_LICENSE("GPL");