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> |
Andrey Konovalov | 782ba45 | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 16 | #include <linux/random.h> |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/string.h> |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 20 | #include <linux/io.h> |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/page.h> |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 24 | |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 25 | #include <kunit/test.h> |
| 26 | |
Walter Wu | f33a014 | 2020-08-06 23:24:54 -0700 | [diff] [blame] | 27 | #include "../mm/kasan/kasan.h" |
| 28 | |
Andrey Konovalov | 70585d9 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 29 | #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE) |
Walter Wu | f33a014 | 2020-08-06 23:24:54 -0700 | [diff] [blame] | 30 | |
Dmitry Vyukov | 828347f | 2016-11-30 15:54:16 -0800 | [diff] [blame] | 31 | /* |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 32 | * 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 Axtens | adb72ae | 2020-06-03 15:56:43 -0700 | [diff] [blame] | 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 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 42 | /* |
| 43 | * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 44 | * 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 Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 47 | */ |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 48 | static int kasan_test_init(struct kunit *test) |
| 49 | { |
Andrey Konovalov | bdf2f9b | 2021-02-03 15:35:07 +1100 | [diff] [blame] | 50 | if (!kasan_enabled()) { |
| 51 | kunit_err(test, "can't run KASAN tests with KASAN disabled"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 55 | multishot = kasan_save_enable_multi_shot(); |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 56 | hw_set_tagging_report_once(false); |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static void kasan_test_exit(struct kunit *test) |
| 61 | { |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 62 | hw_set_tagging_report_once(true); |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 63 | kasan_restore_multi_shot(multishot); |
| 64 | } |
| 65 | |
| 66 | /** |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 67 | * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a |
| 68 | * KASAN report; causes a test failure otherwise. This relies on a KUnit |
| 69 | * resource named "kasan_data". Do not use this name for KUnit resources |
| 70 | * outside of KASAN tests. |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 71 | * |
| 72 | * For hardware tag-based KASAN, when a tag fault happens, tag checking is |
| 73 | * normally auto-disabled. When this happens, this test handler reenables |
| 74 | * tag checking. As tag checking can be only disabled or enabled per CPU, this |
| 75 | * handler disables migration (preemption). |
Andrey Konovalov | 7095a8f | 2021-02-03 15:35:03 +1100 | [diff] [blame] | 76 | * |
| 77 | * Since the compiler doesn't see that the expression can change the fail_data |
| 78 | * fields, it can reorder or optimize away the accesses to those fields. |
| 79 | * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the |
| 80 | * expression to prevent that. |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 81 | */ |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 82 | #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \ |
| 83 | if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \ |
| 84 | migrate_disable(); \ |
Andrey Konovalov | 7095a8f | 2021-02-03 15:35:03 +1100 | [diff] [blame] | 85 | WRITE_ONCE(fail_data.report_expected, true); \ |
| 86 | WRITE_ONCE(fail_data.report_found, false); \ |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 87 | kunit_add_named_resource(test, \ |
| 88 | NULL, \ |
| 89 | NULL, \ |
| 90 | &resource, \ |
| 91 | "kasan_data", &fail_data); \ |
Andrey Konovalov | 7095a8f | 2021-02-03 15:35:03 +1100 | [diff] [blame] | 92 | barrier(); \ |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 93 | expression; \ |
Andrey Konovalov | 7095a8f | 2021-02-03 15:35:03 +1100 | [diff] [blame] | 94 | barrier(); \ |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 95 | KUNIT_EXPECT_EQ(test, \ |
Andrey Konovalov | 7095a8f | 2021-02-03 15:35:03 +1100 | [diff] [blame] | 96 | READ_ONCE(fail_data.report_expected), \ |
| 97 | READ_ONCE(fail_data.report_found)); \ |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 98 | if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \ |
Andrey Konovalov | 7095a8f | 2021-02-03 15:35:03 +1100 | [diff] [blame] | 99 | if (READ_ONCE(fail_data.report_found)) \ |
Andrey Konovalov | a599a4e | 2021-02-03 15:35:02 +1100 | [diff] [blame] | 100 | hw_enable_tagging(); \ |
| 101 | migrate_enable(); \ |
| 102 | } \ |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 103 | } while (0) |
| 104 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 105 | #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \ |
| 106 | if (!IS_ENABLED(config)) { \ |
| 107 | kunit_info((test), "skipping, " #config " required"); \ |
| 108 | return; \ |
| 109 | } \ |
| 110 | } while (0) |
| 111 | |
| 112 | #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \ |
| 113 | if (IS_ENABLED(config)) { \ |
| 114 | kunit_info((test), "skipping, " #config " enabled"); \ |
| 115 | return; \ |
| 116 | } \ |
| 117 | } while (0) |
| 118 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 119 | static void kmalloc_oob_right(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 120 | { |
| 121 | char *ptr; |
| 122 | size_t size = 123; |
| 123 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 124 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 125 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 126 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 127 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x'); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 128 | kfree(ptr); |
| 129 | } |
| 130 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 131 | static void kmalloc_oob_left(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 132 | { |
| 133 | char *ptr; |
| 134 | size_t size = 15; |
| 135 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 136 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 137 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 138 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 139 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1)); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 140 | kfree(ptr); |
| 141 | } |
| 142 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 143 | static void kmalloc_node_oob_right(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 144 | { |
| 145 | char *ptr; |
| 146 | size_t size = 4096; |
| 147 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 148 | ptr = kmalloc_node(size, GFP_KERNEL, 0); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 149 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 150 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 151 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 152 | kfree(ptr); |
| 153 | } |
| 154 | |
Andrey Konovalov | e449e27 | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 155 | /* |
| 156 | * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't |
| 157 | * fit into a slab cache and therefore is allocated via the page allocator |
| 158 | * fallback. Since this kind of fallback is only implemented for SLUB, these |
| 159 | * tests are limited to that allocator. |
| 160 | */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 161 | static void kmalloc_pagealloc_oob_right(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 162 | { |
| 163 | char *ptr; |
| 164 | size_t size = KMALLOC_MAX_CACHE_SIZE + 10; |
| 165 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 166 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 167 | |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 168 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 169 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 170 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 171 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0); |
Andrey Konovalov | e449e27 | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 172 | |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 173 | kfree(ptr); |
| 174 | } |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 175 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 176 | static void kmalloc_pagealloc_uaf(struct kunit *test) |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 177 | { |
| 178 | char *ptr; |
| 179 | size_t size = KMALLOC_MAX_CACHE_SIZE + 10; |
| 180 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 181 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 182 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 183 | ptr = kmalloc(size, GFP_KERNEL); |
| 184 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 185 | kfree(ptr); |
Andrey Konovalov | e449e27 | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 186 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 187 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 190 | static void kmalloc_pagealloc_invalid_free(struct kunit *test) |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 191 | { |
| 192 | char *ptr; |
| 193 | size_t size = KMALLOC_MAX_CACHE_SIZE + 10; |
| 194 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 195 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); |
Dmitry Vyukov | 47adccc | 2018-02-06 15:36:23 -0800 | [diff] [blame] | 196 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 197 | ptr = kmalloc(size, GFP_KERNEL); |
| 198 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 199 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 200 | KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1)); |
| 201 | } |
| 202 | |
Andrey Konovalov | e449e27 | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 203 | static void pagealloc_oob_right(struct kunit *test) |
| 204 | { |
| 205 | char *ptr; |
| 206 | struct page *pages; |
| 207 | size_t order = 4; |
| 208 | size_t size = (1UL << (PAGE_SHIFT + order)); |
| 209 | |
| 210 | /* |
| 211 | * With generic KASAN page allocations have no redzones, thus |
| 212 | * out-of-bounds detection is not guaranteed. |
| 213 | * See https://bugzilla.kernel.org/show_bug.cgi?id=210503. |
| 214 | */ |
| 215 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 216 | |
| 217 | pages = alloc_pages(GFP_KERNEL, order); |
| 218 | ptr = page_address(pages); |
| 219 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 220 | |
| 221 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0); |
| 222 | free_pages((unsigned long)ptr, order); |
| 223 | } |
| 224 | |
| 225 | static void pagealloc_uaf(struct kunit *test) |
| 226 | { |
| 227 | char *ptr; |
| 228 | struct page *pages; |
| 229 | size_t order = 4; |
| 230 | |
| 231 | pages = alloc_pages(GFP_KERNEL, order); |
| 232 | ptr = page_address(pages); |
| 233 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 234 | free_pages((unsigned long)ptr, order); |
| 235 | |
| 236 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0); |
| 237 | } |
| 238 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 239 | static void kmalloc_large_oob_right(struct kunit *test) |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 240 | { |
| 241 | char *ptr; |
| 242 | size_t size = KMALLOC_MAX_CACHE_SIZE - 256; |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Allocate a chunk that is large enough, but still fits into a slab |
Alexander Potapenko | e6e8379 | 2016-03-25 14:21:56 -0700 | [diff] [blame] | 246 | * and does not trigger the page allocator fallback in SLUB. |
| 247 | */ |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 248 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 249 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 250 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 251 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 252 | kfree(ptr); |
| 253 | } |
| 254 | |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 255 | static void krealloc_more_oob_helper(struct kunit *test, |
| 256 | size_t size1, size_t size2) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 257 | { |
| 258 | char *ptr1, *ptr2; |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 259 | size_t middle; |
| 260 | |
| 261 | KUNIT_ASSERT_LT(test, size1, size2); |
| 262 | middle = size1 + (size2 - size1) / 2; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 263 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 264 | ptr1 = kmalloc(size1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 265 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 266 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 267 | ptr2 = krealloc(ptr1, size2, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 268 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 269 | |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 270 | /* All offsets up to size2 must be accessible. */ |
| 271 | ptr2[size1 - 1] = 'x'; |
| 272 | ptr2[size1] = 'x'; |
| 273 | ptr2[middle] = 'x'; |
| 274 | ptr2[size2 - 1] = 'x'; |
| 275 | |
| 276 | /* Generic mode is precise, so unaligned size2 must be inaccessible. */ |
| 277 | if (IS_ENABLED(CONFIG_KASAN_GENERIC)) |
| 278 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x'); |
| 279 | |
| 280 | /* For all modes first aligned offset after size2 must be inaccessible. */ |
| 281 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 282 | ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x'); |
| 283 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 284 | kfree(ptr2); |
| 285 | } |
| 286 | |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 287 | static void krealloc_less_oob_helper(struct kunit *test, |
| 288 | size_t size1, size_t size2) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 289 | { |
| 290 | char *ptr1, *ptr2; |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 291 | size_t middle; |
| 292 | |
| 293 | KUNIT_ASSERT_LT(test, size2, size1); |
| 294 | middle = size2 + (size1 - size2) / 2; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 295 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 296 | ptr1 = kmalloc(size1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 297 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 298 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 299 | ptr2 = krealloc(ptr1, size2, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 300 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
Walter Wu | f33a014 | 2020-08-06 23:24:54 -0700 | [diff] [blame] | 301 | |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 302 | /* Must be accessible for all modes. */ |
| 303 | ptr2[size2 - 1] = 'x'; |
| 304 | |
| 305 | /* Generic mode is precise, so unaligned size2 must be inaccessible. */ |
| 306 | if (IS_ENABLED(CONFIG_KASAN_GENERIC)) |
| 307 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x'); |
| 308 | |
| 309 | /* For all modes first aligned offset after size2 must be inaccessible. */ |
| 310 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 311 | ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x'); |
| 312 | |
| 313 | /* |
| 314 | * For all modes all size2, middle, and size1 should land in separate |
| 315 | * granules and thus the latter two offsets should be inaccessible. |
| 316 | */ |
| 317 | KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE), |
| 318 | round_down(middle, KASAN_GRANULE_SIZE)); |
| 319 | KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE), |
| 320 | round_down(size1, KASAN_GRANULE_SIZE)); |
| 321 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x'); |
| 322 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x'); |
| 323 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x'); |
| 324 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 325 | kfree(ptr2); |
| 326 | } |
| 327 | |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 328 | static void krealloc_more_oob(struct kunit *test) |
| 329 | { |
| 330 | krealloc_more_oob_helper(test, 201, 235); |
| 331 | } |
| 332 | |
| 333 | static void krealloc_less_oob(struct kunit *test) |
| 334 | { |
| 335 | krealloc_less_oob_helper(test, 235, 201); |
| 336 | } |
| 337 | |
| 338 | static void krealloc_pagealloc_more_oob(struct kunit *test) |
| 339 | { |
| 340 | /* page_alloc fallback in only implemented for SLUB. */ |
| 341 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); |
| 342 | |
| 343 | krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201, |
| 344 | KMALLOC_MAX_CACHE_SIZE + 235); |
| 345 | } |
| 346 | |
| 347 | static void krealloc_pagealloc_less_oob(struct kunit *test) |
| 348 | { |
| 349 | /* page_alloc fallback in only implemented for SLUB. */ |
| 350 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); |
| 351 | |
| 352 | krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235, |
| 353 | KMALLOC_MAX_CACHE_SIZE + 201); |
| 354 | } |
| 355 | |
Andrey Konovalov | 4f62c69 | 2021-02-12 17:14:50 +1100 | [diff] [blame^] | 356 | /* |
| 357 | * Check that krealloc() detects a use-after-free, returns NULL, |
| 358 | * and doesn't unpoison the freed object. |
| 359 | */ |
| 360 | static void krealloc_uaf(struct kunit *test) |
| 361 | { |
| 362 | char *ptr1, *ptr2; |
| 363 | int size1 = 201; |
| 364 | int size2 = 235; |
| 365 | |
| 366 | ptr1 = kmalloc(size1, GFP_KERNEL); |
| 367 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 368 | kfree(ptr1); |
| 369 | |
| 370 | KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL)); |
| 371 | KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL); |
| 372 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1); |
| 373 | } |
| 374 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 375 | static void kmalloc_oob_16(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 376 | { |
| 377 | struct { |
| 378 | u64 words[2]; |
| 379 | } *ptr1, *ptr2; |
| 380 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 381 | /* This test is specifically crafted for the generic mode. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 382 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 383 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 384 | ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 385 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 386 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 387 | ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 388 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
| 389 | |
| 390 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 391 | kfree(ptr1); |
| 392 | kfree(ptr2); |
| 393 | } |
| 394 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 395 | static void kmalloc_uaf_16(struct kunit *test) |
| 396 | { |
| 397 | struct { |
| 398 | u64 words[2]; |
| 399 | } *ptr1, *ptr2; |
| 400 | |
| 401 | ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL); |
| 402 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
| 403 | |
| 404 | ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL); |
| 405 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
| 406 | kfree(ptr2); |
| 407 | |
| 408 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2); |
| 409 | kfree(ptr1); |
| 410 | } |
| 411 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 412 | static void kmalloc_oob_memset_2(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 413 | { |
| 414 | char *ptr; |
| 415 | size_t size = 8; |
| 416 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 417 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 418 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 419 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 420 | 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] | 421 | kfree(ptr); |
| 422 | } |
| 423 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 424 | static void kmalloc_oob_memset_4(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 425 | { |
| 426 | char *ptr; |
| 427 | size_t size = 8; |
| 428 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 429 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 430 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 431 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 432 | 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] | 433 | kfree(ptr); |
| 434 | } |
| 435 | |
| 436 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 437 | static void kmalloc_oob_memset_8(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 438 | { |
| 439 | char *ptr; |
| 440 | size_t size = 8; |
| 441 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 442 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 443 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 444 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 445 | 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] | 446 | kfree(ptr); |
| 447 | } |
| 448 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 449 | static void kmalloc_oob_memset_16(struct kunit *test) |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 450 | { |
| 451 | char *ptr; |
| 452 | size_t size = 16; |
| 453 | |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 454 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 455 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Wang Long | f523e73 | 2015-11-05 18:51:15 -0800 | [diff] [blame] | 456 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 457 | 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] | 458 | kfree(ptr); |
| 459 | } |
| 460 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 461 | static void kmalloc_oob_in_memset(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 462 | { |
| 463 | char *ptr; |
| 464 | size_t size = 666; |
| 465 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 466 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 467 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 468 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 469 | 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] | 470 | kfree(ptr); |
| 471 | } |
| 472 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 473 | static void kmalloc_memmove_invalid_size(struct kunit *test) |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 474 | { |
| 475 | char *ptr; |
| 476 | size_t size = 64; |
| 477 | volatile size_t invalid_size = -2; |
| 478 | |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 479 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 480 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 481 | |
| 482 | memset((char *)ptr, 0, 64); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 483 | |
| 484 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 485 | memmove((char *)ptr, (char *)ptr + 4, invalid_size)); |
Walter Wu | 98f3b56 | 2020-04-01 21:09:40 -0700 | [diff] [blame] | 486 | kfree(ptr); |
| 487 | } |
| 488 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 489 | static void kmalloc_uaf(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 490 | { |
| 491 | char *ptr; |
| 492 | size_t size = 10; |
| 493 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 494 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 495 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 496 | |
| 497 | kfree(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 498 | KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x'); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 501 | static void kmalloc_uaf_memset(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 502 | { |
| 503 | char *ptr; |
| 504 | size_t size = 33; |
| 505 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 506 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 507 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 508 | |
| 509 | kfree(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 510 | KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size)); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 513 | static void kmalloc_uaf2(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 514 | { |
| 515 | char *ptr1, *ptr2; |
| 516 | size_t size = 43; |
Andrey Konovalov | 0c23e1c | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 517 | int counter = 0; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 518 | |
Andrey Konovalov | 0c23e1c | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 519 | again: |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 520 | ptr1 = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 521 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 522 | |
| 523 | kfree(ptr1); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 524 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 525 | ptr2 = kmalloc(size, GFP_KERNEL); |
| 526 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); |
| 527 | |
Andrey Konovalov | 0c23e1c | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 528 | /* |
| 529 | * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same. |
| 530 | * Allow up to 16 attempts at generating different tags. |
| 531 | */ |
| 532 | if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) { |
| 533 | kfree(ptr2); |
| 534 | goto again; |
| 535 | } |
| 536 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 537 | KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x'); |
| 538 | KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2); |
| 539 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 540 | kfree(ptr2); |
| 541 | } |
| 542 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 543 | static void kfree_via_page(struct kunit *test) |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 544 | { |
| 545 | char *ptr; |
| 546 | size_t size = 8; |
| 547 | struct page *page; |
| 548 | unsigned long offset; |
| 549 | |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 550 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 551 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 552 | |
| 553 | page = virt_to_page(ptr); |
| 554 | offset = offset_in_page(ptr); |
| 555 | kfree(page_address(page) + offset); |
| 556 | } |
| 557 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 558 | static void kfree_via_phys(struct kunit *test) |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 559 | { |
| 560 | char *ptr; |
| 561 | size_t size = 8; |
| 562 | phys_addr_t phys; |
| 563 | |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 564 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 565 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Mark Rutland | b92a953 | 2019-09-23 15:34:16 -0700 | [diff] [blame] | 566 | |
| 567 | phys = virt_to_phys(ptr); |
| 568 | kfree(phys_to_virt(phys)); |
| 569 | } |
| 570 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 571 | static void kmem_cache_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 572 | { |
| 573 | char *p; |
| 574 | size_t size = 200; |
Andrey Konovalov | 9346eae | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 575 | struct kmem_cache *cache; |
| 576 | |
| 577 | cache = kmem_cache_create("test_cache", size, 0, 0, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 578 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
Andrey Konovalov | 9346eae | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 579 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 580 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 581 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 582 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 583 | kmem_cache_destroy(cache); |
| 584 | return; |
| 585 | } |
| 586 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 587 | KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]); |
Andrey Konovalov | 9346eae | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 588 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 589 | kmem_cache_free(cache, p); |
| 590 | kmem_cache_destroy(cache); |
| 591 | } |
| 592 | |
Andrey Konovalov | 9346eae | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 593 | static void kmem_cache_accounted(struct kunit *test) |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 594 | { |
| 595 | int i; |
| 596 | char *p; |
| 597 | size_t size = 200; |
| 598 | struct kmem_cache *cache; |
| 599 | |
| 600 | cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 601 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 602 | |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 603 | /* |
| 604 | * Several allocations with a delay to allow for lazy per memcg kmem |
| 605 | * cache creation. |
| 606 | */ |
| 607 | for (i = 0; i < 5; i++) { |
| 608 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 609 | if (!p) |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 610 | goto free_cache; |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 611 | |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 612 | kmem_cache_free(cache, p); |
| 613 | msleep(100); |
| 614 | } |
| 615 | |
| 616 | free_cache: |
| 617 | kmem_cache_destroy(cache); |
| 618 | } |
| 619 | |
Andrey Konovalov | 9346eae | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 620 | static void kmem_cache_bulk(struct kunit *test) |
| 621 | { |
| 622 | struct kmem_cache *cache; |
| 623 | size_t size = 200; |
| 624 | char *p[10]; |
| 625 | bool ret; |
| 626 | int i; |
| 627 | |
| 628 | cache = kmem_cache_create("test_cache", size, 0, 0, NULL); |
| 629 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 630 | |
| 631 | ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p); |
| 632 | if (!ret) { |
| 633 | kunit_err(test, "Allocation failed: %s\n", __func__); |
| 634 | kmem_cache_destroy(cache); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | for (i = 0; i < ARRAY_SIZE(p); i++) |
| 639 | p[i][0] = p[i][size - 1] = 42; |
| 640 | |
| 641 | kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p); |
| 642 | kmem_cache_destroy(cache); |
| 643 | } |
| 644 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 645 | static char global_array[10]; |
| 646 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 647 | static void kasan_global_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 648 | { |
| 649 | volatile int i = 3; |
| 650 | char *p = &global_array[ARRAY_SIZE(global_array) + i]; |
| 651 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 652 | /* Only generic mode instruments globals. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 653 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 654 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 655 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Andrey Konovalov | 696574e | 2021-02-03 15:35:05 +1100 | [diff] [blame] | 658 | /* Check that ksize() makes the whole object accessible. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 659 | static void ksize_unpoisons_memory(struct kunit *test) |
| 660 | { |
| 661 | char *ptr; |
| 662 | size_t size = 123, real_size; |
| 663 | |
| 664 | ptr = kmalloc(size, GFP_KERNEL); |
| 665 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 666 | real_size = ksize(ptr); |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 667 | |
| 668 | /* This access shouldn't trigger a KASAN report. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 669 | ptr[size] = 'x'; |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 670 | |
| 671 | /* This one must. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 672 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y'); |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 673 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 674 | kfree(ptr); |
| 675 | } |
| 676 | |
Andrey Konovalov | 696574e | 2021-02-03 15:35:05 +1100 | [diff] [blame] | 677 | /* |
| 678 | * Check that a use-after-free is detected by ksize() and via normal accesses |
| 679 | * after it. |
| 680 | */ |
| 681 | static void ksize_uaf(struct kunit *test) |
| 682 | { |
| 683 | char *ptr; |
| 684 | int size = 128 - KASAN_GRANULE_SIZE; |
| 685 | |
| 686 | ptr = kmalloc(size, GFP_KERNEL); |
| 687 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 688 | kfree(ptr); |
| 689 | |
| 690 | KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr)); |
| 691 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr); |
| 692 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size)); |
| 693 | } |
| 694 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 695 | static void kasan_stack_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 696 | { |
| 697 | char stack_array[10]; |
Andrey Konovalov | 51dcc81 | 2020-08-06 23:25:12 -0700 | [diff] [blame] | 698 | volatile int i = OOB_TAG_OFF; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 699 | char *p = &stack_array[ARRAY_SIZE(stack_array) + i]; |
| 700 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 701 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 702 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 703 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 706 | static void kasan_alloca_oob_left(struct kunit *test) |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 707 | { |
| 708 | volatile int i = 10; |
| 709 | char alloca_array[i]; |
| 710 | char *p = alloca_array - 1; |
| 711 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 712 | /* Only generic mode instruments dynamic allocas. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 713 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
| 714 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 715 | |
| 716 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 719 | static void kasan_alloca_oob_right(struct kunit *test) |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 720 | { |
| 721 | volatile int i = 10; |
| 722 | char alloca_array[i]; |
| 723 | char *p = alloca_array + i; |
| 724 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 725 | /* Only generic mode instruments dynamic allocas. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 726 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
| 727 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 728 | |
| 729 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 732 | static void kmem_cache_double_free(struct kunit *test) |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 733 | { |
| 734 | char *p; |
| 735 | size_t size = 200; |
| 736 | struct kmem_cache *cache; |
| 737 | |
| 738 | cache = kmem_cache_create("test_cache", size, 0, 0, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 739 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 740 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 741 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 742 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 743 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 744 | kmem_cache_destroy(cache); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | kmem_cache_free(cache, p); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 749 | KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p)); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 750 | kmem_cache_destroy(cache); |
| 751 | } |
| 752 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 753 | static void kmem_cache_invalid_free(struct kunit *test) |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 754 | { |
| 755 | char *p; |
| 756 | size_t size = 200; |
| 757 | struct kmem_cache *cache; |
| 758 | |
| 759 | cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU, |
| 760 | NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 761 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 762 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 763 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 764 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 765 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 766 | kmem_cache_destroy(cache); |
| 767 | return; |
| 768 | } |
| 769 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 770 | /* Trigger invalid free, the object doesn't get freed. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 771 | KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1)); |
Andrey Konovalov | 91c93ed | 2018-04-10 16:30:35 -0700 | [diff] [blame] | 772 | |
| 773 | /* |
| 774 | * Properly free the object to prevent the "Objects remaining in |
| 775 | * test_cache on __kmem_cache_shutdown" BUG failure. |
| 776 | */ |
| 777 | kmem_cache_free(cache, p); |
| 778 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 779 | kmem_cache_destroy(cache); |
| 780 | } |
| 781 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 782 | static void kasan_memchr(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 783 | { |
| 784 | char *ptr; |
| 785 | size_t size = 24; |
| 786 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 787 | /* |
| 788 | * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. |
| 789 | * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. |
| 790 | */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 791 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 792 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 793 | if (OOB_TAG_OFF) |
| 794 | size = round_up(size, OOB_TAG_OFF); |
| 795 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 796 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 797 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 798 | |
| 799 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 800 | kasan_ptr_result = memchr(ptr, '1', size + 1)); |
| 801 | |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 802 | kfree(ptr); |
| 803 | } |
| 804 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 805 | static void kasan_memcmp(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 806 | { |
| 807 | char *ptr; |
| 808 | size_t size = 24; |
| 809 | int arr[9]; |
| 810 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 811 | /* |
| 812 | * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. |
| 813 | * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. |
| 814 | */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 815 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 816 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 817 | if (OOB_TAG_OFF) |
| 818 | size = round_up(size, OOB_TAG_OFF); |
| 819 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 820 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 821 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 822 | memset(arr, 0, sizeof(arr)); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 823 | |
| 824 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 825 | kasan_int_result = memcmp(ptr, arr, size+1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 826 | kfree(ptr); |
| 827 | } |
| 828 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 829 | static void kasan_strings(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 830 | { |
| 831 | char *ptr; |
| 832 | size_t size = 24; |
| 833 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 834 | /* |
| 835 | * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. |
| 836 | * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. |
| 837 | */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 838 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 839 | |
| 840 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 841 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 842 | |
| 843 | kfree(ptr); |
| 844 | |
| 845 | /* |
| 846 | * Try to cause only 1 invalid access (less spam in dmesg). |
| 847 | * For that we need ptr to point to zeroed byte. |
| 848 | * Skip metadata that could be stored in freed object so ptr |
| 849 | * will likely point to zeroed byte. |
| 850 | */ |
| 851 | ptr += 16; |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 852 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1')); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 853 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 854 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1')); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 855 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 856 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2")); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 857 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 858 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 859 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 860 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 861 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 862 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 865 | static void kasan_bitops_modify(struct kunit *test, int nr, void *addr) |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 866 | { |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 867 | KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr)); |
| 868 | KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr)); |
| 869 | KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr)); |
| 870 | KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr)); |
| 871 | KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr)); |
| 872 | KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr)); |
| 873 | KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr)); |
| 874 | KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr)); |
| 875 | } |
| 876 | |
| 877 | static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr) |
| 878 | { |
| 879 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr)); |
| 880 | KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr)); |
| 881 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr)); |
| 882 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr)); |
| 883 | KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr)); |
| 884 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr)); |
| 885 | KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr)); |
| 886 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr)); |
| 887 | |
| 888 | #if defined(clear_bit_unlock_is_negative_byte) |
| 889 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = |
| 890 | clear_bit_unlock_is_negative_byte(nr, addr)); |
| 891 | #endif |
| 892 | } |
| 893 | |
| 894 | static void kasan_bitops_generic(struct kunit *test) |
| 895 | { |
| 896 | long *bits; |
| 897 | |
| 898 | /* This test is specifically crafted for the generic mode. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 899 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 900 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 901 | /* |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 902 | * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes; |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 903 | * this way we do not actually corrupt other memory. |
| 904 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 905 | bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 906 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * Below calls try to access bit within allocated memory; however, the |
| 910 | * below accesses are still out-of-bounds, since bitops are defined to |
| 911 | * operate on the whole long the bit is in. |
| 912 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 913 | kasan_bitops_modify(test, BITS_PER_LONG, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * Below calls try to access bit beyond allocated memory. |
| 917 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 918 | 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] | 919 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 920 | kfree(bits); |
| 921 | } |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 922 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 923 | static void kasan_bitops_tags(struct kunit *test) |
| 924 | { |
| 925 | long *bits; |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 926 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 927 | /* This test is specifically crafted for tag-based modes. */ |
| 928 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 929 | |
Andrey Konovalov | c1e807d | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 930 | /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */ |
| 931 | bits = kzalloc(48, GFP_KERNEL); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 932 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 933 | |
Andrey Konovalov | c1e807d | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 934 | /* Do the accesses past the 48 allocated bytes, but within the redone. */ |
| 935 | kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48); |
| 936 | 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] | 937 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 938 | kfree(bits); |
| 939 | } |
| 940 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 941 | static void kmalloc_double_kzfree(struct kunit *test) |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 942 | { |
| 943 | char *ptr; |
| 944 | size_t size = 16; |
| 945 | |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 946 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 947 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 948 | |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 949 | kfree_sensitive(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 950 | KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr)); |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 953 | static void vmalloc_oob(struct kunit *test) |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 954 | { |
| 955 | void *area; |
| 956 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 957 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 958 | |
| 959 | /* |
| 960 | * We have to be careful not to hit the guard page. |
| 961 | * The MMU will catch that and crash us. |
| 962 | */ |
| 963 | area = vmalloc(3000); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 964 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 965 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 966 | KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 967 | vfree(area); |
| 968 | } |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 969 | |
Andrey Konovalov | 782ba45 | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 970 | /* |
| 971 | * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN, |
| 972 | * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based |
| 973 | * modes. |
| 974 | */ |
| 975 | static void match_all_not_assigned(struct kunit *test) |
| 976 | { |
| 977 | char *ptr; |
| 978 | struct page *pages; |
| 979 | int i, size, order; |
| 980 | |
| 981 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 982 | |
| 983 | for (i = 0; i < 256; i++) { |
| 984 | size = (get_random_int() % 1024) + 1; |
| 985 | ptr = kmalloc(size, GFP_KERNEL); |
| 986 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 987 | KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); |
| 988 | KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); |
| 989 | kfree(ptr); |
| 990 | } |
| 991 | |
| 992 | for (i = 0; i < 256; i++) { |
| 993 | order = (get_random_int() % 4) + 1; |
| 994 | pages = alloc_pages(GFP_KERNEL, order); |
| 995 | ptr = page_address(pages); |
| 996 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 997 | KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); |
| 998 | KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); |
| 999 | free_pages((unsigned long)ptr, order); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* Check that 0xff works as a match-all pointer tag for tag-based modes. */ |
| 1004 | static void match_all_ptr_tag(struct kunit *test) |
| 1005 | { |
| 1006 | char *ptr; |
| 1007 | u8 tag; |
| 1008 | |
| 1009 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 1010 | |
| 1011 | ptr = kmalloc(128, GFP_KERNEL); |
| 1012 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 1013 | |
| 1014 | /* Backup the assigned tag. */ |
| 1015 | tag = get_tag(ptr); |
| 1016 | KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL); |
| 1017 | |
| 1018 | /* Reset the tag to 0xff.*/ |
| 1019 | ptr = set_tag(ptr, KASAN_TAG_KERNEL); |
| 1020 | |
| 1021 | /* This access shouldn't trigger a KASAN report. */ |
| 1022 | *ptr = 0; |
| 1023 | |
| 1024 | /* Recover the pointer tag and free. */ |
| 1025 | ptr = set_tag(ptr, tag); |
| 1026 | kfree(ptr); |
| 1027 | } |
| 1028 | |
| 1029 | /* Check that there are no match-all memory tags for tag-based modes. */ |
| 1030 | static void match_all_mem_tag(struct kunit *test) |
| 1031 | { |
| 1032 | char *ptr; |
| 1033 | int tag; |
| 1034 | |
| 1035 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 1036 | |
| 1037 | ptr = kmalloc(128, GFP_KERNEL); |
| 1038 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 1039 | KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); |
| 1040 | |
| 1041 | /* For each possible tag value not matching the pointer tag. */ |
| 1042 | for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) { |
| 1043 | if (tag == get_tag(ptr)) |
| 1044 | continue; |
| 1045 | |
| 1046 | /* Mark the first memory granule with the chosen memory tag. */ |
| 1047 | kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag); |
| 1048 | |
| 1049 | /* This access must cause a KASAN report. */ |
| 1050 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0); |
| 1051 | } |
| 1052 | |
| 1053 | /* Recover the memory tag and free. */ |
| 1054 | kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr)); |
| 1055 | kfree(ptr); |
| 1056 | } |
| 1057 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1058 | static struct kunit_case kasan_kunit_test_cases[] = { |
| 1059 | KUNIT_CASE(kmalloc_oob_right), |
| 1060 | KUNIT_CASE(kmalloc_oob_left), |
| 1061 | KUNIT_CASE(kmalloc_node_oob_right), |
| 1062 | KUNIT_CASE(kmalloc_pagealloc_oob_right), |
| 1063 | KUNIT_CASE(kmalloc_pagealloc_uaf), |
| 1064 | KUNIT_CASE(kmalloc_pagealloc_invalid_free), |
Andrey Konovalov | e449e27 | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 1065 | KUNIT_CASE(pagealloc_oob_right), |
| 1066 | KUNIT_CASE(pagealloc_uaf), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1067 | KUNIT_CASE(kmalloc_large_oob_right), |
Andrey Konovalov | d7ef7af | 2021-02-12 17:14:50 +1100 | [diff] [blame] | 1068 | KUNIT_CASE(krealloc_more_oob), |
| 1069 | KUNIT_CASE(krealloc_less_oob), |
| 1070 | KUNIT_CASE(krealloc_pagealloc_more_oob), |
| 1071 | KUNIT_CASE(krealloc_pagealloc_less_oob), |
Andrey Konovalov | 4f62c69 | 2021-02-12 17:14:50 +1100 | [diff] [blame^] | 1072 | KUNIT_CASE(krealloc_uaf), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1073 | KUNIT_CASE(kmalloc_oob_16), |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 1074 | KUNIT_CASE(kmalloc_uaf_16), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1075 | KUNIT_CASE(kmalloc_oob_in_memset), |
| 1076 | KUNIT_CASE(kmalloc_oob_memset_2), |
| 1077 | KUNIT_CASE(kmalloc_oob_memset_4), |
| 1078 | KUNIT_CASE(kmalloc_oob_memset_8), |
| 1079 | KUNIT_CASE(kmalloc_oob_memset_16), |
| 1080 | KUNIT_CASE(kmalloc_memmove_invalid_size), |
| 1081 | KUNIT_CASE(kmalloc_uaf), |
| 1082 | KUNIT_CASE(kmalloc_uaf_memset), |
| 1083 | KUNIT_CASE(kmalloc_uaf2), |
| 1084 | KUNIT_CASE(kfree_via_page), |
| 1085 | KUNIT_CASE(kfree_via_phys), |
| 1086 | KUNIT_CASE(kmem_cache_oob), |
Andrey Konovalov | 9346eae | 2021-02-03 15:35:06 +1100 | [diff] [blame] | 1087 | KUNIT_CASE(kmem_cache_accounted), |
| 1088 | KUNIT_CASE(kmem_cache_bulk), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1089 | KUNIT_CASE(kasan_global_oob), |
| 1090 | KUNIT_CASE(kasan_stack_oob), |
| 1091 | KUNIT_CASE(kasan_alloca_oob_left), |
| 1092 | KUNIT_CASE(kasan_alloca_oob_right), |
| 1093 | KUNIT_CASE(ksize_unpoisons_memory), |
Andrey Konovalov | 696574e | 2021-02-03 15:35:05 +1100 | [diff] [blame] | 1094 | KUNIT_CASE(ksize_uaf), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1095 | KUNIT_CASE(kmem_cache_double_free), |
| 1096 | KUNIT_CASE(kmem_cache_invalid_free), |
| 1097 | KUNIT_CASE(kasan_memchr), |
| 1098 | KUNIT_CASE(kasan_memcmp), |
| 1099 | KUNIT_CASE(kasan_strings), |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 1100 | KUNIT_CASE(kasan_bitops_generic), |
| 1101 | KUNIT_CASE(kasan_bitops_tags), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1102 | KUNIT_CASE(kmalloc_double_kzfree), |
| 1103 | KUNIT_CASE(vmalloc_oob), |
Andrey Konovalov | 782ba45 | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 1104 | KUNIT_CASE(match_all_not_assigned), |
| 1105 | KUNIT_CASE(match_all_ptr_tag), |
| 1106 | KUNIT_CASE(match_all_mem_tag), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1107 | {} |
| 1108 | }; |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 1109 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1110 | static struct kunit_suite kasan_kunit_test_suite = { |
| 1111 | .name = "kasan", |
| 1112 | .init = kasan_test_init, |
| 1113 | .test_cases = kasan_kunit_test_cases, |
| 1114 | .exit = kasan_test_exit, |
| 1115 | }; |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 1116 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 1117 | kunit_test_suite(kasan_kunit_test_suite); |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 1118 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 1119 | MODULE_LICENSE("GPL"); |