blob: 8be9d4b3b259c64ccb85c505c56d35c50fd5a9d0 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrey Ryabinin3f158012015-02-13 14:39:53 -08002/*
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
Andrey Ryabinin3f158012015-02-13 14:39:53 -08006 */
7
Marco Elver19a33ca2019-07-11 20:53:52 -07008#include <linux/bitops.h>
Greg Thelen0386bf32017-02-24 15:00:08 -08009#include <linux/delay.h>
Marco Elver19a33ca2019-07-11 20:53:52 -070010#include <linux/kasan.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080011#include <linux/kernel.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070012#include <linux/mm.h>
Marco Elver19a33ca2019-07-11 20:53:52 -070013#include <linux/mman.h>
14#include <linux/module.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080015#include <linux/printk.h>
Andrey Konovalov573a4802021-02-24 12:05:21 -080016#include <linux/random.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080017#include <linux/slab.h>
18#include <linux/string.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070019#include <linux/uaccess.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070020#include <linux/io.h>
Daniel Axtens06513912019-11-30 17:54:53 -080021#include <linux/vmalloc.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070022
23#include <asm/page.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080024
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070025#include <kunit/test.h>
26
Walter Wuf33a0142020-08-06 23:24:54 -070027#include "../mm/kasan/kasan.h"
28
Andrey Konovalov1f600622020-12-22 12:00:24 -080029#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
Walter Wuf33a0142020-08-06 23:24:54 -070030
Dmitry Vyukov828347f2016-11-30 15:54:16 -080031/*
Andrey Konovalov0fd37922021-02-24 12:05:13 -080032 * Some tests use these global variables to store return values from function
33 * calls that could otherwise be eliminated by the compiler as dead code.
Daniel Axtensadb72ae2020-06-03 15:56:43 -070034 */
Daniel Axtensadb72ae2020-06-03 15:56:43 -070035void *kasan_ptr_result;
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070036int kasan_int_result;
37
38static struct kunit_resource resource;
39static struct kunit_kasan_expectation fail_data;
40static bool multishot;
41
Andrey Konovalov0fd37922021-02-24 12:05:13 -080042/*
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
Andrey Konovalovf05842c2021-02-24 12:05:26 -080044 * first detected bug and panic the kernel if panic_on_warn is enabled. For
45 * hardware tag-based KASAN also allow tag checking to be reenabled for each
46 * test, see the comment for KUNIT_EXPECT_KASAN_FAIL().
Andrey Konovalov0fd37922021-02-24 12:05:13 -080047 */
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070048static int kasan_test_init(struct kunit *test)
49{
Andrey Konovalovd82dc3a2021-02-24 12:06:02 -080050 if (!kasan_enabled()) {
51 kunit_err(test, "can't run KASAN tests with KASAN disabled");
52 return -1;
53 }
54
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070055 multishot = kasan_save_enable_multi_shot();
Andrey Konovalov99734b52021-04-29 23:00:49 -070056 fail_data.report_found = false;
Andrey Konovalov99734b52021-04-29 23:00:49 -070057 kunit_add_named_resource(test, NULL, NULL, &resource,
58 "kasan_data", &fail_data);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070059 return 0;
60}
61
62static void kasan_test_exit(struct kunit *test)
63{
64 kasan_restore_multi_shot(multishot);
Andrey Konovalov99734b52021-04-29 23:00:49 -070065 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070066}
67
68/**
Andrey Konovalov0fd37922021-02-24 12:05:13 -080069 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
70 * KASAN report; causes a test failure otherwise. This relies on a KUnit
71 * resource named "kasan_data". Do not use this name for KUnit resources
72 * outside of KASAN tests.
Andrey Konovalovf05842c2021-02-24 12:05:26 -080073 *
Andrey Konovalove80a76a2021-03-15 13:20:19 +000074 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
75 * checking is auto-disabled. When this happens, this test handler reenables
76 * tag checking. As tag checking can be only disabled or enabled per CPU,
77 * this handler disables migration (preemption).
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080078 *
79 * Since the compiler doesn't see that the expression can change the fail_data
80 * fields, it can reorder or optimize away the accesses to those fields.
81 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
82 * expression to prevent that.
Andrey Konovalov99734b52021-04-29 23:00:49 -070083 *
84 * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
85 * false. This allows detecting KASAN reports that happen outside of the checks
86 * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
87 * and in kasan_test_exit.
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070088 */
Andrey Konovalov99734b52021-04-29 23:00:49 -070089#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
90 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
91 !kasan_async_mode_enabled()) \
92 migrate_disable(); \
93 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
Andrey Konovalov99734b52021-04-29 23:00:49 -070094 barrier(); \
95 expression; \
96 barrier(); \
David Gow3ff16d32021-06-28 19:40:36 -070097 if (!READ_ONCE(fail_data.report_found)) { \
98 KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure " \
99 "expected in \"" #expression \
100 "\", but none occurred"); \
101 } \
Andrey Konovalov99734b52021-04-29 23:00:49 -0700102 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
103 if (READ_ONCE(fail_data.report_found)) \
104 kasan_enable_tagging_sync(); \
105 migrate_enable(); \
106 } \
107 WRITE_ONCE(fail_data.report_found, false); \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700108} while (0)
109
Andrey Konovalovda17e372021-02-24 12:05:17 -0800110#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
Marco Elver40eb5cf2021-06-24 23:58:15 -0700111 if (!IS_ENABLED(config)) \
112 kunit_skip((test), "Test requires " #config "=y"); \
Andrey Konovalovda17e372021-02-24 12:05:17 -0800113} while (0)
114
115#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
Marco Elver40eb5cf2021-06-24 23:58:15 -0700116 if (IS_ENABLED(config)) \
117 kunit_skip((test), "Test requires " #config "=n"); \
Andrey Konovalovda17e372021-02-24 12:05:17 -0800118} while (0)
119
Patricia Alfonso73228c72020-10-13 16:55:06 -0700120static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800121{
122 char *ptr;
123 size_t size = 123;
124
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800125 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700126 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800127
Patricia Alfonso73228c72020-10-13 16:55:06 -0700128 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800129 kfree(ptr);
130}
131
Patricia Alfonso73228c72020-10-13 16:55:06 -0700132static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800133{
134 char *ptr;
135 size_t size = 15;
136
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800137 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700138 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800139
Patricia Alfonso73228c72020-10-13 16:55:06 -0700140 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800141 kfree(ptr);
142}
143
Patricia Alfonso73228c72020-10-13 16:55:06 -0700144static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800145{
146 char *ptr;
147 size_t size = 4096;
148
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800149 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700150 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800151
Patricia Alfonso73228c72020-10-13 16:55:06 -0700152 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800153 kfree(ptr);
154}
155
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800156/*
157 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
158 * fit into a slab cache and therefore is allocated via the page allocator
159 * fallback. Since this kind of fallback is only implemented for SLUB, these
160 * tests are limited to that allocator.
161 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700162static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800163{
164 char *ptr;
165 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
166
Andrey Konovalovda17e372021-02-24 12:05:17 -0800167 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700168
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700169 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700170 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700171
Patricia Alfonso73228c72020-10-13 16:55:06 -0700172 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800173
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700174 kfree(ptr);
175}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800176
Patricia Alfonso73228c72020-10-13 16:55:06 -0700177static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800178{
179 char *ptr;
180 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
181
Andrey Konovalovda17e372021-02-24 12:05:17 -0800182 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800183
Patricia Alfonso73228c72020-10-13 16:55:06 -0700184 ptr = kmalloc(size, GFP_KERNEL);
185 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800186 kfree(ptr);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800187
Patricia Alfonso73228c72020-10-13 16:55:06 -0700188 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800189}
190
Patricia Alfonso73228c72020-10-13 16:55:06 -0700191static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800192{
193 char *ptr;
194 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
195
Andrey Konovalovda17e372021-02-24 12:05:17 -0800196 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800197
Patricia Alfonso73228c72020-10-13 16:55:06 -0700198 ptr = kmalloc(size, GFP_KERNEL);
199 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700200
Patricia Alfonso73228c72020-10-13 16:55:06 -0700201 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
202}
203
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800204static void pagealloc_oob_right(struct kunit *test)
205{
206 char *ptr;
207 struct page *pages;
208 size_t order = 4;
209 size_t size = (1UL << (PAGE_SHIFT + order));
210
211 /*
212 * With generic KASAN page allocations have no redzones, thus
213 * out-of-bounds detection is not guaranteed.
214 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
215 */
216 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
217
218 pages = alloc_pages(GFP_KERNEL, order);
219 ptr = page_address(pages);
220 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
221
222 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
223 free_pages((unsigned long)ptr, order);
224}
225
226static void pagealloc_uaf(struct kunit *test)
227{
228 char *ptr;
229 struct page *pages;
230 size_t order = 4;
231
232 pages = alloc_pages(GFP_KERNEL, order);
233 ptr = page_address(pages);
234 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
235 free_pages((unsigned long)ptr, order);
236
237 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
238}
239
Patricia Alfonso73228c72020-10-13 16:55:06 -0700240static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700241{
242 char *ptr;
243 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800244
245 /*
246 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700247 * and does not trigger the page allocator fallback in SLUB.
248 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800249 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700250 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800251
Patricia Alfonso73228c72020-10-13 16:55:06 -0700252 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800253 kfree(ptr);
254}
255
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800256static void krealloc_more_oob_helper(struct kunit *test,
257 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800258{
259 char *ptr1, *ptr2;
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800260 size_t middle;
261
262 KUNIT_ASSERT_LT(test, size1, size2);
263 middle = size1 + (size2 - size1) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800264
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800265 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700266 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
267
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800268 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700269 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800270
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800271 /* All offsets up to size2 must be accessible. */
272 ptr2[size1 - 1] = 'x';
273 ptr2[size1] = 'x';
274 ptr2[middle] = 'x';
275 ptr2[size2 - 1] = 'x';
276
277 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
278 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
279 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
280
281 /* For all modes first aligned offset after size2 must be inaccessible. */
282 KUNIT_EXPECT_KASAN_FAIL(test,
283 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
284
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800285 kfree(ptr2);
286}
287
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800288static void krealloc_less_oob_helper(struct kunit *test,
289 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800290{
291 char *ptr1, *ptr2;
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800292 size_t middle;
293
294 KUNIT_ASSERT_LT(test, size2, size1);
295 middle = size2 + (size1 - size2) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800296
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800297 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700298 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
299
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800300 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700301 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700302
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800303 /* Must be accessible for all modes. */
304 ptr2[size2 - 1] = 'x';
305
306 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
307 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
308 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
309
310 /* For all modes first aligned offset after size2 must be inaccessible. */
311 KUNIT_EXPECT_KASAN_FAIL(test,
312 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
313
314 /*
315 * For all modes all size2, middle, and size1 should land in separate
316 * granules and thus the latter two offsets should be inaccessible.
317 */
318 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
319 round_down(middle, KASAN_GRANULE_SIZE));
320 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
321 round_down(size1, KASAN_GRANULE_SIZE));
322 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
323 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
324 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
325
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800326 kfree(ptr2);
327}
328
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800329static void krealloc_more_oob(struct kunit *test)
330{
331 krealloc_more_oob_helper(test, 201, 235);
332}
333
334static void krealloc_less_oob(struct kunit *test)
335{
336 krealloc_less_oob_helper(test, 235, 201);
337}
338
339static void krealloc_pagealloc_more_oob(struct kunit *test)
340{
341 /* page_alloc fallback in only implemented for SLUB. */
342 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
343
344 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
345 KMALLOC_MAX_CACHE_SIZE + 235);
346}
347
348static void krealloc_pagealloc_less_oob(struct kunit *test)
349{
350 /* page_alloc fallback in only implemented for SLUB. */
351 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
352
353 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
354 KMALLOC_MAX_CACHE_SIZE + 201);
355}
356
Andrey Konovalov26a5ca72021-02-25 17:20:19 -0800357/*
358 * Check that krealloc() detects a use-after-free, returns NULL,
359 * and doesn't unpoison the freed object.
360 */
361static void krealloc_uaf(struct kunit *test)
362{
363 char *ptr1, *ptr2;
364 int size1 = 201;
365 int size2 = 235;
366
367 ptr1 = kmalloc(size1, GFP_KERNEL);
368 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
369 kfree(ptr1);
370
371 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
372 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
373 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
374}
375
Patricia Alfonso73228c72020-10-13 16:55:06 -0700376static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800377{
378 struct {
379 u64 words[2];
380 } *ptr1, *ptr2;
381
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800382 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800383 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800384
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800385 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700386 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
387
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800388 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700389 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
390
391 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800392 kfree(ptr1);
393 kfree(ptr2);
394}
395
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800396static void kmalloc_uaf_16(struct kunit *test)
397{
398 struct {
399 u64 words[2];
400 } *ptr1, *ptr2;
401
402 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
403 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
404
405 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
406 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
407 kfree(ptr2);
408
409 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
410 kfree(ptr1);
411}
412
Patricia Alfonso73228c72020-10-13 16:55:06 -0700413static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800414{
415 char *ptr;
416 size_t size = 8;
417
Wang Longf523e732015-11-05 18:51:15 -0800418 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700419 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800420
Patricia Alfonso73228c72020-10-13 16:55:06 -0700421 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800422 kfree(ptr);
423}
424
Patricia Alfonso73228c72020-10-13 16:55:06 -0700425static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800426{
427 char *ptr;
428 size_t size = 8;
429
Wang Longf523e732015-11-05 18:51:15 -0800430 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700431 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800432
Patricia Alfonso73228c72020-10-13 16:55:06 -0700433 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800434 kfree(ptr);
435}
436
437
Patricia Alfonso73228c72020-10-13 16:55:06 -0700438static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800439{
440 char *ptr;
441 size_t size = 8;
442
Wang Longf523e732015-11-05 18:51:15 -0800443 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700444 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800445
Patricia Alfonso73228c72020-10-13 16:55:06 -0700446 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800447 kfree(ptr);
448}
449
Patricia Alfonso73228c72020-10-13 16:55:06 -0700450static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800451{
452 char *ptr;
453 size_t size = 16;
454
Wang Longf523e732015-11-05 18:51:15 -0800455 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700456 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800457
Patricia Alfonso73228c72020-10-13 16:55:06 -0700458 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800459 kfree(ptr);
460}
461
Patricia Alfonso73228c72020-10-13 16:55:06 -0700462static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800463{
464 char *ptr;
465 size_t size = 666;
466
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800467 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700468 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800469
Patricia Alfonso73228c72020-10-13 16:55:06 -0700470 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800471 kfree(ptr);
472}
473
Patricia Alfonso73228c72020-10-13 16:55:06 -0700474static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700475{
476 char *ptr;
477 size_t size = 64;
478 volatile size_t invalid_size = -2;
479
Walter Wu98f3b562020-04-01 21:09:40 -0700480 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700481 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700482
483 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700484
485 KUNIT_EXPECT_KASAN_FAIL(test,
486 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700487 kfree(ptr);
488}
489
Patricia Alfonso73228c72020-10-13 16:55:06 -0700490static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800491{
492 char *ptr;
493 size_t size = 10;
494
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800495 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700496 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800497
498 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700499 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800500}
501
Patricia Alfonso73228c72020-10-13 16:55:06 -0700502static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800503{
504 char *ptr;
505 size_t size = 33;
506
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800507 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700508 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800509
510 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700511 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800512}
513
Patricia Alfonso73228c72020-10-13 16:55:06 -0700514static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800515{
516 char *ptr1, *ptr2;
517 size_t size = 43;
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800518 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800519
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800520again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800521 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700522 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800523
524 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800525
Patricia Alfonso73228c72020-10-13 16:55:06 -0700526 ptr2 = kmalloc(size, GFP_KERNEL);
527 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
528
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800529 /*
530 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
531 * Allow up to 16 attempts at generating different tags.
532 */
533 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
534 kfree(ptr2);
535 goto again;
536 }
537
Patricia Alfonso73228c72020-10-13 16:55:06 -0700538 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
539 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
540
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800541 kfree(ptr2);
542}
543
Patricia Alfonso73228c72020-10-13 16:55:06 -0700544static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700545{
546 char *ptr;
547 size_t size = 8;
548 struct page *page;
549 unsigned long offset;
550
Mark Rutlandb92a9532019-09-23 15:34:16 -0700551 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700552 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700553
554 page = virt_to_page(ptr);
555 offset = offset_in_page(ptr);
556 kfree(page_address(page) + offset);
557}
558
Patricia Alfonso73228c72020-10-13 16:55:06 -0700559static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700560{
561 char *ptr;
562 size_t size = 8;
563 phys_addr_t phys;
564
Mark Rutlandb92a9532019-09-23 15:34:16 -0700565 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700566 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700567
568 phys = virt_to_phys(ptr);
569 kfree(phys_to_virt(phys));
570}
571
Patricia Alfonso73228c72020-10-13 16:55:06 -0700572static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800573{
574 char *p;
575 size_t size = 200;
Andrey Konovalov11516132021-02-24 12:05:59 -0800576 struct kmem_cache *cache;
577
578 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700579 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov11516132021-02-24 12:05:59 -0800580
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800581 p = kmem_cache_alloc(cache, GFP_KERNEL);
582 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700583 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800584 kmem_cache_destroy(cache);
585 return;
586 }
587
Patricia Alfonso73228c72020-10-13 16:55:06 -0700588 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov11516132021-02-24 12:05:59 -0800589
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800590 kmem_cache_free(cache, p);
591 kmem_cache_destroy(cache);
592}
593
Andrey Konovalov11516132021-02-24 12:05:59 -0800594static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800595{
596 int i;
597 char *p;
598 size_t size = 200;
599 struct kmem_cache *cache;
600
601 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700602 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800603
Greg Thelen0386bf32017-02-24 15:00:08 -0800604 /*
605 * Several allocations with a delay to allow for lazy per memcg kmem
606 * cache creation.
607 */
608 for (i = 0; i < 5; i++) {
609 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800610 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800611 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800612
Greg Thelen0386bf32017-02-24 15:00:08 -0800613 kmem_cache_free(cache, p);
614 msleep(100);
615 }
616
617free_cache:
618 kmem_cache_destroy(cache);
619}
620
Andrey Konovalov11516132021-02-24 12:05:59 -0800621static void kmem_cache_bulk(struct kunit *test)
622{
623 struct kmem_cache *cache;
624 size_t size = 200;
625 char *p[10];
626 bool ret;
627 int i;
628
629 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
630 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
631
632 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
633 if (!ret) {
634 kunit_err(test, "Allocation failed: %s\n", __func__);
635 kmem_cache_destroy(cache);
636 return;
637 }
638
639 for (i = 0; i < ARRAY_SIZE(p); i++)
640 p[i][0] = p[i][size - 1] = 42;
641
642 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
643 kmem_cache_destroy(cache);
644}
645
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800646static char global_array[10];
647
Patricia Alfonso73228c72020-10-13 16:55:06 -0700648static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800649{
Peter Collingbournef649dc02021-05-14 17:27:27 -0700650 /*
651 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
Zhen Lei53b0fe32021-07-07 18:07:28 -0700652 * from failing here and panicking the kernel, access the array via a
Peter Collingbournef649dc02021-05-14 17:27:27 -0700653 * volatile pointer, which will prevent the compiler from being able to
654 * determine the array bounds.
655 *
656 * This access uses a volatile pointer to char (char *volatile) rather
657 * than the more conventional pointer to volatile char (volatile char *)
658 * because we want to prevent the compiler from making inferences about
659 * the pointer itself (i.e. its array bounds), not the data that it
660 * refers to.
661 */
662 char *volatile array = global_array;
663 char *p = &array[ARRAY_SIZE(global_array) + 3];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800664
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800665 /* Only generic mode instruments globals. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800666 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800667
Patricia Alfonso73228c72020-10-13 16:55:06 -0700668 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800669}
670
Andrey Konovalov611806b2021-02-24 12:05:50 -0800671/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700672static void ksize_unpoisons_memory(struct kunit *test)
673{
674 char *ptr;
675 size_t size = 123, real_size;
676
677 ptr = kmalloc(size, GFP_KERNEL);
678 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
679 real_size = ksize(ptr);
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800680
681 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700682 ptr[size] = 'x';
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800683
684 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700685 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800686
Patricia Alfonso73228c72020-10-13 16:55:06 -0700687 kfree(ptr);
688}
689
Andrey Konovalov611806b2021-02-24 12:05:50 -0800690/*
691 * Check that a use-after-free is detected by ksize() and via normal accesses
692 * after it.
693 */
694static void ksize_uaf(struct kunit *test)
695{
696 char *ptr;
697 int size = 128 - KASAN_GRANULE_SIZE;
698
699 ptr = kmalloc(size, GFP_KERNEL);
700 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
701 kfree(ptr);
702
703 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
704 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
705 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
706}
707
Patricia Alfonso73228c72020-10-13 16:55:06 -0700708static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800709{
710 char stack_array[10];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700711 /* See comment in kasan_global_oob. */
712 char *volatile array = stack_array;
713 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800714
Andrey Konovalovda17e372021-02-24 12:05:17 -0800715 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700716
Patricia Alfonso73228c72020-10-13 16:55:06 -0700717 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700718}
719
Patricia Alfonso73228c72020-10-13 16:55:06 -0700720static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800721{
722 volatile int i = 10;
723 char alloca_array[i];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700724 /* See comment in kasan_global_oob. */
725 char *volatile array = alloca_array;
726 char *p = array - 1;
Paul Lawrence00a14292018-02-06 15:36:16 -0800727
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800728 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800729 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
730 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700731
732 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800733}
734
Patricia Alfonso73228c72020-10-13 16:55:06 -0700735static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800736{
737 volatile int i = 10;
738 char alloca_array[i];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700739 /* See comment in kasan_global_oob. */
740 char *volatile array = alloca_array;
741 char *p = array + i;
Paul Lawrence00a14292018-02-06 15:36:16 -0800742
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800743 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800744 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
745 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700746
747 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800748}
749
Patricia Alfonso73228c72020-10-13 16:55:06 -0700750static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800751{
752 char *p;
753 size_t size = 200;
754 struct kmem_cache *cache;
755
756 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700757 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
758
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800759 p = kmem_cache_alloc(cache, GFP_KERNEL);
760 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700761 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800762 kmem_cache_destroy(cache);
763 return;
764 }
765
766 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700767 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800768 kmem_cache_destroy(cache);
769}
770
Patricia Alfonso73228c72020-10-13 16:55:06 -0700771static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800772{
773 char *p;
774 size_t size = 200;
775 struct kmem_cache *cache;
776
777 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
778 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700779 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
780
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800781 p = kmem_cache_alloc(cache, GFP_KERNEL);
782 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700783 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800784 kmem_cache_destroy(cache);
785 return;
786 }
787
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800788 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700789 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700790
791 /*
792 * Properly free the object to prevent the "Objects remaining in
793 * test_cache on __kmem_cache_shutdown" BUG failure.
794 */
795 kmem_cache_free(cache, p);
796
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800797 kmem_cache_destroy(cache);
798}
799
Patricia Alfonso73228c72020-10-13 16:55:06 -0700800static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700801{
802 char *ptr;
803 size_t size = 24;
804
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800805 /*
806 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
807 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
808 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800809 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700810
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800811 if (OOB_TAG_OFF)
812 size = round_up(size, OOB_TAG_OFF);
813
Patricia Alfonso73228c72020-10-13 16:55:06 -0700814 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
815 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
816
817 KUNIT_EXPECT_KASAN_FAIL(test,
818 kasan_ptr_result = memchr(ptr, '1', size + 1));
819
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700820 kfree(ptr);
821}
822
Patricia Alfonso73228c72020-10-13 16:55:06 -0700823static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700824{
825 char *ptr;
826 size_t size = 24;
827 int arr[9];
828
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800829 /*
830 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
831 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
832 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800833 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700834
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800835 if (OOB_TAG_OFF)
836 size = round_up(size, OOB_TAG_OFF);
837
Patricia Alfonso73228c72020-10-13 16:55:06 -0700838 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
839 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700840 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700841
842 KUNIT_EXPECT_KASAN_FAIL(test,
843 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700844 kfree(ptr);
845}
846
Patricia Alfonso73228c72020-10-13 16:55:06 -0700847static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700848{
849 char *ptr;
850 size_t size = 24;
851
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800852 /*
853 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
854 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
855 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800856 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700857
858 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
859 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700860
861 kfree(ptr);
862
863 /*
864 * Try to cause only 1 invalid access (less spam in dmesg).
865 * For that we need ptr to point to zeroed byte.
866 * Skip metadata that could be stored in freed object so ptr
867 * will likely point to zeroed byte.
868 */
869 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700870 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700871
Patricia Alfonso73228c72020-10-13 16:55:06 -0700872 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700873
Patricia Alfonso73228c72020-10-13 16:55:06 -0700874 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700875
Patricia Alfonso73228c72020-10-13 16:55:06 -0700876 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700877
Patricia Alfonso73228c72020-10-13 16:55:06 -0700878 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700879
Patricia Alfonso73228c72020-10-13 16:55:06 -0700880 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700881}
882
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800883static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700884{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800885 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
886 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
887 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
888 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
889 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
890 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
891 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
892 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
893}
894
895static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
896{
897 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
898 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
899 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
900 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
901 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
902 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
903 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
904 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
905
906#if defined(clear_bit_unlock_is_negative_byte)
907 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
908 clear_bit_unlock_is_negative_byte(nr, addr));
909#endif
910}
911
912static void kasan_bitops_generic(struct kunit *test)
913{
914 long *bits;
915
916 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800917 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800918
Marco Elver19a33ca2019-07-11 20:53:52 -0700919 /*
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800920 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700921 * this way we do not actually corrupt other memory.
922 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800923 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700924 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700925
926 /*
927 * Below calls try to access bit within allocated memory; however, the
928 * below accesses are still out-of-bounds, since bitops are defined to
929 * operate on the whole long the bit is in.
930 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800931 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700932
933 /*
934 * Below calls try to access bit beyond allocated memory.
935 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800936 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700937
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800938 kfree(bits);
939}
Marco Elver19a33ca2019-07-11 20:53:52 -0700940
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800941static void kasan_bitops_tags(struct kunit *test)
942{
943 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700944
Andrey Konovalovda17e372021-02-24 12:05:17 -0800945 /* This test is specifically crafted for tag-based modes. */
946 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700947
Andrey Konovalove66e1792021-02-24 12:05:42 -0800948 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
949 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800950 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700951
Andrey Konovalove66e1792021-02-24 12:05:42 -0800952 /* Do the accesses past the 48 allocated bytes, but within the redone. */
953 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
954 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700955
Marco Elver19a33ca2019-07-11 20:53:52 -0700956 kfree(bits);
957}
958
Patricia Alfonso73228c72020-10-13 16:55:06 -0700959static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700960{
961 char *ptr;
962 size_t size = 16;
963
Marco Elverbb104ed2019-07-11 20:54:11 -0700964 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700965 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700966
Waiman Long453431a2020-08-06 23:18:13 -0700967 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700968 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700969}
970
Patricia Alfonso73228c72020-10-13 16:55:06 -0700971static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800972{
973 void *area;
974
Andrey Konovalovda17e372021-02-24 12:05:17 -0800975 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800976
977 /*
978 * We have to be careful not to hit the guard page.
979 * The MMU will catch that and crash us.
980 */
981 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700982 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800983
Patricia Alfonso73228c72020-10-13 16:55:06 -0700984 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800985 vfree(area);
986}
Daniel Axtens06513912019-11-30 17:54:53 -0800987
Andrey Konovalov573a4802021-02-24 12:05:21 -0800988/*
989 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
990 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
991 * modes.
992 */
993static void match_all_not_assigned(struct kunit *test)
994{
995 char *ptr;
996 struct page *pages;
997 int i, size, order;
998
999 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1000
1001 for (i = 0; i < 256; i++) {
1002 size = (get_random_int() % 1024) + 1;
1003 ptr = kmalloc(size, GFP_KERNEL);
1004 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1005 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1006 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1007 kfree(ptr);
1008 }
1009
1010 for (i = 0; i < 256; i++) {
1011 order = (get_random_int() % 4) + 1;
1012 pages = alloc_pages(GFP_KERNEL, order);
1013 ptr = page_address(pages);
1014 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1015 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1016 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1017 free_pages((unsigned long)ptr, order);
1018 }
1019}
1020
1021/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1022static void match_all_ptr_tag(struct kunit *test)
1023{
1024 char *ptr;
1025 u8 tag;
1026
1027 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1028
1029 ptr = kmalloc(128, GFP_KERNEL);
1030 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1031
1032 /* Backup the assigned tag. */
1033 tag = get_tag(ptr);
1034 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1035
1036 /* Reset the tag to 0xff.*/
1037 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1038
1039 /* This access shouldn't trigger a KASAN report. */
1040 *ptr = 0;
1041
1042 /* Recover the pointer tag and free. */
1043 ptr = set_tag(ptr, tag);
1044 kfree(ptr);
1045}
1046
1047/* Check that there are no match-all memory tags for tag-based modes. */
1048static void match_all_mem_tag(struct kunit *test)
1049{
1050 char *ptr;
1051 int tag;
1052
1053 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1054
1055 ptr = kmalloc(128, GFP_KERNEL);
1056 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1057 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1058
1059 /* For each possible tag value not matching the pointer tag. */
1060 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1061 if (tag == get_tag(ptr))
1062 continue;
1063
1064 /* Mark the first memory granule with the chosen memory tag. */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -07001065 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
Andrey Konovalov573a4802021-02-24 12:05:21 -08001066
1067 /* This access must cause a KASAN report. */
1068 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1069 }
1070
1071 /* Recover the memory tag and free. */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -07001072 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
Andrey Konovalov573a4802021-02-24 12:05:21 -08001073 kfree(ptr);
1074}
1075
Patricia Alfonso73228c72020-10-13 16:55:06 -07001076static struct kunit_case kasan_kunit_test_cases[] = {
1077 KUNIT_CASE(kmalloc_oob_right),
1078 KUNIT_CASE(kmalloc_oob_left),
1079 KUNIT_CASE(kmalloc_node_oob_right),
1080 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1081 KUNIT_CASE(kmalloc_pagealloc_uaf),
1082 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalov858bdeb2021-02-24 12:05:55 -08001083 KUNIT_CASE(pagealloc_oob_right),
1084 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001085 KUNIT_CASE(kmalloc_large_oob_right),
Andrey Konovalovb87c28b92021-02-25 17:20:15 -08001086 KUNIT_CASE(krealloc_more_oob),
1087 KUNIT_CASE(krealloc_less_oob),
1088 KUNIT_CASE(krealloc_pagealloc_more_oob),
1089 KUNIT_CASE(krealloc_pagealloc_less_oob),
Andrey Konovalov26a5ca72021-02-25 17:20:19 -08001090 KUNIT_CASE(krealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001091 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001092 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001093 KUNIT_CASE(kmalloc_oob_in_memset),
1094 KUNIT_CASE(kmalloc_oob_memset_2),
1095 KUNIT_CASE(kmalloc_oob_memset_4),
1096 KUNIT_CASE(kmalloc_oob_memset_8),
1097 KUNIT_CASE(kmalloc_oob_memset_16),
1098 KUNIT_CASE(kmalloc_memmove_invalid_size),
1099 KUNIT_CASE(kmalloc_uaf),
1100 KUNIT_CASE(kmalloc_uaf_memset),
1101 KUNIT_CASE(kmalloc_uaf2),
1102 KUNIT_CASE(kfree_via_page),
1103 KUNIT_CASE(kfree_via_phys),
1104 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov11516132021-02-24 12:05:59 -08001105 KUNIT_CASE(kmem_cache_accounted),
1106 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001107 KUNIT_CASE(kasan_global_oob),
1108 KUNIT_CASE(kasan_stack_oob),
1109 KUNIT_CASE(kasan_alloca_oob_left),
1110 KUNIT_CASE(kasan_alloca_oob_right),
1111 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov611806b2021-02-24 12:05:50 -08001112 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001113 KUNIT_CASE(kmem_cache_double_free),
1114 KUNIT_CASE(kmem_cache_invalid_free),
1115 KUNIT_CASE(kasan_memchr),
1116 KUNIT_CASE(kasan_memcmp),
1117 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001118 KUNIT_CASE(kasan_bitops_generic),
1119 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001120 KUNIT_CASE(kmalloc_double_kzfree),
1121 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov573a4802021-02-24 12:05:21 -08001122 KUNIT_CASE(match_all_not_assigned),
1123 KUNIT_CASE(match_all_ptr_tag),
1124 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001125 {}
1126};
Walter Wu387d6e42020-08-06 23:24:42 -07001127
Patricia Alfonso73228c72020-10-13 16:55:06 -07001128static struct kunit_suite kasan_kunit_test_suite = {
1129 .name = "kasan",
1130 .init = kasan_test_init,
1131 .test_cases = kasan_kunit_test_cases,
1132 .exit = kasan_test_exit,
1133};
Walter Wu387d6e42020-08-06 23:24:42 -07001134
Patricia Alfonso73228c72020-10-13 16:55:06 -07001135kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001136
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001137MODULE_LICENSE("GPL");