1 /*
2 * Copyright (c) 2000-2006 Apple Computer, 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 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
32 * Version 2.0.
33 */
34 /*
35 * HISTORY
36 * @OSF_COPYRIGHT@
37 */
38 #ifndef _STRING_H_
39 #define _STRING_H_ 1
40
41 #ifdef MACH_KERNEL_PRIVATE
42 #include <types.h>
43 #else
44 #include <sys/types.h>
45 #endif
46 #include <sys/cdefs.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 #ifndef NULL
53 #if defined (__cplusplus)
54 #if __cplusplus >= 201103L
55 #define NULL nullptr
56 #else
57 #define NULL 0
58 #endif
59 #else
60 #define NULL ((void *)0)
61 #endif
62 #endif
63
64 extern void *memcpy(void *dst __sized_by(n), const void *src __sized_by(n), size_t n);
65 extern int memcmp(const void *s1 __sized_by(n), const void *s2 __sized_by(n), size_t n) __stateful_pure;
66 extern void *memmove(void *dst __sized_by(n), const void *src __sized_by(n), size_t n);
67 extern void *memset(void *s __sized_by(n), int, size_t n);
68 extern int memset_s(void *s __sized_by(smax), size_t smax, int c, size_t n);
69
70 #ifdef XNU_KERNEL_PRIVATE
71 /* memcmp_zero_ptr_aligned() checks string s of n bytes contains all zeros.
72 * Address and size of the string s must be pointer-aligned.
73 * Return 0 if true, 1 otherwise. Also return 0 if n is 0.
74 */
75 extern unsigned long memcmp_zero_ptr_aligned(const void *s __sized_by(n), size_t n) __stateful_pure;
76 #endif
77
78 extern size_t strlen(const char *) __stateful_pure;
79 extern size_t strnlen(const char *, size_t) __stateful_pure;
80
81 /* strcpy() and strncpy() are deprecated. Please use strlcpy() instead. */
82 __kpi_deprecated_arm64_macos_unavailable
83 extern char *strcpy(char *, const char *) __deprecated;
84
85 __kpi_deprecated_arm64_macos_unavailable
86 extern char *strncpy(char *, const char *, size_t);
87
88 /* strcat() and strncat() are deprecated. Please use strlcat() instead. */
89 __kpi_deprecated_arm64_macos_unavailable
90 extern char *strcat(char *, const char *) __deprecated;
91
92 __kpi_deprecated_arm64_macos_unavailable
93 extern char *strncat(char *, const char *, size_t);
94
95 extern int strcmp(const char *, const char *) __stateful_pure;
96 extern int strncmp(const char *, const char *, size_t) __stateful_pure;
97
98 extern size_t strlcpy(char *, const char *, size_t);
99 extern size_t strlcat(char *, const char *, size_t);
100
101 extern int strcasecmp(const char *s1, const char *s2) __stateful_pure;
102 extern int strncasecmp(const char *s1, const char *s2, size_t n) __stateful_pure;
103 #ifdef XNU_KERNEL_PRIVATE
104 extern const char *strnstr(const char *s, const char *find, size_t slen) __stateful_pure;
105 #else
106 extern char *strnstr(const char *s, const char *find, size_t slen) __stateful_pure;
107 #endif
108 extern char *strchr(const char *s, int c) __stateful_pure;
109 #ifdef XNU_KERNEL_PRIVATE
110 extern char *strrchr(const char *s, int c) __stateful_pure;
111 #endif
112 extern char *STRDUP(const char *, int);
113 extern int strprefix(const char *s1, const char *s2) __stateful_pure;
114
115 extern int bcmp(const void *s1 __sized_by(n), const void *s2 __sized_by(n), size_t n) __stateful_pure;
116 extern void bcopy(const void *src __sized_by(n), void *dst __sized_by(n), size_t n);
117 extern void bzero(void *s __sized_by(n), size_t n);
118 extern int timingsafe_bcmp(const void *b1 __sized_by(n), const void *b2 __sized_by(n), size_t n);
119
120 #ifdef PRIVATE
121 #include <san/memintrinsics.h>
122 #endif
123
124 #if __has_builtin(__builtin_dynamic_object_size)
125 #define XNU_BOS __builtin_dynamic_object_size
126 #else
127 #define XNU_BOS __builtin_object_size
128 #endif
129
130 /* __nochk_ functions for opting out of type 1 bounds checking */
131 __attribute__((always_inline)) static inline void *
__nochk_memcpy(void * dest __sized_by (len),const void * src __sized_by (len),size_t len)132 __nochk_memcpy(void *dest __sized_by(len), const void *src __sized_by(len), size_t len)
133 {
134 return __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, 0));
135 }
136 __attribute__((always_inline)) static inline void *
__nochk_memmove(void * dest __sized_by (len),const void * src __sized_by (len),size_t len)137 __nochk_memmove(void *dest __sized_by(len), const void *src __sized_by(len), size_t len)
138 {
139 return __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
140 }
141 __attribute__((always_inline)) static inline void
__nochk_bcopy(const void * src __sized_by (len),void * dest __sized_by (len),size_t len)142 __nochk_bcopy(const void *src __sized_by(len), void *dest __sized_by(len), size_t len)
143 {
144 __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
145 }
146
147 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_13
148 /* older deployment target */
149 #elif defined(KASAN) || (defined (_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 0)
150 /* _FORTIFY_SOURCE disabled */
151 #else /* _chk macros */
152
153 #if defined XNU_KERNEL_PRIVATE || defined(_FORTIFY_SOURCE_STRICT)
154 /* Stricter checking is optional for kexts. When type is set to 1, __builtin_object_size
155 * returns the size of the closest surrounding sub-object, which would detect copying past
156 * the end of a struct member. */
157 #define BOS_COPY_TYPE 1
158 #else
159 #define BOS_COPY_TYPE 0
160 #endif
161
162 #if __has_builtin(__builtin___memcpy_chk)
163 #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
164 #endif
165
166 #if __has_builtin(__builtin___memmove_chk)
167 #define memmove(dest, src, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
168 #endif
169
170 #if __has_builtin(__builtin___strncpy_chk)
171 #define strncpy(dest, src, len) __builtin___strncpy_chk(dest, src, len, XNU_BOS(dest, 1))
172 #endif
173
174 #if __has_builtin(__builtin___strncat_chk)
175 #define strncat(dest, src, len) __builtin___strncat_chk(dest, src, len, XNU_BOS(dest, 1))
176 #endif
177
178 #if __has_builtin(__builtin___strlcat_chk)
179 #define strlcat(dest, src, len) __builtin___strlcat_chk(dest, src, len, XNU_BOS(dest, 1))
180 #endif
181
182 #if __has_builtin(__builtin___strlcpy_chk)
183 #define strlcpy(dest, src, len) __builtin___strlcpy_chk(dest, src, len, XNU_BOS(dest, 1))
184 #endif
185
186 #if __has_builtin(__builtin___strcpy_chk)
187 #define strcpy(dest, src, len) __builtin___strcpy_chk(dest, src, XNU_BOS(dest, 1))
188 #endif
189
190 #if __has_builtin(__builtin___strcat_chk)
191 #define strcat(dest, src) __builtin___strcat_chk(dest, src, XNU_BOS(dest, 1))
192 #endif
193
194 #if __has_builtin(__builtin___memmove_chk)
195 #define bcopy(src, dest, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
196 #endif
197
198 #if KERNEL_PRIVATE
199 /*
200 * those allow small memsets/bzeros to be reasoned about by the compiler
201 * despite using -fno-builtin
202 */
203 #ifndef memset
204 #define memset(p, c, len) __builtin_memset(p, c, len)
205 #endif
206
207 #ifndef bzero
208 #define bzero(p, len) __builtin_bzero(p, len)
209 #endif
210 #endif /* KERNEL_PRIVATE */
211
212 #endif /* _chk macros */
213 #ifdef __cplusplus
214 }
215 #endif
216
217 #endif /* _STRING_H_ */
218