blob: 63c26171a791cf69bc223865a9ea49e25a886f61 [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>
16#include <linux/slab.h>
17#include <linux/string.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070018#include <linux/uaccess.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070019#include <linux/io.h>
Daniel Axtens06513912019-11-30 17:54:53 -080020#include <linux/vmalloc.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070021
22#include <asm/page.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080023
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070024#include <kunit/test.h>
25
Walter Wuf33a0142020-08-06 23:24:54 -070026#include "../mm/kasan/kasan.h"
27
28#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
29
Dmitry Vyukov828347f2016-11-30 15:54:16 -080030/*
Daniel Axtensadb72ae2020-06-03 15:56:43 -070031 * We assign some test results to these globals to make sure the tests
32 * are not eliminated as dead code.
33 */
34
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
42static int kasan_test_init(struct kunit *test)
43{
44 /*
45 * Temporarily enable multi-shot mode and set panic_on_warn=0.
46 * Otherwise, we'd only get a report for the first case.
47 */
48 multishot = kasan_save_enable_multi_shot();
49
50 return 0;
51}
52
53static void kasan_test_exit(struct kunit *test)
54{
55 kasan_restore_multi_shot(multishot);
56}
57
58/**
59 * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
60 * not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
61 * Do not use this name for a KUnit resource outside here.
62 *
63 */
64#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
65 fail_data.report_expected = true; \
66 fail_data.report_found = false; \
67 kunit_add_named_resource(test, \
68 NULL, \
69 NULL, \
70 &resource, \
71 "kasan_data", &fail_data); \
72 condition; \
73 KUNIT_EXPECT_EQ(test, \
74 fail_data.report_expected, \
75 fail_data.report_found); \
76} while (0)
77
Patricia Alfonso73228c72020-10-13 16:55:06 -070078static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -080079{
80 char *ptr;
81 size_t size = 123;
82
Andrey Ryabinin3f158012015-02-13 14:39:53 -080083 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -070084 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -080085
Patricia Alfonso73228c72020-10-13 16:55:06 -070086 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -080087 kfree(ptr);
88}
89
Patricia Alfonso73228c72020-10-13 16:55:06 -070090static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -080091{
92 char *ptr;
93 size_t size = 15;
94
Andrey Ryabinin3f158012015-02-13 14:39:53 -080095 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -070096 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -080097
Patricia Alfonso73228c72020-10-13 16:55:06 -070098 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -080099 kfree(ptr);
100}
101
Patricia Alfonso73228c72020-10-13 16:55:06 -0700102static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800103{
104 char *ptr;
105 size_t size = 4096;
106
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800107 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700108 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800109
Patricia Alfonso73228c72020-10-13 16:55:06 -0700110 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800111 kfree(ptr);
112}
113
Patricia Alfonso73228c72020-10-13 16:55:06 -0700114static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800115{
116 char *ptr;
117 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
118
Patricia Alfonso73228c72020-10-13 16:55:06 -0700119 if (!IS_ENABLED(CONFIG_SLUB)) {
120 kunit_info(test, "CONFIG_SLUB is not enabled.");
121 return;
122 }
123
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700124 /* Allocate a chunk that does not fit into a SLUB cache to trigger
125 * the page allocator fallback.
126 */
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700127 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700128 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700129
Patricia Alfonso73228c72020-10-13 16:55:06 -0700130 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700131 kfree(ptr);
132}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800133
Patricia Alfonso73228c72020-10-13 16:55:06 -0700134static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800135{
136 char *ptr;
137 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
138
Patricia Alfonso73228c72020-10-13 16:55:06 -0700139 if (!IS_ENABLED(CONFIG_SLUB)) {
140 kunit_info(test, "CONFIG_SLUB is not enabled.");
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800141 return;
142 }
143
Patricia Alfonso73228c72020-10-13 16:55:06 -0700144 ptr = kmalloc(size, GFP_KERNEL);
145 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
146
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800147 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700148 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800149}
150
Patricia Alfonso73228c72020-10-13 16:55:06 -0700151static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800152{
153 char *ptr;
154 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
155
Patricia Alfonso73228c72020-10-13 16:55:06 -0700156 if (!IS_ENABLED(CONFIG_SLUB)) {
157 kunit_info(test, "CONFIG_SLUB is not enabled.");
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800158 return;
159 }
160
Patricia Alfonso73228c72020-10-13 16:55:06 -0700161 ptr = kmalloc(size, GFP_KERNEL);
162 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700163
Patricia Alfonso73228c72020-10-13 16:55:06 -0700164 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
165}
166
167static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700168{
169 char *ptr;
170 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
171 /* Allocate a chunk that is large enough, but still fits into a slab
172 * and does not trigger the page allocator fallback in SLUB.
173 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800174 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700175 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800176
Patricia Alfonso73228c72020-10-13 16:55:06 -0700177 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800178 kfree(ptr);
179}
180
Patricia Alfonso73228c72020-10-13 16:55:06 -0700181static void kmalloc_oob_krealloc_more(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800182{
183 char *ptr1, *ptr2;
184 size_t size1 = 17;
185 size_t size2 = 19;
186
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800187 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700188 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
189
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800190 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700191 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800192
Patricia Alfonso73228c72020-10-13 16:55:06 -0700193 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800194 kfree(ptr2);
195}
196
Patricia Alfonso73228c72020-10-13 16:55:06 -0700197static void kmalloc_oob_krealloc_less(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800198{
199 char *ptr1, *ptr2;
200 size_t size1 = 17;
201 size_t size2 = 15;
202
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800203 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700204 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
205
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800206 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700207 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700208
Patricia Alfonso73228c72020-10-13 16:55:06 -0700209 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800210 kfree(ptr2);
211}
212
Patricia Alfonso73228c72020-10-13 16:55:06 -0700213static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800214{
215 struct {
216 u64 words[2];
217 } *ptr1, *ptr2;
218
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800219 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700220 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
221
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800222 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700223 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
224
225 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800226 kfree(ptr1);
227 kfree(ptr2);
228}
229
Patricia Alfonso73228c72020-10-13 16:55:06 -0700230static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800231{
232 char *ptr;
233 size_t size = 8;
234
Wang Longf523e732015-11-05 18:51:15 -0800235 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800237
Patricia Alfonso73228c72020-10-13 16:55:06 -0700238 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800239 kfree(ptr);
240}
241
Patricia Alfonso73228c72020-10-13 16:55:06 -0700242static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800243{
244 char *ptr;
245 size_t size = 8;
246
Wang Longf523e732015-11-05 18:51:15 -0800247 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700248 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800249
Patricia Alfonso73228c72020-10-13 16:55:06 -0700250 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800251 kfree(ptr);
252}
253
254
Patricia Alfonso73228c72020-10-13 16:55:06 -0700255static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800256{
257 char *ptr;
258 size_t size = 8;
259
Wang Longf523e732015-11-05 18:51:15 -0800260 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700261 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800262
Patricia Alfonso73228c72020-10-13 16:55:06 -0700263 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800264 kfree(ptr);
265}
266
Patricia Alfonso73228c72020-10-13 16:55:06 -0700267static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800268{
269 char *ptr;
270 size_t size = 16;
271
Wang Longf523e732015-11-05 18:51:15 -0800272 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700273 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800274
Patricia Alfonso73228c72020-10-13 16:55:06 -0700275 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800276 kfree(ptr);
277}
278
Patricia Alfonso73228c72020-10-13 16:55:06 -0700279static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800280{
281 char *ptr;
282 size_t size = 666;
283
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800284 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700285 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800286
Patricia Alfonso73228c72020-10-13 16:55:06 -0700287 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800288 kfree(ptr);
289}
290
Patricia Alfonso73228c72020-10-13 16:55:06 -0700291static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700292{
293 char *ptr;
294 size_t size = 64;
295 volatile size_t invalid_size = -2;
296
Walter Wu98f3b562020-04-01 21:09:40 -0700297 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700298 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700299
300 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700301
302 KUNIT_EXPECT_KASAN_FAIL(test,
303 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700304 kfree(ptr);
305}
306
Patricia Alfonso73228c72020-10-13 16:55:06 -0700307static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800308{
309 char *ptr;
310 size_t size = 10;
311
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800312 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700313 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800314
315 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700316 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800317}
318
Patricia Alfonso73228c72020-10-13 16:55:06 -0700319static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800320{
321 char *ptr;
322 size_t size = 33;
323
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800324 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700325 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800326
327 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700328 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800329}
330
Patricia Alfonso73228c72020-10-13 16:55:06 -0700331static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800332{
333 char *ptr1, *ptr2;
334 size_t size = 43;
335
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800336 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700337 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800338
339 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800340
Patricia Alfonso73228c72020-10-13 16:55:06 -0700341 ptr2 = kmalloc(size, GFP_KERNEL);
342 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
343
344 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
345 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
346
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800347 kfree(ptr2);
348}
349
Patricia Alfonso73228c72020-10-13 16:55:06 -0700350static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700351{
352 char *ptr;
353 size_t size = 8;
354 struct page *page;
355 unsigned long offset;
356
Mark Rutlandb92a9532019-09-23 15:34:16 -0700357 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700358 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700359
360 page = virt_to_page(ptr);
361 offset = offset_in_page(ptr);
362 kfree(page_address(page) + offset);
363}
364
Patricia Alfonso73228c72020-10-13 16:55:06 -0700365static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700366{
367 char *ptr;
368 size_t size = 8;
369 phys_addr_t phys;
370
Mark Rutlandb92a9532019-09-23 15:34:16 -0700371 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700372 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700373
374 phys = virt_to_phys(ptr);
375 kfree(phys_to_virt(phys));
376}
377
Patricia Alfonso73228c72020-10-13 16:55:06 -0700378static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800379{
380 char *p;
381 size_t size = 200;
382 struct kmem_cache *cache = kmem_cache_create("test_cache",
383 size, 0,
384 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700385 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800386 p = kmem_cache_alloc(cache, GFP_KERNEL);
387 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700388 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800389 kmem_cache_destroy(cache);
390 return;
391 }
392
Patricia Alfonso73228c72020-10-13 16:55:06 -0700393 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800394 kmem_cache_free(cache, p);
395 kmem_cache_destroy(cache);
396}
397
Patricia Alfonso73228c72020-10-13 16:55:06 -0700398static void memcg_accounted_kmem_cache(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800399{
400 int i;
401 char *p;
402 size_t size = 200;
403 struct kmem_cache *cache;
404
405 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700406 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800407
Greg Thelen0386bf32017-02-24 15:00:08 -0800408 /*
409 * Several allocations with a delay to allow for lazy per memcg kmem
410 * cache creation.
411 */
412 for (i = 0; i < 5; i++) {
413 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800414 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800415 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800416
Greg Thelen0386bf32017-02-24 15:00:08 -0800417 kmem_cache_free(cache, p);
418 msleep(100);
419 }
420
421free_cache:
422 kmem_cache_destroy(cache);
423}
424
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800425static char global_array[10];
426
Patricia Alfonso73228c72020-10-13 16:55:06 -0700427static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800428{
429 volatile int i = 3;
430 char *p = &global_array[ARRAY_SIZE(global_array) + i];
431
Patricia Alfonso73228c72020-10-13 16:55:06 -0700432 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800433}
434
Patricia Alfonso73228c72020-10-13 16:55:06 -0700435static void ksize_unpoisons_memory(struct kunit *test)
436{
437 char *ptr;
438 size_t size = 123, real_size;
439
440 ptr = kmalloc(size, GFP_KERNEL);
441 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
442 real_size = ksize(ptr);
443 /* This access doesn't trigger an error. */
444 ptr[size] = 'x';
445 /* This one does. */
446 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
447 kfree(ptr);
448}
449
450static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800451{
452 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700453 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800454 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
455
Patricia Alfonso73228c72020-10-13 16:55:06 -0700456 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
457 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700458 return;
459 }
460
Patricia Alfonso73228c72020-10-13 16:55:06 -0700461 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700462}
463
Patricia Alfonso73228c72020-10-13 16:55:06 -0700464static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800465{
466 volatile int i = 10;
467 char alloca_array[i];
468 char *p = alloca_array - 1;
469
Patricia Alfonso73228c72020-10-13 16:55:06 -0700470 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
471 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
472 return;
473 }
474
475 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800476}
477
Patricia Alfonso73228c72020-10-13 16:55:06 -0700478static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800479{
480 volatile int i = 10;
481 char alloca_array[i];
482 char *p = alloca_array + i;
483
Patricia Alfonso73228c72020-10-13 16:55:06 -0700484 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
485 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
486 return;
487 }
488
489 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800490}
491
Patricia Alfonso73228c72020-10-13 16:55:06 -0700492static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800493{
494 char *p;
495 size_t size = 200;
496 struct kmem_cache *cache;
497
498 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700499 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
500
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800501 p = kmem_cache_alloc(cache, GFP_KERNEL);
502 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700503 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800504 kmem_cache_destroy(cache);
505 return;
506 }
507
508 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700509 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800510 kmem_cache_destroy(cache);
511}
512
Patricia Alfonso73228c72020-10-13 16:55:06 -0700513static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800514{
515 char *p;
516 size_t size = 200;
517 struct kmem_cache *cache;
518
519 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
520 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700521 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
522
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800523 p = kmem_cache_alloc(cache, GFP_KERNEL);
524 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700525 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800526 kmem_cache_destroy(cache);
527 return;
528 }
529
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700530 /* Trigger invalid free, the object doesn't get freed */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700531 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700532
533 /*
534 * Properly free the object to prevent the "Objects remaining in
535 * test_cache on __kmem_cache_shutdown" BUG failure.
536 */
537 kmem_cache_free(cache, p);
538
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800539 kmem_cache_destroy(cache);
540}
541
Patricia Alfonso73228c72020-10-13 16:55:06 -0700542static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700543{
544 char *ptr;
545 size_t size = 24;
546
Patricia Alfonso73228c72020-10-13 16:55:06 -0700547 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
548 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
549 kunit_info(test,
550 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700551 return;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700552 }
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700553
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
555 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
556
557 KUNIT_EXPECT_KASAN_FAIL(test,
558 kasan_ptr_result = memchr(ptr, '1', size + 1));
559
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700560 kfree(ptr);
561}
562
Patricia Alfonso73228c72020-10-13 16:55:06 -0700563static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700564{
565 char *ptr;
566 size_t size = 24;
567 int arr[9];
568
Patricia Alfonso73228c72020-10-13 16:55:06 -0700569 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
570 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
571 kunit_info(test,
572 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700573 return;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700574 }
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700575
Patricia Alfonso73228c72020-10-13 16:55:06 -0700576 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
577 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700578 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700579
580 KUNIT_EXPECT_KASAN_FAIL(test,
581 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700582 kfree(ptr);
583}
584
Patricia Alfonso73228c72020-10-13 16:55:06 -0700585static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700586{
587 char *ptr;
588 size_t size = 24;
589
Patricia Alfonso73228c72020-10-13 16:55:06 -0700590 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
591 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
592 kunit_info(test,
593 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700594 return;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700595 }
596
597 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
598 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700599
600 kfree(ptr);
601
602 /*
603 * Try to cause only 1 invalid access (less spam in dmesg).
604 * For that we need ptr to point to zeroed byte.
605 * Skip metadata that could be stored in freed object so ptr
606 * will likely point to zeroed byte.
607 */
608 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700609 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700610
Patricia Alfonso73228c72020-10-13 16:55:06 -0700611 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700612
Patricia Alfonso73228c72020-10-13 16:55:06 -0700613 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700614
Patricia Alfonso73228c72020-10-13 16:55:06 -0700615 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700616
Patricia Alfonso73228c72020-10-13 16:55:06 -0700617 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700618
Patricia Alfonso73228c72020-10-13 16:55:06 -0700619 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700620}
621
Patricia Alfonso73228c72020-10-13 16:55:06 -0700622static void kasan_bitops(struct kunit *test)
Marco Elver19a33ca2019-07-11 20:53:52 -0700623{
624 /*
625 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
626 * this way we do not actually corrupt other memory.
627 */
628 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700629 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700630
631 /*
632 * Below calls try to access bit within allocated memory; however, the
633 * below accesses are still out-of-bounds, since bitops are defined to
634 * operate on the whole long the bit is in.
635 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700636 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700637
Patricia Alfonso73228c72020-10-13 16:55:06 -0700638 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700639
Patricia Alfonso73228c72020-10-13 16:55:06 -0700640 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700641
Patricia Alfonso73228c72020-10-13 16:55:06 -0700642 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700643
Patricia Alfonso73228c72020-10-13 16:55:06 -0700644 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700645
Patricia Alfonso73228c72020-10-13 16:55:06 -0700646 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700647
Patricia Alfonso73228c72020-10-13 16:55:06 -0700648 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700649
Patricia Alfonso73228c72020-10-13 16:55:06 -0700650 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(BITS_PER_LONG, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700651
652 /*
653 * Below calls try to access bit beyond allocated memory.
654 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700655 KUNIT_EXPECT_KASAN_FAIL(test,
656 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700657
Patricia Alfonso73228c72020-10-13 16:55:06 -0700658 KUNIT_EXPECT_KASAN_FAIL(test,
659 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700660
Patricia Alfonso73228c72020-10-13 16:55:06 -0700661 KUNIT_EXPECT_KASAN_FAIL(test,
662 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700663
Patricia Alfonso73228c72020-10-13 16:55:06 -0700664 KUNIT_EXPECT_KASAN_FAIL(test,
665 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700666
Patricia Alfonso73228c72020-10-13 16:55:06 -0700667 KUNIT_EXPECT_KASAN_FAIL(test,
668 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700669
Patricia Alfonso73228c72020-10-13 16:55:06 -0700670 KUNIT_EXPECT_KASAN_FAIL(test,
671 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700672
Patricia Alfonso73228c72020-10-13 16:55:06 -0700673 KUNIT_EXPECT_KASAN_FAIL(test,
674 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700675
Patricia Alfonso73228c72020-10-13 16:55:06 -0700676 KUNIT_EXPECT_KASAN_FAIL(test,
677 kasan_int_result =
678 test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700679
680#if defined(clear_bit_unlock_is_negative_byte)
Patricia Alfonso73228c72020-10-13 16:55:06 -0700681 KUNIT_EXPECT_KASAN_FAIL(test,
682 kasan_int_result = clear_bit_unlock_is_negative_byte(
683 BITS_PER_LONG + BITS_PER_BYTE, bits));
Marco Elver19a33ca2019-07-11 20:53:52 -0700684#endif
685 kfree(bits);
686}
687
Patricia Alfonso73228c72020-10-13 16:55:06 -0700688static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700689{
690 char *ptr;
691 size_t size = 16;
692
Marco Elverbb104ed2019-07-11 20:54:11 -0700693 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700694 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700695
Waiman Long453431a2020-08-06 23:18:13 -0700696 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700697 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700698}
699
Patricia Alfonso73228c72020-10-13 16:55:06 -0700700static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800701{
702 void *area;
703
Patricia Alfonso73228c72020-10-13 16:55:06 -0700704 if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
705 kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled.");
706 return;
707 }
Daniel Axtens06513912019-11-30 17:54:53 -0800708
709 /*
710 * We have to be careful not to hit the guard page.
711 * The MMU will catch that and crash us.
712 */
713 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700714 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800715
Patricia Alfonso73228c72020-10-13 16:55:06 -0700716 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800717 vfree(area);
718}
Daniel Axtens06513912019-11-30 17:54:53 -0800719
Patricia Alfonso73228c72020-10-13 16:55:06 -0700720static struct kunit_case kasan_kunit_test_cases[] = {
721 KUNIT_CASE(kmalloc_oob_right),
722 KUNIT_CASE(kmalloc_oob_left),
723 KUNIT_CASE(kmalloc_node_oob_right),
724 KUNIT_CASE(kmalloc_pagealloc_oob_right),
725 KUNIT_CASE(kmalloc_pagealloc_uaf),
726 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
727 KUNIT_CASE(kmalloc_large_oob_right),
728 KUNIT_CASE(kmalloc_oob_krealloc_more),
729 KUNIT_CASE(kmalloc_oob_krealloc_less),
730 KUNIT_CASE(kmalloc_oob_16),
731 KUNIT_CASE(kmalloc_oob_in_memset),
732 KUNIT_CASE(kmalloc_oob_memset_2),
733 KUNIT_CASE(kmalloc_oob_memset_4),
734 KUNIT_CASE(kmalloc_oob_memset_8),
735 KUNIT_CASE(kmalloc_oob_memset_16),
736 KUNIT_CASE(kmalloc_memmove_invalid_size),
737 KUNIT_CASE(kmalloc_uaf),
738 KUNIT_CASE(kmalloc_uaf_memset),
739 KUNIT_CASE(kmalloc_uaf2),
740 KUNIT_CASE(kfree_via_page),
741 KUNIT_CASE(kfree_via_phys),
742 KUNIT_CASE(kmem_cache_oob),
743 KUNIT_CASE(memcg_accounted_kmem_cache),
744 KUNIT_CASE(kasan_global_oob),
745 KUNIT_CASE(kasan_stack_oob),
746 KUNIT_CASE(kasan_alloca_oob_left),
747 KUNIT_CASE(kasan_alloca_oob_right),
748 KUNIT_CASE(ksize_unpoisons_memory),
749 KUNIT_CASE(kmem_cache_double_free),
750 KUNIT_CASE(kmem_cache_invalid_free),
751 KUNIT_CASE(kasan_memchr),
752 KUNIT_CASE(kasan_memcmp),
753 KUNIT_CASE(kasan_strings),
754 KUNIT_CASE(kasan_bitops),
755 KUNIT_CASE(kmalloc_double_kzfree),
756 KUNIT_CASE(vmalloc_oob),
757 {}
758};
Walter Wu387d6e42020-08-06 23:24:42 -0700759
Patricia Alfonso73228c72020-10-13 16:55:06 -0700760static struct kunit_suite kasan_kunit_test_suite = {
761 .name = "kasan",
762 .init = kasan_test_init,
763 .test_cases = kasan_kunit_test_cases,
764 .exit = kasan_test_exit,
765};
Walter Wu387d6e42020-08-06 23:24:42 -0700766
Patricia Alfonso73228c72020-10-13 16:55:06 -0700767kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -0700768
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800769MODULE_LICENSE("GPL");