blob: 0cda4a1ff394f0857fb1286050a91835938c9f6a [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{
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070050 multishot = kasan_save_enable_multi_shot();
Andrey Konovalova599a4e2021-02-03 15:35:02 +110051 hw_set_tagging_report_once(false);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070052 return 0;
53}
54
55static void kasan_test_exit(struct kunit *test)
56{
Andrey Konovalova599a4e2021-02-03 15:35:02 +110057 hw_set_tagging_report_once(true);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070058 kasan_restore_multi_shot(multishot);
59}
60
61/**
Andrey Konovalovf3e66b22021-02-03 15:34:59 +110062 * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
63 * KASAN report; causes a test failure otherwise. This relies on a KUnit
64 * resource named "kasan_data". Do not use this name for KUnit resources
65 * outside of KASAN tests.
Andrey Konovalova599a4e2021-02-03 15:35:02 +110066 *
67 * For hardware tag-based KASAN, when a tag fault happens, tag checking is
68 * normally auto-disabled. When this happens, this test handler reenables
69 * tag checking. As tag checking can be only disabled or enabled per CPU, this
70 * handler disables migration (preemption).
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110071 *
72 * Since the compiler doesn't see that the expression can change the fail_data
73 * fields, it can reorder or optimize away the accesses to those fields.
74 * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
75 * expression to prevent that.
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070076 */
Andrey Konovalova599a4e2021-02-03 15:35:02 +110077#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
78 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
79 migrate_disable(); \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110080 WRITE_ONCE(fail_data.report_expected, true); \
81 WRITE_ONCE(fail_data.report_found, false); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110082 kunit_add_named_resource(test, \
83 NULL, \
84 NULL, \
85 &resource, \
86 "kasan_data", &fail_data); \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110087 barrier(); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110088 expression; \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110089 barrier(); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110090 KUNIT_EXPECT_EQ(test, \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110091 READ_ONCE(fail_data.report_expected), \
92 READ_ONCE(fail_data.report_found)); \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110093 if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
Andrey Konovalov7095a8f2021-02-03 15:35:03 +110094 if (READ_ONCE(fail_data.report_found)) \
Andrey Konovalova599a4e2021-02-03 15:35:02 +110095 hw_enable_tagging(); \
96 migrate_enable(); \
97 } \
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -070098} while (0)
99
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100100#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
101 if (!IS_ENABLED(config)) { \
102 kunit_info((test), "skipping, " #config " required"); \
103 return; \
104 } \
105} while (0)
106
107#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
108 if (IS_ENABLED(config)) { \
109 kunit_info((test), "skipping, " #config " enabled"); \
110 return; \
111 } \
112} while (0)
113
Patricia Alfonso73228c72020-10-13 16:55:06 -0700114static void kmalloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800115{
116 char *ptr;
117 size_t size = 123;
118
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800119 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700120 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800121
Patricia Alfonso73228c72020-10-13 16:55:06 -0700122 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800123 kfree(ptr);
124}
125
Patricia Alfonso73228c72020-10-13 16:55:06 -0700126static void kmalloc_oob_left(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800127{
128 char *ptr;
129 size_t size = 15;
130
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800131 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700132 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800133
Patricia Alfonso73228c72020-10-13 16:55:06 -0700134 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800135 kfree(ptr);
136}
137
Patricia Alfonso73228c72020-10-13 16:55:06 -0700138static void kmalloc_node_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800139{
140 char *ptr;
141 size_t size = 4096;
142
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800143 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700144 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800145
Patricia Alfonso73228c72020-10-13 16:55:06 -0700146 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800147 kfree(ptr);
148}
149
Patricia Alfonso73228c72020-10-13 16:55:06 -0700150static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800151{
152 char *ptr;
153 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
154
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100155 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700156
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100157 /*
158 * Allocate a chunk that does not fit into a SLUB cache to trigger
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700159 * the page allocator fallback.
160 */
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700161 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700162 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700163
Patricia Alfonso73228c72020-10-13 16:55:06 -0700164 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700165 kfree(ptr);
166}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800167
Patricia Alfonso73228c72020-10-13 16:55:06 -0700168static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800169{
170 char *ptr;
171 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
172
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100173 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800174
Patricia Alfonso73228c72020-10-13 16:55:06 -0700175 ptr = kmalloc(size, GFP_KERNEL);
176 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
177
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800178 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700179 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800180}
181
Patricia Alfonso73228c72020-10-13 16:55:06 -0700182static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800183{
184 char *ptr;
185 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
186
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100187 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800188
Patricia Alfonso73228c72020-10-13 16:55:06 -0700189 ptr = kmalloc(size, GFP_KERNEL);
190 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700191
Patricia Alfonso73228c72020-10-13 16:55:06 -0700192 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
193}
194
195static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700196{
197 char *ptr;
198 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100199
200 /*
201 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700202 * and does not trigger the page allocator fallback in SLUB.
203 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800204 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700205 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800206
Patricia Alfonso73228c72020-10-13 16:55:06 -0700207 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800208 kfree(ptr);
209}
210
Patricia Alfonso73228c72020-10-13 16:55:06 -0700211static void kmalloc_oob_krealloc_more(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800212{
213 char *ptr1, *ptr2;
214 size_t size1 = 17;
215 size_t size2 = 19;
216
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800217 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700218 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
219
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800220 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700221 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800222
Patricia Alfonso73228c72020-10-13 16:55:06 -0700223 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800224 kfree(ptr2);
225}
226
Patricia Alfonso73228c72020-10-13 16:55:06 -0700227static void kmalloc_oob_krealloc_less(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800228{
229 char *ptr1, *ptr2;
230 size_t size1 = 17;
231 size_t size2 = 15;
232
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800233 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700234 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
235
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800236 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700237 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700238
Patricia Alfonso73228c72020-10-13 16:55:06 -0700239 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800240 kfree(ptr2);
241}
242
Patricia Alfonso73228c72020-10-13 16:55:06 -0700243static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800244{
245 struct {
246 u64 words[2];
247 } *ptr1, *ptr2;
248
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800249 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100250 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800251
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800252 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700253 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
254
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800255 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700256 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
257
258 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800259 kfree(ptr1);
260 kfree(ptr2);
261}
262
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800263static void kmalloc_uaf_16(struct kunit *test)
264{
265 struct {
266 u64 words[2];
267 } *ptr1, *ptr2;
268
269 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
270 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
271
272 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
273 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
274 kfree(ptr2);
275
276 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
277 kfree(ptr1);
278}
279
Patricia Alfonso73228c72020-10-13 16:55:06 -0700280static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800281{
282 char *ptr;
283 size_t size = 8;
284
Wang Longf523e732015-11-05 18:51:15 -0800285 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700286 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800287
Patricia Alfonso73228c72020-10-13 16:55:06 -0700288 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800289 kfree(ptr);
290}
291
Patricia Alfonso73228c72020-10-13 16:55:06 -0700292static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800293{
294 char *ptr;
295 size_t size = 8;
296
Wang Longf523e732015-11-05 18:51:15 -0800297 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700298 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800299
Patricia Alfonso73228c72020-10-13 16:55:06 -0700300 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800301 kfree(ptr);
302}
303
304
Patricia Alfonso73228c72020-10-13 16:55:06 -0700305static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800306{
307 char *ptr;
308 size_t size = 8;
309
Wang Longf523e732015-11-05 18:51:15 -0800310 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700311 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800312
Patricia Alfonso73228c72020-10-13 16:55:06 -0700313 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800314 kfree(ptr);
315}
316
Patricia Alfonso73228c72020-10-13 16:55:06 -0700317static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800318{
319 char *ptr;
320 size_t size = 16;
321
Wang Longf523e732015-11-05 18:51:15 -0800322 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700323 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800324
Patricia Alfonso73228c72020-10-13 16:55:06 -0700325 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800326 kfree(ptr);
327}
328
Patricia Alfonso73228c72020-10-13 16:55:06 -0700329static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800330{
331 char *ptr;
332 size_t size = 666;
333
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800334 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700335 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800336
Patricia Alfonso73228c72020-10-13 16:55:06 -0700337 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800338 kfree(ptr);
339}
340
Patricia Alfonso73228c72020-10-13 16:55:06 -0700341static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700342{
343 char *ptr;
344 size_t size = 64;
345 volatile size_t invalid_size = -2;
346
Walter Wu98f3b562020-04-01 21:09:40 -0700347 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700348 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700349
350 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700351
352 KUNIT_EXPECT_KASAN_FAIL(test,
353 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700354 kfree(ptr);
355}
356
Patricia Alfonso73228c72020-10-13 16:55:06 -0700357static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800358{
359 char *ptr;
360 size_t size = 10;
361
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800362 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700363 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800364
365 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700366 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800367}
368
Patricia Alfonso73228c72020-10-13 16:55:06 -0700369static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800370{
371 char *ptr;
372 size_t size = 33;
373
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800374 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700375 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800376
377 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700378 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800379}
380
Patricia Alfonso73228c72020-10-13 16:55:06 -0700381static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800382{
383 char *ptr1, *ptr2;
384 size_t size = 43;
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100385 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800386
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100387again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800388 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700389 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800390
391 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800392
Patricia Alfonso73228c72020-10-13 16:55:06 -0700393 ptr2 = kmalloc(size, GFP_KERNEL);
394 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
395
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100396 /*
397 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
398 * Allow up to 16 attempts at generating different tags.
399 */
400 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
401 kfree(ptr2);
402 goto again;
403 }
404
Patricia Alfonso73228c72020-10-13 16:55:06 -0700405 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
406 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
407
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800408 kfree(ptr2);
409}
410
Patricia Alfonso73228c72020-10-13 16:55:06 -0700411static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700412{
413 char *ptr;
414 size_t size = 8;
415 struct page *page;
416 unsigned long offset;
417
Mark Rutlandb92a9532019-09-23 15:34:16 -0700418 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700419 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700420
421 page = virt_to_page(ptr);
422 offset = offset_in_page(ptr);
423 kfree(page_address(page) + offset);
424}
425
Patricia Alfonso73228c72020-10-13 16:55:06 -0700426static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700427{
428 char *ptr;
429 size_t size = 8;
430 phys_addr_t phys;
431
Mark Rutlandb92a9532019-09-23 15:34:16 -0700432 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700434
435 phys = virt_to_phys(ptr);
436 kfree(phys_to_virt(phys));
437}
438
Patricia Alfonso73228c72020-10-13 16:55:06 -0700439static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800440{
441 char *p;
442 size_t size = 200;
443 struct kmem_cache *cache = kmem_cache_create("test_cache",
444 size, 0,
445 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700446 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800447 p = kmem_cache_alloc(cache, GFP_KERNEL);
448 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700449 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800450 kmem_cache_destroy(cache);
451 return;
452 }
453
Patricia Alfonso73228c72020-10-13 16:55:06 -0700454 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800455 kmem_cache_free(cache, p);
456 kmem_cache_destroy(cache);
457}
458
Patricia Alfonso73228c72020-10-13 16:55:06 -0700459static void memcg_accounted_kmem_cache(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800460{
461 int i;
462 char *p;
463 size_t size = 200;
464 struct kmem_cache *cache;
465
466 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700467 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800468
Greg Thelen0386bf32017-02-24 15:00:08 -0800469 /*
470 * Several allocations with a delay to allow for lazy per memcg kmem
471 * cache creation.
472 */
473 for (i = 0; i < 5; i++) {
474 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800475 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800476 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800477
Greg Thelen0386bf32017-02-24 15:00:08 -0800478 kmem_cache_free(cache, p);
479 msleep(100);
480 }
481
482free_cache:
483 kmem_cache_destroy(cache);
484}
485
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800486static char global_array[10];
487
Patricia Alfonso73228c72020-10-13 16:55:06 -0700488static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800489{
490 volatile int i = 3;
491 char *p = &global_array[ARRAY_SIZE(global_array) + i];
492
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800493 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100494 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800495
Patricia Alfonso73228c72020-10-13 16:55:06 -0700496 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800497}
498
Patricia Alfonso73228c72020-10-13 16:55:06 -0700499static void ksize_unpoisons_memory(struct kunit *test)
500{
501 char *ptr;
502 size_t size = 123, real_size;
503
504 ptr = kmalloc(size, GFP_KERNEL);
505 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
506 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100507
508 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700509 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100510
511 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700512 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100513
Patricia Alfonso73228c72020-10-13 16:55:06 -0700514 kfree(ptr);
515}
516
517static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800518{
519 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700520 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800521 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
522
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100523 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700524
Patricia Alfonso73228c72020-10-13 16:55:06 -0700525 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700526}
527
Patricia Alfonso73228c72020-10-13 16:55:06 -0700528static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800529{
530 volatile int i = 10;
531 char alloca_array[i];
532 char *p = alloca_array - 1;
533
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800534 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100535 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
536 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700537
538 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800539}
540
Patricia Alfonso73228c72020-10-13 16:55:06 -0700541static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800542{
543 volatile int i = 10;
544 char alloca_array[i];
545 char *p = alloca_array + i;
546
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800547 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100548 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
549 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700550
551 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800552}
553
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800555{
556 char *p;
557 size_t size = 200;
558 struct kmem_cache *cache;
559
560 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700561 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
562
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800563 p = kmem_cache_alloc(cache, GFP_KERNEL);
564 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700565 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800566 kmem_cache_destroy(cache);
567 return;
568 }
569
570 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700571 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800572 kmem_cache_destroy(cache);
573}
574
Patricia Alfonso73228c72020-10-13 16:55:06 -0700575static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800576{
577 char *p;
578 size_t size = 200;
579 struct kmem_cache *cache;
580
581 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
582 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700583 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
584
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800585 p = kmem_cache_alloc(cache, GFP_KERNEL);
586 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700587 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800588 kmem_cache_destroy(cache);
589 return;
590 }
591
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100592 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700593 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700594
595 /*
596 * Properly free the object to prevent the "Objects remaining in
597 * test_cache on __kmem_cache_shutdown" BUG failure.
598 */
599 kmem_cache_free(cache, p);
600
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800601 kmem_cache_destroy(cache);
602}
603
Patricia Alfonso73228c72020-10-13 16:55:06 -0700604static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700605{
606 char *ptr;
607 size_t size = 24;
608
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100609 /*
610 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
611 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
612 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100613 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700614
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800615 if (OOB_TAG_OFF)
616 size = round_up(size, OOB_TAG_OFF);
617
Patricia Alfonso73228c72020-10-13 16:55:06 -0700618 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
619 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
620
621 KUNIT_EXPECT_KASAN_FAIL(test,
622 kasan_ptr_result = memchr(ptr, '1', size + 1));
623
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700624 kfree(ptr);
625}
626
Patricia Alfonso73228c72020-10-13 16:55:06 -0700627static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700628{
629 char *ptr;
630 size_t size = 24;
631 int arr[9];
632
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100633 /*
634 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
635 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
636 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100637 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700638
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800639 if (OOB_TAG_OFF)
640 size = round_up(size, OOB_TAG_OFF);
641
Patricia Alfonso73228c72020-10-13 16:55:06 -0700642 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
643 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700644 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700645
646 KUNIT_EXPECT_KASAN_FAIL(test,
647 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700648 kfree(ptr);
649}
650
Patricia Alfonso73228c72020-10-13 16:55:06 -0700651static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700652{
653 char *ptr;
654 size_t size = 24;
655
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100656 /*
657 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
658 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
659 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100660 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700661
662 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
663 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700664
665 kfree(ptr);
666
667 /*
668 * Try to cause only 1 invalid access (less spam in dmesg).
669 * For that we need ptr to point to zeroed byte.
670 * Skip metadata that could be stored in freed object so ptr
671 * will likely point to zeroed byte.
672 */
673 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700674 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700675
Patricia Alfonso73228c72020-10-13 16:55:06 -0700676 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700677
Patricia Alfonso73228c72020-10-13 16:55:06 -0700678 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700679
Patricia Alfonso73228c72020-10-13 16:55:06 -0700680 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700681
Patricia Alfonso73228c72020-10-13 16:55:06 -0700682 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700683
Patricia Alfonso73228c72020-10-13 16:55:06 -0700684 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700685}
686
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800687static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700688{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800689 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
690 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
691 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
692 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
693 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
694 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
695 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
696 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
697}
698
699static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
700{
701 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
702 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
703 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
704 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
705 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
706 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
707 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
708 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
709
710#if defined(clear_bit_unlock_is_negative_byte)
711 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
712 clear_bit_unlock_is_negative_byte(nr, addr));
713#endif
714}
715
716static void kasan_bitops_generic(struct kunit *test)
717{
718 long *bits;
719
720 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100721 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800722
Marco Elver19a33ca2019-07-11 20:53:52 -0700723 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100724 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700725 * this way we do not actually corrupt other memory.
726 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800727 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700728 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700729
730 /*
731 * Below calls try to access bit within allocated memory; however, the
732 * below accesses are still out-of-bounds, since bitops are defined to
733 * operate on the whole long the bit is in.
734 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800735 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700736
737 /*
738 * Below calls try to access bit beyond allocated memory.
739 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800740 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700741
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800742 kfree(bits);
743}
Marco Elver19a33ca2019-07-11 20:53:52 -0700744
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800745static void kasan_bitops_tags(struct kunit *test)
746{
747 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700748
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100749 /* This test is specifically crafted for tag-based modes. */
750 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700751
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800752 /* Allocation size will be rounded to up granule size, which is 16. */
753 bits = kzalloc(sizeof(*bits), GFP_KERNEL);
754 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700755
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800756 /* Do the accesses past the 16 allocated bytes. */
757 kasan_bitops_modify(test, BITS_PER_LONG, &bits[1]);
758 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, &bits[1]);
Marco Elver19a33ca2019-07-11 20:53:52 -0700759
Marco Elver19a33ca2019-07-11 20:53:52 -0700760 kfree(bits);
761}
762
Patricia Alfonso73228c72020-10-13 16:55:06 -0700763static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700764{
765 char *ptr;
766 size_t size = 16;
767
Marco Elverbb104ed2019-07-11 20:54:11 -0700768 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700769 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700770
Waiman Long453431a2020-08-06 23:18:13 -0700771 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700772 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700773}
774
Patricia Alfonso73228c72020-10-13 16:55:06 -0700775static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800776{
777 void *area;
778
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100779 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800780
781 /*
782 * We have to be careful not to hit the guard page.
783 * The MMU will catch that and crash us.
784 */
785 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700786 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800787
Patricia Alfonso73228c72020-10-13 16:55:06 -0700788 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800789 vfree(area);
790}
Daniel Axtens06513912019-11-30 17:54:53 -0800791
Andrey Konovalov782ba452021-02-03 15:35:00 +1100792/*
793 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
794 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
795 * modes.
796 */
797static void match_all_not_assigned(struct kunit *test)
798{
799 char *ptr;
800 struct page *pages;
801 int i, size, order;
802
803 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
804
805 for (i = 0; i < 256; i++) {
806 size = (get_random_int() % 1024) + 1;
807 ptr = kmalloc(size, GFP_KERNEL);
808 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
809 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
810 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
811 kfree(ptr);
812 }
813
814 for (i = 0; i < 256; i++) {
815 order = (get_random_int() % 4) + 1;
816 pages = alloc_pages(GFP_KERNEL, order);
817 ptr = page_address(pages);
818 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
819 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
820 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
821 free_pages((unsigned long)ptr, order);
822 }
823}
824
825/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
826static void match_all_ptr_tag(struct kunit *test)
827{
828 char *ptr;
829 u8 tag;
830
831 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
832
833 ptr = kmalloc(128, GFP_KERNEL);
834 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
835
836 /* Backup the assigned tag. */
837 tag = get_tag(ptr);
838 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
839
840 /* Reset the tag to 0xff.*/
841 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
842
843 /* This access shouldn't trigger a KASAN report. */
844 *ptr = 0;
845
846 /* Recover the pointer tag and free. */
847 ptr = set_tag(ptr, tag);
848 kfree(ptr);
849}
850
851/* Check that there are no match-all memory tags for tag-based modes. */
852static void match_all_mem_tag(struct kunit *test)
853{
854 char *ptr;
855 int tag;
856
857 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
858
859 ptr = kmalloc(128, GFP_KERNEL);
860 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
861 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
862
863 /* For each possible tag value not matching the pointer tag. */
864 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
865 if (tag == get_tag(ptr))
866 continue;
867
868 /* Mark the first memory granule with the chosen memory tag. */
869 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
870
871 /* This access must cause a KASAN report. */
872 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
873 }
874
875 /* Recover the memory tag and free. */
876 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
877 kfree(ptr);
878}
879
Patricia Alfonso73228c72020-10-13 16:55:06 -0700880static struct kunit_case kasan_kunit_test_cases[] = {
881 KUNIT_CASE(kmalloc_oob_right),
882 KUNIT_CASE(kmalloc_oob_left),
883 KUNIT_CASE(kmalloc_node_oob_right),
884 KUNIT_CASE(kmalloc_pagealloc_oob_right),
885 KUNIT_CASE(kmalloc_pagealloc_uaf),
886 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
887 KUNIT_CASE(kmalloc_large_oob_right),
888 KUNIT_CASE(kmalloc_oob_krealloc_more),
889 KUNIT_CASE(kmalloc_oob_krealloc_less),
890 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800891 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700892 KUNIT_CASE(kmalloc_oob_in_memset),
893 KUNIT_CASE(kmalloc_oob_memset_2),
894 KUNIT_CASE(kmalloc_oob_memset_4),
895 KUNIT_CASE(kmalloc_oob_memset_8),
896 KUNIT_CASE(kmalloc_oob_memset_16),
897 KUNIT_CASE(kmalloc_memmove_invalid_size),
898 KUNIT_CASE(kmalloc_uaf),
899 KUNIT_CASE(kmalloc_uaf_memset),
900 KUNIT_CASE(kmalloc_uaf2),
901 KUNIT_CASE(kfree_via_page),
902 KUNIT_CASE(kfree_via_phys),
903 KUNIT_CASE(kmem_cache_oob),
904 KUNIT_CASE(memcg_accounted_kmem_cache),
905 KUNIT_CASE(kasan_global_oob),
906 KUNIT_CASE(kasan_stack_oob),
907 KUNIT_CASE(kasan_alloca_oob_left),
908 KUNIT_CASE(kasan_alloca_oob_right),
909 KUNIT_CASE(ksize_unpoisons_memory),
910 KUNIT_CASE(kmem_cache_double_free),
911 KUNIT_CASE(kmem_cache_invalid_free),
912 KUNIT_CASE(kasan_memchr),
913 KUNIT_CASE(kasan_memcmp),
914 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800915 KUNIT_CASE(kasan_bitops_generic),
916 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700917 KUNIT_CASE(kmalloc_double_kzfree),
918 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +1100919 KUNIT_CASE(match_all_not_assigned),
920 KUNIT_CASE(match_all_ptr_tag),
921 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700922 {}
923};
Walter Wu387d6e42020-08-06 23:24:42 -0700924
Patricia Alfonso73228c72020-10-13 16:55:06 -0700925static struct kunit_suite kasan_kunit_test_suite = {
926 .name = "kasan",
927 .init = kasan_test_init,
928 .test_cases = kasan_kunit_test_cases,
929 .exit = kasan_test_exit,
930};
Walter Wu387d6e42020-08-06 23:24:42 -0700931
Patricia Alfonso73228c72020-10-13 16:55:06 -0700932kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -0700933
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800934MODULE_LICENSE("GPL");