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