blob: ffebad2f0e6eeb40f9b110cdefb7c342a0e42998 [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
Patricia Alfonso73228c72020-10-13 16:55:06 -0700356static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800357{
358 struct {
359 u64 words[2];
360 } *ptr1, *ptr2;
361
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800362 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100363 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800364
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800365 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700366 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
367
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800368 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700369 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
370
371 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800372 kfree(ptr1);
373 kfree(ptr2);
374}
375
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800376static void kmalloc_uaf_16(struct kunit *test)
377{
378 struct {
379 u64 words[2];
380 } *ptr1, *ptr2;
381
382 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
383 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
384
385 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
386 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
387 kfree(ptr2);
388
389 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
390 kfree(ptr1);
391}
392
Patricia Alfonso73228c72020-10-13 16:55:06 -0700393static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800394{
395 char *ptr;
396 size_t size = 8;
397
Wang Longf523e732015-11-05 18:51:15 -0800398 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700399 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800400
Patricia Alfonso73228c72020-10-13 16:55:06 -0700401 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800402 kfree(ptr);
403}
404
Patricia Alfonso73228c72020-10-13 16:55:06 -0700405static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800406{
407 char *ptr;
408 size_t size = 8;
409
Wang Longf523e732015-11-05 18:51:15 -0800410 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700411 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800412
Patricia Alfonso73228c72020-10-13 16:55:06 -0700413 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800414 kfree(ptr);
415}
416
417
Patricia Alfonso73228c72020-10-13 16:55:06 -0700418static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800419{
420 char *ptr;
421 size_t size = 8;
422
Wang Longf523e732015-11-05 18:51:15 -0800423 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700424 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800425
Patricia Alfonso73228c72020-10-13 16:55:06 -0700426 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800427 kfree(ptr);
428}
429
Patricia Alfonso73228c72020-10-13 16:55:06 -0700430static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800431{
432 char *ptr;
433 size_t size = 16;
434
Wang Longf523e732015-11-05 18:51:15 -0800435 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700436 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800437
Patricia Alfonso73228c72020-10-13 16:55:06 -0700438 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800439 kfree(ptr);
440}
441
Patricia Alfonso73228c72020-10-13 16:55:06 -0700442static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800443{
444 char *ptr;
445 size_t size = 666;
446
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800447 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700448 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800449
Patricia Alfonso73228c72020-10-13 16:55:06 -0700450 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800451 kfree(ptr);
452}
453
Patricia Alfonso73228c72020-10-13 16:55:06 -0700454static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700455{
456 char *ptr;
457 size_t size = 64;
458 volatile size_t invalid_size = -2;
459
Walter Wu98f3b562020-04-01 21:09:40 -0700460 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700461 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700462
463 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700464
465 KUNIT_EXPECT_KASAN_FAIL(test,
466 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700467 kfree(ptr);
468}
469
Patricia Alfonso73228c72020-10-13 16:55:06 -0700470static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800471{
472 char *ptr;
473 size_t size = 10;
474
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800475 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700476 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800477
478 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700479 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800480}
481
Patricia Alfonso73228c72020-10-13 16:55:06 -0700482static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800483{
484 char *ptr;
485 size_t size = 33;
486
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800487 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700488 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800489
490 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700491 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800492}
493
Patricia Alfonso73228c72020-10-13 16:55:06 -0700494static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800495{
496 char *ptr1, *ptr2;
497 size_t size = 43;
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100498 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800499
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100500again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800501 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700502 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800503
504 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800505
Patricia Alfonso73228c72020-10-13 16:55:06 -0700506 ptr2 = kmalloc(size, GFP_KERNEL);
507 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
508
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100509 /*
510 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
511 * Allow up to 16 attempts at generating different tags.
512 */
513 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
514 kfree(ptr2);
515 goto again;
516 }
517
Patricia Alfonso73228c72020-10-13 16:55:06 -0700518 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
519 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
520
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800521 kfree(ptr2);
522}
523
Patricia Alfonso73228c72020-10-13 16:55:06 -0700524static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700525{
526 char *ptr;
527 size_t size = 8;
528 struct page *page;
529 unsigned long offset;
530
Mark Rutlandb92a9532019-09-23 15:34:16 -0700531 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700532 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700533
534 page = virt_to_page(ptr);
535 offset = offset_in_page(ptr);
536 kfree(page_address(page) + offset);
537}
538
Patricia Alfonso73228c72020-10-13 16:55:06 -0700539static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700540{
541 char *ptr;
542 size_t size = 8;
543 phys_addr_t phys;
544
Mark Rutlandb92a9532019-09-23 15:34:16 -0700545 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700546 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700547
548 phys = virt_to_phys(ptr);
549 kfree(phys_to_virt(phys));
550}
551
Patricia Alfonso73228c72020-10-13 16:55:06 -0700552static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800553{
554 char *p;
555 size_t size = 200;
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100556 struct kmem_cache *cache;
557
558 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700559 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100560
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800561 p = kmem_cache_alloc(cache, GFP_KERNEL);
562 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700563 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800564 kmem_cache_destroy(cache);
565 return;
566 }
567
Patricia Alfonso73228c72020-10-13 16:55:06 -0700568 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100569
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800570 kmem_cache_free(cache, p);
571 kmem_cache_destroy(cache);
572}
573
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100574static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800575{
576 int i;
577 char *p;
578 size_t size = 200;
579 struct kmem_cache *cache;
580
581 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700582 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800583
Greg Thelen0386bf32017-02-24 15:00:08 -0800584 /*
585 * Several allocations with a delay to allow for lazy per memcg kmem
586 * cache creation.
587 */
588 for (i = 0; i < 5; i++) {
589 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800590 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800591 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800592
Greg Thelen0386bf32017-02-24 15:00:08 -0800593 kmem_cache_free(cache, p);
594 msleep(100);
595 }
596
597free_cache:
598 kmem_cache_destroy(cache);
599}
600
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100601static void kmem_cache_bulk(struct kunit *test)
602{
603 struct kmem_cache *cache;
604 size_t size = 200;
605 char *p[10];
606 bool ret;
607 int i;
608
609 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
610 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
611
612 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
613 if (!ret) {
614 kunit_err(test, "Allocation failed: %s\n", __func__);
615 kmem_cache_destroy(cache);
616 return;
617 }
618
619 for (i = 0; i < ARRAY_SIZE(p); i++)
620 p[i][0] = p[i][size - 1] = 42;
621
622 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
623 kmem_cache_destroy(cache);
624}
625
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800626static char global_array[10];
627
Patricia Alfonso73228c72020-10-13 16:55:06 -0700628static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800629{
630 volatile int i = 3;
631 char *p = &global_array[ARRAY_SIZE(global_array) + i];
632
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800633 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100634 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800635
Patricia Alfonso73228c72020-10-13 16:55:06 -0700636 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800637}
638
Andrey Konovalov696574e2021-02-03 15:35:05 +1100639/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700640static void ksize_unpoisons_memory(struct kunit *test)
641{
642 char *ptr;
643 size_t size = 123, real_size;
644
645 ptr = kmalloc(size, GFP_KERNEL);
646 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
647 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100648
649 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700650 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100651
652 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700653 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100654
Patricia Alfonso73228c72020-10-13 16:55:06 -0700655 kfree(ptr);
656}
657
Andrey Konovalov696574e2021-02-03 15:35:05 +1100658/*
659 * Check that a use-after-free is detected by ksize() and via normal accesses
660 * after it.
661 */
662static void ksize_uaf(struct kunit *test)
663{
664 char *ptr;
665 int size = 128 - KASAN_GRANULE_SIZE;
666
667 ptr = kmalloc(size, GFP_KERNEL);
668 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
669 kfree(ptr);
670
671 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
672 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
673 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
674}
675
Patricia Alfonso73228c72020-10-13 16:55:06 -0700676static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800677{
678 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700679 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800680 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
681
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100682 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700683
Patricia Alfonso73228c72020-10-13 16:55:06 -0700684 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700685}
686
Patricia Alfonso73228c72020-10-13 16:55:06 -0700687static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800688{
689 volatile int i = 10;
690 char alloca_array[i];
691 char *p = alloca_array - 1;
692
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800693 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100694 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
695 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700696
697 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800698}
699
Patricia Alfonso73228c72020-10-13 16:55:06 -0700700static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800701{
702 volatile int i = 10;
703 char alloca_array[i];
704 char *p = alloca_array + i;
705
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800706 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100707 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
708 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700709
710 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800711}
712
Patricia Alfonso73228c72020-10-13 16:55:06 -0700713static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800714{
715 char *p;
716 size_t size = 200;
717 struct kmem_cache *cache;
718
719 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700720 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
721
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800722 p = kmem_cache_alloc(cache, GFP_KERNEL);
723 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700724 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800725 kmem_cache_destroy(cache);
726 return;
727 }
728
729 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700730 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800731 kmem_cache_destroy(cache);
732}
733
Patricia Alfonso73228c72020-10-13 16:55:06 -0700734static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800735{
736 char *p;
737 size_t size = 200;
738 struct kmem_cache *cache;
739
740 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
741 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700742 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
743
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800744 p = kmem_cache_alloc(cache, GFP_KERNEL);
745 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700746 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800747 kmem_cache_destroy(cache);
748 return;
749 }
750
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100751 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700752 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700753
754 /*
755 * Properly free the object to prevent the "Objects remaining in
756 * test_cache on __kmem_cache_shutdown" BUG failure.
757 */
758 kmem_cache_free(cache, p);
759
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800760 kmem_cache_destroy(cache);
761}
762
Patricia Alfonso73228c72020-10-13 16:55:06 -0700763static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700764{
765 char *ptr;
766 size_t size = 24;
767
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100768 /*
769 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
770 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
771 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100772 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700773
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800774 if (OOB_TAG_OFF)
775 size = round_up(size, OOB_TAG_OFF);
776
Patricia Alfonso73228c72020-10-13 16:55:06 -0700777 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
778 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
779
780 KUNIT_EXPECT_KASAN_FAIL(test,
781 kasan_ptr_result = memchr(ptr, '1', size + 1));
782
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700783 kfree(ptr);
784}
785
Patricia Alfonso73228c72020-10-13 16:55:06 -0700786static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700787{
788 char *ptr;
789 size_t size = 24;
790 int arr[9];
791
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100792 /*
793 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
794 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
795 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100796 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700797
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800798 if (OOB_TAG_OFF)
799 size = round_up(size, OOB_TAG_OFF);
800
Patricia Alfonso73228c72020-10-13 16:55:06 -0700801 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
802 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700803 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700804
805 KUNIT_EXPECT_KASAN_FAIL(test,
806 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700807 kfree(ptr);
808}
809
Patricia Alfonso73228c72020-10-13 16:55:06 -0700810static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700811{
812 char *ptr;
813 size_t size = 24;
814
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100815 /*
816 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
817 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
818 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100819 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700820
821 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
822 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700823
824 kfree(ptr);
825
826 /*
827 * Try to cause only 1 invalid access (less spam in dmesg).
828 * For that we need ptr to point to zeroed byte.
829 * Skip metadata that could be stored in freed object so ptr
830 * will likely point to zeroed byte.
831 */
832 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700833 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700834
Patricia Alfonso73228c72020-10-13 16:55:06 -0700835 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700836
Patricia Alfonso73228c72020-10-13 16:55:06 -0700837 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700838
Patricia Alfonso73228c72020-10-13 16:55:06 -0700839 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700840
Patricia Alfonso73228c72020-10-13 16:55:06 -0700841 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700842
Patricia Alfonso73228c72020-10-13 16:55:06 -0700843 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700844}
845
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800846static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700847{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800848 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
849 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
850 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
851 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
852 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
853 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
854 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
855 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
856}
857
858static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
859{
860 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
861 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
862 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
863 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
864 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
865 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
866 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
867 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
868
869#if defined(clear_bit_unlock_is_negative_byte)
870 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
871 clear_bit_unlock_is_negative_byte(nr, addr));
872#endif
873}
874
875static void kasan_bitops_generic(struct kunit *test)
876{
877 long *bits;
878
879 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100880 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800881
Marco Elver19a33ca2019-07-11 20:53:52 -0700882 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100883 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700884 * this way we do not actually corrupt other memory.
885 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800886 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700887 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700888
889 /*
890 * Below calls try to access bit within allocated memory; however, the
891 * below accesses are still out-of-bounds, since bitops are defined to
892 * operate on the whole long the bit is in.
893 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800894 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700895
896 /*
897 * Below calls try to access bit beyond allocated memory.
898 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800899 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700900
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800901 kfree(bits);
902}
Marco Elver19a33ca2019-07-11 20:53:52 -0700903
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800904static void kasan_bitops_tags(struct kunit *test)
905{
906 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700907
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100908 /* This test is specifically crafted for tag-based modes. */
909 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700910
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100911 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
912 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800913 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700914
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100915 /* Do the accesses past the 48 allocated bytes, but within the redone. */
916 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
917 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700918
Marco Elver19a33ca2019-07-11 20:53:52 -0700919 kfree(bits);
920}
921
Patricia Alfonso73228c72020-10-13 16:55:06 -0700922static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700923{
924 char *ptr;
925 size_t size = 16;
926
Marco Elverbb104ed2019-07-11 20:54:11 -0700927 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700928 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700929
Waiman Long453431a2020-08-06 23:18:13 -0700930 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700931 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700932}
933
Patricia Alfonso73228c72020-10-13 16:55:06 -0700934static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800935{
936 void *area;
937
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100938 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800939
940 /*
941 * We have to be careful not to hit the guard page.
942 * The MMU will catch that and crash us.
943 */
944 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700945 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800946
Patricia Alfonso73228c72020-10-13 16:55:06 -0700947 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800948 vfree(area);
949}
Daniel Axtens06513912019-11-30 17:54:53 -0800950
Andrey Konovalov782ba452021-02-03 15:35:00 +1100951/*
952 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
953 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
954 * modes.
955 */
956static void match_all_not_assigned(struct kunit *test)
957{
958 char *ptr;
959 struct page *pages;
960 int i, size, order;
961
962 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
963
964 for (i = 0; i < 256; i++) {
965 size = (get_random_int() % 1024) + 1;
966 ptr = kmalloc(size, GFP_KERNEL);
967 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
968 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
969 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
970 kfree(ptr);
971 }
972
973 for (i = 0; i < 256; i++) {
974 order = (get_random_int() % 4) + 1;
975 pages = alloc_pages(GFP_KERNEL, order);
976 ptr = page_address(pages);
977 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
978 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
979 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
980 free_pages((unsigned long)ptr, order);
981 }
982}
983
984/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
985static void match_all_ptr_tag(struct kunit *test)
986{
987 char *ptr;
988 u8 tag;
989
990 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
991
992 ptr = kmalloc(128, GFP_KERNEL);
993 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
994
995 /* Backup the assigned tag. */
996 tag = get_tag(ptr);
997 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
998
999 /* Reset the tag to 0xff.*/
1000 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
1001
1002 /* This access shouldn't trigger a KASAN report. */
1003 *ptr = 0;
1004
1005 /* Recover the pointer tag and free. */
1006 ptr = set_tag(ptr, tag);
1007 kfree(ptr);
1008}
1009
1010/* Check that there are no match-all memory tags for tag-based modes. */
1011static void match_all_mem_tag(struct kunit *test)
1012{
1013 char *ptr;
1014 int tag;
1015
1016 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
1017
1018 ptr = kmalloc(128, GFP_KERNEL);
1019 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
1020 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
1021
1022 /* For each possible tag value not matching the pointer tag. */
1023 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
1024 if (tag == get_tag(ptr))
1025 continue;
1026
1027 /* Mark the first memory granule with the chosen memory tag. */
1028 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
1029
1030 /* This access must cause a KASAN report. */
1031 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
1032 }
1033
1034 /* Recover the memory tag and free. */
1035 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
1036 kfree(ptr);
1037}
1038
Patricia Alfonso73228c72020-10-13 16:55:06 -07001039static struct kunit_case kasan_kunit_test_cases[] = {
1040 KUNIT_CASE(kmalloc_oob_right),
1041 KUNIT_CASE(kmalloc_oob_left),
1042 KUNIT_CASE(kmalloc_node_oob_right),
1043 KUNIT_CASE(kmalloc_pagealloc_oob_right),
1044 KUNIT_CASE(kmalloc_pagealloc_uaf),
1045 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalove449e272021-02-03 15:35:06 +11001046 KUNIT_CASE(pagealloc_oob_right),
1047 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001048 KUNIT_CASE(kmalloc_large_oob_right),
Andrey Konovalovd7ef7af2021-02-12 17:14:50 +11001049 KUNIT_CASE(krealloc_more_oob),
1050 KUNIT_CASE(krealloc_less_oob),
1051 KUNIT_CASE(krealloc_pagealloc_more_oob),
1052 KUNIT_CASE(krealloc_pagealloc_less_oob),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001053 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001054 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001055 KUNIT_CASE(kmalloc_oob_in_memset),
1056 KUNIT_CASE(kmalloc_oob_memset_2),
1057 KUNIT_CASE(kmalloc_oob_memset_4),
1058 KUNIT_CASE(kmalloc_oob_memset_8),
1059 KUNIT_CASE(kmalloc_oob_memset_16),
1060 KUNIT_CASE(kmalloc_memmove_invalid_size),
1061 KUNIT_CASE(kmalloc_uaf),
1062 KUNIT_CASE(kmalloc_uaf_memset),
1063 KUNIT_CASE(kmalloc_uaf2),
1064 KUNIT_CASE(kfree_via_page),
1065 KUNIT_CASE(kfree_via_phys),
1066 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov9346eae2021-02-03 15:35:06 +11001067 KUNIT_CASE(kmem_cache_accounted),
1068 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001069 KUNIT_CASE(kasan_global_oob),
1070 KUNIT_CASE(kasan_stack_oob),
1071 KUNIT_CASE(kasan_alloca_oob_left),
1072 KUNIT_CASE(kasan_alloca_oob_right),
1073 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov696574e2021-02-03 15:35:05 +11001074 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001075 KUNIT_CASE(kmem_cache_double_free),
1076 KUNIT_CASE(kmem_cache_invalid_free),
1077 KUNIT_CASE(kasan_memchr),
1078 KUNIT_CASE(kasan_memcmp),
1079 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001080 KUNIT_CASE(kasan_bitops_generic),
1081 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001082 KUNIT_CASE(kmalloc_double_kzfree),
1083 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +11001084 KUNIT_CASE(match_all_not_assigned),
1085 KUNIT_CASE(match_all_ptr_tag),
1086 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001087 {}
1088};
Walter Wu387d6e42020-08-06 23:24:42 -07001089
Patricia Alfonso73228c72020-10-13 16:55:06 -07001090static struct kunit_suite kasan_kunit_test_suite = {
1091 .name = "kasan",
1092 .init = kasan_test_init,
1093 .test_cases = kasan_kunit_test_cases,
1094 .exit = kasan_test_exit,
1095};
Walter Wu387d6e42020-08-06 23:24:42 -07001096
Patricia Alfonso73228c72020-10-13 16:55:06 -07001097kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001098
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001099MODULE_LICENSE("GPL");