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