Andrey Konovalov | e886bf9 | 2018-12-28 00:31:14 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 2 | /* |
Andrey Konovalov | 2bd926b | 2018-12-28 00:29:53 -0800 | [diff] [blame] | 3 | * This file contains core generic KASAN code. |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
Andrey Ryabinin | 2baf9e8 | 2015-08-14 15:35:13 -0700 | [diff] [blame] | 6 | * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 7 | * |
Andrey Konovalov | 5d0926e | 2015-11-05 18:51:12 -0800 | [diff] [blame] | 8 | * Some code borrowed from https://github.com/xairy/kasan-prototype by |
Andrey Konovalov | 5f21f3a | 2018-02-06 15:36:41 -0800 | [diff] [blame] | 9 | * Andrey Konovalov <andreyknvl@gmail.com> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 12 | #include <linux/export.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 14 | #include <linux/init.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 15 | #include <linux/kasan.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Alexander Potapenko | 2b83052 | 2021-02-25 17:19:21 -0800 | [diff] [blame] | 17 | #include <linux/kfence.h> |
Andrey Ryabinin | 4593725 | 2015-11-20 15:57:18 -0800 | [diff] [blame] | 18 | #include <linux/kmemleak.h> |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 19 | #include <linux/linkage.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 20 | #include <linux/memblock.h> |
Andrey Ryabinin | 786a895 | 2015-02-13 14:39:21 -0800 | [diff] [blame] | 21 | #include <linux/memory.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 22 | #include <linux/mm.h> |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 24 | #include <linux/printk.h> |
| 25 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 26 | #include <linux/sched/task_stack.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/stacktrace.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/types.h> |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 31 | #include <linux/vmalloc.h> |
Dmitry Vyukov | 9f7d416 | 2016-10-14 16:07:23 +0200 | [diff] [blame] | 32 | #include <linux/bug.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 33 | |
| 34 | #include "kasan.h" |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 35 | #include "../slab.h" |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 36 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 37 | /* |
| 38 | * All functions below always inlined so compiler could |
| 39 | * perform better optimizations in each of __asan_loadX/__assn_storeX |
| 40 | * depending on memory access size X. |
| 41 | */ |
| 42 | |
| 43 | static __always_inline bool memory_is_poisoned_1(unsigned long addr) |
| 44 | { |
| 45 | s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr); |
| 46 | |
| 47 | if (unlikely(shadow_value)) { |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 48 | s8 last_accessible_byte = addr & KASAN_GRANULE_MASK; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 49 | return unlikely(last_accessible_byte >= shadow_value); |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 55 | static __always_inline bool memory_is_poisoned_2_4_8(unsigned long addr, |
| 56 | unsigned long size) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 57 | { |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 58 | u8 *shadow_addr = (u8 *)kasan_mem_to_shadow((void *)addr); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 59 | |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Access crosses 8(shadow size)-byte boundary. Such access maps |
| 62 | * into 2 shadow bytes, so we need to check them both. |
| 63 | */ |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 64 | if (unlikely(((addr + size - 1) & KASAN_GRANULE_MASK) < size - 1)) |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 65 | return *shadow_addr || memory_is_poisoned_1(addr + size - 1); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 66 | |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 67 | return memory_is_poisoned_1(addr + size - 1); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static __always_inline bool memory_is_poisoned_16(unsigned long addr) |
| 71 | { |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 72 | u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 73 | |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 74 | /* Unaligned 16-bytes access maps into 3 shadow bytes. */ |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 75 | if (unlikely(!IS_ALIGNED(addr, KASAN_GRANULE_SIZE))) |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 76 | return *shadow_addr || memory_is_poisoned_1(addr + 15); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 77 | |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 78 | return *shadow_addr; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 81 | static __always_inline unsigned long bytes_is_nonzero(const u8 *start, |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 82 | size_t size) |
| 83 | { |
| 84 | while (size) { |
| 85 | if (unlikely(*start)) |
| 86 | return (unsigned long)start; |
| 87 | start++; |
| 88 | size--; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 94 | static __always_inline unsigned long memory_is_nonzero(const void *start, |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 95 | const void *end) |
| 96 | { |
| 97 | unsigned int words; |
| 98 | unsigned long ret; |
| 99 | unsigned int prefix = (unsigned long)start % 8; |
| 100 | |
| 101 | if (end - start <= 16) |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 102 | return bytes_is_nonzero(start, end - start); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 103 | |
| 104 | if (prefix) { |
| 105 | prefix = 8 - prefix; |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 106 | ret = bytes_is_nonzero(start, prefix); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 107 | if (unlikely(ret)) |
| 108 | return ret; |
| 109 | start += prefix; |
| 110 | } |
| 111 | |
| 112 | words = (end - start) / 8; |
| 113 | while (words) { |
| 114 | if (unlikely(*(u64 *)start)) |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 115 | return bytes_is_nonzero(start, 8); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 116 | start += 8; |
| 117 | words--; |
| 118 | } |
| 119 | |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 120 | return bytes_is_nonzero(start, (end - start) % 8); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static __always_inline bool memory_is_poisoned_n(unsigned long addr, |
| 124 | size_t size) |
| 125 | { |
| 126 | unsigned long ret; |
| 127 | |
Joonsoo Kim | f5bd62c | 2017-07-10 15:50:37 -0700 | [diff] [blame] | 128 | ret = memory_is_nonzero(kasan_mem_to_shadow((void *)addr), |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 129 | kasan_mem_to_shadow((void *)addr + size - 1) + 1); |
| 130 | |
| 131 | if (unlikely(ret)) { |
| 132 | unsigned long last_byte = addr + size - 1; |
| 133 | s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte); |
| 134 | |
| 135 | if (unlikely(ret != (unsigned long)last_shadow || |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 136 | ((long)(last_byte & KASAN_GRANULE_MASK) >= *last_shadow))) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size) |
| 143 | { |
| 144 | if (__builtin_constant_p(size)) { |
| 145 | switch (size) { |
| 146 | case 1: |
| 147 | return memory_is_poisoned_1(addr); |
| 148 | case 2: |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 149 | case 4: |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 150 | case 8: |
Andrey Ryabinin | c634d80 | 2017-07-10 15:50:24 -0700 | [diff] [blame] | 151 | return memory_is_poisoned_2_4_8(addr, size); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 152 | case 16: |
| 153 | return memory_is_poisoned_16(addr); |
| 154 | default: |
| 155 | BUILD_BUG(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return memory_is_poisoned_n(addr, size); |
| 160 | } |
| 161 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 162 | static __always_inline bool check_region_inline(unsigned long addr, |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 163 | size_t size, bool write, |
| 164 | unsigned long ret_ip) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 165 | { |
Daniel Axtens | af3751f3 | 2021-06-28 19:40:42 -0700 | [diff] [blame] | 166 | if (!kasan_arch_is_ready()) |
| 167 | return true; |
| 168 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 169 | if (unlikely(size == 0)) |
Marco Elver | b5f6e0f | 2019-07-11 20:54:07 -0700 | [diff] [blame] | 170 | return true; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 171 | |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 172 | if (unlikely(addr + size < addr)) |
| 173 | return !kasan_report(addr, size, write, ret_ip); |
| 174 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 175 | if (unlikely((void *)addr < |
| 176 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) { |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 177 | return !kasan_report(addr, size, write, ret_ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (likely(!memory_is_poisoned(addr, size))) |
Marco Elver | b5f6e0f | 2019-07-11 20:54:07 -0700 | [diff] [blame] | 181 | return true; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 182 | |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 183 | return !kasan_report(addr, size, write, ret_ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 186 | bool kasan_check_range(unsigned long addr, size_t size, bool write, |
| 187 | unsigned long ret_ip) |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 188 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 189 | return check_region_inline(addr, size, write, ret_ip); |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 190 | } |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 191 | |
Andrey Konovalov | 611806b | 2021-02-24 12:05:50 -0800 | [diff] [blame] | 192 | bool kasan_byte_accessible(const void *addr) |
Andrey Konovalov | 2cdbed6 | 2020-12-22 12:00:46 -0800 | [diff] [blame] | 193 | { |
| 194 | s8 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(addr)); |
| 195 | |
Andrey Konovalov | 611806b | 2021-02-24 12:05:50 -0800 | [diff] [blame] | 196 | return shadow_byte >= 0 && shadow_byte < KASAN_GRANULE_SIZE; |
Andrey Konovalov | 2cdbed6 | 2020-12-22 12:00:46 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 199 | void kasan_cache_shrink(struct kmem_cache *cache) |
| 200 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 201 | kasan_quarantine_remove_cache(cache); |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Greg Thelen | f9fa1d9 | 2017-02-24 15:00:05 -0800 | [diff] [blame] | 204 | void kasan_cache_shutdown(struct kmem_cache *cache) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 205 | { |
Shakeel Butt | f9e13c0 | 2018-04-05 16:21:57 -0700 | [diff] [blame] | 206 | if (!__kmem_cache_empty(cache)) |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 207 | kasan_quarantine_remove_cache(cache); |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 210 | static void register_global(struct kasan_global *global) |
| 211 | { |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 212 | size_t aligned_size = round_up(global->size, KASAN_GRANULE_SIZE); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 213 | |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame] | 214 | kasan_unpoison(global->beg, global->size, false); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 215 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 216 | kasan_poison(global->beg + aligned_size, |
Andrey Konovalov | cebd0eb | 2020-12-22 12:00:21 -0800 | [diff] [blame] | 217 | global->size_with_redzone - aligned_size, |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame] | 218 | KASAN_GLOBAL_REDZONE, false); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void __asan_register_globals(struct kasan_global *globals, size_t size) |
| 222 | { |
| 223 | int i; |
| 224 | |
| 225 | for (i = 0; i < size; i++) |
| 226 | register_global(&globals[i]); |
| 227 | } |
| 228 | EXPORT_SYMBOL(__asan_register_globals); |
| 229 | |
| 230 | void __asan_unregister_globals(struct kasan_global *globals, size_t size) |
| 231 | { |
| 232 | } |
| 233 | EXPORT_SYMBOL(__asan_unregister_globals); |
| 234 | |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 235 | #define DEFINE_ASAN_LOAD_STORE(size) \ |
| 236 | void __asan_load##size(unsigned long addr) \ |
| 237 | { \ |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 238 | check_region_inline(addr, size, false, _RET_IP_); \ |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 239 | } \ |
| 240 | EXPORT_SYMBOL(__asan_load##size); \ |
| 241 | __alias(__asan_load##size) \ |
| 242 | void __asan_load##size##_noabort(unsigned long); \ |
| 243 | EXPORT_SYMBOL(__asan_load##size##_noabort); \ |
| 244 | void __asan_store##size(unsigned long addr) \ |
| 245 | { \ |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 246 | check_region_inline(addr, size, true, _RET_IP_); \ |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 247 | } \ |
| 248 | EXPORT_SYMBOL(__asan_store##size); \ |
| 249 | __alias(__asan_store##size) \ |
| 250 | void __asan_store##size##_noabort(unsigned long); \ |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 251 | EXPORT_SYMBOL(__asan_store##size##_noabort) |
| 252 | |
| 253 | DEFINE_ASAN_LOAD_STORE(1); |
| 254 | DEFINE_ASAN_LOAD_STORE(2); |
| 255 | DEFINE_ASAN_LOAD_STORE(4); |
| 256 | DEFINE_ASAN_LOAD_STORE(8); |
| 257 | DEFINE_ASAN_LOAD_STORE(16); |
| 258 | |
| 259 | void __asan_loadN(unsigned long addr, size_t size) |
| 260 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 261 | kasan_check_range(addr, size, false, _RET_IP_); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 262 | } |
| 263 | EXPORT_SYMBOL(__asan_loadN); |
| 264 | |
| 265 | __alias(__asan_loadN) |
| 266 | void __asan_loadN_noabort(unsigned long, size_t); |
| 267 | EXPORT_SYMBOL(__asan_loadN_noabort); |
| 268 | |
| 269 | void __asan_storeN(unsigned long addr, size_t size) |
| 270 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 271 | kasan_check_range(addr, size, true, _RET_IP_); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 272 | } |
| 273 | EXPORT_SYMBOL(__asan_storeN); |
| 274 | |
| 275 | __alias(__asan_storeN) |
| 276 | void __asan_storeN_noabort(unsigned long, size_t); |
| 277 | EXPORT_SYMBOL(__asan_storeN_noabort); |
| 278 | |
| 279 | /* to shut up compiler complaints */ |
| 280 | void __asan_handle_no_return(void) {} |
| 281 | EXPORT_SYMBOL(__asan_handle_no_return); |
Andrey Ryabinin | 786a895 | 2015-02-13 14:39:21 -0800 | [diff] [blame] | 282 | |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 283 | /* Emitted by compiler to poison alloca()ed objects. */ |
| 284 | void __asan_alloca_poison(unsigned long addr, size_t size) |
| 285 | { |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 286 | size_t rounded_up_size = round_up(size, KASAN_GRANULE_SIZE); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 287 | size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) - |
| 288 | rounded_up_size; |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 289 | size_t rounded_down_size = round_down(size, KASAN_GRANULE_SIZE); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 290 | |
| 291 | const void *left_redzone = (const void *)(addr - |
| 292 | KASAN_ALLOCA_REDZONE_SIZE); |
| 293 | const void *right_redzone = (const void *)(addr + rounded_up_size); |
| 294 | |
| 295 | WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE)); |
| 296 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 297 | kasan_unpoison((const void *)(addr + rounded_down_size), |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame] | 298 | size - rounded_down_size, false); |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 299 | kasan_poison(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame] | 300 | KASAN_ALLOCA_LEFT, false); |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 301 | kasan_poison(right_redzone, padding_size + KASAN_ALLOCA_REDZONE_SIZE, |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame] | 302 | KASAN_ALLOCA_RIGHT, false); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 303 | } |
| 304 | EXPORT_SYMBOL(__asan_alloca_poison); |
| 305 | |
| 306 | /* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */ |
| 307 | void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) |
| 308 | { |
| 309 | if (unlikely(!stack_top || stack_top > stack_bottom)) |
| 310 | return; |
| 311 | |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame] | 312 | kasan_unpoison(stack_top, stack_bottom - stack_top, false); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 313 | } |
| 314 | EXPORT_SYMBOL(__asan_allocas_unpoison); |
| 315 | |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 316 | /* Emitted by the compiler to [un]poison local variables. */ |
| 317 | #define DEFINE_ASAN_SET_SHADOW(byte) \ |
| 318 | void __asan_set_shadow_##byte(const void *addr, size_t size) \ |
| 319 | { \ |
| 320 | __memset((void *)addr, 0x##byte, size); \ |
| 321 | } \ |
| 322 | EXPORT_SYMBOL(__asan_set_shadow_##byte) |
| 323 | |
| 324 | DEFINE_ASAN_SET_SHADOW(00); |
| 325 | DEFINE_ASAN_SET_SHADOW(f1); |
| 326 | DEFINE_ASAN_SET_SHADOW(f2); |
| 327 | DEFINE_ASAN_SET_SHADOW(f3); |
| 328 | DEFINE_ASAN_SET_SHADOW(f5); |
| 329 | DEFINE_ASAN_SET_SHADOW(f8); |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 330 | |
Marco Elver | 7cb3007 | 2021-11-05 13:35:46 -0700 | [diff] [blame] | 331 | static void __kasan_record_aux_stack(void *addr, bool can_alloc) |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 332 | { |
| 333 | struct page *page = kasan_addr_to_page(addr); |
| 334 | struct kmem_cache *cache; |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 335 | struct kasan_alloc_meta *alloc_meta; |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 336 | void *object; |
| 337 | |
Alexander Potapenko | 2b83052 | 2021-02-25 17:19:21 -0800 | [diff] [blame] | 338 | if (is_kfence_address(addr) || !(page && PageSlab(page))) |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 339 | return; |
| 340 | |
| 341 | cache = page->slab_cache; |
| 342 | object = nearest_obj(cache, page, addr); |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 343 | alloc_meta = kasan_get_alloc_meta(cache, object); |
Walter Wu | 13384f6 | 2020-12-29 15:14:46 -0800 | [diff] [blame] | 344 | if (!alloc_meta) |
| 345 | return; |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 346 | |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 347 | alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; |
Marco Elver | 7cb3007 | 2021-11-05 13:35:46 -0700 | [diff] [blame] | 348 | alloc_meta->aux_stack[0] = kasan_save_stack(GFP_NOWAIT, can_alloc); |
| 349 | } |
| 350 | |
| 351 | void kasan_record_aux_stack(void *addr) |
| 352 | { |
| 353 | return __kasan_record_aux_stack(addr, true); |
| 354 | } |
| 355 | |
| 356 | void kasan_record_aux_stack_noalloc(void *addr) |
| 357 | { |
| 358 | return __kasan_record_aux_stack(addr, false); |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 359 | } |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 360 | |
| 361 | void kasan_set_free_info(struct kmem_cache *cache, |
| 362 | void *object, u8 tag) |
| 363 | { |
| 364 | struct kasan_free_meta *free_meta; |
| 365 | |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 366 | free_meta = kasan_get_free_meta(cache, object); |
Andrey Konovalov | 97593ca | 2020-12-22 12:03:28 -0800 | [diff] [blame] | 367 | if (!free_meta) |
| 368 | return; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 369 | |
Andrey Konovalov | 97593ca | 2020-12-22 12:03:28 -0800 | [diff] [blame] | 370 | kasan_set_track(&free_meta->free_track, GFP_NOWAIT); |
| 371 | /* The object was freed and has free track set. */ |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 372 | *(u8 *)kasan_mem_to_shadow(object) = KASAN_KMALLOC_FREETRACK; |
| 373 | } |
| 374 | |
| 375 | struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, |
| 376 | void *object, u8 tag) |
| 377 | { |
| 378 | if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_KMALLOC_FREETRACK) |
| 379 | return NULL; |
Andrey Konovalov | 97593ca | 2020-12-22 12:03:28 -0800 | [diff] [blame] | 380 | /* Free meta must be present with KASAN_KMALLOC_FREETRACK. */ |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 381 | return &kasan_get_free_meta(cache, object)->free_track; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 382 | } |