mm/kasan: move kasan.fault to mm/kasan/report.c
[linux-block.git] / lib / test_kasan.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
3f15801c
AR
2/*
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
3f15801c
AR
6 */
7
19a33ca6 8#include <linux/bitops.h>
0386bf38 9#include <linux/delay.h>
19a33ca6 10#include <linux/kasan.h>
3f15801c 11#include <linux/kernel.h>
eae08dca 12#include <linux/mm.h>
19a33ca6
ME
13#include <linux/mman.h>
14#include <linux/module.h>
3f15801c 15#include <linux/printk.h>
573a4809 16#include <linux/random.h>
3f15801c
AR
17#include <linux/slab.h>
18#include <linux/string.h>
eae08dca 19#include <linux/uaccess.h>
b92a953c 20#include <linux/io.h>
06513916 21#include <linux/vmalloc.h>
b92a953c
MR
22
23#include <asm/page.h>
3f15801c 24
83c4e7a0
PA
25#include <kunit/test.h>
26
f33a0149
WW
27#include "../mm/kasan/kasan.h"
28
1f600626 29#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
f33a0149 30
adb72ae1 31/*
0fd37925
AK
32 * 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.
adb72ae1 34 */
adb72ae1 35void *kasan_ptr_result;
83c4e7a0
PA
36int kasan_int_result;
37
38static struct kunit_resource resource;
39static struct kunit_kasan_expectation fail_data;
40static bool multishot;
41
0fd37925
AK
42/*
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
f05842cf
AK
44 * 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().
0fd37925 47 */
83c4e7a0
PA
48static int kasan_test_init(struct kunit *test)
49{
d82dc3a4
AK
50 if (!kasan_enabled()) {
51 kunit_err(test, "can't run KASAN tests with KASAN disabled");
52 return -1;
53 }
54
83c4e7a0 55 multishot = kasan_save_enable_multi_shot();
f05842cf 56 kasan_set_tagging_report_once(false);
99734b53 57 fail_data.report_found = false;
99734b53
AK
58 kunit_add_named_resource(test, NULL, NULL, &resource,
59 "kasan_data", &fail_data);
83c4e7a0
PA
60 return 0;
61}
62
63static void kasan_test_exit(struct kunit *test)
64{
f05842cf 65 kasan_set_tagging_report_once(true);
83c4e7a0 66 kasan_restore_multi_shot(multishot);
99734b53 67 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
83c4e7a0
PA
68}
69
70/**
0fd37925
AK
71 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
72 * KASAN report; causes a test failure otherwise. This relies on a KUnit
73 * resource named "kasan_data". Do not use this name for KUnit resources
74 * outside of KASAN tests.
f05842cf 75 *
e80a76aa
AK
76 * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
77 * checking is auto-disabled. When this happens, this test handler reenables
78 * tag checking. As tag checking can be only disabled or enabled per CPU,
79 * this handler disables migration (preemption).
2e4bde6a
AK
80 *
81 * Since the compiler doesn't see that the expression can change the fail_data
82 * fields, it can reorder or optimize away the accesses to those fields.
83 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
84 * expression to prevent that.
99734b53
AK
85 *
86 * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
87 * false. This allows detecting KASAN reports that happen outside of the checks
88 * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
89 * and in kasan_test_exit.
83c4e7a0 90 */
99734b53
AK
91#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
92 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
93 !kasan_async_mode_enabled()) \
94 migrate_disable(); \
95 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
99734b53
AK
96 barrier(); \
97 expression; \
98 barrier(); \
3ff16d30
DG
99 if (!READ_ONCE(fail_data.report_found)) { \
100 KUNIT_FAIL(test, KUNIT_SUBTEST_INDENT "KASAN failure " \
101 "expected in \"" #expression \
102 "\", but none occurred"); \
103 } \
99734b53
AK
104 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
105 if (READ_ONCE(fail_data.report_found)) \
106 kasan_enable_tagging_sync(); \
107 migrate_enable(); \
108 } \
109 WRITE_ONCE(fail_data.report_found, false); \
83c4e7a0
PA
110} while (0)
111
da17e377 112#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
40eb5cf4
ME
113 if (!IS_ENABLED(config)) \
114 kunit_skip((test), "Test requires " #config "=y"); \
da17e377
AK
115} while (0)
116
117#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
40eb5cf4
ME
118 if (IS_ENABLED(config)) \
119 kunit_skip((test), "Test requires " #config "=n"); \
da17e377
AK
120} while (0)
121
73228c7e 122static void kmalloc_oob_right(struct kunit *test)
3f15801c
AR
123{
124 char *ptr;
125 size_t size = 123;
126
3f15801c 127 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 128 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 129
73228c7e 130 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
3f15801c
AR
131 kfree(ptr);
132}
133
73228c7e 134static void kmalloc_oob_left(struct kunit *test)
3f15801c
AR
135{
136 char *ptr;
137 size_t size = 15;
138
3f15801c 139 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 140 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801c 141
73228c7e 142 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
3f15801c
AR
143 kfree(ptr);
144}
145
73228c7e 146static void kmalloc_node_oob_right(struct kunit *test)
3f15801c
AR
147{
148 char *ptr;
149 size_t size = 4096;
150
3f15801c 151 ptr = kmalloc_node(size, GFP_KERNEL, 0);
73228c7e 152 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801c 153
73228c7e 154 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
3f15801c
AR
155 kfree(ptr);
156}
157
858bdeb0
AK
158/*
159 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
160 * fit into a slab cache and therefore is allocated via the page allocator
161 * fallback. Since this kind of fallback is only implemented for SLUB, these
162 * tests are limited to that allocator.
163 */
73228c7e 164static void kmalloc_pagealloc_oob_right(struct kunit *test)
3f15801c
AR
165{
166 char *ptr;
167 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
168
da17e377 169 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
73228c7e 170
e6e8379c 171 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 172 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 173
73228c7e 174 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
858bdeb0 175
e6e8379c
AP
176 kfree(ptr);
177}
47adccce 178
73228c7e 179static void kmalloc_pagealloc_uaf(struct kunit *test)
47adccce
DV
180{
181 char *ptr;
182 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
183
da17e377 184 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
47adccce 185
73228c7e
PA
186 ptr = kmalloc(size, GFP_KERNEL);
187 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
47adccce 188 kfree(ptr);
858bdeb0 189
73228c7e 190 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
47adccce
DV
191}
192
73228c7e 193static void kmalloc_pagealloc_invalid_free(struct kunit *test)
47adccce
DV
194{
195 char *ptr;
196 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
197
da17e377 198 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
47adccce 199
73228c7e
PA
200 ptr = kmalloc(size, GFP_KERNEL);
201 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
202
203 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
47adccce 204}
e6e8379c 205
858bdeb0
AK
206static void pagealloc_oob_right(struct kunit *test)
207{
208 char *ptr;
209 struct page *pages;
210 size_t order = 4;
211 size_t size = (1UL << (PAGE_SHIFT + order));
212
213 /*
214 * With generic KASAN page allocations have no redzones, thus
215 * out-of-bounds detection is not guaranteed.
216 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
217 */
218 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
219
220 pages = alloc_pages(GFP_KERNEL, order);
221 ptr = page_address(pages);
222 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
223
224 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
225 free_pages((unsigned long)ptr, order);
226}
227
228static void pagealloc_uaf(struct kunit *test)
229{
230 char *ptr;
231 struct page *pages;
232 size_t order = 4;
233
234 pages = alloc_pages(GFP_KERNEL, order);
235 ptr = page_address(pages);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
237 free_pages((unsigned long)ptr, order);
238
239 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
240}
241
73228c7e 242static void kmalloc_large_oob_right(struct kunit *test)
e6e8379c
AP
243{
244 char *ptr;
245 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
0fd37925
AK
246
247 /*
248 * Allocate a chunk that is large enough, but still fits into a slab
e6e8379c
AP
249 * and does not trigger the page allocator fallback in SLUB.
250 */
3f15801c 251 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 252 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801c 253
73228c7e 254 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
3f15801c
AR
255 kfree(ptr);
256}
257
b87c28b9
AK
258static void krealloc_more_oob_helper(struct kunit *test,
259 size_t size1, size_t size2)
3f15801c
AR
260{
261 char *ptr1, *ptr2;
b87c28b9
AK
262 size_t middle;
263
264 KUNIT_ASSERT_LT(test, size1, size2);
265 middle = size1 + (size2 - size1) / 2;
3f15801c 266
3f15801c 267 ptr1 = kmalloc(size1, GFP_KERNEL);
73228c7e 268 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
3f15801c 269
73228c7e
PA
270 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
271 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
f33a0149 272
b87c28b9
AK
273 /* All offsets up to size2 must be accessible. */
274 ptr2[size1 - 1] = 'x';
275 ptr2[size1] = 'x';
276 ptr2[middle] = 'x';
277 ptr2[size2 - 1] = 'x';
278
279 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
280 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
281 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
282
283 /* For all modes first aligned offset after size2 must be inaccessible. */
284 KUNIT_EXPECT_KASAN_FAIL(test,
285 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
286
3f15801c
AR
287 kfree(ptr2);
288}
289
b87c28b9
AK
290static void krealloc_less_oob_helper(struct kunit *test,
291 size_t size1, size_t size2)
3f15801c
AR
292{
293 char *ptr1, *ptr2;
b87c28b9
AK
294 size_t middle;
295
296 KUNIT_ASSERT_LT(test, size2, size1);
297 middle = size2 + (size1 - size2) / 2;
3f15801c 298
3f15801c 299 ptr1 = kmalloc(size1, GFP_KERNEL);
73228c7e 300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
f33a0149 301
73228c7e
PA
302 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
303 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
f33a0149 304
b87c28b9
AK
305 /* Must be accessible for all modes. */
306 ptr2[size2 - 1] = 'x';
307
308 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
309 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
310 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
311
312 /* For all modes first aligned offset after size2 must be inaccessible. */
313 KUNIT_EXPECT_KASAN_FAIL(test,
314 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
315
316 /*
317 * For all modes all size2, middle, and size1 should land in separate
318 * granules and thus the latter two offsets should be inaccessible.
319 */
320 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
321 round_down(middle, KASAN_GRANULE_SIZE));
322 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
323 round_down(size1, KASAN_GRANULE_SIZE));
324 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
325 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
326 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
327
3f15801c
AR
328 kfree(ptr2);
329}
330
b87c28b9
AK
331static void krealloc_more_oob(struct kunit *test)
332{
333 krealloc_more_oob_helper(test, 201, 235);
334}
335
336static void krealloc_less_oob(struct kunit *test)
337{
338 krealloc_less_oob_helper(test, 235, 201);
339}
340
341static void krealloc_pagealloc_more_oob(struct kunit *test)
342{
343 /* page_alloc fallback in only implemented for SLUB. */
344 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
345
346 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
347 KMALLOC_MAX_CACHE_SIZE + 235);
348}
349
350static void krealloc_pagealloc_less_oob(struct kunit *test)
351{
352 /* page_alloc fallback in only implemented for SLUB. */
353 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
354
355 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
356 KMALLOC_MAX_CACHE_SIZE + 201);
357}
358
26a5ca7a
AK
359/*
360 * Check that krealloc() detects a use-after-free, returns NULL,
361 * and doesn't unpoison the freed object.
362 */
363static void krealloc_uaf(struct kunit *test)
364{
365 char *ptr1, *ptr2;
366 int size1 = 201;
367 int size2 = 235;
368
369 ptr1 = kmalloc(size1, GFP_KERNEL);
370 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
371 kfree(ptr1);
372
373 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
374 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
375 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
376}
377
73228c7e 378static void kmalloc_oob_16(struct kunit *test)
3f15801c
AR
379{
380 struct {
381 u64 words[2];
382 } *ptr1, *ptr2;
383
58b999d7 384 /* This test is specifically crafted for the generic mode. */
da17e377 385 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
58b999d7 386
3f15801c 387 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
73228c7e
PA
388 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
389
3f15801c 390 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
73228c7e
PA
391 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
392
393 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
3f15801c
AR
394 kfree(ptr1);
395 kfree(ptr2);
396}
397
58b999d7
AK
398static void kmalloc_uaf_16(struct kunit *test)
399{
400 struct {
401 u64 words[2];
402 } *ptr1, *ptr2;
403
404 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
406
407 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
408 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
409 kfree(ptr2);
410
411 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
412 kfree(ptr1);
413}
414
73228c7e 415static void kmalloc_oob_memset_2(struct kunit *test)
f523e737
WL
416{
417 char *ptr;
418 size_t size = 8;
419
f523e737 420 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 421 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 422
73228c7e 423 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
f523e737
WL
424 kfree(ptr);
425}
426
73228c7e 427static void kmalloc_oob_memset_4(struct kunit *test)
f523e737
WL
428{
429 char *ptr;
430 size_t size = 8;
431
f523e737 432 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 434
73228c7e 435 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
f523e737
WL
436 kfree(ptr);
437}
438
439
73228c7e 440static void kmalloc_oob_memset_8(struct kunit *test)
f523e737
WL
441{
442 char *ptr;
443 size_t size = 8;
444
f523e737 445 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 446 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 447
73228c7e 448 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
f523e737
WL
449 kfree(ptr);
450}
451
73228c7e 452static void kmalloc_oob_memset_16(struct kunit *test)
f523e737
WL
453{
454 char *ptr;
455 size_t size = 16;
456
f523e737 457 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 458 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 459
73228c7e 460 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
f523e737
WL
461 kfree(ptr);
462}
463
73228c7e 464static void kmalloc_oob_in_memset(struct kunit *test)
3f15801c
AR
465{
466 char *ptr;
467 size_t size = 666;
468
3f15801c 469 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 470 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a0149 471
73228c7e 472 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
3f15801c
AR
473 kfree(ptr);
474}
475
73228c7e 476static void kmalloc_memmove_invalid_size(struct kunit *test)
98f3b56f
WW
477{
478 char *ptr;
479 size_t size = 64;
480 volatile size_t invalid_size = -2;
481
98f3b56f 482 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 483 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
98f3b56f
WW
484
485 memset((char *)ptr, 0, 64);
73228c7e
PA
486
487 KUNIT_EXPECT_KASAN_FAIL(test,
488 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
98f3b56f
WW
489 kfree(ptr);
490}
491
73228c7e 492static void kmalloc_uaf(struct kunit *test)
3f15801c
AR
493{
494 char *ptr;
495 size_t size = 10;
496
3f15801c 497 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 498 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801c
AR
499
500 kfree(ptr);
73228c7e 501 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
3f15801c
AR
502}
503
73228c7e 504static void kmalloc_uaf_memset(struct kunit *test)
3f15801c
AR
505{
506 char *ptr;
507 size_t size = 33;
508
3f15801c 509 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 510 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801c
AR
511
512 kfree(ptr);
73228c7e 513 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
3f15801c
AR
514}
515
73228c7e 516static void kmalloc_uaf2(struct kunit *test)
3f15801c
AR
517{
518 char *ptr1, *ptr2;
519 size_t size = 43;
1b1df4c4 520 int counter = 0;
3f15801c 521
1b1df4c4 522again:
3f15801c 523 ptr1 = kmalloc(size, GFP_KERNEL);
73228c7e 524 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
3f15801c
AR
525
526 kfree(ptr1);
73228c7e 527
3f15801c 528 ptr2 = kmalloc(size, GFP_KERNEL);
73228c7e
PA
529 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
530
1b1df4c4
AK
531 /*
532 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
533 * Allow up to 16 attempts at generating different tags.
534 */
535 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
536 kfree(ptr2);
537 goto again;
538 }
539
73228c7e
PA
540 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
541 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
3f15801c 542
3f15801c
AR
543 kfree(ptr2);
544}
545
73228c7e 546static void kfree_via_page(struct kunit *test)
b92a953c
MR
547{
548 char *ptr;
549 size_t size = 8;
550 struct page *page;
551 unsigned long offset;
552
b92a953c 553 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 554 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
b92a953c
MR
555
556 page = virt_to_page(ptr);
557 offset = offset_in_page(ptr);
558 kfree(page_address(page) + offset);
559}
560
73228c7e 561static void kfree_via_phys(struct kunit *test)
b92a953c
MR
562{
563 char *ptr;
564 size_t size = 8;
565 phys_addr_t phys;
566
b92a953c 567 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 568 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
b92a953c
MR
569
570 phys = virt_to_phys(ptr);
571 kfree(phys_to_virt(phys));
572}
573
73228c7e 574static void kmem_cache_oob(struct kunit *test)
3f15801c
AR
575{
576 char *p;
577 size_t size = 200;
11516135
AK
578 struct kmem_cache *cache;
579
580 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
73228c7e 581 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
11516135 582
3f15801c
AR
583 p = kmem_cache_alloc(cache, GFP_KERNEL);
584 if (!p) {
73228c7e 585 kunit_err(test, "Allocation failed: %s\n", __func__);
3f15801c
AR
586 kmem_cache_destroy(cache);
587 return;
588 }
589
73228c7e 590 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
11516135 591
3f15801c
AR
592 kmem_cache_free(cache, p);
593 kmem_cache_destroy(cache);
594}
595
11516135 596static void kmem_cache_accounted(struct kunit *test)
0386bf38
GT
597{
598 int i;
599 char *p;
600 size_t size = 200;
601 struct kmem_cache *cache;
602
603 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
73228c7e 604 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
0386bf38 605
0386bf38
GT
606 /*
607 * Several allocations with a delay to allow for lazy per memcg kmem
608 * cache creation.
609 */
610 for (i = 0; i < 5; i++) {
611 p = kmem_cache_alloc(cache, GFP_KERNEL);
dc2bf000 612 if (!p)
0386bf38 613 goto free_cache;
dc2bf000 614
0386bf38
GT
615 kmem_cache_free(cache, p);
616 msleep(100);
617 }
618
619free_cache:
620 kmem_cache_destroy(cache);
621}
622
11516135
AK
623static void kmem_cache_bulk(struct kunit *test)
624{
625 struct kmem_cache *cache;
626 size_t size = 200;
627 char *p[10];
628 bool ret;
629 int i;
630
631 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
632 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
633
634 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
635 if (!ret) {
636 kunit_err(test, "Allocation failed: %s\n", __func__);
637 kmem_cache_destroy(cache);
638 return;
639 }
640
641 for (i = 0; i < ARRAY_SIZE(p); i++)
642 p[i][0] = p[i][size - 1] = 42;
643
644 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
645 kmem_cache_destroy(cache);
646}
647
3f15801c
AR
648static char global_array[10];
649
73228c7e 650static void kasan_global_oob(struct kunit *test)
3f15801c 651{
f649dc0e
PC
652 /*
653 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
53b0fe36 654 * from failing here and panicking the kernel, access the array via a
f649dc0e
PC
655 * volatile pointer, which will prevent the compiler from being able to
656 * determine the array bounds.
657 *
658 * This access uses a volatile pointer to char (char *volatile) rather
659 * than the more conventional pointer to volatile char (volatile char *)
660 * because we want to prevent the compiler from making inferences about
661 * the pointer itself (i.e. its array bounds), not the data that it
662 * refers to.
663 */
664 char *volatile array = global_array;
665 char *p = &array[ARRAY_SIZE(global_array) + 3];
3f15801c 666
58b999d7 667 /* Only generic mode instruments globals. */
da17e377 668 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
58b999d7 669
73228c7e 670 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
3f15801c
AR
671}
672
611806b4 673/* Check that ksize() makes the whole object accessible. */
73228c7e 674static void ksize_unpoisons_memory(struct kunit *test)
96fe805f
AP
675{
676 char *ptr;
48c23239 677 size_t size = 123, real_size;
96fe805f 678
96fe805f 679 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 680 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
96fe805f 681 real_size = ksize(ptr);
0fd37925
AK
682
683 /* This access shouldn't trigger a KASAN report. */
96fe805f 684 ptr[size] = 'x';
0fd37925
AK
685
686 /* This one must. */
73228c7e 687 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
0fd37925 688
96fe805f
AP
689 kfree(ptr);
690}
691
611806b4
AK
692/*
693 * Check that a use-after-free is detected by ksize() and via normal accesses
694 * after it.
695 */
696static void ksize_uaf(struct kunit *test)
697{
698 char *ptr;
699 int size = 128 - KASAN_GRANULE_SIZE;
700
701 ptr = kmalloc(size, GFP_KERNEL);
702 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
703 kfree(ptr);
704
705 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
706 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
707 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
708}
709
73228c7e 710static void kasan_stack_oob(struct kunit *test)
eae08dca 711{
73228c7e 712 char stack_array[10];
f649dc0e
PC
713 /* See comment in kasan_global_oob. */
714 char *volatile array = stack_array;
715 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
eae08dca 716
da17e377 717 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
eae08dca 718
73228c7e 719 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
eae08dca
AR
720}
721
73228c7e 722static void kasan_alloca_oob_left(struct kunit *test)
00a14294
PL
723{
724 volatile int i = 10;
725 char alloca_array[i];
f649dc0e
PC
726 /* See comment in kasan_global_oob. */
727 char *volatile array = alloca_array;
728 char *p = array - 1;
00a14294 729
58b999d7 730 /* Only generic mode instruments dynamic allocas. */
da17e377
AK
731 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
732 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
73228c7e
PA
733
734 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
00a14294
PL
735}
736
73228c7e 737static void kasan_alloca_oob_right(struct kunit *test)
00a14294
PL
738{
739 volatile int i = 10;
740 char alloca_array[i];
f649dc0e
PC
741 /* See comment in kasan_global_oob. */
742 char *volatile array = alloca_array;
743 char *p = array + i;
00a14294 744
58b999d7 745 /* Only generic mode instruments dynamic allocas. */
da17e377
AK
746 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
747 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
73228c7e
PA
748
749 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
00a14294
PL
750}
751
73228c7e 752static void kmem_cache_double_free(struct kunit *test)
b1d57289
DV
753{
754 char *p;
755 size_t size = 200;
756 struct kmem_cache *cache;
757
758 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
73228c7e
PA
759 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
760
b1d57289
DV
761 p = kmem_cache_alloc(cache, GFP_KERNEL);
762 if (!p) {
73228c7e 763 kunit_err(test, "Allocation failed: %s\n", __func__);
b1d57289
DV
764 kmem_cache_destroy(cache);
765 return;
766 }
767
768 kmem_cache_free(cache, p);
73228c7e 769 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
b1d57289
DV
770 kmem_cache_destroy(cache);
771}
772
73228c7e 773static void kmem_cache_invalid_free(struct kunit *test)
b1d57289
DV
774{
775 char *p;
776 size_t size = 200;
777 struct kmem_cache *cache;
778
779 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
780 NULL);
73228c7e
PA
781 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
782
b1d57289
DV
783 p = kmem_cache_alloc(cache, GFP_KERNEL);
784 if (!p) {
73228c7e 785 kunit_err(test, "Allocation failed: %s\n", __func__);
b1d57289
DV
786 kmem_cache_destroy(cache);
787 return;
788 }
789
0fd37925 790 /* Trigger invalid free, the object doesn't get freed. */
73228c7e 791 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
91c93ed0
AK
792
793 /*
794 * Properly free the object to prevent the "Objects remaining in
795 * test_cache on __kmem_cache_shutdown" BUG failure.
796 */
797 kmem_cache_free(cache, p);
798
b1d57289
DV
799 kmem_cache_destroy(cache);
800}
801
73228c7e 802static void kasan_memchr(struct kunit *test)
0c96350a
AR
803{
804 char *ptr;
805 size_t size = 24;
806
0fd37925
AK
807 /*
808 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
809 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
810 */
da17e377 811 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
73228c7e 812
58b999d7
AK
813 if (OOB_TAG_OFF)
814 size = round_up(size, OOB_TAG_OFF);
815
73228c7e
PA
816 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
817 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
818
819 KUNIT_EXPECT_KASAN_FAIL(test,
820 kasan_ptr_result = memchr(ptr, '1', size + 1));
0c96350a 821
0c96350a
AR
822 kfree(ptr);
823}
824
73228c7e 825static void kasan_memcmp(struct kunit *test)
0c96350a
AR
826{
827 char *ptr;
828 size_t size = 24;
829 int arr[9];
830
0fd37925
AK
831 /*
832 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
833 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
834 */
da17e377 835 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
0c96350a 836
58b999d7
AK
837 if (OOB_TAG_OFF)
838 size = round_up(size, OOB_TAG_OFF);
839
73228c7e
PA
840 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
841 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
0c96350a 842 memset(arr, 0, sizeof(arr));
73228c7e
PA
843
844 KUNIT_EXPECT_KASAN_FAIL(test,
845 kasan_int_result = memcmp(ptr, arr, size+1));
0c96350a
AR
846 kfree(ptr);
847}
848
73228c7e 849static void kasan_strings(struct kunit *test)
0c96350a
AR
850{
851 char *ptr;
852 size_t size = 24;
853
0fd37925
AK
854 /*
855 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
856 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
857 */
da17e377 858 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
73228c7e
PA
859
860 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
861 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
0c96350a
AR
862
863 kfree(ptr);
864
865 /*
866 * Try to cause only 1 invalid access (less spam in dmesg).
867 * For that we need ptr to point to zeroed byte.
868 * Skip metadata that could be stored in freed object so ptr
869 * will likely point to zeroed byte.
870 */
871 ptr += 16;
73228c7e 872 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
0c96350a 873
73228c7e 874 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
0c96350a 875
73228c7e 876 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
0c96350a 877
73228c7e 878 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
0c96350a 879
73228c7e 880 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
0c96350a 881
73228c7e 882 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
0c96350a
AR
883}
884
58b999d7
AK
885static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
886{
887 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
888 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
889 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
890 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
891 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
892 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
893 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
894 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
895}
896
897static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
898{
899 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
900 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
901 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
902 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
903 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
904 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
905 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
906 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
907
908#if defined(clear_bit_unlock_is_negative_byte)
909 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
910 clear_bit_unlock_is_negative_byte(nr, addr));
911#endif
912}
913
914static void kasan_bitops_generic(struct kunit *test)
19a33ca6 915{
58b999d7
AK
916 long *bits;
917
918 /* This test is specifically crafted for the generic mode. */
da17e377 919 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
58b999d7 920
19a33ca6 921 /*
0fd37925 922 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
19a33ca6
ME
923 * this way we do not actually corrupt other memory.
924 */
58b999d7 925 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
73228c7e 926 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
19a33ca6
ME
927
928 /*
929 * Below calls try to access bit within allocated memory; however, the
930 * below accesses are still out-of-bounds, since bitops are defined to
931 * operate on the whole long the bit is in.
932 */
58b999d7 933 kasan_bitops_modify(test, BITS_PER_LONG, bits);
19a33ca6
ME
934
935 /*
936 * Below calls try to access bit beyond allocated memory.
937 */
58b999d7 938 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
19a33ca6 939
58b999d7
AK
940 kfree(bits);
941}
19a33ca6 942
58b999d7
AK
943static void kasan_bitops_tags(struct kunit *test)
944{
945 long *bits;
19a33ca6 946
da17e377
AK
947 /* This test is specifically crafted for tag-based modes. */
948 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
19a33ca6 949
e66e1799
AK
950 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
951 bits = kzalloc(48, GFP_KERNEL);
58b999d7 952 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
19a33ca6 953
e66e1799
AK
954 /* Do the accesses past the 48 allocated bytes, but within the redone. */
955 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
956 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
19a33ca6 957
19a33ca6
ME
958 kfree(bits);
959}
960
73228c7e 961static void kmalloc_double_kzfree(struct kunit *test)
bb104ed7
ME
962{
963 char *ptr;
964 size_t size = 16;
965
bb104ed7 966 ptr = kmalloc(size, GFP_KERNEL);
73228c7e 967 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
bb104ed7 968
453431a5 969 kfree_sensitive(ptr);
73228c7e 970 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
bb104ed7
ME
971}
972
73228c7e 973static void vmalloc_oob(struct kunit *test)
06513916
DA
974{
975 void *area;
976
da17e377 977 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
06513916
DA
978
979 /*
980 * We have to be careful not to hit the guard page.
981 * The MMU will catch that and crash us.
982 */
983 area = vmalloc(3000);
73228c7e 984 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
06513916 985
73228c7e 986 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
06513916
DA
987 vfree(area);
988}
387d6e46 989
573a4809
AK
990/*
991 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
992 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
993 * modes.
994 */
995static void match_all_not_assigned(struct kunit *test)
996{
997 char *ptr;
998 struct page *pages;
999 int i, size, order;
1000
1001 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1002
1003 for (i = 0; i < 256; i++) {
1004 size = (get_random_int() % 1024) + 1;
1005 ptr = kmalloc(size, GFP_KERNEL);
1006 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1007 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1008 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1009 kfree(ptr);
1010 }
1011
1012 for (i = 0; i < 256; i++) {
1013 order = (get_random_int() % 4) + 1;
1014 pages = alloc_pages(GFP_KERNEL, order);
1015 ptr = page_address(pages);
1016 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1017 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1018 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1019 free_pages((unsigned long)ptr, order);
1020 }
1021}
1022
1023/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1024static void match_all_ptr_tag(struct kunit *test)
1025{
1026 char *ptr;
1027 u8 tag;
1028
1029 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1030
1031 ptr = kmalloc(128, GFP_KERNEL);
1032 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1033
1034 /* Backup the assigned tag. */
1035 tag = get_tag(ptr);
1036 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1037
1038 /* Reset the tag to 0xff.*/
1039 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1040
1041 /* This access shouldn't trigger a KASAN report. */
1042 *ptr = 0;
1043
1044 /* Recover the pointer tag and free. */
1045 ptr = set_tag(ptr, tag);
1046 kfree(ptr);
1047}
1048
1049/* Check that there are no match-all memory tags for tag-based modes. */
1050static void match_all_mem_tag(struct kunit *test)
1051{
1052 char *ptr;
1053 int tag;
1054
1055 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1056
1057 ptr = kmalloc(128, GFP_KERNEL);
1058 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1059 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1060
1061 /* For each possible tag value not matching the pointer tag. */
1062 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1063 if (tag == get_tag(ptr))
1064 continue;
1065
1066 /* Mark the first memory granule with the chosen memory tag. */
aa5c219c 1067 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
573a4809
AK
1068
1069 /* This access must cause a KASAN report. */
1070 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1071 }
1072
1073 /* Recover the memory tag and free. */
aa5c219c 1074 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
573a4809
AK
1075 kfree(ptr);
1076}
1077
73228c7e
PA
1078static struct kunit_case kasan_kunit_test_cases[] = {
1079 KUNIT_CASE(kmalloc_oob_right),
1080 KUNIT_CASE(kmalloc_oob_left),
1081 KUNIT_CASE(kmalloc_node_oob_right),
1082 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1083 KUNIT_CASE(kmalloc_pagealloc_uaf),
1084 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
858bdeb0
AK
1085 KUNIT_CASE(pagealloc_oob_right),
1086 KUNIT_CASE(pagealloc_uaf),
73228c7e 1087 KUNIT_CASE(kmalloc_large_oob_right),
b87c28b9
AK
1088 KUNIT_CASE(krealloc_more_oob),
1089 KUNIT_CASE(krealloc_less_oob),
1090 KUNIT_CASE(krealloc_pagealloc_more_oob),
1091 KUNIT_CASE(krealloc_pagealloc_less_oob),
26a5ca7a 1092 KUNIT_CASE(krealloc_uaf),
73228c7e 1093 KUNIT_CASE(kmalloc_oob_16),
58b999d7 1094 KUNIT_CASE(kmalloc_uaf_16),
73228c7e
PA
1095 KUNIT_CASE(kmalloc_oob_in_memset),
1096 KUNIT_CASE(kmalloc_oob_memset_2),
1097 KUNIT_CASE(kmalloc_oob_memset_4),
1098 KUNIT_CASE(kmalloc_oob_memset_8),
1099 KUNIT_CASE(kmalloc_oob_memset_16),
1100 KUNIT_CASE(kmalloc_memmove_invalid_size),
1101 KUNIT_CASE(kmalloc_uaf),
1102 KUNIT_CASE(kmalloc_uaf_memset),
1103 KUNIT_CASE(kmalloc_uaf2),
1104 KUNIT_CASE(kfree_via_page),
1105 KUNIT_CASE(kfree_via_phys),
1106 KUNIT_CASE(kmem_cache_oob),
11516135
AK
1107 KUNIT_CASE(kmem_cache_accounted),
1108 KUNIT_CASE(kmem_cache_bulk),
73228c7e
PA
1109 KUNIT_CASE(kasan_global_oob),
1110 KUNIT_CASE(kasan_stack_oob),
1111 KUNIT_CASE(kasan_alloca_oob_left),
1112 KUNIT_CASE(kasan_alloca_oob_right),
1113 KUNIT_CASE(ksize_unpoisons_memory),
611806b4 1114 KUNIT_CASE(ksize_uaf),
73228c7e
PA
1115 KUNIT_CASE(kmem_cache_double_free),
1116 KUNIT_CASE(kmem_cache_invalid_free),
1117 KUNIT_CASE(kasan_memchr),
1118 KUNIT_CASE(kasan_memcmp),
1119 KUNIT_CASE(kasan_strings),
58b999d7
AK
1120 KUNIT_CASE(kasan_bitops_generic),
1121 KUNIT_CASE(kasan_bitops_tags),
73228c7e
PA
1122 KUNIT_CASE(kmalloc_double_kzfree),
1123 KUNIT_CASE(vmalloc_oob),
573a4809
AK
1124 KUNIT_CASE(match_all_not_assigned),
1125 KUNIT_CASE(match_all_ptr_tag),
1126 KUNIT_CASE(match_all_mem_tag),
73228c7e
PA
1127 {}
1128};
1129
1130static struct kunit_suite kasan_kunit_test_suite = {
1131 .name = "kasan",
1132 .init = kasan_test_init,
1133 .test_cases = kasan_kunit_test_cases,
1134 .exit = kasan_test_exit,
1135};
1136
1137kunit_test_suite(kasan_kunit_test_suite);
3f15801c 1138
3f15801c 1139MODULE_LICENSE("GPL");