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