blob: 87170a68f7e769a654683b2f7b9fad7d6243e3e5 [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 Konovalov782ba452021-02-03 15:35:00 +110016#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 Konovalov70585d92020-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 Konovalovf3e66b22021-02-03 15:34:59 +110032 * 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 Konovalovf3e66b22021-02-03 15:34:59 +110042/*
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
Andrey Konovalovc7d68c42021-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 Konovalovf3e66b22021-02-03 15:34:59 +110047 */
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070048static int kasan_test_init(struct kunit *test)
49{
Andrey Konovalovbdf2f9b2021-02-03 15:35:07 +110050 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 Konovalovc7d68c42021-02-24 12:05:26 -080056 kasan_set_tagging_report_once(false);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070057 return 0;
58}
59
60static void kasan_test_exit(struct kunit *test)
61{
Andrey Konovalovc7d68c42021-02-24 12:05:26 -080062 kasan_set_tagging_report_once(true);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070063 kasan_restore_multi_shot(multishot);
64}
65
66/**
Andrey Konovalovf3e66b22021-02-03 15:34:59 +110067 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
68 * KASAN report; causes a test failure otherwise. This relies on a KUnit
69 * resource named "kasan_data". Do not use this name for KUnit resources
70 * outside of KASAN tests.
Andrey Konovalova599a4e2021-02-03 15:35:02 +110071 *
Andrey Konovalovccc8708b2021-03-15 13:20:19 +000072 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
73 * checking is auto-disabled. When this happens, this test handler reenables
74 * tag checking. As tag checking can be only disabled or enabled per CPU,
75 * this handler disables migration (preemption).
Andrey Konovalovc7d68c42021-02-24 12:05:26 -080076 *
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110077 * Since the compiler doesn't see that the expression can change the fail_data
78 * fields, it can reorder or optimize away the accesses to those fields.
79 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
80 * expression to prevent that.
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070081 */
Andrey Konovalova599a4e2021-02-03 15:35:02 +110082#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
Andrey Konovalovccc8708b2021-03-15 13:20:19 +000083 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
84 !kasan_async_mode_enabled()) \
Andrey Konovalovc7d68c42021-02-24 12:05:26 -080085 migrate_disable(); \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110086 WRITE_ONCE(fail_data.report_expected, true); \
87 WRITE_ONCE(fail_data.report_found, false); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110088 kunit_add_named_resource(test, \
89 NULL, \
90 NULL, \
91 &resource, \
92 "kasan_data", &fail_data); \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110093 barrier(); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110094 expression; \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110095 barrier(); \
Andrey Konovalovccc8708b2021-03-15 13:20:19 +000096 if (kasan_async_mode_enabled()) \
97 kasan_force_async_fault(); \
98 barrier(); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110099 KUNIT_EXPECT_EQ(test, \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +1100100 READ_ONCE(fail_data.report_expected), \
101 READ_ONCE(fail_data.report_found)); \
Andrey Konovalovccc8708b2021-03-15 13:20:19 +0000102 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
103 !kasan_async_mode_enabled()) { \
Andrey Konovalovc7d68c42021-02-24 12:05:26 -0800104 if (READ_ONCE(fail_data.report_found)) \
Vincenzo Frascino563c7d92021-03-15 13:20:12 +0000105 kasan_enable_tagging_sync(); \
Andrey Konovalovc7d68c42021-02-24 12:05:26 -0800106 migrate_enable(); \
107 } \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700108} while (0)
109
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100110#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
111 if (!IS_ENABLED(config)) { \
112 kunit_info((test), "skipping, " #config " required"); \
113 return; \
114 } \
115} while (0)
116
117#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
118 if (IS_ENABLED(config)) { \
119 kunit_info((test), "skipping, " #config " enabled"); \
120 return; \
121 } \
122} while (0)
123
Patricia Alfonso73228c72020-10-13 16:55:06 -0700124static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800125{
126 char *ptr;
127 size_t size = 123;
128
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800129 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700130 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800131
Patricia Alfonso73228c72020-10-13 16:55:06 -0700132 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800133 kfree(ptr);
134}
135
Patricia Alfonso73228c72020-10-13 16:55:06 -0700136static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800137{
138 char *ptr;
139 size_t size = 15;
140
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800141 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700142 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800143
Patricia Alfonso73228c72020-10-13 16:55:06 -0700144 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800145 kfree(ptr);
146}
147
Patricia Alfonso73228c72020-10-13 16:55:06 -0700148static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800149{
150 char *ptr;
151 size_t size = 4096;
152
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800153 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800155
Patricia Alfonso73228c72020-10-13 16:55:06 -0700156 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800157 kfree(ptr);
158}
159
Andrey Konovalove449e272021-02-03 15:35:06 +1100160/*
161 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
162 * fit into a slab cache and therefore is allocated via the page allocator
163 * fallback. Since this kind of fallback is only implemented for SLUB, these
164 * tests are limited to that allocator.
165 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700166static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800167{
168 char *ptr;
169 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
170
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100171 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700172
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700173 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700174 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700175
Patricia Alfonso73228c72020-10-13 16:55:06 -0700176 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrey Konovalove449e272021-02-03 15:35:06 +1100177
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700178 kfree(ptr);
179}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800180
Patricia Alfonso73228c72020-10-13 16:55:06 -0700181static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800182{
183 char *ptr;
184 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
185
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100186 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800187
Patricia Alfonso73228c72020-10-13 16:55:06 -0700188 ptr = kmalloc(size, GFP_KERNEL);
189 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800190 kfree(ptr);
Andrey Konovalove449e272021-02-03 15:35:06 +1100191
Patricia Alfonso73228c72020-10-13 16:55:06 -0700192 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800193}
194
Patricia Alfonso73228c72020-10-13 16:55:06 -0700195static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800196{
197 char *ptr;
198 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
199
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100200 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800201
Patricia Alfonso73228c72020-10-13 16:55:06 -0700202 ptr = kmalloc(size, GFP_KERNEL);
203 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700204
Patricia Alfonso73228c72020-10-13 16:55:06 -0700205 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
206}
207
Andrey Konovalove449e272021-02-03 15:35:06 +1100208static void pagealloc_oob_right(struct kunit *test)
209{
210 char *ptr;
211 struct page *pages;
212 size_t order = 4;
213 size_t size = (1UL << (PAGE_SHIFT + order));
214
215 /*
216 * With generic KASAN page allocations have no redzones, thus
217 * out-of-bounds detection is not guaranteed.
218 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
219 */
220 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
221
222 pages = alloc_pages(GFP_KERNEL, order);
223 ptr = page_address(pages);
224 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
225
226 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
227 free_pages((unsigned long)ptr, order);
228}
229
230static void pagealloc_uaf(struct kunit *test)
231{
232 char *ptr;
233 struct page *pages;
234 size_t order = 4;
235
236 pages = alloc_pages(GFP_KERNEL, order);
237 ptr = page_address(pages);
238 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
239 free_pages((unsigned long)ptr, order);
240
241 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
242}
243
Patricia Alfonso73228c72020-10-13 16:55:06 -0700244static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700245{
246 char *ptr;
247 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100248
249 /*
250 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700251 * and does not trigger the page allocator fallback in SLUB.
252 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800253 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700254 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800255
Patricia Alfonso73228c72020-10-13 16:55:06 -0700256 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800257 kfree(ptr);
258}
259
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100260static void krealloc_more_oob_helper(struct kunit *test,
261 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800262{
263 char *ptr1, *ptr2;
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100264 size_t middle;
265
266 KUNIT_ASSERT_LT(test, size1, size2);
267 middle = size1 + (size2 - size1) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800268
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800269 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700270 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
271
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800272 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700273 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800274
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100275 /* All offsets up to size2 must be accessible. */
276 ptr2[size1 - 1] = 'x';
277 ptr2[size1] = 'x';
278 ptr2[middle] = 'x';
279 ptr2[size2 - 1] = 'x';
280
281 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
282 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
283 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
284
285 /* For all modes first aligned offset after size2 must be inaccessible. */
286 KUNIT_EXPECT_KASAN_FAIL(test,
287 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
288
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800289 kfree(ptr2);
290}
291
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100292static void krealloc_less_oob_helper(struct kunit *test,
293 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800294{
295 char *ptr1, *ptr2;
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100296 size_t middle;
297
298 KUNIT_ASSERT_LT(test, size2, size1);
299 middle = size2 + (size1 - size2) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800300
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800301 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700302 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
303
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800304 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700305 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700306
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100307 /* Must be accessible for all modes. */
308 ptr2[size2 - 1] = 'x';
309
310 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
311 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
312 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
313
314 /* For all modes first aligned offset after size2 must be inaccessible. */
315 KUNIT_EXPECT_KASAN_FAIL(test,
316 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
317
318 /*
319 * For all modes all size2, middle, and size1 should land in separate
320 * granules and thus the latter two offsets should be inaccessible.
321 */
322 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
323 round_down(middle, KASAN_GRANULE_SIZE));
324 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
325 round_down(size1, KASAN_GRANULE_SIZE));
326 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
327 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
328 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
329
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800330 kfree(ptr2);
331}
332
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100333static void krealloc_more_oob(struct kunit *test)
334{
335 krealloc_more_oob_helper(test, 201, 235);
336}
337
338static void krealloc_less_oob(struct kunit *test)
339{
340 krealloc_less_oob_helper(test, 235, 201);
341}
342
343static void krealloc_pagealloc_more_oob(struct kunit *test)
344{
345 /* page_alloc fallback in only implemented for SLUB. */
346 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
347
348 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
349 KMALLOC_MAX_CACHE_SIZE + 235);
350}
351
352static void krealloc_pagealloc_less_oob(struct kunit *test)
353{
354 /* page_alloc fallback in only implemented for SLUB. */
355 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
356
357 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
358 KMALLOC_MAX_CACHE_SIZE + 201);
359}
360
Andrey Konovalov4f62c692021-02-12 17:14:50 +1100361/*
362 * Check that krealloc() detects a use-after-free, returns NULL,
363 * and doesn't unpoison the freed object.
364 */
365static void krealloc_uaf(struct kunit *test)
366{
367 char *ptr1, *ptr2;
368 int size1 = 201;
369 int size2 = 235;
370
371 ptr1 = kmalloc(size1, GFP_KERNEL);
372 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
373 kfree(ptr1);
374
375 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
376 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
377 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
378}
379
Patricia Alfonso73228c72020-10-13 16:55:06 -0700380static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800381{
382 struct {
383 u64 words[2];
384 } *ptr1, *ptr2;
385
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800386 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100387 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800388
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800389 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700390 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
391
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800392 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700393 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
394
395 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800396 kfree(ptr1);
397 kfree(ptr2);
398}
399
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800400static void kmalloc_uaf_16(struct kunit *test)
401{
402 struct {
403 u64 words[2];
404 } *ptr1, *ptr2;
405
406 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
407 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
408
409 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
410 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
411 kfree(ptr2);
412
413 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
414 kfree(ptr1);
415}
416
Patricia Alfonso73228c72020-10-13 16:55:06 -0700417static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800418{
419 char *ptr;
420 size_t size = 8;
421
Wang Longf523e732015-11-05 18:51:15 -0800422 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700423 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800424
Patricia Alfonso73228c72020-10-13 16:55:06 -0700425 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800426 kfree(ptr);
427}
428
Patricia Alfonso73228c72020-10-13 16:55:06 -0700429static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800430{
431 char *ptr;
432 size_t size = 8;
433
Wang Longf523e732015-11-05 18:51:15 -0800434 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700435 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800436
Patricia Alfonso73228c72020-10-13 16:55:06 -0700437 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800438 kfree(ptr);
439}
440
441
Patricia Alfonso73228c72020-10-13 16:55:06 -0700442static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800443{
444 char *ptr;
445 size_t size = 8;
446
Wang Longf523e732015-11-05 18:51:15 -0800447 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700448 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800449
Patricia Alfonso73228c72020-10-13 16:55:06 -0700450 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800451 kfree(ptr);
452}
453
Patricia Alfonso73228c72020-10-13 16:55:06 -0700454static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800455{
456 char *ptr;
457 size_t size = 16;
458
Wang Longf523e732015-11-05 18:51:15 -0800459 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700460 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800461
Patricia Alfonso73228c72020-10-13 16:55:06 -0700462 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800463 kfree(ptr);
464}
465
Patricia Alfonso73228c72020-10-13 16:55:06 -0700466static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800467{
468 char *ptr;
469 size_t size = 666;
470
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800471 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700472 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800473
Patricia Alfonso73228c72020-10-13 16:55:06 -0700474 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800475 kfree(ptr);
476}
477
Patricia Alfonso73228c72020-10-13 16:55:06 -0700478static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700479{
480 char *ptr;
481 size_t size = 64;
482 volatile size_t invalid_size = -2;
483
Walter Wu98f3b562020-04-01 21:09:40 -0700484 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700485 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700486
487 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700488
489 KUNIT_EXPECT_KASAN_FAIL(test,
490 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700491 kfree(ptr);
492}
493
Patricia Alfonso73228c72020-10-13 16:55:06 -0700494static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800495{
496 char *ptr;
497 size_t size = 10;
498
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800499 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700500 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800501
502 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700503 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800504}
505
Patricia Alfonso73228c72020-10-13 16:55:06 -0700506static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800507{
508 char *ptr;
509 size_t size = 33;
510
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800511 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700512 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800513
514 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700515 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800516}
517
Patricia Alfonso73228c72020-10-13 16:55:06 -0700518static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800519{
520 char *ptr1, *ptr2;
521 size_t size = 43;
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100522 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800523
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100524again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800525 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700526 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800527
528 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800529
Patricia Alfonso73228c72020-10-13 16:55:06 -0700530 ptr2 = kmalloc(size, GFP_KERNEL);
531 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
532
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100533 /*
534 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
535 * Allow up to 16 attempts at generating different tags.
536 */
537 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
538 kfree(ptr2);
539 goto again;
540 }
541
Patricia Alfonso73228c72020-10-13 16:55:06 -0700542 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
543 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
544
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800545 kfree(ptr2);
546}
547
Patricia Alfonso73228c72020-10-13 16:55:06 -0700548static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700549{
550 char *ptr;
551 size_t size = 8;
552 struct page *page;
553 unsigned long offset;
554
Mark Rutlandb92a9532019-09-23 15:34:16 -0700555 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700556 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700557
558 page = virt_to_page(ptr);
559 offset = offset_in_page(ptr);
560 kfree(page_address(page) + offset);
561}
562
Patricia Alfonso73228c72020-10-13 16:55:06 -0700563static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700564{
565 char *ptr;
566 size_t size = 8;
567 phys_addr_t phys;
568
Mark Rutlandb92a9532019-09-23 15:34:16 -0700569 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700570 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700571
572 phys = virt_to_phys(ptr);
573 kfree(phys_to_virt(phys));
574}
575
Patricia Alfonso73228c72020-10-13 16:55:06 -0700576static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800577{
578 char *p;
579 size_t size = 200;
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100580 struct kmem_cache *cache;
581
582 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700583 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100584
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800585 p = kmem_cache_alloc(cache, GFP_KERNEL);
586 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700587 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800588 kmem_cache_destroy(cache);
589 return;
590 }
591
Patricia Alfonso73228c72020-10-13 16:55:06 -0700592 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100593
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800594 kmem_cache_free(cache, p);
595 kmem_cache_destroy(cache);
596}
597
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100598static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800599{
600 int i;
601 char *p;
602 size_t size = 200;
603 struct kmem_cache *cache;
604
605 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700606 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800607
Greg Thelen0386bf32017-02-24 15:00:08 -0800608 /*
609 * Several allocations with a delay to allow for lazy per memcg kmem
610 * cache creation.
611 */
612 for (i = 0; i < 5; i++) {
613 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800614 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800615 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800616
Greg Thelen0386bf32017-02-24 15:00:08 -0800617 kmem_cache_free(cache, p);
618 msleep(100);
619 }
620
621free_cache:
622 kmem_cache_destroy(cache);
623}
624
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100625static void kmem_cache_bulk(struct kunit *test)
626{
627 struct kmem_cache *cache;
628 size_t size = 200;
629 char *p[10];
630 bool ret;
631 int i;
632
633 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
634 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
635
636 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
637 if (!ret) {
638 kunit_err(test, "Allocation failed: %s\n", __func__);
639 kmem_cache_destroy(cache);
640 return;
641 }
642
643 for (i = 0; i < ARRAY_SIZE(p); i++)
644 p[i][0] = p[i][size - 1] = 42;
645
646 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
647 kmem_cache_destroy(cache);
648}
649
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800650static char global_array[10];
651
Patricia Alfonso73228c72020-10-13 16:55:06 -0700652static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800653{
Peter Collingbournefe5c0a62021-05-14 17:27:27 -0700654 /*
655 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
656 * from failing here and panicing the kernel, access the array via a
657 * volatile pointer, which will prevent the compiler from being able to
658 * determine the array bounds.
659 *
660 * This access uses a volatile pointer to char (char *volatile) rather
661 * than the more conventional pointer to volatile char (volatile char *)
662 * because we want to prevent the compiler from making inferences about
663 * the pointer itself (i.e. its array bounds), not the data that it
664 * refers to.
665 */
666 char *volatile array = global_array;
667 char *p = &array[ARRAY_SIZE(global_array) + 3];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800668
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800669 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100670 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800671
Patricia Alfonso73228c72020-10-13 16:55:06 -0700672 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800673}
674
Andrey Konovalov696574e2021-02-03 15:35:05 +1100675/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700676static void ksize_unpoisons_memory(struct kunit *test)
677{
678 char *ptr;
679 size_t size = 123, real_size;
680
681 ptr = kmalloc(size, GFP_KERNEL);
682 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
683 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100684
685 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700686 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100687
688 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700689 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100690
Patricia Alfonso73228c72020-10-13 16:55:06 -0700691 kfree(ptr);
692}
693
Andrey Konovalov696574e2021-02-03 15:35:05 +1100694/*
695 * Check that a use-after-free is detected by ksize() and via normal accesses
696 * after it.
697 */
698static void ksize_uaf(struct kunit *test)
699{
700 char *ptr;
701 int size = 128 - KASAN_GRANULE_SIZE;
702
703 ptr = kmalloc(size, GFP_KERNEL);
704 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
705 kfree(ptr);
706
707 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
708 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
709 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
710}
711
Patricia Alfonso73228c72020-10-13 16:55:06 -0700712static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800713{
714 char stack_array[10];
Peter Collingbournefe5c0a62021-05-14 17:27:27 -0700715 /* See comment in kasan_global_oob. */
716 char *volatile array = stack_array;
717 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800718
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100719 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700720
Patricia Alfonso73228c72020-10-13 16:55:06 -0700721 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700722}
723
Patricia Alfonso73228c72020-10-13 16:55:06 -0700724static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800725{
726 volatile int i = 10;
727 char alloca_array[i];
Peter Collingbournefe5c0a62021-05-14 17:27:27 -0700728 /* See comment in kasan_global_oob. */
729 char *volatile array = alloca_array;
730 char *p = array - 1;
Paul Lawrence00a14292018-02-06 15:36:16 -0800731
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800732 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100733 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
734 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700735
736 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800737}
738
Patricia Alfonso73228c72020-10-13 16:55:06 -0700739static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800740{
741 volatile int i = 10;
742 char alloca_array[i];
Peter Collingbournefe5c0a62021-05-14 17:27:27 -0700743 /* See comment in kasan_global_oob. */
744 char *volatile array = alloca_array;
745 char *p = array + i;
Paul Lawrence00a14292018-02-06 15:36:16 -0800746
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800747 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100748 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
749 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700750
751 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800752}
753
Patricia Alfonso73228c72020-10-13 16:55:06 -0700754static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800755{
756 char *p;
757 size_t size = 200;
758 struct kmem_cache *cache;
759
760 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700761 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
762
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800763 p = kmem_cache_alloc(cache, GFP_KERNEL);
764 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700765 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800766 kmem_cache_destroy(cache);
767 return;
768 }
769
770 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700771 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800772 kmem_cache_destroy(cache);
773}
774
Patricia Alfonso73228c72020-10-13 16:55:06 -0700775static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800776{
777 char *p;
778 size_t size = 200;
779 struct kmem_cache *cache;
780
781 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
782 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700783 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
784
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800785 p = kmem_cache_alloc(cache, GFP_KERNEL);
786 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700787 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800788 kmem_cache_destroy(cache);
789 return;
790 }
791
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100792 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700793 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700794
795 /*
796 * Properly free the object to prevent the "Objects remaining in
797 * test_cache on __kmem_cache_shutdown" BUG failure.
798 */
799 kmem_cache_free(cache, p);
800
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800801 kmem_cache_destroy(cache);
802}
803
Patricia Alfonso73228c72020-10-13 16:55:06 -0700804static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700805{
806 char *ptr;
807 size_t size = 24;
808
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100809 /*
810 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
811 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
812 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100813 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700814
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800815 if (OOB_TAG_OFF)
816 size = round_up(size, OOB_TAG_OFF);
817
Patricia Alfonso73228c72020-10-13 16:55:06 -0700818 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
819 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
820
821 KUNIT_EXPECT_KASAN_FAIL(test,
822 kasan_ptr_result = memchr(ptr, '1', size + 1));
823
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700824 kfree(ptr);
825}
826
Patricia Alfonso73228c72020-10-13 16:55:06 -0700827static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700828{
829 char *ptr;
830 size_t size = 24;
831 int arr[9];
832
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100833 /*
834 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
835 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
836 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100837 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700838
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800839 if (OOB_TAG_OFF)
840 size = round_up(size, OOB_TAG_OFF);
841
Patricia Alfonso73228c72020-10-13 16:55:06 -0700842 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
843 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700844 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700845
846 KUNIT_EXPECT_KASAN_FAIL(test,
847 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700848 kfree(ptr);
849}
850
Patricia Alfonso73228c72020-10-13 16:55:06 -0700851static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700852{
853 char *ptr;
854 size_t size = 24;
855
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100856 /*
857 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
858 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
859 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100860 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700861
862 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
863 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700864
865 kfree(ptr);
866
867 /*
868 * Try to cause only 1 invalid access (less spam in dmesg).
869 * For that we need ptr to point to zeroed byte.
870 * Skip metadata that could be stored in freed object so ptr
871 * will likely point to zeroed byte.
872 */
873 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700874 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700875
Patricia Alfonso73228c72020-10-13 16:55:06 -0700876 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '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 = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700879
Patricia Alfonso73228c72020-10-13 16:55:06 -0700880 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700881
Patricia Alfonso73228c72020-10-13 16:55:06 -0700882 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700883
Patricia Alfonso73228c72020-10-13 16:55:06 -0700884 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700885}
886
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800887static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700888{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800889 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
890 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
891 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
892 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
893 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
894 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
895 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
896 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
897}
898
899static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
900{
901 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
902 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
903 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
904 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
905 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
906 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
907 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
908 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
909
910#if defined(clear_bit_unlock_is_negative_byte)
911 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
912 clear_bit_unlock_is_negative_byte(nr, addr));
913#endif
914}
915
916static void kasan_bitops_generic(struct kunit *test)
917{
918 long *bits;
919
920 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100921 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800922
Marco Elver19a33ca2019-07-11 20:53:52 -0700923 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100924 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700925 * this way we do not actually corrupt other memory.
926 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800927 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700928 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700929
930 /*
931 * Below calls try to access bit within allocated memory; however, the
932 * below accesses are still out-of-bounds, since bitops are defined to
933 * operate on the whole long the bit is in.
934 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800935 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700936
937 /*
938 * Below calls try to access bit beyond allocated memory.
939 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800940 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700941
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800942 kfree(bits);
943}
Marco Elver19a33ca2019-07-11 20:53:52 -0700944
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800945static void kasan_bitops_tags(struct kunit *test)
946{
947 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700948
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100949 /* This test is specifically crafted for tag-based modes. */
950 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700951
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100952 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
953 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800954 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700955
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100956 /* Do the accesses past the 48 allocated bytes, but within the redone. */
957 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
958 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700959
Marco Elver19a33ca2019-07-11 20:53:52 -0700960 kfree(bits);
961}
962
Patricia Alfonso73228c72020-10-13 16:55:06 -0700963static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700964{
965 char *ptr;
966 size_t size = 16;
967
Marco Elverbb104ed2019-07-11 20:54:11 -0700968 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700969 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700970
Waiman Long453431a2020-08-06 23:18:13 -0700971 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700972 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700973}
974
Patricia Alfonso73228c72020-10-13 16:55:06 -0700975static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800976{
977 void *area;
978
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100979 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800980
981 /*
982 * We have to be careful not to hit the guard page.
983 * The MMU will catch that and crash us.
984 */
985 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700986 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800987
Patricia Alfonso73228c72020-10-13 16:55:06 -0700988 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800989 vfree(area);
990}
Daniel Axtens06513912019-11-30 17:54:53 -0800991
Andrey Konovalov782ba452021-02-03 15:35:00 +1100992/*
993 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
994 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
995 * modes.
996 */
997static void match_all_not_assigned(struct kunit *test)
998{
999 char *ptr;
1000 struct page *pages;
1001 int i, size, order;
1002
1003 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1004
1005 for (i = 0; i < 256; i++) {
1006 size = (get_random_int() % 1024) + 1;
1007 ptr = kmalloc(size, GFP_KERNEL);
1008 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1009 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1010 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1011 kfree(ptr);
1012 }
1013
1014 for (i = 0; i < 256; i++) {
1015 order = (get_random_int() % 4) + 1;
1016 pages = alloc_pages(GFP_KERNEL, order);
1017 ptr = page_address(pages);
1018 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1019 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1020 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1021 free_pages((unsigned long)ptr, order);
1022 }
1023}
1024
1025/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1026static void match_all_ptr_tag(struct kunit *test)
1027{
1028 char *ptr;
1029 u8 tag;
1030
1031 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1032
1033 ptr = kmalloc(128, GFP_KERNEL);
1034 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1035
1036 /* Backup the assigned tag. */
1037 tag = get_tag(ptr);
1038 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1039
1040 /* Reset the tag to 0xff.*/
1041 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1042
1043 /* This access shouldn't trigger a KASAN report. */
1044 *ptr = 0;
1045
1046 /* Recover the pointer tag and free. */
1047 ptr = set_tag(ptr, tag);
1048 kfree(ptr);
1049}
1050
1051/* Check that there are no match-all memory tags for tag-based modes. */
1052static void match_all_mem_tag(struct kunit *test)
1053{
1054 char *ptr;
1055 int tag;
1056
1057 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1058
1059 ptr = kmalloc(128, GFP_KERNEL);
1060 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1061 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1062
1063 /* For each possible tag value not matching the pointer tag. */
1064 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1065 if (tag == get_tag(ptr))
1066 continue;
1067
1068 /* Mark the first memory granule with the chosen memory tag. */
Andrey Konovalov7186ac02021-03-18 17:01:40 +11001069 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
Andrey Konovalov782ba452021-02-03 15:35:00 +11001070
1071 /* This access must cause a KASAN report. */
1072 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1073 }
1074
1075 /* Recover the memory tag and free. */
Andrey Konovalov7186ac02021-03-18 17:01:40 +11001076 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
Andrey Konovalov782ba452021-02-03 15:35:00 +11001077 kfree(ptr);
1078}
1079
Patricia Alfonso73228c72020-10-13 16:55:06 -07001080static struct kunit_case kasan_kunit_test_cases[] = {
1081 KUNIT_CASE(kmalloc_oob_right),
1082 KUNIT_CASE(kmalloc_oob_left),
1083 KUNIT_CASE(kmalloc_node_oob_right),
1084 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1085 KUNIT_CASE(kmalloc_pagealloc_uaf),
1086 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalove449e272021-02-03 15:35:06 +11001087 KUNIT_CASE(pagealloc_oob_right),
1088 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001089 KUNIT_CASE(kmalloc_large_oob_right),
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +11001090 KUNIT_CASE(krealloc_more_oob),
1091 KUNIT_CASE(krealloc_less_oob),
1092 KUNIT_CASE(krealloc_pagealloc_more_oob),
1093 KUNIT_CASE(krealloc_pagealloc_less_oob),
Andrey Konovalov4f62c692021-02-12 17:14:50 +11001094 KUNIT_CASE(krealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001095 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001096 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001097 KUNIT_CASE(kmalloc_oob_in_memset),
1098 KUNIT_CASE(kmalloc_oob_memset_2),
1099 KUNIT_CASE(kmalloc_oob_memset_4),
1100 KUNIT_CASE(kmalloc_oob_memset_8),
1101 KUNIT_CASE(kmalloc_oob_memset_16),
1102 KUNIT_CASE(kmalloc_memmove_invalid_size),
1103 KUNIT_CASE(kmalloc_uaf),
1104 KUNIT_CASE(kmalloc_uaf_memset),
1105 KUNIT_CASE(kmalloc_uaf2),
1106 KUNIT_CASE(kfree_via_page),
1107 KUNIT_CASE(kfree_via_phys),
1108 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov9346eae2021-02-03 15:35:06 +11001109 KUNIT_CASE(kmem_cache_accounted),
1110 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001111 KUNIT_CASE(kasan_global_oob),
1112 KUNIT_CASE(kasan_stack_oob),
1113 KUNIT_CASE(kasan_alloca_oob_left),
1114 KUNIT_CASE(kasan_alloca_oob_right),
1115 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov696574e2021-02-03 15:35:05 +11001116 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001117 KUNIT_CASE(kmem_cache_double_free),
1118 KUNIT_CASE(kmem_cache_invalid_free),
1119 KUNIT_CASE(kasan_memchr),
1120 KUNIT_CASE(kasan_memcmp),
1121 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001122 KUNIT_CASE(kasan_bitops_generic),
1123 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001124 KUNIT_CASE(kmalloc_double_kzfree),
1125 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +11001126 KUNIT_CASE(match_all_not_assigned),
1127 KUNIT_CASE(match_all_ptr_tag),
1128 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001129 {}
1130};
Walter Wu387d6e42020-08-06 23:24:42 -07001131
Patricia Alfonso73228c72020-10-13 16:55:06 -07001132static struct kunit_suite kasan_kunit_test_suite = {
1133 .name = "kasan",
1134 .init = kasan_test_init,
1135 .test_cases = kasan_kunit_test_cases,
1136 .exit = kasan_test_exit,
1137};
Walter Wu387d6e42020-08-06 23:24:42 -07001138
Patricia Alfonso73228c72020-10-13 16:55:06 -07001139kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001140
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001141MODULE_LICENSE("GPL");