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; |
| 482 | struct kmem_cache *cache = kmem_cache_create("test_cache", |
| 483 | size, 0, |
| 484 | 0, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 485 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 486 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 487 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 488 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 489 | kmem_cache_destroy(cache); |
| 490 | return; |
| 491 | } |
| 492 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 493 | KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 494 | kmem_cache_free(cache, p); |
| 495 | kmem_cache_destroy(cache); |
| 496 | } |
| 497 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 498 | static void memcg_accounted_kmem_cache(struct kunit *test) |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 499 | { |
| 500 | int i; |
| 501 | char *p; |
| 502 | size_t size = 200; |
| 503 | struct kmem_cache *cache; |
| 504 | |
| 505 | cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 506 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 507 | |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 508 | /* |
| 509 | * Several allocations with a delay to allow for lazy per memcg kmem |
| 510 | * cache creation. |
| 511 | */ |
| 512 | for (i = 0; i < 5; i++) { |
| 513 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 514 | if (!p) |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 515 | goto free_cache; |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 516 | |
Greg Thelen | 0386bf3 | 2017-02-24 15:00:08 -0800 | [diff] [blame] | 517 | kmem_cache_free(cache, p); |
| 518 | msleep(100); |
| 519 | } |
| 520 | |
| 521 | free_cache: |
| 522 | kmem_cache_destroy(cache); |
| 523 | } |
| 524 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 525 | static char global_array[10]; |
| 526 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 527 | static void kasan_global_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 528 | { |
| 529 | volatile int i = 3; |
| 530 | char *p = &global_array[ARRAY_SIZE(global_array) + i]; |
| 531 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 532 | /* Only generic mode instruments globals. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 533 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 534 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 535 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Andrey Konovalov | 696574e | 2021-02-03 15:35:05 +1100 | [diff] [blame] | 538 | /* Check that ksize() makes the whole object accessible. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 539 | static void ksize_unpoisons_memory(struct kunit *test) |
| 540 | { |
| 541 | char *ptr; |
| 542 | size_t size = 123, real_size; |
| 543 | |
| 544 | ptr = kmalloc(size, GFP_KERNEL); |
| 545 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 546 | real_size = ksize(ptr); |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 547 | |
| 548 | /* This access shouldn't trigger a KASAN report. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 549 | ptr[size] = 'x'; |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 550 | |
| 551 | /* This one must. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 552 | KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y'); |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 553 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 554 | kfree(ptr); |
| 555 | } |
| 556 | |
Andrey Konovalov | 696574e | 2021-02-03 15:35:05 +1100 | [diff] [blame] | 557 | /* |
| 558 | * Check that a use-after-free is detected by ksize() and via normal accesses |
| 559 | * after it. |
| 560 | */ |
| 561 | static void ksize_uaf(struct kunit *test) |
| 562 | { |
| 563 | char *ptr; |
| 564 | int size = 128 - KASAN_GRANULE_SIZE; |
| 565 | |
| 566 | ptr = kmalloc(size, GFP_KERNEL); |
| 567 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 568 | kfree(ptr); |
| 569 | |
| 570 | KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr)); |
| 571 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr); |
| 572 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size)); |
| 573 | } |
| 574 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 575 | static void kasan_stack_oob(struct kunit *test) |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 576 | { |
| 577 | char stack_array[10]; |
Andrey Konovalov | 51dcc81 | 2020-08-06 23:25:12 -0700 | [diff] [blame] | 578 | volatile int i = OOB_TAG_OFF; |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 579 | char *p = &stack_array[ARRAY_SIZE(stack_array) + i]; |
| 580 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 581 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 582 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 583 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Andrey Ryabinin | eae08dc | 2016-05-20 16:59:34 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 586 | static void kasan_alloca_oob_left(struct kunit *test) |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 587 | { |
| 588 | volatile int i = 10; |
| 589 | char alloca_array[i]; |
| 590 | char *p = alloca_array - 1; |
| 591 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 592 | /* Only generic mode instruments dynamic allocas. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 593 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
| 594 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 595 | |
| 596 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 599 | static void kasan_alloca_oob_right(struct kunit *test) |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 600 | { |
| 601 | volatile int i = 10; |
| 602 | char alloca_array[i]; |
| 603 | char *p = alloca_array + i; |
| 604 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 605 | /* Only generic mode instruments dynamic allocas. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 606 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
| 607 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 608 | |
| 609 | KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p); |
Paul Lawrence | 00a1429 | 2018-02-06 15:36:16 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 612 | static void kmem_cache_double_free(struct kunit *test) |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 613 | { |
| 614 | char *p; |
| 615 | size_t size = 200; |
| 616 | struct kmem_cache *cache; |
| 617 | |
| 618 | cache = kmem_cache_create("test_cache", size, 0, 0, NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 619 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 620 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 621 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 622 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 623 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 624 | kmem_cache_destroy(cache); |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | kmem_cache_free(cache, p); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 629 | KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p)); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 630 | kmem_cache_destroy(cache); |
| 631 | } |
| 632 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 633 | static void kmem_cache_invalid_free(struct kunit *test) |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 634 | { |
| 635 | char *p; |
| 636 | size_t size = 200; |
| 637 | struct kmem_cache *cache; |
| 638 | |
| 639 | cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU, |
| 640 | NULL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 641 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); |
| 642 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 643 | p = kmem_cache_alloc(cache, GFP_KERNEL); |
| 644 | if (!p) { |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 645 | kunit_err(test, "Allocation failed: %s\n", __func__); |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 646 | kmem_cache_destroy(cache); |
| 647 | return; |
| 648 | } |
| 649 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 650 | /* Trigger invalid free, the object doesn't get freed. */ |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 651 | KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1)); |
Andrey Konovalov | 91c93ed | 2018-04-10 16:30:35 -0700 | [diff] [blame] | 652 | |
| 653 | /* |
| 654 | * Properly free the object to prevent the "Objects remaining in |
| 655 | * test_cache on __kmem_cache_shutdown" BUG failure. |
| 656 | */ |
| 657 | kmem_cache_free(cache, p); |
| 658 | |
Dmitry Vyukov | b1d5728 | 2018-02-06 15:36:37 -0800 | [diff] [blame] | 659 | kmem_cache_destroy(cache); |
| 660 | } |
| 661 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 662 | static void kasan_memchr(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 663 | { |
| 664 | char *ptr; |
| 665 | size_t size = 24; |
| 666 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 667 | /* |
| 668 | * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. |
| 669 | * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. |
| 670 | */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 671 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 672 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 673 | if (OOB_TAG_OFF) |
| 674 | size = round_up(size, OOB_TAG_OFF); |
| 675 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 676 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 677 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 678 | |
| 679 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 680 | kasan_ptr_result = memchr(ptr, '1', size + 1)); |
| 681 | |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 682 | kfree(ptr); |
| 683 | } |
| 684 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 685 | static void kasan_memcmp(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 686 | { |
| 687 | char *ptr; |
| 688 | size_t size = 24; |
| 689 | int arr[9]; |
| 690 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 691 | /* |
| 692 | * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. |
| 693 | * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. |
| 694 | */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 695 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 696 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 697 | if (OOB_TAG_OFF) |
| 698 | size = round_up(size, OOB_TAG_OFF); |
| 699 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 700 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 701 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 702 | memset(arr, 0, sizeof(arr)); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 703 | |
| 704 | KUNIT_EXPECT_KASAN_FAIL(test, |
| 705 | kasan_int_result = memcmp(ptr, arr, size+1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 706 | kfree(ptr); |
| 707 | } |
| 708 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 709 | static void kasan_strings(struct kunit *test) |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 710 | { |
| 711 | char *ptr; |
| 712 | size_t size = 24; |
| 713 | |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 714 | /* |
| 715 | * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. |
| 716 | * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. |
| 717 | */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 718 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 719 | |
| 720 | ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); |
| 721 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 722 | |
| 723 | kfree(ptr); |
| 724 | |
| 725 | /* |
| 726 | * Try to cause only 1 invalid access (less spam in dmesg). |
| 727 | * For that we need ptr to point to zeroed byte. |
| 728 | * Skip metadata that could be stored in freed object so ptr |
| 729 | * will likely point to zeroed byte. |
| 730 | */ |
| 731 | ptr += 16; |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 732 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1')); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 733 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 734 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1')); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 735 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 736 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2")); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 737 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 738 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 739 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 740 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 741 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 742 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1)); |
Andrey Ryabinin | 0c96350 | 2018-10-26 15:02:34 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 745 | static void kasan_bitops_modify(struct kunit *test, int nr, void *addr) |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 746 | { |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 747 | KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr)); |
| 748 | KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr)); |
| 749 | KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr)); |
| 750 | KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr)); |
| 751 | KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr)); |
| 752 | KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr)); |
| 753 | KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr)); |
| 754 | KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr)); |
| 755 | } |
| 756 | |
| 757 | static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr) |
| 758 | { |
| 759 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr)); |
| 760 | KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr)); |
| 761 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr)); |
| 762 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr)); |
| 763 | KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr)); |
| 764 | KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr)); |
| 765 | KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr)); |
| 766 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr)); |
| 767 | |
| 768 | #if defined(clear_bit_unlock_is_negative_byte) |
| 769 | KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = |
| 770 | clear_bit_unlock_is_negative_byte(nr, addr)); |
| 771 | #endif |
| 772 | } |
| 773 | |
| 774 | static void kasan_bitops_generic(struct kunit *test) |
| 775 | { |
| 776 | long *bits; |
| 777 | |
| 778 | /* This test is specifically crafted for the generic mode. */ |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 779 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 780 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 781 | /* |
Andrey Konovalov | f3e66b2 | 2021-02-03 15:34:59 +1100 | [diff] [blame] | 782 | * 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] | 783 | * this way we do not actually corrupt other memory. |
| 784 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 785 | bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 786 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 787 | |
| 788 | /* |
| 789 | * Below calls try to access bit within allocated memory; however, the |
| 790 | * below accesses are still out-of-bounds, since bitops are defined to |
| 791 | * operate on the whole long the bit is in. |
| 792 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 793 | kasan_bitops_modify(test, BITS_PER_LONG, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | * Below calls try to access bit beyond allocated memory. |
| 797 | */ |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 798 | 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] | 799 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 800 | kfree(bits); |
| 801 | } |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 802 | |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 803 | static void kasan_bitops_tags(struct kunit *test) |
| 804 | { |
| 805 | long *bits; |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 806 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 807 | /* This test is specifically crafted for tag-based modes. */ |
| 808 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 809 | |
Andrey Konovalov | c1e807d | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 810 | /* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */ |
| 811 | bits = kzalloc(48, GFP_KERNEL); |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 812 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits); |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 813 | |
Andrey Konovalov | c1e807d | 2021-02-03 15:35:04 +1100 | [diff] [blame] | 814 | /* Do the accesses past the 48 allocated bytes, but within the redone. */ |
| 815 | kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48); |
| 816 | 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] | 817 | |
Marco Elver | 19a33ca | 2019-07-11 20:53:52 -0700 | [diff] [blame] | 818 | kfree(bits); |
| 819 | } |
| 820 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 821 | static void kmalloc_double_kzfree(struct kunit *test) |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 822 | { |
| 823 | char *ptr; |
| 824 | size_t size = 16; |
| 825 | |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 826 | ptr = kmalloc(size, GFP_KERNEL); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 827 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 828 | |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 829 | kfree_sensitive(ptr); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 830 | KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr)); |
Marco Elver | bb104ed | 2019-07-11 20:54:11 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 833 | static void vmalloc_oob(struct kunit *test) |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 834 | { |
| 835 | void *area; |
| 836 | |
Andrey Konovalov | 127ffef | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 837 | KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * We have to be careful not to hit the guard page. |
| 841 | * The MMU will catch that and crash us. |
| 842 | */ |
| 843 | area = vmalloc(3000); |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 844 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 845 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 846 | KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]); |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 847 | vfree(area); |
| 848 | } |
Daniel Axtens | 0651391 | 2019-11-30 17:54:53 -0800 | [diff] [blame] | 849 | |
Andrey Konovalov | 782ba45 | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 850 | /* |
| 851 | * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN, |
| 852 | * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based |
| 853 | * modes. |
| 854 | */ |
| 855 | static void match_all_not_assigned(struct kunit *test) |
| 856 | { |
| 857 | char *ptr; |
| 858 | struct page *pages; |
| 859 | int i, size, order; |
| 860 | |
| 861 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 862 | |
| 863 | for (i = 0; i < 256; i++) { |
| 864 | size = (get_random_int() % 1024) + 1; |
| 865 | ptr = kmalloc(size, GFP_KERNEL); |
| 866 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 867 | KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); |
| 868 | KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); |
| 869 | kfree(ptr); |
| 870 | } |
| 871 | |
| 872 | for (i = 0; i < 256; i++) { |
| 873 | order = (get_random_int() % 4) + 1; |
| 874 | pages = alloc_pages(GFP_KERNEL, order); |
| 875 | ptr = page_address(pages); |
| 876 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 877 | KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); |
| 878 | KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); |
| 879 | free_pages((unsigned long)ptr, order); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /* Check that 0xff works as a match-all pointer tag for tag-based modes. */ |
| 884 | static void match_all_ptr_tag(struct kunit *test) |
| 885 | { |
| 886 | char *ptr; |
| 887 | u8 tag; |
| 888 | |
| 889 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 890 | |
| 891 | ptr = kmalloc(128, GFP_KERNEL); |
| 892 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 893 | |
| 894 | /* Backup the assigned tag. */ |
| 895 | tag = get_tag(ptr); |
| 896 | KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL); |
| 897 | |
| 898 | /* Reset the tag to 0xff.*/ |
| 899 | ptr = set_tag(ptr, KASAN_TAG_KERNEL); |
| 900 | |
| 901 | /* This access shouldn't trigger a KASAN report. */ |
| 902 | *ptr = 0; |
| 903 | |
| 904 | /* Recover the pointer tag and free. */ |
| 905 | ptr = set_tag(ptr, tag); |
| 906 | kfree(ptr); |
| 907 | } |
| 908 | |
| 909 | /* Check that there are no match-all memory tags for tag-based modes. */ |
| 910 | static void match_all_mem_tag(struct kunit *test) |
| 911 | { |
| 912 | char *ptr; |
| 913 | int tag; |
| 914 | |
| 915 | KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); |
| 916 | |
| 917 | ptr = kmalloc(128, GFP_KERNEL); |
| 918 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); |
| 919 | KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); |
| 920 | |
| 921 | /* For each possible tag value not matching the pointer tag. */ |
| 922 | for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) { |
| 923 | if (tag == get_tag(ptr)) |
| 924 | continue; |
| 925 | |
| 926 | /* Mark the first memory granule with the chosen memory tag. */ |
| 927 | kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag); |
| 928 | |
| 929 | /* This access must cause a KASAN report. */ |
| 930 | KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0); |
| 931 | } |
| 932 | |
| 933 | /* Recover the memory tag and free. */ |
| 934 | kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr)); |
| 935 | kfree(ptr); |
| 936 | } |
| 937 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 938 | static struct kunit_case kasan_kunit_test_cases[] = { |
| 939 | KUNIT_CASE(kmalloc_oob_right), |
| 940 | KUNIT_CASE(kmalloc_oob_left), |
| 941 | KUNIT_CASE(kmalloc_node_oob_right), |
| 942 | KUNIT_CASE(kmalloc_pagealloc_oob_right), |
| 943 | KUNIT_CASE(kmalloc_pagealloc_uaf), |
| 944 | KUNIT_CASE(kmalloc_pagealloc_invalid_free), |
Andrey Konovalov | e449e27 | 2021-02-03 15:35:06 +1100 | [diff] [blame^] | 945 | KUNIT_CASE(pagealloc_oob_right), |
| 946 | KUNIT_CASE(pagealloc_uaf), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 947 | KUNIT_CASE(kmalloc_large_oob_right), |
| 948 | KUNIT_CASE(kmalloc_oob_krealloc_more), |
| 949 | KUNIT_CASE(kmalloc_oob_krealloc_less), |
| 950 | KUNIT_CASE(kmalloc_oob_16), |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 951 | KUNIT_CASE(kmalloc_uaf_16), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 952 | KUNIT_CASE(kmalloc_oob_in_memset), |
| 953 | KUNIT_CASE(kmalloc_oob_memset_2), |
| 954 | KUNIT_CASE(kmalloc_oob_memset_4), |
| 955 | KUNIT_CASE(kmalloc_oob_memset_8), |
| 956 | KUNIT_CASE(kmalloc_oob_memset_16), |
| 957 | KUNIT_CASE(kmalloc_memmove_invalid_size), |
| 958 | KUNIT_CASE(kmalloc_uaf), |
| 959 | KUNIT_CASE(kmalloc_uaf_memset), |
| 960 | KUNIT_CASE(kmalloc_uaf2), |
| 961 | KUNIT_CASE(kfree_via_page), |
| 962 | KUNIT_CASE(kfree_via_phys), |
| 963 | KUNIT_CASE(kmem_cache_oob), |
| 964 | KUNIT_CASE(memcg_accounted_kmem_cache), |
| 965 | KUNIT_CASE(kasan_global_oob), |
| 966 | KUNIT_CASE(kasan_stack_oob), |
| 967 | KUNIT_CASE(kasan_alloca_oob_left), |
| 968 | KUNIT_CASE(kasan_alloca_oob_right), |
| 969 | KUNIT_CASE(ksize_unpoisons_memory), |
Andrey Konovalov | 696574e | 2021-02-03 15:35:05 +1100 | [diff] [blame] | 970 | KUNIT_CASE(ksize_uaf), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 971 | KUNIT_CASE(kmem_cache_double_free), |
| 972 | KUNIT_CASE(kmem_cache_invalid_free), |
| 973 | KUNIT_CASE(kasan_memchr), |
| 974 | KUNIT_CASE(kasan_memcmp), |
| 975 | KUNIT_CASE(kasan_strings), |
Andrey Konovalov | 58b999d | 2020-11-01 17:07:37 -0800 | [diff] [blame] | 976 | KUNIT_CASE(kasan_bitops_generic), |
| 977 | KUNIT_CASE(kasan_bitops_tags), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 978 | KUNIT_CASE(kmalloc_double_kzfree), |
| 979 | KUNIT_CASE(vmalloc_oob), |
Andrey Konovalov | 782ba45 | 2021-02-03 15:35:00 +1100 | [diff] [blame] | 980 | KUNIT_CASE(match_all_not_assigned), |
| 981 | KUNIT_CASE(match_all_ptr_tag), |
| 982 | KUNIT_CASE(match_all_mem_tag), |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 983 | {} |
| 984 | }; |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 985 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 986 | static struct kunit_suite kasan_kunit_test_suite = { |
| 987 | .name = "kasan", |
| 988 | .init = kasan_test_init, |
| 989 | .test_cases = kasan_kunit_test_cases, |
| 990 | .exit = kasan_test_exit, |
| 991 | }; |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 992 | |
Patricia Alfonso | 73228c7 | 2020-10-13 16:55:06 -0700 | [diff] [blame] | 993 | kunit_test_suite(kasan_kunit_test_suite); |
Walter Wu | 387d6e4 | 2020-08-06 23:24:42 -0700 | [diff] [blame] | 994 | |
Andrey Ryabinin | 3f15801 | 2015-02-13 14:39:53 -0800 | [diff] [blame] | 995 | MODULE_LICENSE("GPL"); |