blob: 26a5c9007653aa5deabae35157cdfcf66788ca43 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrey Ryabinin3f158012015-02-13 14:39:53 -08002/*
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
Andrey Ryabinin3f158012015-02-13 14:39:53 -08006 */
7
Marco Elver19a33ca2019-07-11 20:53:52 -07008#include <linux/bitops.h>
Greg Thelen0386bf32017-02-24 15:00:08 -08009#include <linux/delay.h>
Marco Elver19a33ca2019-07-11 20:53:52 -070010#include <linux/kasan.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080011#include <linux/kernel.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070012#include <linux/mm.h>
Marco Elver19a33ca2019-07-11 20:53:52 -070013#include <linux/mman.h>
14#include <linux/module.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080015#include <linux/printk.h>
Andrey Konovalov573a4802021-02-24 12:05:21 -080016#include <linux/random.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080017#include <linux/slab.h>
18#include <linux/string.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070019#include <linux/uaccess.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070020#include <linux/io.h>
Daniel Axtens06513912019-11-30 17:54:53 -080021#include <linux/vmalloc.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070022
23#include <asm/page.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080024
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070025#include <kunit/test.h>
26
Walter Wuf33a0142020-08-06 23:24:54 -070027#include "../mm/kasan/kasan.h"
28
Andrey Konovalov1f600622020-12-22 12:00:24 -080029#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
Walter Wuf33a0142020-08-06 23:24:54 -070030
Dmitry Vyukov828347f2016-11-30 15:54:16 -080031/*
Andrey Konovalov0fd37922021-02-24 12:05:13 -080032 * Some tests use these global variables to store return values from function
33 * calls that could otherwise be eliminated by the compiler as dead code.
Daniel Axtensadb72ae2020-06-03 15:56:43 -070034 */
Daniel Axtensadb72ae2020-06-03 15:56:43 -070035void *kasan_ptr_result;
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070036int kasan_int_result;
37
38static struct kunit_resource resource;
39static struct kunit_kasan_expectation fail_data;
40static bool multishot;
41
Andrey Konovalov0fd37922021-02-24 12:05:13 -080042/*
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
Andrey Konovalovf05842c2021-02-24 12:05:26 -080044 * first detected bug and panic the kernel if panic_on_warn is enabled. For
45 * hardware tag-based KASAN also allow tag checking to be reenabled for each
46 * test, see the comment for KUNIT_EXPECT_KASAN_FAIL().
Andrey Konovalov0fd37922021-02-24 12:05:13 -080047 */
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070048static int kasan_test_init(struct kunit *test)
49{
Andrey Konovalovd82dc3a2021-02-24 12:06:02 -080050 if (!kasan_enabled()) {
51 kunit_err(test, "can't run KASAN tests with KASAN disabled");
52 return -1;
53 }
54
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070055 multishot = kasan_save_enable_multi_shot();
Andrey Konovalov99734b52021-04-29 23:00:49 -070056 fail_data.report_found = false;
Andrey Konovalov99734b52021-04-29 23:00:49 -070057 kunit_add_named_resource(test, NULL, NULL, &resource,
58 "kasan_data", &fail_data);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070059 return 0;
60}
61
62static void kasan_test_exit(struct kunit *test)
63{
64 kasan_restore_multi_shot(multishot);
Andrey Konovalov99734b52021-04-29 23:00:49 -070065 KUNIT_EXPECT_FALSE(test, fail_data.report_found);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070066}
67
68/**
Andrey Konovalov0fd37922021-02-24 12:05:13 -080069 * 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.
Andrey Konovalovf05842c2021-02-24 12:05:26 -080073 *
Andrey Konovalove80a76a2021-03-15 13:20:19 +000074 * 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).
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080078 *
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.
Andrey Konovalov99734b52021-04-29 23:00:49 -070083 *
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.
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070088 */
Andrey Konovalov99734b52021-04-29 23:00:49 -070089#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
90 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
Vincenzo Frascino2d27e582021-10-06 16:47:51 +010091 kasan_sync_fault_possible()) \
Andrey Konovalov99734b52021-04-29 23:00:49 -070092 migrate_disable(); \
93 KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
Andrey Konovalov99734b52021-04-29 23:00:49 -070094 barrier(); \
95 expression; \
96 barrier(); \
David Gow3ff16d32021-06-28 19:40:36 -070097 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 } \
Andrey Konovalov99734b52021-04-29 23:00:49 -0700102 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); \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700108} while (0)
109
Andrey Konovalovda17e372021-02-24 12:05:17 -0800110#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
Marco Elver40eb5cf2021-06-24 23:58:15 -0700111 if (!IS_ENABLED(config)) \
112 kunit_skip((test), "Test requires " #config "=y"); \
Andrey Konovalovda17e372021-02-24 12:05:17 -0800113} while (0)
114
115#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
Marco Elver40eb5cf2021-06-24 23:58:15 -0700116 if (IS_ENABLED(config)) \
117 kunit_skip((test), "Test requires " #config "=n"); \
Andrey Konovalovda17e372021-02-24 12:05:17 -0800118} while (0)
119
Patricia Alfonso73228c72020-10-13 16:55:06 -0700120static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800121{
122 char *ptr;
Andrey Konovalovab512802021-09-02 14:57:32 -0700123 size_t size = 128 - KASAN_GRANULE_SIZE - 5;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800124
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800125 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700126 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800127
Andrey Konovalovab512802021-09-02 14:57:32 -0700128 /*
129 * An unaligned access past the requested kmalloc size.
130 * Only generic KASAN can precisely detect these.
131 */
132 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
133 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');
134
135 /*
136 * An aligned access into the first out-of-bounds granule that falls
137 * within the aligned kmalloc object.
138 */
139 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');
140
141 /* Out-of-bounds access past the aligned kmalloc object. */
142 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] =
143 ptr[size + KASAN_GRANULE_SIZE + 5]);
144
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800145 kfree(ptr);
146}
147
Patricia Alfonso73228c72020-10-13 16:55:06 -0700148static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800149{
150 char *ptr;
151 size_t size = 15;
152
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800153 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800155
Patricia Alfonso73228c72020-10-13 16:55:06 -0700156 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800157 kfree(ptr);
158}
159
Patricia Alfonso73228c72020-10-13 16:55:06 -0700160static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800161{
162 char *ptr;
163 size_t size = 4096;
164
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800165 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700166 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800167
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700168 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800169 kfree(ptr);
170}
171
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800172/*
173 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
174 * fit into a slab cache and therefore is allocated via the page allocator
175 * fallback. Since this kind of fallback is only implemented for SLUB, these
176 * tests are limited to that allocator.
177 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700178static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800179{
180 char *ptr;
181 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
182
Andrey Konovalovda17e372021-02-24 12:05:17 -0800183 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700184
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700185 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700186 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700187
Patricia Alfonso73228c72020-10-13 16:55:06 -0700188 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800189
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700190 kfree(ptr);
191}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800192
Patricia Alfonso73228c72020-10-13 16:55:06 -0700193static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800194{
195 char *ptr;
196 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
197
Andrey Konovalovda17e372021-02-24 12:05:17 -0800198 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800199
Patricia Alfonso73228c72020-10-13 16:55:06 -0700200 ptr = kmalloc(size, GFP_KERNEL);
201 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800202 kfree(ptr);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800203
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700204 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800205}
206
Patricia Alfonso73228c72020-10-13 16:55:06 -0700207static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800208{
209 char *ptr;
210 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
211
Andrey Konovalovda17e372021-02-24 12:05:17 -0800212 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800213
Patricia Alfonso73228c72020-10-13 16:55:06 -0700214 ptr = kmalloc(size, GFP_KERNEL);
215 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700216
Patricia Alfonso73228c72020-10-13 16:55:06 -0700217 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
218}
219
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800220static void pagealloc_oob_right(struct kunit *test)
221{
222 char *ptr;
223 struct page *pages;
224 size_t order = 4;
225 size_t size = (1UL << (PAGE_SHIFT + order));
226
227 /*
228 * With generic KASAN page allocations have no redzones, thus
229 * out-of-bounds detection is not guaranteed.
230 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
231 */
232 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
233
234 pages = alloc_pages(GFP_KERNEL, order);
235 ptr = page_address(pages);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
237
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700238 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800239 free_pages((unsigned long)ptr, order);
240}
241
242static void pagealloc_uaf(struct kunit *test)
243{
244 char *ptr;
245 struct page *pages;
246 size_t order = 4;
247
248 pages = alloc_pages(GFP_KERNEL, order);
249 ptr = page_address(pages);
250 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
251 free_pages((unsigned long)ptr, order);
252
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700253 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800254}
255
Patricia Alfonso73228c72020-10-13 16:55:06 -0700256static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700257{
258 char *ptr;
259 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800260
261 /*
262 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700263 * and does not trigger the page allocator fallback in SLUB.
264 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800265 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700266 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800267
Patricia Alfonso73228c72020-10-13 16:55:06 -0700268 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800269 kfree(ptr);
270}
271
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800272static void krealloc_more_oob_helper(struct kunit *test,
273 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800274{
275 char *ptr1, *ptr2;
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800276 size_t middle;
277
278 KUNIT_ASSERT_LT(test, size1, size2);
279 middle = size1 + (size2 - size1) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800280
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800281 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700282 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
283
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800284 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700285 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800286
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800287 /* All offsets up to size2 must be accessible. */
288 ptr2[size1 - 1] = 'x';
289 ptr2[size1] = 'x';
290 ptr2[middle] = 'x';
291 ptr2[size2 - 1] = 'x';
292
293 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
294 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
295 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
296
297 /* For all modes first aligned offset after size2 must be inaccessible. */
298 KUNIT_EXPECT_KASAN_FAIL(test,
299 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
300
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800301 kfree(ptr2);
302}
303
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800304static void krealloc_less_oob_helper(struct kunit *test,
305 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800306{
307 char *ptr1, *ptr2;
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800308 size_t middle;
309
310 KUNIT_ASSERT_LT(test, size2, size1);
311 middle = size2 + (size1 - size2) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800312
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800313 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700314 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
315
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800316 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700317 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700318
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800319 /* Must be accessible for all modes. */
320 ptr2[size2 - 1] = 'x';
321
322 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
323 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
324 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
325
326 /* For all modes first aligned offset after size2 must be inaccessible. */
327 KUNIT_EXPECT_KASAN_FAIL(test,
328 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
329
330 /*
331 * For all modes all size2, middle, and size1 should land in separate
332 * granules and thus the latter two offsets should be inaccessible.
333 */
334 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
335 round_down(middle, KASAN_GRANULE_SIZE));
336 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
337 round_down(size1, KASAN_GRANULE_SIZE));
338 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
339 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
340 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
341
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800342 kfree(ptr2);
343}
344
Andrey Konovalovb87c28b92021-02-25 17:20:15 -0800345static void krealloc_more_oob(struct kunit *test)
346{
347 krealloc_more_oob_helper(test, 201, 235);
348}
349
350static void krealloc_less_oob(struct kunit *test)
351{
352 krealloc_less_oob_helper(test, 235, 201);
353}
354
355static void krealloc_pagealloc_more_oob(struct kunit *test)
356{
357 /* page_alloc fallback in only implemented for SLUB. */
358 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
359
360 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
361 KMALLOC_MAX_CACHE_SIZE + 235);
362}
363
364static void krealloc_pagealloc_less_oob(struct kunit *test)
365{
366 /* page_alloc fallback in only implemented for SLUB. */
367 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
368
369 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
370 KMALLOC_MAX_CACHE_SIZE + 201);
371}
372
Andrey Konovalov26a5ca72021-02-25 17:20:19 -0800373/*
374 * Check that krealloc() detects a use-after-free, returns NULL,
375 * and doesn't unpoison the freed object.
376 */
377static void krealloc_uaf(struct kunit *test)
378{
379 char *ptr1, *ptr2;
380 int size1 = 201;
381 int size2 = 235;
382
383 ptr1 = kmalloc(size1, GFP_KERNEL);
384 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
385 kfree(ptr1);
386
387 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
388 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
389 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
390}
391
Patricia Alfonso73228c72020-10-13 16:55:06 -0700392static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800393{
394 struct {
395 u64 words[2];
396 } *ptr1, *ptr2;
397
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800398 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800399 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800400
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800401 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700402 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
403
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800404 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
406
407 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800408 kfree(ptr1);
409 kfree(ptr2);
410}
411
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800412static void kmalloc_uaf_16(struct kunit *test)
413{
414 struct {
415 u64 words[2];
416 } *ptr1, *ptr2;
417
418 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
419 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
420
421 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
422 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
423 kfree(ptr2);
424
425 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
426 kfree(ptr1);
427}
428
Andrey Konovalov555999a2021-09-02 14:57:38 -0700429/*
430 * Note: in the memset tests below, the written range touches both valid and
431 * invalid memory. This makes sure that the instrumentation does not only check
432 * the starting address but the whole range.
433 */
434
Patricia Alfonso73228c72020-10-13 16:55:06 -0700435static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800436{
437 char *ptr;
Andrey Konovalov555999a2021-09-02 14:57:38 -0700438 size_t size = 128 - KASAN_GRANULE_SIZE;
Wang Longf523e732015-11-05 18:51:15 -0800439
Wang Longf523e732015-11-05 18:51:15 -0800440 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700441 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800442
Kees Cookd73dad42021-11-05 13:36:12 -0700443 OPTIMIZER_HIDE_VAR(size);
Andrey Konovalov555999a2021-09-02 14:57:38 -0700444 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800445 kfree(ptr);
446}
447
Patricia Alfonso73228c72020-10-13 16:55:06 -0700448static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800449{
450 char *ptr;
Andrey Konovalov555999a2021-09-02 14:57:38 -0700451 size_t size = 128 - KASAN_GRANULE_SIZE;
Wang Longf523e732015-11-05 18:51:15 -0800452
Wang Longf523e732015-11-05 18:51:15 -0800453 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700454 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800455
Kees Cookd73dad42021-11-05 13:36:12 -0700456 OPTIMIZER_HIDE_VAR(size);
Andrey Konovalov555999a2021-09-02 14:57:38 -0700457 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800458 kfree(ptr);
459}
460
Patricia Alfonso73228c72020-10-13 16:55:06 -0700461static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800462{
463 char *ptr;
Andrey Konovalov555999a2021-09-02 14:57:38 -0700464 size_t size = 128 - KASAN_GRANULE_SIZE;
Wang Longf523e732015-11-05 18:51:15 -0800465
Wang Longf523e732015-11-05 18:51:15 -0800466 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700467 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800468
Kees Cookd73dad42021-11-05 13:36:12 -0700469 OPTIMIZER_HIDE_VAR(size);
Andrey Konovalov555999a2021-09-02 14:57:38 -0700470 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800471 kfree(ptr);
472}
473
Patricia Alfonso73228c72020-10-13 16:55:06 -0700474static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800475{
476 char *ptr;
Andrey Konovalov555999a2021-09-02 14:57:38 -0700477 size_t size = 128 - KASAN_GRANULE_SIZE;
Wang Longf523e732015-11-05 18:51:15 -0800478
Wang Longf523e732015-11-05 18:51:15 -0800479 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700480 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800481
Kees Cookd73dad42021-11-05 13:36:12 -0700482 OPTIMIZER_HIDE_VAR(size);
Andrey Konovalov555999a2021-09-02 14:57:38 -0700483 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800484 kfree(ptr);
485}
486
Patricia Alfonso73228c72020-10-13 16:55:06 -0700487static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800488{
489 char *ptr;
Andrey Konovalov555999a2021-09-02 14:57:38 -0700490 size_t size = 128 - KASAN_GRANULE_SIZE;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800491
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800492 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700493 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800494
Marco Elver09c63042022-01-29 13:41:11 -0800495 OPTIMIZER_HIDE_VAR(ptr);
Kees Cookd73dad42021-11-05 13:36:12 -0700496 OPTIMIZER_HIDE_VAR(size);
Andrey Konovalov555999a2021-09-02 14:57:38 -0700497 KUNIT_EXPECT_KASAN_FAIL(test,
498 memset(ptr, 0, size + KASAN_GRANULE_SIZE));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800499 kfree(ptr);
500}
501
Peter Collingbourne758caba2021-11-05 13:35:56 -0700502static void kmalloc_memmove_negative_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700503{
504 char *ptr;
505 size_t size = 64;
Kees Cookd73dad42021-11-05 13:36:12 -0700506 size_t invalid_size = -2;
Walter Wu98f3b562020-04-01 21:09:40 -0700507
Andrey Konovalov1b0668b2021-09-02 14:57:41 -0700508 /*
509 * Hardware tag-based mode doesn't check memmove for negative size.
510 * As a result, this test introduces a side-effect memory corruption,
511 * which can result in a crash.
512 */
513 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_HW_TAGS);
514
Walter Wu98f3b562020-04-01 21:09:40 -0700515 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700516 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700517
518 memset((char *)ptr, 0, 64);
Marco Elver09c63042022-01-29 13:41:11 -0800519 OPTIMIZER_HIDE_VAR(ptr);
Kees Cookd73dad42021-11-05 13:36:12 -0700520 OPTIMIZER_HIDE_VAR(invalid_size);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700521 KUNIT_EXPECT_KASAN_FAIL(test,
522 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700523 kfree(ptr);
524}
525
Peter Collingbourne758caba2021-11-05 13:35:56 -0700526static void kmalloc_memmove_invalid_size(struct kunit *test)
527{
528 char *ptr;
529 size_t size = 64;
530 volatile size_t invalid_size = size;
531
532 ptr = kmalloc(size, GFP_KERNEL);
533 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
534
535 memset((char *)ptr, 0, 64);
Marco Elver09c63042022-01-29 13:41:11 -0800536 OPTIMIZER_HIDE_VAR(ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800537 KUNIT_EXPECT_KASAN_FAIL(test,
538 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
539 kfree(ptr);
540}
541
Patricia Alfonso73228c72020-10-13 16:55:06 -0700542static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800543{
544 char *ptr;
545 size_t size = 10;
546
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800547 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700548 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800549
550 kfree(ptr);
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700551 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[8]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800552}
553
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800555{
556 char *ptr;
557 size_t size = 33;
558
Andrey Konovalov25b12a52021-09-02 14:57:44 -0700559 /*
560 * Only generic KASAN uses quarantine, which is required to avoid a
561 * kernel memory corruption this test causes.
562 */
563 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
564
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800565 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700566 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800567
568 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700569 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800570}
571
Patricia Alfonso73228c72020-10-13 16:55:06 -0700572static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800573{
574 char *ptr1, *ptr2;
575 size_t size = 43;
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800576 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800577
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800578again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800579 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700580 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800581
582 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800583
Patricia Alfonso73228c72020-10-13 16:55:06 -0700584 ptr2 = kmalloc(size, GFP_KERNEL);
585 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
586
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800587 /*
588 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
589 * Allow up to 16 attempts at generating different tags.
590 */
591 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
592 kfree(ptr2);
593 goto again;
594 }
595
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700596 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[40]);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700597 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
598
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800599 kfree(ptr2);
600}
601
Patricia Alfonso73228c72020-10-13 16:55:06 -0700602static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700603{
604 char *ptr;
605 size_t size = 8;
606 struct page *page;
607 unsigned long offset;
608
Mark Rutlandb92a9532019-09-23 15:34:16 -0700609 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700610 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700611
612 page = virt_to_page(ptr);
613 offset = offset_in_page(ptr);
614 kfree(page_address(page) + offset);
615}
616
Patricia Alfonso73228c72020-10-13 16:55:06 -0700617static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700618{
619 char *ptr;
620 size_t size = 8;
621 phys_addr_t phys;
622
Mark Rutlandb92a9532019-09-23 15:34:16 -0700623 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700624 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700625
626 phys = virt_to_phys(ptr);
627 kfree(phys_to_virt(phys));
628}
629
Patricia Alfonso73228c72020-10-13 16:55:06 -0700630static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800631{
632 char *p;
633 size_t size = 200;
Andrey Konovalov11516132021-02-24 12:05:59 -0800634 struct kmem_cache *cache;
635
636 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700637 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov11516132021-02-24 12:05:59 -0800638
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800639 p = kmem_cache_alloc(cache, GFP_KERNEL);
640 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700641 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800642 kmem_cache_destroy(cache);
643 return;
644 }
645
Patricia Alfonso73228c72020-10-13 16:55:06 -0700646 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov11516132021-02-24 12:05:59 -0800647
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800648 kmem_cache_free(cache, p);
649 kmem_cache_destroy(cache);
650}
651
Andrey Konovalov11516132021-02-24 12:05:59 -0800652static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800653{
654 int i;
655 char *p;
656 size_t size = 200;
657 struct kmem_cache *cache;
658
659 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700660 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800661
Greg Thelen0386bf32017-02-24 15:00:08 -0800662 /*
663 * Several allocations with a delay to allow for lazy per memcg kmem
664 * cache creation.
665 */
666 for (i = 0; i < 5; i++) {
667 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800668 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800669 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800670
Greg Thelen0386bf32017-02-24 15:00:08 -0800671 kmem_cache_free(cache, p);
672 msleep(100);
673 }
674
675free_cache:
676 kmem_cache_destroy(cache);
677}
678
Andrey Konovalov11516132021-02-24 12:05:59 -0800679static void kmem_cache_bulk(struct kunit *test)
680{
681 struct kmem_cache *cache;
682 size_t size = 200;
683 char *p[10];
684 bool ret;
685 int i;
686
687 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
688 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
689
690 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
691 if (!ret) {
692 kunit_err(test, "Allocation failed: %s\n", __func__);
693 kmem_cache_destroy(cache);
694 return;
695 }
696
697 for (i = 0; i < ARRAY_SIZE(p); i++)
698 p[i][0] = p[i][size - 1] = 42;
699
700 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
701 kmem_cache_destroy(cache);
702}
703
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800704static char global_array[10];
705
Marco Elvere5f47282022-01-14 14:04:51 -0800706static void kasan_global_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800707{
Peter Collingbournef649dc02021-05-14 17:27:27 -0700708 /*
709 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
Zhen Lei53b0fe32021-07-07 18:07:28 -0700710 * from failing here and panicking the kernel, access the array via a
Peter Collingbournef649dc02021-05-14 17:27:27 -0700711 * volatile pointer, which will prevent the compiler from being able to
712 * determine the array bounds.
713 *
714 * This access uses a volatile pointer to char (char *volatile) rather
715 * than the more conventional pointer to volatile char (volatile char *)
716 * because we want to prevent the compiler from making inferences about
717 * the pointer itself (i.e. its array bounds), not the data that it
718 * refers to.
719 */
720 char *volatile array = global_array;
721 char *p = &array[ARRAY_SIZE(global_array) + 3];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800722
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800723 /* Only generic mode instruments globals. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800724 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800725
Patricia Alfonso73228c72020-10-13 16:55:06 -0700726 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800727}
728
Marco Elvere5f47282022-01-14 14:04:51 -0800729static void kasan_global_oob_left(struct kunit *test)
730{
731 char *volatile array = global_array;
732 char *p = array - 3;
733
734 /*
735 * GCC is known to fail this test, skip it.
736 * See https://bugzilla.kernel.org/show_bug.cgi?id=215051.
737 */
738 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_CC_IS_CLANG);
739 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
740 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
741}
742
Andrey Konovalov611806b2021-02-24 12:05:50 -0800743/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700744static void ksize_unpoisons_memory(struct kunit *test)
745{
746 char *ptr;
747 size_t size = 123, real_size;
748
749 ptr = kmalloc(size, GFP_KERNEL);
750 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
751 real_size = ksize(ptr);
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800752
753 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700754 ptr[size] = 'x';
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800755
756 /* This one must. */
Andrey Konovalov8fbad192021-09-02 14:57:35 -0700757 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[real_size]);
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800758
Patricia Alfonso73228c72020-10-13 16:55:06 -0700759 kfree(ptr);
760}
761
Andrey Konovalov611806b2021-02-24 12:05:50 -0800762/*
763 * Check that a use-after-free is detected by ksize() and via normal accesses
764 * after it.
765 */
766static void ksize_uaf(struct kunit *test)
767{
768 char *ptr;
769 int size = 128 - KASAN_GRANULE_SIZE;
770
771 ptr = kmalloc(size, GFP_KERNEL);
772 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
773 kfree(ptr);
774
775 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
Andrey Konovalovb38fcca2021-09-02 14:57:47 -0700776 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
777 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);
Andrey Konovalov611806b2021-02-24 12:05:50 -0800778}
779
Patricia Alfonso73228c72020-10-13 16:55:06 -0700780static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800781{
782 char stack_array[10];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700783 /* See comment in kasan_global_oob. */
784 char *volatile array = stack_array;
785 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800786
Andrey Konovalovda17e372021-02-24 12:05:17 -0800787 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700788
Patricia Alfonso73228c72020-10-13 16:55:06 -0700789 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700790}
791
Patricia Alfonso73228c72020-10-13 16:55:06 -0700792static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800793{
794 volatile int i = 10;
795 char alloca_array[i];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700796 /* See comment in kasan_global_oob. */
797 char *volatile array = alloca_array;
798 char *p = array - 1;
Paul Lawrence00a14292018-02-06 15:36:16 -0800799
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800800 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800801 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
802 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700803
804 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800805}
806
Patricia Alfonso73228c72020-10-13 16:55:06 -0700807static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800808{
809 volatile int i = 10;
810 char alloca_array[i];
Peter Collingbournef649dc02021-05-14 17:27:27 -0700811 /* See comment in kasan_global_oob. */
812 char *volatile array = alloca_array;
813 char *p = array + i;
Paul Lawrence00a14292018-02-06 15:36:16 -0800814
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800815 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800816 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
817 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700818
819 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800820}
821
Patricia Alfonso73228c72020-10-13 16:55:06 -0700822static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800823{
824 char *p;
825 size_t size = 200;
826 struct kmem_cache *cache;
827
828 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700829 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
830
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800831 p = kmem_cache_alloc(cache, GFP_KERNEL);
832 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700833 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800834 kmem_cache_destroy(cache);
835 return;
836 }
837
838 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700839 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800840 kmem_cache_destroy(cache);
841}
842
Patricia Alfonso73228c72020-10-13 16:55:06 -0700843static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800844{
845 char *p;
846 size_t size = 200;
847 struct kmem_cache *cache;
848
849 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
850 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700851 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
852
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800853 p = kmem_cache_alloc(cache, GFP_KERNEL);
854 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700855 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800856 kmem_cache_destroy(cache);
857 return;
858 }
859
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800860 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700861 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700862
863 /*
864 * Properly free the object to prevent the "Objects remaining in
865 * test_cache on __kmem_cache_shutdown" BUG failure.
866 */
867 kmem_cache_free(cache, p);
868
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800869 kmem_cache_destroy(cache);
870}
871
Marco Elverf98f9662022-01-14 14:04:57 -0800872static void kmem_cache_double_destroy(struct kunit *test)
873{
874 struct kmem_cache *cache;
875
876 cache = kmem_cache_create("test_cache", 200, 0, 0, NULL);
877 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
878 kmem_cache_destroy(cache);
879 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_destroy(cache));
880}
881
Patricia Alfonso73228c72020-10-13 16:55:06 -0700882static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700883{
884 char *ptr;
885 size_t size = 24;
886
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800887 /*
888 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
889 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
890 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800891 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700892
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800893 if (OOB_TAG_OFF)
894 size = round_up(size, OOB_TAG_OFF);
895
Patricia Alfonso73228c72020-10-13 16:55:06 -0700896 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
897 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
898
Marco Elver09c63042022-01-29 13:41:11 -0800899 OPTIMIZER_HIDE_VAR(ptr);
Kees Cookcab71f72021-11-19 16:43:46 -0800900 OPTIMIZER_HIDE_VAR(size);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700901 KUNIT_EXPECT_KASAN_FAIL(test,
902 kasan_ptr_result = memchr(ptr, '1', size + 1));
903
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700904 kfree(ptr);
905}
906
Patricia Alfonso73228c72020-10-13 16:55:06 -0700907static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700908{
909 char *ptr;
910 size_t size = 24;
911 int arr[9];
912
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800913 /*
914 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
915 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
916 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800917 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700918
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800919 if (OOB_TAG_OFF)
920 size = round_up(size, OOB_TAG_OFF);
921
Patricia Alfonso73228c72020-10-13 16:55:06 -0700922 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
923 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700924 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700925
Marco Elver09c63042022-01-29 13:41:11 -0800926 OPTIMIZER_HIDE_VAR(ptr);
Kees Cookcab71f72021-11-19 16:43:46 -0800927 OPTIMIZER_HIDE_VAR(size);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700928 KUNIT_EXPECT_KASAN_FAIL(test,
929 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700930 kfree(ptr);
931}
932
Patricia Alfonso73228c72020-10-13 16:55:06 -0700933static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700934{
935 char *ptr;
936 size_t size = 24;
937
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800938 /*
939 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
940 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
941 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800942 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700943
944 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
945 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700946
947 kfree(ptr);
948
949 /*
950 * Try to cause only 1 invalid access (less spam in dmesg).
951 * For that we need ptr to point to zeroed byte.
952 * Skip metadata that could be stored in freed object so ptr
953 * will likely point to zeroed byte.
954 */
955 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700956 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700957
Patricia Alfonso73228c72020-10-13 16:55:06 -0700958 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700959
Patricia Alfonso73228c72020-10-13 16:55:06 -0700960 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700961
Patricia Alfonso73228c72020-10-13 16:55:06 -0700962 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700963
Patricia Alfonso73228c72020-10-13 16:55:06 -0700964 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700965
Patricia Alfonso73228c72020-10-13 16:55:06 -0700966 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700967}
968
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800969static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700970{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800971 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
972 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
973 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
974 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
975 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
976 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
977 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
978 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
979}
980
981static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
982{
983 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
984 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
985 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
986 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
987 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
988 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
989 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
990 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
991
992#if defined(clear_bit_unlock_is_negative_byte)
993 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
994 clear_bit_unlock_is_negative_byte(nr, addr));
995#endif
996}
997
998static void kasan_bitops_generic(struct kunit *test)
999{
1000 long *bits;
1001
1002 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -08001003 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001004
Marco Elver19a33ca2019-07-11 20:53:52 -07001005 /*
Andrey Konovalov0fd37922021-02-24 12:05:13 -08001006 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -07001007 * this way we do not actually corrupt other memory.
1008 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001009 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -07001010 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -07001011
1012 /*
1013 * Below calls try to access bit within allocated memory; however, the
1014 * below accesses are still out-of-bounds, since bitops are defined to
1015 * operate on the whole long the bit is in.
1016 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001017 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -07001018
1019 /*
1020 * Below calls try to access bit beyond allocated memory.
1021 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001022 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -07001023
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001024 kfree(bits);
1025}
Marco Elver19a33ca2019-07-11 20:53:52 -07001026
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001027static void kasan_bitops_tags(struct kunit *test)
1028{
1029 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -07001030
Andrey Konovalovda17e372021-02-24 12:05:17 -08001031 /* This test is specifically crafted for tag-based modes. */
1032 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -07001033
Andrey Konovalove66e1792021-02-24 12:05:42 -08001034 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
1035 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001036 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -07001037
Andrey Konovalove66e1792021-02-24 12:05:42 -08001038 /* Do the accesses past the 48 allocated bytes, but within the redone. */
1039 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
1040 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -07001041
Marco Elver19a33ca2019-07-11 20:53:52 -07001042 kfree(bits);
1043}
1044
Patricia Alfonso73228c72020-10-13 16:55:06 -07001045static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -07001046{
1047 char *ptr;
1048 size_t size = 16;
1049
Marco Elverbb104ed2019-07-11 20:54:11 -07001050 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -07001051 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -07001052
Waiman Long453431a2020-08-06 23:18:13 -07001053 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -07001054 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -07001055}
1056
Patricia Alfonso73228c72020-10-13 16:55:06 -07001057static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -08001058{
1059 void *area;
1060
Andrey Konovalovda17e372021-02-24 12:05:17 -08001061 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -08001062
1063 /*
1064 * We have to be careful not to hit the guard page.
1065 * The MMU will catch that and crash us.
1066 */
1067 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -07001068 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -08001069
Patricia Alfonso73228c72020-10-13 16:55:06 -07001070 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -08001071 vfree(area);
1072}
Daniel Axtens06513912019-11-30 17:54:53 -08001073
Andrey Konovalov573a4802021-02-24 12:05:21 -08001074/*
1075 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
1076 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
1077 * modes.
1078 */
1079static void match_all_not_assigned(struct kunit *test)
1080{
1081 char *ptr;
1082 struct page *pages;
1083 int i, size, order;
1084
1085 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1086
1087 for (i = 0; i < 256; i++) {
1088 size = (get_random_int() % 1024) + 1;
1089 ptr = kmalloc(size, GFP_KERNEL);
1090 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1091 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1092 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1093 kfree(ptr);
1094 }
1095
1096 for (i = 0; i < 256; i++) {
1097 order = (get_random_int() % 4) + 1;
1098 pages = alloc_pages(GFP_KERNEL, order);
1099 ptr = page_address(pages);
1100 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1101 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
1102 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1103 free_pages((unsigned long)ptr, order);
1104 }
1105}
1106
1107/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1108static void match_all_ptr_tag(struct kunit *test)
1109{
1110 char *ptr;
1111 u8 tag;
1112
1113 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1114
1115 ptr = kmalloc(128, GFP_KERNEL);
1116 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1117
1118 /* Backup the assigned tag. */
1119 tag = get_tag(ptr);
1120 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1121
1122 /* Reset the tag to 0xff.*/
1123 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1124
1125 /* This access shouldn't trigger a KASAN report. */
1126 *ptr = 0;
1127
1128 /* Recover the pointer tag and free. */
1129 ptr = set_tag(ptr, tag);
1130 kfree(ptr);
1131}
1132
1133/* Check that there are no match-all memory tags for tag-based modes. */
1134static void match_all_mem_tag(struct kunit *test)
1135{
1136 char *ptr;
1137 int tag;
1138
1139 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1140
1141 ptr = kmalloc(128, GFP_KERNEL);
1142 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1143 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1144
1145 /* For each possible tag value not matching the pointer tag. */
1146 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1147 if (tag == get_tag(ptr))
1148 continue;
1149
1150 /* Mark the first memory granule with the chosen memory tag. */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -07001151 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
Andrey Konovalov573a4802021-02-24 12:05:21 -08001152
1153 /* This access must cause a KASAN report. */
1154 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1155 }
1156
1157 /* Recover the memory tag and free. */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -07001158 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
Andrey Konovalov573a4802021-02-24 12:05:21 -08001159 kfree(ptr);
1160}
1161
Patricia Alfonso73228c72020-10-13 16:55:06 -07001162static struct kunit_case kasan_kunit_test_cases[] = {
1163 KUNIT_CASE(kmalloc_oob_right),
1164 KUNIT_CASE(kmalloc_oob_left),
1165 KUNIT_CASE(kmalloc_node_oob_right),
1166 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1167 KUNIT_CASE(kmalloc_pagealloc_uaf),
1168 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalov858bdeb2021-02-24 12:05:55 -08001169 KUNIT_CASE(pagealloc_oob_right),
1170 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001171 KUNIT_CASE(kmalloc_large_oob_right),
Andrey Konovalovb87c28b92021-02-25 17:20:15 -08001172 KUNIT_CASE(krealloc_more_oob),
1173 KUNIT_CASE(krealloc_less_oob),
1174 KUNIT_CASE(krealloc_pagealloc_more_oob),
1175 KUNIT_CASE(krealloc_pagealloc_less_oob),
Andrey Konovalov26a5ca72021-02-25 17:20:19 -08001176 KUNIT_CASE(krealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001177 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001178 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001179 KUNIT_CASE(kmalloc_oob_in_memset),
1180 KUNIT_CASE(kmalloc_oob_memset_2),
1181 KUNIT_CASE(kmalloc_oob_memset_4),
1182 KUNIT_CASE(kmalloc_oob_memset_8),
1183 KUNIT_CASE(kmalloc_oob_memset_16),
Peter Collingbourne758caba2021-11-05 13:35:56 -07001184 KUNIT_CASE(kmalloc_memmove_negative_size),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001185 KUNIT_CASE(kmalloc_memmove_invalid_size),
1186 KUNIT_CASE(kmalloc_uaf),
1187 KUNIT_CASE(kmalloc_uaf_memset),
1188 KUNIT_CASE(kmalloc_uaf2),
1189 KUNIT_CASE(kfree_via_page),
1190 KUNIT_CASE(kfree_via_phys),
1191 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov11516132021-02-24 12:05:59 -08001192 KUNIT_CASE(kmem_cache_accounted),
1193 KUNIT_CASE(kmem_cache_bulk),
Marco Elvere5f47282022-01-14 14:04:51 -08001194 KUNIT_CASE(kasan_global_oob_right),
1195 KUNIT_CASE(kasan_global_oob_left),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001196 KUNIT_CASE(kasan_stack_oob),
1197 KUNIT_CASE(kasan_alloca_oob_left),
1198 KUNIT_CASE(kasan_alloca_oob_right),
1199 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov611806b2021-02-24 12:05:50 -08001200 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001201 KUNIT_CASE(kmem_cache_double_free),
1202 KUNIT_CASE(kmem_cache_invalid_free),
Marco Elverf98f9662022-01-14 14:04:57 -08001203 KUNIT_CASE(kmem_cache_double_destroy),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001204 KUNIT_CASE(kasan_memchr),
1205 KUNIT_CASE(kasan_memcmp),
1206 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001207 KUNIT_CASE(kasan_bitops_generic),
1208 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001209 KUNIT_CASE(kmalloc_double_kzfree),
1210 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov573a4802021-02-24 12:05:21 -08001211 KUNIT_CASE(match_all_not_assigned),
1212 KUNIT_CASE(match_all_ptr_tag),
1213 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001214 {}
1215};
Walter Wu387d6e42020-08-06 23:24:42 -07001216
Patricia Alfonso73228c72020-10-13 16:55:06 -07001217static struct kunit_suite kasan_kunit_test_suite = {
1218 .name = "kasan",
1219 .init = kasan_test_init,
1220 .test_cases = kasan_kunit_test_cases,
1221 .exit = kasan_test_exit,
1222};
Walter Wu387d6e42020-08-06 23:24:42 -07001223
Patricia Alfonso73228c72020-10-13 16:55:06 -07001224kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001225
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001226MODULE_LICENSE("GPL");