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