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