blob: 2419e36e117b5d3af80e57f5a682e8a865bb1d6a [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;
385
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800386 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700387 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800388
389 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800390
Patricia Alfonso73228c72020-10-13 16:55:06 -0700391 ptr2 = kmalloc(size, GFP_KERNEL);
392 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
393
394 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
395 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
396
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800397 kfree(ptr2);
398}
399
Patricia Alfonso73228c72020-10-13 16:55:06 -0700400static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700401{
402 char *ptr;
403 size_t size = 8;
404 struct page *page;
405 unsigned long offset;
406
Mark Rutlandb92a9532019-09-23 15:34:16 -0700407 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700408 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700409
410 page = virt_to_page(ptr);
411 offset = offset_in_page(ptr);
412 kfree(page_address(page) + offset);
413}
414
Patricia Alfonso73228c72020-10-13 16:55:06 -0700415static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700416{
417 char *ptr;
418 size_t size = 8;
419 phys_addr_t phys;
420
Mark Rutlandb92a9532019-09-23 15:34:16 -0700421 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700422 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700423
424 phys = virt_to_phys(ptr);
425 kfree(phys_to_virt(phys));
426}
427
Patricia Alfonso73228c72020-10-13 16:55:06 -0700428static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800429{
430 char *p;
431 size_t size = 200;
432 struct kmem_cache *cache = kmem_cache_create("test_cache",
433 size, 0,
434 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700435 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800436 p = kmem_cache_alloc(cache, GFP_KERNEL);
437 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700438 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800439 kmem_cache_destroy(cache);
440 return;
441 }
442
Patricia Alfonso73228c72020-10-13 16:55:06 -0700443 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800444 kmem_cache_free(cache, p);
445 kmem_cache_destroy(cache);
446}
447
Patricia Alfonso73228c72020-10-13 16:55:06 -0700448static void memcg_accounted_kmem_cache(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800449{
450 int i;
451 char *p;
452 size_t size = 200;
453 struct kmem_cache *cache;
454
455 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700456 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800457
Greg Thelen0386bf32017-02-24 15:00:08 -0800458 /*
459 * Several allocations with a delay to allow for lazy per memcg kmem
460 * cache creation.
461 */
462 for (i = 0; i < 5; i++) {
463 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800464 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800465 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800466
Greg Thelen0386bf32017-02-24 15:00:08 -0800467 kmem_cache_free(cache, p);
468 msleep(100);
469 }
470
471free_cache:
472 kmem_cache_destroy(cache);
473}
474
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800475static char global_array[10];
476
Patricia Alfonso73228c72020-10-13 16:55:06 -0700477static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800478{
479 volatile int i = 3;
480 char *p = &global_array[ARRAY_SIZE(global_array) + i];
481
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800482 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100483 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800484
Patricia Alfonso73228c72020-10-13 16:55:06 -0700485 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800486}
487
Patricia Alfonso73228c72020-10-13 16:55:06 -0700488static void ksize_unpoisons_memory(struct kunit *test)
489{
490 char *ptr;
491 size_t size = 123, real_size;
492
493 ptr = kmalloc(size, GFP_KERNEL);
494 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
495 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100496
497 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700498 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100499
500 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700501 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100502
Patricia Alfonso73228c72020-10-13 16:55:06 -0700503 kfree(ptr);
504}
505
506static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800507{
508 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700509 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800510 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
511
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100512 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700513
Patricia Alfonso73228c72020-10-13 16:55:06 -0700514 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700515}
516
Patricia Alfonso73228c72020-10-13 16:55:06 -0700517static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800518{
519 volatile int i = 10;
520 char alloca_array[i];
521 char *p = alloca_array - 1;
522
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800523 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100524 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
525 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700526
527 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800528}
529
Patricia Alfonso73228c72020-10-13 16:55:06 -0700530static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800531{
532 volatile int i = 10;
533 char alloca_array[i];
534 char *p = alloca_array + i;
535
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800536 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100537 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
538 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700539
540 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800541}
542
Patricia Alfonso73228c72020-10-13 16:55:06 -0700543static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800544{
545 char *p;
546 size_t size = 200;
547 struct kmem_cache *cache;
548
549 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700550 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
551
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800552 p = kmem_cache_alloc(cache, GFP_KERNEL);
553 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800555 kmem_cache_destroy(cache);
556 return;
557 }
558
559 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700560 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800561 kmem_cache_destroy(cache);
562}
563
Patricia Alfonso73228c72020-10-13 16:55:06 -0700564static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800565{
566 char *p;
567 size_t size = 200;
568 struct kmem_cache *cache;
569
570 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
571 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700572 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
573
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800574 p = kmem_cache_alloc(cache, GFP_KERNEL);
575 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700576 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800577 kmem_cache_destroy(cache);
578 return;
579 }
580
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100581 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700582 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700583
584 /*
585 * Properly free the object to prevent the "Objects remaining in
586 * test_cache on __kmem_cache_shutdown" BUG failure.
587 */
588 kmem_cache_free(cache, p);
589
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800590 kmem_cache_destroy(cache);
591}
592
Patricia Alfonso73228c72020-10-13 16:55:06 -0700593static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700594{
595 char *ptr;
596 size_t size = 24;
597
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100598 /*
599 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
600 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
601 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100602 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700603
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800604 if (OOB_TAG_OFF)
605 size = round_up(size, OOB_TAG_OFF);
606
Patricia Alfonso73228c72020-10-13 16:55:06 -0700607 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
608 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
609
610 KUNIT_EXPECT_KASAN_FAIL(test,
611 kasan_ptr_result = memchr(ptr, '1', size + 1));
612
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700613 kfree(ptr);
614}
615
Patricia Alfonso73228c72020-10-13 16:55:06 -0700616static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700617{
618 char *ptr;
619 size_t size = 24;
620 int arr[9];
621
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100622 /*
623 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
624 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
625 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100626 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700627
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800628 if (OOB_TAG_OFF)
629 size = round_up(size, OOB_TAG_OFF);
630
Patricia Alfonso73228c72020-10-13 16:55:06 -0700631 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
632 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700633 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700634
635 KUNIT_EXPECT_KASAN_FAIL(test,
636 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700637 kfree(ptr);
638}
639
Patricia Alfonso73228c72020-10-13 16:55:06 -0700640static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700641{
642 char *ptr;
643 size_t size = 24;
644
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100645 /*
646 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
647 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
648 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100649 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700650
651 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
652 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700653
654 kfree(ptr);
655
656 /*
657 * Try to cause only 1 invalid access (less spam in dmesg).
658 * For that we need ptr to point to zeroed byte.
659 * Skip metadata that could be stored in freed object so ptr
660 * will likely point to zeroed byte.
661 */
662 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700663 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700664
Patricia Alfonso73228c72020-10-13 16:55:06 -0700665 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700666
Patricia Alfonso73228c72020-10-13 16:55:06 -0700667 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700668
Patricia Alfonso73228c72020-10-13 16:55:06 -0700669 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700670
Patricia Alfonso73228c72020-10-13 16:55:06 -0700671 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700672
Patricia Alfonso73228c72020-10-13 16:55:06 -0700673 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700674}
675
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800676static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700677{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800678 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
679 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
680 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
681 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
682 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
683 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
684 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
685 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
686}
687
688static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
689{
690 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
691 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
692 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
693 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
694 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
695 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
696 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
697 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
698
699#if defined(clear_bit_unlock_is_negative_byte)
700 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
701 clear_bit_unlock_is_negative_byte(nr, addr));
702#endif
703}
704
705static void kasan_bitops_generic(struct kunit *test)
706{
707 long *bits;
708
709 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100710 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800711
Marco Elver19a33ca2019-07-11 20:53:52 -0700712 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100713 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700714 * this way we do not actually corrupt other memory.
715 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800716 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700717 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700718
719 /*
720 * Below calls try to access bit within allocated memory; however, the
721 * below accesses are still out-of-bounds, since bitops are defined to
722 * operate on the whole long the bit is in.
723 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800724 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700725
726 /*
727 * Below calls try to access bit beyond allocated memory.
728 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800729 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700730
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800731 kfree(bits);
732}
Marco Elver19a33ca2019-07-11 20:53:52 -0700733
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800734static void kasan_bitops_tags(struct kunit *test)
735{
736 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700737
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100738 /* This test is specifically crafted for tag-based modes. */
739 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700740
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800741 /* Allocation size will be rounded to up granule size, which is 16. */
742 bits = kzalloc(sizeof(*bits), GFP_KERNEL);
743 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700744
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800745 /* Do the accesses past the 16 allocated bytes. */
746 kasan_bitops_modify(test, BITS_PER_LONG, &bits[1]);
747 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, &bits[1]);
Marco Elver19a33ca2019-07-11 20:53:52 -0700748
Marco Elver19a33ca2019-07-11 20:53:52 -0700749 kfree(bits);
750}
751
Patricia Alfonso73228c72020-10-13 16:55:06 -0700752static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700753{
754 char *ptr;
755 size_t size = 16;
756
Marco Elverbb104ed2019-07-11 20:54:11 -0700757 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700758 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700759
Waiman Long453431a2020-08-06 23:18:13 -0700760 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700761 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700762}
763
Patricia Alfonso73228c72020-10-13 16:55:06 -0700764static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800765{
766 void *area;
767
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100768 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800769
770 /*
771 * We have to be careful not to hit the guard page.
772 * The MMU will catch that and crash us.
773 */
774 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700775 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800776
Patricia Alfonso73228c72020-10-13 16:55:06 -0700777 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800778 vfree(area);
779}
Daniel Axtens06513912019-11-30 17:54:53 -0800780
Andrey Konovalov782ba452021-02-03 15:35:00 +1100781/*
782 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
783 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
784 * modes.
785 */
786static void match_all_not_assigned(struct kunit *test)
787{
788 char *ptr;
789 struct page *pages;
790 int i, size, order;
791
792 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
793
794 for (i = 0; i < 256; i++) {
795 size = (get_random_int() % 1024) + 1;
796 ptr = kmalloc(size, GFP_KERNEL);
797 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
798 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
799 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
800 kfree(ptr);
801 }
802
803 for (i = 0; i < 256; i++) {
804 order = (get_random_int() % 4) + 1;
805 pages = alloc_pages(GFP_KERNEL, order);
806 ptr = page_address(pages);
807 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
808 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
809 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
810 free_pages((unsigned long)ptr, order);
811 }
812}
813
814/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
815static void match_all_ptr_tag(struct kunit *test)
816{
817 char *ptr;
818 u8 tag;
819
820 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
821
822 ptr = kmalloc(128, GFP_KERNEL);
823 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
824
825 /* Backup the assigned tag. */
826 tag = get_tag(ptr);
827 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
828
829 /* Reset the tag to 0xff.*/
830 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
831
832 /* This access shouldn't trigger a KASAN report. */
833 *ptr = 0;
834
835 /* Recover the pointer tag and free. */
836 ptr = set_tag(ptr, tag);
837 kfree(ptr);
838}
839
840/* Check that there are no match-all memory tags for tag-based modes. */
841static void match_all_mem_tag(struct kunit *test)
842{
843 char *ptr;
844 int tag;
845
846 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
847
848 ptr = kmalloc(128, GFP_KERNEL);
849 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
850 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
851
852 /* For each possible tag value not matching the pointer tag. */
853 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
854 if (tag == get_tag(ptr))
855 continue;
856
857 /* Mark the first memory granule with the chosen memory tag. */
858 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
859
860 /* This access must cause a KASAN report. */
861 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
862 }
863
864 /* Recover the memory tag and free. */
865 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
866 kfree(ptr);
867}
868
Patricia Alfonso73228c72020-10-13 16:55:06 -0700869static struct kunit_case kasan_kunit_test_cases[] = {
870 KUNIT_CASE(kmalloc_oob_right),
871 KUNIT_CASE(kmalloc_oob_left),
872 KUNIT_CASE(kmalloc_node_oob_right),
873 KUNIT_CASE(kmalloc_pagealloc_oob_right),
874 KUNIT_CASE(kmalloc_pagealloc_uaf),
875 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
876 KUNIT_CASE(kmalloc_large_oob_right),
877 KUNIT_CASE(kmalloc_oob_krealloc_more),
878 KUNIT_CASE(kmalloc_oob_krealloc_less),
879 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800880 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700881 KUNIT_CASE(kmalloc_oob_in_memset),
882 KUNIT_CASE(kmalloc_oob_memset_2),
883 KUNIT_CASE(kmalloc_oob_memset_4),
884 KUNIT_CASE(kmalloc_oob_memset_8),
885 KUNIT_CASE(kmalloc_oob_memset_16),
886 KUNIT_CASE(kmalloc_memmove_invalid_size),
887 KUNIT_CASE(kmalloc_uaf),
888 KUNIT_CASE(kmalloc_uaf_memset),
889 KUNIT_CASE(kmalloc_uaf2),
890 KUNIT_CASE(kfree_via_page),
891 KUNIT_CASE(kfree_via_phys),
892 KUNIT_CASE(kmem_cache_oob),
893 KUNIT_CASE(memcg_accounted_kmem_cache),
894 KUNIT_CASE(kasan_global_oob),
895 KUNIT_CASE(kasan_stack_oob),
896 KUNIT_CASE(kasan_alloca_oob_left),
897 KUNIT_CASE(kasan_alloca_oob_right),
898 KUNIT_CASE(ksize_unpoisons_memory),
899 KUNIT_CASE(kmem_cache_double_free),
900 KUNIT_CASE(kmem_cache_invalid_free),
901 KUNIT_CASE(kasan_memchr),
902 KUNIT_CASE(kasan_memcmp),
903 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800904 KUNIT_CASE(kasan_bitops_generic),
905 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700906 KUNIT_CASE(kmalloc_double_kzfree),
907 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +1100908 KUNIT_CASE(match_all_not_assigned),
909 KUNIT_CASE(match_all_ptr_tag),
910 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700911 {}
912};
Walter Wu387d6e42020-08-06 23:24:42 -0700913
Patricia Alfonso73228c72020-10-13 16:55:06 -0700914static struct kunit_suite kasan_kunit_test_suite = {
915 .name = "kasan",
916 .init = kasan_test_init,
917 .test_cases = kasan_kunit_test_cases,
918 .exit = kasan_test_exit,
919};
Walter Wu387d6e42020-08-06 23:24:42 -0700920
Patricia Alfonso73228c72020-10-13 16:55:06 -0700921kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -0700922
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800923MODULE_LICENSE("GPL");