1 1.28 riastrad /* $NetBSD: subr_asan.c,v 1.28 2023/04/09 09:18:09 riastradh Exp $ */ 2 1.1 maxv 3 1.1 maxv /* 4 1.26 maxv * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net 5 1.1 maxv * All rights reserved. 6 1.1 maxv * 7 1.26 maxv * This code is part of the KASAN subsystem of the NetBSD kernel. 8 1.1 maxv * 9 1.1 maxv * Redistribution and use in source and binary forms, with or without 10 1.1 maxv * modification, are permitted provided that the following conditions 11 1.1 maxv * are met: 12 1.1 maxv * 1. Redistributions of source code must retain the above copyright 13 1.1 maxv * notice, this list of conditions and the following disclaimer. 14 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 maxv * notice, this list of conditions and the following disclaimer in the 16 1.1 maxv * documentation and/or other materials provided with the distribution. 17 1.1 maxv * 18 1.26 maxv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 1.26 maxv * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 1.26 maxv * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 1.26 maxv * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 1.26 maxv * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 1.26 maxv * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 1.26 maxv * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 1.26 maxv * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 1.26 maxv * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 1.26 maxv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 1.26 maxv * SUCH DAMAGE. 29 1.1 maxv */ 30 1.1 maxv 31 1.1 maxv #include <sys/cdefs.h> 32 1.28 riastrad __KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.28 2023/04/09 09:18:09 riastradh Exp $"); 33 1.1 maxv 34 1.1 maxv #include <sys/param.h> 35 1.1 maxv #include <sys/device.h> 36 1.1 maxv #include <sys/kernel.h> 37 1.1 maxv #include <sys/param.h> 38 1.1 maxv #include <sys/conf.h> 39 1.1 maxv #include <sys/systm.h> 40 1.1 maxv #include <sys/types.h> 41 1.1 maxv #include <sys/asan.h> 42 1.1 maxv 43 1.25 riastrad #include <uvm/uvm_extern.h> 44 1.1 maxv 45 1.27 martin #ifdef DDB 46 1.27 martin #include <machine/db_machdep.h> 47 1.27 martin #include <ddb/db_extern.h> 48 1.27 martin #endif 49 1.27 martin 50 1.10 maxv #ifdef KASAN_PANIC 51 1.10 maxv #define REPORT panic 52 1.10 maxv #else 53 1.10 maxv #define REPORT printf 54 1.10 maxv #endif 55 1.10 maxv 56 1.1 maxv /* ASAN constants. Part of the compiler ABI. */ 57 1.1 maxv #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) 58 1.1 maxv #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) 59 1.19 maxv #define KASAN_ALLOCA_SCALE_SIZE 32 60 1.1 maxv 61 1.1 maxv /* The MD code. */ 62 1.1 maxv #include <machine/asan.h> 63 1.1 maxv 64 1.1 maxv /* ASAN ABI version. */ 65 1.1 maxv #if defined(__clang__) && (__clang_major__ - 0 >= 6) 66 1.1 maxv #define ASAN_ABI_VERSION 8 67 1.1 maxv #elif __GNUC_PREREQ__(7, 1) && !defined(__clang__) 68 1.1 maxv #define ASAN_ABI_VERSION 8 69 1.1 maxv #elif __GNUC_PREREQ__(6, 1) && !defined(__clang__) 70 1.1 maxv #define ASAN_ABI_VERSION 6 71 1.1 maxv #else 72 1.1 maxv #error "Unsupported compiler version" 73 1.1 maxv #endif 74 1.1 maxv 75 1.1 maxv #define __RET_ADDR (unsigned long)__builtin_return_address(0) 76 1.1 maxv 77 1.1 maxv /* Global variable descriptor. Part of the compiler ABI. */ 78 1.1 maxv struct __asan_global_source_location { 79 1.1 maxv const char *filename; 80 1.1 maxv int line_no; 81 1.1 maxv int column_no; 82 1.1 maxv }; 83 1.1 maxv struct __asan_global { 84 1.1 maxv const void *beg; /* address of the global variable */ 85 1.1 maxv size_t size; /* size of the global variable */ 86 1.1 maxv size_t size_with_redzone; /* size with the redzone */ 87 1.1 maxv const void *name; /* name of the variable */ 88 1.1 maxv const void *module_name; /* name of the module where the var is declared */ 89 1.1 maxv unsigned long has_dynamic_init; /* the var has dyn initializer (c++) */ 90 1.1 maxv struct __asan_global_source_location *location; 91 1.1 maxv #if ASAN_ABI_VERSION >= 7 92 1.1 maxv uintptr_t odr_indicator; /* the address of the ODR indicator symbol */ 93 1.1 maxv #endif 94 1.1 maxv }; 95 1.1 maxv 96 1.1 maxv static bool kasan_enabled __read_mostly = false; 97 1.1 maxv 98 1.1 maxv /* -------------------------------------------------------------------------- */ 99 1.1 maxv 100 1.1 maxv void 101 1.1 maxv kasan_shadow_map(void *addr, size_t size) 102 1.1 maxv { 103 1.1 maxv size_t sz, npages, i; 104 1.1 maxv vaddr_t sva, eva; 105 1.1 maxv 106 1.1 maxv KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0); 107 1.1 maxv 108 1.1 maxv sz = roundup(size, KASAN_SHADOW_SCALE_SIZE) / KASAN_SHADOW_SCALE_SIZE; 109 1.1 maxv 110 1.1 maxv sva = (vaddr_t)kasan_md_addr_to_shad(addr); 111 1.1 maxv eva = (vaddr_t)kasan_md_addr_to_shad(addr) + sz; 112 1.1 maxv 113 1.1 maxv sva = rounddown(sva, PAGE_SIZE); 114 1.1 maxv eva = roundup(eva, PAGE_SIZE); 115 1.1 maxv 116 1.1 maxv npages = (eva - sva) / PAGE_SIZE; 117 1.1 maxv 118 1.28 riastrad KASSERT(sva >= KASAN_MD_SHADOW_START); 119 1.28 riastrad KASSERT(eva < KASAN_MD_SHADOW_END); 120 1.1 maxv 121 1.1 maxv for (i = 0; i < npages; i++) { 122 1.1 maxv kasan_md_shadow_map_page(sva + i * PAGE_SIZE); 123 1.1 maxv } 124 1.1 maxv } 125 1.1 maxv 126 1.1 maxv static void 127 1.1 maxv kasan_ctors(void) 128 1.1 maxv { 129 1.23 skrll extern Elf_Addr __CTOR_LIST__, __CTOR_END__; 130 1.1 maxv size_t nentries, i; 131 1.23 skrll Elf_Addr *ptr; 132 1.1 maxv 133 1.1 maxv nentries = ((size_t)&__CTOR_END__ - (size_t)&__CTOR_LIST__) / 134 1.1 maxv sizeof(uintptr_t); 135 1.1 maxv 136 1.1 maxv ptr = &__CTOR_LIST__; 137 1.1 maxv for (i = 0; i < nentries; i++) { 138 1.1 maxv void (*func)(void); 139 1.1 maxv 140 1.1 maxv func = (void *)(*ptr); 141 1.1 maxv (*func)(); 142 1.1 maxv 143 1.1 maxv ptr++; 144 1.1 maxv } 145 1.1 maxv } 146 1.1 maxv 147 1.1 maxv void 148 1.1 maxv kasan_early_init(void *stack) 149 1.1 maxv { 150 1.1 maxv kasan_md_early_init(stack); 151 1.1 maxv } 152 1.1 maxv 153 1.1 maxv void 154 1.1 maxv kasan_init(void) 155 1.1 maxv { 156 1.1 maxv /* MD initialization. */ 157 1.1 maxv kasan_md_init(); 158 1.1 maxv 159 1.1 maxv /* Now officially enabled. */ 160 1.1 maxv kasan_enabled = true; 161 1.1 maxv 162 1.1 maxv /* Call the ASAN constructors. */ 163 1.1 maxv kasan_ctors(); 164 1.1 maxv } 165 1.1 maxv 166 1.5 maxv static inline const char * 167 1.5 maxv kasan_code_name(uint8_t code) 168 1.5 maxv { 169 1.5 maxv switch (code) { 170 1.6 maxv case KASAN_GENERIC_REDZONE: 171 1.6 maxv return "GenericRedZone"; 172 1.6 maxv case KASAN_MALLOC_REDZONE: 173 1.6 maxv return "MallocRedZone"; 174 1.6 maxv case KASAN_KMEM_REDZONE: 175 1.6 maxv return "KmemRedZone"; 176 1.6 maxv case KASAN_POOL_REDZONE: 177 1.6 maxv return "PoolRedZone"; 178 1.6 maxv case KASAN_POOL_FREED: 179 1.6 maxv return "PoolUseAfterFree"; 180 1.5 maxv case 1 ... 7: 181 1.5 maxv return "RedZonePartial"; 182 1.5 maxv case KASAN_STACK_LEFT: 183 1.5 maxv return "StackLeft"; 184 1.18 maxv case KASAN_STACK_MID: 185 1.18 maxv return "StackMiddle"; 186 1.5 maxv case KASAN_STACK_RIGHT: 187 1.5 maxv return "StackRight"; 188 1.18 maxv case KASAN_USE_AFTER_RET: 189 1.18 maxv return "UseAfterRet"; 190 1.5 maxv case KASAN_USE_AFTER_SCOPE: 191 1.5 maxv return "UseAfterScope"; 192 1.5 maxv default: 193 1.5 maxv return "Unknown"; 194 1.5 maxv } 195 1.5 maxv } 196 1.5 maxv 197 1.1 maxv static void 198 1.5 maxv kasan_report(unsigned long addr, size_t size, bool write, unsigned long pc, 199 1.5 maxv uint8_t code) 200 1.1 maxv { 201 1.10 maxv REPORT("ASan: Unauthorized Access In %p: Addr %p [%zu byte%s, %s," 202 1.5 maxv " %s]\n", 203 1.1 maxv (void *)pc, (void *)addr, size, (size > 1 ? "s" : ""), 204 1.5 maxv (write ? "write" : "read"), kasan_code_name(code)); 205 1.1 maxv kasan_md_unwind(); 206 1.1 maxv } 207 1.1 maxv 208 1.1 maxv static __always_inline void 209 1.1 maxv kasan_shadow_1byte_markvalid(unsigned long addr) 210 1.1 maxv { 211 1.1 maxv int8_t *byte = kasan_md_addr_to_shad((void *)addr); 212 1.1 maxv int8_t last = (addr & KASAN_SHADOW_MASK) + 1; 213 1.1 maxv 214 1.1 maxv *byte = last; 215 1.1 maxv } 216 1.1 maxv 217 1.1 maxv static __always_inline void 218 1.4 maxv kasan_shadow_Nbyte_markvalid(const void *addr, size_t size) 219 1.4 maxv { 220 1.4 maxv size_t i; 221 1.4 maxv 222 1.4 maxv for (i = 0; i < size; i++) { 223 1.4 maxv kasan_shadow_1byte_markvalid((unsigned long)addr+i); 224 1.4 maxv } 225 1.4 maxv } 226 1.4 maxv 227 1.4 maxv static __always_inline void 228 1.6 maxv kasan_shadow_Nbyte_fill(const void *addr, size_t size, uint8_t code) 229 1.1 maxv { 230 1.1 maxv void *shad; 231 1.1 maxv 232 1.1 maxv if (__predict_false(size == 0)) 233 1.1 maxv return; 234 1.1 maxv if (__predict_false(kasan_md_unsupported((vaddr_t)addr))) 235 1.1 maxv return; 236 1.1 maxv 237 1.1 maxv KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0); 238 1.1 maxv KASSERT(size % KASAN_SHADOW_SCALE_SIZE == 0); 239 1.1 maxv 240 1.1 maxv shad = (void *)kasan_md_addr_to_shad(addr); 241 1.1 maxv size = size >> KASAN_SHADOW_SCALE_SHIFT; 242 1.1 maxv 243 1.6 maxv __builtin_memset(shad, code, size); 244 1.1 maxv } 245 1.1 maxv 246 1.1 maxv void 247 1.1 maxv kasan_add_redzone(size_t *size) 248 1.1 maxv { 249 1.1 maxv *size = roundup(*size, KASAN_SHADOW_SCALE_SIZE); 250 1.1 maxv *size += KASAN_SHADOW_SCALE_SIZE; 251 1.1 maxv } 252 1.1 maxv 253 1.1 maxv void 254 1.1 maxv kasan_softint(struct lwp *l) 255 1.1 maxv { 256 1.1 maxv const void *stk = (const void *)uvm_lwp_getuarea(l); 257 1.1 maxv 258 1.1 maxv kasan_shadow_Nbyte_fill(stk, USPACE, 0); 259 1.1 maxv } 260 1.1 maxv 261 1.2 maxv /* 262 1.2 maxv * In an area of size 'sz_with_redz', mark the 'size' first bytes as valid, 263 1.2 maxv * and the rest as invalid. There are generally two use cases: 264 1.2 maxv * 265 1.6 maxv * o kasan_mark(addr, origsize, size, code), with origsize < size. This marks 266 1.6 maxv * the redzone at the end of the buffer as invalid. 267 1.2 maxv * 268 1.6 maxv * o kasan_mark(addr, size, size, 0). This marks the entire buffer as valid. 269 1.2 maxv */ 270 1.1 maxv void 271 1.6 maxv kasan_mark(const void *addr, size_t size, size_t sz_with_redz, uint8_t code) 272 1.1 maxv { 273 1.9 maxv size_t i, n, redz; 274 1.9 maxv int8_t *shad; 275 1.9 maxv 276 1.9 maxv KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0); 277 1.9 maxv redz = sz_with_redz - roundup(size, KASAN_SHADOW_SCALE_SIZE); 278 1.9 maxv KASSERT(redz % KASAN_SHADOW_SCALE_SIZE == 0); 279 1.9 maxv shad = kasan_md_addr_to_shad(addr); 280 1.9 maxv 281 1.9 maxv /* Chunks of 8 bytes, valid. */ 282 1.9 maxv n = size / KASAN_SHADOW_SCALE_SIZE; 283 1.9 maxv for (i = 0; i < n; i++) { 284 1.9 maxv *shad++ = 0; 285 1.9 maxv } 286 1.9 maxv 287 1.9 maxv /* Possibly one chunk, mid. */ 288 1.9 maxv if ((size & KASAN_SHADOW_MASK) != 0) { 289 1.9 maxv *shad++ = (size & KASAN_SHADOW_MASK); 290 1.9 maxv } 291 1.9 maxv 292 1.9 maxv /* Chunks of 8 bytes, invalid. */ 293 1.9 maxv n = redz / KASAN_SHADOW_SCALE_SIZE; 294 1.9 maxv for (i = 0; i < n; i++) { 295 1.9 maxv *shad++ = code; 296 1.9 maxv } 297 1.1 maxv } 298 1.1 maxv 299 1.1 maxv /* -------------------------------------------------------------------------- */ 300 1.1 maxv 301 1.1 maxv #define ADDR_CROSSES_SCALE_BOUNDARY(addr, size) \ 302 1.1 maxv (addr >> KASAN_SHADOW_SCALE_SHIFT) != \ 303 1.1 maxv ((addr + size - 1) >> KASAN_SHADOW_SCALE_SHIFT) 304 1.1 maxv 305 1.1 maxv static __always_inline bool 306 1.5 maxv kasan_shadow_1byte_isvalid(unsigned long addr, uint8_t *code) 307 1.1 maxv { 308 1.1 maxv int8_t *byte = kasan_md_addr_to_shad((void *)addr); 309 1.1 maxv int8_t last = (addr & KASAN_SHADOW_MASK) + 1; 310 1.1 maxv 311 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) { 312 1.5 maxv return true; 313 1.5 maxv } 314 1.5 maxv *code = *byte; 315 1.5 maxv return false; 316 1.1 maxv } 317 1.1 maxv 318 1.1 maxv static __always_inline bool 319 1.5 maxv kasan_shadow_2byte_isvalid(unsigned long addr, uint8_t *code) 320 1.1 maxv { 321 1.1 maxv int8_t *byte, last; 322 1.1 maxv 323 1.1 maxv if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 2)) { 324 1.5 maxv return (kasan_shadow_1byte_isvalid(addr, code) && 325 1.5 maxv kasan_shadow_1byte_isvalid(addr+1, code)); 326 1.1 maxv } 327 1.1 maxv 328 1.1 maxv byte = kasan_md_addr_to_shad((void *)addr); 329 1.1 maxv last = ((addr + 1) & KASAN_SHADOW_MASK) + 1; 330 1.1 maxv 331 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) { 332 1.5 maxv return true; 333 1.5 maxv } 334 1.5 maxv *code = *byte; 335 1.5 maxv return false; 336 1.1 maxv } 337 1.1 maxv 338 1.1 maxv static __always_inline bool 339 1.5 maxv kasan_shadow_4byte_isvalid(unsigned long addr, uint8_t *code) 340 1.1 maxv { 341 1.1 maxv int8_t *byte, last; 342 1.1 maxv 343 1.1 maxv if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 4)) { 344 1.5 maxv return (kasan_shadow_2byte_isvalid(addr, code) && 345 1.5 maxv kasan_shadow_2byte_isvalid(addr+2, code)); 346 1.1 maxv } 347 1.1 maxv 348 1.1 maxv byte = kasan_md_addr_to_shad((void *)addr); 349 1.1 maxv last = ((addr + 3) & KASAN_SHADOW_MASK) + 1; 350 1.1 maxv 351 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) { 352 1.5 maxv return true; 353 1.5 maxv } 354 1.5 maxv *code = *byte; 355 1.5 maxv return false; 356 1.1 maxv } 357 1.1 maxv 358 1.1 maxv static __always_inline bool 359 1.5 maxv kasan_shadow_8byte_isvalid(unsigned long addr, uint8_t *code) 360 1.1 maxv { 361 1.1 maxv int8_t *byte, last; 362 1.1 maxv 363 1.1 maxv if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 8)) { 364 1.5 maxv return (kasan_shadow_4byte_isvalid(addr, code) && 365 1.5 maxv kasan_shadow_4byte_isvalid(addr+4, code)); 366 1.1 maxv } 367 1.1 maxv 368 1.1 maxv byte = kasan_md_addr_to_shad((void *)addr); 369 1.1 maxv last = ((addr + 7) & KASAN_SHADOW_MASK) + 1; 370 1.1 maxv 371 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) { 372 1.5 maxv return true; 373 1.5 maxv } 374 1.5 maxv *code = *byte; 375 1.5 maxv return false; 376 1.1 maxv } 377 1.1 maxv 378 1.1 maxv static __always_inline bool 379 1.5 maxv kasan_shadow_Nbyte_isvalid(unsigned long addr, size_t size, uint8_t *code) 380 1.1 maxv { 381 1.1 maxv size_t i; 382 1.1 maxv 383 1.1 maxv for (i = 0; i < size; i++) { 384 1.5 maxv if (!kasan_shadow_1byte_isvalid(addr+i, code)) 385 1.1 maxv return false; 386 1.1 maxv } 387 1.1 maxv 388 1.1 maxv return true; 389 1.1 maxv } 390 1.1 maxv 391 1.1 maxv static __always_inline void 392 1.1 maxv kasan_shadow_check(unsigned long addr, size_t size, bool write, 393 1.1 maxv unsigned long retaddr) 394 1.1 maxv { 395 1.5 maxv uint8_t code; 396 1.1 maxv bool valid; 397 1.1 maxv 398 1.1 maxv if (__predict_false(!kasan_enabled)) 399 1.1 maxv return; 400 1.27 martin #ifdef DDB 401 1.27 martin if (__predict_false(db_recover != NULL)) 402 1.27 martin return; 403 1.27 martin #endif 404 1.1 maxv if (__predict_false(size == 0)) 405 1.1 maxv return; 406 1.1 maxv if (__predict_false(kasan_md_unsupported(addr))) 407 1.1 maxv return; 408 1.1 maxv 409 1.1 maxv if (__builtin_constant_p(size)) { 410 1.1 maxv switch (size) { 411 1.1 maxv case 1: 412 1.5 maxv valid = kasan_shadow_1byte_isvalid(addr, &code); 413 1.1 maxv break; 414 1.1 maxv case 2: 415 1.5 maxv valid = kasan_shadow_2byte_isvalid(addr, &code); 416 1.1 maxv break; 417 1.1 maxv case 4: 418 1.5 maxv valid = kasan_shadow_4byte_isvalid(addr, &code); 419 1.1 maxv break; 420 1.1 maxv case 8: 421 1.5 maxv valid = kasan_shadow_8byte_isvalid(addr, &code); 422 1.1 maxv break; 423 1.1 maxv default: 424 1.5 maxv valid = kasan_shadow_Nbyte_isvalid(addr, size, &code); 425 1.1 maxv break; 426 1.1 maxv } 427 1.1 maxv } else { 428 1.5 maxv valid = kasan_shadow_Nbyte_isvalid(addr, size, &code); 429 1.1 maxv } 430 1.1 maxv 431 1.1 maxv if (__predict_false(!valid)) { 432 1.5 maxv kasan_report(addr, size, write, retaddr, code); 433 1.1 maxv } 434 1.1 maxv } 435 1.1 maxv 436 1.1 maxv /* -------------------------------------------------------------------------- */ 437 1.1 maxv 438 1.1 maxv void * 439 1.1 maxv kasan_memcpy(void *dst, const void *src, size_t len) 440 1.1 maxv { 441 1.1 maxv kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR); 442 1.1 maxv kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR); 443 1.1 maxv return __builtin_memcpy(dst, src, len); 444 1.1 maxv } 445 1.1 maxv 446 1.1 maxv int 447 1.1 maxv kasan_memcmp(const void *b1, const void *b2, size_t len) 448 1.1 maxv { 449 1.1 maxv kasan_shadow_check((unsigned long)b1, len, false, __RET_ADDR); 450 1.1 maxv kasan_shadow_check((unsigned long)b2, len, false, __RET_ADDR); 451 1.1 maxv return __builtin_memcmp(b1, b2, len); 452 1.1 maxv } 453 1.1 maxv 454 1.1 maxv void * 455 1.1 maxv kasan_memset(void *b, int c, size_t len) 456 1.1 maxv { 457 1.1 maxv kasan_shadow_check((unsigned long)b, len, true, __RET_ADDR); 458 1.1 maxv return __builtin_memset(b, c, len); 459 1.1 maxv } 460 1.1 maxv 461 1.12 maxv void * 462 1.12 maxv kasan_memmove(void *dst, const void *src, size_t len) 463 1.12 maxv { 464 1.12 maxv kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR); 465 1.12 maxv kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR); 466 1.12 maxv return __builtin_memmove(dst, src, len); 467 1.12 maxv } 468 1.12 maxv 469 1.1 maxv char * 470 1.1 maxv kasan_strcpy(char *dst, const char *src) 471 1.1 maxv { 472 1.1 maxv char *save = dst; 473 1.1 maxv 474 1.1 maxv while (1) { 475 1.1 maxv kasan_shadow_check((unsigned long)src, 1, false, __RET_ADDR); 476 1.1 maxv kasan_shadow_check((unsigned long)dst, 1, true, __RET_ADDR); 477 1.1 maxv *dst = *src; 478 1.1 maxv if (*src == '\0') 479 1.1 maxv break; 480 1.1 maxv src++, dst++; 481 1.1 maxv } 482 1.1 maxv 483 1.1 maxv return save; 484 1.1 maxv } 485 1.1 maxv 486 1.1 maxv int 487 1.1 maxv kasan_strcmp(const char *s1, const char *s2) 488 1.1 maxv { 489 1.1 maxv while (1) { 490 1.1 maxv kasan_shadow_check((unsigned long)s1, 1, false, __RET_ADDR); 491 1.1 maxv kasan_shadow_check((unsigned long)s2, 1, false, __RET_ADDR); 492 1.1 maxv if (*s1 != *s2) 493 1.1 maxv break; 494 1.1 maxv if (*s1 == '\0') 495 1.1 maxv return 0; 496 1.1 maxv s1++, s2++; 497 1.1 maxv } 498 1.1 maxv 499 1.1 maxv return (*(const unsigned char *)s1 - *(const unsigned char *)s2); 500 1.1 maxv } 501 1.1 maxv 502 1.1 maxv size_t 503 1.1 maxv kasan_strlen(const char *str) 504 1.1 maxv { 505 1.1 maxv const char *s; 506 1.1 maxv 507 1.1 maxv s = str; 508 1.1 maxv while (1) { 509 1.1 maxv kasan_shadow_check((unsigned long)s, 1, false, __RET_ADDR); 510 1.1 maxv if (*s == '\0') 511 1.1 maxv break; 512 1.1 maxv s++; 513 1.1 maxv } 514 1.1 maxv 515 1.1 maxv return (s - str); 516 1.1 maxv } 517 1.1 maxv 518 1.20 maxv char * 519 1.20 maxv kasan_strcat(char *dst, const char *src) 520 1.20 maxv { 521 1.20 maxv size_t ldst, lsrc; 522 1.20 maxv 523 1.20 maxv ldst = __builtin_strlen(dst); 524 1.20 maxv lsrc = __builtin_strlen(src); 525 1.20 maxv kasan_shadow_check((unsigned long)dst, ldst + lsrc + 1, true, 526 1.20 maxv __RET_ADDR); 527 1.20 maxv kasan_shadow_check((unsigned long)src, lsrc + 1, false, 528 1.20 maxv __RET_ADDR); 529 1.20 maxv 530 1.20 maxv return __builtin_strcat(dst, src); 531 1.20 maxv } 532 1.20 maxv 533 1.20 maxv char * 534 1.20 maxv kasan_strchr(const char *s, int c) 535 1.20 maxv { 536 1.20 maxv kasan_shadow_check((unsigned long)s, __builtin_strlen(s) + 1, false, 537 1.20 maxv __RET_ADDR); 538 1.20 maxv return __builtin_strchr(s, c); 539 1.20 maxv } 540 1.20 maxv 541 1.20 maxv char * 542 1.20 maxv kasan_strrchr(const char *s, int c) 543 1.20 maxv { 544 1.20 maxv kasan_shadow_check((unsigned long)s, __builtin_strlen(s) + 1, false, 545 1.20 maxv __RET_ADDR); 546 1.20 maxv return __builtin_strrchr(s, c); 547 1.20 maxv } 548 1.20 maxv 549 1.8 maxv #undef kcopy 550 1.7 maxv #undef copyinstr 551 1.7 maxv #undef copyoutstr 552 1.7 maxv #undef copyin 553 1.7 maxv 554 1.8 maxv int kasan_kcopy(const void *, void *, size_t); 555 1.7 maxv int kasan_copyinstr(const void *, void *, size_t, size_t *); 556 1.7 maxv int kasan_copyoutstr(const void *, void *, size_t, size_t *); 557 1.7 maxv int kasan_copyin(const void *, void *, size_t); 558 1.8 maxv int kcopy(const void *, void *, size_t); 559 1.7 maxv int copyinstr(const void *, void *, size_t, size_t *); 560 1.7 maxv int copyoutstr(const void *, void *, size_t, size_t *); 561 1.7 maxv int copyin(const void *, void *, size_t); 562 1.7 maxv 563 1.7 maxv int 564 1.8 maxv kasan_kcopy(const void *src, void *dst, size_t len) 565 1.8 maxv { 566 1.8 maxv kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR); 567 1.8 maxv kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR); 568 1.8 maxv return kcopy(src, dst, len); 569 1.8 maxv } 570 1.8 maxv 571 1.8 maxv int 572 1.7 maxv kasan_copyin(const void *uaddr, void *kaddr, size_t len) 573 1.7 maxv { 574 1.7 maxv kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR); 575 1.7 maxv return copyin(uaddr, kaddr, len); 576 1.7 maxv } 577 1.7 maxv 578 1.7 maxv int 579 1.7 maxv kasan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) 580 1.7 maxv { 581 1.7 maxv kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR); 582 1.7 maxv return copyinstr(uaddr, kaddr, len, done); 583 1.7 maxv } 584 1.7 maxv 585 1.7 maxv int 586 1.7 maxv kasan_copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) 587 1.7 maxv { 588 1.7 maxv kasan_shadow_check((unsigned long)kaddr, len, false, __RET_ADDR); 589 1.7 maxv return copyoutstr(kaddr, uaddr, len, done); 590 1.7 maxv } 591 1.7 maxv 592 1.1 maxv /* -------------------------------------------------------------------------- */ 593 1.1 maxv 594 1.16 maxv #undef _ucas_32 595 1.16 maxv #undef _ucas_32_mp 596 1.16 maxv #undef _ucas_64 597 1.16 maxv #undef _ucas_64_mp 598 1.16 maxv #undef _ufetch_8 599 1.16 maxv #undef _ufetch_16 600 1.16 maxv #undef _ufetch_32 601 1.16 maxv #undef _ufetch_64 602 1.16 maxv 603 1.16 maxv int _ucas_32(volatile uint32_t *, uint32_t, uint32_t, uint32_t *); 604 1.16 maxv int kasan__ucas_32(volatile uint32_t *, uint32_t, uint32_t, uint32_t *); 605 1.16 maxv int 606 1.16 maxv kasan__ucas_32(volatile uint32_t *uaddr, uint32_t old, uint32_t new, 607 1.16 maxv uint32_t *ret) 608 1.16 maxv { 609 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true, 610 1.16 maxv __RET_ADDR); 611 1.16 maxv return _ucas_32(uaddr, old, new, ret); 612 1.16 maxv } 613 1.16 maxv 614 1.16 maxv #ifdef __HAVE_UCAS_MP 615 1.16 maxv int _ucas_32_mp(volatile uint32_t *, uint32_t, uint32_t, uint32_t *); 616 1.16 maxv int kasan__ucas_32_mp(volatile uint32_t *, uint32_t, uint32_t, uint32_t *); 617 1.16 maxv int 618 1.16 maxv kasan__ucas_32_mp(volatile uint32_t *uaddr, uint32_t old, uint32_t new, 619 1.16 maxv uint32_t *ret) 620 1.16 maxv { 621 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true, 622 1.16 maxv __RET_ADDR); 623 1.16 maxv return _ucas_32_mp(uaddr, old, new, ret); 624 1.16 maxv } 625 1.16 maxv #endif 626 1.16 maxv 627 1.16 maxv #ifdef _LP64 628 1.16 maxv int _ucas_64(volatile uint64_t *, uint64_t, uint64_t, uint64_t *); 629 1.16 maxv int kasan__ucas_64(volatile uint64_t *, uint64_t, uint64_t, uint64_t *); 630 1.16 maxv int 631 1.16 maxv kasan__ucas_64(volatile uint64_t *uaddr, uint64_t old, uint64_t new, 632 1.16 maxv uint64_t *ret) 633 1.16 maxv { 634 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true, 635 1.16 maxv __RET_ADDR); 636 1.16 maxv return _ucas_64(uaddr, old, new, ret); 637 1.16 maxv } 638 1.16 maxv 639 1.16 maxv #ifdef __HAVE_UCAS_MP 640 1.16 maxv int _ucas_64_mp(volatile uint64_t *, uint64_t, uint64_t, uint64_t *); 641 1.16 maxv int kasan__ucas_64_mp(volatile uint64_t *, uint64_t, uint64_t, uint64_t *); 642 1.16 maxv int 643 1.16 maxv kasan__ucas_64_mp(volatile uint64_t *uaddr, uint64_t old, uint64_t new, 644 1.16 maxv uint64_t *ret) 645 1.16 maxv { 646 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true, 647 1.16 maxv __RET_ADDR); 648 1.16 maxv return _ucas_64_mp(uaddr, old, new, ret); 649 1.16 maxv } 650 1.16 maxv #endif 651 1.16 maxv #endif 652 1.16 maxv 653 1.16 maxv int _ufetch_8(const uint8_t *, uint8_t *); 654 1.16 maxv int kasan__ufetch_8(const uint8_t *, uint8_t *); 655 1.16 maxv int 656 1.16 maxv kasan__ufetch_8(const uint8_t *uaddr, uint8_t *valp) 657 1.16 maxv { 658 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true, 659 1.16 maxv __RET_ADDR); 660 1.16 maxv return _ufetch_8(uaddr, valp); 661 1.16 maxv } 662 1.16 maxv 663 1.16 maxv int _ufetch_16(const uint16_t *, uint16_t *); 664 1.16 maxv int kasan__ufetch_16(const uint16_t *, uint16_t *); 665 1.16 maxv int 666 1.16 maxv kasan__ufetch_16(const uint16_t *uaddr, uint16_t *valp) 667 1.16 maxv { 668 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true, 669 1.16 maxv __RET_ADDR); 670 1.16 maxv return _ufetch_16(uaddr, valp); 671 1.16 maxv } 672 1.16 maxv 673 1.16 maxv int _ufetch_32(const uint32_t *, uint32_t *); 674 1.16 maxv int kasan__ufetch_32(const uint32_t *, uint32_t *); 675 1.16 maxv int 676 1.16 maxv kasan__ufetch_32(const uint32_t *uaddr, uint32_t *valp) 677 1.16 maxv { 678 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true, 679 1.16 maxv __RET_ADDR); 680 1.16 maxv return _ufetch_32(uaddr, valp); 681 1.16 maxv } 682 1.16 maxv 683 1.16 maxv #ifdef _LP64 684 1.16 maxv int _ufetch_64(const uint64_t *, uint64_t *); 685 1.16 maxv int kasan__ufetch_64(const uint64_t *, uint64_t *); 686 1.16 maxv int 687 1.16 maxv kasan__ufetch_64(const uint64_t *uaddr, uint64_t *valp) 688 1.16 maxv { 689 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true, 690 1.16 maxv __RET_ADDR); 691 1.16 maxv return _ufetch_64(uaddr, valp); 692 1.16 maxv } 693 1.16 maxv #endif 694 1.16 maxv 695 1.16 maxv /* -------------------------------------------------------------------------- */ 696 1.16 maxv 697 1.11 maxv #undef atomic_add_32 698 1.11 maxv #undef atomic_add_int 699 1.11 maxv #undef atomic_add_long 700 1.11 maxv #undef atomic_add_ptr 701 1.11 maxv #undef atomic_add_64 702 1.11 maxv #undef atomic_add_32_nv 703 1.11 maxv #undef atomic_add_int_nv 704 1.11 maxv #undef atomic_add_long_nv 705 1.11 maxv #undef atomic_add_ptr_nv 706 1.11 maxv #undef atomic_add_64_nv 707 1.11 maxv #undef atomic_and_32 708 1.11 maxv #undef atomic_and_uint 709 1.11 maxv #undef atomic_and_ulong 710 1.11 maxv #undef atomic_and_64 711 1.11 maxv #undef atomic_and_32_nv 712 1.11 maxv #undef atomic_and_uint_nv 713 1.11 maxv #undef atomic_and_ulong_nv 714 1.11 maxv #undef atomic_and_64_nv 715 1.11 maxv #undef atomic_or_32 716 1.11 maxv #undef atomic_or_uint 717 1.11 maxv #undef atomic_or_ulong 718 1.11 maxv #undef atomic_or_64 719 1.11 maxv #undef atomic_or_32_nv 720 1.11 maxv #undef atomic_or_uint_nv 721 1.11 maxv #undef atomic_or_ulong_nv 722 1.11 maxv #undef atomic_or_64_nv 723 1.11 maxv #undef atomic_cas_32 724 1.11 maxv #undef atomic_cas_uint 725 1.11 maxv #undef atomic_cas_ulong 726 1.11 maxv #undef atomic_cas_ptr 727 1.11 maxv #undef atomic_cas_64 728 1.11 maxv #undef atomic_cas_32_ni 729 1.11 maxv #undef atomic_cas_uint_ni 730 1.11 maxv #undef atomic_cas_ulong_ni 731 1.11 maxv #undef atomic_cas_ptr_ni 732 1.11 maxv #undef atomic_cas_64_ni 733 1.11 maxv #undef atomic_swap_32 734 1.11 maxv #undef atomic_swap_uint 735 1.11 maxv #undef atomic_swap_ulong 736 1.11 maxv #undef atomic_swap_ptr 737 1.11 maxv #undef atomic_swap_64 738 1.11 maxv #undef atomic_dec_32 739 1.11 maxv #undef atomic_dec_uint 740 1.11 maxv #undef atomic_dec_ulong 741 1.11 maxv #undef atomic_dec_ptr 742 1.11 maxv #undef atomic_dec_64 743 1.11 maxv #undef atomic_dec_32_nv 744 1.11 maxv #undef atomic_dec_uint_nv 745 1.11 maxv #undef atomic_dec_ulong_nv 746 1.11 maxv #undef atomic_dec_ptr_nv 747 1.11 maxv #undef atomic_dec_64_nv 748 1.11 maxv #undef atomic_inc_32 749 1.11 maxv #undef atomic_inc_uint 750 1.11 maxv #undef atomic_inc_ulong 751 1.11 maxv #undef atomic_inc_ptr 752 1.11 maxv #undef atomic_inc_64 753 1.11 maxv #undef atomic_inc_32_nv 754 1.11 maxv #undef atomic_inc_uint_nv 755 1.11 maxv #undef atomic_inc_ulong_nv 756 1.11 maxv #undef atomic_inc_ptr_nv 757 1.11 maxv #undef atomic_inc_64_nv 758 1.11 maxv 759 1.11 maxv #define ASAN_ATOMIC_FUNC_ADD(name, tret, targ1, targ2) \ 760 1.11 maxv void atomic_add_##name(volatile targ1 *, targ2); \ 761 1.11 maxv void kasan_atomic_add_##name(volatile targ1 *, targ2); \ 762 1.11 maxv void kasan_atomic_add_##name(volatile targ1 *ptr, targ2 val) \ 763 1.11 maxv { \ 764 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 765 1.11 maxv __RET_ADDR); \ 766 1.11 maxv atomic_add_##name(ptr, val); \ 767 1.11 maxv } \ 768 1.11 maxv tret atomic_add_##name##_nv(volatile targ1 *, targ2); \ 769 1.11 maxv tret kasan_atomic_add_##name##_nv(volatile targ1 *, targ2); \ 770 1.11 maxv tret kasan_atomic_add_##name##_nv(volatile targ1 *ptr, targ2 val) \ 771 1.11 maxv { \ 772 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 773 1.11 maxv __RET_ADDR); \ 774 1.11 maxv return atomic_add_##name##_nv(ptr, val); \ 775 1.11 maxv } 776 1.11 maxv 777 1.11 maxv #define ASAN_ATOMIC_FUNC_AND(name, tret, targ1, targ2) \ 778 1.11 maxv void atomic_and_##name(volatile targ1 *, targ2); \ 779 1.11 maxv void kasan_atomic_and_##name(volatile targ1 *, targ2); \ 780 1.11 maxv void kasan_atomic_and_##name(volatile targ1 *ptr, targ2 val) \ 781 1.11 maxv { \ 782 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 783 1.11 maxv __RET_ADDR); \ 784 1.11 maxv atomic_and_##name(ptr, val); \ 785 1.11 maxv } \ 786 1.11 maxv tret atomic_and_##name##_nv(volatile targ1 *, targ2); \ 787 1.11 maxv tret kasan_atomic_and_##name##_nv(volatile targ1 *, targ2); \ 788 1.11 maxv tret kasan_atomic_and_##name##_nv(volatile targ1 *ptr, targ2 val) \ 789 1.11 maxv { \ 790 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 791 1.11 maxv __RET_ADDR); \ 792 1.11 maxv return atomic_and_##name##_nv(ptr, val); \ 793 1.11 maxv } 794 1.11 maxv 795 1.11 maxv #define ASAN_ATOMIC_FUNC_OR(name, tret, targ1, targ2) \ 796 1.11 maxv void atomic_or_##name(volatile targ1 *, targ2); \ 797 1.11 maxv void kasan_atomic_or_##name(volatile targ1 *, targ2); \ 798 1.11 maxv void kasan_atomic_or_##name(volatile targ1 *ptr, targ2 val) \ 799 1.11 maxv { \ 800 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 801 1.11 maxv __RET_ADDR); \ 802 1.11 maxv atomic_or_##name(ptr, val); \ 803 1.11 maxv } \ 804 1.11 maxv tret atomic_or_##name##_nv(volatile targ1 *, targ2); \ 805 1.11 maxv tret kasan_atomic_or_##name##_nv(volatile targ1 *, targ2); \ 806 1.11 maxv tret kasan_atomic_or_##name##_nv(volatile targ1 *ptr, targ2 val) \ 807 1.11 maxv { \ 808 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 809 1.11 maxv __RET_ADDR); \ 810 1.11 maxv return atomic_or_##name##_nv(ptr, val); \ 811 1.11 maxv } 812 1.11 maxv 813 1.11 maxv #define ASAN_ATOMIC_FUNC_CAS(name, tret, targ1, targ2) \ 814 1.11 maxv tret atomic_cas_##name(volatile targ1 *, targ2, targ2); \ 815 1.11 maxv tret kasan_atomic_cas_##name(volatile targ1 *, targ2, targ2); \ 816 1.11 maxv tret kasan_atomic_cas_##name(volatile targ1 *ptr, targ2 exp, targ2 new) \ 817 1.11 maxv { \ 818 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 819 1.11 maxv __RET_ADDR); \ 820 1.11 maxv return atomic_cas_##name(ptr, exp, new); \ 821 1.11 maxv } \ 822 1.11 maxv tret atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \ 823 1.11 maxv tret kasan_atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \ 824 1.11 maxv tret kasan_atomic_cas_##name##_ni(volatile targ1 *ptr, targ2 exp, targ2 new) \ 825 1.11 maxv { \ 826 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 827 1.11 maxv __RET_ADDR); \ 828 1.11 maxv return atomic_cas_##name##_ni(ptr, exp, new); \ 829 1.11 maxv } 830 1.11 maxv 831 1.11 maxv #define ASAN_ATOMIC_FUNC_SWAP(name, tret, targ1, targ2) \ 832 1.11 maxv tret atomic_swap_##name(volatile targ1 *, targ2); \ 833 1.11 maxv tret kasan_atomic_swap_##name(volatile targ1 *, targ2); \ 834 1.11 maxv tret kasan_atomic_swap_##name(volatile targ1 *ptr, targ2 val) \ 835 1.11 maxv { \ 836 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 837 1.11 maxv __RET_ADDR); \ 838 1.11 maxv return atomic_swap_##name(ptr, val); \ 839 1.11 maxv } 840 1.11 maxv 841 1.11 maxv #define ASAN_ATOMIC_FUNC_DEC(name, tret, targ1) \ 842 1.11 maxv void atomic_dec_##name(volatile targ1 *); \ 843 1.11 maxv void kasan_atomic_dec_##name(volatile targ1 *); \ 844 1.11 maxv void kasan_atomic_dec_##name(volatile targ1 *ptr) \ 845 1.11 maxv { \ 846 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 847 1.11 maxv __RET_ADDR); \ 848 1.11 maxv atomic_dec_##name(ptr); \ 849 1.11 maxv } \ 850 1.11 maxv tret atomic_dec_##name##_nv(volatile targ1 *); \ 851 1.11 maxv tret kasan_atomic_dec_##name##_nv(volatile targ1 *); \ 852 1.11 maxv tret kasan_atomic_dec_##name##_nv(volatile targ1 *ptr) \ 853 1.11 maxv { \ 854 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 855 1.11 maxv __RET_ADDR); \ 856 1.11 maxv return atomic_dec_##name##_nv(ptr); \ 857 1.11 maxv } 858 1.11 maxv 859 1.11 maxv #define ASAN_ATOMIC_FUNC_INC(name, tret, targ1) \ 860 1.11 maxv void atomic_inc_##name(volatile targ1 *); \ 861 1.11 maxv void kasan_atomic_inc_##name(volatile targ1 *); \ 862 1.11 maxv void kasan_atomic_inc_##name(volatile targ1 *ptr) \ 863 1.11 maxv { \ 864 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 865 1.11 maxv __RET_ADDR); \ 866 1.11 maxv atomic_inc_##name(ptr); \ 867 1.11 maxv } \ 868 1.11 maxv tret atomic_inc_##name##_nv(volatile targ1 *); \ 869 1.11 maxv tret kasan_atomic_inc_##name##_nv(volatile targ1 *); \ 870 1.11 maxv tret kasan_atomic_inc_##name##_nv(volatile targ1 *ptr) \ 871 1.11 maxv { \ 872 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \ 873 1.11 maxv __RET_ADDR); \ 874 1.11 maxv return atomic_inc_##name##_nv(ptr); \ 875 1.11 maxv } 876 1.11 maxv 877 1.11 maxv ASAN_ATOMIC_FUNC_ADD(32, uint32_t, uint32_t, int32_t); 878 1.11 maxv ASAN_ATOMIC_FUNC_ADD(64, uint64_t, uint64_t, int64_t); 879 1.11 maxv ASAN_ATOMIC_FUNC_ADD(int, unsigned int, unsigned int, int); 880 1.11 maxv ASAN_ATOMIC_FUNC_ADD(long, unsigned long, unsigned long, long); 881 1.11 maxv ASAN_ATOMIC_FUNC_ADD(ptr, void *, void, ssize_t); 882 1.11 maxv 883 1.11 maxv ASAN_ATOMIC_FUNC_AND(32, uint32_t, uint32_t, uint32_t); 884 1.11 maxv ASAN_ATOMIC_FUNC_AND(64, uint64_t, uint64_t, uint64_t); 885 1.11 maxv ASAN_ATOMIC_FUNC_AND(uint, unsigned int, unsigned int, unsigned int); 886 1.11 maxv ASAN_ATOMIC_FUNC_AND(ulong, unsigned long, unsigned long, unsigned long); 887 1.11 maxv 888 1.11 maxv ASAN_ATOMIC_FUNC_OR(32, uint32_t, uint32_t, uint32_t); 889 1.11 maxv ASAN_ATOMIC_FUNC_OR(64, uint64_t, uint64_t, uint64_t); 890 1.11 maxv ASAN_ATOMIC_FUNC_OR(uint, unsigned int, unsigned int, unsigned int); 891 1.11 maxv ASAN_ATOMIC_FUNC_OR(ulong, unsigned long, unsigned long, unsigned long); 892 1.11 maxv 893 1.11 maxv ASAN_ATOMIC_FUNC_CAS(32, uint32_t, uint32_t, uint32_t); 894 1.11 maxv ASAN_ATOMIC_FUNC_CAS(64, uint64_t, uint64_t, uint64_t); 895 1.11 maxv ASAN_ATOMIC_FUNC_CAS(uint, unsigned int, unsigned int, unsigned int); 896 1.11 maxv ASAN_ATOMIC_FUNC_CAS(ulong, unsigned long, unsigned long, unsigned long); 897 1.11 maxv ASAN_ATOMIC_FUNC_CAS(ptr, void *, void, void *); 898 1.11 maxv 899 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(32, uint32_t, uint32_t, uint32_t); 900 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(64, uint64_t, uint64_t, uint64_t); 901 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(uint, unsigned int, unsigned int, unsigned int); 902 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(ulong, unsigned long, unsigned long, unsigned long); 903 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(ptr, void *, void, void *); 904 1.11 maxv 905 1.11 maxv ASAN_ATOMIC_FUNC_DEC(32, uint32_t, uint32_t) 906 1.11 maxv ASAN_ATOMIC_FUNC_DEC(64, uint64_t, uint64_t) 907 1.11 maxv ASAN_ATOMIC_FUNC_DEC(uint, unsigned int, unsigned int); 908 1.11 maxv ASAN_ATOMIC_FUNC_DEC(ulong, unsigned long, unsigned long); 909 1.11 maxv ASAN_ATOMIC_FUNC_DEC(ptr, void *, void); 910 1.11 maxv 911 1.11 maxv ASAN_ATOMIC_FUNC_INC(32, uint32_t, uint32_t) 912 1.11 maxv ASAN_ATOMIC_FUNC_INC(64, uint64_t, uint64_t) 913 1.11 maxv ASAN_ATOMIC_FUNC_INC(uint, unsigned int, unsigned int); 914 1.11 maxv ASAN_ATOMIC_FUNC_INC(ulong, unsigned long, unsigned long); 915 1.11 maxv ASAN_ATOMIC_FUNC_INC(ptr, void *, void); 916 1.11 maxv 917 1.11 maxv /* -------------------------------------------------------------------------- */ 918 1.11 maxv 919 1.14 maxv #ifdef __HAVE_KASAN_INSTR_BUS 920 1.14 maxv 921 1.13 maxv #include <sys/bus.h> 922 1.13 maxv 923 1.13 maxv #undef bus_space_read_multi_1 924 1.13 maxv #undef bus_space_read_multi_2 925 1.13 maxv #undef bus_space_read_multi_4 926 1.13 maxv #undef bus_space_read_multi_8 927 1.13 maxv #undef bus_space_read_multi_stream_1 928 1.13 maxv #undef bus_space_read_multi_stream_2 929 1.13 maxv #undef bus_space_read_multi_stream_4 930 1.13 maxv #undef bus_space_read_multi_stream_8 931 1.13 maxv #undef bus_space_read_region_1 932 1.13 maxv #undef bus_space_read_region_2 933 1.13 maxv #undef bus_space_read_region_4 934 1.13 maxv #undef bus_space_read_region_8 935 1.13 maxv #undef bus_space_read_region_stream_1 936 1.13 maxv #undef bus_space_read_region_stream_2 937 1.13 maxv #undef bus_space_read_region_stream_4 938 1.13 maxv #undef bus_space_read_region_stream_8 939 1.13 maxv #undef bus_space_write_multi_1 940 1.13 maxv #undef bus_space_write_multi_2 941 1.13 maxv #undef bus_space_write_multi_4 942 1.13 maxv #undef bus_space_write_multi_8 943 1.13 maxv #undef bus_space_write_multi_stream_1 944 1.13 maxv #undef bus_space_write_multi_stream_2 945 1.13 maxv #undef bus_space_write_multi_stream_4 946 1.13 maxv #undef bus_space_write_multi_stream_8 947 1.13 maxv #undef bus_space_write_region_1 948 1.13 maxv #undef bus_space_write_region_2 949 1.13 maxv #undef bus_space_write_region_4 950 1.13 maxv #undef bus_space_write_region_8 951 1.13 maxv #undef bus_space_write_region_stream_1 952 1.13 maxv #undef bus_space_write_region_stream_2 953 1.13 maxv #undef bus_space_write_region_stream_4 954 1.13 maxv #undef bus_space_write_region_stream_8 955 1.13 maxv 956 1.13 maxv #define ASAN_BUS_READ_FUNC(bytes, bits) \ 957 1.13 maxv void bus_space_read_multi_##bytes(bus_space_tag_t, bus_space_handle_t, \ 958 1.13 maxv bus_size_t, uint##bits##_t *, bus_size_t); \ 959 1.13 maxv void kasan_bus_space_read_multi_##bytes(bus_space_tag_t, \ 960 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \ 961 1.13 maxv void kasan_bus_space_read_multi_##bytes(bus_space_tag_t tag, \ 962 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \ 963 1.13 maxv bus_size_t count) \ 964 1.13 maxv { \ 965 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 966 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \ 967 1.13 maxv bus_space_read_multi_##bytes(tag, hnd, size, buf, count); \ 968 1.13 maxv } \ 969 1.13 maxv void bus_space_read_multi_stream_##bytes(bus_space_tag_t, \ 970 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \ 971 1.13 maxv void kasan_bus_space_read_multi_stream_##bytes(bus_space_tag_t, \ 972 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \ 973 1.13 maxv void kasan_bus_space_read_multi_stream_##bytes(bus_space_tag_t tag, \ 974 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \ 975 1.13 maxv bus_size_t count) \ 976 1.13 maxv { \ 977 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 978 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \ 979 1.13 maxv bus_space_read_multi_stream_##bytes(tag, hnd, size, buf, count);\ 980 1.13 maxv } \ 981 1.13 maxv void bus_space_read_region_##bytes(bus_space_tag_t, bus_space_handle_t, \ 982 1.13 maxv bus_size_t, uint##bits##_t *, bus_size_t); \ 983 1.13 maxv void kasan_bus_space_read_region_##bytes(bus_space_tag_t, \ 984 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \ 985 1.13 maxv void kasan_bus_space_read_region_##bytes(bus_space_tag_t tag, \ 986 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \ 987 1.13 maxv bus_size_t count) \ 988 1.13 maxv { \ 989 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 990 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \ 991 1.13 maxv bus_space_read_region_##bytes(tag, hnd, size, buf, count); \ 992 1.13 maxv } \ 993 1.13 maxv void bus_space_read_region_stream_##bytes(bus_space_tag_t, \ 994 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \ 995 1.13 maxv void kasan_bus_space_read_region_stream_##bytes(bus_space_tag_t, \ 996 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \ 997 1.13 maxv void kasan_bus_space_read_region_stream_##bytes(bus_space_tag_t tag, \ 998 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \ 999 1.13 maxv bus_size_t count) \ 1000 1.13 maxv { \ 1001 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 1002 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \ 1003 1.13 maxv bus_space_read_region_stream_##bytes(tag, hnd, size, buf, count);\ 1004 1.13 maxv } 1005 1.13 maxv 1006 1.13 maxv #define ASAN_BUS_WRITE_FUNC(bytes, bits) \ 1007 1.13 maxv void bus_space_write_multi_##bytes(bus_space_tag_t, bus_space_handle_t, \ 1008 1.13 maxv bus_size_t, const uint##bits##_t *, bus_size_t); \ 1009 1.13 maxv void kasan_bus_space_write_multi_##bytes(bus_space_tag_t, \ 1010 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\ 1011 1.13 maxv void kasan_bus_space_write_multi_##bytes(bus_space_tag_t tag, \ 1012 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \ 1013 1.13 maxv bus_size_t count) \ 1014 1.13 maxv { \ 1015 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 1016 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \ 1017 1.13 maxv bus_space_write_multi_##bytes(tag, hnd, size, buf, count); \ 1018 1.13 maxv } \ 1019 1.13 maxv void bus_space_write_multi_stream_##bytes(bus_space_tag_t, \ 1020 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\ 1021 1.13 maxv void kasan_bus_space_write_multi_stream_##bytes(bus_space_tag_t, \ 1022 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\ 1023 1.13 maxv void kasan_bus_space_write_multi_stream_##bytes(bus_space_tag_t tag, \ 1024 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \ 1025 1.13 maxv bus_size_t count) \ 1026 1.13 maxv { \ 1027 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 1028 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \ 1029 1.13 maxv bus_space_write_multi_stream_##bytes(tag, hnd, size, buf, count);\ 1030 1.13 maxv } \ 1031 1.13 maxv void bus_space_write_region_##bytes(bus_space_tag_t, bus_space_handle_t,\ 1032 1.13 maxv bus_size_t, const uint##bits##_t *, bus_size_t); \ 1033 1.13 maxv void kasan_bus_space_write_region_##bytes(bus_space_tag_t, \ 1034 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\ 1035 1.13 maxv void kasan_bus_space_write_region_##bytes(bus_space_tag_t tag, \ 1036 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \ 1037 1.13 maxv bus_size_t count) \ 1038 1.13 maxv { \ 1039 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 1040 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \ 1041 1.13 maxv bus_space_write_region_##bytes(tag, hnd, size, buf, count); \ 1042 1.13 maxv } \ 1043 1.13 maxv void bus_space_write_region_stream_##bytes(bus_space_tag_t, \ 1044 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\ 1045 1.13 maxv void kasan_bus_space_write_region_stream_##bytes(bus_space_tag_t, \ 1046 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\ 1047 1.13 maxv void kasan_bus_space_write_region_stream_##bytes(bus_space_tag_t tag, \ 1048 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \ 1049 1.13 maxv bus_size_t count) \ 1050 1.13 maxv { \ 1051 1.13 maxv kasan_shadow_check((uintptr_t)buf, \ 1052 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \ 1053 1.13 maxv bus_space_write_region_stream_##bytes(tag, hnd, size, buf, count);\ 1054 1.13 maxv } 1055 1.13 maxv 1056 1.13 maxv ASAN_BUS_READ_FUNC(1, 8) 1057 1.13 maxv ASAN_BUS_READ_FUNC(2, 16) 1058 1.13 maxv ASAN_BUS_READ_FUNC(4, 32) 1059 1.13 maxv ASAN_BUS_READ_FUNC(8, 64) 1060 1.13 maxv 1061 1.13 maxv ASAN_BUS_WRITE_FUNC(1, 8) 1062 1.13 maxv ASAN_BUS_WRITE_FUNC(2, 16) 1063 1.13 maxv ASAN_BUS_WRITE_FUNC(4, 32) 1064 1.13 maxv ASAN_BUS_WRITE_FUNC(8, 64) 1065 1.13 maxv 1066 1.14 maxv #endif /* __HAVE_KASAN_INSTR_BUS */ 1067 1.14 maxv 1068 1.13 maxv /* -------------------------------------------------------------------------- */ 1069 1.13 maxv 1070 1.15 maxv #include <sys/mbuf.h> 1071 1.15 maxv 1072 1.15 maxv static void 1073 1.15 maxv kasan_dma_sync_linear(uint8_t *buf, bus_addr_t offset, bus_size_t len, 1074 1.15 maxv bool write, uintptr_t pc) 1075 1.15 maxv { 1076 1.15 maxv kasan_shadow_check((uintptr_t)(buf + offset), len, write, pc); 1077 1.15 maxv } 1078 1.15 maxv 1079 1.15 maxv static void 1080 1.15 maxv kasan_dma_sync_mbuf(struct mbuf *m, bus_addr_t offset, bus_size_t len, 1081 1.15 maxv bool write, uintptr_t pc) 1082 1.15 maxv { 1083 1.15 maxv bus_addr_t minlen; 1084 1.15 maxv 1085 1.15 maxv for (; m != NULL && len != 0; m = m->m_next) { 1086 1.15 maxv kasan_shadow_check((uintptr_t)m, sizeof(*m), false, pc); 1087 1.15 maxv 1088 1.15 maxv if (offset >= m->m_len) { 1089 1.15 maxv offset -= m->m_len; 1090 1.15 maxv continue; 1091 1.15 maxv } 1092 1.15 maxv 1093 1.15 maxv minlen = MIN(len, m->m_len - offset); 1094 1.15 maxv kasan_shadow_check((uintptr_t)(mtod(m, char *) + offset), 1095 1.15 maxv minlen, write, pc); 1096 1.15 maxv 1097 1.15 maxv offset = 0; 1098 1.15 maxv len -= minlen; 1099 1.15 maxv } 1100 1.15 maxv } 1101 1.15 maxv 1102 1.15 maxv static void 1103 1.15 maxv kasan_dma_sync_uio(struct uio *uio, bus_addr_t offset, bus_size_t len, 1104 1.15 maxv bool write, uintptr_t pc) 1105 1.15 maxv { 1106 1.15 maxv bus_size_t minlen, resid; 1107 1.15 maxv struct iovec *iov; 1108 1.15 maxv int i; 1109 1.15 maxv 1110 1.17 maxv kasan_shadow_check((uintptr_t)uio, sizeof(struct uio), false, pc); 1111 1.17 maxv 1112 1.17 maxv if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) 1113 1.15 maxv return; 1114 1.15 maxv 1115 1.15 maxv resid = uio->uio_resid; 1116 1.15 maxv iov = uio->uio_iov; 1117 1.15 maxv 1118 1.15 maxv for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) { 1119 1.15 maxv kasan_shadow_check((uintptr_t)&iov[i], sizeof(iov[i]), 1120 1.15 maxv false, pc); 1121 1.15 maxv minlen = MIN(resid, iov[i].iov_len); 1122 1.15 maxv kasan_shadow_check((uintptr_t)iov[i].iov_base, minlen, 1123 1.15 maxv write, pc); 1124 1.15 maxv resid -= minlen; 1125 1.15 maxv } 1126 1.15 maxv } 1127 1.15 maxv 1128 1.15 maxv void 1129 1.15 maxv kasan_dma_sync(bus_dmamap_t map, bus_addr_t offset, bus_size_t len, int ops) 1130 1.15 maxv { 1131 1.15 maxv bool write = (ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTWRITE)) != 0; 1132 1.15 maxv 1133 1.15 maxv switch (map->dm_buftype) { 1134 1.15 maxv case KASAN_DMA_LINEAR: 1135 1.15 maxv kasan_dma_sync_linear(map->dm_buf, offset, len, write, 1136 1.15 maxv __RET_ADDR); 1137 1.15 maxv break; 1138 1.15 maxv case KASAN_DMA_MBUF: 1139 1.15 maxv kasan_dma_sync_mbuf(map->dm_buf, offset, len, write, 1140 1.15 maxv __RET_ADDR); 1141 1.15 maxv break; 1142 1.15 maxv case KASAN_DMA_UIO: 1143 1.15 maxv kasan_dma_sync_uio(map->dm_buf, offset, len, write, 1144 1.15 maxv __RET_ADDR); 1145 1.15 maxv break; 1146 1.15 maxv case KASAN_DMA_RAW: 1147 1.15 maxv break; 1148 1.15 maxv default: 1149 1.15 maxv panic("%s: impossible", __func__); 1150 1.15 maxv } 1151 1.15 maxv } 1152 1.15 maxv 1153 1.15 maxv void 1154 1.15 maxv kasan_dma_load(bus_dmamap_t map, void *buf, bus_size_t buflen, int type) 1155 1.15 maxv { 1156 1.15 maxv map->dm_buf = buf; 1157 1.15 maxv map->dm_buflen = buflen; 1158 1.15 maxv map->dm_buftype = type; 1159 1.15 maxv } 1160 1.15 maxv 1161 1.15 maxv /* -------------------------------------------------------------------------- */ 1162 1.15 maxv 1163 1.1 maxv void __asan_register_globals(struct __asan_global *, size_t); 1164 1.1 maxv void __asan_unregister_globals(struct __asan_global *, size_t); 1165 1.1 maxv 1166 1.1 maxv void 1167 1.1 maxv __asan_register_globals(struct __asan_global *globals, size_t n) 1168 1.1 maxv { 1169 1.1 maxv size_t i; 1170 1.1 maxv 1171 1.1 maxv for (i = 0; i < n; i++) { 1172 1.2 maxv kasan_mark(globals[i].beg, globals[i].size, 1173 1.6 maxv globals[i].size_with_redzone, KASAN_GENERIC_REDZONE); 1174 1.1 maxv } 1175 1.1 maxv } 1176 1.1 maxv 1177 1.1 maxv void 1178 1.1 maxv __asan_unregister_globals(struct __asan_global *globals, size_t n) 1179 1.1 maxv { 1180 1.1 maxv /* never called */ 1181 1.1 maxv } 1182 1.1 maxv 1183 1.1 maxv #define ASAN_LOAD_STORE(size) \ 1184 1.1 maxv void __asan_load##size(unsigned long); \ 1185 1.1 maxv void __asan_load##size(unsigned long addr) \ 1186 1.1 maxv { \ 1187 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR);\ 1188 1.1 maxv } \ 1189 1.1 maxv void __asan_load##size##_noabort(unsigned long); \ 1190 1.1 maxv void __asan_load##size##_noabort(unsigned long addr) \ 1191 1.1 maxv { \ 1192 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR);\ 1193 1.1 maxv } \ 1194 1.1 maxv void __asan_store##size(unsigned long); \ 1195 1.1 maxv void __asan_store##size(unsigned long addr) \ 1196 1.1 maxv { \ 1197 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR);\ 1198 1.1 maxv } \ 1199 1.1 maxv void __asan_store##size##_noabort(unsigned long); \ 1200 1.1 maxv void __asan_store##size##_noabort(unsigned long addr) \ 1201 1.1 maxv { \ 1202 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR);\ 1203 1.1 maxv } 1204 1.1 maxv 1205 1.1 maxv ASAN_LOAD_STORE(1); 1206 1.1 maxv ASAN_LOAD_STORE(2); 1207 1.1 maxv ASAN_LOAD_STORE(4); 1208 1.1 maxv ASAN_LOAD_STORE(8); 1209 1.1 maxv ASAN_LOAD_STORE(16); 1210 1.1 maxv 1211 1.1 maxv void __asan_loadN(unsigned long, size_t); 1212 1.1 maxv void __asan_loadN_noabort(unsigned long, size_t); 1213 1.1 maxv void __asan_storeN(unsigned long, size_t); 1214 1.1 maxv void __asan_storeN_noabort(unsigned long, size_t); 1215 1.1 maxv void __asan_handle_no_return(void); 1216 1.1 maxv 1217 1.1 maxv void 1218 1.1 maxv __asan_loadN(unsigned long addr, size_t size) 1219 1.1 maxv { 1220 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR); 1221 1.1 maxv } 1222 1.1 maxv 1223 1.1 maxv void 1224 1.1 maxv __asan_loadN_noabort(unsigned long addr, size_t size) 1225 1.1 maxv { 1226 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR); 1227 1.1 maxv } 1228 1.1 maxv 1229 1.1 maxv void 1230 1.1 maxv __asan_storeN(unsigned long addr, size_t size) 1231 1.1 maxv { 1232 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR); 1233 1.1 maxv } 1234 1.1 maxv 1235 1.1 maxv void 1236 1.1 maxv __asan_storeN_noabort(unsigned long addr, size_t size) 1237 1.1 maxv { 1238 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR); 1239 1.1 maxv } 1240 1.1 maxv 1241 1.1 maxv void 1242 1.1 maxv __asan_handle_no_return(void) 1243 1.1 maxv { 1244 1.1 maxv /* nothing */ 1245 1.1 maxv } 1246 1.1 maxv 1247 1.1 maxv #define ASAN_SET_SHADOW(byte) \ 1248 1.1 maxv void __asan_set_shadow_##byte(void *, size_t); \ 1249 1.1 maxv void __asan_set_shadow_##byte(void *addr, size_t size) \ 1250 1.1 maxv { \ 1251 1.1 maxv __builtin_memset((void *)addr, 0x##byte, size); \ 1252 1.1 maxv } 1253 1.1 maxv 1254 1.1 maxv ASAN_SET_SHADOW(00); 1255 1.1 maxv ASAN_SET_SHADOW(f1); 1256 1.1 maxv ASAN_SET_SHADOW(f2); 1257 1.1 maxv ASAN_SET_SHADOW(f3); 1258 1.1 maxv ASAN_SET_SHADOW(f5); 1259 1.1 maxv ASAN_SET_SHADOW(f8); 1260 1.4 maxv 1261 1.4 maxv void __asan_poison_stack_memory(const void *, size_t); 1262 1.4 maxv void __asan_unpoison_stack_memory(const void *, size_t); 1263 1.4 maxv 1264 1.17 maxv void 1265 1.17 maxv __asan_poison_stack_memory(const void *addr, size_t size) 1266 1.4 maxv { 1267 1.4 maxv size = roundup(size, KASAN_SHADOW_SCALE_SIZE); 1268 1.4 maxv kasan_shadow_Nbyte_fill(addr, size, KASAN_USE_AFTER_SCOPE); 1269 1.4 maxv } 1270 1.4 maxv 1271 1.17 maxv void 1272 1.17 maxv __asan_unpoison_stack_memory(const void *addr, size_t size) 1273 1.4 maxv { 1274 1.4 maxv kasan_shadow_Nbyte_markvalid(addr, size); 1275 1.4 maxv } 1276 1.19 maxv 1277 1.19 maxv void __asan_alloca_poison(const void *, size_t); 1278 1.19 maxv void __asan_allocas_unpoison(const void *, const void *); 1279 1.19 maxv 1280 1.19 maxv void __asan_alloca_poison(const void *addr, size_t size) 1281 1.19 maxv { 1282 1.19 maxv const void *l, *r; 1283 1.19 maxv 1284 1.19 maxv KASSERT((vaddr_t)addr % KASAN_ALLOCA_SCALE_SIZE == 0); 1285 1.19 maxv 1286 1.19 maxv l = (const uint8_t *)addr - KASAN_ALLOCA_SCALE_SIZE; 1287 1.19 maxv r = (const uint8_t *)addr + roundup(size, KASAN_ALLOCA_SCALE_SIZE); 1288 1.19 maxv 1289 1.19 maxv kasan_shadow_Nbyte_fill(l, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_LEFT); 1290 1.19 maxv kasan_mark(addr, size, roundup(size, KASAN_ALLOCA_SCALE_SIZE), 1291 1.19 maxv KASAN_STACK_MID); 1292 1.19 maxv kasan_shadow_Nbyte_fill(r, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_RIGHT); 1293 1.19 maxv } 1294 1.19 maxv 1295 1.19 maxv void __asan_allocas_unpoison(const void *stkbegin, const void *stkend) 1296 1.19 maxv { 1297 1.19 maxv size_t size; 1298 1.19 maxv 1299 1.19 maxv if (__predict_false(!stkbegin)) 1300 1.19 maxv return; 1301 1.19 maxv if (__predict_false((uintptr_t)stkbegin > (uintptr_t)stkend)) 1302 1.19 maxv return; 1303 1.19 maxv size = (uintptr_t)stkend - (uintptr_t)stkbegin; 1304 1.19 maxv 1305 1.19 maxv kasan_shadow_Nbyte_fill(stkbegin, size, 0); 1306 1.19 maxv } 1307