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