• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/set_memory.h>
23 
24 #include <asm/page.h>
25 
26 #include <kunit/test.h>
27 
28 #include "../mm/kasan/kasan.h"
29 
30 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
31 
32 /*
33  * Some tests use these global variables to store return values from function
34  * calls that could otherwise be eliminated by the compiler as dead code.
35  */
36 void *kasan_ptr_result;
37 int kasan_int_result;
38 
39 static struct kunit_resource resource;
40 static struct kunit_kasan_status test_status;
41 static bool multishot;
42 
43 /*
44  * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
45  * first detected bug and panic the kernel if panic_on_warn is enabled. For
46  * hardware tag-based KASAN also allow tag checking to be reenabled for each
47  * test, see the comment for KUNIT_EXPECT_KASAN_FAIL().
48  */
kasan_test_init(struct kunit * test)49 static int kasan_test_init(struct kunit *test)
50 {
51 	if (!kasan_enabled()) {
52 		kunit_err(test, "can't run KASAN tests with KASAN disabled");
53 		return -1;
54 	}
55 
56 	multishot = kasan_save_enable_multi_shot();
57 	test_status.report_found = false;
58 	test_status.sync_fault = false;
59 	kunit_add_named_resource(test, NULL, NULL, &resource,
60 					"kasan_status", &test_status);
61 	return 0;
62 }
63 
kasan_test_exit(struct kunit * test)64 static void kasan_test_exit(struct kunit *test)
65 {
66 	kasan_restore_multi_shot(multishot);
67 	KUNIT_EXPECT_FALSE(test, test_status.report_found);
68 }
69 
70 /**
71  * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
72  * KASAN report; causes a test failure otherwise. This relies on a KUnit
73  * resource named "kasan_status". Do not use this name for KUnit resources
74  * outside of KASAN tests.
75  *
76  * For hardware tag-based KASAN, when a synchronous tag fault happens, tag
77  * checking is auto-disabled. When this happens, this test handler reenables
78  * tag checking. As tag checking can be only disabled or enabled per CPU,
79  * this handler disables migration (preemption).
80  *
81  * Since the compiler doesn't see that the expression can change the test_status
82  * fields, it can reorder or optimize away the accesses to those fields.
83  * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
84  * expression to prevent that.
85  *
86  * In between KUNIT_EXPECT_KASAN_FAIL checks, test_status.report_found is kept
87  * as false. This allows detecting KASAN reports that happen outside of the
88  * checks by asserting !test_status.report_found at the start of
89  * KUNIT_EXPECT_KASAN_FAIL and in kasan_test_exit.
90  */
91 #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do {			\
92 	if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) &&				\
93 	    kasan_sync_fault_possible())				\
94 		migrate_disable();					\
95 	KUNIT_EXPECT_FALSE(test, READ_ONCE(test_status.report_found));	\
96 	barrier();							\
97 	expression;							\
98 	barrier();							\
99 	if (kasan_async_fault_possible())				\
100 		kasan_force_async_fault();				\
101 	if (!READ_ONCE(test_status.report_found)) {			\
102 		KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure "	\
103 				"expected in \"" #expression		\
104 				 "\", but none occurred");		\
105 	}								\
106 	if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) &&				\
107 	    kasan_sync_fault_possible()) {				\
108 		if (READ_ONCE(test_status.report_found) &&		\
109 		    READ_ONCE(test_status.sync_fault))			\
110 			kasan_enable_tagging();				\
111 		migrate_enable();					\
112 	}								\
113 	WRITE_ONCE(test_status.report_found, false);			\
114 } while (0)
115 
116 #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do {			\
117 	if (!IS_ENABLED(config)) {					\
118 		kunit_info((test), "skipping, " #config " required");	\
119 		return;							\
120 	}								\
121 } while (0)
122 
123 #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do {			\
124 	if (IS_ENABLED(config)) {					\
125 		kunit_info((test), "skipping, " #config " enabled");	\
126 		return;							\
127 	}								\
128 } while (0)
129 
kmalloc_oob_right(struct kunit * test)130 static void kmalloc_oob_right(struct kunit *test)
131 {
132 	char *ptr;
133 	size_t size = 128 - KASAN_GRANULE_SIZE - 5;
134 
135 	ptr = kmalloc(size, GFP_KERNEL);
136 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
137 
138 	/*
139 	 * An unaligned access past the requested kmalloc size.
140 	 * Only generic KASAN can precisely detect these.
141 	 */
142 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
143 		KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');
144 
145 	/*
146 	 * An aligned access into the first out-of-bounds granule that falls
147 	 * within the aligned kmalloc object.
148 	 */
149 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');
150 
151 	/* Out-of-bounds access past the aligned kmalloc object. */
152 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] =
153 					ptr[size + KASAN_GRANULE_SIZE + 5]);
154 
155 	kfree(ptr);
156 }
157 
kmalloc_oob_left(struct kunit * test)158 static void kmalloc_oob_left(struct kunit *test)
159 {
160 	char *ptr;
161 	size_t size = 15;
162 
163 	ptr = kmalloc(size, GFP_KERNEL);
164 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
165 
166 	KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
167 	kfree(ptr);
168 }
169 
kmalloc_node_oob_right(struct kunit * test)170 static void kmalloc_node_oob_right(struct kunit *test)
171 {
172 	char *ptr;
173 	size_t size = 4096;
174 
175 	ptr = kmalloc_node(size, GFP_KERNEL, 0);
176 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
177 
178 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
179 	kfree(ptr);
180 }
181 
182 /*
183  * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
184  * fit into a slab cache and therefore is allocated via the page allocator
185  * fallback. Since this kind of fallback is only implemented for SLUB, these
186  * tests are limited to that allocator.
187  */
kmalloc_pagealloc_oob_right(struct kunit * test)188 static void kmalloc_pagealloc_oob_right(struct kunit *test)
189 {
190 	char *ptr;
191 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
192 
193 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
194 
195 	ptr = kmalloc(size, GFP_KERNEL);
196 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
197 
198 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
199 
200 	kfree(ptr);
201 }
202 
kmalloc_pagealloc_uaf(struct kunit * test)203 static void kmalloc_pagealloc_uaf(struct kunit *test)
204 {
205 	char *ptr;
206 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
207 
208 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
209 
210 	ptr = kmalloc(size, GFP_KERNEL);
211 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
212 	kfree(ptr);
213 
214 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
215 }
216 
kmalloc_pagealloc_invalid_free(struct kunit * test)217 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
218 {
219 	char *ptr;
220 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
221 
222 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
223 
224 	ptr = kmalloc(size, GFP_KERNEL);
225 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
226 
227 	KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
228 }
229 
pagealloc_oob_right(struct kunit * test)230 static void pagealloc_oob_right(struct kunit *test)
231 {
232 	char *ptr;
233 	struct page *pages;
234 	size_t order = 4;
235 	size_t size = (1UL << (PAGE_SHIFT + order));
236 
237 	/*
238 	 * With generic KASAN page allocations have no redzones, thus
239 	 * out-of-bounds detection is not guaranteed.
240 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
241 	 */
242 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
243 
244 	pages = alloc_pages(GFP_KERNEL, order);
245 	ptr = page_address(pages);
246 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
247 
248 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
249 	free_pages((unsigned long)ptr, order);
250 }
251 
pagealloc_uaf(struct kunit * test)252 static void pagealloc_uaf(struct kunit *test)
253 {
254 	char *ptr;
255 	struct page *pages;
256 	size_t order = 4;
257 
258 	pages = alloc_pages(GFP_KERNEL, order);
259 	ptr = page_address(pages);
260 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
261 	free_pages((unsigned long)ptr, order);
262 
263 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
264 }
265 
kmalloc_large_oob_right(struct kunit * test)266 static void kmalloc_large_oob_right(struct kunit *test)
267 {
268 	char *ptr;
269 	size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
270 
271 	/*
272 	 * Allocate a chunk that is large enough, but still fits into a slab
273 	 * and does not trigger the page allocator fallback in SLUB.
274 	 */
275 	ptr = kmalloc(size, GFP_KERNEL);
276 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
277 
278 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
279 	kfree(ptr);
280 }
281 
krealloc_more_oob_helper(struct kunit * test,size_t size1,size_t size2)282 static void krealloc_more_oob_helper(struct kunit *test,
283 					size_t size1, size_t size2)
284 {
285 	char *ptr1, *ptr2;
286 	size_t middle;
287 
288 	KUNIT_ASSERT_LT(test, size1, size2);
289 	middle = size1 + (size2 - size1) / 2;
290 
291 	ptr1 = kmalloc(size1, GFP_KERNEL);
292 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
293 
294 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
295 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
296 
297 	/* All offsets up to size2 must be accessible. */
298 	ptr2[size1 - 1] = 'x';
299 	ptr2[size1] = 'x';
300 	ptr2[middle] = 'x';
301 	ptr2[size2 - 1] = 'x';
302 
303 	/* Generic mode is precise, so unaligned size2 must be inaccessible. */
304 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
305 		KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
306 
307 	/* For all modes first aligned offset after size2 must be inaccessible. */
308 	KUNIT_EXPECT_KASAN_FAIL(test,
309 		ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
310 
311 	kfree(ptr2);
312 }
313 
krealloc_less_oob_helper(struct kunit * test,size_t size1,size_t size2)314 static void krealloc_less_oob_helper(struct kunit *test,
315 					size_t size1, size_t size2)
316 {
317 	char *ptr1, *ptr2;
318 	size_t middle;
319 
320 	KUNIT_ASSERT_LT(test, size2, size1);
321 	middle = size2 + (size1 - size2) / 2;
322 
323 	ptr1 = kmalloc(size1, GFP_KERNEL);
324 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
325 
326 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
327 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
328 
329 	/* Must be accessible for all modes. */
330 	ptr2[size2 - 1] = 'x';
331 
332 	/* Generic mode is precise, so unaligned size2 must be inaccessible. */
333 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
334 		KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
335 
336 	/* For all modes first aligned offset after size2 must be inaccessible. */
337 	KUNIT_EXPECT_KASAN_FAIL(test,
338 		ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
339 
340 	/*
341 	 * For all modes all size2, middle, and size1 should land in separate
342 	 * granules and thus the latter two offsets should be inaccessible.
343 	 */
344 	KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
345 				round_down(middle, KASAN_GRANULE_SIZE));
346 	KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
347 				round_down(size1, KASAN_GRANULE_SIZE));
348 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
349 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
350 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
351 
352 	kfree(ptr2);
353 }
354 
krealloc_more_oob(struct kunit * test)355 static void krealloc_more_oob(struct kunit *test)
356 {
357 	krealloc_more_oob_helper(test, 201, 235);
358 }
359 
krealloc_less_oob(struct kunit * test)360 static void krealloc_less_oob(struct kunit *test)
361 {
362 	krealloc_less_oob_helper(test, 235, 201);
363 }
364 
krealloc_pagealloc_more_oob(struct kunit * test)365 static void krealloc_pagealloc_more_oob(struct kunit *test)
366 {
367 	/* page_alloc fallback in only implemented for SLUB. */
368 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
369 
370 	krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
371 					KMALLOC_MAX_CACHE_SIZE + 235);
372 }
373 
krealloc_pagealloc_less_oob(struct kunit * test)374 static void krealloc_pagealloc_less_oob(struct kunit *test)
375 {
376 	/* page_alloc fallback in only implemented for SLUB. */
377 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
378 
379 	krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
380 					KMALLOC_MAX_CACHE_SIZE + 201);
381 }
382 
383 /*
384  * Check that krealloc() detects a use-after-free, returns NULL,
385  * and doesn't unpoison the freed object.
386  */
krealloc_uaf(struct kunit * test)387 static void krealloc_uaf(struct kunit *test)
388 {
389 	char *ptr1, *ptr2;
390 	int size1 = 201;
391 	int size2 = 235;
392 
393 	ptr1 = kmalloc(size1, GFP_KERNEL);
394 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
395 	kfree(ptr1);
396 
397 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
398 	KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
399 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
400 }
401 
kmalloc_oob_16(struct kunit * test)402 static void kmalloc_oob_16(struct kunit *test)
403 {
404 	struct {
405 		u64 words[2];
406 	} *ptr1, *ptr2;
407 
408 	/* This test is specifically crafted for the generic mode. */
409 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
410 
411 	ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
412 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
413 
414 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
415 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
416 
417 	KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
418 	kfree(ptr1);
419 	kfree(ptr2);
420 }
421 
kmalloc_uaf_16(struct kunit * test)422 static void kmalloc_uaf_16(struct kunit *test)
423 {
424 	struct {
425 		u64 words[2];
426 	} *ptr1, *ptr2;
427 
428 	ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
429 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
430 
431 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
432 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
433 	kfree(ptr2);
434 
435 	KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
436 	kfree(ptr1);
437 }
438 
439 /*
440  * Note: in the memset tests below, the written range touches both valid and
441  * invalid memory. This makes sure that the instrumentation does not only check
442  * the starting address but the whole range.
443  */
444 
kmalloc_oob_memset_2(struct kunit * test)445 static void kmalloc_oob_memset_2(struct kunit *test)
446 {
447 	char *ptr;
448 	size_t size = 128 - KASAN_GRANULE_SIZE;
449 
450 	ptr = kmalloc(size, GFP_KERNEL);
451 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
452 
453 	OPTIMIZER_HIDE_VAR(size);
454 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
455 	kfree(ptr);
456 }
457 
kmalloc_oob_memset_4(struct kunit * test)458 static void kmalloc_oob_memset_4(struct kunit *test)
459 {
460 	char *ptr;
461 	size_t size = 128 - KASAN_GRANULE_SIZE;
462 
463 	ptr = kmalloc(size, GFP_KERNEL);
464 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
465 
466 	OPTIMIZER_HIDE_VAR(size);
467 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
468 	kfree(ptr);
469 }
470 
kmalloc_oob_memset_8(struct kunit * test)471 static void kmalloc_oob_memset_8(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 	OPTIMIZER_HIDE_VAR(size);
480 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
481 	kfree(ptr);
482 }
483 
kmalloc_oob_memset_16(struct kunit * test)484 static void kmalloc_oob_memset_16(struct kunit *test)
485 {
486 	char *ptr;
487 	size_t size = 128 - KASAN_GRANULE_SIZE;
488 
489 	ptr = kmalloc(size, GFP_KERNEL);
490 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
491 
492 	OPTIMIZER_HIDE_VAR(size);
493 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
494 	kfree(ptr);
495 }
496 
kmalloc_oob_in_memset(struct kunit * test)497 static void kmalloc_oob_in_memset(struct kunit *test)
498 {
499 	char *ptr;
500 	size_t size = 128 - KASAN_GRANULE_SIZE;
501 
502 	ptr = kmalloc(size, GFP_KERNEL);
503 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
504 
505 	OPTIMIZER_HIDE_VAR(ptr);
506 	OPTIMIZER_HIDE_VAR(size);
507 	KUNIT_EXPECT_KASAN_FAIL(test,
508 				memset(ptr, 0, size + KASAN_GRANULE_SIZE));
509 	kfree(ptr);
510 }
511 
kmalloc_memmove_negative_size(struct kunit * test)512 static void kmalloc_memmove_negative_size(struct kunit *test)
513 {
514 	char *ptr;
515 	size_t size = 64;
516 	size_t invalid_size = -2;
517 
518 	/*
519 	 * Hardware tag-based mode doesn't check memmove for negative size.
520 	 * As a result, this test introduces a side-effect memory corruption,
521 	 * which can result in a crash.
522 	 */
523 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_HW_TAGS);
524 
525 	ptr = kmalloc(size, GFP_KERNEL);
526 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
527 
528 	memset((char *)ptr, 0, 64);
529 	OPTIMIZER_HIDE_VAR(ptr);
530 	OPTIMIZER_HIDE_VAR(invalid_size);
531 	KUNIT_EXPECT_KASAN_FAIL(test,
532 		memmove((char *)ptr, (char *)ptr + 4, invalid_size));
533 	kfree(ptr);
534 }
535 
kmalloc_memmove_invalid_size(struct kunit * test)536 static void kmalloc_memmove_invalid_size(struct kunit *test)
537 {
538 	char *ptr;
539 	size_t size = 64;
540 	volatile size_t invalid_size = size;
541 
542 	ptr = kmalloc(size, GFP_KERNEL);
543 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
544 
545 	memset((char *)ptr, 0, 64);
546 	OPTIMIZER_HIDE_VAR(ptr);
547 	KUNIT_EXPECT_KASAN_FAIL(test,
548 		memmove((char *)ptr, (char *)ptr + 4, invalid_size));
549 	kfree(ptr);
550 }
551 
kmalloc_uaf(struct kunit * test)552 static void kmalloc_uaf(struct kunit *test)
553 {
554 	char *ptr;
555 	size_t size = 10;
556 
557 	ptr = kmalloc(size, GFP_KERNEL);
558 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
559 
560 	kfree(ptr);
561 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[8]);
562 }
563 
kmalloc_uaf_memset(struct kunit * test)564 static void kmalloc_uaf_memset(struct kunit *test)
565 {
566 	char *ptr;
567 	size_t size = 33;
568 
569 	/*
570 	 * Only generic KASAN uses quarantine, which is required to avoid a
571 	 * kernel memory corruption this test causes.
572 	 */
573 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
574 
575 	ptr = kmalloc(size, GFP_KERNEL);
576 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
577 
578 	kfree(ptr);
579 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
580 }
581 
kmalloc_uaf2(struct kunit * test)582 static void kmalloc_uaf2(struct kunit *test)
583 {
584 	char *ptr1, *ptr2;
585 	size_t size = 43;
586 	int counter = 0;
587 
588 again:
589 	ptr1 = kmalloc(size, GFP_KERNEL);
590 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
591 
592 	kfree(ptr1);
593 
594 	ptr2 = kmalloc(size, GFP_KERNEL);
595 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
596 
597 	/*
598 	 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
599 	 * Allow up to 16 attempts at generating different tags.
600 	 */
601 	if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
602 		kfree(ptr2);
603 		goto again;
604 	}
605 
606 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[40]);
607 	KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
608 
609 	kfree(ptr2);
610 }
611 
kfree_via_page(struct kunit * test)612 static void kfree_via_page(struct kunit *test)
613 {
614 	char *ptr;
615 	size_t size = 8;
616 	struct page *page;
617 	unsigned long offset;
618 
619 	ptr = kmalloc(size, GFP_KERNEL);
620 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
621 
622 	page = virt_to_page(ptr);
623 	offset = offset_in_page(ptr);
624 	kfree(page_address(page) + offset);
625 }
626 
kfree_via_phys(struct kunit * test)627 static void kfree_via_phys(struct kunit *test)
628 {
629 	char *ptr;
630 	size_t size = 8;
631 	phys_addr_t phys;
632 
633 	ptr = kmalloc(size, GFP_KERNEL);
634 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
635 
636 	phys = virt_to_phys(ptr);
637 	kfree(phys_to_virt(phys));
638 }
639 
kmem_cache_oob(struct kunit * test)640 static void kmem_cache_oob(struct kunit *test)
641 {
642 	char *p;
643 	size_t size = 200;
644 	struct kmem_cache *cache;
645 
646 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
647 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
648 
649 	p = kmem_cache_alloc(cache, GFP_KERNEL);
650 	if (!p) {
651 		kunit_err(test, "Allocation failed: %s\n", __func__);
652 		kmem_cache_destroy(cache);
653 		return;
654 	}
655 
656 	KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
657 
658 	kmem_cache_free(cache, p);
659 	kmem_cache_destroy(cache);
660 }
661 
kmem_cache_accounted(struct kunit * test)662 static void kmem_cache_accounted(struct kunit *test)
663 {
664 	int i;
665 	char *p;
666 	size_t size = 200;
667 	struct kmem_cache *cache;
668 
669 	cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
670 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
671 
672 	/*
673 	 * Several allocations with a delay to allow for lazy per memcg kmem
674 	 * cache creation.
675 	 */
676 	for (i = 0; i < 5; i++) {
677 		p = kmem_cache_alloc(cache, GFP_KERNEL);
678 		if (!p)
679 			goto free_cache;
680 
681 		kmem_cache_free(cache, p);
682 		msleep(100);
683 	}
684 
685 free_cache:
686 	kmem_cache_destroy(cache);
687 }
688 
kmem_cache_bulk(struct kunit * test)689 static void kmem_cache_bulk(struct kunit *test)
690 {
691 	struct kmem_cache *cache;
692 	size_t size = 200;
693 	char *p[10];
694 	bool ret;
695 	int i;
696 
697 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
698 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
699 
700 	ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
701 	if (!ret) {
702 		kunit_err(test, "Allocation failed: %s\n", __func__);
703 		kmem_cache_destroy(cache);
704 		return;
705 	}
706 
707 	for (i = 0; i < ARRAY_SIZE(p); i++)
708 		p[i][0] = p[i][size - 1] = 42;
709 
710 	kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
711 	kmem_cache_destroy(cache);
712 }
713 
714 static char global_array[10];
715 
kasan_global_oob_right(struct kunit * test)716 static void kasan_global_oob_right(struct kunit *test)
717 {
718 	/*
719 	 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
720 	 * from failing here and panicing the kernel, access the array via a
721 	 * volatile pointer, which will prevent the compiler from being able to
722 	 * determine the array bounds.
723 	 *
724 	 * This access uses a volatile pointer to char (char *volatile) rather
725 	 * than the more conventional pointer to volatile char (volatile char *)
726 	 * because we want to prevent the compiler from making inferences about
727 	 * the pointer itself (i.e. its array bounds), not the data that it
728 	 * refers to.
729 	 */
730 	char *volatile array = global_array;
731 	char *p = &array[ARRAY_SIZE(global_array) + 3];
732 
733 	/* Only generic mode instruments globals. */
734 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
735 
736 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
737 }
738 
kasan_global_oob_left(struct kunit * test)739 static void kasan_global_oob_left(struct kunit *test)
740 {
741 	char *volatile array = global_array;
742 	char *p = array - 3;
743 
744 	/*
745 	 * GCC is known to fail this test, skip it.
746 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=215051.
747 	 */
748 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_CC_IS_CLANG);
749 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
750 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
751 }
752 
753 /* Check that ksize() makes the whole object accessible. */
ksize_unpoisons_memory(struct kunit * test)754 static void ksize_unpoisons_memory(struct kunit *test)
755 {
756 	char *ptr;
757 	size_t size = 123, real_size;
758 
759 	ptr = kmalloc(size, GFP_KERNEL);
760 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
761 	real_size = ksize(ptr);
762 
763 	/* This access shouldn't trigger a KASAN report. */
764 	ptr[size] = 'x';
765 
766 	/* This one must. */
767 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[real_size]);
768 
769 	kfree(ptr);
770 }
771 
772 /*
773  * Check that a use-after-free is detected by ksize() and via normal accesses
774  * after it.
775  */
ksize_uaf(struct kunit * test)776 static void ksize_uaf(struct kunit *test)
777 {
778 	char *ptr;
779 	int size = 128 - KASAN_GRANULE_SIZE;
780 
781 	ptr = kmalloc(size, GFP_KERNEL);
782 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
783 	kfree(ptr);
784 
785 	KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
786 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
787 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);
788 }
789 
kasan_stack_oob(struct kunit * test)790 static void kasan_stack_oob(struct kunit *test)
791 {
792 	char stack_array[10];
793 	/* See comment in kasan_global_oob. */
794 	char *volatile array = stack_array;
795 	char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
796 
797 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
798 
799 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
800 }
801 
kasan_alloca_oob_left(struct kunit * test)802 static void kasan_alloca_oob_left(struct kunit *test)
803 {
804 	volatile int i = 10;
805 	char alloca_array[i];
806 	/* See comment in kasan_global_oob. */
807 	char *volatile array = alloca_array;
808 	char *p = array - 1;
809 
810 	/* Only generic mode instruments dynamic allocas. */
811 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
812 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
813 
814 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
815 }
816 
kasan_alloca_oob_right(struct kunit * test)817 static void kasan_alloca_oob_right(struct kunit *test)
818 {
819 	volatile int i = 10;
820 	char alloca_array[i];
821 	/* See comment in kasan_global_oob. */
822 	char *volatile array = alloca_array;
823 	char *p = array + i;
824 
825 	/* Only generic mode instruments dynamic allocas. */
826 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
827 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
828 
829 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
830 }
831 
kmem_cache_double_free(struct kunit * test)832 static void kmem_cache_double_free(struct kunit *test)
833 {
834 	char *p;
835 	size_t size = 200;
836 	struct kmem_cache *cache;
837 
838 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
839 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
840 
841 	p = kmem_cache_alloc(cache, GFP_KERNEL);
842 	if (!p) {
843 		kunit_err(test, "Allocation failed: %s\n", __func__);
844 		kmem_cache_destroy(cache);
845 		return;
846 	}
847 
848 	kmem_cache_free(cache, p);
849 	KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
850 	kmem_cache_destroy(cache);
851 }
852 
kmem_cache_invalid_free(struct kunit * test)853 static void kmem_cache_invalid_free(struct kunit *test)
854 {
855 	char *p;
856 	size_t size = 200;
857 	struct kmem_cache *cache;
858 
859 	cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
860 				  NULL);
861 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
862 
863 	p = kmem_cache_alloc(cache, GFP_KERNEL);
864 	if (!p) {
865 		kunit_err(test, "Allocation failed: %s\n", __func__);
866 		kmem_cache_destroy(cache);
867 		return;
868 	}
869 
870 	/* Trigger invalid free, the object doesn't get freed. */
871 	KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
872 
873 	/*
874 	 * Properly free the object to prevent the "Objects remaining in
875 	 * test_cache on __kmem_cache_shutdown" BUG failure.
876 	 */
877 	kmem_cache_free(cache, p);
878 
879 	kmem_cache_destroy(cache);
880 }
881 
empty_cache_ctor(void * object)882 static void empty_cache_ctor(void *object) { }
883 
kmem_cache_double_destroy(struct kunit * test)884 static void kmem_cache_double_destroy(struct kunit *test)
885 {
886 	struct kmem_cache *cache;
887 
888 	/* Provide a constructor to prevent cache merging. */
889 	cache = kmem_cache_create("test_cache", 200, 0, 0, empty_cache_ctor);
890 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
891 	kmem_cache_destroy(cache);
892 	KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_destroy(cache));
893 }
894 
kasan_memchr(struct kunit * test)895 static void kasan_memchr(struct kunit *test)
896 {
897 	char *ptr;
898 	size_t size = 24;
899 
900 	/*
901 	 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
902 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
903 	 */
904 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
905 
906 	if (OOB_TAG_OFF)
907 		size = round_up(size, OOB_TAG_OFF);
908 
909 	ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
910 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
911 
912 	OPTIMIZER_HIDE_VAR(ptr);
913 	OPTIMIZER_HIDE_VAR(size);
914 	KUNIT_EXPECT_KASAN_FAIL(test,
915 		kasan_ptr_result = memchr(ptr, '1', size + 1));
916 
917 	kfree(ptr);
918 }
919 
kasan_memcmp(struct kunit * test)920 static void kasan_memcmp(struct kunit *test)
921 {
922 	char *ptr;
923 	size_t size = 24;
924 	int arr[9];
925 
926 	/*
927 	 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
928 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
929 	 */
930 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
931 
932 	if (OOB_TAG_OFF)
933 		size = round_up(size, OOB_TAG_OFF);
934 
935 	ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
936 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
937 	memset(arr, 0, sizeof(arr));
938 
939 	OPTIMIZER_HIDE_VAR(ptr);
940 	OPTIMIZER_HIDE_VAR(size);
941 	KUNIT_EXPECT_KASAN_FAIL(test,
942 		kasan_int_result = memcmp(ptr, arr, size+1));
943 	kfree(ptr);
944 }
945 
kasan_strings(struct kunit * test)946 static void kasan_strings(struct kunit *test)
947 {
948 	char *ptr;
949 	size_t size = 24;
950 
951 	/*
952 	 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
953 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
954 	 */
955 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
956 
957 	ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
958 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
959 
960 	kfree(ptr);
961 
962 	/*
963 	 * Try to cause only 1 invalid access (less spam in dmesg).
964 	 * For that we need ptr to point to zeroed byte.
965 	 * Skip metadata that could be stored in freed object so ptr
966 	 * will likely point to zeroed byte.
967 	 */
968 	ptr += 16;
969 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
970 
971 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
972 
973 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
974 
975 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
976 
977 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
978 
979 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
980 }
981 
kasan_bitops_modify(struct kunit * test,int nr,void * addr)982 static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
983 {
984 	KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
985 	KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
986 	KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
987 	KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
988 	KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
989 	KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
990 	KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
991 	KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
992 }
993 
kasan_bitops_test_and_modify(struct kunit * test,int nr,void * addr)994 static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
995 {
996 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
997 	KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
998 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
999 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
1000 	KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
1001 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
1002 	KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
1003 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
1004 
1005 #if defined(clear_bit_unlock_is_negative_byte)
1006 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
1007 				clear_bit_unlock_is_negative_byte(nr, addr));
1008 #endif
1009 }
1010 
kasan_bitops_generic(struct kunit * test)1011 static void kasan_bitops_generic(struct kunit *test)
1012 {
1013 	long *bits;
1014 
1015 	/* This test is specifically crafted for the generic mode. */
1016 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
1017 
1018 	/*
1019 	 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
1020 	 * this way we do not actually corrupt other memory.
1021 	 */
1022 	bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
1023 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
1024 
1025 	/*
1026 	 * Below calls try to access bit within allocated memory; however, the
1027 	 * below accesses are still out-of-bounds, since bitops are defined to
1028 	 * operate on the whole long the bit is in.
1029 	 */
1030 	kasan_bitops_modify(test, BITS_PER_LONG, bits);
1031 
1032 	/*
1033 	 * Below calls try to access bit beyond allocated memory.
1034 	 */
1035 	kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
1036 
1037 	kfree(bits);
1038 }
1039 
kasan_bitops_tags(struct kunit * test)1040 static void kasan_bitops_tags(struct kunit *test)
1041 {
1042 	long *bits;
1043 
1044 	/* This test is specifically crafted for tag-based modes. */
1045 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1046 
1047 	/* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
1048 	bits = kzalloc(48, GFP_KERNEL);
1049 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
1050 
1051 	/* Do the accesses past the 48 allocated bytes, but within the redone. */
1052 	kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
1053 	kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
1054 
1055 	kfree(bits);
1056 }
1057 
kmalloc_double_kzfree(struct kunit * test)1058 static void kmalloc_double_kzfree(struct kunit *test)
1059 {
1060 	char *ptr;
1061 	size_t size = 16;
1062 
1063 	ptr = kmalloc(size, GFP_KERNEL);
1064 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1065 
1066 	kfree_sensitive(ptr);
1067 	KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
1068 }
1069 
vmalloc_helpers_tags(struct kunit * test)1070 static void vmalloc_helpers_tags(struct kunit *test)
1071 {
1072 	void *ptr;
1073 
1074 	/* This test is intended for tag-based modes. */
1075 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1076 
1077 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1078 
1079 	ptr = vmalloc(PAGE_SIZE);
1080 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1081 
1082 	/* Check that the returned pointer is tagged. */
1083 	KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1084 	KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1085 
1086 	/* Make sure exported vmalloc helpers handle tagged pointers. */
1087 	KUNIT_ASSERT_TRUE(test, is_vmalloc_addr(ptr));
1088 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, vmalloc_to_page(ptr));
1089 
1090 #if !IS_MODULE(CONFIG_KASAN_KUNIT_TEST)
1091 	{
1092 		int rv;
1093 
1094 		/* Make sure vmalloc'ed memory permissions can be changed. */
1095 		rv = set_memory_ro((unsigned long)ptr, 1);
1096 		KUNIT_ASSERT_GE(test, rv, 0);
1097 		rv = set_memory_rw((unsigned long)ptr, 1);
1098 		KUNIT_ASSERT_GE(test, rv, 0);
1099 	}
1100 #endif
1101 
1102 	vfree(ptr);
1103 }
1104 
vmalloc_oob(struct kunit * test)1105 static void vmalloc_oob(struct kunit *test)
1106 {
1107 	char *v_ptr, *p_ptr;
1108 	struct page *page;
1109 	size_t size = PAGE_SIZE / 2 - KASAN_GRANULE_SIZE - 5;
1110 
1111 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1112 
1113 	v_ptr = vmalloc(size);
1114 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1115 
1116 	OPTIMIZER_HIDE_VAR(v_ptr);
1117 
1118 	/*
1119 	 * We have to be careful not to hit the guard page in vmalloc tests.
1120 	 * The MMU will catch that and crash us.
1121 	 */
1122 
1123 	/* Make sure in-bounds accesses are valid. */
1124 	v_ptr[0] = 0;
1125 	v_ptr[size - 1] = 0;
1126 
1127 	/*
1128 	 * An unaligned access past the requested vmalloc size.
1129 	 * Only generic KASAN can precisely detect these.
1130 	 */
1131 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
1132 		KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)v_ptr)[size]);
1133 
1134 	/* An aligned access into the first out-of-bounds granule. */
1135 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)v_ptr)[size + 5]);
1136 
1137 	/* Check that in-bounds accesses to the physical page are valid. */
1138 	page = vmalloc_to_page(v_ptr);
1139 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, page);
1140 	p_ptr = page_address(page);
1141 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_ptr);
1142 	p_ptr[0] = 0;
1143 
1144 	vfree(v_ptr);
1145 
1146 	/*
1147 	 * We can't check for use-after-unmap bugs in this nor in the following
1148 	 * vmalloc tests, as the page might be fully unmapped and accessing it
1149 	 * will crash the kernel.
1150 	 */
1151 }
1152 
vmap_tags(struct kunit * test)1153 static void vmap_tags(struct kunit *test)
1154 {
1155 	char *p_ptr, *v_ptr;
1156 	struct page *p_page, *v_page;
1157 
1158 	/*
1159 	 * This test is specifically crafted for the software tag-based mode,
1160 	 * the only tag-based mode that poisons vmap mappings.
1161 	 */
1162 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_SW_TAGS);
1163 
1164 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
1165 
1166 	p_page = alloc_pages(GFP_KERNEL, 1);
1167 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_page);
1168 	p_ptr = page_address(p_page);
1169 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_ptr);
1170 
1171 	v_ptr = vmap(&p_page, 1, VM_MAP, PAGE_KERNEL);
1172 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1173 
1174 	/*
1175 	 * We can't check for out-of-bounds bugs in this nor in the following
1176 	 * vmalloc tests, as allocations have page granularity and accessing
1177 	 * the guard page will crash the kernel.
1178 	 */
1179 
1180 	KUNIT_EXPECT_GE(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_MIN);
1181 	KUNIT_EXPECT_LT(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_KERNEL);
1182 
1183 	/* Make sure that in-bounds accesses through both pointers work. */
1184 	*p_ptr = 0;
1185 	*v_ptr = 0;
1186 
1187 	/* Make sure vmalloc_to_page() correctly recovers the page pointer. */
1188 	v_page = vmalloc_to_page(v_ptr);
1189 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_page);
1190 	KUNIT_EXPECT_PTR_EQ(test, p_page, v_page);
1191 
1192 	vunmap(v_ptr);
1193 	free_pages((unsigned long)p_ptr, 1);
1194 }
1195 
vm_map_ram_tags(struct kunit * test)1196 static void vm_map_ram_tags(struct kunit *test)
1197 {
1198 	char *p_ptr, *v_ptr;
1199 	struct page *page;
1200 
1201 	/*
1202 	 * This test is specifically crafted for the software tag-based mode,
1203 	 * the only tag-based mode that poisons vm_map_ram mappings.
1204 	 */
1205 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_SW_TAGS);
1206 
1207 	page = alloc_pages(GFP_KERNEL, 1);
1208 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, page);
1209 	p_ptr = page_address(page);
1210 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p_ptr);
1211 
1212 	v_ptr = vm_map_ram(&page, 1, -1);
1213 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1214 
1215 	KUNIT_EXPECT_GE(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_MIN);
1216 	KUNIT_EXPECT_LT(test, (u8)get_tag(v_ptr), (u8)KASAN_TAG_KERNEL);
1217 
1218 	/* Make sure that in-bounds accesses through both pointers work. */
1219 	*p_ptr = 0;
1220 	*v_ptr = 0;
1221 
1222 	vm_unmap_ram(v_ptr, 1);
1223 	free_pages((unsigned long)p_ptr, 1);
1224 }
1225 
vmalloc_percpu(struct kunit * test)1226 static void vmalloc_percpu(struct kunit *test)
1227 {
1228 	char __percpu *ptr;
1229 	int cpu;
1230 
1231 	/*
1232 	 * This test is specifically crafted for the software tag-based mode,
1233 	 * the only tag-based mode that poisons percpu mappings.
1234 	 */
1235 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_SW_TAGS);
1236 
1237 	ptr = __alloc_percpu(PAGE_SIZE, PAGE_SIZE);
1238 
1239 	for_each_possible_cpu(cpu) {
1240 		char *c_ptr = per_cpu_ptr(ptr, cpu);
1241 
1242 		KUNIT_EXPECT_GE(test, (u8)get_tag(c_ptr), (u8)KASAN_TAG_MIN);
1243 		KUNIT_EXPECT_LT(test, (u8)get_tag(c_ptr), (u8)KASAN_TAG_KERNEL);
1244 
1245 		/* Make sure that in-bounds accesses don't crash the kernel. */
1246 		*c_ptr = 0;
1247 	}
1248 
1249 	free_percpu(ptr);
1250 }
1251 
1252 /*
1253  * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
1254  * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
1255  * modes.
1256  */
match_all_not_assigned(struct kunit * test)1257 static void match_all_not_assigned(struct kunit *test)
1258 {
1259 	char *ptr;
1260 	struct page *pages;
1261 	int i, size, order;
1262 
1263 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1264 
1265 	for (i = 0; i < 256; i++) {
1266 		size = (get_random_int() % 1024) + 1;
1267 		ptr = kmalloc(size, GFP_KERNEL);
1268 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1269 		KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1270 		KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1271 		kfree(ptr);
1272 	}
1273 
1274 	for (i = 0; i < 256; i++) {
1275 		order = (get_random_int() % 4) + 1;
1276 		pages = alloc_pages(GFP_KERNEL, order);
1277 		ptr = page_address(pages);
1278 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1279 		KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1280 		KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1281 		free_pages((unsigned long)ptr, order);
1282 	}
1283 
1284 	if (!IS_ENABLED(CONFIG_KASAN_VMALLOC))
1285 		return;
1286 
1287 	for (i = 0; i < 256; i++) {
1288 		size = (get_random_int() % 1024) + 1;
1289 		ptr = vmalloc(size);
1290 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1291 		KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1292 		KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1293 		vfree(ptr);
1294 	}
1295 }
1296 
1297 /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
match_all_ptr_tag(struct kunit * test)1298 static void match_all_ptr_tag(struct kunit *test)
1299 {
1300 	char *ptr;
1301 	u8 tag;
1302 
1303 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1304 
1305 	ptr = kmalloc(128, GFP_KERNEL);
1306 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1307 
1308 	/* Backup the assigned tag. */
1309 	tag = get_tag(ptr);
1310 	KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1311 
1312 	/* Reset the tag to 0xff.*/
1313 	ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1314 
1315 	/* This access shouldn't trigger a KASAN report. */
1316 	*ptr = 0;
1317 
1318 	/* Recover the pointer tag and free. */
1319 	ptr = set_tag(ptr, tag);
1320 	kfree(ptr);
1321 }
1322 
1323 /* Check that there are no match-all memory tags for tag-based modes. */
match_all_mem_tag(struct kunit * test)1324 static void match_all_mem_tag(struct kunit *test)
1325 {
1326 	char *ptr;
1327 	int tag;
1328 
1329 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1330 
1331 	ptr = kmalloc(128, GFP_KERNEL);
1332 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1333 	KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1334 
1335 	/* For each possible tag value not matching the pointer tag. */
1336 	for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1337 		if (tag == get_tag(ptr))
1338 			continue;
1339 
1340 		/* Mark the first memory granule with the chosen memory tag. */
1341 		kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
1342 
1343 		/* This access must cause a KASAN report. */
1344 		KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1345 	}
1346 
1347 	/* Recover the memory tag and free. */
1348 	kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
1349 	kfree(ptr);
1350 }
1351 
1352 static struct kunit_case kasan_kunit_test_cases[] = {
1353 	KUNIT_CASE(kmalloc_oob_right),
1354 	KUNIT_CASE(kmalloc_oob_left),
1355 	KUNIT_CASE(kmalloc_node_oob_right),
1356 	KUNIT_CASE(kmalloc_pagealloc_oob_right),
1357 	KUNIT_CASE(kmalloc_pagealloc_uaf),
1358 	KUNIT_CASE(kmalloc_pagealloc_invalid_free),
1359 	KUNIT_CASE(pagealloc_oob_right),
1360 	KUNIT_CASE(pagealloc_uaf),
1361 	KUNIT_CASE(kmalloc_large_oob_right),
1362 	KUNIT_CASE(krealloc_more_oob),
1363 	KUNIT_CASE(krealloc_less_oob),
1364 	KUNIT_CASE(krealloc_pagealloc_more_oob),
1365 	KUNIT_CASE(krealloc_pagealloc_less_oob),
1366 	KUNIT_CASE(krealloc_uaf),
1367 	KUNIT_CASE(kmalloc_oob_16),
1368 	KUNIT_CASE(kmalloc_uaf_16),
1369 	KUNIT_CASE(kmalloc_oob_in_memset),
1370 	KUNIT_CASE(kmalloc_oob_memset_2),
1371 	KUNIT_CASE(kmalloc_oob_memset_4),
1372 	KUNIT_CASE(kmalloc_oob_memset_8),
1373 	KUNIT_CASE(kmalloc_oob_memset_16),
1374 	KUNIT_CASE(kmalloc_memmove_negative_size),
1375 	KUNIT_CASE(kmalloc_memmove_invalid_size),
1376 	KUNIT_CASE(kmalloc_uaf),
1377 	KUNIT_CASE(kmalloc_uaf_memset),
1378 	KUNIT_CASE(kmalloc_uaf2),
1379 	KUNIT_CASE(kfree_via_page),
1380 	KUNIT_CASE(kfree_via_phys),
1381 	KUNIT_CASE(kmem_cache_oob),
1382 	KUNIT_CASE(kmem_cache_accounted),
1383 	KUNIT_CASE(kmem_cache_bulk),
1384 	KUNIT_CASE(kasan_global_oob_right),
1385 	KUNIT_CASE(kasan_global_oob_left),
1386 	KUNIT_CASE(kasan_stack_oob),
1387 	KUNIT_CASE(kasan_alloca_oob_left),
1388 	KUNIT_CASE(kasan_alloca_oob_right),
1389 	KUNIT_CASE(ksize_unpoisons_memory),
1390 	KUNIT_CASE(ksize_uaf),
1391 	KUNIT_CASE(kmem_cache_double_free),
1392 	KUNIT_CASE(kmem_cache_invalid_free),
1393 	KUNIT_CASE(kmem_cache_double_destroy),
1394 	KUNIT_CASE(kasan_memchr),
1395 	KUNIT_CASE(kasan_memcmp),
1396 	KUNIT_CASE(kasan_strings),
1397 	KUNIT_CASE(kasan_bitops_generic),
1398 	KUNIT_CASE(kasan_bitops_tags),
1399 	KUNIT_CASE(kmalloc_double_kzfree),
1400 	KUNIT_CASE(vmalloc_helpers_tags),
1401 	KUNIT_CASE(vmalloc_oob),
1402 	KUNIT_CASE(vmap_tags),
1403 	KUNIT_CASE(vm_map_ram_tags),
1404 	KUNIT_CASE(vmalloc_percpu),
1405 	KUNIT_CASE(match_all_not_assigned),
1406 	KUNIT_CASE(match_all_ptr_tag),
1407 	KUNIT_CASE(match_all_mem_tag),
1408 	{}
1409 };
1410 
1411 static struct kunit_suite kasan_kunit_test_suite = {
1412 	.name = "kasan",
1413 	.init = kasan_test_init,
1414 	.test_cases = kasan_kunit_test_cases,
1415 	.exit = kasan_test_exit,
1416 };
1417 
1418 kunit_test_suite(kasan_kunit_test_suite);
1419 
1420 MODULE_LICENSE("GPL");
1421