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