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