blob: 662f862702fc8da9f3180adc48636360c6edad71 [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 Konovalov58b999d2020-11-01 17:07:37 -0800219 /* This test is specifically crafted for the generic mode. */
220 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
221 kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
222 return;
223 }
224
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800225 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700226 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
227
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800228 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700229 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
230
231 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800232 kfree(ptr1);
233 kfree(ptr2);
234}
235
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800236static void kmalloc_uaf_16(struct kunit *test)
237{
238 struct {
239 u64 words[2];
240 } *ptr1, *ptr2;
241
242 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
243 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
244
245 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
246 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
247 kfree(ptr2);
248
249 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
250 kfree(ptr1);
251}
252
Patricia Alfonso73228c72020-10-13 16:55:06 -0700253static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800254{
255 char *ptr;
256 size_t size = 8;
257
Wang Longf523e732015-11-05 18:51:15 -0800258 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700259 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800260
Patricia Alfonso73228c72020-10-13 16:55:06 -0700261 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800262 kfree(ptr);
263}
264
Patricia Alfonso73228c72020-10-13 16:55:06 -0700265static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800266{
267 char *ptr;
268 size_t size = 8;
269
Wang Longf523e732015-11-05 18:51:15 -0800270 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700271 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800272
Patricia Alfonso73228c72020-10-13 16:55:06 -0700273 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800274 kfree(ptr);
275}
276
277
Patricia Alfonso73228c72020-10-13 16:55:06 -0700278static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800279{
280 char *ptr;
281 size_t size = 8;
282
Wang Longf523e732015-11-05 18:51:15 -0800283 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700284 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800285
Patricia Alfonso73228c72020-10-13 16:55:06 -0700286 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800287 kfree(ptr);
288}
289
Patricia Alfonso73228c72020-10-13 16:55:06 -0700290static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800291{
292 char *ptr;
293 size_t size = 16;
294
Wang Longf523e732015-11-05 18:51:15 -0800295 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700296 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800297
Patricia Alfonso73228c72020-10-13 16:55:06 -0700298 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800299 kfree(ptr);
300}
301
Patricia Alfonso73228c72020-10-13 16:55:06 -0700302static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800303{
304 char *ptr;
305 size_t size = 666;
306
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800307 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700308 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800309
Patricia Alfonso73228c72020-10-13 16:55:06 -0700310 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800311 kfree(ptr);
312}
313
Patricia Alfonso73228c72020-10-13 16:55:06 -0700314static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700315{
316 char *ptr;
317 size_t size = 64;
318 volatile size_t invalid_size = -2;
319
Walter Wu98f3b562020-04-01 21:09:40 -0700320 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700321 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700322
323 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700324
325 KUNIT_EXPECT_KASAN_FAIL(test,
326 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700327 kfree(ptr);
328}
329
Patricia Alfonso73228c72020-10-13 16:55:06 -0700330static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800331{
332 char *ptr;
333 size_t size = 10;
334
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800335 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700336 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800337
338 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700339 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800340}
341
Patricia Alfonso73228c72020-10-13 16:55:06 -0700342static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800343{
344 char *ptr;
345 size_t size = 33;
346
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800347 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700348 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800349
350 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700351 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800352}
353
Patricia Alfonso73228c72020-10-13 16:55:06 -0700354static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800355{
356 char *ptr1, *ptr2;
357 size_t size = 43;
358
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800359 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700360 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800361
362 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800363
Patricia Alfonso73228c72020-10-13 16:55:06 -0700364 ptr2 = kmalloc(size, GFP_KERNEL);
365 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
366
367 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
368 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
369
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800370 kfree(ptr2);
371}
372
Patricia Alfonso73228c72020-10-13 16:55:06 -0700373static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700374{
375 char *ptr;
376 size_t size = 8;
377 struct page *page;
378 unsigned long offset;
379
Mark Rutlandb92a9532019-09-23 15:34:16 -0700380 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700381 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700382
383 page = virt_to_page(ptr);
384 offset = offset_in_page(ptr);
385 kfree(page_address(page) + offset);
386}
387
Patricia Alfonso73228c72020-10-13 16:55:06 -0700388static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700389{
390 char *ptr;
391 size_t size = 8;
392 phys_addr_t phys;
393
Mark Rutlandb92a9532019-09-23 15:34:16 -0700394 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700395 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700396
397 phys = virt_to_phys(ptr);
398 kfree(phys_to_virt(phys));
399}
400
Patricia Alfonso73228c72020-10-13 16:55:06 -0700401static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800402{
403 char *p;
404 size_t size = 200;
405 struct kmem_cache *cache = kmem_cache_create("test_cache",
406 size, 0,
407 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700408 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800409 p = kmem_cache_alloc(cache, GFP_KERNEL);
410 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700411 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800412 kmem_cache_destroy(cache);
413 return;
414 }
415
Patricia Alfonso73228c72020-10-13 16:55:06 -0700416 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800417 kmem_cache_free(cache, p);
418 kmem_cache_destroy(cache);
419}
420
Patricia Alfonso73228c72020-10-13 16:55:06 -0700421static void memcg_accounted_kmem_cache(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800422{
423 int i;
424 char *p;
425 size_t size = 200;
426 struct kmem_cache *cache;
427
428 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700429 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800430
Greg Thelen0386bf32017-02-24 15:00:08 -0800431 /*
432 * Several allocations with a delay to allow for lazy per memcg kmem
433 * cache creation.
434 */
435 for (i = 0; i < 5; i++) {
436 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800437 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800438 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800439
Greg Thelen0386bf32017-02-24 15:00:08 -0800440 kmem_cache_free(cache, p);
441 msleep(100);
442 }
443
444free_cache:
445 kmem_cache_destroy(cache);
446}
447
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800448static char global_array[10];
449
Patricia Alfonso73228c72020-10-13 16:55:06 -0700450static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800451{
452 volatile int i = 3;
453 char *p = &global_array[ARRAY_SIZE(global_array) + i];
454
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800455 /* Only generic mode instruments globals. */
456 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
457 kunit_info(test, "CONFIG_KASAN_GENERIC required");
458 return;
459 }
460
Patricia Alfonso73228c72020-10-13 16:55:06 -0700461 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800462}
463
Patricia Alfonso73228c72020-10-13 16:55:06 -0700464static void ksize_unpoisons_memory(struct kunit *test)
465{
466 char *ptr;
467 size_t size = 123, real_size;
468
469 ptr = kmalloc(size, GFP_KERNEL);
470 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
471 real_size = ksize(ptr);
472 /* This access doesn't trigger an error. */
473 ptr[size] = 'x';
474 /* This one does. */
475 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
476 kfree(ptr);
477}
478
479static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800480{
481 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700482 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800483 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
484
Patricia Alfonso73228c72020-10-13 16:55:06 -0700485 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
486 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700487 return;
488 }
489
Patricia Alfonso73228c72020-10-13 16:55:06 -0700490 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700491}
492
Patricia Alfonso73228c72020-10-13 16:55:06 -0700493static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800494{
495 volatile int i = 10;
496 char alloca_array[i];
497 char *p = alloca_array - 1;
498
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800499 /* Only generic mode instruments dynamic allocas. */
500 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
501 kunit_info(test, "CONFIG_KASAN_GENERIC required");
502 return;
503 }
504
Patricia Alfonso73228c72020-10-13 16:55:06 -0700505 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
506 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
507 return;
508 }
509
510 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800511}
512
Patricia Alfonso73228c72020-10-13 16:55:06 -0700513static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800514{
515 volatile int i = 10;
516 char alloca_array[i];
517 char *p = alloca_array + i;
518
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800519 /* Only generic mode instruments dynamic allocas. */
520 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
521 kunit_info(test, "CONFIG_KASAN_GENERIC required");
522 return;
523 }
524
Patricia Alfonso73228c72020-10-13 16:55:06 -0700525 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
526 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
527 return;
528 }
529
530 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800531}
532
Patricia Alfonso73228c72020-10-13 16:55:06 -0700533static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800534{
535 char *p;
536 size_t size = 200;
537 struct kmem_cache *cache;
538
539 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700540 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
541
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800542 p = kmem_cache_alloc(cache, GFP_KERNEL);
543 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700544 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800545 kmem_cache_destroy(cache);
546 return;
547 }
548
549 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700550 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800551 kmem_cache_destroy(cache);
552}
553
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800555{
556 char *p;
557 size_t size = 200;
558 struct kmem_cache *cache;
559
560 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
561 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700562 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
563
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800564 p = kmem_cache_alloc(cache, GFP_KERNEL);
565 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700566 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800567 kmem_cache_destroy(cache);
568 return;
569 }
570
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700571 /* Trigger invalid free, the object doesn't get freed */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700572 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700573
574 /*
575 * Properly free the object to prevent the "Objects remaining in
576 * test_cache on __kmem_cache_shutdown" BUG failure.
577 */
578 kmem_cache_free(cache, p);
579
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800580 kmem_cache_destroy(cache);
581}
582
Patricia Alfonso73228c72020-10-13 16:55:06 -0700583static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700584{
585 char *ptr;
586 size_t size = 24;
587
Patricia Alfonso73228c72020-10-13 16:55:06 -0700588 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
589 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
590 kunit_info(test,
591 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700592 return;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700593 }
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700594
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800595 if (OOB_TAG_OFF)
596 size = round_up(size, OOB_TAG_OFF);
597
Patricia Alfonso73228c72020-10-13 16:55:06 -0700598 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
599 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
600
601 KUNIT_EXPECT_KASAN_FAIL(test,
602 kasan_ptr_result = memchr(ptr, '1', size + 1));
603
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700604 kfree(ptr);
605}
606
Patricia Alfonso73228c72020-10-13 16:55:06 -0700607static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700608{
609 char *ptr;
610 size_t size = 24;
611 int arr[9];
612
Patricia Alfonso73228c72020-10-13 16:55:06 -0700613 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
614 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
615 kunit_info(test,
616 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700617 return;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700618 }
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700619
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800620 if (OOB_TAG_OFF)
621 size = round_up(size, OOB_TAG_OFF);
622
Patricia Alfonso73228c72020-10-13 16:55:06 -0700623 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
624 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700625 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700626
627 KUNIT_EXPECT_KASAN_FAIL(test,
628 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700629 kfree(ptr);
630}
631
Patricia Alfonso73228c72020-10-13 16:55:06 -0700632static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700633{
634 char *ptr;
635 size_t size = 24;
636
Patricia Alfonso73228c72020-10-13 16:55:06 -0700637 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
638 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
639 kunit_info(test,
640 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700641 return;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700642 }
643
644 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
645 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700646
647 kfree(ptr);
648
649 /*
650 * Try to cause only 1 invalid access (less spam in dmesg).
651 * For that we need ptr to point to zeroed byte.
652 * Skip metadata that could be stored in freed object so ptr
653 * will likely point to zeroed byte.
654 */
655 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700656 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700657
Patricia Alfonso73228c72020-10-13 16:55:06 -0700658 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700659
Patricia Alfonso73228c72020-10-13 16:55:06 -0700660 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700661
Patricia Alfonso73228c72020-10-13 16:55:06 -0700662 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700663
Patricia Alfonso73228c72020-10-13 16:55:06 -0700664 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700665
Patricia Alfonso73228c72020-10-13 16:55:06 -0700666 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700667}
668
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800669static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700670{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800671 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
672 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
673 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
674 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
675 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
676 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
677 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
678 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
679}
680
681static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
682{
683 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
684 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
685 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
686 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
687 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
688 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
689 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
690 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
691
692#if defined(clear_bit_unlock_is_negative_byte)
693 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
694 clear_bit_unlock_is_negative_byte(nr, addr));
695#endif
696}
697
698static void kasan_bitops_generic(struct kunit *test)
699{
700 long *bits;
701
702 /* This test is specifically crafted for the generic mode. */
703 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
704 kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
705 return;
706 }
707
Marco Elver19a33ca2019-07-11 20:53:52 -0700708 /*
709 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
710 * this way we do not actually corrupt other memory.
711 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800712 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700713 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700714
715 /*
716 * Below calls try to access bit within allocated memory; however, the
717 * below accesses are still out-of-bounds, since bitops are defined to
718 * operate on the whole long the bit is in.
719 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800720 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700721
722 /*
723 * Below calls try to access bit beyond allocated memory.
724 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800725 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700726
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800727 kfree(bits);
728}
Marco Elver19a33ca2019-07-11 20:53:52 -0700729
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800730static void kasan_bitops_tags(struct kunit *test)
731{
732 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700733
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800734 /* This test is specifically crafted for the tag-based mode. */
735 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
736 kunit_info(test, "CONFIG_KASAN_SW_TAGS required\n");
737 return;
738 }
Marco Elver19a33ca2019-07-11 20:53:52 -0700739
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800740 /* Allocation size will be rounded to up granule size, which is 16. */
741 bits = kzalloc(sizeof(*bits), GFP_KERNEL);
742 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700743
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800744 /* Do the accesses past the 16 allocated bytes. */
745 kasan_bitops_modify(test, BITS_PER_LONG, &bits[1]);
746 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, &bits[1]);
Marco Elver19a33ca2019-07-11 20:53:52 -0700747
Marco Elver19a33ca2019-07-11 20:53:52 -0700748 kfree(bits);
749}
750
Patricia Alfonso73228c72020-10-13 16:55:06 -0700751static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700752{
753 char *ptr;
754 size_t size = 16;
755
Marco Elverbb104ed2019-07-11 20:54:11 -0700756 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700757 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700758
Waiman Long453431a2020-08-06 23:18:13 -0700759 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700760 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700761}
762
Patricia Alfonso73228c72020-10-13 16:55:06 -0700763static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800764{
765 void *area;
766
Patricia Alfonso73228c72020-10-13 16:55:06 -0700767 if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
768 kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled.");
769 return;
770 }
Daniel Axtens06513912019-11-30 17:54:53 -0800771
772 /*
773 * We have to be careful not to hit the guard page.
774 * The MMU will catch that and crash us.
775 */
776 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700777 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800778
Patricia Alfonso73228c72020-10-13 16:55:06 -0700779 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800780 vfree(area);
781}
Daniel Axtens06513912019-11-30 17:54:53 -0800782
Patricia Alfonso73228c72020-10-13 16:55:06 -0700783static struct kunit_case kasan_kunit_test_cases[] = {
784 KUNIT_CASE(kmalloc_oob_right),
785 KUNIT_CASE(kmalloc_oob_left),
786 KUNIT_CASE(kmalloc_node_oob_right),
787 KUNIT_CASE(kmalloc_pagealloc_oob_right),
788 KUNIT_CASE(kmalloc_pagealloc_uaf),
789 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
790 KUNIT_CASE(kmalloc_large_oob_right),
791 KUNIT_CASE(kmalloc_oob_krealloc_more),
792 KUNIT_CASE(kmalloc_oob_krealloc_less),
793 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800794 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700795 KUNIT_CASE(kmalloc_oob_in_memset),
796 KUNIT_CASE(kmalloc_oob_memset_2),
797 KUNIT_CASE(kmalloc_oob_memset_4),
798 KUNIT_CASE(kmalloc_oob_memset_8),
799 KUNIT_CASE(kmalloc_oob_memset_16),
800 KUNIT_CASE(kmalloc_memmove_invalid_size),
801 KUNIT_CASE(kmalloc_uaf),
802 KUNIT_CASE(kmalloc_uaf_memset),
803 KUNIT_CASE(kmalloc_uaf2),
804 KUNIT_CASE(kfree_via_page),
805 KUNIT_CASE(kfree_via_phys),
806 KUNIT_CASE(kmem_cache_oob),
807 KUNIT_CASE(memcg_accounted_kmem_cache),
808 KUNIT_CASE(kasan_global_oob),
809 KUNIT_CASE(kasan_stack_oob),
810 KUNIT_CASE(kasan_alloca_oob_left),
811 KUNIT_CASE(kasan_alloca_oob_right),
812 KUNIT_CASE(ksize_unpoisons_memory),
813 KUNIT_CASE(kmem_cache_double_free),
814 KUNIT_CASE(kmem_cache_invalid_free),
815 KUNIT_CASE(kasan_memchr),
816 KUNIT_CASE(kasan_memcmp),
817 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800818 KUNIT_CASE(kasan_bitops_generic),
819 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700820 KUNIT_CASE(kmalloc_double_kzfree),
821 KUNIT_CASE(vmalloc_oob),
822 {}
823};
Walter Wu387d6e42020-08-06 23:24:42 -0700824
Patricia Alfonso73228c72020-10-13 16:55:06 -0700825static struct kunit_suite kasan_kunit_test_suite = {
826 .name = "kasan",
827 .init = kasan_test_init,
828 .test_cases = kasan_kunit_test_cases,
829 .exit = kasan_test_exit,
830};
Walter Wu387d6e42020-08-06 23:24:42 -0700831
Patricia Alfonso73228c72020-10-13 16:55:06 -0700832kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -0700833
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800834MODULE_LICENSE("GPL");