Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
| 5 | * Author: Andrey Ryabinin <a.ryabinin@samsung.com> |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 8 | #include <linux/bitops.h> |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 9 | #include <linux/delay.h> |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 10 | #include <linux/kasan.h> |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 13 | #include <linux/mman.h> |
| 14 | #include <linux/module.h> |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 15 | #include <linux/printk.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/string.h> |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 19 | #include <linux/io.h> |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 20 | #include <linux/vmalloc.h> |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/page.h> |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 23 | |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 24 | #include <kunit/test.h> |
| 25 | |
Walter Wu | f33a014 | 2020-08-06 23:24:54 -0700 | [diff] [blame] | 26 | #include "../mm/kasan/kasan.h" |
| 27 | |
| 28 | #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE) |
| 29 | |
Dmitry Vyukov | 828347f | 2016-11-30 15:54:16 -0800 | [diff] [blame] | 30 | /* |
Daniel Axtens | adb72ae | 2020-06-03 15:56:43 -0700 | [diff] [blame] | 31 | * We assign some test results to these globals to make sure the tests |
| 32 | * are not eliminated as dead code. |
| 33 | */ |
| 34 | |
Daniel Axtens | adb72ae | 2020-06-03 15:56:43 -0700 | [diff] [blame] | 35 | void *kasan_ptr_result; |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 36 | int kasan_int_result; |
| 37 | |
| 38 | static struct kunit_resource resource; |
| 39 | static struct kunit_kasan_expectation fail_data; |
| 40 | static bool multishot; |
| 41 | |
| 42 | static 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 | |
| 53 | static 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 | * |
| 63 | */ |
| 64 | #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) |
| 77 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 78 | static void kmalloc_oob_right(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 79 | { |
| 80 | char *ptr; |
| 81 | size_t size = 123; |
| 82 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 83 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 84 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 85 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 86 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x'); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 87 | kfree(ptr); |
| 88 | } |
| 89 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 90 | static void kmalloc_oob_left(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 91 | { |
| 92 | char *ptr; |
| 93 | size_t size = 15; |
| 94 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 95 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 96 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 97 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 98 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1)); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 99 | kfree(ptr); |
| 100 | } |
| 101 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 102 | static void kmalloc_node_oob_right(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 103 | { |
| 104 | char *ptr; |
| 105 | size_t size = 4096; |
| 106 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 107 | ptr = kmalloc_node(size, GFP_KERNEL, 0); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 108 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 109 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 110 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 111 | kfree(ptr); |
| 112 | } |
| 113 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 114 | static void kmalloc_pagealloc_oob_right(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 115 | { |
| 116 | char *ptr; |
| 117 | size_t size = KMALLOC_MAX_CACHE_SIZE + 10; |
| 118 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 119 | if (!IS_ENABLED(CONFIG_SLUB)) { |
| 120 | kunit_info(test, "CONFIG_SLUB is not enabled."); |
| 121 | return; |
| 122 | } |
| 123 | |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 124 | /* Allocate a chunk that does not fit into a SLUB cache to trigger |
| 125 | * the page allocator fallback. |
| 126 | */ |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 127 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 128 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 129 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 130 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0); |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 131 | kfree(ptr); |
| 132 | } |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 133 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 134 | static void kmalloc_pagealloc_uaf(struct kunit *test) |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 135 | { |
| 136 | char *ptr; |
| 137 | size_t size = KMALLOC_MAX_CACHE_SIZE + 10; |
| 138 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 139 | if (!IS_ENABLED(CONFIG_SLUB)) { |
| 140 | kunit_info(test, "CONFIG_SLUB is not enabled."); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 144 | ptr = kmalloc(size, GFP_KERNEL); |
| 145 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 146 | |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 147 | kfree(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 148 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 151 | static void kmalloc_pagealloc_invalid_free(struct kunit *test) |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 152 | { |
| 153 | char *ptr; |
| 154 | size_t size = KMALLOC_MAX_CACHE_SIZE + 10; |
| 155 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 156 | if (!IS_ENABLED(CONFIG_SLUB)) { |
| 157 | kunit_info(test, "CONFIG_SLUB is not enabled."); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 161 | ptr = kmalloc(size, GFP_KERNEL); |
| 162 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 163 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 164 | KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1)); |
| 165 | } |
| 166 | |
| 167 | static void kmalloc_large_oob_right(struct kunit *test) |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 168 | { |
| 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 | */ |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 174 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 175 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 176 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 177 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 178 | kfree(ptr); |
| 179 | } |
| 180 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 181 | static void kmalloc_oob_krealloc_more(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 182 | { |
| 183 | char *ptr1, *ptr2; |
| 184 | size_t size1 = 17; |
| 185 | size_t size2 = 19; |
| 186 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 187 | ptr1 = kmalloc(size1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 188 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 189 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 190 | ptr2 = krealloc(ptr1, size2, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 191 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 192 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 193 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x'); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 194 | kfree(ptr2); |
| 195 | } |
| 196 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 197 | static void kmalloc_oob_krealloc_less(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 198 | { |
| 199 | char *ptr1, *ptr2; |
| 200 | size_t size1 = 17; |
| 201 | size_t size2 = 15; |
| 202 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 203 | ptr1 = kmalloc(size1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 204 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 205 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 206 | ptr2 = krealloc(ptr1, size2, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 207 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
Walter Wu | f33a014 | 2020-08-06 23:24:54 -0700 | [diff] [blame] | 208 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 209 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x'); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 210 | kfree(ptr2); |
| 211 | } |
| 212 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 213 | static void kmalloc_oob_16(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 214 | { |
| 215 | struct { |
| 216 | u64 words[2]; |
| 217 | } *ptr1, *ptr2; |
| 218 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 219 | /* 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"); |
| 222 | return; |
| 223 | } |
| 224 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 225 | ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 226 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 227 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 228 | ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 229 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
| 230 | |
| 231 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 232 | kfree(ptr1); |
| 233 | kfree(ptr2); |
| 234 | } |
| 235 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 236 | static 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 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 253 | static void kmalloc_oob_memset_2(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 254 | { |
| 255 | char *ptr; |
| 256 | size_t size = 8; |
| 257 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 258 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 259 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 260 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 261 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2)); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 262 | kfree(ptr); |
| 263 | } |
| 264 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 265 | static void kmalloc_oob_memset_4(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 266 | { |
| 267 | char *ptr; |
| 268 | size_t size = 8; |
| 269 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 270 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 271 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 272 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 273 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4)); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 274 | kfree(ptr); |
| 275 | } |
| 276 | |
| 277 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 278 | static void kmalloc_oob_memset_8(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 279 | { |
| 280 | char *ptr; |
| 281 | size_t size = 8; |
| 282 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 283 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 284 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 285 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 286 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8)); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 287 | kfree(ptr); |
| 288 | } |
| 289 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 290 | static void kmalloc_oob_memset_16(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 291 | { |
| 292 | char *ptr; |
| 293 | size_t size = 16; |
| 294 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 295 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 296 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 297 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 298 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16)); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 299 | kfree(ptr); |
| 300 | } |
| 301 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 302 | static void kmalloc_oob_in_memset(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 303 | { |
| 304 | char *ptr; |
| 305 | size_t size = 666; |
| 306 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 307 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 308 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 309 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 310 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF)); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 311 | kfree(ptr); |
| 312 | } |
| 313 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 314 | static void kmalloc_memmove_invalid_size(struct kunit *test) |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 315 | { |
| 316 | char *ptr; |
| 317 | size_t size = 64; |
| 318 | volatile size_t invalid_size = -2; |
| 319 | |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 320 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 321 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 322 | |
| 323 | memset((char *)ptr, 0, 64); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 324 | |
| 325 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 326 | memmove((char *)ptr, (char *)ptr + 4, invalid_size)); |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 327 | kfree(ptr); |
| 328 | } |
| 329 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 330 | static void kmalloc_uaf(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 331 | { |
| 332 | char *ptr; |
| 333 | size_t size = 10; |
| 334 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 335 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 336 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 337 | |
| 338 | kfree(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 339 | KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x'); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 342 | static void kmalloc_uaf_memset(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 343 | { |
| 344 | char *ptr; |
| 345 | size_t size = 33; |
| 346 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 347 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 348 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 349 | |
| 350 | kfree(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 351 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size)); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 354 | static void kmalloc_uaf2(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 355 | { |
| 356 | char *ptr1, *ptr2; |
| 357 | size_t size = 43; |
| 358 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 359 | ptr1 = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 360 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 361 | |
| 362 | kfree(ptr1); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 363 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 364 | 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 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 370 | kfree(ptr2); |
| 371 | } |
| 372 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 373 | static void kfree_via_page(struct kunit *test) |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 374 | { |
| 375 | char *ptr; |
| 376 | size_t size = 8; |
| 377 | struct page *page; |
| 378 | unsigned long offset; |
| 379 | |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 380 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 381 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 382 | |
| 383 | page = virt_to_page(ptr); |
| 384 | offset = offset_in_page(ptr); |
| 385 | kfree(page_address(page) + offset); |
| 386 | } |
| 387 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 388 | static void kfree_via_phys(struct kunit *test) |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 389 | { |
| 390 | char *ptr; |
| 391 | size_t size = 8; |
| 392 | phys_addr_t phys; |
| 393 | |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 394 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 395 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 396 | |
| 397 | phys = virt_to_phys(ptr); |
| 398 | kfree(phys_to_virt(phys)); |
| 399 | } |
| 400 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 401 | static void kmem_cache_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 402 | { |
| 403 | char *p; |
| 404 | size_t size = 200; |
| 405 | struct kmem_cache *cache = kmem_cache_create("test_cache", |
| 406 | size, 0, |
| 407 | 0, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 408 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 409 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 410 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 411 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 412 | kmem_cache_destroy(cache); |
| 413 | return; |
| 414 | } |
| 415 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 416 | KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 417 | kmem_cache_free(cache, p); |
| 418 | kmem_cache_destroy(cache); |
| 419 | } |
| 420 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 421 | static void memcg_accounted_kmem_cache(struct kunit *test) |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 422 | { |
| 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); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 429 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 430 | |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 431 | /* |
| 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); |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 437 | if (!p) |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 438 | goto free_cache; |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 439 | |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 440 | kmem_cache_free(cache, p); |
| 441 | msleep(100); |
| 442 | } |
| 443 | |
| 444 | free_cache: |
| 445 | kmem_cache_destroy(cache); |
| 446 | } |
| 447 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 448 | static char global_array[10]; |
| 449 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 450 | static void kasan_global_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 451 | { |
Peter Collingbourne | fe5c0a6 | 2021-05-14 17:27:27 -0700 | [diff] [blame] | 452 | /* |
| 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]; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 466 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 467 | /* Only generic mode instruments globals. */ |
| 468 | if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { |
| 469 | kunit_info(test, "CONFIG_KASAN_GENERIC required"); |
| 470 | return; |
| 471 | } |
| 472 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 473 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 476 | static void ksize_unpoisons_memory(struct kunit *test) |
| 477 | { |
| 478 | char *ptr; |
| 479 | size_t size = 123, real_size; |
| 480 | |
| 481 | ptr = kmalloc(size, GFP_KERNEL); |
| 482 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 483 | real_size = ksize(ptr); |
| 484 | /* This access doesn't trigger an error. */ |
| 485 | ptr[size] = 'x'; |
| 486 | /* This one does. */ |
| 487 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y'); |
| 488 | kfree(ptr); |
| 489 | } |
| 490 | |
| 491 | static void kasan_stack_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 492 | { |
| 493 | char stack_array[10]; |
Peter Collingbourne | fe5c0a6 | 2021-05-14 17:27:27 -0700 | [diff] [blame] | 494 | /* See comment in kasan_global_oob. */ |
| 495 | char *volatile array = stack_array; |
| 496 | char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF]; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 497 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 498 | if (!IS_ENABLED(CONFIG_KASAN_STACK)) { |
| 499 | kunit_info(test, "CONFIG_KASAN_STACK is not enabled"); |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 503 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 506 | static void kasan_alloca_oob_left(struct kunit *test) |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 507 | { |
| 508 | volatile int i = 10; |
| 509 | char alloca_array[i]; |
Peter Collingbourne | fe5c0a6 | 2021-05-14 17:27:27 -0700 | [diff] [blame] | 510 | /* See comment in kasan_global_oob. */ |
| 511 | char *volatile array = alloca_array; |
| 512 | char *p = array - 1; |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 513 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 514 | /* 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 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 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); |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 528 | static void kasan_alloca_oob_right(struct kunit *test) |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 529 | { |
| 530 | volatile int i = 10; |
| 531 | char alloca_array[i]; |
Peter Collingbourne | fe5c0a6 | 2021-05-14 17:27:27 -0700 | [diff] [blame] | 532 | /* See comment in kasan_global_oob. */ |
| 533 | char *volatile array = alloca_array; |
| 534 | char *p = array + i; |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 535 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 536 | /* 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 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 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); |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 550 | static void kmem_cache_double_free(struct kunit *test) |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 551 | { |
| 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); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 557 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 558 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 559 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 560 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 561 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 562 | kmem_cache_destroy(cache); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | kmem_cache_free(cache, p); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 567 | KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p)); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 568 | kmem_cache_destroy(cache); |
| 569 | } |
| 570 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 571 | static void kmem_cache_invalid_free(struct kunit *test) |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 572 | { |
| 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); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 579 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 580 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 581 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 582 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 583 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 584 | kmem_cache_destroy(cache); |
| 585 | return; |
| 586 | } |
| 587 | |
Andrey Konovalov | 91c93ed | 2018-04-10 16:30:35 -0700 | [diff] [blame] | 588 | /* Trigger invalid free, the object doesn't get freed */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 589 | KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1)); |
Andrey Konovalov | 91c93ed | 2018-04-10 16:30:35 -0700 | [diff] [blame] | 590 | |
| 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 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 597 | kmem_cache_destroy(cache); |
| 598 | } |
| 599 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 600 | static void kasan_memchr(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 601 | { |
| 602 | char *ptr; |
| 603 | size_t size = 24; |
| 604 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 605 | /* 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"); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 609 | return; |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 610 | } |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 611 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 612 | if (OOB_TAG_OFF) |
| 613 | size = round_up(size, OOB_TAG_OFF); |
| 614 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 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 | |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 621 | kfree(ptr); |
| 622 | } |
| 623 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 624 | static void kasan_memcmp(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 625 | { |
| 626 | char *ptr; |
| 627 | size_t size = 24; |
| 628 | int arr[9]; |
| 629 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 630 | /* 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"); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 634 | return; |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 635 | } |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 636 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 637 | if (OOB_TAG_OFF) |
| 638 | size = round_up(size, OOB_TAG_OFF); |
| 639 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 640 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 641 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 642 | memset(arr, 0, sizeof(arr)); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 643 | |
| 644 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 645 | kasan_int_result = memcmp(ptr, arr, size+1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 646 | kfree(ptr); |
| 647 | } |
| 648 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 649 | static void kasan_strings(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 650 | { |
| 651 | char *ptr; |
| 652 | size_t size = 24; |
| 653 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 654 | /* 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"); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 658 | return; |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 662 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 663 | |
| 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; |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 673 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1')); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 674 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 675 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1')); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 676 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 677 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2")); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 678 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 679 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 680 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 681 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 682 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 683 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 686 | static void kasan_bitops_modify(struct kunit *test, int nr, void *addr) |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 687 | { |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 688 | 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 | |
| 698 | static 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 | |
| 715 | static 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 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 725 | /* |
| 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 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 729 | bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 730 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 731 | |
| 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 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 737 | kasan_bitops_modify(test, BITS_PER_LONG, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 738 | |
| 739 | /* |
| 740 | * Below calls try to access bit beyond allocated memory. |
| 741 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 742 | kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 743 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 744 | kfree(bits); |
| 745 | } |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 746 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 747 | static void kasan_bitops_tags(struct kunit *test) |
| 748 | { |
| 749 | long *bits; |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 750 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 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 | } |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 756 | |
Andrey Konovalov | 219fc4b | 2021-02-24 12:05:42 -0800 | [diff] [blame] | 757 | /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */ |
| 758 | bits = kzalloc(48, GFP_KERNEL); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 759 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 760 | |
Andrey Konovalov | 219fc4b | 2021-02-24 12:05:42 -0800 | [diff] [blame] | 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); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 764 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 765 | kfree(bits); |
| 766 | } |
| 767 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 768 | static void kmalloc_double_kzfree(struct kunit *test) |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 769 | { |
| 770 | char *ptr; |
| 771 | size_t size = 16; |
| 772 | |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 773 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 774 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 775 | |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 776 | kfree_sensitive(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 777 | KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr)); |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 780 | static void vmalloc_oob(struct kunit *test) |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 781 | { |
| 782 | void *area; |
| 783 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 784 | if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) { |
| 785 | kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled."); |
| 786 | return; |
| 787 | } |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * We have to be careful not to hit the guard page. |
| 791 | * The MMU will catch that and crash us. |
| 792 | */ |
| 793 | area = vmalloc(3000); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 794 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 795 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 796 | KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 797 | vfree(area); |
| 798 | } |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 799 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 800 | static 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), |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 811 | KUNIT_CASE(kmalloc_uaf_16), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 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), |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 835 | KUNIT_CASE(kasan_bitops_generic), |
| 836 | KUNIT_CASE(kasan_bitops_tags), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 837 | KUNIT_CASE(kmalloc_double_kzfree), |
| 838 | KUNIT_CASE(vmalloc_oob), |
| 839 | {} |
| 840 | }; |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 841 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 842 | static 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 | }; |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 848 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 849 | kunit_test_suite(kasan_kunit_test_suite); |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 850 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 851 | MODULE_LICENSE("GPL"); |