xref: /xnu-8019.80.24/san/memory/kasan-memintrinsics.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <string.h>
30 #include <mach/boolean.h>
31 
32 #include <mach/boolean.h>
33 #include <machine/limits.h>
34 #include <kern/debug.h>
35 
36 #include "kasan_internal.h"
37 #include "memintrinsics.h"
38 
39 void
__asan_bcopy(const void * src,void * dst,size_t sz)40 __asan_bcopy(const void *src, void *dst, size_t sz)
41 {
42 	kasan_check_range(src, sz, TYPE_MEMR);
43 	kasan_check_range(dst, sz, TYPE_MEMW);
44 	__nosan_bcopy(src, dst, sz);
45 }
46 
47 void *
__asan_memmove(void * src,const void * dst,size_t sz)48 __asan_memmove(void *src, const void *dst, size_t sz)
49 {
50 	kasan_check_range(src, sz, TYPE_MEMR);
51 	kasan_check_range(dst, sz, TYPE_MEMW);
52 	return __nosan_memmove(src, dst, sz);
53 }
54 
55 void *
__asan_memcpy(void * dst,const void * src,size_t sz)56 __asan_memcpy(void *dst, const void *src, size_t sz)
57 {
58 	kasan_check_range(src, sz, TYPE_MEMR);
59 	kasan_check_range(dst, sz, TYPE_MEMW);
60 	return __nosan_memcpy(dst, src, sz);
61 }
62 
63 void *
__asan_memset(void * dst,int c,size_t sz)64 __asan_memset(void *dst, int c, size_t sz)
65 {
66 	kasan_check_range(dst, sz, TYPE_MEMW);
67 	return __nosan_memset(dst, c, sz);
68 }
69 
70 void
__asan_bzero(void * dst,size_t sz)71 __asan_bzero(void *dst, size_t sz)
72 {
73 	kasan_check_range(dst, sz, TYPE_MEMW);
74 	__nosan_bzero(dst, sz);
75 }
76 
77 int
__asan_bcmp(const void * a,const void * b,size_t len)78 __asan_bcmp(const void *a, const void *b, size_t len)
79 {
80 	kasan_check_range(a, len, TYPE_MEMR);
81 	kasan_check_range(b, len, TYPE_MEMR);
82 	return __nosan_bcmp(a, b, len);
83 }
84 
85 int
__asan_memcmp(const void * a,const void * b,size_t n)86 __asan_memcmp(const void *a, const void *b, size_t n)
87 {
88 	kasan_check_range(a, n, TYPE_MEMR);
89 	kasan_check_range(b, n, TYPE_MEMR);
90 	return __nosan_memcmp(a, b, n);
91 }
92 
93 size_t
__asan_strlcpy(char * dst,const char * src,size_t sz)94 __asan_strlcpy(char *dst, const char *src, size_t sz)
95 {
96 	kasan_check_range(dst, sz, TYPE_STRW);
97 	return __nosan_strlcpy(dst, src, sz);
98 }
99 
100 size_t
__asan_strlcat(char * dst,const char * src,size_t sz)101 __asan_strlcat(char *dst, const char *src, size_t sz)
102 {
103 	kasan_check_range(dst, sz, TYPE_STRW);
104 	return __nosan_strlcat(dst, src, sz);
105 }
106 
107 char *
__asan_strncpy(char * dst,const char * src,size_t sz)108 __asan_strncpy(char *dst, const char *src, size_t sz)
109 {
110 	kasan_check_range(dst, sz, TYPE_STRW);
111 	return __nosan_strncpy(dst, src, sz);
112 }
113 
114 char *
__asan_strncat(char * dst,const char * src,size_t sz)115 __asan_strncat(char *dst, const char *src, size_t sz)
116 {
117 	kasan_check_range(dst, strlen(dst) + sz + 1, TYPE_STRW);
118 	return __nosan_strncat(dst, src, sz);
119 }
120 
121 size_t
__asan_strnlen(const char * src,size_t sz)122 __asan_strnlen(const char *src, size_t sz)
123 {
124 	kasan_check_range(src, sz, TYPE_STRR);
125 	return __nosan_strnlen(src, sz);
126 }
127 
128 size_t
__asan_strlen(const char * src)129 __asan_strlen(const char *src)
130 {
131 	size_t sz = __nosan_strlen(src);
132 	kasan_check_range(src, sz + 1, TYPE_STRR);
133 	return sz;
134 }
135