1e8d8bef9SDimitry Andric //===-- FuzzerInterceptors.cpp --------------------------------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric // Intercept certain libc functions to aid fuzzing.
9e8d8bef9SDimitry Andric // Linked only when other RTs that define their own interceptors are not linked.
10e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
11e8d8bef9SDimitry Andric
12e8d8bef9SDimitry Andric #include "FuzzerPlatform.h"
13e8d8bef9SDimitry Andric
14e8d8bef9SDimitry Andric #if LIBFUZZER_LINUX
15e8d8bef9SDimitry Andric
16e8d8bef9SDimitry Andric #define GET_CALLER_PC() __builtin_return_address(0)
17e8d8bef9SDimitry Andric
18e8d8bef9SDimitry Andric #define PTR_TO_REAL(x) real_##x
19e8d8bef9SDimitry Andric #define REAL(x) __interception::PTR_TO_REAL(x)
20e8d8bef9SDimitry Andric #define FUNC_TYPE(x) x##_type
21e8d8bef9SDimitry Andric #define DEFINE_REAL(ret_type, func, ...) \
22e8d8bef9SDimitry Andric typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
23e8d8bef9SDimitry Andric namespace __interception { \
24e8d8bef9SDimitry Andric FUNC_TYPE(func) PTR_TO_REAL(func); \
25e8d8bef9SDimitry Andric }
26e8d8bef9SDimitry Andric
27e8d8bef9SDimitry Andric #include <cassert>
28*fe6060f1SDimitry Andric #include <cstddef> // for size_t
29e8d8bef9SDimitry Andric #include <cstdint>
30e8d8bef9SDimitry Andric #include <dlfcn.h> // for dlsym()
31e8d8bef9SDimitry Andric
getFuncAddr(const char * name,uintptr_t wrapper_addr)32e8d8bef9SDimitry Andric static void *getFuncAddr(const char *name, uintptr_t wrapper_addr) {
33e8d8bef9SDimitry Andric void *addr = dlsym(RTLD_NEXT, name);
34e8d8bef9SDimitry Andric if (!addr) {
35e8d8bef9SDimitry Andric // If the lookup using RTLD_NEXT failed, the sanitizer runtime library is
36e8d8bef9SDimitry Andric // later in the library search order than the DSO that we are trying to
37e8d8bef9SDimitry Andric // intercept, which means that we cannot intercept this function. We still
38e8d8bef9SDimitry Andric // want the address of the real definition, though, so look it up using
39e8d8bef9SDimitry Andric // RTLD_DEFAULT.
40e8d8bef9SDimitry Andric addr = dlsym(RTLD_DEFAULT, name);
41e8d8bef9SDimitry Andric
42e8d8bef9SDimitry Andric // In case `name' is not loaded, dlsym ends up finding the actual wrapper.
43e8d8bef9SDimitry Andric // We don't want to intercept the wrapper and have it point to itself.
44e8d8bef9SDimitry Andric if (reinterpret_cast<uintptr_t>(addr) == wrapper_addr)
45e8d8bef9SDimitry Andric addr = nullptr;
46e8d8bef9SDimitry Andric }
47e8d8bef9SDimitry Andric return addr;
48e8d8bef9SDimitry Andric }
49e8d8bef9SDimitry Andric
50e8d8bef9SDimitry Andric static int FuzzerInited = 0;
51e8d8bef9SDimitry Andric static bool FuzzerInitIsRunning;
52e8d8bef9SDimitry Andric
53e8d8bef9SDimitry Andric static void fuzzerInit();
54e8d8bef9SDimitry Andric
ensureFuzzerInited()55e8d8bef9SDimitry Andric static void ensureFuzzerInited() {
56e8d8bef9SDimitry Andric assert(!FuzzerInitIsRunning);
57e8d8bef9SDimitry Andric if (!FuzzerInited) {
58e8d8bef9SDimitry Andric fuzzerInit();
59e8d8bef9SDimitry Andric }
60e8d8bef9SDimitry Andric }
61e8d8bef9SDimitry Andric
internal_strcmp_strncmp(const char * s1,const char * s2,bool strncmp,size_t n)62e8d8bef9SDimitry Andric static int internal_strcmp_strncmp(const char *s1, const char *s2, bool strncmp,
63e8d8bef9SDimitry Andric size_t n) {
64e8d8bef9SDimitry Andric size_t i = 0;
65e8d8bef9SDimitry Andric while (true) {
66e8d8bef9SDimitry Andric if (strncmp) {
67e8d8bef9SDimitry Andric if (i == n)
68e8d8bef9SDimitry Andric break;
69e8d8bef9SDimitry Andric i++;
70e8d8bef9SDimitry Andric }
71e8d8bef9SDimitry Andric unsigned c1 = *s1;
72e8d8bef9SDimitry Andric unsigned c2 = *s2;
73e8d8bef9SDimitry Andric if (c1 != c2)
74e8d8bef9SDimitry Andric return (c1 < c2) ? -1 : 1;
75e8d8bef9SDimitry Andric if (c1 == 0)
76e8d8bef9SDimitry Andric break;
77e8d8bef9SDimitry Andric s1++;
78e8d8bef9SDimitry Andric s2++;
79e8d8bef9SDimitry Andric }
80e8d8bef9SDimitry Andric return 0;
81e8d8bef9SDimitry Andric }
82e8d8bef9SDimitry Andric
internal_strncmp(const char * s1,const char * s2,size_t n)83e8d8bef9SDimitry Andric static int internal_strncmp(const char *s1, const char *s2, size_t n) {
84e8d8bef9SDimitry Andric return internal_strcmp_strncmp(s1, s2, true, n);
85e8d8bef9SDimitry Andric }
86e8d8bef9SDimitry Andric
internal_strcmp(const char * s1,const char * s2)87e8d8bef9SDimitry Andric static int internal_strcmp(const char *s1, const char *s2) {
88e8d8bef9SDimitry Andric return internal_strcmp_strncmp(s1, s2, false, 0);
89e8d8bef9SDimitry Andric }
90e8d8bef9SDimitry Andric
internal_memcmp(const void * s1,const void * s2,size_t n)91e8d8bef9SDimitry Andric static int internal_memcmp(const void *s1, const void *s2, size_t n) {
92e8d8bef9SDimitry Andric const uint8_t *t1 = static_cast<const uint8_t *>(s1);
93e8d8bef9SDimitry Andric const uint8_t *t2 = static_cast<const uint8_t *>(s2);
94e8d8bef9SDimitry Andric for (size_t i = 0; i < n; ++i, ++t1, ++t2)
95e8d8bef9SDimitry Andric if (*t1 != *t2)
96e8d8bef9SDimitry Andric return *t1 < *t2 ? -1 : 1;
97e8d8bef9SDimitry Andric return 0;
98e8d8bef9SDimitry Andric }
99e8d8bef9SDimitry Andric
internal_strlen(const char * s)100e8d8bef9SDimitry Andric static size_t internal_strlen(const char *s) {
101e8d8bef9SDimitry Andric size_t i = 0;
102e8d8bef9SDimitry Andric while (s[i])
103e8d8bef9SDimitry Andric i++;
104e8d8bef9SDimitry Andric return i;
105e8d8bef9SDimitry Andric }
106e8d8bef9SDimitry Andric
internal_strstr(const char * haystack,const char * needle)107e8d8bef9SDimitry Andric static char *internal_strstr(const char *haystack, const char *needle) {
108e8d8bef9SDimitry Andric // This is O(N^2), but we are not using it in hot places.
109e8d8bef9SDimitry Andric size_t len1 = internal_strlen(haystack);
110e8d8bef9SDimitry Andric size_t len2 = internal_strlen(needle);
111e8d8bef9SDimitry Andric if (len1 < len2)
112e8d8bef9SDimitry Andric return nullptr;
113e8d8bef9SDimitry Andric for (size_t pos = 0; pos <= len1 - len2; pos++) {
114e8d8bef9SDimitry Andric if (internal_memcmp(haystack + pos, needle, len2) == 0)
115e8d8bef9SDimitry Andric return const_cast<char *>(haystack) + pos;
116e8d8bef9SDimitry Andric }
117e8d8bef9SDimitry Andric return nullptr;
118e8d8bef9SDimitry Andric }
119e8d8bef9SDimitry Andric
120e8d8bef9SDimitry Andric extern "C" {
121e8d8bef9SDimitry Andric
122e8d8bef9SDimitry Andric // Weak hooks forward-declared to avoid dependency on
123e8d8bef9SDimitry Andric // <sanitizer/common_interface_defs.h>.
124e8d8bef9SDimitry Andric void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
125e8d8bef9SDimitry Andric const void *s2, size_t n, int result);
126e8d8bef9SDimitry Andric void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
127e8d8bef9SDimitry Andric const char *s2, size_t n, int result);
128e8d8bef9SDimitry Andric void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
129e8d8bef9SDimitry Andric const char *s2, size_t n, int result);
130e8d8bef9SDimitry Andric void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
131e8d8bef9SDimitry Andric const char *s2, int result);
132e8d8bef9SDimitry Andric void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
133e8d8bef9SDimitry Andric const char *s2, int result);
134e8d8bef9SDimitry Andric void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
135e8d8bef9SDimitry Andric const char *s2, char *result);
136e8d8bef9SDimitry Andric void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
137e8d8bef9SDimitry Andric const char *s2, char *result);
138e8d8bef9SDimitry Andric void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
139e8d8bef9SDimitry Andric const void *s2, size_t len2, void *result);
140e8d8bef9SDimitry Andric
DEFINE_REAL(int,bcmp,const void *,const void *,size_t)141e8d8bef9SDimitry Andric DEFINE_REAL(int, bcmp, const void *, const void *, size_t)
142e8d8bef9SDimitry Andric DEFINE_REAL(int, memcmp, const void *, const void *, size_t)
143e8d8bef9SDimitry Andric DEFINE_REAL(int, strncmp, const char *, const char *, size_t)
144e8d8bef9SDimitry Andric DEFINE_REAL(int, strcmp, const char *, const char *)
145e8d8bef9SDimitry Andric DEFINE_REAL(int, strncasecmp, const char *, const char *, size_t)
146e8d8bef9SDimitry Andric DEFINE_REAL(int, strcasecmp, const char *, const char *)
147e8d8bef9SDimitry Andric DEFINE_REAL(char *, strstr, const char *, const char *)
148e8d8bef9SDimitry Andric DEFINE_REAL(char *, strcasestr, const char *, const char *)
149e8d8bef9SDimitry Andric DEFINE_REAL(void *, memmem, const void *, size_t, const void *, size_t)
150e8d8bef9SDimitry Andric
151e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE int bcmp(const char *s1, const char *s2, size_t n) {
152e8d8bef9SDimitry Andric if (!FuzzerInited)
153e8d8bef9SDimitry Andric return internal_memcmp(s1, s2, n);
154e8d8bef9SDimitry Andric int result = REAL(bcmp)(s1, s2, n);
155e8d8bef9SDimitry Andric __sanitizer_weak_hook_memcmp(GET_CALLER_PC(), s1, s2, n, result);
156e8d8bef9SDimitry Andric return result;
157e8d8bef9SDimitry Andric }
158e8d8bef9SDimitry Andric
memcmp(const void * s1,const void * s2,size_t n)159e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE int memcmp(const void *s1, const void *s2, size_t n) {
160e8d8bef9SDimitry Andric if (!FuzzerInited)
161e8d8bef9SDimitry Andric return internal_memcmp(s1, s2, n);
162e8d8bef9SDimitry Andric int result = REAL(memcmp)(s1, s2, n);
163e8d8bef9SDimitry Andric __sanitizer_weak_hook_memcmp(GET_CALLER_PC(), s1, s2, n, result);
164e8d8bef9SDimitry Andric return result;
165e8d8bef9SDimitry Andric }
166e8d8bef9SDimitry Andric
strncmp(const char * s1,const char * s2,size_t n)167e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE int strncmp(const char *s1, const char *s2, size_t n) {
168e8d8bef9SDimitry Andric if (!FuzzerInited)
169e8d8bef9SDimitry Andric return internal_strncmp(s1, s2, n);
170e8d8bef9SDimitry Andric int result = REAL(strncmp)(s1, s2, n);
171e8d8bef9SDimitry Andric __sanitizer_weak_hook_strncmp(GET_CALLER_PC(), s1, s2, n, result);
172e8d8bef9SDimitry Andric return result;
173e8d8bef9SDimitry Andric }
174e8d8bef9SDimitry Andric
strcmp(const char * s1,const char * s2)175e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE int strcmp(const char *s1, const char *s2) {
176e8d8bef9SDimitry Andric if (!FuzzerInited)
177e8d8bef9SDimitry Andric return internal_strcmp(s1, s2);
178e8d8bef9SDimitry Andric int result = REAL(strcmp)(s1, s2);
179e8d8bef9SDimitry Andric __sanitizer_weak_hook_strcmp(GET_CALLER_PC(), s1, s2, result);
180e8d8bef9SDimitry Andric return result;
181e8d8bef9SDimitry Andric }
182e8d8bef9SDimitry Andric
strncasecmp(const char * s1,const char * s2,size_t n)183e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE int strncasecmp(const char *s1, const char *s2, size_t n) {
184e8d8bef9SDimitry Andric ensureFuzzerInited();
185e8d8bef9SDimitry Andric int result = REAL(strncasecmp)(s1, s2, n);
186e8d8bef9SDimitry Andric __sanitizer_weak_hook_strncasecmp(GET_CALLER_PC(), s1, s2, n, result);
187e8d8bef9SDimitry Andric return result;
188e8d8bef9SDimitry Andric }
189e8d8bef9SDimitry Andric
strcasecmp(const char * s1,const char * s2)190e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE int strcasecmp(const char *s1, const char *s2) {
191e8d8bef9SDimitry Andric ensureFuzzerInited();
192e8d8bef9SDimitry Andric int result = REAL(strcasecmp)(s1, s2);
193e8d8bef9SDimitry Andric __sanitizer_weak_hook_strcasecmp(GET_CALLER_PC(), s1, s2, result);
194e8d8bef9SDimitry Andric return result;
195e8d8bef9SDimitry Andric }
196e8d8bef9SDimitry Andric
strstr(const char * s1,const char * s2)197e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE char *strstr(const char *s1, const char *s2) {
198e8d8bef9SDimitry Andric if (!FuzzerInited)
199e8d8bef9SDimitry Andric return internal_strstr(s1, s2);
200e8d8bef9SDimitry Andric char *result = REAL(strstr)(s1, s2);
201e8d8bef9SDimitry Andric __sanitizer_weak_hook_strstr(GET_CALLER_PC(), s1, s2, result);
202e8d8bef9SDimitry Andric return result;
203e8d8bef9SDimitry Andric }
204e8d8bef9SDimitry Andric
strcasestr(const char * s1,const char * s2)205e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE char *strcasestr(const char *s1, const char *s2) {
206e8d8bef9SDimitry Andric ensureFuzzerInited();
207e8d8bef9SDimitry Andric char *result = REAL(strcasestr)(s1, s2);
208e8d8bef9SDimitry Andric __sanitizer_weak_hook_strcasestr(GET_CALLER_PC(), s1, s2, result);
209e8d8bef9SDimitry Andric return result;
210e8d8bef9SDimitry Andric }
211e8d8bef9SDimitry Andric
212e8d8bef9SDimitry Andric ATTRIBUTE_INTERFACE
memmem(const void * s1,size_t len1,const void * s2,size_t len2)213e8d8bef9SDimitry Andric void *memmem(const void *s1, size_t len1, const void *s2, size_t len2) {
214e8d8bef9SDimitry Andric ensureFuzzerInited();
215e8d8bef9SDimitry Andric void *result = REAL(memmem)(s1, len1, s2, len2);
216e8d8bef9SDimitry Andric __sanitizer_weak_hook_memmem(GET_CALLER_PC(), s1, len1, s2, len2, result);
217e8d8bef9SDimitry Andric return result;
218e8d8bef9SDimitry Andric }
219e8d8bef9SDimitry Andric
220e8d8bef9SDimitry Andric __attribute__((section(".preinit_array"),
221e8d8bef9SDimitry Andric used)) static void (*__local_fuzzer_preinit)(void) = fuzzerInit;
222e8d8bef9SDimitry Andric
223e8d8bef9SDimitry Andric } // extern "C"
224e8d8bef9SDimitry Andric
fuzzerInit()225e8d8bef9SDimitry Andric static void fuzzerInit() {
226e8d8bef9SDimitry Andric assert(!FuzzerInitIsRunning);
227e8d8bef9SDimitry Andric if (FuzzerInited)
228e8d8bef9SDimitry Andric return;
229e8d8bef9SDimitry Andric FuzzerInitIsRunning = true;
230e8d8bef9SDimitry Andric
231e8d8bef9SDimitry Andric REAL(bcmp) = reinterpret_cast<memcmp_type>(
232e8d8bef9SDimitry Andric getFuncAddr("bcmp", reinterpret_cast<uintptr_t>(&bcmp)));
233e8d8bef9SDimitry Andric REAL(memcmp) = reinterpret_cast<memcmp_type>(
234e8d8bef9SDimitry Andric getFuncAddr("memcmp", reinterpret_cast<uintptr_t>(&memcmp)));
235e8d8bef9SDimitry Andric REAL(strncmp) = reinterpret_cast<strncmp_type>(
236e8d8bef9SDimitry Andric getFuncAddr("strncmp", reinterpret_cast<uintptr_t>(&strncmp)));
237e8d8bef9SDimitry Andric REAL(strcmp) = reinterpret_cast<strcmp_type>(
238e8d8bef9SDimitry Andric getFuncAddr("strcmp", reinterpret_cast<uintptr_t>(&strcmp)));
239e8d8bef9SDimitry Andric REAL(strncasecmp) = reinterpret_cast<strncasecmp_type>(
240e8d8bef9SDimitry Andric getFuncAddr("strncasecmp", reinterpret_cast<uintptr_t>(&strncasecmp)));
241e8d8bef9SDimitry Andric REAL(strcasecmp) = reinterpret_cast<strcasecmp_type>(
242e8d8bef9SDimitry Andric getFuncAddr("strcasecmp", reinterpret_cast<uintptr_t>(&strcasecmp)));
243e8d8bef9SDimitry Andric REAL(strstr) = reinterpret_cast<strstr_type>(
244e8d8bef9SDimitry Andric getFuncAddr("strstr", reinterpret_cast<uintptr_t>(&strstr)));
245e8d8bef9SDimitry Andric REAL(strcasestr) = reinterpret_cast<strcasestr_type>(
246e8d8bef9SDimitry Andric getFuncAddr("strcasestr", reinterpret_cast<uintptr_t>(&strcasestr)));
247e8d8bef9SDimitry Andric REAL(memmem) = reinterpret_cast<memmem_type>(
248e8d8bef9SDimitry Andric getFuncAddr("memmem", reinterpret_cast<uintptr_t>(&memmem)));
249e8d8bef9SDimitry Andric
250e8d8bef9SDimitry Andric FuzzerInitIsRunning = false;
251e8d8bef9SDimitry Andric FuzzerInited = 1;
252e8d8bef9SDimitry Andric }
253e8d8bef9SDimitry Andric
254e8d8bef9SDimitry Andric #endif
255