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