blob: 4ba7461210fd40823d2345e0ce6d0ff4af618b2c [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;
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100482 struct kmem_cache *cache;
483
484 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700485 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100486
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800487 p = kmem_cache_alloc(cache, GFP_KERNEL);
488 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700489 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800490 kmem_cache_destroy(cache);
491 return;
492 }
493
Patricia Alfonso73228c72020-10-13 16:55:06 -0700494 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100495
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800496 kmem_cache_free(cache, p);
497 kmem_cache_destroy(cache);
498}
499
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100500static void kmem_cache_accounted(struct kunit *test)
Greg Thelen0386bf32017-02-24 15:00:08 -0800501{
502 int i;
503 char *p;
504 size_t size = 200;
505 struct kmem_cache *cache;
506
507 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700508 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Greg Thelen0386bf32017-02-24 15:00:08 -0800509
Greg Thelen0386bf32017-02-24 15:00:08 -0800510 /*
511 * Several allocations with a delay to allow for lazy per memcg kmem
512 * cache creation.
513 */
514 for (i = 0; i < 5; i++) {
515 p = kmem_cache_alloc(cache, GFP_KERNEL);
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800516 if (!p)
Greg Thelen0386bf32017-02-24 15:00:08 -0800517 goto free_cache;
Markus Elfringdc2bf0002017-11-17 15:28:00 -0800518
Greg Thelen0386bf32017-02-24 15:00:08 -0800519 kmem_cache_free(cache, p);
520 msleep(100);
521 }
522
523free_cache:
524 kmem_cache_destroy(cache);
525}
526
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100527static void kmem_cache_bulk(struct kunit *test)
528{
529 struct kmem_cache *cache;
530 size_t size = 200;
531 char *p[10];
532 bool ret;
533 int i;
534
535 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
536 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
537
538 ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
539 if (!ret) {
540 kunit_err(test, "Allocation failed: %s\n", __func__);
541 kmem_cache_destroy(cache);
542 return;
543 }
544
545 for (i = 0; i < ARRAY_SIZE(p); i++)
546 p[i][0] = p[i][size - 1] = 42;
547
548 kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
549 kmem_cache_destroy(cache);
550}
551
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800552static char global_array[10];
553
Patricia Alfonso73228c72020-10-13 16:55:06 -0700554static void kasan_global_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800555{
556 volatile int i = 3;
557 char *p = &global_array[ARRAY_SIZE(global_array) + i];
558
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800559 /* Only generic mode instruments globals. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100560 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800561
Patricia Alfonso73228c72020-10-13 16:55:06 -0700562 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800563}
564
Andrey Konovalov696574e2021-02-03 15:35:05 +1100565/* Check that ksize() makes the whole object accessible. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700566static void ksize_unpoisons_memory(struct kunit *test)
567{
568 char *ptr;
569 size_t size = 123, real_size;
570
571 ptr = kmalloc(size, GFP_KERNEL);
572 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
573 real_size = ksize(ptr);
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100574
575 /* This access shouldn't trigger a KASAN report. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700576 ptr[size] = 'x';
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100577
578 /* This one must. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700579 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100580
Patricia Alfonso73228c72020-10-13 16:55:06 -0700581 kfree(ptr);
582}
583
Andrey Konovalov696574e2021-02-03 15:35:05 +1100584/*
585 * Check that a use-after-free is detected by ksize() and via normal accesses
586 * after it.
587 */
588static void ksize_uaf(struct kunit *test)
589{
590 char *ptr;
591 int size = 128 - KASAN_GRANULE_SIZE;
592
593 ptr = kmalloc(size, GFP_KERNEL);
594 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
595 kfree(ptr);
596
597 KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
598 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
599 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
600}
601
Patricia Alfonso73228c72020-10-13 16:55:06 -0700602static void kasan_stack_oob(struct kunit *test)
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800603{
604 char stack_array[10];
Andrey Konovalov51dcc812020-08-06 23:25:12 -0700605 volatile int i = OOB_TAG_OFF;
Andrey Ryabinin3f158012015-02-13 14:39:53 -0800606 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
607
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100608 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700609
Patricia Alfonso73228c72020-10-13 16:55:06 -0700610 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrey Ryabinineae08dc2016-05-20 16:59:34 -0700611}
612
Patricia Alfonso73228c72020-10-13 16:55:06 -0700613static void kasan_alloca_oob_left(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800614{
615 volatile int i = 10;
616 char alloca_array[i];
617 char *p = alloca_array - 1;
618
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800619 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100620 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
621 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700622
623 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800624}
625
Patricia Alfonso73228c72020-10-13 16:55:06 -0700626static void kasan_alloca_oob_right(struct kunit *test)
Paul Lawrence00a14292018-02-06 15:36:16 -0800627{
628 volatile int i = 10;
629 char alloca_array[i];
630 char *p = alloca_array + i;
631
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800632 /* Only generic mode instruments dynamic allocas. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100633 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
634 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700635
636 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Paul Lawrence00a14292018-02-06 15:36:16 -0800637}
638
Patricia Alfonso73228c72020-10-13 16:55:06 -0700639static void kmem_cache_double_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800640{
641 char *p;
642 size_t size = 200;
643 struct kmem_cache *cache;
644
645 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700646 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
647
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800648 p = kmem_cache_alloc(cache, GFP_KERNEL);
649 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700650 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800651 kmem_cache_destroy(cache);
652 return;
653 }
654
655 kmem_cache_free(cache, p);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700656 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800657 kmem_cache_destroy(cache);
658}
659
Patricia Alfonso73228c72020-10-13 16:55:06 -0700660static void kmem_cache_invalid_free(struct kunit *test)
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800661{
662 char *p;
663 size_t size = 200;
664 struct kmem_cache *cache;
665
666 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
667 NULL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700668 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
669
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800670 p = kmem_cache_alloc(cache, GFP_KERNEL);
671 if (!p) {
Patricia Alfonso73228c72020-10-13 16:55:06 -0700672 kunit_err(test, "Allocation failed: %s\n", __func__);
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800673 kmem_cache_destroy(cache);
674 return;
675 }
676
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100677 /* Trigger invalid free, the object doesn't get freed. */
Patricia Alfonso73228c72020-10-13 16:55:06 -0700678 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrey Konovalov91c93ed2018-04-10 16:30:35 -0700679
680 /*
681 * Properly free the object to prevent the "Objects remaining in
682 * test_cache on __kmem_cache_shutdown" BUG failure.
683 */
684 kmem_cache_free(cache, p);
685
Dmitry Vyukovb1d57282018-02-06 15:36:37 -0800686 kmem_cache_destroy(cache);
687}
688
Patricia Alfonso73228c72020-10-13 16:55:06 -0700689static void kasan_memchr(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700690{
691 char *ptr;
692 size_t size = 24;
693
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100694 /*
695 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
696 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
697 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100698 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700699
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800700 if (OOB_TAG_OFF)
701 size = round_up(size, OOB_TAG_OFF);
702
Patricia Alfonso73228c72020-10-13 16:55:06 -0700703 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
704 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
705
706 KUNIT_EXPECT_KASAN_FAIL(test,
707 kasan_ptr_result = memchr(ptr, '1', size + 1));
708
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700709 kfree(ptr);
710}
711
Patricia Alfonso73228c72020-10-13 16:55:06 -0700712static void kasan_memcmp(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700713{
714 char *ptr;
715 size_t size = 24;
716 int arr[9];
717
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100718 /*
719 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
720 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
721 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100722 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700723
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800724 if (OOB_TAG_OFF)
725 size = round_up(size, OOB_TAG_OFF);
726
Patricia Alfonso73228c72020-10-13 16:55:06 -0700727 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
728 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700729 memset(arr, 0, sizeof(arr));
Patricia Alfonso73228c72020-10-13 16:55:06 -0700730
731 KUNIT_EXPECT_KASAN_FAIL(test,
732 kasan_int_result = memcmp(ptr, arr, size+1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700733 kfree(ptr);
734}
735
Patricia Alfonso73228c72020-10-13 16:55:06 -0700736static void kasan_strings(struct kunit *test)
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700737{
738 char *ptr;
739 size_t size = 24;
740
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100741 /*
742 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
743 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
744 */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100745 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700746
747 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
748 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700749
750 kfree(ptr);
751
752 /*
753 * Try to cause only 1 invalid access (less spam in dmesg).
754 * For that we need ptr to point to zeroed byte.
755 * Skip metadata that could be stored in freed object so ptr
756 * will likely point to zeroed byte.
757 */
758 ptr += 16;
Patricia Alfonso73228c72020-10-13 16:55:06 -0700759 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700760
Patricia Alfonso73228c72020-10-13 16:55:06 -0700761 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700762
Patricia Alfonso73228c72020-10-13 16:55:06 -0700763 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700764
Patricia Alfonso73228c72020-10-13 16:55:06 -0700765 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700766
Patricia Alfonso73228c72020-10-13 16:55:06 -0700767 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700768
Patricia Alfonso73228c72020-10-13 16:55:06 -0700769 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
Andrey Ryabinin0c963502018-10-26 15:02:34 -0700770}
771
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800772static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
Marco Elver19a33ca2019-07-11 20:53:52 -0700773{
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800774 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
775 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
776 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
777 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
778 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
779 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
780 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
781 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
782}
783
784static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
785{
786 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
787 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
788 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
789 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
790 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
791 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
792 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
793 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
794
795#if defined(clear_bit_unlock_is_negative_byte)
796 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
797 clear_bit_unlock_is_negative_byte(nr, addr));
798#endif
799}
800
801static void kasan_bitops_generic(struct kunit *test)
802{
803 long *bits;
804
805 /* This test is specifically crafted for the generic mode. */
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100806 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800807
Marco Elver19a33ca2019-07-11 20:53:52 -0700808 /*
Andrey Konovalovf3e66b22021-02-03 15:34:59 +1100809 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Marco Elver19a33ca2019-07-11 20:53:52 -0700810 * this way we do not actually corrupt other memory.
811 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800812 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700813 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700814
815 /*
816 * Below calls try to access bit within allocated memory; however, the
817 * below accesses are still out-of-bounds, since bitops are defined to
818 * operate on the whole long the bit is in.
819 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800820 kasan_bitops_modify(test, BITS_PER_LONG, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700821
822 /*
823 * Below calls try to access bit beyond allocated memory.
824 */
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800825 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700826
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800827 kfree(bits);
828}
Marco Elver19a33ca2019-07-11 20:53:52 -0700829
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800830static void kasan_bitops_tags(struct kunit *test)
831{
832 long *bits;
Marco Elver19a33ca2019-07-11 20:53:52 -0700833
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100834 /* This test is specifically crafted for tag-based modes. */
835 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
Marco Elver19a33ca2019-07-11 20:53:52 -0700836
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100837 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
838 bits = kzalloc(48, GFP_KERNEL);
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800839 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
Marco Elver19a33ca2019-07-11 20:53:52 -0700840
Andrey Konovalovc1e807d2021-02-03 15:35:04 +1100841 /* Do the accesses past the 48 allocated bytes, but within the redone. */
842 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
843 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
Marco Elver19a33ca2019-07-11 20:53:52 -0700844
Marco Elver19a33ca2019-07-11 20:53:52 -0700845 kfree(bits);
846}
847
Patricia Alfonso73228c72020-10-13 16:55:06 -0700848static void kmalloc_double_kzfree(struct kunit *test)
Marco Elverbb104ed2019-07-11 20:54:11 -0700849{
850 char *ptr;
851 size_t size = 16;
852
Marco Elverbb104ed2019-07-11 20:54:11 -0700853 ptr = kmalloc(size, GFP_KERNEL);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700854 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Marco Elverbb104ed2019-07-11 20:54:11 -0700855
Waiman Long453431a2020-08-06 23:18:13 -0700856 kfree_sensitive(ptr);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700857 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
Marco Elverbb104ed2019-07-11 20:54:11 -0700858}
859
Patricia Alfonso73228c72020-10-13 16:55:06 -0700860static void vmalloc_oob(struct kunit *test)
Daniel Axtens06513912019-11-30 17:54:53 -0800861{
862 void *area;
863
Andrey Konovalov127ffef2021-02-03 15:35:00 +1100864 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
Daniel Axtens06513912019-11-30 17:54:53 -0800865
866 /*
867 * We have to be careful not to hit the guard page.
868 * The MMU will catch that and crash us.
869 */
870 area = vmalloc(3000);
Patricia Alfonso73228c72020-10-13 16:55:06 -0700871 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Daniel Axtens06513912019-11-30 17:54:53 -0800872
Patricia Alfonso73228c72020-10-13 16:55:06 -0700873 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
Daniel Axtens06513912019-11-30 17:54:53 -0800874 vfree(area);
875}
Daniel Axtens06513912019-11-30 17:54:53 -0800876
Andrey Konovalov782ba452021-02-03 15:35:00 +1100877/*
878 * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
879 * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
880 * modes.
881 */
882static void match_all_not_assigned(struct kunit *test)
883{
884 char *ptr;
885 struct page *pages;
886 int i, size, order;
887
888 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
889
890 for (i = 0; i < 256; i++) {
891 size = (get_random_int() % 1024) + 1;
892 ptr = kmalloc(size, GFP_KERNEL);
893 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
894 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
895 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
896 kfree(ptr);
897 }
898
899 for (i = 0; i < 256; i++) {
900 order = (get_random_int() % 4) + 1;
901 pages = alloc_pages(GFP_KERNEL, order);
902 ptr = page_address(pages);
903 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
904 KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
905 KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
906 free_pages((unsigned long)ptr, order);
907 }
908}
909
910/* Check that 0xff works as a match-all pointer tag for tag-based modes. */
911static void match_all_ptr_tag(struct kunit *test)
912{
913 char *ptr;
914 u8 tag;
915
916 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
917
918 ptr = kmalloc(128, GFP_KERNEL);
919 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
920
921 /* Backup the assigned tag. */
922 tag = get_tag(ptr);
923 KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
924
925 /* Reset the tag to 0xff.*/
926 ptr = set_tag(ptr, KASAN_TAG_KERNEL);
927
928 /* This access shouldn't trigger a KASAN report. */
929 *ptr = 0;
930
931 /* Recover the pointer tag and free. */
932 ptr = set_tag(ptr, tag);
933 kfree(ptr);
934}
935
936/* Check that there are no match-all memory tags for tag-based modes. */
937static void match_all_mem_tag(struct kunit *test)
938{
939 char *ptr;
940 int tag;
941
942 KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
943
944 ptr = kmalloc(128, GFP_KERNEL);
945 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
946 KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
947
948 /* For each possible tag value not matching the pointer tag. */
949 for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
950 if (tag == get_tag(ptr))
951 continue;
952
953 /* Mark the first memory granule with the chosen memory tag. */
954 kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag);
955
956 /* This access must cause a KASAN report. */
957 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
958 }
959
960 /* Recover the memory tag and free. */
961 kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr));
962 kfree(ptr);
963}
964
Patricia Alfonso73228c72020-10-13 16:55:06 -0700965static struct kunit_case kasan_kunit_test_cases[] = {
966 KUNIT_CASE(kmalloc_oob_right),
967 KUNIT_CASE(kmalloc_oob_left),
968 KUNIT_CASE(kmalloc_node_oob_right),
969 KUNIT_CASE(kmalloc_pagealloc_oob_right),
970 KUNIT_CASE(kmalloc_pagealloc_uaf),
971 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
Andrey Konovalove449e272021-02-03 15:35:06 +1100972 KUNIT_CASE(pagealloc_oob_right),
973 KUNIT_CASE(pagealloc_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700974 KUNIT_CASE(kmalloc_large_oob_right),
975 KUNIT_CASE(kmalloc_oob_krealloc_more),
976 KUNIT_CASE(kmalloc_oob_krealloc_less),
977 KUNIT_CASE(kmalloc_oob_16),
Andrey Konovalov58b999d2020-11-01 17:07:37 -0800978 KUNIT_CASE(kmalloc_uaf_16),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700979 KUNIT_CASE(kmalloc_oob_in_memset),
980 KUNIT_CASE(kmalloc_oob_memset_2),
981 KUNIT_CASE(kmalloc_oob_memset_4),
982 KUNIT_CASE(kmalloc_oob_memset_8),
983 KUNIT_CASE(kmalloc_oob_memset_16),
984 KUNIT_CASE(kmalloc_memmove_invalid_size),
985 KUNIT_CASE(kmalloc_uaf),
986 KUNIT_CASE(kmalloc_uaf_memset),
987 KUNIT_CASE(kmalloc_uaf2),
988 KUNIT_CASE(kfree_via_page),
989 KUNIT_CASE(kfree_via_phys),
990 KUNIT_CASE(kmem_cache_oob),
Andrey Konovalov9346eae2021-02-03 15:35:06 +1100991 KUNIT_CASE(kmem_cache_accounted),
992 KUNIT_CASE(kmem_cache_bulk),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700993 KUNIT_CASE(kasan_global_oob),
994 KUNIT_CASE(kasan_stack_oob),
995 KUNIT_CASE(kasan_alloca_oob_left),
996 KUNIT_CASE(kasan_alloca_oob_right),
997 KUNIT_CASE(ksize_unpoisons_memory),
Andrey Konovalov696574e2021-02-03 15:35:05 +1100998 KUNIT_CASE(ksize_uaf),
Patricia Alfonso73228c72020-10-13 16:55:06 -0700999 KUNIT_CASE(kmem_cache_double_free),
1000 KUNIT_CASE(kmem_cache_invalid_free),
1001 KUNIT_CASE(kasan_memchr),
1002 KUNIT_CASE(kasan_memcmp),
1003 KUNIT_CASE(kasan_strings),
Andrey Konovalov58b999d2020-11-01 17:07:37 -08001004 KUNIT_CASE(kasan_bitops_generic),
1005 KUNIT_CASE(kasan_bitops_tags),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001006 KUNIT_CASE(kmalloc_double_kzfree),
1007 KUNIT_CASE(vmalloc_oob),
Andrey Konovalov782ba452021-02-03 15:35:00 +11001008 KUNIT_CASE(match_all_not_assigned),
1009 KUNIT_CASE(match_all_ptr_tag),
1010 KUNIT_CASE(match_all_mem_tag),
Patricia Alfonso73228c72020-10-13 16:55:06 -07001011 {}
1012};
Walter Wu387d6e42020-08-06 23:24:42 -07001013
Patricia Alfonso73228c72020-10-13 16:55:06 -07001014static struct kunit_suite kasan_kunit_test_suite = {
1015 .name = "kasan",
1016 .init = kasan_test_init,
1017 .test_cases = kasan_kunit_test_cases,
1018 .exit = kasan_test_exit,
1019};
Walter Wu387d6e42020-08-06 23:24:42 -07001020
Patricia Alfonso73228c72020-10-13 16:55:06 -07001021kunit_test_suite(kasan_kunit_test_suite);
Walter Wu387d6e42020-08-06 23:24:42 -07001022
Andrey Ryabinin3f158012015-02-13 14:39:53 -08001023MODULE_LICENSE("GPL");