blob: 1328c468fdb5810f434a52292b49b126221863c1 [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 Konovalov782ba452021-02-03 15:35:00 +110016#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 Konovalov70585d92020-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 Konovalovf3e66b22021-02-03 15:34:59 +110032 * 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 Konovalovf3e66b22021-02-03 15:34:59 +110042/*
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
Andrey Konovalova599a4e2021-02-03 15:35:02 +110044 * 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 Konovalovf3e66b22021-02-03 15:34:59 +110047 */
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070048static int kasan_test_init(struct kunit *test)
49{
Andrey Konovalovbdf2f9b2021-02-03 15:35:07 +110050 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 Konovalova599a4e2021-02-03 15:35:02 +110056 hw_set_tagging_report_once(false);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070057 return 0;
58}
59
60static void kasan_test_exit(struct kunit *test)
61{
Andrey Konovalova599a4e2021-02-03 15:35:02 +110062 hw_set_tagging_report_once(true);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070063 kasan_restore_multi_shot(multishot);
64}
65
66/**
Andrey Konovalovf3e66b22021-02-03 15:34:59 +110067 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
68 * KASAN report; causes a test failure otherwise. This relies on a KUnit
69 * resource named "kasan_data". Do not use this name for KUnit resources
70 * outside of KASAN tests.
Andrey Konovalova599a4e2021-02-03 15:35:02 +110071 *
72 * For hardware tag-based KASAN, when a tag fault happens, tag checking is
73 * normally auto-disabled. When this happens, this test handler reenables
74 * tag checking. As tag checking can be only disabled or enabled per CPU, this
75 * handler disables migration (preemption).
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110076 *
77 * Since the compiler doesn't see that the expression can change the fail_data
78 * fields, it can reorder or optimize away the accesses to those fields.
79 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
80 * expression to prevent that.
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070081 */
Andrey Konovalova599a4e2021-02-03 15:35:02 +110082#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
83 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
84 migrate_disable(); \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110085 WRITE_ONCE(fail_data.report_expected, true); \
86 WRITE_ONCE(fail_data.report_found, false); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110087 kunit_add_named_resource(test, \
88 NULL, \
89 NULL, \
90 &resource, \
91 "kasan_data", &fail_data); \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110092 barrier(); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110093 expression; \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110094 barrier(); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110095 KUNIT_EXPECT_EQ(test, \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110096 READ_ONCE(fail_data.report_expected), \
97 READ_ONCE(fail_data.report_found)); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110098 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110099 if (READ_ONCE(fail_data.report_found)) \
Andrey Konovalova599a4e2021-02-03 15:35:02 +1100100 hw_enable_tagging(); \
101 migrate_enable(); \
102 } \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700103} while (0)
104
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100105#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
106 if (!IS_ENABLED(config)) { \
107 kunit_info((test), "skipping, " #config " required"); \
108 return; \
109 } \
110} while (0)
111
112#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
113 if (IS_ENABLED(config)) { \
114 kunit_info((test), "skipping, " #config " enabled"); \
115 return; \
116 } \
117} while (0)
118
Patricia Alfonso73228c72020-10-13 16:55:06 -0700119static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800120{
121 char *ptr;
122 size_t size = 123;
123
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800124 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700125 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800126
Patricia Alfonso73228c72020-10-13 16:55:06 -0700127 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800128 kfree(ptr);
129}
130
Patricia Alfonso73228c72020-10-13 16:55:06 -0700131static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800132{
133 char *ptr;
134 size_t size = 15;
135
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800136 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700137 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800138
Patricia Alfonso73228c72020-10-13 16:55:06 -0700139 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800140 kfree(ptr);
141}
142
Patricia Alfonso73228c72020-10-13 16:55:06 -0700143static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800144{
145 char *ptr;
146 size_t size = 4096;
147
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800148 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700149 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800150
Patricia Alfonso73228c72020-10-13 16:55:06 -0700151 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800152 kfree(ptr);
153}
154
Andrey Konovalove449e272021-02-03 15:35:06 +1100155/*
156 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
157 * fit into a slab cache and therefore is allocated via the page allocator
158 * fallback. Since this kind of fallback is only implemented for SLUB, these
159 * tests are limited to that allocator.
160 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700161static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800162{
163 char *ptr;
164 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
165
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100166 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700167
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700168 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700169 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700170
Patricia Alfonso73228c72020-10-13 16:55:06 -0700171 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrey Konovalove449e272021-02-03 15:35:06 +1100172
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700173 kfree(ptr);
174}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800175
Patricia Alfonso73228c72020-10-13 16:55:06 -0700176static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800177{
178 char *ptr;
179 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
180
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100181 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800182
Patricia Alfonso73228c72020-10-13 16:55:06 -0700183 ptr = kmalloc(size, GFP_KERNEL);
184 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800185 kfree(ptr);
Andrey Konovalove449e272021-02-03 15:35:06 +1100186
Patricia Alfonso73228c72020-10-13 16:55:06 -0700187 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800188}
189
Patricia Alfonso73228c72020-10-13 16:55:06 -0700190static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800191{
192 char *ptr;
193 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
194
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100195 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800196
Patricia Alfonso73228c72020-10-13 16:55:06 -0700197 ptr = kmalloc(size, GFP_KERNEL);
198 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700199
Patricia Alfonso73228c72020-10-13 16:55:06 -0700200 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
201}
202
Andrey Konovalove449e272021-02-03 15:35:06 +1100203static void pagealloc_oob_right(struct kunit *test)
204{
205 char *ptr;
206 struct page *pages;
207 size_t order = 4;
208 size_t size = (1UL << (PAGE_SHIFT + order));
209
210 /*
211 * With generic KASAN page allocations have no redzones, thus
212 * out-of-bounds detection is not guaranteed.
213 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
214 */
215 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
216
217 pages = alloc_pages(GFP_KERNEL, order);
218 ptr = page_address(pages);
219 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
220
221 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
222 free_pages((unsigned long)ptr, order);
223}
224
225static void pagealloc_uaf(struct kunit *test)
226{
227 char *ptr;
228 struct page *pages;
229 size_t order = 4;
230
231 pages = alloc_pages(GFP_KERNEL, order);
232 ptr = page_address(pages);
233 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
234 free_pages((unsigned long)ptr, order);
235
236 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
237}
238
Patricia Alfonso73228c72020-10-13 16:55:06 -0700239static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700240{
241 char *ptr;
242 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100243
244 /*
245 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700246 * and does not trigger the page allocator fallback in SLUB.
247 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800248 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700249 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800250
Patricia Alfonso73228c72020-10-13 16:55:06 -0700251 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800252 kfree(ptr);
253}
254
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100255static void krealloc_more_oob_helper(struct kunit *test,
256 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800257{
258 char *ptr1, *ptr2;
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100259 size_t middle;
260
261 KUNIT_ASSERT_LT(test, size1, size2);
262 middle = size1 + (size2 - size1) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800263
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800264 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700265 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
266
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800267 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700268 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800269
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100270 /* All offsets up to size2 must be accessible. */
271 ptr2[size1 - 1] = 'x';
272 ptr2[size1] = 'x';
273 ptr2[middle] = 'x';
274 ptr2[size2 - 1] = 'x';
275
276 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
277 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
278 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
279
280 /* For all modes first aligned offset after size2 must be inaccessible. */
281 KUNIT_EXPECT_KASAN_FAIL(test,
282 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
283
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800284 kfree(ptr2);
285}
286
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100287static void krealloc_less_oob_helper(struct kunit *test,
288 size_t size1, size_t size2)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800289{
290 char *ptr1, *ptr2;
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100291 size_t middle;
292
293 KUNIT_ASSERT_LT(test, size2, size1);
294 middle = size2 + (size1 - size2) / 2;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800295
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800296 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700297 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
298
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800299 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700301
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100302 /* Must be accessible for all modes. */
303 ptr2[size2 - 1] = 'x';
304
305 /* Generic mode is precise, so unaligned size2 must be inaccessible. */
306 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
307 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
308
309 /* For all modes first aligned offset after size2 must be inaccessible. */
310 KUNIT_EXPECT_KASAN_FAIL(test,
311 ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
312
313 /*
314 * For all modes all size2, middle, and size1 should land in separate
315 * granules and thus the latter two offsets should be inaccessible.
316 */
317 KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
318 round_down(middle, KASAN_GRANULE_SIZE));
319 KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
320 round_down(size1, KASAN_GRANULE_SIZE));
321 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
322 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
323 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
324
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800325 kfree(ptr2);
326}
327
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +1100328static void krealloc_more_oob(struct kunit *test)
329{
330 krealloc_more_oob_helper(test, 201, 235);
331}
332
333static void krealloc_less_oob(struct kunit *test)
334{
335 krealloc_less_oob_helper(test, 235, 201);
336}
337
338static void krealloc_pagealloc_more_oob(struct kunit *test)
339{
340 /* page_alloc fallback in only implemented for SLUB. */
341 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
342
343 krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
344 KMALLOC_MAX_CACHE_SIZE + 235);
345}
346
347static void krealloc_pagealloc_less_oob(struct kunit *test)
348{
349 /* page_alloc fallback in only implemented for SLUB. */
350 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
351
352 krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
353 KMALLOC_MAX_CACHE_SIZE + 201);
354}
355
Andrey Konovalov4f62c692021-02-12 17:14:50 +1100356/*
357 * Check that krealloc() detects a use-after-free, returns NULL,
358 * and doesn't unpoison the freed object.
359 */
360static void krealloc_uaf(struct kunit *test)
361{
362 char *ptr1, *ptr2;
363 int size1 = 201;
364 int size2 = 235;
365
366 ptr1 = kmalloc(size1, GFP_KERNEL);
367 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
368 kfree(ptr1);
369
370 KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
371 KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
372 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
373}
374
Patricia Alfonso73228c72020-10-13 16:55:06 -0700375static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800376{
377 struct {
378 u64 words[2];
379 } *ptr1, *ptr2;
380
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800381 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100382 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800383
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800384 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700385 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
386
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800387 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700388 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
389
390 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800391 kfree(ptr1);
392 kfree(ptr2);
393}
394
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800395static void kmalloc_uaf_16(struct kunit *test)
396{
397 struct {
398 u64 words[2];
399 } *ptr1, *ptr2;
400
401 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
402 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
403
404 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
405 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
406 kfree(ptr2);
407
408 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
409 kfree(ptr1);
410}
411
Patricia Alfonso73228c72020-10-13 16:55:06 -0700412static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800413{
414 char *ptr;
415 size_t size = 8;
416
Wang Longf523e732015-11-05 18:51:15 -0800417 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700418 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800419
Patricia Alfonso73228c72020-10-13 16:55:06 -0700420 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800421 kfree(ptr);
422}
423
Patricia Alfonso73228c72020-10-13 16:55:06 -0700424static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800425{
426 char *ptr;
427 size_t size = 8;
428
Wang Longf523e732015-11-05 18:51:15 -0800429 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700430 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800431
Patricia Alfonso73228c72020-10-13 16:55:06 -0700432 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800433 kfree(ptr);
434}
435
436
Patricia Alfonso73228c72020-10-13 16:55:06 -0700437static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800438{
439 char *ptr;
440 size_t size = 8;
441
Wang Longf523e732015-11-05 18:51:15 -0800442 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700443 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800444
Patricia Alfonso73228c72020-10-13 16:55:06 -0700445 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800446 kfree(ptr);
447}
448
Patricia Alfonso73228c72020-10-13 16:55:06 -0700449static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800450{
451 char *ptr;
452 size_t size = 16;
453
Wang Longf523e732015-11-05 18:51:15 -0800454 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700455 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800456
Patricia Alfonso73228c72020-10-13 16:55:06 -0700457 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800458 kfree(ptr);
459}
460
Patricia Alfonso73228c72020-10-13 16:55:06 -0700461static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800462{
463 char *ptr;
464 size_t size = 666;
465
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800466 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700467 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800468
Patricia Alfonso73228c72020-10-13 16:55:06 -0700469 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800470 kfree(ptr);
471}
472
Patricia Alfonso73228c72020-10-13 16:55:06 -0700473static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700474{
475 char *ptr;
476 size_t size = 64;
477 volatile size_t invalid_size = -2;
478
Walter Wu98f3b562020-04-01 21:09:40 -0700479 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700480 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700481
482 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700483
484 KUNIT_EXPECT_KASAN_FAIL(test,
485 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700486 kfree(ptr);
487}
488
Patricia Alfonso73228c72020-10-13 16:55:06 -0700489static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800490{
491 char *ptr;
492 size_t size = 10;
493
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800494 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700495 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800496
497 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700498 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800499}
500
Patricia Alfonso73228c72020-10-13 16:55:06 -0700501static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800502{
503 char *ptr;
504 size_t size = 33;
505
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800506 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700507 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800508
509 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700510 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800511}
512
Patricia Alfonso73228c72020-10-13 16:55:06 -0700513static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800514{
515 char *ptr1, *ptr2;
516 size_t size = 43;
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100517 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800518
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100519again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800520 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700521 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800522
523 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800524
Patricia Alfonso73228c72020-10-13 16:55:06 -0700525 ptr2 = kmalloc(size, GFP_KERNEL);
526 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
527
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100528 /*
529 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
530 * Allow up to 16 attempts at generating different tags.
531 */
532 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
533 kfree(ptr2);
534 goto again;
535 }
536
Patricia Alfonso73228c72020-10-13 16:55:06 -0700537 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
538 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
539
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800540 kfree(ptr2);
541}
542
Patricia Alfonso73228c72020-10-13 16:55:06 -0700543static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700544{
545 char *ptr;
546 size_t size = 8;
547 struct page *page;
548 unsigned long offset;
549
Mark Rutlandb92a9532019-09-23 15:34:16 -0700550 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700551 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700552
553 page = virt_to_page(ptr);
554 offset = offset_in_page(ptr);
555 kfree(page_address(page) + offset);
556}
557
Patricia Alfonso73228c72020-10-13 16:55:06 -0700558static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700559{
560 char *ptr;
561 size_t size = 8;
562 phys_addr_t phys;
563
Mark Rutlandb92a9532019-09-23 15:34:16 -0700564 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700565 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700566
567 phys = virt_to_phys(ptr);
568 kfree(phys_to_virt(phys));
569}
570
Patricia Alfonso73228c72020-10-13 16:55:06 -0700571static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800572{
573 char *p;
574 size_t size = 200;
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100575 struct kmem_cache *cache;
576
577 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700578 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100579
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800580 p = kmem_cache_alloc(cache, GFP_KERNEL);
581 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700582 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800583 kmem_cache_destroy(cache);
584 return;
585 }
586
Patricia Alfonso73228c72020-10-13 16:55:06 -0700587 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100588
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800589 kmem_cache_free(cache, p);
590 kmem_cache_destroy(cache);
591}
592
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100593static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800594{
595 int i;
596 char *p;
597 size_t size = 200;
598 struct kmem_cache *cache;
599
600 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700601 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800602
Greg Thelen0386bf32017-02-24 15:00:08 -0800603 /*
604 * Several allocations with a delay to allow for lazy per memcg kmem
605 * cache creation.
606 */
607 for (i = 0; i < 5; i++) {
608 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800609 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800610 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800611
Greg Thelen0386bf32017-02-24 15:00:08 -0800612 kmem_cache_free(cache, p);
613 msleep(100);
614 }
615
616free_cache:
617 kmem_cache_destroy(cache);
618}
619
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100620static void kmem_cache_bulk(struct kunit *test)
621{
622 struct kmem_cache *cache;
623 size_t size = 200;
624 char *p[10];
625 bool ret;
626 int i;
627
628 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
629 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
630
631 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
632 if (!ret) {
633 kunit_err(test, "Allocation failed: %s\n", __func__);
634 kmem_cache_destroy(cache);
635 return;
636 }
637
638 for (i = 0; i < ARRAY_SIZE(p); i++)
639 p[i][0] = p[i][size - 1] = 42;
640
641 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
642 kmem_cache_destroy(cache);
643}
644
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800645static char global_array[10];
646
Patricia Alfonso73228c72020-10-13 16:55:06 -0700647static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800648{
649 volatile int i = 3;
650 char *p = &global_array[ARRAY_SIZE(global_array) + i];
651
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800652 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100653 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800654
Patricia Alfonso73228c72020-10-13 16:55:06 -0700655 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800656}
657
Andrey Konovalov696574e2021-02-03 15:35:05 +1100658/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700659static void ksize_unpoisons_memory(struct kunit *test)
660{
661 char *ptr;
662 size_t size = 123, real_size;
663
664 ptr = kmalloc(size, GFP_KERNEL);
665 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
666 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100667
668 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700669 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100670
671 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700672 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100673
Patricia Alfonso73228c72020-10-13 16:55:06 -0700674 kfree(ptr);
675}
676
Andrey Konovalov696574e2021-02-03 15:35:05 +1100677/*
678 * Check that a use-after-free is detected by ksize() and via normal accesses
679 * after it.
680 */
681static void ksize_uaf(struct kunit *test)
682{
683 char *ptr;
684 int size = 128 - KASAN_GRANULE_SIZE;
685
686 ptr = kmalloc(size, GFP_KERNEL);
687 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
688 kfree(ptr);
689
690 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
691 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
692 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
693}
694
Patricia Alfonso73228c72020-10-13 16:55:06 -0700695static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800696{
697 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700698 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800699 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
700
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100701 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700702
Patricia Alfonso73228c72020-10-13 16:55:06 -0700703 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700704}
705
Patricia Alfonso73228c72020-10-13 16:55:06 -0700706static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800707{
708 volatile int i = 10;
709 char alloca_array[i];
710 char *p = alloca_array - 1;
711
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800712 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100713 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
714 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700715
716 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800717}
718
Patricia Alfonso73228c72020-10-13 16:55:06 -0700719static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800720{
721 volatile int i = 10;
722 char alloca_array[i];
723 char *p = alloca_array + i;
724
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800725 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100726 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
727 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700728
729 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800730}
731
Patricia Alfonso73228c72020-10-13 16:55:06 -0700732static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800733{
734 char *p;
735 size_t size = 200;
736 struct kmem_cache *cache;
737
738 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700739 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
740
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800741 p = kmem_cache_alloc(cache, GFP_KERNEL);
742 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700743 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800744 kmem_cache_destroy(cache);
745 return;
746 }
747
748 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700749 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800750 kmem_cache_destroy(cache);
751}
752
Patricia Alfonso73228c72020-10-13 16:55:06 -0700753static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800754{
755 char *p;
756 size_t size = 200;
757 struct kmem_cache *cache;
758
759 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
760 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700761 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
762
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800763 p = kmem_cache_alloc(cache, GFP_KERNEL);
764 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700765 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800766 kmem_cache_destroy(cache);
767 return;
768 }
769
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100770 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700771 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700772
773 /*
774 * Properly free the object to prevent the "Objects remaining in
775 * test_cache on __kmem_cache_shutdown" BUG failure.
776 */
777 kmem_cache_free(cache, p);
778
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800779 kmem_cache_destroy(cache);
780}
781
Patricia Alfonso73228c72020-10-13 16:55:06 -0700782static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700783{
784 char *ptr;
785 size_t size = 24;
786
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100787 /*
788 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
789 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
790 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100791 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700792
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800793 if (OOB_TAG_OFF)
794 size = round_up(size, OOB_TAG_OFF);
795
Patricia Alfonso73228c72020-10-13 16:55:06 -0700796 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
797 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
798
799 KUNIT_EXPECT_KASAN_FAIL(test,
800 kasan_ptr_result = memchr(ptr, '1', size + 1));
801
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700802 kfree(ptr);
803}
804
Patricia Alfonso73228c72020-10-13 16:55:06 -0700805static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700806{
807 char *ptr;
808 size_t size = 24;
809 int arr[9];
810
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100811 /*
812 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
813 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
814 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100815 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700816
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800817 if (OOB_TAG_OFF)
818 size = round_up(size, OOB_TAG_OFF);
819
Patricia Alfonso73228c72020-10-13 16:55:06 -0700820 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
821 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700822 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700823
824 KUNIT_EXPECT_KASAN_FAIL(test,
825 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700826 kfree(ptr);
827}
828
Patricia Alfonso73228c72020-10-13 16:55:06 -0700829static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700830{
831 char *ptr;
832 size_t size = 24;
833
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100834 /*
835 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
836 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
837 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100838 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700839
840 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
841 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700842
843 kfree(ptr);
844
845 /*
846 * Try to cause only 1 invalid access (less spam in dmesg).
847 * For that we need ptr to point to zeroed byte.
848 * Skip metadata that could be stored in freed object so ptr
849 * will likely point to zeroed byte.
850 */
851 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700852 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700853
Patricia Alfonso73228c72020-10-13 16:55:06 -0700854 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700855
Patricia Alfonso73228c72020-10-13 16:55:06 -0700856 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700857
Patricia Alfonso73228c72020-10-13 16:55:06 -0700858 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700859
Patricia Alfonso73228c72020-10-13 16:55:06 -0700860 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700861
Patricia Alfonso73228c72020-10-13 16:55:06 -0700862 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700863}
864
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800865static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700866{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800867 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
868 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
869 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
870 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
871 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
872 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
873 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
874 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
875}
876
877static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
878{
879 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
880 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
881 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
882 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
883 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
884 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
885 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
886 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
887
888#if defined(clear_bit_unlock_is_negative_byte)
889 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
890 clear_bit_unlock_is_negative_byte(nr, addr));
891#endif
892}
893
894static void kasan_bitops_generic(struct kunit *test)
895{
896 long *bits;
897
898 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100899 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800900
Marco Elver19a33ca2019-07-11 20:53:52 -0700901 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100902 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700903 * this way we do not actually corrupt other memory.
904 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800905 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700906 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700907
908 /*
909 * Below calls try to access bit within allocated memory; however, the
910 * below accesses are still out-of-bounds, since bitops are defined to
911 * operate on the whole long the bit is in.
912 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800913 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700914
915 /*
916 * Below calls try to access bit beyond allocated memory.
917 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800918 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700919
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800920 kfree(bits);
921}
Marco Elver19a33ca2019-07-11 20:53:52 -0700922
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800923static void kasan_bitops_tags(struct kunit *test)
924{
925 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700926
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100927 /* This test is specifically crafted for tag-based modes. */
928 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700929
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100930 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
931 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800932 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700933
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100934 /* Do the accesses past the 48 allocated bytes, but within the redone. */
935 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
936 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700937
Marco Elver19a33ca2019-07-11 20:53:52 -0700938 kfree(bits);
939}
940
Patricia Alfonso73228c72020-10-13 16:55:06 -0700941static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700942{
943 char *ptr;
944 size_t size = 16;
945
Marco Elverbb104ed2019-07-11 20:54:11 -0700946 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700947 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700948
Waiman Long453431a2020-08-06 23:18:13 -0700949 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700950 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700951}
952
Patricia Alfonso73228c72020-10-13 16:55:06 -0700953static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800954{
955 void *area;
956
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100957 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800958
959 /*
960 * We have to be careful not to hit the guard page.
961 * The MMU will catch that and crash us.
962 */
963 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700964 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800965
Patricia Alfonso73228c72020-10-13 16:55:06 -0700966 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800967 vfree(area);
968}
Daniel Axtens06513912019-11-30 17:54:53 -0800969
Andrey Konovalov782ba452021-02-03 15:35:00 +1100970/*
971 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
972 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
973 * modes.
974 */
975static void match_all_not_assigned(struct kunit *test)
976{
977 char *ptr;
978 struct page *pages;
979 int i, size, order;
980
981 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
982
983 for (i = 0; i < 256; i++) {
984 size = (get_random_int() % 1024) + 1;
985 ptr = kmalloc(size, GFP_KERNEL);
986 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
987 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
988 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
989 kfree(ptr);
990 }
991
992 for (i = 0; i < 256; i++) {
993 order = (get_random_int() % 4) + 1;
994 pages = alloc_pages(GFP_KERNEL, order);
995 ptr = page_address(pages);
996 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
997 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
998 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
999 free_pages((unsigned long)ptr, order);
1000 }
1001}
1002
1003/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
1004static void match_all_ptr_tag(struct kunit *test)
1005{
1006 char *ptr;
1007 u8 tag;
1008
1009 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1010
1011 ptr = kmalloc(128, GFP_KERNEL);
1012 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1013
1014 /* Backup the assigned tag. */
1015 tag = get_tag(ptr);
1016 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
1017
1018 /* Reset the tag to 0xff.*/
1019 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1020
1021 /* This access shouldn't trigger a KASAN report. */
1022 *ptr = 0;
1023
1024 /* Recover the pointer tag and free. */
1025 ptr = set_tag(ptr, tag);
1026 kfree(ptr);
1027}
1028
1029/* Check that there are no match-all memory tags for tag-based modes. */
1030static void match_all_mem_tag(struct kunit *test)
1031{
1032 char *ptr;
1033 int tag;
1034
1035 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1036
1037 ptr = kmalloc(128, GFP_KERNEL);
1038 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1039 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1040
1041 /* For each possible tag value not matching the pointer tag. */
1042 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1043 if (tag == get_tag(ptr))
1044 continue;
1045
1046 /* Mark the first memory granule with the chosen memory tag. */
1047 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
1048
1049 /* This access must cause a KASAN report. */
1050 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1051 }
1052
1053 /* Recover the memory tag and free. */
1054 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
1055 kfree(ptr);
1056}
1057
Patricia Alfonso73228c72020-10-13 16:55:06 -07001058static struct kunit_case kasan_kunit_test_cases[] = {
1059 KUNIT_CASE(kmalloc_oob_right),
1060 KUNIT_CASE(kmalloc_oob_left),
1061 KUNIT_CASE(kmalloc_node_oob_right),
1062 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1063 KUNIT_CASE(kmalloc_pagealloc_uaf),
1064 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalove449e272021-02-03 15:35:06 +11001065 KUNIT_CASE(pagealloc_oob_right),
1066 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001067 KUNIT_CASE(kmalloc_large_oob_right),
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +11001068 KUNIT_CASE(krealloc_more_oob),
1069 KUNIT_CASE(krealloc_less_oob),
1070 KUNIT_CASE(krealloc_pagealloc_more_oob),
1071 KUNIT_CASE(krealloc_pagealloc_less_oob),
Andrey Konovalov4f62c692021-02-12 17:14:50 +11001072 KUNIT_CASE(krealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001073 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001074 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001075 KUNIT_CASE(kmalloc_oob_in_memset),
1076 KUNIT_CASE(kmalloc_oob_memset_2),
1077 KUNIT_CASE(kmalloc_oob_memset_4),
1078 KUNIT_CASE(kmalloc_oob_memset_8),
1079 KUNIT_CASE(kmalloc_oob_memset_16),
1080 KUNIT_CASE(kmalloc_memmove_invalid_size),
1081 KUNIT_CASE(kmalloc_uaf),
1082 KUNIT_CASE(kmalloc_uaf_memset),
1083 KUNIT_CASE(kmalloc_uaf2),
1084 KUNIT_CASE(kfree_via_page),
1085 KUNIT_CASE(kfree_via_phys),
1086 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov9346eae2021-02-03 15:35:06 +11001087 KUNIT_CASE(kmem_cache_accounted),
1088 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001089 KUNIT_CASE(kasan_global_oob),
1090 KUNIT_CASE(kasan_stack_oob),
1091 KUNIT_CASE(kasan_alloca_oob_left),
1092 KUNIT_CASE(kasan_alloca_oob_right),
1093 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov696574e2021-02-03 15:35:05 +11001094 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001095 KUNIT_CASE(kmem_cache_double_free),
1096 KUNIT_CASE(kmem_cache_invalid_free),
1097 KUNIT_CASE(kasan_memchr),
1098 KUNIT_CASE(kasan_memcmp),
1099 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001100 KUNIT_CASE(kasan_bitops_generic),
1101 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001102 KUNIT_CASE(kmalloc_double_kzfree),
1103 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +11001104 KUNIT_CASE(match_all_not_assigned),
1105 KUNIT_CASE(match_all_ptr_tag),
1106 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001107 {}
1108};
Walter Wu387d6e42020-08-06 23:24:42 -07001109
Patricia Alfonso73228c72020-10-13 16:55:06 -07001110static struct kunit_suite kasan_kunit_test_suite = {
1111 .name = "kasan",
1112 .init = kasan_test_init,
1113 .test_cases = kasan_kunit_test_cases,
1114 .exit = kasan_test_exit,
1115};
Walter Wu387d6e42020-08-06 23:24:42 -07001116
Patricia Alfonso73228c72020-10-13 16:55:06 -07001117kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001118
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001119MODULE_LICENSE("GPL");