blob: 8f7b0b2f6e11d07c5b627dbb4d7db86491d16d59 [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 Konovalovf05842c2021-02-24 12:05:26 -080056 kasan_set_tagging_report_once(false);
Andrey Konovalov99734b52021-04-29 23:00:49 -070057 fail_data.report_found = false;
Andrey Konovalov99734b52021-04-29 23:00:49 -070058 kunit_add_named_resource(test, NULL, NULL, &resource,
59 "kasan_data", &fail_data);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070060 return 0;
61}
62
63static void kasan_test_exit(struct kunit *test)
64{
Andrey Konovalovf05842c2021-02-24 12:05:26 -080065 kasan_set_tagging_report_once(true);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070066 kasan_restore_multi_shot(multishot);
Andrey Konovalov99734b52021-04-29 23:00:49 -070067 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070068}
69
70/**
Andrey Konovalov0fd37922021-02-24 12:05:13 -080071 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
72 * KASAN report; causes a test failure otherwise. This relies on a KUnit
73 * resource named "kasan_data". Do not use this name for KUnit resources
74 * outside of KASAN tests.
Andrey Konovalovf05842c2021-02-24 12:05:26 -080075 *
Andrey Konovalove80a76a2021-03-15 13:20:19 +000076 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
77 * checking is auto-disabled. When this happens, this test handler reenables
78 * tag checking. As tag checking can be only disabled or enabled per CPU,
79 * this handler disables migration (preemption).
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080080 *
81 * Since the compiler doesn't see that the expression can change the fail_data
82 * fields, it can reorder or optimize away the accesses to those fields.
83 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
84 * expression to prevent that.
Andrey Konovalov99734b52021-04-29 23:00:49 -070085 *
86 * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
87 * false. This allows detecting KASAN reports that happen outside of the checks
88 * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
89 * and in kasan_test_exit.
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070090 */
Andrey Konovalov99734b52021-04-29 23:00:49 -070091#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
92 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
93 !kasan_async_mode_enabled()) \
94 migrate_disable(); \
95 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
Andrey Konovalov99734b52021-04-29 23:00:49 -070096 barrier(); \
97 expression; \
98 barrier(); \
David Gow3ff16d32021-06-28 19:40:36 -070099 if (!READ_ONCE(fail_data.report_found)) { \
100 KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure " \
101 "expected in \"" #expression \
102 "\", but none occurred"); \
103 } \
Andrey Konovalov99734b52021-04-29 23:00:49 -0700104 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
105 if (READ_ONCE(fail_data.report_found)) \
106 kasan_enable_tagging_sync(); \
107 migrate_enable(); \
108 } \
109 WRITE_ONCE(fail_data.report_found, false); \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700110} while (0)
111
Andrey Konovalovda17e372021-02-24 12:05:17 -0800112#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
Marco Elver40eb5cf2021-06-24 23:58:15 -0700113 if (!IS_ENABLED(config)) \
114 kunit_skip((test), "Test requires " #config "=y"); \
Andrey Konovalovda17e372021-02-24 12:05:17 -0800115} while (0)
116
117#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
Marco Elver40eb5cf2021-06-24 23:58:15 -0700118 if (IS_ENABLED(config)) \
119 kunit_skip((test), "Test requires " #config "=n"); \
Andrey Konovalovda17e372021-02-24 12:05:17 -0800120} while (0)
121
Patricia Alfonso73228c72020-10-13 16:55:06 -0700122static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800123{
124 char *ptr;
125 size_t size = 123;
126
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800127 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700128 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800129
Patricia Alfonso73228c72020-10-13 16:55:06 -0700130 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800131 kfree(ptr);
132}
133
Patricia Alfonso73228c72020-10-13 16:55:06 -0700134static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800135{
136 char *ptr;
137 size_t size = 15;
138
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800139 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700140 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800141
Patricia Alfonso73228c72020-10-13 16:55:06 -0700142 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800143 kfree(ptr);
144}
145
Patricia Alfonso73228c72020-10-13 16:55:06 -0700146static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800147{
148 char *ptr;
149 size_t size = 4096;
150
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800151 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700152 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800153
Patricia Alfonso73228c72020-10-13 16:55:06 -0700154 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800155 kfree(ptr);
156}
157
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800158/*
159 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
160 * fit into a slab cache and therefore is allocated via the page allocator
161 * fallback. Since this kind of fallback is only implemented for SLUB, these
162 * tests are limited to that allocator.
163 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700164static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800165{
166 char *ptr;
167 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
168
Andrey Konovalovda17e372021-02-24 12:05:17 -0800169 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700170
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700171 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700172 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700173
Patricia Alfonso73228c72020-10-13 16:55:06 -0700174 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800175
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700176 kfree(ptr);
177}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800178
Patricia Alfonso73228c72020-10-13 16:55:06 -0700179static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800180{
181 char *ptr;
182 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
183
Andrey Konovalovda17e372021-02-24 12:05:17 -0800184 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800185
Patricia Alfonso73228c72020-10-13 16:55:06 -0700186 ptr = kmalloc(size, GFP_KERNEL);
187 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800188 kfree(ptr);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800189
Patricia Alfonso73228c72020-10-13 16:55:06 -0700190 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800191}
192
Patricia Alfonso73228c72020-10-13 16:55:06 -0700193static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800194{
195 char *ptr;
196 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
197
Andrey Konovalovda17e372021-02-24 12:05:17 -0800198 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800199
Patricia Alfonso73228c72020-10-13 16:55:06 -0700200 ptr = kmalloc(size, GFP_KERNEL);
201 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700202
Patricia Alfonso73228c72020-10-13 16:55:06 -0700203 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
204}
205
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800206static void pagealloc_oob_right(struct kunit *test)
207{
208 char *ptr;
209 struct page *pages;
210 size_t order = 4;
211 size_t size = (1UL << (PAGE_SHIFT + order));
212
213 /*
214 * With generic KASAN page allocations have no redzones, thus
215 * out-of-bounds detection is not guaranteed.
216 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
217 */
218 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
219
220 pages = alloc_pages(GFP_KERNEL, order);
221 ptr = page_address(pages);
222 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
223
224 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
225 free_pages((unsigned long)ptr, order);
226}
227
228static void pagealloc_uaf(struct kunit *test)
229{
230 char *ptr;
231 struct page *pages;
232 size_t order = 4;
233
234 pages = alloc_pages(GFP_KERNEL, order);
235 ptr = page_address(pages);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
237 free_pages((unsigned long)ptr, order);
238
239 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
240}
241
Patricia Alfonso73228c72020-10-13 16:55:06 -0700242static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700243{
244 char *ptr;
245 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800246
247 /*
248 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700249 * and does not trigger the page allocator fallback in SLUB.
250 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800251 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700252 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800253
Patricia Alfonso73228c72020-10-13 16:55:06 -0700254 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800255 kfree(ptr);
256}
257
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800258static void krealloc_more_oob_helper(struct kunit *test,
259 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800260{
261 char *ptr1, *ptr2;
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800262 size_t middle;
263
264 KUNIT_ASSERT_LT(test, size1, size2);
265 middle = size1 + (size2 - size1) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800266
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800267 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700268 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
269
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800270 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700271 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800272
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800273 /* All offsets up to size2 must be accessible. */
274 ptr2[size1 - 1] = 'x';
275 ptr2[size1] = 'x';
276 ptr2[middle] = 'x';
277 ptr2[size2 - 1] = 'x';
278
279 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
280 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
281 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
282
283 /* For all modes first aligned offset after size2 must be inaccessible. */
284 KUNIT_EXPECT_KASAN_FAIL(test,
285 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
286
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800287 kfree(ptr2);
288}
289
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800290static void krealloc_less_oob_helper(struct kunit *test,
291 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800292{
293 char *ptr1, *ptr2;
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800294 size_t middle;
295
296 KUNIT_ASSERT_LT(test, size2, size1);
297 middle = size2 + (size1 - size2) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800298
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800299 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
301
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800302 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700303 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700304
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800305 /* Must be accessible for all modes. */
306 ptr2[size2 - 1] = 'x';
307
308 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
309 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
310 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
311
312 /* For all modes first aligned offset after size2 must be inaccessible. */
313 KUNIT_EXPECT_KASAN_FAIL(test,
314 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
315
316 /*
317 * For all modes all size2, middle, and size1 should land in separate
318 * granules and thus the latter two offsets should be inaccessible.
319 */
320 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
321 round_down(middle, KASAN_GRANULE_SIZE));
322 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
323 round_down(size1, KASAN_GRANULE_SIZE));
324 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
325 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
326 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
327
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800328 kfree(ptr2);
329}
330
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800331static void krealloc_more_oob(struct kunit *test)
332{
333 krealloc_more_oob_helper(test, 201, 235);
334}
335
336static void krealloc_less_oob(struct kunit *test)
337{
338 krealloc_less_oob_helper(test, 235, 201);
339}
340
341static void krealloc_pagealloc_more_oob(struct kunit *test)
342{
343 /* page_alloc fallback in only implemented for SLUB. */
344 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
345
346 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
347 KMALLOC_MAX_CACHE_SIZE + 235);
348}
349
350static void krealloc_pagealloc_less_oob(struct kunit *test)
351{
352 /* page_alloc fallback in only implemented for SLUB. */
353 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
354
355 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
356 KMALLOC_MAX_CACHE_SIZE + 201);
357}
358
Andrey Konovalov26a5ca72021-02-25 17:20:19 -0800359/*
360 * Check that krealloc() detects a use-after-free, returns NULL,
361 * and doesn't unpoison the freed object.
362 */
363static void krealloc_uaf(struct kunit *test)
364{
365 char *ptr1, *ptr2;
366 int size1 = 201;
367 int size2 = 235;
368
369 ptr1 = kmalloc(size1, GFP_KERNEL);
370 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
371 kfree(ptr1);
372
373 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
374 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
375 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
376}
377
Patricia Alfonso73228c72020-10-13 16:55:06 -0700378static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800379{
380 struct {
381 u64 words[2];
382 } *ptr1, *ptr2;
383
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800384 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800385 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800386
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800387 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700388 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
389
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800390 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700391 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
392
393 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800394 kfree(ptr1);
395 kfree(ptr2);
396}
397
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800398static void kmalloc_uaf_16(struct kunit *test)
399{
400 struct {
401 u64 words[2];
402 } *ptr1, *ptr2;
403
404 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
406
407 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
408 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
409 kfree(ptr2);
410
411 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
412 kfree(ptr1);
413}
414
Patricia Alfonso73228c72020-10-13 16:55:06 -0700415static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800416{
417 char *ptr;
418 size_t size = 8;
419
Wang Longf523e732015-11-05 18:51:15 -0800420 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700421 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800422
Patricia Alfonso73228c72020-10-13 16:55:06 -0700423 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800424 kfree(ptr);
425}
426
Patricia Alfonso73228c72020-10-13 16:55:06 -0700427static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800428{
429 char *ptr;
430 size_t size = 8;
431
Wang Longf523e732015-11-05 18:51:15 -0800432 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800434
Patricia Alfonso73228c72020-10-13 16:55:06 -0700435 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800436 kfree(ptr);
437}
438
439
Patricia Alfonso73228c72020-10-13 16:55:06 -0700440static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800441{
442 char *ptr;
443 size_t size = 8;
444
Wang Longf523e732015-11-05 18:51:15 -0800445 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700446 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800447
Patricia Alfonso73228c72020-10-13 16:55:06 -0700448 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800449 kfree(ptr);
450}
451
Patricia Alfonso73228c72020-10-13 16:55:06 -0700452static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800453{
454 char *ptr;
455 size_t size = 16;
456
Wang Longf523e732015-11-05 18:51:15 -0800457 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700458 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800459
Patricia Alfonso73228c72020-10-13 16:55:06 -0700460 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800461 kfree(ptr);
462}
463
Patricia Alfonso73228c72020-10-13 16:55:06 -0700464static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800465{
466 char *ptr;
467 size_t size = 666;
468
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800469 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700470 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800471
Patricia Alfonso73228c72020-10-13 16:55:06 -0700472 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800473 kfree(ptr);
474}
475
Patricia Alfonso73228c72020-10-13 16:55:06 -0700476static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700477{
478 char *ptr;
479 size_t size = 64;
480 volatile size_t invalid_size = -2;
481
Walter Wu98f3b562020-04-01 21:09:40 -0700482 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700483 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700484
485 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700486
487 KUNIT_EXPECT_KASAN_FAIL(test,
488 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700489 kfree(ptr);
490}
491
Patricia Alfonso73228c72020-10-13 16:55:06 -0700492static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800493{
494 char *ptr;
495 size_t size = 10;
496
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800497 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700498 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800499
500 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700501 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800502}
503
Patricia Alfonso73228c72020-10-13 16:55:06 -0700504static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800505{
506 char *ptr;
507 size_t size = 33;
508
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800509 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700510 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800511
512 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700513 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800514}
515
Patricia Alfonso73228c72020-10-13 16:55:06 -0700516static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800517{
518 char *ptr1, *ptr2;
519 size_t size = 43;
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800520 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800521
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800522again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800523 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700524 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800525
526 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800527
Patricia Alfonso73228c72020-10-13 16:55:06 -0700528 ptr2 = kmalloc(size, GFP_KERNEL);
529 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
530
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800531 /*
532 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
533 * Allow up to 16 attempts at generating different tags.
534 */
535 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
536 kfree(ptr2);
537 goto again;
538 }
539
Patricia Alfonso73228c72020-10-13 16:55:06 -0700540 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
541 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
542
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800543 kfree(ptr2);
544}
545
Patricia Alfonso73228c72020-10-13 16:55:06 -0700546static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700547{
548 char *ptr;
549 size_t size = 8;
550 struct page *page;
551 unsigned long offset;
552
Mark Rutlandb92a9532019-09-23 15:34:16 -0700553 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700555
556 page = virt_to_page(ptr);
557 offset = offset_in_page(ptr);
558 kfree(page_address(page) + offset);
559}
560
Patricia Alfonso73228c72020-10-13 16:55:06 -0700561static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700562{
563 char *ptr;
564 size_t size = 8;
565 phys_addr_t phys;
566
Mark Rutlandb92a9532019-09-23 15:34:16 -0700567 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700568 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700569
570 phys = virt_to_phys(ptr);
571 kfree(phys_to_virt(phys));
572}
573
Patricia Alfonso73228c72020-10-13 16:55:06 -0700574static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800575{
576 char *p;
577 size_t size = 200;
Andrey Konovalov11516132021-02-24 12:05:59 -0800578 struct kmem_cache *cache;
579
580 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700581 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov11516132021-02-24 12:05:59 -0800582
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800583 p = kmem_cache_alloc(cache, GFP_KERNEL);
584 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700585 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800586 kmem_cache_destroy(cache);
587 return;
588 }
589
Patricia Alfonso73228c72020-10-13 16:55:06 -0700590 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov11516132021-02-24 12:05:59 -0800591
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800592 kmem_cache_free(cache, p);
593 kmem_cache_destroy(cache);
594}
595
Andrey Konovalov11516132021-02-24 12:05:59 -0800596static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800597{
598 int i;
599 char *p;
600 size_t size = 200;
601 struct kmem_cache *cache;
602
603 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700604 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800605
Greg Thelen0386bf32017-02-24 15:00:08 -0800606 /*
607 * Several allocations with a delay to allow for lazy per memcg kmem
608 * cache creation.
609 */
610 for (i = 0; i < 5; i++) {
611 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800612 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800613 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800614
Greg Thelen0386bf32017-02-24 15:00:08 -0800615 kmem_cache_free(cache, p);
616 msleep(100);
617 }
618
619free_cache:
620 kmem_cache_destroy(cache);
621}
622
Andrey Konovalov11516132021-02-24 12:05:59 -0800623static void kmem_cache_bulk(struct kunit *test)
624{
625 struct kmem_cache *cache;
626 size_t size = 200;
627 char *p[10];
628 bool ret;
629 int i;
630
631 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
632 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
633
634 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
635 if (!ret) {
636 kunit_err(test, "Allocation failed: %s\n", __func__);
637 kmem_cache_destroy(cache);
638 return;
639 }
640
641 for (i = 0; i < ARRAY_SIZE(p); i++)
642 p[i][0] = p[i][size - 1] = 42;
643
644 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
645 kmem_cache_destroy(cache);
646}
647
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800648static char global_array[10];
649
Patricia Alfonso73228c72020-10-13 16:55:06 -0700650static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800651{
Peter Collingbournef649dc02021-05-14 17:27:27 -0700652 /*
653 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
Zhen Lei53b0fe32021-07-07 18:07:28 -0700654 * from failing here and panicking the kernel, access the array via a
Peter Collingbournef649dc02021-05-14 17:27:27 -0700655 * volatile pointer, which will prevent the compiler from being able to
656 * determine the array bounds.
657 *
658 * This access uses a volatile pointer to char (char *volatile) rather
659 * than the more conventional pointer to volatile char (volatile char *)
660 * because we want to prevent the compiler from making inferences about
661 * the pointer itself (i.e. its array bounds), not the data that it
662 * refers to.
663 */
664 char *volatile array = global_array;
665 char *p = &array[ARRAY_SIZE(global_array) + 3];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800666
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800667 /* Only generic mode instruments globals. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800668 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800669
Patricia Alfonso73228c72020-10-13 16:55:06 -0700670 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800671}
672
Andrey Konovalov611806b2021-02-24 12:05:50 -0800673/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700674static void ksize_unpoisons_memory(struct kunit *test)
675{
676 char *ptr;
677 size_t size = 123, real_size;
678
679 ptr = kmalloc(size, GFP_KERNEL);
680 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
681 real_size = ksize(ptr);
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800682
683 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700684 ptr[size] = 'x';
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800685
686 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700687 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800688
Patricia Alfonso73228c72020-10-13 16:55:06 -0700689 kfree(ptr);
690}
691
Andrey Konovalov611806b2021-02-24 12:05:50 -0800692/*
693 * Check that a use-after-free is detected by ksize() and via normal accesses
694 * after it.
695 */
696static void ksize_uaf(struct kunit *test)
697{
698 char *ptr;
699 int size = 128 - KASAN_GRANULE_SIZE;
700
701 ptr = kmalloc(size, GFP_KERNEL);
702 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
703 kfree(ptr);
704
705 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
706 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
707 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
708}
709
Patricia Alfonso73228c72020-10-13 16:55:06 -0700710static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800711{
712 char stack_array[10];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700713 /* See comment in kasan_global_oob. */
714 char *volatile array = stack_array;
715 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800716
Andrey Konovalovda17e372021-02-24 12:05:17 -0800717 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700718
Patricia Alfonso73228c72020-10-13 16:55:06 -0700719 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700720}
721
Patricia Alfonso73228c72020-10-13 16:55:06 -0700722static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800723{
724 volatile int i = 10;
725 char alloca_array[i];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700726 /* See comment in kasan_global_oob. */
727 char *volatile array = alloca_array;
728 char *p = array - 1;
Paul Lawrence00a14292018-02-06 15:36:16 -0800729
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800730 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800731 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
732 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700733
734 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800735}
736
Patricia Alfonso73228c72020-10-13 16:55:06 -0700737static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800738{
739 volatile int i = 10;
740 char alloca_array[i];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700741 /* See comment in kasan_global_oob. */
742 char *volatile array = alloca_array;
743 char *p = array + i;
Paul Lawrence00a14292018-02-06 15:36:16 -0800744
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800745 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800746 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
747 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700748
749 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800750}
751
Patricia Alfonso73228c72020-10-13 16:55:06 -0700752static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800753{
754 char *p;
755 size_t size = 200;
756 struct kmem_cache *cache;
757
758 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700759 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
760
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800761 p = kmem_cache_alloc(cache, GFP_KERNEL);
762 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700763 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800764 kmem_cache_destroy(cache);
765 return;
766 }
767
768 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700769 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800770 kmem_cache_destroy(cache);
771}
772
Patricia Alfonso73228c72020-10-13 16:55:06 -0700773static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800774{
775 char *p;
776 size_t size = 200;
777 struct kmem_cache *cache;
778
779 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
780 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700781 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
782
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800783 p = kmem_cache_alloc(cache, GFP_KERNEL);
784 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700785 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800786 kmem_cache_destroy(cache);
787 return;
788 }
789
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800790 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700791 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700792
793 /*
794 * Properly free the object to prevent the "Objects remaining in
795 * test_cache on __kmem_cache_shutdown" BUG failure.
796 */
797 kmem_cache_free(cache, p);
798
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800799 kmem_cache_destroy(cache);
800}
801
Patricia Alfonso73228c72020-10-13 16:55:06 -0700802static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700803{
804 char *ptr;
805 size_t size = 24;
806
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800807 /*
808 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
809 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
810 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800811 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700812
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800813 if (OOB_TAG_OFF)
814 size = round_up(size, OOB_TAG_OFF);
815
Patricia Alfonso73228c72020-10-13 16:55:06 -0700816 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
817 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
818
819 KUNIT_EXPECT_KASAN_FAIL(test,
820 kasan_ptr_result = memchr(ptr, '1', size + 1));
821
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700822 kfree(ptr);
823}
824
Patricia Alfonso73228c72020-10-13 16:55:06 -0700825static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700826{
827 char *ptr;
828 size_t size = 24;
829 int arr[9];
830
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800831 /*
832 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
833 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
834 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800835 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700836
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800837 if (OOB_TAG_OFF)
838 size = round_up(size, OOB_TAG_OFF);
839
Patricia Alfonso73228c72020-10-13 16:55:06 -0700840 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
841 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700842 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700843
844 KUNIT_EXPECT_KASAN_FAIL(test,
845 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700846 kfree(ptr);
847}
848
Patricia Alfonso73228c72020-10-13 16:55:06 -0700849static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700850{
851 char *ptr;
852 size_t size = 24;
853
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800854 /*
855 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
856 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
857 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800858 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700859
860 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
861 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700862
863 kfree(ptr);
864
865 /*
866 * Try to cause only 1 invalid access (less spam in dmesg).
867 * For that we need ptr to point to zeroed byte.
868 * Skip metadata that could be stored in freed object so ptr
869 * will likely point to zeroed byte.
870 */
871 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700872 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700873
Patricia Alfonso73228c72020-10-13 16:55:06 -0700874 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700875
Patricia Alfonso73228c72020-10-13 16:55:06 -0700876 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700877
Patricia Alfonso73228c72020-10-13 16:55:06 -0700878 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700879
Patricia Alfonso73228c72020-10-13 16:55:06 -0700880 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700881
Patricia Alfonso73228c72020-10-13 16:55:06 -0700882 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700883}
884
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800885static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700886{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800887 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
888 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
889 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
890 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
891 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
892 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
893 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
894 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
895}
896
897static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
898{
899 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
900 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
901 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
902 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
903 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
904 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
905 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
906 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
907
908#if defined(clear_bit_unlock_is_negative_byte)
909 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
910 clear_bit_unlock_is_negative_byte(nr, addr));
911#endif
912}
913
914static void kasan_bitops_generic(struct kunit *test)
915{
916 long *bits;
917
918 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800919 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800920
Marco Elver19a33ca2019-07-11 20:53:52 -0700921 /*
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800922 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700923 * this way we do not actually corrupt other memory.
924 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800925 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700926 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700927
928 /*
929 * Below calls try to access bit within allocated memory; however, the
930 * below accesses are still out-of-bounds, since bitops are defined to
931 * operate on the whole long the bit is in.
932 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800933 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700934
935 /*
936 * Below calls try to access bit beyond allocated memory.
937 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800938 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700939
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800940 kfree(bits);
941}
Marco Elver19a33ca2019-07-11 20:53:52 -0700942
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800943static void kasan_bitops_tags(struct kunit *test)
944{
945 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700946
Andrey Konovalovda17e372021-02-24 12:05:17 -0800947 /* This test is specifically crafted for tag-based modes. */
948 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700949
Andrey Konovalove66e1792021-02-24 12:05:42 -0800950 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
951 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800952 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700953
Andrey Konovalove66e1792021-02-24 12:05:42 -0800954 /* Do the accesses past the 48 allocated bytes, but within the redone. */
955 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
956 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700957
Marco Elver19a33ca2019-07-11 20:53:52 -0700958 kfree(bits);
959}
960
Patricia Alfonso73228c72020-10-13 16:55:06 -0700961static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700962{
963 char *ptr;
964 size_t size = 16;
965
Marco Elverbb104ed2019-07-11 20:54:11 -0700966 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700967 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700968
Waiman Long453431a2020-08-06 23:18:13 -0700969 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700970 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700971}
972
Patricia Alfonso73228c72020-10-13 16:55:06 -0700973static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800974{
975 void *area;
976
Andrey Konovalovda17e372021-02-24 12:05:17 -0800977 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800978
979 /*
980 * We have to be careful not to hit the guard page.
981 * The MMU will catch that and crash us.
982 */
983 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700984 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800985
Patricia Alfonso73228c72020-10-13 16:55:06 -0700986 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800987 vfree(area);
988}
Daniel Axtens06513912019-11-30 17:54:53 -0800989
Andrey Konovalov573a4802021-02-24 12:05:21 -0800990/*
991 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
992 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
993 * modes.
994 */
995static void match_all_not_assigned(struct kunit *test)
996{
997 char *ptr;
998 struct page *pages;
999 int i, size, order;
1000
1001 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1002
1003 for (i = 0; i < 256; i++) {
1004 size = (get_random_int() % 1024) + 1;
1005 ptr = kmalloc(size, GFP_KERNEL);
1006 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1007 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1008 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1009 kfree(ptr);
1010 }
1011
1012 for (i = 0; i < 256; i++) {
1013 order = (get_random_int() % 4) + 1;
1014 pages = alloc_pages(GFP_KERNEL, order);
1015 ptr = page_address(pages);
1016 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1017 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1018 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1019 free_pages((unsigned long)ptr, order);
1020 }
1021}
1022
1023/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1024static void match_all_ptr_tag(struct kunit *test)
1025{
1026 char *ptr;
1027 u8 tag;
1028
1029 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1030
1031 ptr = kmalloc(128, GFP_KERNEL);
1032 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1033
1034 /* Backup the assigned tag. */
1035 tag = get_tag(ptr);
1036 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1037
1038 /* Reset the tag to 0xff.*/
1039 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1040
1041 /* This access shouldn't trigger a KASAN report. */
1042 *ptr = 0;
1043
1044 /* Recover the pointer tag and free. */
1045 ptr = set_tag(ptr, tag);
1046 kfree(ptr);
1047}
1048
1049/* Check that there are no match-all memory tags for tag-based modes. */
1050static void match_all_mem_tag(struct kunit *test)
1051{
1052 char *ptr;
1053 int tag;
1054
1055 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1056
1057 ptr = kmalloc(128, GFP_KERNEL);
1058 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1059 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1060
1061 /* For each possible tag value not matching the pointer tag. */
1062 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1063 if (tag == get_tag(ptr))
1064 continue;
1065
1066 /* Mark the first memory granule with the chosen memory tag. */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -07001067 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
Andrey Konovalov573a4802021-02-24 12:05:21 -08001068
1069 /* This access must cause a KASAN report. */
1070 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1071 }
1072
1073 /* Recover the memory tag and free. */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -07001074 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
Andrey Konovalov573a4802021-02-24 12:05:21 -08001075 kfree(ptr);
1076}
1077
Patricia Alfonso73228c72020-10-13 16:55:06 -07001078static struct kunit_case kasan_kunit_test_cases[] = {
1079 KUNIT_CASE(kmalloc_oob_right),
1080 KUNIT_CASE(kmalloc_oob_left),
1081 KUNIT_CASE(kmalloc_node_oob_right),
1082 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1083 KUNIT_CASE(kmalloc_pagealloc_uaf),
1084 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalov858bdeb2021-02-24 12:05:55 -08001085 KUNIT_CASE(pagealloc_oob_right),
1086 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001087 KUNIT_CASE(kmalloc_large_oob_right),
Andrey Konovalovb87c28b92021-02-25 17:20:15 -08001088 KUNIT_CASE(krealloc_more_oob),
1089 KUNIT_CASE(krealloc_less_oob),
1090 KUNIT_CASE(krealloc_pagealloc_more_oob),
1091 KUNIT_CASE(krealloc_pagealloc_less_oob),
Andrey Konovalov26a5ca72021-02-25 17:20:19 -08001092 KUNIT_CASE(krealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001093 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001094 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001095 KUNIT_CASE(kmalloc_oob_in_memset),
1096 KUNIT_CASE(kmalloc_oob_memset_2),
1097 KUNIT_CASE(kmalloc_oob_memset_4),
1098 KUNIT_CASE(kmalloc_oob_memset_8),
1099 KUNIT_CASE(kmalloc_oob_memset_16),
1100 KUNIT_CASE(kmalloc_memmove_invalid_size),
1101 KUNIT_CASE(kmalloc_uaf),
1102 KUNIT_CASE(kmalloc_uaf_memset),
1103 KUNIT_CASE(kmalloc_uaf2),
1104 KUNIT_CASE(kfree_via_page),
1105 KUNIT_CASE(kfree_via_phys),
1106 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov11516132021-02-24 12:05:59 -08001107 KUNIT_CASE(kmem_cache_accounted),
1108 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001109 KUNIT_CASE(kasan_global_oob),
1110 KUNIT_CASE(kasan_stack_oob),
1111 KUNIT_CASE(kasan_alloca_oob_left),
1112 KUNIT_CASE(kasan_alloca_oob_right),
1113 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov611806b2021-02-24 12:05:50 -08001114 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001115 KUNIT_CASE(kmem_cache_double_free),
1116 KUNIT_CASE(kmem_cache_invalid_free),
1117 KUNIT_CASE(kasan_memchr),
1118 KUNIT_CASE(kasan_memcmp),
1119 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001120 KUNIT_CASE(kasan_bitops_generic),
1121 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001122 KUNIT_CASE(kmalloc_double_kzfree),
1123 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov573a4802021-02-24 12:05:21 -08001124 KUNIT_CASE(match_all_not_assigned),
1125 KUNIT_CASE(match_all_ptr_tag),
1126 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001127 {}
1128};
Walter Wu387d6e42020-08-06 23:24:42 -07001129
Patricia Alfonso73228c72020-10-13 16:55:06 -07001130static struct kunit_suite kasan_kunit_test_suite = {
1131 .name = "kasan",
1132 .init = kasan_test_init,
1133 .test_cases = kasan_kunit_test_cases,
1134 .exit = kasan_test_exit,
1135};
Walter Wu387d6e42020-08-06 23:24:42 -07001136
Patricia Alfonso73228c72020-10-13 16:55:06 -07001137kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001138
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001139MODULE_LICENSE("GPL");