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 | { |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 166 | if (unlikely(size == 0)) |
Marco Elver | b5f6e0f | 2019-07-11 20:54:07 -0700 | [diff] [blame] | 167 | return true; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 168 | |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 169 | if (unlikely(addr + size < addr)) |
| 170 | return !kasan_report(addr, size, write, ret_ip); |
| 171 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 172 | if (unlikely((void *)addr < |
| 173 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) { |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 174 | return !kasan_report(addr, size, write, ret_ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (likely(!memory_is_poisoned(addr, size))) |
Marco Elver | b5f6e0f | 2019-07-11 20:54:07 -0700 | [diff] [blame] | 178 | return true; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 179 | |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 180 | return !kasan_report(addr, size, write, ret_ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 183 | bool kasan_check_range(unsigned long addr, size_t size, bool write, |
| 184 | unsigned long ret_ip) |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 185 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 186 | return check_region_inline(addr, size, write, ret_ip); |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 187 | } |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 188 | |
Andrey Konovalov | 611806b | 2021-02-24 12:05:50 -0800 | [diff] [blame] | 189 | bool kasan_byte_accessible(const void *addr) |
Andrey Konovalov | 2cdbed6 | 2020-12-22 12:00:46 -0800 | [diff] [blame] | 190 | { |
| 191 | s8 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(addr)); |
| 192 | |
Andrey Konovalov | 611806b | 2021-02-24 12:05:50 -0800 | [diff] [blame] | 193 | return shadow_byte >= 0 && shadow_byte < KASAN_GRANULE_SIZE; |
Andrey Konovalov | 2cdbed6 | 2020-12-22 12:00:46 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 196 | void kasan_cache_shrink(struct kmem_cache *cache) |
| 197 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 198 | kasan_quarantine_remove_cache(cache); |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Greg Thelen | f9fa1d9 | 2017-02-24 15:00:05 -0800 | [diff] [blame] | 201 | void kasan_cache_shutdown(struct kmem_cache *cache) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 202 | { |
Shakeel Butt | f9e13c0 | 2018-04-05 16:21:57 -0700 | [diff] [blame] | 203 | if (!__kmem_cache_empty(cache)) |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 204 | kasan_quarantine_remove_cache(cache); |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 207 | static void register_global(struct kasan_global *global) |
| 208 | { |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 209 | size_t aligned_size = round_up(global->size, KASAN_GRANULE_SIZE); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 210 | |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame^] | 211 | kasan_unpoison(global->beg, global->size, false); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 212 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 213 | kasan_poison(global->beg + aligned_size, |
Andrey Konovalov | cebd0eb | 2020-12-22 12:00:21 -0800 | [diff] [blame] | 214 | global->size_with_redzone - aligned_size, |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame^] | 215 | KASAN_GLOBAL_REDZONE, false); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void __asan_register_globals(struct kasan_global *globals, size_t size) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < size; i++) |
| 223 | register_global(&globals[i]); |
| 224 | } |
| 225 | EXPORT_SYMBOL(__asan_register_globals); |
| 226 | |
| 227 | void __asan_unregister_globals(struct kasan_global *globals, size_t size) |
| 228 | { |
| 229 | } |
| 230 | EXPORT_SYMBOL(__asan_unregister_globals); |
| 231 | |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 232 | #define DEFINE_ASAN_LOAD_STORE(size) \ |
| 233 | void __asan_load##size(unsigned long addr) \ |
| 234 | { \ |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 235 | check_region_inline(addr, size, false, _RET_IP_); \ |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 236 | } \ |
| 237 | EXPORT_SYMBOL(__asan_load##size); \ |
| 238 | __alias(__asan_load##size) \ |
| 239 | void __asan_load##size##_noabort(unsigned long); \ |
| 240 | EXPORT_SYMBOL(__asan_load##size##_noabort); \ |
| 241 | void __asan_store##size(unsigned long addr) \ |
| 242 | { \ |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 243 | check_region_inline(addr, size, true, _RET_IP_); \ |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 244 | } \ |
| 245 | EXPORT_SYMBOL(__asan_store##size); \ |
| 246 | __alias(__asan_store##size) \ |
| 247 | void __asan_store##size##_noabort(unsigned long); \ |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 248 | EXPORT_SYMBOL(__asan_store##size##_noabort) |
| 249 | |
| 250 | DEFINE_ASAN_LOAD_STORE(1); |
| 251 | DEFINE_ASAN_LOAD_STORE(2); |
| 252 | DEFINE_ASAN_LOAD_STORE(4); |
| 253 | DEFINE_ASAN_LOAD_STORE(8); |
| 254 | DEFINE_ASAN_LOAD_STORE(16); |
| 255 | |
| 256 | void __asan_loadN(unsigned long addr, size_t size) |
| 257 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 258 | kasan_check_range(addr, size, false, _RET_IP_); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL(__asan_loadN); |
| 261 | |
| 262 | __alias(__asan_loadN) |
| 263 | void __asan_loadN_noabort(unsigned long, size_t); |
| 264 | EXPORT_SYMBOL(__asan_loadN_noabort); |
| 265 | |
| 266 | void __asan_storeN(unsigned long addr, size_t size) |
| 267 | { |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 268 | kasan_check_range(addr, size, true, _RET_IP_); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 269 | } |
| 270 | EXPORT_SYMBOL(__asan_storeN); |
| 271 | |
| 272 | __alias(__asan_storeN) |
| 273 | void __asan_storeN_noabort(unsigned long, size_t); |
| 274 | EXPORT_SYMBOL(__asan_storeN_noabort); |
| 275 | |
| 276 | /* to shut up compiler complaints */ |
| 277 | void __asan_handle_no_return(void) {} |
| 278 | EXPORT_SYMBOL(__asan_handle_no_return); |
Andrey Ryabinin | 786a895 | 2015-02-13 14:39:21 -0800 | [diff] [blame] | 279 | |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 280 | /* Emitted by compiler to poison alloca()ed objects. */ |
| 281 | void __asan_alloca_poison(unsigned long addr, size_t size) |
| 282 | { |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 283 | size_t rounded_up_size = round_up(size, KASAN_GRANULE_SIZE); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 284 | size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) - |
| 285 | rounded_up_size; |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 286 | size_t rounded_down_size = round_down(size, KASAN_GRANULE_SIZE); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 287 | |
| 288 | const void *left_redzone = (const void *)(addr - |
| 289 | KASAN_ALLOCA_REDZONE_SIZE); |
| 290 | const void *right_redzone = (const void *)(addr + rounded_up_size); |
| 291 | |
| 292 | WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE)); |
| 293 | |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 294 | kasan_unpoison((const void *)(addr + rounded_down_size), |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame^] | 295 | size - rounded_down_size, false); |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 296 | kasan_poison(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame^] | 297 | KASAN_ALLOCA_LEFT, false); |
Andrey Konovalov | f00748b | 2021-02-24 12:05:05 -0800 | [diff] [blame] | 298 | kasan_poison(right_redzone, padding_size + KASAN_ALLOCA_REDZONE_SIZE, |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame^] | 299 | KASAN_ALLOCA_RIGHT, false); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 300 | } |
| 301 | EXPORT_SYMBOL(__asan_alloca_poison); |
| 302 | |
| 303 | /* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */ |
| 304 | void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) |
| 305 | { |
| 306 | if (unlikely(!stack_top || stack_top > stack_bottom)) |
| 307 | return; |
| 308 | |
Andrey Konovalov | aa5c219 | 2021-04-29 22:59:59 -0700 | [diff] [blame^] | 309 | kasan_unpoison(stack_top, stack_bottom - stack_top, false); |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 310 | } |
| 311 | EXPORT_SYMBOL(__asan_allocas_unpoison); |
| 312 | |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 313 | /* Emitted by the compiler to [un]poison local variables. */ |
| 314 | #define DEFINE_ASAN_SET_SHADOW(byte) \ |
| 315 | void __asan_set_shadow_##byte(const void *addr, size_t size) \ |
| 316 | { \ |
| 317 | __memset((void *)addr, 0x##byte, size); \ |
| 318 | } \ |
| 319 | EXPORT_SYMBOL(__asan_set_shadow_##byte) |
| 320 | |
| 321 | DEFINE_ASAN_SET_SHADOW(00); |
| 322 | DEFINE_ASAN_SET_SHADOW(f1); |
| 323 | DEFINE_ASAN_SET_SHADOW(f2); |
| 324 | DEFINE_ASAN_SET_SHADOW(f3); |
| 325 | DEFINE_ASAN_SET_SHADOW(f5); |
| 326 | DEFINE_ASAN_SET_SHADOW(f8); |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 327 | |
| 328 | void kasan_record_aux_stack(void *addr) |
| 329 | { |
| 330 | struct page *page = kasan_addr_to_page(addr); |
| 331 | struct kmem_cache *cache; |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 332 | struct kasan_alloc_meta *alloc_meta; |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 333 | void *object; |
| 334 | |
Alexander Potapenko | 2b83052 | 2021-02-25 17:19:21 -0800 | [diff] [blame] | 335 | if (is_kfence_address(addr) || !(page && PageSlab(page))) |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 336 | return; |
| 337 | |
| 338 | cache = page->slab_cache; |
| 339 | object = nearest_obj(cache, page, addr); |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 340 | alloc_meta = kasan_get_alloc_meta(cache, object); |
Walter Wu | 13384f6 | 2020-12-29 15:14:46 -0800 | [diff] [blame] | 341 | if (!alloc_meta) |
| 342 | return; |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 343 | |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 344 | alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; |
| 345 | alloc_meta->aux_stack[0] = kasan_save_stack(GFP_NOWAIT); |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 346 | } |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 347 | |
| 348 | void kasan_set_free_info(struct kmem_cache *cache, |
| 349 | void *object, u8 tag) |
| 350 | { |
| 351 | struct kasan_free_meta *free_meta; |
| 352 | |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 353 | free_meta = kasan_get_free_meta(cache, object); |
Andrey Konovalov | 97593ca | 2020-12-22 12:03:28 -0800 | [diff] [blame] | 354 | if (!free_meta) |
| 355 | return; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 356 | |
Andrey Konovalov | 97593ca | 2020-12-22 12:03:28 -0800 | [diff] [blame] | 357 | kasan_set_track(&free_meta->free_track, GFP_NOWAIT); |
| 358 | /* The object was freed and has free track set. */ |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 359 | *(u8 *)kasan_mem_to_shadow(object) = KASAN_KMALLOC_FREETRACK; |
| 360 | } |
| 361 | |
| 362 | struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, |
| 363 | void *object, u8 tag) |
| 364 | { |
| 365 | if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_KMALLOC_FREETRACK) |
| 366 | return NULL; |
Andrey Konovalov | 97593ca | 2020-12-22 12:03:28 -0800 | [diff] [blame] | 367 | /* Free meta must be present with KASAN_KMALLOC_FREETRACK. */ |
Andrey Konovalov | 6476792 | 2020-12-22 12:02:34 -0800 | [diff] [blame] | 368 | return &kasan_get_free_meta(cache, object)->free_track; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 369 | } |