1 //===-- dfsan.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 DataFlowSanitizer.
10 //
11 // This file defines the custom functions listed in done_abilist.txt.
12 //===----------------------------------------------------------------------===//
13
14 #include <arpa/inet.h>
15 #include <assert.h>
16 #include <ctype.h>
17 #include <dlfcn.h>
18 #include <link.h>
19 #include <poll.h>
20 #include <pthread.h>
21 #include <pwd.h>
22 #include <sched.h>
23 #include <signal.h>
24 #include <stdarg.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/epoll.h>
30 #include <sys/resource.h>
31 #include <sys/select.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <time.h>
36 #include <unistd.h>
37
38 #include "dfsan/dfsan.h"
39 #include "sanitizer_common/sanitizer_common.h"
40 #include "sanitizer_common/sanitizer_internal_defs.h"
41 #include "sanitizer_common/sanitizer_linux.h"
42
43 using namespace __dfsan;
44
45 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) \
46 do { \
47 if (f) \
48 f(__VA_ARGS__); \
49 } while (false)
50 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
51 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__);
52
53 extern "C" {
54 SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_stat(const char * path,struct stat * buf,dfsan_label path_label,dfsan_label buf_label,dfsan_label * ret_label)55 __dfsw_stat(const char *path, struct stat *buf, dfsan_label path_label,
56 dfsan_label buf_label, dfsan_label *ret_label) {
57 int ret = stat(path, buf);
58 if (ret == 0)
59 dfsan_set_label(0, buf, sizeof(struct stat));
60 *ret_label = 0;
61 return ret;
62 }
63
__dfsw_fstat(int fd,struct stat * buf,dfsan_label fd_label,dfsan_label buf_label,dfsan_label * ret_label)64 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_fstat(int fd, struct stat *buf,
65 dfsan_label fd_label,
66 dfsan_label buf_label,
67 dfsan_label *ret_label) {
68 int ret = fstat(fd, buf);
69 if (ret == 0)
70 dfsan_set_label(0, buf, sizeof(struct stat));
71 *ret_label = 0;
72 return ret;
73 }
74
__dfsw_strchr(const char * s,int c,dfsan_label s_label,dfsan_label c_label,dfsan_label * ret_label)75 SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strchr(const char *s, int c,
76 dfsan_label s_label,
77 dfsan_label c_label,
78 dfsan_label *ret_label) {
79 for (size_t i = 0;; ++i) {
80 if (s[i] == c || s[i] == 0) {
81 if (flags().strict_data_dependencies) {
82 *ret_label = s_label;
83 } else {
84 *ret_label = dfsan_union(dfsan_read_label(s, i + 1),
85 dfsan_union(s_label, c_label));
86 }
87
88 // If s[i] is the \0 at the end of the string, and \0 is not the
89 // character we are searching for, then return null.
90 if (s[i] == 0 && c != 0) {
91 return nullptr;
92 }
93 return const_cast<char *>(s + i);
94 }
95 }
96 }
97
__dfsw_strpbrk(const char * s,const char * accept,dfsan_label s_label,dfsan_label accept_label,dfsan_label * ret_label)98 SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strpbrk(const char *s,
99 const char *accept,
100 dfsan_label s_label,
101 dfsan_label accept_label,
102 dfsan_label *ret_label) {
103 const char *ret = strpbrk(s, accept);
104 if (flags().strict_data_dependencies) {
105 *ret_label = ret ? s_label : 0;
106 } else {
107 size_t s_bytes_read = (ret ? ret - s : strlen(s)) + 1;
108 *ret_label =
109 dfsan_union(dfsan_read_label(s, s_bytes_read),
110 dfsan_union(dfsan_read_label(accept, strlen(accept) + 1),
111 dfsan_union(s_label, accept_label)));
112 }
113 return const_cast<char *>(ret);
114 }
115
dfsan_memcmp_bcmp(const void * s1,const void * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label,dfsan_label * ret_label)116 static int dfsan_memcmp_bcmp(const void *s1, const void *s2, size_t n,
117 dfsan_label s1_label, dfsan_label s2_label,
118 dfsan_label n_label, dfsan_label *ret_label) {
119 const char *cs1 = (const char *) s1, *cs2 = (const char *) s2;
120 for (size_t i = 0; i != n; ++i) {
121 if (cs1[i] != cs2[i]) {
122 if (flags().strict_data_dependencies) {
123 *ret_label = 0;
124 } else {
125 *ret_label = dfsan_union(dfsan_read_label(cs1, i + 1),
126 dfsan_read_label(cs2, i + 1));
127 }
128 return cs1[i] - cs2[i];
129 }
130 }
131
132 if (flags().strict_data_dependencies) {
133 *ret_label = 0;
134 } else {
135 *ret_label = dfsan_union(dfsan_read_label(cs1, n),
136 dfsan_read_label(cs2, n));
137 }
138 return 0;
139 }
140
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp,uptr caller_pc,const void * s1,const void * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label)141 DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, uptr caller_pc,
142 const void *s1, const void *s2, size_t n,
143 dfsan_label s1_label, dfsan_label s2_label,
144 dfsan_label n_label)
145
146 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_memcmp(const void *s1, const void *s2,
147 size_t n, dfsan_label s1_label,
148 dfsan_label s2_label,
149 dfsan_label n_label,
150 dfsan_label *ret_label) {
151 CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, GET_CALLER_PC(), s1, s2, n,
152 s1_label, s2_label, n_label);
153 return dfsan_memcmp_bcmp(s1, s2, n, s1_label, s2_label, n_label, ret_label);
154 }
155
__dfsw_bcmp(const void * s1,const void * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label,dfsan_label * ret_label)156 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_bcmp(const void *s1, const void *s2,
157 size_t n, dfsan_label s1_label,
158 dfsan_label s2_label,
159 dfsan_label n_label,
160 dfsan_label *ret_label) {
161 return dfsan_memcmp_bcmp(s1, s2, n, s1_label, s2_label, n_label, ret_label);
162 }
163
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp,uptr caller_pc,const char * s1,const char * s2,dfsan_label s1_label,dfsan_label s2_label)164 DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, uptr caller_pc,
165 const char *s1, const char *s2,
166 dfsan_label s1_label, dfsan_label s2_label)
167
168 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strcmp(const char *s1, const char *s2,
169 dfsan_label s1_label,
170 dfsan_label s2_label,
171 dfsan_label *ret_label) {
172 CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, GET_CALLER_PC(), s1, s2,
173 s1_label, s2_label);
174 for (size_t i = 0;; ++i) {
175 if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0) {
176 if (flags().strict_data_dependencies) {
177 *ret_label = 0;
178 } else {
179 *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
180 dfsan_read_label(s2, i + 1));
181 }
182 return s1[i] - s2[i];
183 }
184 }
185 return 0;
186 }
187
188 SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_strcasecmp(const char * s1,const char * s2,dfsan_label s1_label,dfsan_label s2_label,dfsan_label * ret_label)189 __dfsw_strcasecmp(const char *s1, const char *s2, dfsan_label s1_label,
190 dfsan_label s2_label, dfsan_label *ret_label) {
191 for (size_t i = 0;; ++i) {
192 char s1_lower = tolower(s1[i]);
193 char s2_lower = tolower(s2[i]);
194
195 if (s1_lower != s2_lower || s1[i] == 0 || s2[i] == 0) {
196 if (flags().strict_data_dependencies) {
197 *ret_label = 0;
198 } else {
199 *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
200 dfsan_read_label(s2, i + 1));
201 }
202 return s1_lower - s2_lower;
203 }
204 }
205 return 0;
206 }
207
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp,uptr caller_pc,const char * s1,const char * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label)208 DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp, uptr caller_pc,
209 const char *s1, const char *s2, size_t n,
210 dfsan_label s1_label, dfsan_label s2_label,
211 dfsan_label n_label)
212
213 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strncmp(const char *s1, const char *s2,
214 size_t n, dfsan_label s1_label,
215 dfsan_label s2_label,
216 dfsan_label n_label,
217 dfsan_label *ret_label) {
218 if (n == 0) {
219 *ret_label = 0;
220 return 0;
221 }
222
223 CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp, GET_CALLER_PC(), s1, s2,
224 n, s1_label, s2_label, n_label);
225
226 for (size_t i = 0;; ++i) {
227 if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0 || i == n - 1) {
228 if (flags().strict_data_dependencies) {
229 *ret_label = 0;
230 } else {
231 *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
232 dfsan_read_label(s2, i + 1));
233 }
234 return s1[i] - s2[i];
235 }
236 }
237 return 0;
238 }
239
240 SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_strncasecmp(const char * s1,const char * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label,dfsan_label * ret_label)241 __dfsw_strncasecmp(const char *s1, const char *s2, size_t n,
242 dfsan_label s1_label, dfsan_label s2_label,
243 dfsan_label n_label, dfsan_label *ret_label) {
244 if (n == 0) {
245 *ret_label = 0;
246 return 0;
247 }
248
249 for (size_t i = 0;; ++i) {
250 char s1_lower = tolower(s1[i]);
251 char s2_lower = tolower(s2[i]);
252
253 if (s1_lower != s2_lower || s1[i] == 0 || s2[i] == 0 || i == n - 1) {
254 if (flags().strict_data_dependencies) {
255 *ret_label = 0;
256 } else {
257 *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
258 dfsan_read_label(s2, i + 1));
259 }
260 return s1_lower - s2_lower;
261 }
262 }
263 return 0;
264 }
265
__dfsw_calloc(size_t nmemb,size_t size,dfsan_label nmemb_label,dfsan_label size_label,dfsan_label * ret_label)266 SANITIZER_INTERFACE_ATTRIBUTE void *__dfsw_calloc(size_t nmemb, size_t size,
267 dfsan_label nmemb_label,
268 dfsan_label size_label,
269 dfsan_label *ret_label) {
270 void *p = calloc(nmemb, size);
271 dfsan_set_label(0, p, nmemb * size);
272 *ret_label = 0;
273 return p;
274 }
275
276 SANITIZER_INTERFACE_ATTRIBUTE size_t
__dfsw_strlen(const char * s,dfsan_label s_label,dfsan_label * ret_label)277 __dfsw_strlen(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
278 size_t ret = strlen(s);
279 if (flags().strict_data_dependencies) {
280 *ret_label = 0;
281 } else {
282 *ret_label = dfsan_read_label(s, ret + 1);
283 }
284 return ret;
285 }
286
287
dfsan_memcpy(void * dest,const void * src,size_t n)288 static void *dfsan_memcpy(void *dest, const void *src, size_t n) {
289 dfsan_label *sdest = shadow_for(dest);
290 const dfsan_label *ssrc = shadow_for(src);
291 internal_memcpy((void *)sdest, (const void *)ssrc, n * sizeof(dfsan_label));
292 return internal_memcpy(dest, src, n);
293 }
294
dfsan_memset(void * s,int c,dfsan_label c_label,size_t n)295 static void dfsan_memset(void *s, int c, dfsan_label c_label, size_t n) {
296 internal_memset(s, c, n);
297 dfsan_set_label(c_label, s, n);
298 }
299
300 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_memcpy(void * dest,const void * src,size_t n,dfsan_label dest_label,dfsan_label src_label,dfsan_label n_label,dfsan_label * ret_label)301 void *__dfsw_memcpy(void *dest, const void *src, size_t n,
302 dfsan_label dest_label, dfsan_label src_label,
303 dfsan_label n_label, dfsan_label *ret_label) {
304 *ret_label = dest_label;
305 return dfsan_memcpy(dest, src, n);
306 }
307
308 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_memset(void * s,int c,size_t n,dfsan_label s_label,dfsan_label c_label,dfsan_label n_label,dfsan_label * ret_label)309 void *__dfsw_memset(void *s, int c, size_t n,
310 dfsan_label s_label, dfsan_label c_label,
311 dfsan_label n_label, dfsan_label *ret_label) {
312 dfsan_memset(s, c, c_label, n);
313 *ret_label = s_label;
314 return s;
315 }
316
317 SANITIZER_INTERFACE_ATTRIBUTE char *
__dfsw_strdup(const char * s,dfsan_label s_label,dfsan_label * ret_label)318 __dfsw_strdup(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
319 size_t len = strlen(s);
320 void *p = malloc(len+1);
321 dfsan_memcpy(p, s, len+1);
322 *ret_label = 0;
323 return static_cast<char *>(p);
324 }
325
326 SANITIZER_INTERFACE_ATTRIBUTE char *
__dfsw_strncpy(char * s1,const char * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label,dfsan_label * ret_label)327 __dfsw_strncpy(char *s1, const char *s2, size_t n, dfsan_label s1_label,
328 dfsan_label s2_label, dfsan_label n_label,
329 dfsan_label *ret_label) {
330 size_t len = strlen(s2);
331 if (len < n) {
332 dfsan_memcpy(s1, s2, len+1);
333 dfsan_memset(s1+len+1, 0, 0, n-len-1);
334 } else {
335 dfsan_memcpy(s1, s2, n);
336 }
337
338 *ret_label = s1_label;
339 return s1;
340 }
341
342 SANITIZER_INTERFACE_ATTRIBUTE ssize_t
__dfsw_pread(int fd,void * buf,size_t count,off_t offset,dfsan_label fd_label,dfsan_label buf_label,dfsan_label count_label,dfsan_label offset_label,dfsan_label * ret_label)343 __dfsw_pread(int fd, void *buf, size_t count, off_t offset,
344 dfsan_label fd_label, dfsan_label buf_label,
345 dfsan_label count_label, dfsan_label offset_label,
346 dfsan_label *ret_label) {
347 ssize_t ret = pread(fd, buf, count, offset);
348 if (ret > 0)
349 dfsan_set_label(0, buf, ret);
350 *ret_label = 0;
351 return ret;
352 }
353
354 SANITIZER_INTERFACE_ATTRIBUTE ssize_t
__dfsw_read(int fd,void * buf,size_t count,dfsan_label fd_label,dfsan_label buf_label,dfsan_label count_label,dfsan_label * ret_label)355 __dfsw_read(int fd, void *buf, size_t count,
356 dfsan_label fd_label, dfsan_label buf_label,
357 dfsan_label count_label,
358 dfsan_label *ret_label) {
359 ssize_t ret = read(fd, buf, count);
360 if (ret > 0)
361 dfsan_set_label(0, buf, ret);
362 *ret_label = 0;
363 return ret;
364 }
365
__dfsw_clock_gettime(clockid_t clk_id,struct timespec * tp,dfsan_label clk_id_label,dfsan_label tp_label,dfsan_label * ret_label)366 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_clock_gettime(clockid_t clk_id,
367 struct timespec *tp,
368 dfsan_label clk_id_label,
369 dfsan_label tp_label,
370 dfsan_label *ret_label) {
371 int ret = clock_gettime(clk_id, tp);
372 if (ret == 0)
373 dfsan_set_label(0, tp, sizeof(struct timespec));
374 *ret_label = 0;
375 return ret;
376 }
377
unpoison(const void * ptr,uptr size)378 static void unpoison(const void *ptr, uptr size) {
379 dfsan_set_label(0, const_cast<void *>(ptr), size);
380 }
381
382 // dlopen() ultimately calls mmap() down inside the loader, which generally
383 // doesn't participate in dynamic symbol resolution. Therefore we won't
384 // intercept its calls to mmap, and we have to hook it here.
385 SANITIZER_INTERFACE_ATTRIBUTE void *
__dfsw_dlopen(const char * filename,int flag,dfsan_label filename_label,dfsan_label flag_label,dfsan_label * ret_label)386 __dfsw_dlopen(const char *filename, int flag, dfsan_label filename_label,
387 dfsan_label flag_label, dfsan_label *ret_label) {
388 void *handle = dlopen(filename, flag);
389 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE(handle);
390 if (map)
391 ForEachMappedRegion(map, unpoison);
392 *ret_label = 0;
393 return handle;
394 }
395
396 struct pthread_create_info {
397 void *(*start_routine_trampoline)(void *, void *, dfsan_label, dfsan_label *);
398 void *start_routine;
399 void *arg;
400 };
401
pthread_create_cb(void * p)402 static void *pthread_create_cb(void *p) {
403 pthread_create_info pci(*(pthread_create_info *)p);
404 free(p);
405 dfsan_label ret_label;
406 return pci.start_routine_trampoline(pci.start_routine, pci.arg, 0,
407 &ret_label);
408 }
409
__dfsw_pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* start_routine_trampoline)(void *,void *,dfsan_label,dfsan_label *),void * start_routine,void * arg,dfsan_label thread_label,dfsan_label attr_label,dfsan_label start_routine_label,dfsan_label arg_label,dfsan_label * ret_label)410 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_pthread_create(
411 pthread_t *thread, const pthread_attr_t *attr,
412 void *(*start_routine_trampoline)(void *, void *, dfsan_label,
413 dfsan_label *),
414 void *start_routine, void *arg, dfsan_label thread_label,
415 dfsan_label attr_label, dfsan_label start_routine_label,
416 dfsan_label arg_label, dfsan_label *ret_label) {
417 pthread_create_info *pci =
418 (pthread_create_info *)malloc(sizeof(pthread_create_info));
419 pci->start_routine_trampoline = start_routine_trampoline;
420 pci->start_routine = start_routine;
421 pci->arg = arg;
422 int rv = pthread_create(thread, attr, pthread_create_cb, (void *)pci);
423 if (rv != 0)
424 free(pci);
425 *ret_label = 0;
426 return rv;
427 }
428
429 struct dl_iterate_phdr_info {
430 int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
431 size_t size, void *data, dfsan_label info_label,
432 dfsan_label size_label, dfsan_label data_label,
433 dfsan_label *ret_label);
434 void *callback;
435 void *data;
436 };
437
dl_iterate_phdr_cb(struct dl_phdr_info * info,size_t size,void * data)438 int dl_iterate_phdr_cb(struct dl_phdr_info *info, size_t size, void *data) {
439 dl_iterate_phdr_info *dipi = (dl_iterate_phdr_info *)data;
440 dfsan_set_label(0, *info);
441 dfsan_set_label(0, const_cast<char *>(info->dlpi_name),
442 strlen(info->dlpi_name) + 1);
443 dfsan_set_label(
444 0, const_cast<char *>(reinterpret_cast<const char *>(info->dlpi_phdr)),
445 sizeof(*info->dlpi_phdr) * info->dlpi_phnum);
446 dfsan_label ret_label;
447 return dipi->callback_trampoline(dipi->callback, info, size, dipi->data, 0, 0,
448 0, &ret_label);
449 }
450
__dfsw_dl_iterate_phdr(int (* callback_trampoline)(void * callback,struct dl_phdr_info * info,size_t size,void * data,dfsan_label info_label,dfsan_label size_label,dfsan_label data_label,dfsan_label * ret_label),void * callback,void * data,dfsan_label callback_label,dfsan_label data_label,dfsan_label * ret_label)451 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_dl_iterate_phdr(
452 int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
453 size_t size, void *data, dfsan_label info_label,
454 dfsan_label size_label, dfsan_label data_label,
455 dfsan_label *ret_label),
456 void *callback, void *data, dfsan_label callback_label,
457 dfsan_label data_label, dfsan_label *ret_label) {
458 dl_iterate_phdr_info dipi = { callback_trampoline, callback, data };
459 *ret_label = 0;
460 return dl_iterate_phdr(dl_iterate_phdr_cb, &dipi);
461 }
462
463 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_ctime_r(const time_t * timep,char * buf,dfsan_label timep_label,dfsan_label buf_label,dfsan_label * ret_label)464 char *__dfsw_ctime_r(const time_t *timep, char *buf, dfsan_label timep_label,
465 dfsan_label buf_label, dfsan_label *ret_label) {
466 char *ret = ctime_r(timep, buf);
467 if (ret) {
468 dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), buf,
469 strlen(buf) + 1);
470 *ret_label = buf_label;
471 } else {
472 *ret_label = 0;
473 }
474 return ret;
475 }
476
477 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_fgets(char * s,int size,FILE * stream,dfsan_label s_label,dfsan_label size_label,dfsan_label stream_label,dfsan_label * ret_label)478 char *__dfsw_fgets(char *s, int size, FILE *stream, dfsan_label s_label,
479 dfsan_label size_label, dfsan_label stream_label,
480 dfsan_label *ret_label) {
481 char *ret = fgets(s, size, stream);
482 if (ret) {
483 dfsan_set_label(0, ret, strlen(ret) + 1);
484 *ret_label = s_label;
485 } else {
486 *ret_label = 0;
487 }
488 return ret;
489 }
490
491 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getcwd(char * buf,size_t size,dfsan_label buf_label,dfsan_label size_label,dfsan_label * ret_label)492 char *__dfsw_getcwd(char *buf, size_t size, dfsan_label buf_label,
493 dfsan_label size_label, dfsan_label *ret_label) {
494 char *ret = getcwd(buf, size);
495 if (ret) {
496 dfsan_set_label(0, ret, strlen(ret) + 1);
497 *ret_label = buf_label;
498 } else {
499 *ret_label = 0;
500 }
501 return ret;
502 }
503
504 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_get_current_dir_name(dfsan_label * ret_label)505 char *__dfsw_get_current_dir_name(dfsan_label *ret_label) {
506 char *ret = get_current_dir_name();
507 if (ret) {
508 dfsan_set_label(0, ret, strlen(ret) + 1);
509 }
510 *ret_label = 0;
511 return ret;
512 }
513
514 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_gethostname(char * name,size_t len,dfsan_label name_label,dfsan_label len_label,dfsan_label * ret_label)515 int __dfsw_gethostname(char *name, size_t len, dfsan_label name_label,
516 dfsan_label len_label, dfsan_label *ret_label) {
517 int ret = gethostname(name, len);
518 if (ret == 0) {
519 dfsan_set_label(0, name, strlen(name) + 1);
520 }
521 *ret_label = 0;
522 return ret;
523 }
524
525 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getrlimit(int resource,struct rlimit * rlim,dfsan_label resource_label,dfsan_label rlim_label,dfsan_label * ret_label)526 int __dfsw_getrlimit(int resource, struct rlimit *rlim,
527 dfsan_label resource_label, dfsan_label rlim_label,
528 dfsan_label *ret_label) {
529 int ret = getrlimit(resource, rlim);
530 if (ret == 0) {
531 dfsan_set_label(0, rlim, sizeof(struct rlimit));
532 }
533 *ret_label = 0;
534 return ret;
535 }
536
537 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getrusage(int who,struct rusage * usage,dfsan_label who_label,dfsan_label usage_label,dfsan_label * ret_label)538 int __dfsw_getrusage(int who, struct rusage *usage, dfsan_label who_label,
539 dfsan_label usage_label, dfsan_label *ret_label) {
540 int ret = getrusage(who, usage);
541 if (ret == 0) {
542 dfsan_set_label(0, usage, sizeof(struct rusage));
543 }
544 *ret_label = 0;
545 return ret;
546 }
547
548 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strcpy(char * dest,const char * src,dfsan_label dst_label,dfsan_label src_label,dfsan_label * ret_label)549 char *__dfsw_strcpy(char *dest, const char *src, dfsan_label dst_label,
550 dfsan_label src_label, dfsan_label *ret_label) {
551 char *ret = strcpy(dest, src); // NOLINT
552 if (ret) {
553 internal_memcpy(shadow_for(dest), shadow_for(src),
554 sizeof(dfsan_label) * (strlen(src) + 1));
555 }
556 *ret_label = dst_label;
557 return ret;
558 }
559
560 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtol(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)561 long int __dfsw_strtol(const char *nptr, char **endptr, int base,
562 dfsan_label nptr_label, dfsan_label endptr_label,
563 dfsan_label base_label, dfsan_label *ret_label) {
564 char *tmp_endptr;
565 long int ret = strtol(nptr, &tmp_endptr, base);
566 if (endptr) {
567 *endptr = tmp_endptr;
568 }
569 if (tmp_endptr > nptr) {
570 // If *tmp_endptr is '\0' include its label as well.
571 *ret_label = dfsan_union(
572 base_label,
573 dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
574 } else {
575 *ret_label = 0;
576 }
577 return ret;
578 }
579
580 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtod(const char * nptr,char ** endptr,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label * ret_label)581 double __dfsw_strtod(const char *nptr, char **endptr,
582 dfsan_label nptr_label, dfsan_label endptr_label,
583 dfsan_label *ret_label) {
584 char *tmp_endptr;
585 double ret = strtod(nptr, &tmp_endptr);
586 if (endptr) {
587 *endptr = tmp_endptr;
588 }
589 if (tmp_endptr > nptr) {
590 // If *tmp_endptr is '\0' include its label as well.
591 *ret_label = dfsan_read_label(
592 nptr,
593 tmp_endptr - nptr + (*tmp_endptr ? 0 : 1));
594 } else {
595 *ret_label = 0;
596 }
597 return ret;
598 }
599
600 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtoll(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)601 long long int __dfsw_strtoll(const char *nptr, char **endptr, int base,
602 dfsan_label nptr_label, dfsan_label endptr_label,
603 dfsan_label base_label, dfsan_label *ret_label) {
604 char *tmp_endptr;
605 long long int ret = strtoll(nptr, &tmp_endptr, base);
606 if (endptr) {
607 *endptr = tmp_endptr;
608 }
609 if (tmp_endptr > nptr) {
610 // If *tmp_endptr is '\0' include its label as well.
611 *ret_label = dfsan_union(
612 base_label,
613 dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
614 } else {
615 *ret_label = 0;
616 }
617 return ret;
618 }
619
620 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtoul(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)621 unsigned long int __dfsw_strtoul(const char *nptr, char **endptr, int base,
622 dfsan_label nptr_label, dfsan_label endptr_label,
623 dfsan_label base_label, dfsan_label *ret_label) {
624 char *tmp_endptr;
625 unsigned long int ret = strtoul(nptr, &tmp_endptr, base);
626 if (endptr) {
627 *endptr = tmp_endptr;
628 }
629 if (tmp_endptr > nptr) {
630 // If *tmp_endptr is '\0' include its label as well.
631 *ret_label = dfsan_union(
632 base_label,
633 dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
634 } else {
635 *ret_label = 0;
636 }
637 return ret;
638 }
639
640 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtoull(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)641 long long unsigned int __dfsw_strtoull(const char *nptr, char **endptr,
642 int base, dfsan_label nptr_label,
643 dfsan_label endptr_label,
644 dfsan_label base_label,
645 dfsan_label *ret_label) {
646 char *tmp_endptr;
647 long long unsigned int ret = strtoull(nptr, &tmp_endptr, base);
648 if (endptr) {
649 *endptr = tmp_endptr;
650 }
651 if (tmp_endptr > nptr) {
652 // If *tmp_endptr is '\0' include its label as well.
653 *ret_label = dfsan_union(
654 base_label,
655 dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
656 } else {
657 *ret_label = 0;
658 }
659 return ret;
660 }
661
662 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_time(time_t * t,dfsan_label t_label,dfsan_label * ret_label)663 time_t __dfsw_time(time_t *t, dfsan_label t_label, dfsan_label *ret_label) {
664 time_t ret = time(t);
665 if (ret != (time_t) -1 && t) {
666 dfsan_set_label(0, t, sizeof(time_t));
667 }
668 *ret_label = 0;
669 return ret;
670 }
671
672 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_inet_pton(int af,const char * src,void * dst,dfsan_label af_label,dfsan_label src_label,dfsan_label dst_label,dfsan_label * ret_label)673 int __dfsw_inet_pton(int af, const char *src, void *dst, dfsan_label af_label,
674 dfsan_label src_label, dfsan_label dst_label,
675 dfsan_label *ret_label) {
676 int ret = inet_pton(af, src, dst);
677 if (ret == 1) {
678 dfsan_set_label(dfsan_read_label(src, strlen(src) + 1), dst,
679 af == AF_INET ? sizeof(struct in_addr) : sizeof(in6_addr));
680 }
681 *ret_label = 0;
682 return ret;
683 }
684
685 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_localtime_r(const time_t * timep,struct tm * result,dfsan_label timep_label,dfsan_label result_label,dfsan_label * ret_label)686 struct tm *__dfsw_localtime_r(const time_t *timep, struct tm *result,
687 dfsan_label timep_label, dfsan_label result_label,
688 dfsan_label *ret_label) {
689 struct tm *ret = localtime_r(timep, result);
690 if (ret) {
691 dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), result,
692 sizeof(struct tm));
693 *ret_label = result_label;
694 } else {
695 *ret_label = 0;
696 }
697 return ret;
698 }
699
700 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getpwuid_r(id_t uid,struct passwd * pwd,char * buf,size_t buflen,struct passwd ** result,dfsan_label uid_label,dfsan_label pwd_label,dfsan_label buf_label,dfsan_label buflen_label,dfsan_label result_label,dfsan_label * ret_label)701 int __dfsw_getpwuid_r(id_t uid, struct passwd *pwd,
702 char *buf, size_t buflen, struct passwd **result,
703 dfsan_label uid_label, dfsan_label pwd_label,
704 dfsan_label buf_label, dfsan_label buflen_label,
705 dfsan_label result_label, dfsan_label *ret_label) {
706 // Store the data in pwd, the strings referenced from pwd in buf, and the
707 // address of pwd in *result. On failure, NULL is stored in *result.
708 int ret = getpwuid_r(uid, pwd, buf, buflen, result);
709 if (ret == 0) {
710 dfsan_set_label(0, pwd, sizeof(struct passwd));
711 dfsan_set_label(0, buf, strlen(buf) + 1);
712 }
713 *ret_label = 0;
714 dfsan_set_label(0, result, sizeof(struct passwd*));
715 return ret;
716 }
717
718 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_epoll_wait(int epfd,struct epoll_event * events,int maxevents,int timeout,dfsan_label epfd_label,dfsan_label events_label,dfsan_label maxevents_label,dfsan_label timeout_label,dfsan_label * ret_label)719 int __dfsw_epoll_wait(int epfd, struct epoll_event *events, int maxevents,
720 int timeout, dfsan_label epfd_label,
721 dfsan_label events_label, dfsan_label maxevents_label,
722 dfsan_label timeout_label, dfsan_label *ret_label) {
723 int ret = epoll_wait(epfd, events, maxevents, timeout);
724 if (ret > 0)
725 dfsan_set_label(0, events, ret * sizeof(*events));
726 *ret_label = 0;
727 return ret;
728 }
729
730 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_poll(struct pollfd * fds,nfds_t nfds,int timeout,dfsan_label dfs_label,dfsan_label nfds_label,dfsan_label timeout_label,dfsan_label * ret_label)731 int __dfsw_poll(struct pollfd *fds, nfds_t nfds, int timeout,
732 dfsan_label dfs_label, dfsan_label nfds_label,
733 dfsan_label timeout_label, dfsan_label *ret_label) {
734 int ret = poll(fds, nfds, timeout);
735 if (ret >= 0) {
736 for (; nfds > 0; --nfds) {
737 dfsan_set_label(0, &fds[nfds - 1].revents, sizeof(fds[nfds - 1].revents));
738 }
739 }
740 *ret_label = 0;
741 return ret;
742 }
743
744 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout,dfsan_label nfds_label,dfsan_label readfds_label,dfsan_label writefds_label,dfsan_label exceptfds_label,dfsan_label timeout_label,dfsan_label * ret_label)745 int __dfsw_select(int nfds, fd_set *readfds, fd_set *writefds,
746 fd_set *exceptfds, struct timeval *timeout,
747 dfsan_label nfds_label, dfsan_label readfds_label,
748 dfsan_label writefds_label, dfsan_label exceptfds_label,
749 dfsan_label timeout_label, dfsan_label *ret_label) {
750 int ret = select(nfds, readfds, writefds, exceptfds, timeout);
751 // Clear everything (also on error) since their content is either set or
752 // undefined.
753 if (readfds) {
754 dfsan_set_label(0, readfds, sizeof(fd_set));
755 }
756 if (writefds) {
757 dfsan_set_label(0, writefds, sizeof(fd_set));
758 }
759 if (exceptfds) {
760 dfsan_set_label(0, exceptfds, sizeof(fd_set));
761 }
762 dfsan_set_label(0, timeout, sizeof(struct timeval));
763 *ret_label = 0;
764 return ret;
765 }
766
767 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sched_getaffinity(pid_t pid,size_t cpusetsize,cpu_set_t * mask,dfsan_label pid_label,dfsan_label cpusetsize_label,dfsan_label mask_label,dfsan_label * ret_label)768 int __dfsw_sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask,
769 dfsan_label pid_label,
770 dfsan_label cpusetsize_label,
771 dfsan_label mask_label, dfsan_label *ret_label) {
772 int ret = sched_getaffinity(pid, cpusetsize, mask);
773 if (ret == 0) {
774 dfsan_set_label(0, mask, cpusetsize);
775 }
776 *ret_label = 0;
777 return ret;
778 }
779
780 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sigemptyset(sigset_t * set,dfsan_label set_label,dfsan_label * ret_label)781 int __dfsw_sigemptyset(sigset_t *set, dfsan_label set_label,
782 dfsan_label *ret_label) {
783 int ret = sigemptyset(set);
784 dfsan_set_label(0, set, sizeof(sigset_t));
785 return ret;
786 }
787
788 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sigaction(int signum,const struct sigaction * act,struct sigaction * oldact,dfsan_label signum_label,dfsan_label act_label,dfsan_label oldact_label,dfsan_label * ret_label)789 int __dfsw_sigaction(int signum, const struct sigaction *act,
790 struct sigaction *oldact, dfsan_label signum_label,
791 dfsan_label act_label, dfsan_label oldact_label,
792 dfsan_label *ret_label) {
793 int ret = sigaction(signum, act, oldact);
794 if (oldact) {
795 dfsan_set_label(0, oldact, sizeof(struct sigaction));
796 }
797 *ret_label = 0;
798 return ret;
799 }
800
801 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_gettimeofday(struct timeval * tv,struct timezone * tz,dfsan_label tv_label,dfsan_label tz_label,dfsan_label * ret_label)802 int __dfsw_gettimeofday(struct timeval *tv, struct timezone *tz,
803 dfsan_label tv_label, dfsan_label tz_label,
804 dfsan_label *ret_label) {
805 int ret = gettimeofday(tv, tz);
806 if (tv) {
807 dfsan_set_label(0, tv, sizeof(struct timeval));
808 }
809 if (tz) {
810 dfsan_set_label(0, tz, sizeof(struct timezone));
811 }
812 *ret_label = 0;
813 return ret;
814 }
815
__dfsw_memchr(void * s,int c,size_t n,dfsan_label s_label,dfsan_label c_label,dfsan_label n_label,dfsan_label * ret_label)816 SANITIZER_INTERFACE_ATTRIBUTE void *__dfsw_memchr(void *s, int c, size_t n,
817 dfsan_label s_label,
818 dfsan_label c_label,
819 dfsan_label n_label,
820 dfsan_label *ret_label) {
821 void *ret = memchr(s, c, n);
822 if (flags().strict_data_dependencies) {
823 *ret_label = ret ? s_label : 0;
824 } else {
825 size_t len =
826 ret ? reinterpret_cast<char *>(ret) - reinterpret_cast<char *>(s) + 1
827 : n;
828 *ret_label =
829 dfsan_union(dfsan_read_label(s, len), dfsan_union(s_label, c_label));
830 }
831 return ret;
832 }
833
__dfsw_strrchr(char * s,int c,dfsan_label s_label,dfsan_label c_label,dfsan_label * ret_label)834 SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strrchr(char *s, int c,
835 dfsan_label s_label,
836 dfsan_label c_label,
837 dfsan_label *ret_label) {
838 char *ret = strrchr(s, c);
839 if (flags().strict_data_dependencies) {
840 *ret_label = ret ? s_label : 0;
841 } else {
842 *ret_label =
843 dfsan_union(dfsan_read_label(s, strlen(s) + 1),
844 dfsan_union(s_label, c_label));
845 }
846
847 return ret;
848 }
849
__dfsw_strstr(char * haystack,char * needle,dfsan_label haystack_label,dfsan_label needle_label,dfsan_label * ret_label)850 SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strstr(char *haystack, char *needle,
851 dfsan_label haystack_label,
852 dfsan_label needle_label,
853 dfsan_label *ret_label) {
854 char *ret = strstr(haystack, needle);
855 if (flags().strict_data_dependencies) {
856 *ret_label = ret ? haystack_label : 0;
857 } else {
858 size_t len = ret ? ret + strlen(needle) - haystack : strlen(haystack) + 1;
859 *ret_label =
860 dfsan_union(dfsan_read_label(haystack, len),
861 dfsan_union(dfsan_read_label(needle, strlen(needle) + 1),
862 dfsan_union(haystack_label, needle_label)));
863 }
864
865 return ret;
866 }
867
__dfsw_nanosleep(const struct timespec * req,struct timespec * rem,dfsan_label req_label,dfsan_label rem_label,dfsan_label * ret_label)868 SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_nanosleep(const struct timespec *req,
869 struct timespec *rem,
870 dfsan_label req_label,
871 dfsan_label rem_label,
872 dfsan_label *ret_label) {
873 int ret = nanosleep(req, rem);
874 *ret_label = 0;
875 if (ret == -1) {
876 // Interrupted by a signal, rem is filled with the remaining time.
877 dfsan_set_label(0, rem, sizeof(struct timespec));
878 }
879 return ret;
880 }
881
882 SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_socketpair(int domain,int type,int protocol,int sv[2],dfsan_label domain_label,dfsan_label type_label,dfsan_label protocol_label,dfsan_label sv_label,dfsan_label * ret_label)883 __dfsw_socketpair(int domain, int type, int protocol, int sv[2],
884 dfsan_label domain_label, dfsan_label type_label,
885 dfsan_label protocol_label, dfsan_label sv_label,
886 dfsan_label *ret_label) {
887 int ret = socketpair(domain, type, protocol, sv);
888 *ret_label = 0;
889 if (ret == 0) {
890 dfsan_set_label(0, sv, sizeof(*sv) * 2);
891 }
892 return ret;
893 }
894
895 // Type of the trampoline function passed to the custom version of
896 // dfsan_set_write_callback.
897 typedef void (*write_trampoline_t)(
898 void *callback,
899 int fd, const void *buf, ssize_t count,
900 dfsan_label fd_label, dfsan_label buf_label, dfsan_label count_label);
901
902 // Calls to dfsan_set_write_callback() set the values in this struct.
903 // Calls to the custom version of write() read (and invoke) them.
904 static struct {
905 write_trampoline_t write_callback_trampoline = nullptr;
906 void *write_callback = nullptr;
907 } write_callback_info;
908
909 SANITIZER_INTERFACE_ATTRIBUTE void
__dfsw_dfsan_set_write_callback(write_trampoline_t write_callback_trampoline,void * write_callback,dfsan_label write_callback_label,dfsan_label * ret_label)910 __dfsw_dfsan_set_write_callback(
911 write_trampoline_t write_callback_trampoline,
912 void *write_callback,
913 dfsan_label write_callback_label,
914 dfsan_label *ret_label) {
915 write_callback_info.write_callback_trampoline = write_callback_trampoline;
916 write_callback_info.write_callback = write_callback;
917 }
918
919 SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_write(int fd,const void * buf,size_t count,dfsan_label fd_label,dfsan_label buf_label,dfsan_label count_label,dfsan_label * ret_label)920 __dfsw_write(int fd, const void *buf, size_t count,
921 dfsan_label fd_label, dfsan_label buf_label,
922 dfsan_label count_label, dfsan_label *ret_label) {
923 if (write_callback_info.write_callback) {
924 write_callback_info.write_callback_trampoline(
925 write_callback_info.write_callback,
926 fd, buf, count,
927 fd_label, buf_label, count_label);
928 }
929
930 *ret_label = 0;
931 return write(fd, buf, count);
932 }
933 } // namespace __dfsan
934
935 // Type used to extract a dfsan_label with va_arg()
936 typedef int dfsan_label_va;
937
938 // Formats a chunk either a constant string or a single format directive (e.g.,
939 // '%.3f').
940 struct Formatter {
FormatterFormatter941 Formatter(char *str_, const char *fmt_, size_t size_)
942 : str(str_), str_off(0), size(size_), fmt_start(fmt_), fmt_cur(fmt_),
943 width(-1) {}
944
formatFormatter945 int format() {
946 char *tmp_fmt = build_format_string();
947 int retval =
948 snprintf(str + str_off, str_off < size ? size - str_off : 0, tmp_fmt,
949 0 /* used only to avoid warnings */);
950 free(tmp_fmt);
951 return retval;
952 }
953
formatFormatter954 template <typename T> int format(T arg) {
955 char *tmp_fmt = build_format_string();
956 int retval;
957 if (width >= 0) {
958 retval = snprintf(str + str_off, str_off < size ? size - str_off : 0,
959 tmp_fmt, width, arg);
960 } else {
961 retval = snprintf(str + str_off, str_off < size ? size - str_off : 0,
962 tmp_fmt, arg);
963 }
964 free(tmp_fmt);
965 return retval;
966 }
967
build_format_stringFormatter968 char *build_format_string() {
969 size_t fmt_size = fmt_cur - fmt_start + 1;
970 char *new_fmt = (char *)malloc(fmt_size + 1);
971 assert(new_fmt);
972 internal_memcpy(new_fmt, fmt_start, fmt_size);
973 new_fmt[fmt_size] = '\0';
974 return new_fmt;
975 }
976
str_curFormatter977 char *str_cur() { return str + str_off; }
978
num_written_bytesFormatter979 size_t num_written_bytes(int retval) {
980 if (retval < 0) {
981 return 0;
982 }
983
984 size_t num_avail = str_off < size ? size - str_off : 0;
985 if (num_avail == 0) {
986 return 0;
987 }
988
989 size_t num_written = retval;
990 // A return value of {v,}snprintf of size or more means that the output was
991 // truncated.
992 if (num_written >= num_avail) {
993 num_written -= num_avail;
994 }
995
996 return num_written;
997 }
998
999 char *str;
1000 size_t str_off;
1001 size_t size;
1002 const char *fmt_start;
1003 const char *fmt_cur;
1004 int width;
1005 };
1006
1007 // Formats the input and propagates the input labels to the output. The output
1008 // is stored in 'str'. 'size' bounds the number of output bytes. 'format' and
1009 // 'ap' are the format string and the list of arguments for formatting. Returns
1010 // the return value vsnprintf would return.
1011 //
1012 // The function tokenizes the format string in chunks representing either a
1013 // constant string or a single format directive (e.g., '%.3f') and formats each
1014 // chunk independently into the output string. This approach allows to figure
1015 // out which bytes of the output string depends on which argument and thus to
1016 // propagate labels more precisely.
1017 //
1018 // WARNING: This implementation does not support conversion specifiers with
1019 // positional arguments.
format_buffer(char * str,size_t size,const char * fmt,dfsan_label * va_labels,dfsan_label * ret_label,va_list ap)1020 static int format_buffer(char *str, size_t size, const char *fmt,
1021 dfsan_label *va_labels, dfsan_label *ret_label,
1022 va_list ap) {
1023 Formatter formatter(str, fmt, size);
1024
1025 while (*formatter.fmt_cur) {
1026 formatter.fmt_start = formatter.fmt_cur;
1027 formatter.width = -1;
1028 int retval = 0;
1029
1030 if (*formatter.fmt_cur != '%') {
1031 // Ordinary character. Consume all the characters until a '%' or the end
1032 // of the string.
1033 for (; *(formatter.fmt_cur + 1) && *(formatter.fmt_cur + 1) != '%';
1034 ++formatter.fmt_cur) {}
1035 retval = formatter.format();
1036 dfsan_set_label(0, formatter.str_cur(),
1037 formatter.num_written_bytes(retval));
1038 } else {
1039 // Conversion directive. Consume all the characters until a conversion
1040 // specifier or the end of the string.
1041 bool end_fmt = false;
1042 for (; *formatter.fmt_cur && !end_fmt; ) {
1043 switch (*++formatter.fmt_cur) {
1044 case 'd':
1045 case 'i':
1046 case 'o':
1047 case 'u':
1048 case 'x':
1049 case 'X':
1050 switch (*(formatter.fmt_cur - 1)) {
1051 case 'h':
1052 // Also covers the 'hh' case (since the size of the arg is still
1053 // an int).
1054 retval = formatter.format(va_arg(ap, int));
1055 break;
1056 case 'l':
1057 if (formatter.fmt_cur - formatter.fmt_start >= 2 &&
1058 *(formatter.fmt_cur - 2) == 'l') {
1059 retval = formatter.format(va_arg(ap, long long int));
1060 } else {
1061 retval = formatter.format(va_arg(ap, long int));
1062 }
1063 break;
1064 case 'q':
1065 retval = formatter.format(va_arg(ap, long long int));
1066 break;
1067 case 'j':
1068 retval = formatter.format(va_arg(ap, intmax_t));
1069 break;
1070 case 'z':
1071 case 't':
1072 retval = formatter.format(va_arg(ap, size_t));
1073 break;
1074 default:
1075 retval = formatter.format(va_arg(ap, int));
1076 }
1077 dfsan_set_label(*va_labels++, formatter.str_cur(),
1078 formatter.num_written_bytes(retval));
1079 end_fmt = true;
1080 break;
1081
1082 case 'a':
1083 case 'A':
1084 case 'e':
1085 case 'E':
1086 case 'f':
1087 case 'F':
1088 case 'g':
1089 case 'G':
1090 if (*(formatter.fmt_cur - 1) == 'L') {
1091 retval = formatter.format(va_arg(ap, long double));
1092 } else {
1093 retval = formatter.format(va_arg(ap, double));
1094 }
1095 dfsan_set_label(*va_labels++, formatter.str_cur(),
1096 formatter.num_written_bytes(retval));
1097 end_fmt = true;
1098 break;
1099
1100 case 'c':
1101 retval = formatter.format(va_arg(ap, int));
1102 dfsan_set_label(*va_labels++, formatter.str_cur(),
1103 formatter.num_written_bytes(retval));
1104 end_fmt = true;
1105 break;
1106
1107 case 's': {
1108 char *arg = va_arg(ap, char *);
1109 retval = formatter.format(arg);
1110 va_labels++;
1111 internal_memcpy(shadow_for(formatter.str_cur()), shadow_for(arg),
1112 sizeof(dfsan_label) *
1113 formatter.num_written_bytes(retval));
1114 end_fmt = true;
1115 break;
1116 }
1117
1118 case 'p':
1119 retval = formatter.format(va_arg(ap, void *));
1120 dfsan_set_label(*va_labels++, formatter.str_cur(),
1121 formatter.num_written_bytes(retval));
1122 end_fmt = true;
1123 break;
1124
1125 case 'n': {
1126 int *ptr = va_arg(ap, int *);
1127 *ptr = (int)formatter.str_off;
1128 va_labels++;
1129 dfsan_set_label(0, ptr, sizeof(ptr));
1130 end_fmt = true;
1131 break;
1132 }
1133
1134 case '%':
1135 retval = formatter.format();
1136 dfsan_set_label(0, formatter.str_cur(),
1137 formatter.num_written_bytes(retval));
1138 end_fmt = true;
1139 break;
1140
1141 case '*':
1142 formatter.width = va_arg(ap, int);
1143 va_labels++;
1144 break;
1145
1146 default:
1147 break;
1148 }
1149 }
1150 }
1151
1152 if (retval < 0) {
1153 return retval;
1154 }
1155
1156 formatter.fmt_cur++;
1157 formatter.str_off += retval;
1158 }
1159
1160 *ret_label = 0;
1161
1162 // Number of bytes written in total.
1163 return formatter.str_off;
1164 }
1165
1166 extern "C" {
1167 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sprintf(char * str,const char * format,dfsan_label str_label,dfsan_label format_label,dfsan_label * va_labels,dfsan_label * ret_label,...)1168 int __dfsw_sprintf(char *str, const char *format, dfsan_label str_label,
1169 dfsan_label format_label, dfsan_label *va_labels,
1170 dfsan_label *ret_label, ...) {
1171 va_list ap;
1172 va_start(ap, ret_label);
1173 int ret = format_buffer(str, ~0ul, format, va_labels, ret_label, ap);
1174 va_end(ap);
1175 return ret;
1176 }
1177
1178 SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_snprintf(char * str,size_t size,const char * format,dfsan_label str_label,dfsan_label size_label,dfsan_label format_label,dfsan_label * va_labels,dfsan_label * ret_label,...)1179 int __dfsw_snprintf(char *str, size_t size, const char *format,
1180 dfsan_label str_label, dfsan_label size_label,
1181 dfsan_label format_label, dfsan_label *va_labels,
1182 dfsan_label *ret_label, ...) {
1183 va_list ap;
1184 va_start(ap, ret_label);
1185 int ret = format_buffer(str, size, format, va_labels, ret_label, ap);
1186 va_end(ap);
1187 return ret;
1188 }
1189
1190 // Default empty implementations (weak). Users should redefine them.
SANITIZER_INTERFACE_WEAK_DEF(void,__sanitizer_cov_trace_pc_guard,u32 *)1191 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32 *) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__sanitizer_cov_trace_pc_guard_init,u32 *,u32 *)1192 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init, u32 *,
1193 u32 *) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__sanitizer_cov_pcs_init,void)1194 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_pcs_init, void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__sanitizer_cov_trace_pc_indir,void)1195 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {}
1196
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_cmp,void)1197 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp, void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_cmp1,void)1198 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp1, void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_cmp2,void)1199 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp2, void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_cmp4,void)1200 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp4, void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_cmp8,void)1201 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp8, void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_const_cmp1,void)1202 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp1,
1203 void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_const_cmp2,void)1204 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp2,
1205 void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_const_cmp4,void)1206 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp4,
1207 void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_const_cmp8,void)1208 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp8,
1209 void) {}
SANITIZER_INTERFACE_WEAK_DEF(void,__dfsw___sanitizer_cov_trace_switch,void)1210 SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_switch, void) {}
1211 } // extern "C"
1212