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