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