blob: 28c7c123a185715f34c3858d9baa200683cc9513 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
David Brazdil0f672f62019-12-10 10:32:29 +00008#include <linux/bitops.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009#include <linux/delay.h>
David Brazdil0f672f62019-12-10 10:32:29 +000010#include <linux/kasan.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#include <linux/kernel.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/mm.h>
David Brazdil0f672f62019-12-10 10:32:29 +000013#include <linux/mman.h>
14#include <linux/module.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include <linux/printk.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/uaccess.h>
David Brazdil0f672f62019-12-10 10:32:29 +000019#include <linux/io.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020020#include <linux/vmalloc.h>
David Brazdil0f672f62019-12-10 10:32:29 +000021
22#include <asm/page.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023
Olivier Deprez157378f2022-04-04 15:47:50 +020024#include <kunit/test.h>
25
26#include "../mm/kasan/kasan.h"
27
28#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
29
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000030/*
Olivier Deprez0e641232021-09-23 10:07:05 +020031 * We assign some test results to these globals to make sure the tests
32 * are not eliminated as dead code.
33 */
34
Olivier Deprez0e641232021-09-23 10:07:05 +020035void *kasan_ptr_result;
Olivier Deprez157378f2022-04-04 15:47:50 +020036int kasan_int_result;
Olivier Deprez0e641232021-09-23 10:07:05 +020037
Olivier Deprez157378f2022-04-04 15:47:50 +020038static struct kunit_resource resource;
39static struct kunit_kasan_expectation fail_data;
40static bool multishot;
41
42static int kasan_test_init(struct kunit *test)
43{
44 /*
45 * Temporarily enable multi-shot mode and set panic_on_warn=0.
46 * Otherwise, we'd only get a report for the first case.
47 */
48 multishot = kasan_save_enable_multi_shot();
49
50 return 0;
51}
52
53static void kasan_test_exit(struct kunit *test)
54{
55 kasan_restore_multi_shot(multishot);
56}
57
58/**
59 * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
60 * not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
61 * Do not use this name for a KUnit resource outside here.
62 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 */
Olivier Deprez157378f2022-04-04 15:47:50 +020064#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
65 fail_data.report_expected = true; \
66 fail_data.report_found = false; \
67 kunit_add_named_resource(test, \
68 NULL, \
69 NULL, \
70 &resource, \
71 "kasan_data", &fail_data); \
72 condition; \
73 KUNIT_EXPECT_EQ(test, \
74 fail_data.report_expected, \
75 fail_data.report_found); \
76} while (0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077
Olivier Deprez157378f2022-04-04 15:47:50 +020078static void kmalloc_oob_right(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079{
80 char *ptr;
81 size_t size = 123;
82
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +020084 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085
Olivier Deprez157378f2022-04-04 15:47:50 +020086 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 kfree(ptr);
88}
89
Olivier Deprez157378f2022-04-04 15:47:50 +020090static void kmalloc_oob_left(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091{
92 char *ptr;
93 size_t size = 15;
94
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +020096 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097
Olivier Deprez157378f2022-04-04 15:47:50 +020098 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 kfree(ptr);
100}
101
Olivier Deprez157378f2022-04-04 15:47:50 +0200102static void kmalloc_node_oob_right(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103{
104 char *ptr;
105 size_t size = 4096;
106
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107 ptr = kmalloc_node(size, GFP_KERNEL, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +0200108 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111 kfree(ptr);
112}
113
Olivier Deprez157378f2022-04-04 15:47:50 +0200114static void kmalloc_pagealloc_oob_right(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115{
116 char *ptr;
117 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
118
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 if (!IS_ENABLED(CONFIG_SLUB)) {
120 kunit_info(test, "CONFIG_SLUB is not enabled.");
121 return;
122 }
123
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124 /* Allocate a chunk that does not fit into a SLUB cache to trigger
125 * the page allocator fallback.
126 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200128 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129
Olivier Deprez157378f2022-04-04 15:47:50 +0200130 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131 kfree(ptr);
132}
133
Olivier Deprez157378f2022-04-04 15:47:50 +0200134static void kmalloc_pagealloc_uaf(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135{
136 char *ptr;
137 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
138
Olivier Deprez157378f2022-04-04 15:47:50 +0200139 if (!IS_ENABLED(CONFIG_SLUB)) {
140 kunit_info(test, "CONFIG_SLUB is not enabled.");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 return;
142 }
143
Olivier Deprez157378f2022-04-04 15:47:50 +0200144 ptr = kmalloc(size, GFP_KERNEL);
145 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
146
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 kfree(ptr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200148 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149}
150
Olivier Deprez157378f2022-04-04 15:47:50 +0200151static void kmalloc_pagealloc_invalid_free(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152{
153 char *ptr;
154 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
155
Olivier Deprez157378f2022-04-04 15:47:50 +0200156 if (!IS_ENABLED(CONFIG_SLUB)) {
157 kunit_info(test, "CONFIG_SLUB is not enabled.");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158 return;
159 }
160
Olivier Deprez157378f2022-04-04 15:47:50 +0200161 ptr = kmalloc(size, GFP_KERNEL);
162 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163
Olivier Deprez157378f2022-04-04 15:47:50 +0200164 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
165}
166
167static void kmalloc_large_oob_right(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168{
169 char *ptr;
170 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
171 /* Allocate a chunk that is large enough, but still fits into a slab
172 * and does not trigger the page allocator fallback in SLUB.
173 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200175 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000176
Olivier Deprez157378f2022-04-04 15:47:50 +0200177 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 kfree(ptr);
179}
180
Olivier Deprez157378f2022-04-04 15:47:50 +0200181static void kmalloc_oob_krealloc_more(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182{
183 char *ptr1, *ptr2;
184 size_t size1 = 17;
185 size_t size2 = 19;
186
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000187 ptr1 = kmalloc(size1, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189
Olivier Deprez157378f2022-04-04 15:47:50 +0200190 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
191 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
192
193 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194 kfree(ptr2);
195}
196
Olivier Deprez157378f2022-04-04 15:47:50 +0200197static void kmalloc_oob_krealloc_less(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198{
199 char *ptr1, *ptr2;
200 size_t size1 = 17;
201 size_t size2 = 15;
202
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 ptr1 = kmalloc(size1, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200204 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
205
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200207 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
208
209 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210 kfree(ptr2);
211}
212
Olivier Deprez157378f2022-04-04 15:47:50 +0200213static void kmalloc_oob_16(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214{
215 struct {
216 u64 words[2];
217 } *ptr1, *ptr2;
218
Olivier Deprez157378f2022-04-04 15:47:50 +0200219 /* This test is specifically crafted for the generic mode. */
220 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
221 kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 return;
223 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200224
225 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
226 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
227
228 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
229 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
230
231 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 kfree(ptr1);
233 kfree(ptr2);
234}
235
Olivier Deprez157378f2022-04-04 15:47:50 +0200236static void kmalloc_uaf_16(struct kunit *test)
237{
238 struct {
239 u64 words[2];
240 } *ptr1, *ptr2;
241
242 ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
243 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
244
245 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
246 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
247 kfree(ptr2);
248
249 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
250 kfree(ptr1);
251}
252
253static void kmalloc_oob_memset_2(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254{
255 char *ptr;
256 size_t size = 8;
257
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000258 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200259 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260
Olivier Deprez157378f2022-04-04 15:47:50 +0200261 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262 kfree(ptr);
263}
264
Olivier Deprez157378f2022-04-04 15:47:50 +0200265static void kmalloc_oob_memset_4(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266{
267 char *ptr;
268 size_t size = 8;
269
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200271 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000272
Olivier Deprez157378f2022-04-04 15:47:50 +0200273 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274 kfree(ptr);
275}
276
277
Olivier Deprez157378f2022-04-04 15:47:50 +0200278static void kmalloc_oob_memset_8(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279{
280 char *ptr;
281 size_t size = 8;
282
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200284 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000285
Olivier Deprez157378f2022-04-04 15:47:50 +0200286 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287 kfree(ptr);
288}
289
Olivier Deprez157378f2022-04-04 15:47:50 +0200290static void kmalloc_oob_memset_16(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291{
292 char *ptr;
293 size_t size = 16;
294
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200296 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297
Olivier Deprez157378f2022-04-04 15:47:50 +0200298 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299 kfree(ptr);
300}
301
Olivier Deprez157378f2022-04-04 15:47:50 +0200302static void kmalloc_oob_in_memset(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303{
304 char *ptr;
305 size_t size = 666;
306
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200308 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000309
Olivier Deprez157378f2022-04-04 15:47:50 +0200310 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311 kfree(ptr);
312}
313
Olivier Deprez157378f2022-04-04 15:47:50 +0200314static void kmalloc_memmove_invalid_size(struct kunit *test)
315{
316 char *ptr;
317 size_t size = 64;
318 volatile size_t invalid_size = -2;
319
320 ptr = kmalloc(size, GFP_KERNEL);
321 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
322
323 memset((char *)ptr, 0, 64);
324
325 KUNIT_EXPECT_KASAN_FAIL(test,
326 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
327 kfree(ptr);
328}
329
330static void kmalloc_uaf(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331{
332 char *ptr;
333 size_t size = 10;
334
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200336 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000337
338 kfree(ptr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200339 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340}
341
Olivier Deprez157378f2022-04-04 15:47:50 +0200342static void kmalloc_uaf_memset(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343{
344 char *ptr;
345 size_t size = 33;
346
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000347 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200348 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349
350 kfree(ptr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200351 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352}
353
Olivier Deprez157378f2022-04-04 15:47:50 +0200354static void kmalloc_uaf2(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000355{
356 char *ptr1, *ptr2;
357 size_t size = 43;
358
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359 ptr1 = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200360 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361
362 kfree(ptr1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000363
Olivier Deprez157378f2022-04-04 15:47:50 +0200364 ptr2 = kmalloc(size, GFP_KERNEL);
365 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
366
367 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
368 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
369
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370 kfree(ptr2);
371}
372
Olivier Deprez157378f2022-04-04 15:47:50 +0200373static void kfree_via_page(struct kunit *test)
David Brazdil0f672f62019-12-10 10:32:29 +0000374{
375 char *ptr;
376 size_t size = 8;
377 struct page *page;
378 unsigned long offset;
379
David Brazdil0f672f62019-12-10 10:32:29 +0000380 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200381 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
David Brazdil0f672f62019-12-10 10:32:29 +0000382
383 page = virt_to_page(ptr);
384 offset = offset_in_page(ptr);
385 kfree(page_address(page) + offset);
386}
387
Olivier Deprez157378f2022-04-04 15:47:50 +0200388static void kfree_via_phys(struct kunit *test)
David Brazdil0f672f62019-12-10 10:32:29 +0000389{
390 char *ptr;
391 size_t size = 8;
392 phys_addr_t phys;
393
David Brazdil0f672f62019-12-10 10:32:29 +0000394 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200395 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
David Brazdil0f672f62019-12-10 10:32:29 +0000396
397 phys = virt_to_phys(ptr);
398 kfree(phys_to_virt(phys));
399}
400
Olivier Deprez157378f2022-04-04 15:47:50 +0200401static void kmem_cache_oob(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402{
403 char *p;
404 size_t size = 200;
405 struct kmem_cache *cache = kmem_cache_create("test_cache",
406 size, 0,
407 0, NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200408 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409 p = kmem_cache_alloc(cache, GFP_KERNEL);
410 if (!p) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200411 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000412 kmem_cache_destroy(cache);
413 return;
414 }
415
Olivier Deprez157378f2022-04-04 15:47:50 +0200416 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 kmem_cache_free(cache, p);
418 kmem_cache_destroy(cache);
419}
420
Olivier Deprez157378f2022-04-04 15:47:50 +0200421static void memcg_accounted_kmem_cache(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422{
423 int i;
424 char *p;
425 size_t size = 200;
426 struct kmem_cache *cache;
427
428 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200429 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431 /*
432 * Several allocations with a delay to allow for lazy per memcg kmem
433 * cache creation.
434 */
435 for (i = 0; i < 5; i++) {
436 p = kmem_cache_alloc(cache, GFP_KERNEL);
437 if (!p)
438 goto free_cache;
439
440 kmem_cache_free(cache, p);
441 msleep(100);
442 }
443
444free_cache:
445 kmem_cache_destroy(cache);
446}
447
448static char global_array[10];
449
Olivier Deprez157378f2022-04-04 15:47:50 +0200450static void kasan_global_oob(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451{
Olivier Deprez157378f2022-04-04 15:47:50 +0200452 /*
453 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
454 * from failing here and panicing the kernel, access the array via a
455 * volatile pointer, which will prevent the compiler from being able to
456 * determine the array bounds.
457 *
458 * This access uses a volatile pointer to char (char *volatile) rather
459 * than the more conventional pointer to volatile char (volatile char *)
460 * because we want to prevent the compiler from making inferences about
461 * the pointer itself (i.e. its array bounds), not the data that it
462 * refers to.
463 */
464 char *volatile array = global_array;
465 char *p = &array[ARRAY_SIZE(global_array) + 3];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466
Olivier Deprez157378f2022-04-04 15:47:50 +0200467 /* Only generic mode instruments globals. */
468 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
469 kunit_info(test, "CONFIG_KASAN_GENERIC required");
470 return;
471 }
472
473 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000474}
475
Olivier Deprez157378f2022-04-04 15:47:50 +0200476static void ksize_unpoisons_memory(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477{
478 char *ptr;
479 size_t size = 123, real_size;
480
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000481 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200482 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000483 real_size = ksize(ptr);
484 /* This access doesn't trigger an error. */
485 ptr[size] = 'x';
486 /* This one does. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200487 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000488 kfree(ptr);
489}
490
Olivier Deprez157378f2022-04-04 15:47:50 +0200491static void kasan_stack_oob(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000492{
Olivier Deprez157378f2022-04-04 15:47:50 +0200493 char stack_array[10];
494 /* See comment in kasan_global_oob. */
495 char *volatile array = stack_array;
496 char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497
Olivier Deprez157378f2022-04-04 15:47:50 +0200498 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
499 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 return;
501 }
502
Olivier Deprez157378f2022-04-04 15:47:50 +0200503 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000504}
505
Olivier Deprez157378f2022-04-04 15:47:50 +0200506static void kasan_alloca_oob_left(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000507{
508 volatile int i = 10;
509 char alloca_array[i];
Olivier Deprez157378f2022-04-04 15:47:50 +0200510 /* See comment in kasan_global_oob. */
511 char *volatile array = alloca_array;
512 char *p = array - 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000513
Olivier Deprez157378f2022-04-04 15:47:50 +0200514 /* Only generic mode instruments dynamic allocas. */
515 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
516 kunit_info(test, "CONFIG_KASAN_GENERIC required");
517 return;
518 }
519
520 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
521 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
522 return;
523 }
524
525 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000526}
527
Olivier Deprez157378f2022-04-04 15:47:50 +0200528static void kasan_alloca_oob_right(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000529{
530 volatile int i = 10;
531 char alloca_array[i];
Olivier Deprez157378f2022-04-04 15:47:50 +0200532 /* See comment in kasan_global_oob. */
533 char *volatile array = alloca_array;
534 char *p = array + i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000535
Olivier Deprez157378f2022-04-04 15:47:50 +0200536 /* Only generic mode instruments dynamic allocas. */
537 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
538 kunit_info(test, "CONFIG_KASAN_GENERIC required");
539 return;
540 }
541
542 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
543 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
544 return;
545 }
546
547 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548}
549
Olivier Deprez157378f2022-04-04 15:47:50 +0200550static void kmem_cache_double_free(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551{
552 char *p;
553 size_t size = 200;
554 struct kmem_cache *cache;
555
556 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200557 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
558
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000559 p = kmem_cache_alloc(cache, GFP_KERNEL);
560 if (!p) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200561 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562 kmem_cache_destroy(cache);
563 return;
564 }
565
566 kmem_cache_free(cache, p);
Olivier Deprez157378f2022-04-04 15:47:50 +0200567 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568 kmem_cache_destroy(cache);
569}
570
Olivier Deprez157378f2022-04-04 15:47:50 +0200571static void kmem_cache_invalid_free(struct kunit *test)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572{
573 char *p;
574 size_t size = 200;
575 struct kmem_cache *cache;
576
577 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
578 NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200579 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
580
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581 p = kmem_cache_alloc(cache, GFP_KERNEL);
582 if (!p) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200583 kunit_err(test, "Allocation failed: %s\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 kmem_cache_destroy(cache);
585 return;
586 }
587
588 /* Trigger invalid free, the object doesn't get freed */
Olivier Deprez157378f2022-04-04 15:47:50 +0200589 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590
591 /*
592 * Properly free the object to prevent the "Objects remaining in
593 * test_cache on __kmem_cache_shutdown" BUG failure.
594 */
595 kmem_cache_free(cache, p);
596
597 kmem_cache_destroy(cache);
598}
599
Olivier Deprez157378f2022-04-04 15:47:50 +0200600static void kasan_memchr(struct kunit *test)
David Brazdil0f672f62019-12-10 10:32:29 +0000601{
602 char *ptr;
603 size_t size = 24;
604
Olivier Deprez157378f2022-04-04 15:47:50 +0200605 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
606 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
607 kunit_info(test,
608 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
David Brazdil0f672f62019-12-10 10:32:29 +0000609 return;
Olivier Deprez157378f2022-04-04 15:47:50 +0200610 }
David Brazdil0f672f62019-12-10 10:32:29 +0000611
Olivier Deprez157378f2022-04-04 15:47:50 +0200612 if (OOB_TAG_OFF)
613 size = round_up(size, OOB_TAG_OFF);
614
615 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
616 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
617
618 KUNIT_EXPECT_KASAN_FAIL(test,
619 kasan_ptr_result = memchr(ptr, '1', size + 1));
620
David Brazdil0f672f62019-12-10 10:32:29 +0000621 kfree(ptr);
622}
623
Olivier Deprez157378f2022-04-04 15:47:50 +0200624static void kasan_memcmp(struct kunit *test)
David Brazdil0f672f62019-12-10 10:32:29 +0000625{
626 char *ptr;
627 size_t size = 24;
628 int arr[9];
629
Olivier Deprez157378f2022-04-04 15:47:50 +0200630 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
631 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
632 kunit_info(test,
633 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
David Brazdil0f672f62019-12-10 10:32:29 +0000634 return;
Olivier Deprez157378f2022-04-04 15:47:50 +0200635 }
David Brazdil0f672f62019-12-10 10:32:29 +0000636
Olivier Deprez157378f2022-04-04 15:47:50 +0200637 if (OOB_TAG_OFF)
638 size = round_up(size, OOB_TAG_OFF);
639
640 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
641 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
David Brazdil0f672f62019-12-10 10:32:29 +0000642 memset(arr, 0, sizeof(arr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200643
644 KUNIT_EXPECT_KASAN_FAIL(test,
645 kasan_int_result = memcmp(ptr, arr, size+1));
David Brazdil0f672f62019-12-10 10:32:29 +0000646 kfree(ptr);
647}
648
Olivier Deprez157378f2022-04-04 15:47:50 +0200649static void kasan_strings(struct kunit *test)
David Brazdil0f672f62019-12-10 10:32:29 +0000650{
651 char *ptr;
652 size_t size = 24;
653
Olivier Deprez157378f2022-04-04 15:47:50 +0200654 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
655 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
656 kunit_info(test,
657 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
David Brazdil0f672f62019-12-10 10:32:29 +0000658 return;
Olivier Deprez157378f2022-04-04 15:47:50 +0200659 }
660
661 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
662 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
David Brazdil0f672f62019-12-10 10:32:29 +0000663
664 kfree(ptr);
665
666 /*
667 * Try to cause only 1 invalid access (less spam in dmesg).
668 * For that we need ptr to point to zeroed byte.
669 * Skip metadata that could be stored in freed object so ptr
670 * will likely point to zeroed byte.
671 */
672 ptr += 16;
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
David Brazdil0f672f62019-12-10 10:32:29 +0000674
Olivier Deprez157378f2022-04-04 15:47:50 +0200675 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
David Brazdil0f672f62019-12-10 10:32:29 +0000676
Olivier Deprez157378f2022-04-04 15:47:50 +0200677 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
David Brazdil0f672f62019-12-10 10:32:29 +0000678
Olivier Deprez157378f2022-04-04 15:47:50 +0200679 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
David Brazdil0f672f62019-12-10 10:32:29 +0000680
Olivier Deprez157378f2022-04-04 15:47:50 +0200681 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
David Brazdil0f672f62019-12-10 10:32:29 +0000682
Olivier Deprez157378f2022-04-04 15:47:50 +0200683 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
David Brazdil0f672f62019-12-10 10:32:29 +0000684}
685
Olivier Deprez157378f2022-04-04 15:47:50 +0200686static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
David Brazdil0f672f62019-12-10 10:32:29 +0000687{
Olivier Deprez157378f2022-04-04 15:47:50 +0200688 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
689 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
690 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
691 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
692 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
693 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
694 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
695 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
696}
697
698static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
699{
700 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
701 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
702 KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
703 KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
704 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
705 KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
706 KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
707 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
708
709#if defined(clear_bit_unlock_is_negative_byte)
710 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
711 clear_bit_unlock_is_negative_byte(nr, addr));
712#endif
713}
714
715static void kasan_bitops_generic(struct kunit *test)
716{
717 long *bits;
718
719 /* This test is specifically crafted for the generic mode. */
720 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
721 kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
722 return;
723 }
724
David Brazdil0f672f62019-12-10 10:32:29 +0000725 /*
726 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
727 * this way we do not actually corrupt other memory.
728 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200729 bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
730 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
David Brazdil0f672f62019-12-10 10:32:29 +0000731
732 /*
733 * Below calls try to access bit within allocated memory; however, the
734 * below accesses are still out-of-bounds, since bitops are defined to
735 * operate on the whole long the bit is in.
736 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200737 kasan_bitops_modify(test, BITS_PER_LONG, bits);
David Brazdil0f672f62019-12-10 10:32:29 +0000738
739 /*
740 * Below calls try to access bit beyond allocated memory.
741 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200742 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
David Brazdil0f672f62019-12-10 10:32:29 +0000743
David Brazdil0f672f62019-12-10 10:32:29 +0000744 kfree(bits);
745}
746
Olivier Deprez157378f2022-04-04 15:47:50 +0200747static void kasan_bitops_tags(struct kunit *test)
748{
749 long *bits;
750
751 /* This test is specifically crafted for the tag-based mode. */
752 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
753 kunit_info(test, "CONFIG_KASAN_SW_TAGS required\n");
754 return;
755 }
756
757 /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
758 bits = kzalloc(48, GFP_KERNEL);
759 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
760
761 /* Do the accesses past the 48 allocated bytes, but within the redone. */
762 kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
763 kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
764
765 kfree(bits);
766}
767
768static void kmalloc_double_kzfree(struct kunit *test)
David Brazdil0f672f62019-12-10 10:32:29 +0000769{
770 char *ptr;
771 size_t size = 16;
772
David Brazdil0f672f62019-12-10 10:32:29 +0000773 ptr = kmalloc(size, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200774 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
775
776 kfree_sensitive(ptr);
777 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
778}
779
780static void vmalloc_oob(struct kunit *test)
781{
782 void *area;
783
784 if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
785 kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled.");
David Brazdil0f672f62019-12-10 10:32:29 +0000786 return;
787 }
788
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200790 * We have to be careful not to hit the guard page.
791 * The MMU will catch that and crash us.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000792 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200793 area = vmalloc(3000);
794 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795
Olivier Deprez157378f2022-04-04 15:47:50 +0200796 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
797 vfree(area);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000798}
799
Olivier Deprez157378f2022-04-04 15:47:50 +0200800static struct kunit_case kasan_kunit_test_cases[] = {
801 KUNIT_CASE(kmalloc_oob_right),
802 KUNIT_CASE(kmalloc_oob_left),
803 KUNIT_CASE(kmalloc_node_oob_right),
804 KUNIT_CASE(kmalloc_pagealloc_oob_right),
805 KUNIT_CASE(kmalloc_pagealloc_uaf),
806 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
807 KUNIT_CASE(kmalloc_large_oob_right),
808 KUNIT_CASE(kmalloc_oob_krealloc_more),
809 KUNIT_CASE(kmalloc_oob_krealloc_less),
810 KUNIT_CASE(kmalloc_oob_16),
811 KUNIT_CASE(kmalloc_uaf_16),
812 KUNIT_CASE(kmalloc_oob_in_memset),
813 KUNIT_CASE(kmalloc_oob_memset_2),
814 KUNIT_CASE(kmalloc_oob_memset_4),
815 KUNIT_CASE(kmalloc_oob_memset_8),
816 KUNIT_CASE(kmalloc_oob_memset_16),
817 KUNIT_CASE(kmalloc_memmove_invalid_size),
818 KUNIT_CASE(kmalloc_uaf),
819 KUNIT_CASE(kmalloc_uaf_memset),
820 KUNIT_CASE(kmalloc_uaf2),
821 KUNIT_CASE(kfree_via_page),
822 KUNIT_CASE(kfree_via_phys),
823 KUNIT_CASE(kmem_cache_oob),
824 KUNIT_CASE(memcg_accounted_kmem_cache),
825 KUNIT_CASE(kasan_global_oob),
826 KUNIT_CASE(kasan_stack_oob),
827 KUNIT_CASE(kasan_alloca_oob_left),
828 KUNIT_CASE(kasan_alloca_oob_right),
829 KUNIT_CASE(ksize_unpoisons_memory),
830 KUNIT_CASE(kmem_cache_double_free),
831 KUNIT_CASE(kmem_cache_invalid_free),
832 KUNIT_CASE(kasan_memchr),
833 KUNIT_CASE(kasan_memcmp),
834 KUNIT_CASE(kasan_strings),
835 KUNIT_CASE(kasan_bitops_generic),
836 KUNIT_CASE(kasan_bitops_tags),
837 KUNIT_CASE(kmalloc_double_kzfree),
838 KUNIT_CASE(vmalloc_oob),
839 {}
840};
841
842static struct kunit_suite kasan_kunit_test_suite = {
843 .name = "kasan",
844 .init = kasan_test_init,
845 .test_cases = kasan_kunit_test_cases,
846 .exit = kasan_test_exit,
847};
848
849kunit_test_suite(kasan_kunit_test_suite);
850
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000851MODULE_LICENSE("GPL");