blob: 25576303897bd1ab9400c0c0425657b042b63530 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrey Ryabinin3f158012015-02-13 14:39:53 -08002/*
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
Andrey Ryabinin3f158012015-02-13 14:39:53 -08006 */
7
Marco Elver19a33ca2019-07-11 20:53:52 -07008#include <linux/bitops.h>
Greg Thelen0386bf32017-02-24 15:00:08 -08009#include <linux/delay.h>
Marco Elver19a33ca2019-07-11 20:53:52 -070010#include <linux/kasan.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080011#include <linux/kernel.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070012#include <linux/mm.h>
Marco Elver19a33ca2019-07-11 20:53:52 -070013#include <linux/mman.h>
14#include <linux/module.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080015#include <linux/printk.h>
Andrey Konovalov573a4802021-02-24 12:05:21 -080016#include <linux/random.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080017#include <linux/slab.h>
18#include <linux/string.h>
Andrey Ryabinineae08dc2016-05-20 16:59:34 -070019#include <linux/uaccess.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070020#include <linux/io.h>
Daniel Axtens06513912019-11-30 17:54:53 -080021#include <linux/vmalloc.h>
Mark Rutlandb92a9532019-09-23 15:34:16 -070022
23#include <asm/page.h>
Andrey Ryabinin3f158012015-02-13 14:39:53 -080024
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070025#include <kunit/test.h>
26
Walter Wuf33a0142020-08-06 23:24:54 -070027#include "../mm/kasan/kasan.h"
28
Andrey Konovalov1f600622020-12-22 12:00:24 -080029#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
Walter Wuf33a0142020-08-06 23:24:54 -070030
Dmitry Vyukov828347f2016-11-30 15:54:16 -080031/*
Andrey Konovalov0fd37922021-02-24 12:05:13 -080032 * Some tests use these global variables to store return values from function
33 * calls that could otherwise be eliminated by the compiler as dead code.
Daniel Axtensadb72ae2020-06-03 15:56:43 -070034 */
Daniel Axtensadb72ae2020-06-03 15:56:43 -070035void *kasan_ptr_result;
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070036int kasan_int_result;
37
38static struct kunit_resource resource;
39static struct kunit_kasan_expectation fail_data;
40static bool multishot;
41
Andrey Konovalov0fd37922021-02-24 12:05:13 -080042/*
43 * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
Andrey Konovalovf05842c2021-02-24 12:05:26 -080044 * first detected bug and panic the kernel if panic_on_warn is enabled. For
45 * hardware tag-based KASAN also allow tag checking to be reenabled for each
46 * test, see the comment for KUNIT_EXPECT_KASAN_FAIL().
Andrey Konovalov0fd37922021-02-24 12:05:13 -080047 */
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070048static int kasan_test_init(struct kunit *test)
49{
Andrey Konovalovd82dc3a2021-02-24 12:06:02 -080050 if (!kasan_enabled()) {
51 kunit_err(test, "can't run KASAN tests with KASAN disabled");
52 return -1;
53 }
54
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070055 multishot = kasan_save_enable_multi_shot();
Andrey Konovalovf05842c2021-02-24 12:05:26 -080056 kasan_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 Konovalovf05842c2021-02-24 12:05:26 -080062 kasan_set_tagging_report_once(true);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070063 kasan_restore_multi_shot(multishot);
64}
65
66/**
Andrey Konovalov0fd37922021-02-24 12:05:13 -080067 * 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 Konovalovf05842c2021-02-24 12:05:26 -080071 *
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 Konovalov2e4bde62021-02-24 12:05:34 -080076 *
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 Konovalovf05842c2021-02-24 12:05:26 -080082#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
83 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
84 migrate_disable(); \
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080085 WRITE_ONCE(fail_data.report_expected, true); \
86 WRITE_ONCE(fail_data.report_found, false); \
Andrey Konovalovf05842c2021-02-24 12:05:26 -080087 kunit_add_named_resource(test, \
88 NULL, \
89 NULL, \
90 &resource, \
91 "kasan_data", &fail_data); \
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080092 barrier(); \
Andrey Konovalovf05842c2021-02-24 12:05:26 -080093 expression; \
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080094 barrier(); \
Andrey Konovalovf05842c2021-02-24 12:05:26 -080095 KUNIT_EXPECT_EQ(test, \
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080096 READ_ONCE(fail_data.report_expected), \
97 READ_ONCE(fail_data.report_found)); \
Andrey Konovalovf05842c2021-02-24 12:05:26 -080098 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
Andrey Konovalov2e4bde62021-02-24 12:05:34 -080099 if (READ_ONCE(fail_data.report_found)) \
Andrey Konovalovf05842c2021-02-24 12:05:26 -0800100 kasan_enable_tagging(); \
101 migrate_enable(); \
102 } \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700103} while (0)
104
Andrey Konovalovda17e372021-02-24 12:05:17 -0800105#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 Konovalov858bdeb2021-02-24 12:05:55 -0800155/*
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 Konovalovda17e372021-02-24 12:05:17 -0800166 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 Konovalov858bdeb2021-02-24 12:05:55 -0800172
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 Konovalovda17e372021-02-24 12:05:17 -0800181 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 Konovalov858bdeb2021-02-24 12:05:55 -0800186
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 Konovalovda17e372021-02-24 12:05:17 -0800195 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 Konovalov858bdeb2021-02-24 12:05:55 -0800203static 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 Konovalov0fd37922021-02-24 12:05:13 -0800243
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
Patricia Alfonso73228c72020-10-13 16:55:06 -0700255static void kmalloc_oob_krealloc_more(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800256{
257 char *ptr1, *ptr2;
258 size_t size1 = 17;
259 size_t size2 = 19;
260
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800261 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700262 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
263
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800264 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700265 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800266
Patricia Alfonso73228c72020-10-13 16:55:06 -0700267 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800268 kfree(ptr2);
269}
270
Patricia Alfonso73228c72020-10-13 16:55:06 -0700271static void kmalloc_oob_krealloc_less(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800272{
273 char *ptr1, *ptr2;
274 size_t size1 = 17;
275 size_t size2 = 15;
276
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800277 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700278 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
279
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800280 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700281 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700282
Patricia Alfonso73228c72020-10-13 16:55:06 -0700283 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800284 kfree(ptr2);
285}
286
Patricia Alfonso73228c72020-10-13 16:55:06 -0700287static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800288{
289 struct {
290 u64 words[2];
291 } *ptr1, *ptr2;
292
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800293 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800294 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800295
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800296 ptr1 = kmalloc(sizeof(*ptr1) - 3, 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 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
301
302 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800303 kfree(ptr1);
304 kfree(ptr2);
305}
306
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800307static void kmalloc_uaf_16(struct kunit *test)
308{
309 struct {
310 u64 words[2];
311 } *ptr1, *ptr2;
312
313 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
314 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
315
316 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
317 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
318 kfree(ptr2);
319
320 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
321 kfree(ptr1);
322}
323
Patricia Alfonso73228c72020-10-13 16:55:06 -0700324static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800325{
326 char *ptr;
327 size_t size = 8;
328
Wang Longf523e732015-11-05 18:51:15 -0800329 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700330 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800331
Patricia Alfonso73228c72020-10-13 16:55:06 -0700332 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800333 kfree(ptr);
334}
335
Patricia Alfonso73228c72020-10-13 16:55:06 -0700336static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800337{
338 char *ptr;
339 size_t size = 8;
340
Wang Longf523e732015-11-05 18:51:15 -0800341 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700342 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800343
Patricia Alfonso73228c72020-10-13 16:55:06 -0700344 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800345 kfree(ptr);
346}
347
348
Patricia Alfonso73228c72020-10-13 16:55:06 -0700349static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800350{
351 char *ptr;
352 size_t size = 8;
353
Wang Longf523e732015-11-05 18:51:15 -0800354 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700355 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800356
Patricia Alfonso73228c72020-10-13 16:55:06 -0700357 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800358 kfree(ptr);
359}
360
Patricia Alfonso73228c72020-10-13 16:55:06 -0700361static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800362{
363 char *ptr;
364 size_t size = 16;
365
Wang Longf523e732015-11-05 18:51:15 -0800366 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700367 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800368
Patricia Alfonso73228c72020-10-13 16:55:06 -0700369 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800370 kfree(ptr);
371}
372
Patricia Alfonso73228c72020-10-13 16:55:06 -0700373static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800374{
375 char *ptr;
376 size_t size = 666;
377
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800378 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700379 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800380
Patricia Alfonso73228c72020-10-13 16:55:06 -0700381 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800382 kfree(ptr);
383}
384
Patricia Alfonso73228c72020-10-13 16:55:06 -0700385static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700386{
387 char *ptr;
388 size_t size = 64;
389 volatile size_t invalid_size = -2;
390
Walter Wu98f3b562020-04-01 21:09:40 -0700391 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700392 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700393
394 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700395
396 KUNIT_EXPECT_KASAN_FAIL(test,
397 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700398 kfree(ptr);
399}
400
Patricia Alfonso73228c72020-10-13 16:55:06 -0700401static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800402{
403 char *ptr;
404 size_t size = 10;
405
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800406 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700407 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800408
409 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700410 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800411}
412
Patricia Alfonso73228c72020-10-13 16:55:06 -0700413static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800414{
415 char *ptr;
416 size_t size = 33;
417
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800418 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700419 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800420
421 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700422 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800423}
424
Patricia Alfonso73228c72020-10-13 16:55:06 -0700425static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800426{
427 char *ptr1, *ptr2;
428 size_t size = 43;
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800429 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800430
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800431again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800432 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800434
435 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800436
Patricia Alfonso73228c72020-10-13 16:55:06 -0700437 ptr2 = kmalloc(size, GFP_KERNEL);
438 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
439
Andrey Konovalov1b1df4c2021-02-24 12:05:38 -0800440 /*
441 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
442 * Allow up to 16 attempts at generating different tags.
443 */
444 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
445 kfree(ptr2);
446 goto again;
447 }
448
Patricia Alfonso73228c72020-10-13 16:55:06 -0700449 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
450 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
451
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800452 kfree(ptr2);
453}
454
Patricia Alfonso73228c72020-10-13 16:55:06 -0700455static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700456{
457 char *ptr;
458 size_t size = 8;
459 struct page *page;
460 unsigned long offset;
461
Mark Rutlandb92a9532019-09-23 15:34:16 -0700462 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700463 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700464
465 page = virt_to_page(ptr);
466 offset = offset_in_page(ptr);
467 kfree(page_address(page) + offset);
468}
469
Patricia Alfonso73228c72020-10-13 16:55:06 -0700470static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700471{
472 char *ptr;
473 size_t size = 8;
474 phys_addr_t phys;
475
Mark Rutlandb92a9532019-09-23 15:34:16 -0700476 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700477 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700478
479 phys = virt_to_phys(ptr);
480 kfree(phys_to_virt(phys));
481}
482
Patricia Alfonso73228c72020-10-13 16:55:06 -0700483static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800484{
485 char *p;
486 size_t size = 200;
Andrey Konovalov11516132021-02-24 12:05:59 -0800487 struct kmem_cache *cache;
488
489 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700490 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov11516132021-02-24 12:05:59 -0800491
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800492 p = kmem_cache_alloc(cache, GFP_KERNEL);
493 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700494 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800495 kmem_cache_destroy(cache);
496 return;
497 }
498
Patricia Alfonso73228c72020-10-13 16:55:06 -0700499 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov11516132021-02-24 12:05:59 -0800500
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800501 kmem_cache_free(cache, p);
502 kmem_cache_destroy(cache);
503}
504
Andrey Konovalov11516132021-02-24 12:05:59 -0800505static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800506{
507 int i;
508 char *p;
509 size_t size = 200;
510 struct kmem_cache *cache;
511
512 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700513 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800514
Greg Thelen0386bf32017-02-24 15:00:08 -0800515 /*
516 * Several allocations with a delay to allow for lazy per memcg kmem
517 * cache creation.
518 */
519 for (i = 0; i < 5; i++) {
520 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800521 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800522 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800523
Greg Thelen0386bf32017-02-24 15:00:08 -0800524 kmem_cache_free(cache, p);
525 msleep(100);
526 }
527
528free_cache:
529 kmem_cache_destroy(cache);
530}
531
Andrey Konovalov11516132021-02-24 12:05:59 -0800532static void kmem_cache_bulk(struct kunit *test)
533{
534 struct kmem_cache *cache;
535 size_t size = 200;
536 char *p[10];
537 bool ret;
538 int i;
539
540 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
541 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
542
543 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
544 if (!ret) {
545 kunit_err(test, "Allocation failed: %s\n", __func__);
546 kmem_cache_destroy(cache);
547 return;
548 }
549
550 for (i = 0; i < ARRAY_SIZE(p); i++)
551 p[i][0] = p[i][size - 1] = 42;
552
553 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
554 kmem_cache_destroy(cache);
555}
556
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800557static char global_array[10];
558
Patricia Alfonso73228c72020-10-13 16:55:06 -0700559static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800560{
561 volatile int i = 3;
562 char *p = &global_array[ARRAY_SIZE(global_array) + i];
563
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800564 /* Only generic mode instruments globals. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800565 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800566
Patricia Alfonso73228c72020-10-13 16:55:06 -0700567 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800568}
569
Andrey Konovalov611806b2021-02-24 12:05:50 -0800570/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700571static void ksize_unpoisons_memory(struct kunit *test)
572{
573 char *ptr;
574 size_t size = 123, real_size;
575
576 ptr = kmalloc(size, GFP_KERNEL);
577 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
578 real_size = ksize(ptr);
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800579
580 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700581 ptr[size] = 'x';
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800582
583 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700584 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800585
Patricia Alfonso73228c72020-10-13 16:55:06 -0700586 kfree(ptr);
587}
588
Andrey Konovalov611806b2021-02-24 12:05:50 -0800589/*
590 * Check that a use-after-free is detected by ksize() and via normal accesses
591 * after it.
592 */
593static void ksize_uaf(struct kunit *test)
594{
595 char *ptr;
596 int size = 128 - KASAN_GRANULE_SIZE;
597
598 ptr = kmalloc(size, GFP_KERNEL);
599 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
600 kfree(ptr);
601
602 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
603 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
604 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
605}
606
Patricia Alfonso73228c72020-10-13 16:55:06 -0700607static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800608{
609 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700610 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800611 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
612
Andrey Konovalovda17e372021-02-24 12:05:17 -0800613 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700614
Patricia Alfonso73228c72020-10-13 16:55:06 -0700615 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700616}
617
Patricia Alfonso73228c72020-10-13 16:55:06 -0700618static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800619{
620 volatile int i = 10;
621 char alloca_array[i];
622 char *p = alloca_array - 1;
623
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800624 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800625 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
626 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700627
628 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800629}
630
Patricia Alfonso73228c72020-10-13 16:55:06 -0700631static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800632{
633 volatile int i = 10;
634 char alloca_array[i];
635 char *p = alloca_array + i;
636
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800637 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800638 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
639 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700640
641 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800642}
643
Patricia Alfonso73228c72020-10-13 16:55:06 -0700644static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800645{
646 char *p;
647 size_t size = 200;
648 struct kmem_cache *cache;
649
650 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700651 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
652
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800653 p = kmem_cache_alloc(cache, GFP_KERNEL);
654 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700655 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800656 kmem_cache_destroy(cache);
657 return;
658 }
659
660 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700661 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800662 kmem_cache_destroy(cache);
663}
664
Patricia Alfonso73228c72020-10-13 16:55:06 -0700665static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800666{
667 char *p;
668 size_t size = 200;
669 struct kmem_cache *cache;
670
671 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
672 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700673 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
674
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800675 p = kmem_cache_alloc(cache, GFP_KERNEL);
676 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700677 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800678 kmem_cache_destroy(cache);
679 return;
680 }
681
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800682 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700683 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700684
685 /*
686 * Properly free the object to prevent the "Objects remaining in
687 * test_cache on __kmem_cache_shutdown" BUG failure.
688 */
689 kmem_cache_free(cache, p);
690
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800691 kmem_cache_destroy(cache);
692}
693
Patricia Alfonso73228c72020-10-13 16:55:06 -0700694static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700695{
696 char *ptr;
697 size_t size = 24;
698
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800699 /*
700 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
701 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
702 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800703 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700704
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800705 if (OOB_TAG_OFF)
706 size = round_up(size, OOB_TAG_OFF);
707
Patricia Alfonso73228c72020-10-13 16:55:06 -0700708 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
709 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
710
711 KUNIT_EXPECT_KASAN_FAIL(test,
712 kasan_ptr_result = memchr(ptr, '1', size + 1));
713
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700714 kfree(ptr);
715}
716
Patricia Alfonso73228c72020-10-13 16:55:06 -0700717static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700718{
719 char *ptr;
720 size_t size = 24;
721 int arr[9];
722
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800723 /*
724 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
725 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
726 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800727 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700728
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800729 if (OOB_TAG_OFF)
730 size = round_up(size, OOB_TAG_OFF);
731
Patricia Alfonso73228c72020-10-13 16:55:06 -0700732 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
733 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700734 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700735
736 KUNIT_EXPECT_KASAN_FAIL(test,
737 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700738 kfree(ptr);
739}
740
Patricia Alfonso73228c72020-10-13 16:55:06 -0700741static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700742{
743 char *ptr;
744 size_t size = 24;
745
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800746 /*
747 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
748 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
749 */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800750 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700751
752 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
753 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700754
755 kfree(ptr);
756
757 /*
758 * Try to cause only 1 invalid access (less spam in dmesg).
759 * For that we need ptr to point to zeroed byte.
760 * Skip metadata that could be stored in freed object so ptr
761 * will likely point to zeroed byte.
762 */
763 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700764 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700765
Patricia Alfonso73228c72020-10-13 16:55:06 -0700766 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700767
Patricia Alfonso73228c72020-10-13 16:55:06 -0700768 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700769
Patricia Alfonso73228c72020-10-13 16:55:06 -0700770 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700771
Patricia Alfonso73228c72020-10-13 16:55:06 -0700772 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700773
Patricia Alfonso73228c72020-10-13 16:55:06 -0700774 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700775}
776
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800777static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700778{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800779 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
780 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
781 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
782 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
783 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
784 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
785 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
786 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
787}
788
789static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
790{
791 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
792 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
793 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
794 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
795 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
796 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
797 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
798 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
799
800#if defined(clear_bit_unlock_is_negative_byte)
801 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
802 clear_bit_unlock_is_negative_byte(nr, addr));
803#endif
804}
805
806static void kasan_bitops_generic(struct kunit *test)
807{
808 long *bits;
809
810 /* This test is specifically crafted for the generic mode. */
Andrey Konovalovda17e372021-02-24 12:05:17 -0800811 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800812
Marco Elver19a33ca2019-07-11 20:53:52 -0700813 /*
Andrey Konovalov0fd37922021-02-24 12:05:13 -0800814 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700815 * this way we do not actually corrupt other memory.
816 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800817 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700818 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700819
820 /*
821 * Below calls try to access bit within allocated memory; however, the
822 * below accesses are still out-of-bounds, since bitops are defined to
823 * operate on the whole long the bit is in.
824 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800825 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700826
827 /*
828 * Below calls try to access bit beyond allocated memory.
829 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800830 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700831
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800832 kfree(bits);
833}
Marco Elver19a33ca2019-07-11 20:53:52 -0700834
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800835static void kasan_bitops_tags(struct kunit *test)
836{
837 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700838
Andrey Konovalovda17e372021-02-24 12:05:17 -0800839 /* This test is specifically crafted for tag-based modes. */
840 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700841
Andrey Konovalove66e1792021-02-24 12:05:42 -0800842 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
843 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800844 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700845
Andrey Konovalove66e1792021-02-24 12:05:42 -0800846 /* Do the accesses past the 48 allocated bytes, but within the redone. */
847 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
848 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700849
Marco Elver19a33ca2019-07-11 20:53:52 -0700850 kfree(bits);
851}
852
Patricia Alfonso73228c72020-10-13 16:55:06 -0700853static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700854{
855 char *ptr;
856 size_t size = 16;
857
Marco Elverbb104ed2019-07-11 20:54:11 -0700858 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700859 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700860
Waiman Long453431a2020-08-06 23:18:13 -0700861 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700862 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700863}
864
Patricia Alfonso73228c72020-10-13 16:55:06 -0700865static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800866{
867 void *area;
868
Andrey Konovalovda17e372021-02-24 12:05:17 -0800869 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800870
871 /*
872 * We have to be careful not to hit the guard page.
873 * The MMU will catch that and crash us.
874 */
875 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700876 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800877
Patricia Alfonso73228c72020-10-13 16:55:06 -0700878 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800879 vfree(area);
880}
Daniel Axtens06513912019-11-30 17:54:53 -0800881
Andrey Konovalov573a4802021-02-24 12:05:21 -0800882/*
883 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
884 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
885 * modes.
886 */
887static void match_all_not_assigned(struct kunit *test)
888{
889 char *ptr;
890 struct page *pages;
891 int i, size, order;
892
893 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
894
895 for (i = 0; i < 256; i++) {
896 size = (get_random_int() % 1024) + 1;
897 ptr = kmalloc(size, GFP_KERNEL);
898 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
899 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
900 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
901 kfree(ptr);
902 }
903
904 for (i = 0; i < 256; i++) {
905 order = (get_random_int() % 4) + 1;
906 pages = alloc_pages(GFP_KERNEL, order);
907 ptr = page_address(pages);
908 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
909 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
910 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
911 free_pages((unsigned long)ptr, order);
912 }
913}
914
915/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
916static void match_all_ptr_tag(struct kunit *test)
917{
918 char *ptr;
919 u8 tag;
920
921 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
922
923 ptr = kmalloc(128, GFP_KERNEL);
924 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
925
926 /* Backup the assigned tag. */
927 tag = get_tag(ptr);
928 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
929
930 /* Reset the tag to 0xff.*/
931 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
932
933 /* This access shouldn't trigger a KASAN report. */
934 *ptr = 0;
935
936 /* Recover the pointer tag and free. */
937 ptr = set_tag(ptr, tag);
938 kfree(ptr);
939}
940
941/* Check that there are no match-all memory tags for tag-based modes. */
942static void match_all_mem_tag(struct kunit *test)
943{
944 char *ptr;
945 int tag;
946
947 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
948
949 ptr = kmalloc(128, GFP_KERNEL);
950 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
951 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
952
953 /* For each possible tag value not matching the pointer tag. */
954 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
955 if (tag == get_tag(ptr))
956 continue;
957
958 /* Mark the first memory granule with the chosen memory tag. */
959 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
960
961 /* This access must cause a KASAN report. */
962 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
963 }
964
965 /* Recover the memory tag and free. */
966 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
967 kfree(ptr);
968}
969
Patricia Alfonso73228c72020-10-13 16:55:06 -0700970static struct kunit_case kasan_kunit_test_cases[] = {
971 KUNIT_CASE(kmalloc_oob_right),
972 KUNIT_CASE(kmalloc_oob_left),
973 KUNIT_CASE(kmalloc_node_oob_right),
974 KUNIT_CASE(kmalloc_pagealloc_oob_right),
975 KUNIT_CASE(kmalloc_pagealloc_uaf),
976 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalov858bdeb2021-02-24 12:05:55 -0800977 KUNIT_CASE(pagealloc_oob_right),
978 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700979 KUNIT_CASE(kmalloc_large_oob_right),
980 KUNIT_CASE(kmalloc_oob_krealloc_more),
981 KUNIT_CASE(kmalloc_oob_krealloc_less),
982 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800983 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700984 KUNIT_CASE(kmalloc_oob_in_memset),
985 KUNIT_CASE(kmalloc_oob_memset_2),
986 KUNIT_CASE(kmalloc_oob_memset_4),
987 KUNIT_CASE(kmalloc_oob_memset_8),
988 KUNIT_CASE(kmalloc_oob_memset_16),
989 KUNIT_CASE(kmalloc_memmove_invalid_size),
990 KUNIT_CASE(kmalloc_uaf),
991 KUNIT_CASE(kmalloc_uaf_memset),
992 KUNIT_CASE(kmalloc_uaf2),
993 KUNIT_CASE(kfree_via_page),
994 KUNIT_CASE(kfree_via_phys),
995 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov11516132021-02-24 12:05:59 -0800996 KUNIT_CASE(kmem_cache_accounted),
997 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700998 KUNIT_CASE(kasan_global_oob),
999 KUNIT_CASE(kasan_stack_oob),
1000 KUNIT_CASE(kasan_alloca_oob_left),
1001 KUNIT_CASE(kasan_alloca_oob_right),
1002 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov611806b2021-02-24 12:05:50 -08001003 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001004 KUNIT_CASE(kmem_cache_double_free),
1005 KUNIT_CASE(kmem_cache_invalid_free),
1006 KUNIT_CASE(kasan_memchr),
1007 KUNIT_CASE(kasan_memcmp),
1008 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001009 KUNIT_CASE(kasan_bitops_generic),
1010 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001011 KUNIT_CASE(kmalloc_double_kzfree),
1012 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov573a4802021-02-24 12:05:21 -08001013 KUNIT_CASE(match_all_not_assigned),
1014 KUNIT_CASE(match_all_ptr_tag),
1015 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001016 {}
1017};
Walter Wu387d6e42020-08-06 23:24:42 -07001018
Patricia Alfonso73228c72020-10-13 16:55:06 -07001019static struct kunit_suite kasan_kunit_test_suite = {
1020 .name = "kasan",
1021 .init = kasan_test_init,
1022 .test_cases = kasan_kunit_test_cases,
1023 .exit = kasan_test_exit,
1024};
Walter Wu387d6e42020-08-06 23:24:42 -07001025
Patricia Alfonso73228c72020-10-13 16:55:06 -07001026kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001027
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001028MODULE_LICENSE("GPL");