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