blob: 566d894ba20be4db958495215600149ac1333ba1 [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
Andrey Konovalov696574e2021-02-03 15:35:05 +1100499/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700500static void ksize_unpoisons_memory(struct kunit *test)
501{
502 char *ptr;
503 size_t size = 123, real_size;
504
505 ptr = kmalloc(size, GFP_KERNEL);
506 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
507 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100508
509 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700510 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100511
512 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700513 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100514
Patricia Alfonso73228c72020-10-13 16:55:06 -0700515 kfree(ptr);
516}
517
Andrey Konovalov696574e2021-02-03 15:35:05 +1100518/*
519 * Check that a use-after-free is detected by ksize() and via normal accesses
520 * after it.
521 */
522static void ksize_uaf(struct kunit *test)
523{
524 char *ptr;
525 int size = 128 - KASAN_GRANULE_SIZE;
526
527 ptr = kmalloc(size, GFP_KERNEL);
528 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
529 kfree(ptr);
530
531 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
532 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
533 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
534}
535
Patricia Alfonso73228c72020-10-13 16:55:06 -0700536static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800537{
538 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700539 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800540 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
541
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100542 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700543
Patricia Alfonso73228c72020-10-13 16:55:06 -0700544 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700545}
546
Patricia Alfonso73228c72020-10-13 16:55:06 -0700547static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800548{
549 volatile int i = 10;
550 char alloca_array[i];
551 char *p = alloca_array - 1;
552
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800553 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100554 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
555 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700556
557 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800558}
559
Patricia Alfonso73228c72020-10-13 16:55:06 -0700560static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800561{
562 volatile int i = 10;
563 char alloca_array[i];
564 char *p = alloca_array + i;
565
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800566 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100567 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
568 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700569
570 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800571}
572
Patricia Alfonso73228c72020-10-13 16:55:06 -0700573static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800574{
575 char *p;
576 size_t size = 200;
577 struct kmem_cache *cache;
578
579 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700580 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
581
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800582 p = kmem_cache_alloc(cache, GFP_KERNEL);
583 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700584 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800585 kmem_cache_destroy(cache);
586 return;
587 }
588
589 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700590 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800591 kmem_cache_destroy(cache);
592}
593
Patricia Alfonso73228c72020-10-13 16:55:06 -0700594static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800595{
596 char *p;
597 size_t size = 200;
598 struct kmem_cache *cache;
599
600 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
601 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700602 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
603
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800604 p = kmem_cache_alloc(cache, GFP_KERNEL);
605 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700606 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800607 kmem_cache_destroy(cache);
608 return;
609 }
610
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100611 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700612 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700613
614 /*
615 * Properly free the object to prevent the "Objects remaining in
616 * test_cache on __kmem_cache_shutdown" BUG failure.
617 */
618 kmem_cache_free(cache, p);
619
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800620 kmem_cache_destroy(cache);
621}
622
Patricia Alfonso73228c72020-10-13 16:55:06 -0700623static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700624{
625 char *ptr;
626 size_t size = 24;
627
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100628 /*
629 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
630 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
631 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100632 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700633
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800634 if (OOB_TAG_OFF)
635 size = round_up(size, OOB_TAG_OFF);
636
Patricia Alfonso73228c72020-10-13 16:55:06 -0700637 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
638 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
639
640 KUNIT_EXPECT_KASAN_FAIL(test,
641 kasan_ptr_result = memchr(ptr, '1', size + 1));
642
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700643 kfree(ptr);
644}
645
Patricia Alfonso73228c72020-10-13 16:55:06 -0700646static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700647{
648 char *ptr;
649 size_t size = 24;
650 int arr[9];
651
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100652 /*
653 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
654 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
655 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100656 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700657
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800658 if (OOB_TAG_OFF)
659 size = round_up(size, OOB_TAG_OFF);
660
Patricia Alfonso73228c72020-10-13 16:55:06 -0700661 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
662 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700663 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700664
665 KUNIT_EXPECT_KASAN_FAIL(test,
666 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700667 kfree(ptr);
668}
669
Patricia Alfonso73228c72020-10-13 16:55:06 -0700670static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700671{
672 char *ptr;
673 size_t size = 24;
674
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100675 /*
676 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
677 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
678 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100679 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700680
681 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
682 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700683
684 kfree(ptr);
685
686 /*
687 * Try to cause only 1 invalid access (less spam in dmesg).
688 * For that we need ptr to point to zeroed byte.
689 * Skip metadata that could be stored in freed object so ptr
690 * will likely point to zeroed byte.
691 */
692 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700693 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700694
Patricia Alfonso73228c72020-10-13 16:55:06 -0700695 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700696
Patricia Alfonso73228c72020-10-13 16:55:06 -0700697 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700698
Patricia Alfonso73228c72020-10-13 16:55:06 -0700699 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700700
Patricia Alfonso73228c72020-10-13 16:55:06 -0700701 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700702
Patricia Alfonso73228c72020-10-13 16:55:06 -0700703 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700704}
705
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800706static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700707{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800708 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
709 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
710 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
711 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
712 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
713 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
714 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
715 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
716}
717
718static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
719{
720 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
721 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
722 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
723 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
724 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
725 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
726 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
727 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
728
729#if defined(clear_bit_unlock_is_negative_byte)
730 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
731 clear_bit_unlock_is_negative_byte(nr, addr));
732#endif
733}
734
735static void kasan_bitops_generic(struct kunit *test)
736{
737 long *bits;
738
739 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100740 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800741
Marco Elver19a33ca2019-07-11 20:53:52 -0700742 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100743 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700744 * this way we do not actually corrupt other memory.
745 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800746 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700747 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700748
749 /*
750 * Below calls try to access bit within allocated memory; however, the
751 * below accesses are still out-of-bounds, since bitops are defined to
752 * operate on the whole long the bit is in.
753 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800754 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700755
756 /*
757 * Below calls try to access bit beyond allocated memory.
758 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800759 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700760
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800761 kfree(bits);
762}
Marco Elver19a33ca2019-07-11 20:53:52 -0700763
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800764static void kasan_bitops_tags(struct kunit *test)
765{
766 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700767
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100768 /* This test is specifically crafted for tag-based modes. */
769 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700770
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100771 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
772 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800773 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700774
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100775 /* Do the accesses past the 48 allocated bytes, but within the redone. */
776 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
777 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700778
Marco Elver19a33ca2019-07-11 20:53:52 -0700779 kfree(bits);
780}
781
Patricia Alfonso73228c72020-10-13 16:55:06 -0700782static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700783{
784 char *ptr;
785 size_t size = 16;
786
Marco Elverbb104ed2019-07-11 20:54:11 -0700787 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700788 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700789
Waiman Long453431a2020-08-06 23:18:13 -0700790 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700791 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700792}
793
Patricia Alfonso73228c72020-10-13 16:55:06 -0700794static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800795{
796 void *area;
797
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100798 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800799
800 /*
801 * We have to be careful not to hit the guard page.
802 * The MMU will catch that and crash us.
803 */
804 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700805 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800806
Patricia Alfonso73228c72020-10-13 16:55:06 -0700807 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800808 vfree(area);
809}
Daniel Axtens06513912019-11-30 17:54:53 -0800810
Andrey Konovalov782ba452021-02-03 15:35:00 +1100811/*
812 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
813 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
814 * modes.
815 */
816static void match_all_not_assigned(struct kunit *test)
817{
818 char *ptr;
819 struct page *pages;
820 int i, size, order;
821
822 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
823
824 for (i = 0; i < 256; i++) {
825 size = (get_random_int() % 1024) + 1;
826 ptr = kmalloc(size, GFP_KERNEL);
827 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
828 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
829 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
830 kfree(ptr);
831 }
832
833 for (i = 0; i < 256; i++) {
834 order = (get_random_int() % 4) + 1;
835 pages = alloc_pages(GFP_KERNEL, order);
836 ptr = page_address(pages);
837 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
838 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
839 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
840 free_pages((unsigned long)ptr, order);
841 }
842}
843
844/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
845static void match_all_ptr_tag(struct kunit *test)
846{
847 char *ptr;
848 u8 tag;
849
850 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
851
852 ptr = kmalloc(128, GFP_KERNEL);
853 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
854
855 /* Backup the assigned tag. */
856 tag = get_tag(ptr);
857 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
858
859 /* Reset the tag to 0xff.*/
860 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
861
862 /* This access shouldn't trigger a KASAN report. */
863 *ptr = 0;
864
865 /* Recover the pointer tag and free. */
866 ptr = set_tag(ptr, tag);
867 kfree(ptr);
868}
869
870/* Check that there are no match-all memory tags for tag-based modes. */
871static void match_all_mem_tag(struct kunit *test)
872{
873 char *ptr;
874 int tag;
875
876 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
877
878 ptr = kmalloc(128, GFP_KERNEL);
879 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
880 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
881
882 /* For each possible tag value not matching the pointer tag. */
883 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
884 if (tag == get_tag(ptr))
885 continue;
886
887 /* Mark the first memory granule with the chosen memory tag. */
888 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
889
890 /* This access must cause a KASAN report. */
891 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
892 }
893
894 /* Recover the memory tag and free. */
895 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
896 kfree(ptr);
897}
898
Patricia Alfonso73228c72020-10-13 16:55:06 -0700899static struct kunit_case kasan_kunit_test_cases[] = {
900 KUNIT_CASE(kmalloc_oob_right),
901 KUNIT_CASE(kmalloc_oob_left),
902 KUNIT_CASE(kmalloc_node_oob_right),
903 KUNIT_CASE(kmalloc_pagealloc_oob_right),
904 KUNIT_CASE(kmalloc_pagealloc_uaf),
905 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
906 KUNIT_CASE(kmalloc_large_oob_right),
907 KUNIT_CASE(kmalloc_oob_krealloc_more),
908 KUNIT_CASE(kmalloc_oob_krealloc_less),
909 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800910 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700911 KUNIT_CASE(kmalloc_oob_in_memset),
912 KUNIT_CASE(kmalloc_oob_memset_2),
913 KUNIT_CASE(kmalloc_oob_memset_4),
914 KUNIT_CASE(kmalloc_oob_memset_8),
915 KUNIT_CASE(kmalloc_oob_memset_16),
916 KUNIT_CASE(kmalloc_memmove_invalid_size),
917 KUNIT_CASE(kmalloc_uaf),
918 KUNIT_CASE(kmalloc_uaf_memset),
919 KUNIT_CASE(kmalloc_uaf2),
920 KUNIT_CASE(kfree_via_page),
921 KUNIT_CASE(kfree_via_phys),
922 KUNIT_CASE(kmem_cache_oob),
923 KUNIT_CASE(memcg_accounted_kmem_cache),
924 KUNIT_CASE(kasan_global_oob),
925 KUNIT_CASE(kasan_stack_oob),
926 KUNIT_CASE(kasan_alloca_oob_left),
927 KUNIT_CASE(kasan_alloca_oob_right),
928 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov696574e2021-02-03 15:35:05 +1100929 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700930 KUNIT_CASE(kmem_cache_double_free),
931 KUNIT_CASE(kmem_cache_invalid_free),
932 KUNIT_CASE(kasan_memchr),
933 KUNIT_CASE(kasan_memcmp),
934 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800935 KUNIT_CASE(kasan_bitops_generic),
936 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700937 KUNIT_CASE(kmalloc_double_kzfree),
938 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +1100939 KUNIT_CASE(match_all_not_assigned),
940 KUNIT_CASE(match_all_ptr_tag),
941 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700942 {}
943};
Walter Wu387d6e42020-08-06 23:24:42 -0700944
Patricia Alfonso73228c72020-10-13 16:55:06 -0700945static struct kunit_suite kasan_kunit_test_suite = {
946 .name = "kasan",
947 .init = kasan_test_init,
948 .test_cases = kasan_kunit_test_cases,
949 .exit = kasan_test_exit,
950};
Walter Wu387d6e42020-08-06 23:24:42 -0700951
Patricia Alfonso73228c72020-10-13 16:55:06 -0700952kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -0700953
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800954MODULE_LICENSE("GPL");