blob: ab22a653762e3fe71674e9f856809070ca39dc0e [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
Andrey Konovalove449e272021-02-03 15:35:06 +1100150/*
151 * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
152 * fit into a slab cache and therefore is allocated via the page allocator
153 * fallback. Since this kind of fallback is only implemented for SLUB, these
154 * tests are limited to that allocator.
155 */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700156static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800157{
158 char *ptr;
159 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
160
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100161 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700162
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700163 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700164 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700165
Patricia Alfonso73228c72020-10-13 16:55:06 -0700166 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrey Konovalove449e272021-02-03 15:35:06 +1100167
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700168 kfree(ptr);
169}
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800170
Patricia Alfonso73228c72020-10-13 16:55:06 -0700171static void kmalloc_pagealloc_uaf(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800172{
173 char *ptr;
174 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
175
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100176 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800177
Patricia Alfonso73228c72020-10-13 16:55:06 -0700178 ptr = kmalloc(size, GFP_KERNEL);
179 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800180 kfree(ptr);
Andrey Konovalove449e272021-02-03 15:35:06 +1100181
Patricia Alfonso73228c72020-10-13 16:55:06 -0700182 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800183}
184
Patricia Alfonso73228c72020-10-13 16:55:06 -0700185static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800186{
187 char *ptr;
188 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
189
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100190 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800191
Patricia Alfonso73228c72020-10-13 16:55:06 -0700192 ptr = kmalloc(size, GFP_KERNEL);
193 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700194
Patricia Alfonso73228c72020-10-13 16:55:06 -0700195 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
196}
197
Andrey Konovalove449e272021-02-03 15:35:06 +1100198static void pagealloc_oob_right(struct kunit *test)
199{
200 char *ptr;
201 struct page *pages;
202 size_t order = 4;
203 size_t size = (1UL << (PAGE_SHIFT + order));
204
205 /*
206 * With generic KASAN page allocations have no redzones, thus
207 * out-of-bounds detection is not guaranteed.
208 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
209 */
210 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
211
212 pages = alloc_pages(GFP_KERNEL, order);
213 ptr = page_address(pages);
214 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
215
216 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
217 free_pages((unsigned long)ptr, order);
218}
219
220static void pagealloc_uaf(struct kunit *test)
221{
222 char *ptr;
223 struct page *pages;
224 size_t order = 4;
225
226 pages = alloc_pages(GFP_KERNEL, order);
227 ptr = page_address(pages);
228 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
229 free_pages((unsigned long)ptr, order);
230
231 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
232}
233
Patricia Alfonso73228c72020-10-13 16:55:06 -0700234static void kmalloc_large_oob_right(struct kunit *test)
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700235{
236 char *ptr;
237 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100238
239 /*
240 * Allocate a chunk that is large enough, but still fits into a slab
Alexander Potapenkoe6e83792016-03-25 14:21:56 -0700241 * and does not trigger the page allocator fallback in SLUB.
242 */
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800243 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700244 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800245
Patricia Alfonso73228c72020-10-13 16:55:06 -0700246 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800247 kfree(ptr);
248}
249
Patricia Alfonso73228c72020-10-13 16:55:06 -0700250static void kmalloc_oob_krealloc_more(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800251{
252 char *ptr1, *ptr2;
253 size_t size1 = 17;
254 size_t size2 = 19;
255
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800256 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700257 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
258
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800259 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700260 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800261
Patricia Alfonso73228c72020-10-13 16:55:06 -0700262 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800263 kfree(ptr2);
264}
265
Patricia Alfonso73228c72020-10-13 16:55:06 -0700266static void kmalloc_oob_krealloc_less(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800267{
268 char *ptr1, *ptr2;
269 size_t size1 = 17;
270 size_t size2 = 15;
271
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800272 ptr1 = kmalloc(size1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700273 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
274
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800275 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700276 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
Walter Wuf33a0142020-08-06 23:24:54 -0700277
Patricia Alfonso73228c72020-10-13 16:55:06 -0700278 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800279 kfree(ptr2);
280}
281
Patricia Alfonso73228c72020-10-13 16:55:06 -0700282static void kmalloc_oob_16(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800283{
284 struct {
285 u64 words[2];
286 } *ptr1, *ptr2;
287
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800288 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100289 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800290
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800291 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700292 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
293
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800294 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700295 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
296
297 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800298 kfree(ptr1);
299 kfree(ptr2);
300}
301
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800302static void kmalloc_uaf_16(struct kunit *test)
303{
304 struct {
305 u64 words[2];
306 } *ptr1, *ptr2;
307
308 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
309 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
310
311 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
312 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
313 kfree(ptr2);
314
315 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
316 kfree(ptr1);
317}
318
Patricia Alfonso73228c72020-10-13 16:55:06 -0700319static void kmalloc_oob_memset_2(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800320{
321 char *ptr;
322 size_t size = 8;
323
Wang Longf523e732015-11-05 18:51:15 -0800324 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700325 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800326
Patricia Alfonso73228c72020-10-13 16:55:06 -0700327 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Wang Longf523e732015-11-05 18:51:15 -0800328 kfree(ptr);
329}
330
Patricia Alfonso73228c72020-10-13 16:55:06 -0700331static void kmalloc_oob_memset_4(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800332{
333 char *ptr;
334 size_t size = 8;
335
Wang Longf523e732015-11-05 18:51:15 -0800336 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700337 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800338
Patricia Alfonso73228c72020-10-13 16:55:06 -0700339 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Wang Longf523e732015-11-05 18:51:15 -0800340 kfree(ptr);
341}
342
343
Patricia Alfonso73228c72020-10-13 16:55:06 -0700344static void kmalloc_oob_memset_8(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800345{
346 char *ptr;
347 size_t size = 8;
348
Wang Longf523e732015-11-05 18:51:15 -0800349 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700350 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800351
Patricia Alfonso73228c72020-10-13 16:55:06 -0700352 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Wang Longf523e732015-11-05 18:51:15 -0800353 kfree(ptr);
354}
355
Patricia Alfonso73228c72020-10-13 16:55:06 -0700356static void kmalloc_oob_memset_16(struct kunit *test)
Wang Longf523e732015-11-05 18:51:15 -0800357{
358 char *ptr;
359 size_t size = 16;
360
Wang Longf523e732015-11-05 18:51:15 -0800361 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700362 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Wang Longf523e732015-11-05 18:51:15 -0800363
Patricia Alfonso73228c72020-10-13 16:55:06 -0700364 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Wang Longf523e732015-11-05 18:51:15 -0800365 kfree(ptr);
366}
367
Patricia Alfonso73228c72020-10-13 16:55:06 -0700368static void kmalloc_oob_in_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800369{
370 char *ptr;
371 size_t size = 666;
372
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800373 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700374 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800375
Patricia Alfonso73228c72020-10-13 16:55:06 -0700376 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800377 kfree(ptr);
378}
379
Patricia Alfonso73228c72020-10-13 16:55:06 -0700380static void kmalloc_memmove_invalid_size(struct kunit *test)
Walter Wu98f3b562020-04-01 21:09:40 -0700381{
382 char *ptr;
383 size_t size = 64;
384 volatile size_t invalid_size = -2;
385
Walter Wu98f3b562020-04-01 21:09:40 -0700386 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700387 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Walter Wu98f3b562020-04-01 21:09:40 -0700388
389 memset((char *)ptr, 0, 64);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700390
391 KUNIT_EXPECT_KASAN_FAIL(test,
392 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
Walter Wu98f3b562020-04-01 21:09:40 -0700393 kfree(ptr);
394}
395
Patricia Alfonso73228c72020-10-13 16:55:06 -0700396static void kmalloc_uaf(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800397{
398 char *ptr;
399 size_t size = 10;
400
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800401 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700402 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800403
404 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700405 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800406}
407
Patricia Alfonso73228c72020-10-13 16:55:06 -0700408static void kmalloc_uaf_memset(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800409{
410 char *ptr;
411 size_t size = 33;
412
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800413 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700414 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800415
416 kfree(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700417 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800418}
419
Patricia Alfonso73228c72020-10-13 16:55:06 -0700420static void kmalloc_uaf2(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800421{
422 char *ptr1, *ptr2;
423 size_t size = 43;
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100424 int counter = 0;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800425
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100426again:
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800427 ptr1 = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700428 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800429
430 kfree(ptr1);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800431
Patricia Alfonso73228c72020-10-13 16:55:06 -0700432 ptr2 = kmalloc(size, GFP_KERNEL);
433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
434
Andrey Konovalov0c23e1c2021-02-03 15:35:04 +1100435 /*
436 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
437 * Allow up to 16 attempts at generating different tags.
438 */
439 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
440 kfree(ptr2);
441 goto again;
442 }
443
Patricia Alfonso73228c72020-10-13 16:55:06 -0700444 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
445 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
446
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800447 kfree(ptr2);
448}
449
Patricia Alfonso73228c72020-10-13 16:55:06 -0700450static void kfree_via_page(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700451{
452 char *ptr;
453 size_t size = 8;
454 struct page *page;
455 unsigned long offset;
456
Mark Rutlandb92a9532019-09-23 15:34:16 -0700457 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700458 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700459
460 page = virt_to_page(ptr);
461 offset = offset_in_page(ptr);
462 kfree(page_address(page) + offset);
463}
464
Patricia Alfonso73228c72020-10-13 16:55:06 -0700465static void kfree_via_phys(struct kunit *test)
Mark Rutlandb92a9532019-09-23 15:34:16 -0700466{
467 char *ptr;
468 size_t size = 8;
469 phys_addr_t phys;
470
Mark Rutlandb92a9532019-09-23 15:34:16 -0700471 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700472 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Mark Rutlandb92a9532019-09-23 15:34:16 -0700473
474 phys = virt_to_phys(ptr);
475 kfree(phys_to_virt(phys));
476}
477
Patricia Alfonso73228c72020-10-13 16:55:06 -0700478static void kmem_cache_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800479{
480 char *p;
481 size_t size = 200;
482 struct kmem_cache *cache = kmem_cache_create("test_cache",
483 size, 0,
484 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700485 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800486 p = kmem_cache_alloc(cache, GFP_KERNEL);
487 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700488 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800489 kmem_cache_destroy(cache);
490 return;
491 }
492
Patricia Alfonso73228c72020-10-13 16:55:06 -0700493 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800494 kmem_cache_free(cache, p);
495 kmem_cache_destroy(cache);
496}
497
Patricia Alfonso73228c72020-10-13 16:55:06 -0700498static void memcg_accounted_kmem_cache(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800499{
500 int i;
501 char *p;
502 size_t size = 200;
503 struct kmem_cache *cache;
504
505 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700506 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800507
Greg Thelen0386bf32017-02-24 15:00:08 -0800508 /*
509 * Several allocations with a delay to allow for lazy per memcg kmem
510 * cache creation.
511 */
512 for (i = 0; i < 5; i++) {
513 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800514 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800515 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800516
Greg Thelen0386bf32017-02-24 15:00:08 -0800517 kmem_cache_free(cache, p);
518 msleep(100);
519 }
520
521free_cache:
522 kmem_cache_destroy(cache);
523}
524
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800525static char global_array[10];
526
Patricia Alfonso73228c72020-10-13 16:55:06 -0700527static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800528{
529 volatile int i = 3;
530 char *p = &global_array[ARRAY_SIZE(global_array) + i];
531
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800532 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100533 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800534
Patricia Alfonso73228c72020-10-13 16:55:06 -0700535 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800536}
537
Andrey Konovalov696574e2021-02-03 15:35:05 +1100538/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700539static void ksize_unpoisons_memory(struct kunit *test)
540{
541 char *ptr;
542 size_t size = 123, real_size;
543
544 ptr = kmalloc(size, GFP_KERNEL);
545 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
546 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100547
548 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700549 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100550
551 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700552 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100553
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554 kfree(ptr);
555}
556
Andrey Konovalov696574e2021-02-03 15:35:05 +1100557/*
558 * Check that a use-after-free is detected by ksize() and via normal accesses
559 * after it.
560 */
561static void ksize_uaf(struct kunit *test)
562{
563 char *ptr;
564 int size = 128 - KASAN_GRANULE_SIZE;
565
566 ptr = kmalloc(size, GFP_KERNEL);
567 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
568 kfree(ptr);
569
570 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
571 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
572 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
573}
574
Patricia Alfonso73228c72020-10-13 16:55:06 -0700575static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800576{
577 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700578 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800579 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
580
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100581 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700582
Patricia Alfonso73228c72020-10-13 16:55:06 -0700583 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700584}
585
Patricia Alfonso73228c72020-10-13 16:55:06 -0700586static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800587{
588 volatile int i = 10;
589 char alloca_array[i];
590 char *p = alloca_array - 1;
591
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800592 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100593 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
594 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700595
596 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800597}
598
Patricia Alfonso73228c72020-10-13 16:55:06 -0700599static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800600{
601 volatile int i = 10;
602 char alloca_array[i];
603 char *p = alloca_array + i;
604
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800605 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100606 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
607 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700608
609 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800610}
611
Patricia Alfonso73228c72020-10-13 16:55:06 -0700612static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800613{
614 char *p;
615 size_t size = 200;
616 struct kmem_cache *cache;
617
618 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700619 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
620
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800621 p = kmem_cache_alloc(cache, GFP_KERNEL);
622 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700623 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800624 kmem_cache_destroy(cache);
625 return;
626 }
627
628 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700629 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800630 kmem_cache_destroy(cache);
631}
632
Patricia Alfonso73228c72020-10-13 16:55:06 -0700633static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800634{
635 char *p;
636 size_t size = 200;
637 struct kmem_cache *cache;
638
639 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
640 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700641 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
642
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800643 p = kmem_cache_alloc(cache, GFP_KERNEL);
644 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700645 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800646 kmem_cache_destroy(cache);
647 return;
648 }
649
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100650 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700651 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700652
653 /*
654 * Properly free the object to prevent the "Objects remaining in
655 * test_cache on __kmem_cache_shutdown" BUG failure.
656 */
657 kmem_cache_free(cache, p);
658
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800659 kmem_cache_destroy(cache);
660}
661
Patricia Alfonso73228c72020-10-13 16:55:06 -0700662static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700663{
664 char *ptr;
665 size_t size = 24;
666
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100667 /*
668 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
669 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
670 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100671 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700672
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800673 if (OOB_TAG_OFF)
674 size = round_up(size, OOB_TAG_OFF);
675
Patricia Alfonso73228c72020-10-13 16:55:06 -0700676 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
677 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
678
679 KUNIT_EXPECT_KASAN_FAIL(test,
680 kasan_ptr_result = memchr(ptr, '1', size + 1));
681
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700682 kfree(ptr);
683}
684
Patricia Alfonso73228c72020-10-13 16:55:06 -0700685static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700686{
687 char *ptr;
688 size_t size = 24;
689 int arr[9];
690
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100691 /*
692 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
693 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
694 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100695 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700696
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800697 if (OOB_TAG_OFF)
698 size = round_up(size, OOB_TAG_OFF);
699
Patricia Alfonso73228c72020-10-13 16:55:06 -0700700 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
701 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700702 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700703
704 KUNIT_EXPECT_KASAN_FAIL(test,
705 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700706 kfree(ptr);
707}
708
Patricia Alfonso73228c72020-10-13 16:55:06 -0700709static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700710{
711 char *ptr;
712 size_t size = 24;
713
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100714 /*
715 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
716 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
717 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100718 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700719
720 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
721 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700722
723 kfree(ptr);
724
725 /*
726 * Try to cause only 1 invalid access (less spam in dmesg).
727 * For that we need ptr to point to zeroed byte.
728 * Skip metadata that could be stored in freed object so ptr
729 * will likely point to zeroed byte.
730 */
731 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700732 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700733
Patricia Alfonso73228c72020-10-13 16:55:06 -0700734 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700735
Patricia Alfonso73228c72020-10-13 16:55:06 -0700736 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700737
Patricia Alfonso73228c72020-10-13 16:55:06 -0700738 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700739
Patricia Alfonso73228c72020-10-13 16:55:06 -0700740 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700741
Patricia Alfonso73228c72020-10-13 16:55:06 -0700742 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700743}
744
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800745static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700746{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800747 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
748 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
749 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
750 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
751 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
752 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
753 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
754 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
755}
756
757static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
758{
759 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
760 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
761 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
762 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
763 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
764 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
765 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
766 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
767
768#if defined(clear_bit_unlock_is_negative_byte)
769 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
770 clear_bit_unlock_is_negative_byte(nr, addr));
771#endif
772}
773
774static void kasan_bitops_generic(struct kunit *test)
775{
776 long *bits;
777
778 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100779 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800780
Marco Elver19a33ca2019-07-11 20:53:52 -0700781 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100782 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700783 * this way we do not actually corrupt other memory.
784 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800785 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700786 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700787
788 /*
789 * Below calls try to access bit within allocated memory; however, the
790 * below accesses are still out-of-bounds, since bitops are defined to
791 * operate on the whole long the bit is in.
792 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800793 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700794
795 /*
796 * Below calls try to access bit beyond allocated memory.
797 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800798 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700799
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800800 kfree(bits);
801}
Marco Elver19a33ca2019-07-11 20:53:52 -0700802
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800803static void kasan_bitops_tags(struct kunit *test)
804{
805 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700806
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100807 /* This test is specifically crafted for tag-based modes. */
808 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700809
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100810 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
811 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800812 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700813
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100814 /* Do the accesses past the 48 allocated bytes, but within the redone. */
815 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
816 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700817
Marco Elver19a33ca2019-07-11 20:53:52 -0700818 kfree(bits);
819}
820
Patricia Alfonso73228c72020-10-13 16:55:06 -0700821static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700822{
823 char *ptr;
824 size_t size = 16;
825
Marco Elverbb104ed2019-07-11 20:54:11 -0700826 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700827 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700828
Waiman Long453431a2020-08-06 23:18:13 -0700829 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700830 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700831}
832
Patricia Alfonso73228c72020-10-13 16:55:06 -0700833static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800834{
835 void *area;
836
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100837 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800838
839 /*
840 * We have to be careful not to hit the guard page.
841 * The MMU will catch that and crash us.
842 */
843 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700844 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800845
Patricia Alfonso73228c72020-10-13 16:55:06 -0700846 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800847 vfree(area);
848}
Daniel Axtens06513912019-11-30 17:54:53 -0800849
Andrey Konovalov782ba452021-02-03 15:35:00 +1100850/*
851 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
852 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
853 * modes.
854 */
855static void match_all_not_assigned(struct kunit *test)
856{
857 char *ptr;
858 struct page *pages;
859 int i, size, order;
860
861 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
862
863 for (i = 0; i < 256; i++) {
864 size = (get_random_int() % 1024) + 1;
865 ptr = kmalloc(size, GFP_KERNEL);
866 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
867 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
868 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
869 kfree(ptr);
870 }
871
872 for (i = 0; i < 256; i++) {
873 order = (get_random_int() % 4) + 1;
874 pages = alloc_pages(GFP_KERNEL, order);
875 ptr = page_address(pages);
876 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
877 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
878 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
879 free_pages((unsigned long)ptr, order);
880 }
881}
882
883/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
884static void match_all_ptr_tag(struct kunit *test)
885{
886 char *ptr;
887 u8 tag;
888
889 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
890
891 ptr = kmalloc(128, GFP_KERNEL);
892 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
893
894 /* Backup the assigned tag. */
895 tag = get_tag(ptr);
896 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
897
898 /* Reset the tag to 0xff.*/
899 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
900
901 /* This access shouldn't trigger a KASAN report. */
902 *ptr = 0;
903
904 /* Recover the pointer tag and free. */
905 ptr = set_tag(ptr, tag);
906 kfree(ptr);
907}
908
909/* Check that there are no match-all memory tags for tag-based modes. */
910static void match_all_mem_tag(struct kunit *test)
911{
912 char *ptr;
913 int tag;
914
915 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
916
917 ptr = kmalloc(128, GFP_KERNEL);
918 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
919 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
920
921 /* For each possible tag value not matching the pointer tag. */
922 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
923 if (tag == get_tag(ptr))
924 continue;
925
926 /* Mark the first memory granule with the chosen memory tag. */
927 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
928
929 /* This access must cause a KASAN report. */
930 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
931 }
932
933 /* Recover the memory tag and free. */
934 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
935 kfree(ptr);
936}
937
Patricia Alfonso73228c72020-10-13 16:55:06 -0700938static struct kunit_case kasan_kunit_test_cases[] = {
939 KUNIT_CASE(kmalloc_oob_right),
940 KUNIT_CASE(kmalloc_oob_left),
941 KUNIT_CASE(kmalloc_node_oob_right),
942 KUNIT_CASE(kmalloc_pagealloc_oob_right),
943 KUNIT_CASE(kmalloc_pagealloc_uaf),
944 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalove449e272021-02-03 15:35:06 +1100945 KUNIT_CASE(pagealloc_oob_right),
946 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700947 KUNIT_CASE(kmalloc_large_oob_right),
948 KUNIT_CASE(kmalloc_oob_krealloc_more),
949 KUNIT_CASE(kmalloc_oob_krealloc_less),
950 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800951 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700952 KUNIT_CASE(kmalloc_oob_in_memset),
953 KUNIT_CASE(kmalloc_oob_memset_2),
954 KUNIT_CASE(kmalloc_oob_memset_4),
955 KUNIT_CASE(kmalloc_oob_memset_8),
956 KUNIT_CASE(kmalloc_oob_memset_16),
957 KUNIT_CASE(kmalloc_memmove_invalid_size),
958 KUNIT_CASE(kmalloc_uaf),
959 KUNIT_CASE(kmalloc_uaf_memset),
960 KUNIT_CASE(kmalloc_uaf2),
961 KUNIT_CASE(kfree_via_page),
962 KUNIT_CASE(kfree_via_phys),
963 KUNIT_CASE(kmem_cache_oob),
964 KUNIT_CASE(memcg_accounted_kmem_cache),
965 KUNIT_CASE(kasan_global_oob),
966 KUNIT_CASE(kasan_stack_oob),
967 KUNIT_CASE(kasan_alloca_oob_left),
968 KUNIT_CASE(kasan_alloca_oob_right),
969 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov696574e2021-02-03 15:35:05 +1100970 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700971 KUNIT_CASE(kmem_cache_double_free),
972 KUNIT_CASE(kmem_cache_invalid_free),
973 KUNIT_CASE(kasan_memchr),
974 KUNIT_CASE(kasan_memcmp),
975 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800976 KUNIT_CASE(kasan_bitops_generic),
977 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700978 KUNIT_CASE(kmalloc_double_kzfree),
979 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +1100980 KUNIT_CASE(match_all_not_assigned),
981 KUNIT_CASE(match_all_ptr_tag),
982 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700983 {}
984};
Walter Wu387d6e42020-08-06 23:24:42 -0700985
Patricia Alfonso73228c72020-10-13 16:55:06 -0700986static struct kunit_suite kasan_kunit_test_suite = {
987 .name = "kasan",
988 .init = kasan_test_init,
989 .test_cases = kasan_kunit_test_cases,
990 .exit = kasan_test_exit,
991};
Walter Wu387d6e42020-08-06 23:24:42 -0700992
Patricia Alfonso73228c72020-10-13 16:55:06 -0700993kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -0700994
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800995MODULE_LICENSE("GPL");