xref: /freebsd/sys/kern/subr_asan.c (revision a80b9ee15aa0f2da9f9f0c48e13324e0e5f2e840)
1 /*	$NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $	*/
2 
3 /*
4  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
5  * All rights reserved.
6  *
7  * This code is part of the KASAN subsystem of the NetBSD kernel.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #define	SAN_RUNTIME
32 
33 #include <sys/cdefs.h>
34 #if 0
35 __KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $");
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/asan.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/stack.h>
44 #include <sys/sysctl.h>
45 
46 #include <machine/asan.h>
47 #include <machine/bus.h>
48 
49 /* ASAN constants. Part of the compiler ABI. */
50 #define KASAN_SHADOW_MASK		(KASAN_SHADOW_SCALE - 1)
51 #define KASAN_ALLOCA_SCALE_SIZE		32
52 
53 /* ASAN ABI version. */
54 #if defined(__clang__) && (__clang_major__ - 0 >= 6)
55 #define ASAN_ABI_VERSION	8
56 #elif __GNUC_PREREQ__(7, 1) && !defined(__clang__)
57 #define ASAN_ABI_VERSION	8
58 #elif __GNUC_PREREQ__(6, 1) && !defined(__clang__)
59 #define ASAN_ABI_VERSION	6
60 #else
61 #error "Unsupported compiler version"
62 #endif
63 
64 #define __RET_ADDR	(unsigned long)__builtin_return_address(0)
65 
66 /* Global variable descriptor. Part of the compiler ABI.  */
67 struct __asan_global_source_location {
68 	const char *filename;
69 	int line_no;
70 	int column_no;
71 };
72 
73 struct __asan_global {
74 	const void *beg;		/* address of the global variable */
75 	size_t size;			/* size of the global variable */
76 	size_t size_with_redzone;	/* size with the redzone */
77 	const void *name;		/* name of the variable */
78 	const void *module_name;	/* name of the module where the var is declared */
79 	unsigned long has_dynamic_init;	/* the var has dyn initializer (c++) */
80 	struct __asan_global_source_location *location;
81 #if ASAN_ABI_VERSION >= 7
82 	uintptr_t odr_indicator;	/* the address of the ODR indicator symbol */
83 #endif
84 };
85 
86 FEATURE(kasan, "Kernel address sanitizer");
87 
88 static SYSCTL_NODE(_debug, OID_AUTO, kasan, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
89     "KASAN options");
90 
91 static int panic_on_violation = 1;
92 SYSCTL_INT(_debug_kasan, OID_AUTO, panic_on_violation, CTLFLAG_RDTUN,
93     &panic_on_violation, 0,
94     "Panic if an invalid access is detected");
95 
96 #define kasan_enabled (!kasan_disabled)
97 static bool kasan_disabled __read_mostly = true;
98 SYSCTL_BOOL(_debug_kasan, OID_AUTO, disabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
99     &kasan_disabled, 0, "KASAN is disabled");
100 
101 /* -------------------------------------------------------------------------- */
102 
103 void
kasan_shadow_map(vm_offset_t addr,size_t size)104 kasan_shadow_map(vm_offset_t addr, size_t size)
105 {
106 	size_t sz, npages, i;
107 	vm_offset_t sva, eva;
108 
109 	KASSERT(addr % KASAN_SHADOW_SCALE == 0,
110 	    ("%s: invalid address %#lx", __func__, addr));
111 
112 	sz = roundup(size, KASAN_SHADOW_SCALE) / KASAN_SHADOW_SCALE;
113 
114 	sva = kasan_md_addr_to_shad(addr);
115 	eva = kasan_md_addr_to_shad(addr) + sz;
116 
117 	sva = rounddown(sva, PAGE_SIZE);
118 	eva = roundup(eva, PAGE_SIZE);
119 
120 	npages = (eva - sva) / PAGE_SIZE;
121 
122 	KASSERT(sva >= KASAN_MIN_ADDRESS && eva < KASAN_MAX_ADDRESS,
123 	    ("%s: invalid address range %#lx-%#lx", __func__, sva, eva));
124 
125 	for (i = 0; i < npages; i++)
126 		pmap_san_enter(sva + ptoa(i));
127 }
128 
129 void
kasan_init(void)130 kasan_init(void)
131 {
132 	int disabled;
133 
134 	disabled = 0;
135 	TUNABLE_INT_FETCH("debug.kasan.disabled", &disabled);
136 	if (disabled)
137 		return;
138 
139 	/* MD initialization. */
140 	kasan_md_init();
141 
142 	/* Now officially enabled. */
143 	kasan_disabled = false;
144 }
145 
146 void
kasan_init_early(vm_offset_t stack,size_t size)147 kasan_init_early(vm_offset_t stack, size_t size)
148 {
149 	kasan_md_init_early(stack, size);
150 }
151 
152 static inline const char *
kasan_code_name(uint8_t code)153 kasan_code_name(uint8_t code)
154 {
155 	switch (code) {
156 	case KASAN_GENERIC_REDZONE:
157 		return "GenericRedZone";
158 	case KASAN_MALLOC_REDZONE:
159 		return "MallocRedZone";
160 	case KASAN_KMEM_REDZONE:
161 		return "KmemRedZone";
162 	case KASAN_UMA_FREED:
163 		return "UMAUseAfterFree";
164 	case KASAN_KSTACK_FREED:
165 		return "KernelStack";
166 	case KASAN_EXEC_ARGS_FREED:
167 		return "ExecKVA";
168 	case 1 ... 7:
169 		return "RedZonePartial";
170 	case KASAN_STACK_LEFT:
171 		return "StackLeft";
172 	case KASAN_STACK_MID:
173 		return "StackMiddle";
174 	case KASAN_STACK_RIGHT:
175 		return "StackRight";
176 	case KASAN_USE_AFTER_RET:
177 		return "UseAfterRet";
178 	case KASAN_USE_AFTER_SCOPE:
179 		return "UseAfterScope";
180 	default:
181 		return "Unknown";
182 	}
183 }
184 
185 #define	REPORT(f, ...) do {				\
186 	if (panic_on_violation) {			\
187 		kasan_disabled = true;			\
188 		panic(f, __VA_ARGS__);			\
189 	} else {					\
190 		struct stack st;			\
191 							\
192 		stack_save(&st);			\
193 		printf(f "\n", __VA_ARGS__);		\
194 		stack_print_ddb(&st);			\
195 	}						\
196 } while (0)
197 
198 static void
kasan_report(unsigned long addr,size_t size,bool write,unsigned long pc,uint8_t code)199 kasan_report(unsigned long addr, size_t size, bool write, unsigned long pc,
200     uint8_t code)
201 {
202 	REPORT("ASan: Invalid access, %zu-byte %s at %#lx, %s(%x)",
203 	    size, (write ? "write" : "read"), addr, kasan_code_name(code),
204 	    code);
205 }
206 
207 static __always_inline void
kasan_shadow_1byte_markvalid(unsigned long addr)208 kasan_shadow_1byte_markvalid(unsigned long addr)
209 {
210 	int8_t *byte = (int8_t *)kasan_md_addr_to_shad(addr);
211 	int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
212 
213 	*byte = last;
214 }
215 
216 static __always_inline void
kasan_shadow_Nbyte_markvalid(const void * addr,size_t size)217 kasan_shadow_Nbyte_markvalid(const void *addr, size_t size)
218 {
219 	size_t i;
220 
221 	for (i = 0; i < size; i++) {
222 		kasan_shadow_1byte_markvalid((unsigned long)addr + i);
223 	}
224 }
225 
226 static __always_inline void
kasan_shadow_Nbyte_fill(const void * addr,size_t size,uint8_t code)227 kasan_shadow_Nbyte_fill(const void *addr, size_t size, uint8_t code)
228 {
229 	void *shad;
230 
231 	if (__predict_false(size == 0))
232 		return;
233 	if (__predict_false(kasan_md_unsupported((vm_offset_t)addr)))
234 		return;
235 
236 	KASSERT((vm_offset_t)addr % KASAN_SHADOW_SCALE == 0,
237 	    ("%s: invalid address %p", __func__, addr));
238 	KASSERT(size % KASAN_SHADOW_SCALE == 0,
239 	    ("%s: invalid size %zu", __func__, size));
240 
241 	shad = (void *)kasan_md_addr_to_shad((uintptr_t)addr);
242 	size = size >> KASAN_SHADOW_SCALE_SHIFT;
243 
244 	__builtin_memset(shad, code, size);
245 }
246 
247 /*
248  * In an area of size 'sz_with_redz', mark the 'size' first bytes as valid,
249  * and the rest as invalid. There are generally two use cases:
250  *
251  *  o kasan_mark(addr, origsize, size, code), with origsize < size. This marks
252  *    the redzone at the end of the buffer as invalid. If the entire is to be
253  *    marked invalid, origsize will be 0.
254  *
255  *  o kasan_mark(addr, size, size, 0). This marks the entire buffer as valid.
256  */
257 void
kasan_mark(const void * addr,size_t size,size_t redzsize,uint8_t code)258 kasan_mark(const void *addr, size_t size, size_t redzsize, uint8_t code)
259 {
260 	size_t i, n, redz;
261 	int8_t *shad;
262 
263 	if (__predict_false(!kasan_enabled))
264 		return;
265 
266 	if ((vm_offset_t)addr >= DMAP_MIN_ADDRESS &&
267 	    (vm_offset_t)addr < DMAP_MAX_ADDRESS)
268 		return;
269 
270 	KASSERT((vm_offset_t)addr >= VM_MIN_KERNEL_ADDRESS &&
271 	    (vm_offset_t)addr < VM_MAX_KERNEL_ADDRESS,
272 	    ("%s: invalid address %p", __func__, addr));
273 	KASSERT((vm_offset_t)addr % KASAN_SHADOW_SCALE == 0,
274 	    ("%s: invalid address %p", __func__, addr));
275 	redz = redzsize - roundup(size, KASAN_SHADOW_SCALE);
276 	KASSERT(redz % KASAN_SHADOW_SCALE == 0,
277 	    ("%s: invalid size %zu", __func__, redz));
278 	shad = (int8_t *)kasan_md_addr_to_shad((uintptr_t)addr);
279 
280 	/* Chunks of 8 bytes, valid. */
281 	n = size / KASAN_SHADOW_SCALE;
282 	for (i = 0; i < n; i++) {
283 		*shad++ = 0;
284 	}
285 
286 	/* Possibly one chunk, mid. */
287 	if ((size & KASAN_SHADOW_MASK) != 0) {
288 		*shad++ = (size & KASAN_SHADOW_MASK);
289 	}
290 
291 	/* Chunks of 8 bytes, invalid. */
292 	n = redz / KASAN_SHADOW_SCALE;
293 	for (i = 0; i < n; i++) {
294 		*shad++ = code;
295 	}
296 }
297 
298 void
kasan_thread_alloc(struct thread * td)299 kasan_thread_alloc(struct thread *td)
300 {
301 	if (td->td_kstack != 0) {
302 		kasan_mark((void *)td->td_kstack, ptoa(td->td_kstack_pages),
303 		    ptoa(td->td_kstack_pages), 0);
304 	}
305 }
306 
307 /* -------------------------------------------------------------------------- */
308 
309 #define ADDR_CROSSES_SCALE_BOUNDARY(addr, size) 		\
310 	(addr >> KASAN_SHADOW_SCALE_SHIFT) !=			\
311 	    ((addr + size - 1) >> KASAN_SHADOW_SCALE_SHIFT)
312 
313 static __always_inline bool
kasan_shadow_1byte_isvalid(unsigned long addr,uint8_t * code)314 kasan_shadow_1byte_isvalid(unsigned long addr, uint8_t *code)
315 {
316 	int8_t *byte = (int8_t *)kasan_md_addr_to_shad(addr);
317 	int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
318 
319 	if (__predict_true(*byte == 0 || last <= *byte)) {
320 		return (true);
321 	}
322 	*code = *byte;
323 	return (false);
324 }
325 
326 static __always_inline bool
kasan_shadow_2byte_isvalid(unsigned long addr,uint8_t * code)327 kasan_shadow_2byte_isvalid(unsigned long addr, uint8_t *code)
328 {
329 	int8_t *byte, last;
330 
331 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 2)) {
332 		return (kasan_shadow_1byte_isvalid(addr, code) &&
333 		    kasan_shadow_1byte_isvalid(addr+1, code));
334 	}
335 
336 	byte = (int8_t *)kasan_md_addr_to_shad(addr);
337 	last = ((addr + 1) & KASAN_SHADOW_MASK) + 1;
338 
339 	if (__predict_true(*byte == 0 || last <= *byte)) {
340 		return (true);
341 	}
342 	*code = *byte;
343 	return (false);
344 }
345 
346 static __always_inline bool
kasan_shadow_4byte_isvalid(unsigned long addr,uint8_t * code)347 kasan_shadow_4byte_isvalid(unsigned long addr, uint8_t *code)
348 {
349 	int8_t *byte, last;
350 
351 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 4)) {
352 		return (kasan_shadow_2byte_isvalid(addr, code) &&
353 		    kasan_shadow_2byte_isvalid(addr+2, code));
354 	}
355 
356 	byte = (int8_t *)kasan_md_addr_to_shad(addr);
357 	last = ((addr + 3) & KASAN_SHADOW_MASK) + 1;
358 
359 	if (__predict_true(*byte == 0 || last <= *byte)) {
360 		return (true);
361 	}
362 	*code = *byte;
363 	return (false);
364 }
365 
366 static __always_inline bool
kasan_shadow_8byte_isvalid(unsigned long addr,uint8_t * code)367 kasan_shadow_8byte_isvalid(unsigned long addr, uint8_t *code)
368 {
369 	int8_t *byte, last;
370 
371 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 8)) {
372 		return (kasan_shadow_4byte_isvalid(addr, code) &&
373 		    kasan_shadow_4byte_isvalid(addr+4, code));
374 	}
375 
376 	byte = (int8_t *)kasan_md_addr_to_shad(addr);
377 	last = ((addr + 7) & KASAN_SHADOW_MASK) + 1;
378 
379 	if (__predict_true(*byte == 0 || last <= *byte)) {
380 		return (true);
381 	}
382 	*code = *byte;
383 	return (false);
384 }
385 
386 static __always_inline bool
kasan_shadow_Nbyte_isvalid(unsigned long addr,size_t size,uint8_t * code)387 kasan_shadow_Nbyte_isvalid(unsigned long addr, size_t size, uint8_t *code)
388 {
389 	size_t i;
390 
391 	for (i = 0; i < size; i++) {
392 		if (!kasan_shadow_1byte_isvalid(addr+i, code))
393 			return (false);
394 	}
395 
396 	return (true);
397 }
398 
399 static __always_inline void
kasan_shadow_check(unsigned long addr,size_t size,bool write,unsigned long retaddr)400 kasan_shadow_check(unsigned long addr, size_t size, bool write,
401     unsigned long retaddr)
402 {
403 	uint8_t code;
404 	bool valid;
405 
406 	if (__predict_false(!kasan_enabled))
407 		return;
408 	if (__predict_false(size == 0))
409 		return;
410 	if (__predict_false(kasan_md_unsupported(addr)))
411 		return;
412 	if (KERNEL_PANICKED())
413 		return;
414 
415 	if (__builtin_constant_p(size)) {
416 		switch (size) {
417 		case 1:
418 			valid = kasan_shadow_1byte_isvalid(addr, &code);
419 			break;
420 		case 2:
421 			valid = kasan_shadow_2byte_isvalid(addr, &code);
422 			break;
423 		case 4:
424 			valid = kasan_shadow_4byte_isvalid(addr, &code);
425 			break;
426 		case 8:
427 			valid = kasan_shadow_8byte_isvalid(addr, &code);
428 			break;
429 		default:
430 			valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
431 			break;
432 		}
433 	} else {
434 		valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
435 	}
436 
437 	if (__predict_false(!valid)) {
438 		kasan_report(addr, size, write, retaddr, code);
439 	}
440 }
441 
442 /* -------------------------------------------------------------------------- */
443 
444 void *
kasan_memcpy(void * dst,const void * src,size_t len)445 kasan_memcpy(void *dst, const void *src, size_t len)
446 {
447 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
448 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
449 	return (__builtin_memcpy(dst, src, len));
450 }
451 
452 int
kasan_memcmp(const void * b1,const void * b2,size_t len)453 kasan_memcmp(const void *b1, const void *b2, size_t len)
454 {
455 	kasan_shadow_check((unsigned long)b1, len, false, __RET_ADDR);
456 	kasan_shadow_check((unsigned long)b2, len, false, __RET_ADDR);
457 	return (__builtin_memcmp(b1, b2, len));
458 }
459 
460 void *
kasan_memset(void * b,int c,size_t len)461 kasan_memset(void *b, int c, size_t len)
462 {
463 	kasan_shadow_check((unsigned long)b, len, true, __RET_ADDR);
464 	return (__builtin_memset(b, c, len));
465 }
466 
467 void *
kasan_memmove(void * dst,const void * src,size_t len)468 kasan_memmove(void *dst, const void *src, size_t len)
469 {
470 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
471 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
472 	return (__builtin_memmove(dst, src, len));
473 }
474 
475 size_t
kasan_strlen(const char * str)476 kasan_strlen(const char *str)
477 {
478 	const char *s;
479 
480 	s = str;
481 	while (1) {
482 		kasan_shadow_check((unsigned long)s, 1, false, __RET_ADDR);
483 		if (*s == '\0')
484 			break;
485 		s++;
486 	}
487 
488 	return (s - str);
489 }
490 
491 char *
kasan_strcpy(char * dst,const char * src)492 kasan_strcpy(char *dst, const char *src)
493 {
494 	char *save = dst;
495 
496 	while (1) {
497 		kasan_shadow_check((unsigned long)src, 1, false, __RET_ADDR);
498 		kasan_shadow_check((unsigned long)dst, 1, true, __RET_ADDR);
499 		*dst = *src;
500 		if (*src == '\0')
501 			break;
502 		src++, dst++;
503 	}
504 
505 	return save;
506 }
507 
508 int
kasan_strcmp(const char * s1,const char * s2)509 kasan_strcmp(const char *s1, const char *s2)
510 {
511 	while (1) {
512 		kasan_shadow_check((unsigned long)s1, 1, false, __RET_ADDR);
513 		kasan_shadow_check((unsigned long)s2, 1, false, __RET_ADDR);
514 		if (*s1 != *s2)
515 			break;
516 		if (*s1 == '\0')
517 			return 0;
518 		s1++, s2++;
519 	}
520 
521 	return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
522 }
523 
524 int
kasan_copyin(const void * uaddr,void * kaddr,size_t len)525 kasan_copyin(const void *uaddr, void *kaddr, size_t len)
526 {
527 	kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
528 	return (copyin(uaddr, kaddr, len));
529 }
530 
531 int
kasan_copyinstr(const void * uaddr,void * kaddr,size_t len,size_t * done)532 kasan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
533 {
534 	kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
535 	return (copyinstr(uaddr, kaddr, len, done));
536 }
537 
538 int
kasan_copyout(const void * kaddr,void * uaddr,size_t len)539 kasan_copyout(const void *kaddr, void *uaddr, size_t len)
540 {
541 	kasan_shadow_check((unsigned long)kaddr, len, false, __RET_ADDR);
542 	return (copyout(kaddr, uaddr, len));
543 }
544 
545 /* -------------------------------------------------------------------------- */
546 
547 int
kasan_fubyte(volatile const void * base)548 kasan_fubyte(volatile const void *base)
549 {
550 	return (fubyte(base));
551 }
552 
553 int
kasan_fuword16(volatile const void * base)554 kasan_fuword16(volatile const void *base)
555 {
556 	return (fuword16(base));
557 }
558 
559 int
kasan_fueword(volatile const void * base,long * val)560 kasan_fueword(volatile const void *base, long *val)
561 {
562 	kasan_shadow_check((unsigned long)val, sizeof(*val), true, __RET_ADDR);
563 	return (fueword(base, val));
564 }
565 
566 int
kasan_fueword32(volatile const void * base,int32_t * val)567 kasan_fueword32(volatile const void *base, int32_t *val)
568 {
569 	kasan_shadow_check((unsigned long)val, sizeof(*val), true, __RET_ADDR);
570 	return (fueword32(base, val));
571 }
572 
573 int
kasan_fueword64(volatile const void * base,int64_t * val)574 kasan_fueword64(volatile const void *base, int64_t *val)
575 {
576 	kasan_shadow_check((unsigned long)val, sizeof(*val), true, __RET_ADDR);
577 	return (fueword64(base, val));
578 }
579 
580 int
kasan_subyte(volatile void * base,int byte)581 kasan_subyte(volatile void *base, int byte)
582 {
583 	return (subyte(base, byte));
584 }
585 
586 int
kasan_suword(volatile void * base,long word)587 kasan_suword(volatile void *base, long word)
588 {
589 	return (suword(base, word));
590 }
591 
592 int
kasan_suword16(volatile void * base,int word)593 kasan_suword16(volatile void *base, int word)
594 {
595 	return (suword16(base, word));
596 }
597 
598 int
kasan_suword32(volatile void * base,int32_t word)599 kasan_suword32(volatile void *base, int32_t word)
600 {
601 	return (suword32(base, word));
602 }
603 
604 int
kasan_suword64(volatile void * base,int64_t word)605 kasan_suword64(volatile void *base, int64_t word)
606 {
607 	return (suword64(base, word));
608 }
609 
610 int
kasan_casueword32(volatile uint32_t * base,uint32_t oldval,uint32_t * oldvalp,uint32_t newval)611 kasan_casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
612     uint32_t newval)
613 {
614 	kasan_shadow_check((unsigned long)oldvalp, sizeof(*oldvalp), true,
615 	    __RET_ADDR);
616 	return (casueword32(base, oldval, oldvalp, newval));
617 }
618 
619 int
kasan_casueword(volatile u_long * base,u_long oldval,u_long * oldvalp,u_long newval)620 kasan_casueword(volatile u_long *base, u_long oldval, u_long *oldvalp,
621     u_long newval)
622 {
623 	kasan_shadow_check((unsigned long)oldvalp, sizeof(*oldvalp), true,
624 	    __RET_ADDR);
625 	return (casueword(base, oldval, oldvalp, newval));
626 }
627 
628 /* -------------------------------------------------------------------------- */
629 
630 #include <machine/atomic.h>
631 #include <sys/atomic_san.h>
632 
633 #define _ASAN_ATOMIC_FUNC_ADD(name, type)				\
634 	void kasan_atomic_add_##name(volatile type *ptr, type val)	\
635 	{								\
636 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
637 		    __RET_ADDR);					\
638 		atomic_add_##name(ptr, val);				\
639 	}
640 
641 #define	ASAN_ATOMIC_FUNC_ADD(name, type)				\
642 	_ASAN_ATOMIC_FUNC_ADD(name, type)				\
643 	_ASAN_ATOMIC_FUNC_ADD(acq_##name, type)				\
644 	_ASAN_ATOMIC_FUNC_ADD(rel_##name, type)
645 
646 #define _ASAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
647 	void kasan_atomic_subtract_##name(volatile type *ptr, type val)	\
648 	{								\
649 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
650 		    __RET_ADDR);					\
651 		atomic_subtract_##name(ptr, val);			\
652 	}
653 
654 #define	ASAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
655 	_ASAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
656 	_ASAN_ATOMIC_FUNC_SUBTRACT(acq_##name, type)			\
657 	_ASAN_ATOMIC_FUNC_SUBTRACT(rel_##name, type)
658 
659 #define _ASAN_ATOMIC_FUNC_SET(name, type)				\
660 	void kasan_atomic_set_##name(volatile type *ptr, type val)	\
661 	{								\
662 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
663 		    __RET_ADDR);					\
664 		atomic_set_##name(ptr, val);				\
665 	}
666 
667 #define	ASAN_ATOMIC_FUNC_SET(name, type)				\
668 	_ASAN_ATOMIC_FUNC_SET(name, type)				\
669 	_ASAN_ATOMIC_FUNC_SET(acq_##name, type)				\
670 	_ASAN_ATOMIC_FUNC_SET(rel_##name, type)
671 
672 #define _ASAN_ATOMIC_FUNC_CLEAR(name, type)				\
673 	void kasan_atomic_clear_##name(volatile type *ptr, type val)	\
674 	{								\
675 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
676 		    __RET_ADDR);					\
677 		atomic_clear_##name(ptr, val);				\
678 	}
679 
680 #define	ASAN_ATOMIC_FUNC_CLEAR(name, type)				\
681 	_ASAN_ATOMIC_FUNC_CLEAR(name, type)				\
682 	_ASAN_ATOMIC_FUNC_CLEAR(acq_##name, type)			\
683 	_ASAN_ATOMIC_FUNC_CLEAR(rel_##name, type)
684 
685 #define	ASAN_ATOMIC_FUNC_FETCHADD(name, type)				\
686 	type kasan_atomic_fetchadd_##name(volatile type *ptr, type val)	\
687 	{								\
688 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
689 		    __RET_ADDR);					\
690 		return (atomic_fetchadd_##name(ptr, val));		\
691 	}
692 
693 #define	ASAN_ATOMIC_FUNC_READANDCLEAR(name, type)			\
694 	type kasan_atomic_readandclear_##name(volatile type *ptr)	\
695 	{								\
696 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
697 		    __RET_ADDR);					\
698 		return (atomic_readandclear_##name(ptr));		\
699 	}
700 
701 #define	ASAN_ATOMIC_FUNC_TESTANDCLEAR(name, type)			\
702 	int kasan_atomic_testandclear_##name(volatile type *ptr, u_int v) \
703 	{								\
704 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
705 		    __RET_ADDR);					\
706 		return (atomic_testandclear_##name(ptr, v));		\
707 	}
708 
709 #define	ASAN_ATOMIC_FUNC_TESTANDSET(name, type)				\
710 	int kasan_atomic_testandset_##name(volatile type *ptr, u_int v) \
711 	{								\
712 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
713 		    __RET_ADDR);					\
714 		return (atomic_testandset_##name(ptr, v));		\
715 	}
716 
717 #define	ASAN_ATOMIC_FUNC_SWAP(name, type)				\
718 	type kasan_atomic_swap_##name(volatile type *ptr, type val)	\
719 	{								\
720 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
721 		    __RET_ADDR);					\
722 		return (atomic_swap_##name(ptr, val));			\
723 	}
724 
725 #define _ASAN_ATOMIC_FUNC_CMPSET(name, type)				\
726 	int kasan_atomic_cmpset_##name(volatile type *ptr, type oval,	\
727 	    type nval)							\
728 	{								\
729 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
730 		    __RET_ADDR);					\
731 		return (atomic_cmpset_##name(ptr, oval, nval));		\
732 	}
733 
734 #define	ASAN_ATOMIC_FUNC_CMPSET(name, type)				\
735 	_ASAN_ATOMIC_FUNC_CMPSET(name, type)				\
736 	_ASAN_ATOMIC_FUNC_CMPSET(acq_##name, type)			\
737 	_ASAN_ATOMIC_FUNC_CMPSET(rel_##name, type)
738 
739 #define _ASAN_ATOMIC_FUNC_FCMPSET(name, type)				\
740 	int kasan_atomic_fcmpset_##name(volatile type *ptr, type *oval,	\
741 	    type nval)							\
742 	{								\
743 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
744 		    __RET_ADDR);					\
745 		return (atomic_fcmpset_##name(ptr, oval, nval));	\
746 	}
747 
748 #define	ASAN_ATOMIC_FUNC_FCMPSET(name, type)				\
749 	_ASAN_ATOMIC_FUNC_FCMPSET(name, type)				\
750 	_ASAN_ATOMIC_FUNC_FCMPSET(acq_##name, type)			\
751 	_ASAN_ATOMIC_FUNC_FCMPSET(rel_##name, type)
752 
753 #define ASAN_ATOMIC_FUNC_THREAD_FENCE(name)				\
754 	void kasan_atomic_thread_fence_##name(void)			\
755 	{								\
756 		atomic_thread_fence_##name();				\
757 	}
758 
759 #define	_ASAN_ATOMIC_FUNC_LOAD(name, type)				\
760 	type kasan_atomic_load_##name(volatile type *ptr)		\
761 	{								\
762 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
763 		    __RET_ADDR);					\
764 		return (atomic_load_##name(ptr));			\
765 	}
766 
767 #define	ASAN_ATOMIC_FUNC_LOAD(name, type)				\
768 	_ASAN_ATOMIC_FUNC_LOAD(name, type)				\
769 	_ASAN_ATOMIC_FUNC_LOAD(acq_##name, type)
770 
771 #define	_ASAN_ATOMIC_FUNC_STORE(name, type)				\
772 	void kasan_atomic_store_##name(volatile type *ptr, type val)	\
773 	{								\
774 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
775 		    __RET_ADDR);					\
776 		atomic_store_##name(ptr, val);				\
777 	}
778 
779 #define	ASAN_ATOMIC_FUNC_STORE(name, type)				\
780 	_ASAN_ATOMIC_FUNC_STORE(name, type)				\
781 	_ASAN_ATOMIC_FUNC_STORE(rel_##name, type)
782 
783 ASAN_ATOMIC_FUNC_ADD(8, uint8_t);
784 ASAN_ATOMIC_FUNC_ADD(16, uint16_t);
785 ASAN_ATOMIC_FUNC_ADD(32, uint32_t);
786 ASAN_ATOMIC_FUNC_ADD(64, uint64_t);
787 ASAN_ATOMIC_FUNC_ADD(int, u_int);
788 ASAN_ATOMIC_FUNC_ADD(long, u_long);
789 ASAN_ATOMIC_FUNC_ADD(ptr, uintptr_t);
790 
791 ASAN_ATOMIC_FUNC_SUBTRACT(8, uint8_t);
792 ASAN_ATOMIC_FUNC_SUBTRACT(16, uint16_t);
793 ASAN_ATOMIC_FUNC_SUBTRACT(32, uint32_t);
794 ASAN_ATOMIC_FUNC_SUBTRACT(64, uint64_t);
795 ASAN_ATOMIC_FUNC_SUBTRACT(int, u_int);
796 ASAN_ATOMIC_FUNC_SUBTRACT(long, u_long);
797 ASAN_ATOMIC_FUNC_SUBTRACT(ptr, uintptr_t);
798 
799 ASAN_ATOMIC_FUNC_SET(8, uint8_t);
800 ASAN_ATOMIC_FUNC_SET(16, uint16_t);
801 ASAN_ATOMIC_FUNC_SET(32, uint32_t);
802 ASAN_ATOMIC_FUNC_SET(64, uint64_t);
803 ASAN_ATOMIC_FUNC_SET(int, u_int);
804 ASAN_ATOMIC_FUNC_SET(long, u_long);
805 ASAN_ATOMIC_FUNC_SET(ptr, uintptr_t);
806 
807 ASAN_ATOMIC_FUNC_CLEAR(8, uint8_t);
808 ASAN_ATOMIC_FUNC_CLEAR(16, uint16_t);
809 ASAN_ATOMIC_FUNC_CLEAR(32, uint32_t);
810 ASAN_ATOMIC_FUNC_CLEAR(64, uint64_t);
811 ASAN_ATOMIC_FUNC_CLEAR(int, u_int);
812 ASAN_ATOMIC_FUNC_CLEAR(long, u_long);
813 ASAN_ATOMIC_FUNC_CLEAR(ptr, uintptr_t);
814 
815 ASAN_ATOMIC_FUNC_FETCHADD(32, uint32_t);
816 ASAN_ATOMIC_FUNC_FETCHADD(64, uint64_t);
817 ASAN_ATOMIC_FUNC_FETCHADD(int, u_int);
818 ASAN_ATOMIC_FUNC_FETCHADD(long, u_long);
819 
820 ASAN_ATOMIC_FUNC_READANDCLEAR(32, uint32_t);
821 ASAN_ATOMIC_FUNC_READANDCLEAR(64, uint64_t);
822 ASAN_ATOMIC_FUNC_READANDCLEAR(int, u_int);
823 ASAN_ATOMIC_FUNC_READANDCLEAR(long, u_long);
824 ASAN_ATOMIC_FUNC_READANDCLEAR(ptr, uintptr_t);
825 
826 ASAN_ATOMIC_FUNC_TESTANDCLEAR(32, uint32_t);
827 ASAN_ATOMIC_FUNC_TESTANDCLEAR(64, uint64_t);
828 ASAN_ATOMIC_FUNC_TESTANDCLEAR(int, u_int);
829 ASAN_ATOMIC_FUNC_TESTANDCLEAR(long, u_long);
830 ASAN_ATOMIC_FUNC_TESTANDCLEAR(ptr, uintptr_t);
831 
832 ASAN_ATOMIC_FUNC_TESTANDSET(32, uint32_t);
833 ASAN_ATOMIC_FUNC_TESTANDSET(64, uint64_t);
834 ASAN_ATOMIC_FUNC_TESTANDSET(int, u_int);
835 ASAN_ATOMIC_FUNC_TESTANDSET(long, u_long);
836 ASAN_ATOMIC_FUNC_TESTANDSET(ptr, uintptr_t);
837 
838 ASAN_ATOMIC_FUNC_SWAP(32, uint32_t);
839 ASAN_ATOMIC_FUNC_SWAP(64, uint64_t);
840 ASAN_ATOMIC_FUNC_SWAP(int, u_int);
841 ASAN_ATOMIC_FUNC_SWAP(long, u_long);
842 ASAN_ATOMIC_FUNC_SWAP(ptr, uintptr_t);
843 
844 ASAN_ATOMIC_FUNC_CMPSET(8, uint8_t);
845 ASAN_ATOMIC_FUNC_CMPSET(16, uint16_t);
846 ASAN_ATOMIC_FUNC_CMPSET(32, uint32_t);
847 ASAN_ATOMIC_FUNC_CMPSET(64, uint64_t);
848 ASAN_ATOMIC_FUNC_CMPSET(int, u_int);
849 ASAN_ATOMIC_FUNC_CMPSET(long, u_long);
850 ASAN_ATOMIC_FUNC_CMPSET(ptr, uintptr_t);
851 
852 ASAN_ATOMIC_FUNC_FCMPSET(8, uint8_t);
853 ASAN_ATOMIC_FUNC_FCMPSET(16, uint16_t);
854 ASAN_ATOMIC_FUNC_FCMPSET(32, uint32_t);
855 ASAN_ATOMIC_FUNC_FCMPSET(64, uint64_t);
856 ASAN_ATOMIC_FUNC_FCMPSET(int, u_int);
857 ASAN_ATOMIC_FUNC_FCMPSET(long, u_long);
858 ASAN_ATOMIC_FUNC_FCMPSET(ptr, uintptr_t);
859 
860 _ASAN_ATOMIC_FUNC_LOAD(bool, bool);
861 ASAN_ATOMIC_FUNC_LOAD(8, uint8_t);
862 ASAN_ATOMIC_FUNC_LOAD(16, uint16_t);
863 ASAN_ATOMIC_FUNC_LOAD(32, uint32_t);
864 ASAN_ATOMIC_FUNC_LOAD(64, uint64_t);
865 ASAN_ATOMIC_FUNC_LOAD(char, u_char);
866 ASAN_ATOMIC_FUNC_LOAD(short, u_short);
867 ASAN_ATOMIC_FUNC_LOAD(int, u_int);
868 ASAN_ATOMIC_FUNC_LOAD(long, u_long);
869 ASAN_ATOMIC_FUNC_LOAD(ptr, uintptr_t);
870 
871 _ASAN_ATOMIC_FUNC_STORE(bool, bool);
872 ASAN_ATOMIC_FUNC_STORE(8, uint8_t);
873 ASAN_ATOMIC_FUNC_STORE(16, uint16_t);
874 ASAN_ATOMIC_FUNC_STORE(32, uint32_t);
875 ASAN_ATOMIC_FUNC_STORE(64, uint64_t);
876 ASAN_ATOMIC_FUNC_STORE(char, u_char);
877 ASAN_ATOMIC_FUNC_STORE(short, u_short);
878 ASAN_ATOMIC_FUNC_STORE(int, u_int);
879 ASAN_ATOMIC_FUNC_STORE(long, u_long);
880 ASAN_ATOMIC_FUNC_STORE(ptr, uintptr_t);
881 
882 ASAN_ATOMIC_FUNC_THREAD_FENCE(acq);
883 ASAN_ATOMIC_FUNC_THREAD_FENCE(rel);
884 ASAN_ATOMIC_FUNC_THREAD_FENCE(acq_rel);
885 ASAN_ATOMIC_FUNC_THREAD_FENCE(seq_cst);
886 
887 void
kasan_atomic_interrupt_fence(void)888 kasan_atomic_interrupt_fence(void)
889 {
890 }
891 
892 /* -------------------------------------------------------------------------- */
893 
894 #include <sys/bus.h>
895 #include <machine/bus.h>
896 #include <sys/bus_san.h>
897 
898 int
kasan_bus_space_map(bus_space_tag_t tag,bus_addr_t hnd,bus_size_t size,int flags,bus_space_handle_t * handlep)899 kasan_bus_space_map(bus_space_tag_t tag, bus_addr_t hnd, bus_size_t size,
900     int flags, bus_space_handle_t *handlep)
901 {
902 	return (bus_space_map(tag, hnd, size, flags, handlep));
903 }
904 
905 void
kasan_bus_space_unmap(bus_space_tag_t tag,bus_space_handle_t hnd,bus_size_t size)906 kasan_bus_space_unmap(bus_space_tag_t tag, bus_space_handle_t hnd,
907     bus_size_t size)
908 {
909 	bus_space_unmap(tag, hnd, size);
910 }
911 
912 int
kasan_bus_space_subregion(bus_space_tag_t tag,bus_space_handle_t hnd,bus_size_t offset,bus_size_t size,bus_space_handle_t * handlep)913 kasan_bus_space_subregion(bus_space_tag_t tag, bus_space_handle_t hnd,
914     bus_size_t offset, bus_size_t size, bus_space_handle_t *handlep)
915 {
916 	return (bus_space_subregion(tag, hnd, offset, size, handlep));
917 }
918 
919 void
kasan_bus_space_free(bus_space_tag_t tag,bus_space_handle_t hnd,bus_size_t size)920 kasan_bus_space_free(bus_space_tag_t tag, bus_space_handle_t hnd,
921     bus_size_t size)
922 {
923 	bus_space_free(tag, hnd, size);
924 }
925 
926 void
kasan_bus_space_barrier(bus_space_tag_t tag,bus_space_handle_t hnd,bus_size_t offset,bus_size_t size,int flags)927 kasan_bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t hnd,
928     bus_size_t offset, bus_size_t size, int flags)
929 {
930 	bus_space_barrier(tag, hnd, offset, size, flags);
931 }
932 
933 #define ASAN_BUS_READ_FUNC(func, width, type)				\
934 	type kasan_bus_space_read##func##_##width(bus_space_tag_t tag,	\
935 	    bus_space_handle_t hnd, bus_size_t offset)			\
936 	{								\
937 		return (bus_space_read##func##_##width(tag, hnd,	\
938 		    offset));						\
939 	}								\
940 
941 #define ASAN_BUS_READ_PTR_FUNC(func, width, type)			\
942 	void kasan_bus_space_read_##func##_##width(bus_space_tag_t tag,	\
943 	    bus_space_handle_t hnd, bus_size_t size, type *buf,		\
944 	    bus_size_t count)						\
945 	{								\
946 		kasan_shadow_check((uintptr_t)buf, sizeof(type) * count,\
947 		    false, __RET_ADDR);					\
948 		bus_space_read_##func##_##width(tag, hnd, size, buf, 	\
949 		    count);						\
950 	}
951 
952 ASAN_BUS_READ_FUNC(, 1, uint8_t)
953 ASAN_BUS_READ_FUNC(_stream, 1, uint8_t)
954 ASAN_BUS_READ_PTR_FUNC(multi, 1, uint8_t)
955 ASAN_BUS_READ_PTR_FUNC(multi_stream, 1, uint8_t)
956 ASAN_BUS_READ_PTR_FUNC(region, 1, uint8_t)
957 ASAN_BUS_READ_PTR_FUNC(region_stream, 1, uint8_t)
958 
959 ASAN_BUS_READ_FUNC(, 2, uint16_t)
960 ASAN_BUS_READ_FUNC(_stream, 2, uint16_t)
961 ASAN_BUS_READ_PTR_FUNC(multi, 2, uint16_t)
962 ASAN_BUS_READ_PTR_FUNC(multi_stream, 2, uint16_t)
963 ASAN_BUS_READ_PTR_FUNC(region, 2, uint16_t)
964 ASAN_BUS_READ_PTR_FUNC(region_stream, 2, uint16_t)
965 
966 ASAN_BUS_READ_FUNC(, 4, uint32_t)
967 ASAN_BUS_READ_FUNC(_stream, 4, uint32_t)
968 ASAN_BUS_READ_PTR_FUNC(multi, 4, uint32_t)
969 ASAN_BUS_READ_PTR_FUNC(multi_stream, 4, uint32_t)
970 ASAN_BUS_READ_PTR_FUNC(region, 4, uint32_t)
971 ASAN_BUS_READ_PTR_FUNC(region_stream, 4, uint32_t)
972 
973 ASAN_BUS_READ_FUNC(, 8, uint64_t)
974 #if defined(__aarch64__)
975 ASAN_BUS_READ_FUNC(_stream, 8, uint64_t)
976 ASAN_BUS_READ_PTR_FUNC(multi, 8, uint64_t)
977 ASAN_BUS_READ_PTR_FUNC(multi_stream, 8, uint64_t)
978 ASAN_BUS_READ_PTR_FUNC(region, 8, uint64_t)
979 ASAN_BUS_READ_PTR_FUNC(region_stream, 8, uint64_t)
980 #endif
981 
982 #define	ASAN_BUS_WRITE_FUNC(func, width, type)				\
983 	void kasan_bus_space_write##func##_##width(bus_space_tag_t tag,	\
984 	    bus_space_handle_t hnd, bus_size_t offset, type value)	\
985 	{								\
986 		bus_space_write##func##_##width(tag, hnd, offset, value);\
987 	}								\
988 
989 #define	ASAN_BUS_WRITE_PTR_FUNC(func, width, type)			\
990 	void kasan_bus_space_write_##func##_##width(bus_space_tag_t tag,\
991 	    bus_space_handle_t hnd, bus_size_t size, const type *buf,	\
992 	    bus_size_t count)						\
993 	{								\
994 		kasan_shadow_check((uintptr_t)buf, sizeof(type) * count,\
995 		    true, __RET_ADDR);					\
996 		bus_space_write_##func##_##width(tag, hnd, size, buf, 	\
997 		    count);						\
998 	}
999 
1000 ASAN_BUS_WRITE_FUNC(, 1, uint8_t)
1001 ASAN_BUS_WRITE_FUNC(_stream, 1, uint8_t)
1002 ASAN_BUS_WRITE_PTR_FUNC(multi, 1, uint8_t)
1003 ASAN_BUS_WRITE_PTR_FUNC(multi_stream, 1, uint8_t)
1004 ASAN_BUS_WRITE_PTR_FUNC(region, 1, uint8_t)
1005 ASAN_BUS_WRITE_PTR_FUNC(region_stream, 1, uint8_t)
1006 
1007 ASAN_BUS_WRITE_FUNC(, 2, uint16_t)
1008 ASAN_BUS_WRITE_FUNC(_stream, 2, uint16_t)
1009 ASAN_BUS_WRITE_PTR_FUNC(multi, 2, uint16_t)
1010 ASAN_BUS_WRITE_PTR_FUNC(multi_stream, 2, uint16_t)
1011 ASAN_BUS_WRITE_PTR_FUNC(region, 2, uint16_t)
1012 ASAN_BUS_WRITE_PTR_FUNC(region_stream, 2, uint16_t)
1013 
1014 ASAN_BUS_WRITE_FUNC(, 4, uint32_t)
1015 ASAN_BUS_WRITE_FUNC(_stream, 4, uint32_t)
1016 ASAN_BUS_WRITE_PTR_FUNC(multi, 4, uint32_t)
1017 ASAN_BUS_WRITE_PTR_FUNC(multi_stream, 4, uint32_t)
1018 ASAN_BUS_WRITE_PTR_FUNC(region, 4, uint32_t)
1019 ASAN_BUS_WRITE_PTR_FUNC(region_stream, 4, uint32_t)
1020 
1021 ASAN_BUS_WRITE_FUNC(, 8, uint64_t)
1022 
1023 #define	ASAN_BUS_SET_FUNC(func, width, type)				\
1024 	void kasan_bus_space_set_##func##_##width(bus_space_tag_t tag,	\
1025 	    bus_space_handle_t hnd, bus_size_t offset, type value,	\
1026 	    bus_size_t count)						\
1027 	{								\
1028 		bus_space_set_##func##_##width(tag, hnd, offset, value,	\
1029 		    count);						\
1030 	}
1031 
1032 ASAN_BUS_SET_FUNC(multi, 1, uint8_t)
1033 ASAN_BUS_SET_FUNC(region, 1, uint8_t)
1034 ASAN_BUS_SET_FUNC(multi_stream, 1, uint8_t)
1035 ASAN_BUS_SET_FUNC(region_stream, 1, uint8_t)
1036 
1037 ASAN_BUS_SET_FUNC(multi, 2, uint16_t)
1038 ASAN_BUS_SET_FUNC(region, 2, uint16_t)
1039 ASAN_BUS_SET_FUNC(multi_stream, 2, uint16_t)
1040 ASAN_BUS_SET_FUNC(region_stream, 2, uint16_t)
1041 
1042 ASAN_BUS_SET_FUNC(multi, 4, uint32_t)
1043 ASAN_BUS_SET_FUNC(region, 4, uint32_t)
1044 ASAN_BUS_SET_FUNC(multi_stream, 4, uint32_t)
1045 ASAN_BUS_SET_FUNC(region_stream, 4, uint32_t)
1046 
1047 #define	ASAN_BUS_PEEK_FUNC(width, type)					\
1048 	int kasan_bus_space_peek_##width(bus_space_tag_t tag,		\
1049 	    bus_space_handle_t hnd, bus_size_t offset, type *valuep)	\
1050 	{								\
1051 		return (bus_space_peek_##width(tag, hnd, offset,	\
1052 		    valuep));						\
1053 	}
1054 
1055 ASAN_BUS_PEEK_FUNC(1, uint8_t)
1056 ASAN_BUS_PEEK_FUNC(2, uint16_t)
1057 ASAN_BUS_PEEK_FUNC(4, uint32_t)
1058 ASAN_BUS_PEEK_FUNC(8, uint64_t)
1059 
1060 #define	ASAN_BUS_POKE_FUNC(width, type)					\
1061 	int kasan_bus_space_poke_##width(bus_space_tag_t tag,		\
1062 	    bus_space_handle_t hnd, bus_size_t offset, type value)	\
1063 	{								\
1064 		return (bus_space_poke_##width(tag, hnd, offset,	\
1065 		    value));						\
1066 	}
1067 
1068 ASAN_BUS_POKE_FUNC(1, uint8_t)
1069 ASAN_BUS_POKE_FUNC(2, uint16_t)
1070 ASAN_BUS_POKE_FUNC(4, uint32_t)
1071 ASAN_BUS_POKE_FUNC(8, uint64_t)
1072 
1073 /* -------------------------------------------------------------------------- */
1074 
1075 void __asan_register_globals(struct __asan_global *, size_t);
1076 void __asan_unregister_globals(struct __asan_global *, size_t);
1077 
1078 void
__asan_register_globals(struct __asan_global * globals,size_t n)1079 __asan_register_globals(struct __asan_global *globals, size_t n)
1080 {
1081 	size_t i;
1082 
1083 	for (i = 0; i < n; i++) {
1084 		kasan_mark(globals[i].beg, globals[i].size,
1085 		    globals[i].size_with_redzone, KASAN_GENERIC_REDZONE);
1086 	}
1087 }
1088 
1089 void
__asan_unregister_globals(struct __asan_global * globals,size_t n)1090 __asan_unregister_globals(struct __asan_global *globals, size_t n)
1091 {
1092 	size_t i;
1093 
1094 	for (i = 0; i < n; i++) {
1095 		kasan_mark(globals[i].beg, globals[i].size_with_redzone,
1096 		    globals[i].size_with_redzone, 0);
1097 	}
1098 }
1099 
1100 #define ASAN_LOAD_STORE(size)					\
1101 	void __asan_load##size(unsigned long);			\
1102 	void __asan_load##size(unsigned long addr)		\
1103 	{							\
1104 		kasan_shadow_check(addr, size, false, __RET_ADDR);\
1105 	} 							\
1106 	void __asan_load##size##_noabort(unsigned long);	\
1107 	void __asan_load##size##_noabort(unsigned long addr)	\
1108 	{							\
1109 		kasan_shadow_check(addr, size, false, __RET_ADDR);\
1110 	}							\
1111 	void __asan_store##size(unsigned long);			\
1112 	void __asan_store##size(unsigned long addr)		\
1113 	{							\
1114 		kasan_shadow_check(addr, size, true, __RET_ADDR);\
1115 	}							\
1116 	void __asan_store##size##_noabort(unsigned long);	\
1117 	void __asan_store##size##_noabort(unsigned long addr)	\
1118 	{							\
1119 		kasan_shadow_check(addr, size, true, __RET_ADDR);\
1120 	}
1121 
1122 ASAN_LOAD_STORE(1);
1123 ASAN_LOAD_STORE(2);
1124 ASAN_LOAD_STORE(4);
1125 ASAN_LOAD_STORE(8);
1126 ASAN_LOAD_STORE(16);
1127 
1128 void __asan_loadN(unsigned long, size_t);
1129 void __asan_loadN_noabort(unsigned long, size_t);
1130 void __asan_storeN(unsigned long, size_t);
1131 void __asan_storeN_noabort(unsigned long, size_t);
1132 void __asan_handle_no_return(void);
1133 
1134 void
__asan_loadN(unsigned long addr,size_t size)1135 __asan_loadN(unsigned long addr, size_t size)
1136 {
1137 	kasan_shadow_check(addr, size, false, __RET_ADDR);
1138 }
1139 
1140 void
__asan_loadN_noabort(unsigned long addr,size_t size)1141 __asan_loadN_noabort(unsigned long addr, size_t size)
1142 {
1143 	kasan_shadow_check(addr, size, false, __RET_ADDR);
1144 }
1145 
1146 void
__asan_storeN(unsigned long addr,size_t size)1147 __asan_storeN(unsigned long addr, size_t size)
1148 {
1149 	kasan_shadow_check(addr, size, true, __RET_ADDR);
1150 }
1151 
1152 void
__asan_storeN_noabort(unsigned long addr,size_t size)1153 __asan_storeN_noabort(unsigned long addr, size_t size)
1154 {
1155 	kasan_shadow_check(addr, size, true, __RET_ADDR);
1156 }
1157 
1158 void
__asan_handle_no_return(void)1159 __asan_handle_no_return(void)
1160 {
1161 	/* nothing */
1162 }
1163 
1164 #define ASAN_SET_SHADOW(byte) \
1165 	void __asan_set_shadow_##byte(void *, size_t);			\
1166 	void __asan_set_shadow_##byte(void *addr, size_t size)		\
1167 	{								\
1168 		__builtin_memset((void *)addr, 0x##byte, size);		\
1169 	}
1170 
1171 ASAN_SET_SHADOW(00);
1172 ASAN_SET_SHADOW(f1);
1173 ASAN_SET_SHADOW(f2);
1174 ASAN_SET_SHADOW(f3);
1175 ASAN_SET_SHADOW(f5);
1176 ASAN_SET_SHADOW(f8);
1177 
1178 void __asan_poison_stack_memory(const void *, size_t);
1179 void __asan_unpoison_stack_memory(const void *, size_t);
1180 
1181 void
__asan_poison_stack_memory(const void * addr,size_t size)1182 __asan_poison_stack_memory(const void *addr, size_t size)
1183 {
1184 	size = roundup(size, KASAN_SHADOW_SCALE);
1185 	kasan_shadow_Nbyte_fill(addr, size, KASAN_USE_AFTER_SCOPE);
1186 }
1187 
1188 void
__asan_unpoison_stack_memory(const void * addr,size_t size)1189 __asan_unpoison_stack_memory(const void *addr, size_t size)
1190 {
1191 	kasan_shadow_Nbyte_markvalid(addr, size);
1192 }
1193 
1194 void __asan_alloca_poison(const void *, size_t);
1195 void __asan_allocas_unpoison(const void *, const void *);
1196 
1197 void
__asan_alloca_poison(const void * addr,size_t size)1198 __asan_alloca_poison(const void *addr, size_t size)
1199 {
1200 	const void *l, *r;
1201 
1202 	KASSERT((vm_offset_t)addr % KASAN_ALLOCA_SCALE_SIZE == 0,
1203 	    ("%s: invalid address %p", __func__, addr));
1204 
1205 	l = (const uint8_t *)addr - KASAN_ALLOCA_SCALE_SIZE;
1206 	r = (const uint8_t *)addr + roundup(size, KASAN_ALLOCA_SCALE_SIZE);
1207 
1208 	kasan_shadow_Nbyte_fill(l, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_LEFT);
1209 	kasan_mark(addr, size, roundup(size, KASAN_ALLOCA_SCALE_SIZE),
1210 	    KASAN_STACK_MID);
1211 	kasan_shadow_Nbyte_fill(r, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_RIGHT);
1212 }
1213 
1214 void
__asan_allocas_unpoison(const void * stkbegin,const void * stkend)1215 __asan_allocas_unpoison(const void *stkbegin, const void *stkend)
1216 {
1217 	size_t size;
1218 
1219 	if (__predict_false(!stkbegin))
1220 		return;
1221 	if (__predict_false((uintptr_t)stkbegin > (uintptr_t)stkend))
1222 		return;
1223 	size = (uintptr_t)stkend - (uintptr_t)stkbegin;
1224 
1225 	kasan_shadow_Nbyte_fill(stkbegin, size, 0);
1226 }
1227 
1228 void __asan_poison_memory_region(const void *addr, size_t size);
1229 void __asan_unpoison_memory_region(const void *addr, size_t size);
1230 
1231 void
__asan_poison_memory_region(const void * addr,size_t size)1232 __asan_poison_memory_region(const void *addr, size_t size)
1233 {
1234 }
1235 
1236 void
__asan_unpoison_memory_region(const void * addr,size_t size)1237 __asan_unpoison_memory_region(const void *addr, size_t size)
1238 {
1239 }
1240