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