1 | //===-- msan_interceptors.cpp ---------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is a part of MemorySanitizer. |
10 | // |
11 | // Interceptors for standard library functions. |
12 | // |
13 | // FIXME: move as many interceptors as possible into |
14 | // sanitizer_common/sanitizer_common_interceptors.h |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS |
18 | |
19 | #include "interception/interception.h" |
20 | #include "msan.h" |
21 | #include "msan_chained_origin_depot.h" |
22 | #include "msan_dl.h" |
23 | #include "msan_origin.h" |
24 | #include "msan_poisoning.h" |
25 | #include "msan_report.h" |
26 | #include "msan_thread.h" |
27 | #include "sanitizer_common/sanitizer_allocator.h" |
28 | #include "sanitizer_common/sanitizer_allocator_dlsym.h" |
29 | #include "sanitizer_common/sanitizer_allocator_interface.h" |
30 | #include "sanitizer_common/sanitizer_atomic.h" |
31 | #include "sanitizer_common/sanitizer_common.h" |
32 | #include "sanitizer_common/sanitizer_errno.h" |
33 | #include "sanitizer_common/sanitizer_errno_codes.h" |
34 | #include "sanitizer_common/sanitizer_glibc_version.h" |
35 | #include "sanitizer_common/sanitizer_libc.h" |
36 | #include "sanitizer_common/sanitizer_linux.h" |
37 | #include "sanitizer_common/sanitizer_platform_limits_netbsd.h" |
38 | #include "sanitizer_common/sanitizer_platform_limits_posix.h" |
39 | #include "sanitizer_common/sanitizer_stackdepot.h" |
40 | #include "sanitizer_common/sanitizer_vector.h" |
41 | |
42 | #if SANITIZER_NETBSD |
43 | #define fstat __fstat50 |
44 | #define gettimeofday __gettimeofday50 |
45 | #define getrusage __getrusage50 |
46 | #define tzset __tzset50 |
47 | #endif |
48 | |
49 | #include <stdarg.h> |
50 | // ACHTUNG! No other system header includes in this file. |
51 | // Ideally, we should get rid of stdarg.h as well. |
52 | |
53 | using namespace __msan; |
54 | |
55 | using __sanitizer::memory_order; |
56 | using __sanitizer::atomic_load; |
57 | using __sanitizer::atomic_store; |
58 | using __sanitizer::atomic_uintptr_t; |
59 | |
60 | DECLARE_REAL(SIZE_T, strlen, const char *s) |
61 | DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen) |
62 | DECLARE_REAL(void *, memcpy, void *dest, const void *src, SIZE_T n) |
63 | DECLARE_REAL(void *, memset, void *dest, int c, SIZE_T n) |
64 | |
65 | // True if this is a nested interceptor. |
66 | static THREADLOCAL int in_interceptor_scope; |
67 | |
68 | void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; } |
69 | void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; } |
70 | |
71 | struct InterceptorScope { |
72 | InterceptorScope() { ++in_interceptor_scope; } |
73 | ~InterceptorScope() { --in_interceptor_scope; } |
74 | }; |
75 | |
76 | bool IsInInterceptorScope() { |
77 | return in_interceptor_scope; |
78 | } |
79 | |
80 | struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> { |
81 | static bool UseImpl() { return !msan_inited; } |
82 | }; |
83 | |
84 | #define ENSURE_MSAN_INITED() do { \ |
85 | CHECK(!msan_init_is_running); \ |
86 | if (!msan_inited) { \ |
87 | __msan_init(); \ |
88 | } \ |
89 | } while (0) |
90 | |
91 | // Check that [x, x+n) range is unpoisoned. |
92 | #define CHECK_UNPOISONED_0(x, n) \ |
93 | do { \ |
94 | sptr __offset = __msan_test_shadow(x, n); \ |
95 | if (__msan::IsInSymbolizerOrUnwider()) \ |
96 | break; \ |
97 | if (__offset >= 0 && __msan::flags()->report_umrs) { \ |
98 | GET_CALLER_PC_BP; \ |
99 | ReportUMRInsideAddressRange(__func__, x, n, __offset); \ |
100 | __msan::PrintWarningWithOrigin( \ |
101 | pc, bp, __msan_get_origin((const char *)x + __offset)); \ |
102 | if (__msan::flags()->halt_on_error) { \ |
103 | Printf("Exiting\n"); \ |
104 | Die(); \ |
105 | } \ |
106 | } \ |
107 | } while (0) |
108 | |
109 | // Check that [x, x+n) range is unpoisoned unless we are in a nested |
110 | // interceptor. |
111 | #define CHECK_UNPOISONED(x, n) \ |
112 | do { \ |
113 | if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \ |
114 | } while (0) |
115 | |
116 | #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \ |
117 | CHECK_UNPOISONED((x), \ |
118 | common_flags()->strict_string_checks ? (len) + 1 : (n) ) |
119 | |
120 | #define CHECK_UNPOISONED_STRING(x, n) \ |
121 | CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n)) |
122 | |
123 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
124 | INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb, |
125 | void *file) { |
126 | ENSURE_MSAN_INITED(); |
127 | SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file); |
128 | if (res > 0) |
129 | __msan_unpoison(a: ptr, size: res *size); |
130 | return res; |
131 | } |
132 | #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked) |
133 | #else |
134 | #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED |
135 | #endif |
136 | |
137 | #if !SANITIZER_NETBSD |
138 | INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) { |
139 | return (char *)__msan_memcpy(dst: dest, src, size: n) + n; |
140 | } |
141 | #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy) |
142 | #else |
143 | #define MSAN_MAYBE_INTERCEPT_MEMPCPY |
144 | #endif |
145 | |
146 | INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) { |
147 | ENSURE_MSAN_INITED(); |
148 | void *res = REAL(memccpy)(dest, src, c, n); |
149 | CHECK(!res || (res >= dest && res <= (char *)dest + n)); |
150 | SIZE_T sz = res ? (char *)res - (char *)dest : n; |
151 | CHECK_UNPOISONED(src, sz); |
152 | __msan_unpoison(a: dest, size: sz); |
153 | return res; |
154 | } |
155 | |
156 | INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) { |
157 | return __msan_memmove(dest, src, n); |
158 | } |
159 | |
160 | INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) { |
161 | GET_MALLOC_STACK_TRACE; |
162 | CHECK_NE(memptr, 0); |
163 | int res = msan_posix_memalign(memptr, alignment, size, stack: &stack); |
164 | if (!res) |
165 | __msan_unpoison(a: memptr, size: sizeof(*memptr)); |
166 | return res; |
167 | } |
168 | |
169 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
170 | INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) { |
171 | GET_MALLOC_STACK_TRACE; |
172 | return msan_memalign(alignment, size, stack: &stack); |
173 | } |
174 | #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign) |
175 | #else |
176 | #define MSAN_MAYBE_INTERCEPT_MEMALIGN |
177 | #endif |
178 | |
179 | INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) { |
180 | GET_MALLOC_STACK_TRACE; |
181 | return msan_aligned_alloc(alignment, size, stack: &stack); |
182 | } |
183 | |
184 | #if !SANITIZER_NETBSD |
185 | INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) { |
186 | GET_MALLOC_STACK_TRACE; |
187 | return msan_memalign(alignment, size, stack: &stack); |
188 | } |
189 | #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign) |
190 | #else |
191 | #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN |
192 | #endif |
193 | |
194 | INTERCEPTOR(void *, valloc, SIZE_T size) { |
195 | GET_MALLOC_STACK_TRACE; |
196 | return msan_valloc(size, stack: &stack); |
197 | } |
198 | |
199 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
200 | INTERCEPTOR(void *, pvalloc, SIZE_T size) { |
201 | GET_MALLOC_STACK_TRACE; |
202 | return msan_pvalloc(size, stack: &stack); |
203 | } |
204 | #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc) |
205 | #else |
206 | #define MSAN_MAYBE_INTERCEPT_PVALLOC |
207 | #endif |
208 | |
209 | INTERCEPTOR(void, free, void *ptr) { |
210 | if (UNLIKELY(!ptr)) |
211 | return; |
212 | if (DlsymAlloc::PointerIsMine(ptr)) |
213 | return DlsymAlloc::Free(ptr); |
214 | GET_MALLOC_STACK_TRACE; |
215 | MsanDeallocate(stack: &stack, ptr); |
216 | } |
217 | |
218 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
219 | INTERCEPTOR(void, cfree, void *ptr) { |
220 | if (UNLIKELY(!ptr)) |
221 | return; |
222 | if (DlsymAlloc::PointerIsMine(ptr)) |
223 | return DlsymAlloc::Free(ptr); |
224 | GET_MALLOC_STACK_TRACE; |
225 | MsanDeallocate(stack: &stack, ptr); |
226 | } |
227 | # define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree) |
228 | #else |
229 | #define MSAN_MAYBE_INTERCEPT_CFREE |
230 | #endif |
231 | |
232 | #if !SANITIZER_NETBSD |
233 | INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { |
234 | return __sanitizer_get_allocated_size(p: ptr); |
235 | } |
236 | #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \ |
237 | INTERCEPT_FUNCTION(malloc_usable_size) |
238 | #else |
239 | #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE |
240 | #endif |
241 | |
242 | #if (!SANITIZER_FREEBSD && !SANITIZER_NETBSD) || __GLIBC_PREREQ(2, 33) |
243 | template <class T> |
244 | static NOINLINE void clear_mallinfo(T *sret) { |
245 | ENSURE_MSAN_INITED(); |
246 | internal_memset(sret, 0, sizeof(*sret)); |
247 | __msan_unpoison(sret, sizeof(*sret)); |
248 | } |
249 | #endif |
250 | |
251 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
252 | // Interceptors use NRVO and assume that sret will be pre-allocated in |
253 | // caller frame. |
254 | INTERCEPTOR(__sanitizer_struct_mallinfo, mallinfo,) { |
255 | __sanitizer_struct_mallinfo sret; |
256 | clear_mallinfo(sret: &sret); |
257 | return sret; |
258 | } |
259 | # define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo) |
260 | #else |
261 | # define MSAN_MAYBE_INTERCEPT_MALLINFO |
262 | #endif |
263 | |
264 | #if __GLIBC_PREREQ(2, 33) |
265 | INTERCEPTOR(__sanitizer_struct_mallinfo2, mallinfo2) { |
266 | __sanitizer_struct_mallinfo2 sret; |
267 | clear_mallinfo(sret: &sret); |
268 | return sret; |
269 | } |
270 | # define MSAN_MAYBE_INTERCEPT_MALLINFO2 INTERCEPT_FUNCTION(mallinfo2) |
271 | #else |
272 | # define MSAN_MAYBE_INTERCEPT_MALLINFO2 |
273 | #endif |
274 | |
275 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
276 | INTERCEPTOR(int, mallopt, int cmd, int value) { |
277 | return 0; |
278 | } |
279 | #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt) |
280 | #else |
281 | #define MSAN_MAYBE_INTERCEPT_MALLOPT |
282 | #endif |
283 | |
284 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
285 | INTERCEPTOR(void, malloc_stats, void) { |
286 | // FIXME: implement, but don't call REAL(malloc_stats)! |
287 | } |
288 | #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats) |
289 | #else |
290 | #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS |
291 | #endif |
292 | |
293 | INTERCEPTOR(char *, strcpy, char *dest, const char *src) { |
294 | ENSURE_MSAN_INITED(); |
295 | GET_STORE_STACK_TRACE; |
296 | SIZE_T n = internal_strlen(s: src); |
297 | CHECK_UNPOISONED_STRING(src + n, 0); |
298 | char *res = REAL(strcpy)(dest, src); |
299 | CopyShadowAndOrigin(dst: dest, src, size: n + 1, stack: &stack); |
300 | return res; |
301 | } |
302 | |
303 | INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { |
304 | ENSURE_MSAN_INITED(); |
305 | GET_STORE_STACK_TRACE; |
306 | SIZE_T copy_size = internal_strnlen(s: src, maxlen: n); |
307 | if (copy_size < n) |
308 | copy_size++; // trailing \0 |
309 | char *res = REAL(strncpy)(dest, src, n); |
310 | CopyShadowAndOrigin(dst: dest, src, size: copy_size, stack: &stack); |
311 | __msan_unpoison(a: dest + copy_size, size: n - copy_size); |
312 | return res; |
313 | } |
314 | |
315 | #if !SANITIZER_NETBSD |
316 | INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { |
317 | ENSURE_MSAN_INITED(); |
318 | GET_STORE_STACK_TRACE; |
319 | SIZE_T n = internal_strlen(s: src); |
320 | CHECK_UNPOISONED_STRING(src + n, 0); |
321 | char *res = REAL(stpcpy)(dest, src); |
322 | CopyShadowAndOrigin(dst: dest, src, size: n + 1, stack: &stack); |
323 | return res; |
324 | } |
325 | |
326 | INTERCEPTOR(char *, stpncpy, char *dest, const char *src, SIZE_T n) { |
327 | ENSURE_MSAN_INITED(); |
328 | GET_STORE_STACK_TRACE; |
329 | SIZE_T copy_size = Min(a: n, b: internal_strnlen(s: src, maxlen: n) + 1); |
330 | char *res = REAL(stpncpy)(dest, src, n); |
331 | CopyShadowAndOrigin(dst: dest, src, size: copy_size, stack: &stack); |
332 | __msan_unpoison(a: dest + copy_size, size: n - copy_size); |
333 | return res; |
334 | } |
335 | # define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy) |
336 | # define MSAN_MAYBE_INTERCEPT_STPNCPY INTERCEPT_FUNCTION(stpncpy) |
337 | #else |
338 | #define MSAN_MAYBE_INTERCEPT_STPCPY |
339 | # define MSAN_MAYBE_INTERCEPT_STPNCPY |
340 | #endif |
341 | |
342 | INTERCEPTOR(char *, strdup, char *src) { |
343 | ENSURE_MSAN_INITED(); |
344 | GET_STORE_STACK_TRACE; |
345 | // On FreeBSD strdup() leverages strlen(). |
346 | InterceptorScope interceptor_scope; |
347 | SIZE_T n = internal_strlen(s: src); |
348 | CHECK_UNPOISONED_STRING(src + n, 0); |
349 | char *res = REAL(strdup)(src); |
350 | CopyShadowAndOrigin(dst: res, src, size: n + 1, stack: &stack); |
351 | return res; |
352 | } |
353 | |
354 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
355 | INTERCEPTOR(char *, __strdup, char *src) { |
356 | ENSURE_MSAN_INITED(); |
357 | GET_STORE_STACK_TRACE; |
358 | SIZE_T n = internal_strlen(s: src); |
359 | CHECK_UNPOISONED_STRING(src + n, 0); |
360 | char *res = REAL(__strdup)(src); |
361 | CopyShadowAndOrigin(dst: res, src, size: n + 1, stack: &stack); |
362 | return res; |
363 | } |
364 | #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup) |
365 | #else |
366 | #define MSAN_MAYBE_INTERCEPT___STRDUP |
367 | #endif |
368 | |
369 | #if !SANITIZER_NETBSD |
370 | INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) { |
371 | ENSURE_MSAN_INITED(); |
372 | char *res = REAL(gcvt)(number, ndigit, buf); |
373 | SIZE_T n = internal_strlen(s: buf); |
374 | __msan_unpoison(a: buf, size: n + 1); |
375 | return res; |
376 | } |
377 | #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt) |
378 | #else |
379 | #define MSAN_MAYBE_INTERCEPT_GCVT |
380 | #endif |
381 | |
382 | INTERCEPTOR(char *, strcat, char *dest, const char *src) { |
383 | ENSURE_MSAN_INITED(); |
384 | GET_STORE_STACK_TRACE; |
385 | SIZE_T src_size = internal_strlen(s: src); |
386 | SIZE_T dest_size = internal_strlen(s: dest); |
387 | CHECK_UNPOISONED_STRING(src + src_size, 0); |
388 | CHECK_UNPOISONED_STRING(dest + dest_size, 0); |
389 | char *res = REAL(strcat)(dest, src); |
390 | CopyShadowAndOrigin(dst: dest + dest_size, src, size: src_size + 1, stack: &stack); |
391 | return res; |
392 | } |
393 | |
394 | INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { |
395 | ENSURE_MSAN_INITED(); |
396 | GET_STORE_STACK_TRACE; |
397 | SIZE_T dest_size = internal_strlen(s: dest); |
398 | SIZE_T copy_size = internal_strnlen(s: src, maxlen: n); |
399 | CHECK_UNPOISONED_STRING(dest + dest_size, 0); |
400 | char *res = REAL(strncat)(dest, src, n); |
401 | CopyShadowAndOrigin(dst: dest + dest_size, src, size: copy_size, stack: &stack); |
402 | __msan_unpoison(a: dest + dest_size + copy_size, size: 1); // \0 |
403 | return res; |
404 | } |
405 | |
406 | // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to |
407 | // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO. |
408 | #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \ |
409 | ENSURE_MSAN_INITED(); \ |
410 | ret_type res = REAL(func)(__VA_ARGS__); \ |
411 | __msan_unpoison(endptr, sizeof(*endptr)); \ |
412 | return res; |
413 | |
414 | // On s390x, long double return values are passed via implicit reference, |
415 | // which needs to be unpoisoned. We make the implicit pointer explicit. |
416 | #define INTERCEPTOR_STRTO_SRET_BODY(func, sret, ...) \ |
417 | ENSURE_MSAN_INITED(); \ |
418 | REAL(func)(sret, __VA_ARGS__); \ |
419 | __msan_unpoison(sret, sizeof(*sret)); \ |
420 | __msan_unpoison(endptr, sizeof(*endptr)); |
421 | |
422 | #define INTERCEPTOR_STRTO(ret_type, func, char_type) \ |
423 | INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \ |
424 | INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \ |
425 | } |
426 | |
427 | #define INTERCEPTOR_STRTO_SRET(ret_type, func, char_type) \ |
428 | INTERCEPTOR(void, func, ret_type *sret, const char_type *nptr, \ |
429 | char_type **endptr) { \ |
430 | INTERCEPTOR_STRTO_SRET_BODY(func, sret, nptr, endptr); \ |
431 | } |
432 | |
433 | #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \ |
434 | INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ |
435 | int base) { \ |
436 | INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \ |
437 | } |
438 | |
439 | #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \ |
440 | INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ |
441 | void *loc) { \ |
442 | INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \ |
443 | } |
444 | |
445 | #define INTERCEPTOR_STRTO_SRET_LOC(ret_type, func, char_type) \ |
446 | INTERCEPTOR(void, func, ret_type *sret, const char_type *nptr, \ |
447 | char_type **endptr, void *loc) { \ |
448 | INTERCEPTOR_STRTO_SRET_BODY(func, sret, nptr, endptr, loc); \ |
449 | } |
450 | |
451 | #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \ |
452 | INTERCEPTOR(ret_type, func, const char_type * |
---|