1 // RUN: %clang_dfsan %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %run %t
2 // RUN: %clang_dfsan -mllvm -dfsan-args-abi %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %run %t
3 // RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES %s -o %t && %run %t
4 // RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES -mllvm -dfsan-args-abi %s -o %t && %run %t
5 
6 // Tests custom implementations of various glibc functions.
7 
8 #include <sanitizer/dfsan_interface.h>
9 
10 #include <arpa/inet.h>
11 #include <assert.h>
12 #include <fcntl.h>
13 #include <link.h>
14 #include <poll.h>
15 #include <pthread.h>
16 #include <pwd.h>
17 #include <sched.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/select.h>
24 #include <sys/resource.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 dfsan_label i_label = 0;
32 dfsan_label j_label = 0;
33 dfsan_label k_label = 0;
34 dfsan_label i_j_label = 0;
35 
36 #define ASSERT_ZERO_LABEL(data) \
37   assert(0 == dfsan_get_label((long) (data)))
38 
39 #define ASSERT_READ_ZERO_LABEL(ptr, size) \
40   assert(0 == dfsan_read_label(ptr, size))
41 
42 #define ASSERT_LABEL(data, label) \
43   assert(label == dfsan_get_label((long) (data)))
44 
45 #define ASSERT_READ_LABEL(ptr, size, label) \
46   assert(label == dfsan_read_label(ptr, size))
47 
test_stat()48 void test_stat() {
49   int i = 1;
50   dfsan_set_label(i_label, &i, sizeof(i));
51 
52   struct stat s;
53   s.st_dev = i;
54   assert(0 == stat("/", &s));
55   ASSERT_ZERO_LABEL(s.st_dev);
56 
57   s.st_dev = i;
58   assert(-1 == stat("/nonexistent", &s));
59   ASSERT_LABEL(s.st_dev, i_label);
60 }
61 
test_fstat()62 void test_fstat() {
63   int i = 1;
64   dfsan_set_label(i_label, &i, sizeof(i));
65 
66   struct stat s;
67   int fd = open("/dev/zero", O_RDONLY);
68   s.st_dev = i;
69   int rv = fstat(fd, &s);
70   assert(0 == rv);
71   ASSERT_ZERO_LABEL(s.st_dev);
72 }
73 
test_memcmp()74 void test_memcmp() {
75   char str1[] = "str1", str2[] = "str2";
76   dfsan_set_label(i_label, &str1[3], 1);
77   dfsan_set_label(j_label, &str2[3], 1);
78 
79   int rv = memcmp(str1, str2, sizeof(str1));
80   assert(rv < 0);
81 #ifdef STRICT_DATA_DEPENDENCIES
82   ASSERT_ZERO_LABEL(rv);
83 #else
84   ASSERT_LABEL(rv, i_j_label);
85 #endif
86 }
87 
test_memcpy()88 void test_memcpy() {
89   char str1[] = "str1";
90   char str2[sizeof(str1)];
91   dfsan_set_label(i_label, &str1[3], 1);
92 
93   ASSERT_ZERO_LABEL(memcpy(str2, str1, sizeof(str1)));
94   assert(0 == memcmp(str2, str1, sizeof(str1)));
95   ASSERT_ZERO_LABEL(str2[0]);
96   ASSERT_LABEL(str2[3], i_label);
97 }
98 
test_memset()99 void test_memset() {
100   char buf[8];
101   int j = 'a';
102   dfsan_set_label(j_label, &j, sizeof(j));
103 
104   ASSERT_ZERO_LABEL(memset(&buf, j, sizeof(buf)));
105   for (int i = 0; i < 8; ++i) {
106     ASSERT_LABEL(buf[i], j_label);
107     assert(buf[i] == 'a');
108   }
109 }
110 
test_strcmp()111 void test_strcmp() {
112   char str1[] = "str1", str2[] = "str2";
113   dfsan_set_label(i_label, &str1[3], 1);
114   dfsan_set_label(j_label, &str2[3], 1);
115 
116   int rv = strcmp(str1, str2);
117   assert(rv < 0);
118 #ifdef STRICT_DATA_DEPENDENCIES
119   ASSERT_ZERO_LABEL(rv);
120 #else
121   ASSERT_LABEL(rv, i_j_label);
122 #endif
123 }
124 
test_strlen()125 void test_strlen() {
126   char str1[] = "str1";
127   dfsan_set_label(i_label, &str1[3], 1);
128 
129   int rv = strlen(str1);
130   assert(rv == 4);
131 #ifdef STRICT_DATA_DEPENDENCIES
132   ASSERT_ZERO_LABEL(rv);
133 #else
134   ASSERT_LABEL(rv, i_label);
135 #endif
136 }
137 
test_strdup()138 void test_strdup() {
139   char str1[] = "str1";
140   dfsan_set_label(i_label, &str1[3], 1);
141 
142   char *strd = strdup(str1);
143   ASSERT_ZERO_LABEL(strd[0]);
144   ASSERT_LABEL(strd[3], i_label);
145   free(strd);
146 }
147 
test_strncpy()148 void test_strncpy() {
149   char str1[] = "str1";
150   char str2[sizeof(str1)];
151   dfsan_set_label(i_label, &str1[3], 1);
152 
153   char *strd = strncpy(str2, str1, 5);
154   assert(strd == str2);
155   assert(strcmp(str1, str2) == 0);
156   ASSERT_ZERO_LABEL(strd);
157   ASSERT_ZERO_LABEL(strd[0]);
158   ASSERT_ZERO_LABEL(strd[1]);
159   ASSERT_ZERO_LABEL(strd[2]);
160   ASSERT_LABEL(strd[3], i_label);
161 
162   strd = strncpy(str2, str1, 3);
163   assert(strd == str2);
164   assert(strncmp(str1, str2, 3) == 0);
165   ASSERT_ZERO_LABEL(strd);
166   ASSERT_ZERO_LABEL(strd[0]);
167   ASSERT_ZERO_LABEL(strd[1]);
168   ASSERT_ZERO_LABEL(strd[2]);
169 }
170 
test_strncmp()171 void test_strncmp() {
172   char str1[] = "str1", str2[] = "str2";
173   dfsan_set_label(i_label, &str1[3], 1);
174   dfsan_set_label(j_label, &str2[3], 1);
175 
176   int rv = strncmp(str1, str2, sizeof(str1));
177   assert(rv < 0);
178 #ifdef STRICT_DATA_DEPENDENCIES
179   ASSERT_ZERO_LABEL(rv);
180 #else
181   ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
182 #endif
183 
184   rv = strncmp(str1, str2, 3);
185   assert(rv == 0);
186   ASSERT_ZERO_LABEL(rv);
187 }
188 
test_strcasecmp()189 void test_strcasecmp() {
190   char str1[] = "str1", str2[] = "str2", str3[] = "Str1";
191   dfsan_set_label(i_label, &str1[3], 1);
192   dfsan_set_label(j_label, &str2[3], 1);
193   dfsan_set_label(j_label, &str3[2], 1);
194 
195   int rv = strcasecmp(str1, str2);
196   assert(rv < 0);
197 #ifdef STRICT_DATA_DEPENDENCIES
198   ASSERT_ZERO_LABEL(rv);
199 #else
200   ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
201 #endif
202 
203   rv = strcasecmp(str1, str3);
204   assert(rv == 0);
205 #ifdef STRICT_DATA_DEPENDENCIES
206   ASSERT_ZERO_LABEL(rv);
207 #else
208   ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
209 #endif
210 
211   char s1[] = "AbZ";
212   char s2[] = "aBy";
213   dfsan_set_label(i_label, &s1[2], 1);
214   dfsan_set_label(j_label, &s2[2], 1);
215 
216   rv = strcasecmp(s1, s2);
217   assert(rv > 0); // 'Z' > 'y'
218 #ifdef STRICT_DATA_DEPENDENCIES
219   ASSERT_ZERO_LABEL(rv);
220 #else
221   ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
222 #endif
223 }
224 
test_strncasecmp()225 void test_strncasecmp() {
226   char str1[] = "Str1", str2[] = "str2";
227   dfsan_set_label(i_label, &str1[3], 1);
228   dfsan_set_label(j_label, &str2[3], 1);
229 
230   int rv = strncasecmp(str1, str2, sizeof(str1));
231   assert(rv < 0);
232 #ifdef STRICT_DATA_DEPENDENCIES
233   ASSERT_ZERO_LABEL(rv);
234 #else
235   ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
236 #endif
237 
238   rv = strncasecmp(str1, str2, 3);
239   assert(rv == 0);
240   ASSERT_ZERO_LABEL(rv);
241 
242   char s1[] = "AbZ";
243   char s2[] = "aBy";
244   dfsan_set_label(i_label, &s1[2], 1);
245   dfsan_set_label(j_label, &s2[2], 1);
246 
247   rv = strncasecmp(s1, s2, 0);
248   assert(rv == 0); // Compare zero chars.
249   ASSERT_ZERO_LABEL(rv);
250 
251   rv = strncasecmp(s1, s2, 1);
252   assert(rv == 0); // 'A' == 'a'
253   ASSERT_ZERO_LABEL(rv);
254 
255   rv = strncasecmp(s1, s2, 2);
256   assert(rv == 0); // 'b' == 'B'
257   ASSERT_ZERO_LABEL(rv);
258 
259   rv = strncasecmp(s1, s2, 3);
260   assert(rv > 0); // 'Z' > 'y'
261 #ifdef STRICT_DATA_DEPENDENCIES
262   ASSERT_ZERO_LABEL(rv);
263 #else
264   ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
265 #endif
266 }
267 
test_strchr()268 void test_strchr() {
269   char str1[] = "str1";
270   dfsan_set_label(i_label, &str1[3], 1);
271 
272   char *crv = strchr(str1, 'r');
273   assert(crv == &str1[2]);
274   ASSERT_ZERO_LABEL(crv);
275 
276   crv = strchr(str1, '1');
277   assert(crv == &str1[3]);
278 #ifdef STRICT_DATA_DEPENDENCIES
279   ASSERT_ZERO_LABEL(crv);
280 #else
281   ASSERT_LABEL(crv, i_label);
282 #endif
283 
284   crv = strchr(str1, 'x');
285   assert(!crv);
286 #ifdef STRICT_DATA_DEPENDENCIES
287   ASSERT_ZERO_LABEL(crv);
288 #else
289   ASSERT_LABEL(crv, i_label);
290 #endif
291 
292   // `man strchr` says:
293   // The terminating null byte is considered part of the string, so that if c
294   // is specified as '\0', these functions return a pointer to the terminator.
295   crv = strchr(str1, '\0');
296   assert(crv == &str1[4]);
297 #ifdef STRICT_DATA_DEPENDENCIES
298   ASSERT_ZERO_LABEL(crv);
299 #else
300   ASSERT_LABEL(crv, i_label);
301 #endif
302 }
303 
test_calloc()304 void test_calloc() {
305   // With any luck this sequence of calls will cause calloc to return the same
306   // pointer both times.  This is probably the best we can do to test this
307   // function.
308   char *crv = (char *) calloc(4096, 1);
309   ASSERT_ZERO_LABEL(crv[0]);
310   dfsan_set_label(i_label, crv, 100);
311   free(crv);
312 
313   crv = (char *) calloc(4096, 1);
314   ASSERT_ZERO_LABEL(crv[0]);
315   free(crv);
316 }
317 
test_read()318 void test_read() {
319   char buf[16];
320   dfsan_set_label(i_label, buf, 1);
321   dfsan_set_label(j_label, buf + 15, 1);
322 
323   ASSERT_LABEL(buf[0], i_label);
324   ASSERT_LABEL(buf[15], j_label);
325 
326   int fd = open("/dev/zero", O_RDONLY);
327   int rv = read(fd, buf, sizeof(buf));
328   assert(rv == sizeof(buf));
329   ASSERT_ZERO_LABEL(rv);
330   ASSERT_ZERO_LABEL(buf[0]);
331   ASSERT_ZERO_LABEL(buf[15]);
332   close(fd);
333 }
334 
test_pread()335 void test_pread() {
336   char buf[16];
337   dfsan_set_label(i_label, buf, 1);
338   dfsan_set_label(j_label, buf + 15, 1);
339 
340   ASSERT_LABEL(buf[0], i_label);
341   ASSERT_LABEL(buf[15], j_label);
342 
343   int fd = open("/bin/sh", O_RDONLY);
344   int rv = pread(fd, buf, sizeof(buf), 0);
345   assert(rv == sizeof(buf));
346   ASSERT_ZERO_LABEL(rv);
347   ASSERT_ZERO_LABEL(buf[0]);
348   ASSERT_ZERO_LABEL(buf[15]);
349   close(fd);
350 }
351 
test_dlopen()352 void test_dlopen() {
353   void *map = dlopen(NULL, RTLD_NOW);
354   assert(map);
355   ASSERT_ZERO_LABEL(map);
356   dlclose(map);
357   map = dlopen("/nonexistent", RTLD_NOW);
358   assert(!map);
359   ASSERT_ZERO_LABEL(map);
360 }
361 
test_clock_gettime()362 void test_clock_gettime() {
363   struct timespec tp;
364   dfsan_set_label(j_label, ((char *)&tp) + 3, 1);
365   int t = clock_gettime(CLOCK_REALTIME, &tp);
366   assert(t == 0);
367   ASSERT_ZERO_LABEL(t);
368   ASSERT_ZERO_LABEL(((char *)&tp)[3]);
369 }
370 
test_ctime_r()371 void test_ctime_r() {
372   char *buf = (char*) malloc(64);
373   time_t t = 0;
374 
375   char *ret = ctime_r(&t, buf);
376   ASSERT_ZERO_LABEL(ret);
377   assert(buf == ret);
378   ASSERT_READ_ZERO_LABEL(buf, strlen(buf) + 1);
379 
380   dfsan_set_label(i_label, &t, sizeof(t));
381   ret = ctime_r(&t, buf);
382   ASSERT_ZERO_LABEL(ret);
383   ASSERT_READ_LABEL(buf, strlen(buf) + 1, i_label);
384 
385   t = 0;
386   dfsan_set_label(j_label, &buf, sizeof(&buf));
387   ret = ctime_r(&t, buf);
388   ASSERT_LABEL(ret, j_label);
389   ASSERT_READ_ZERO_LABEL(buf, strlen(buf) + 1);
390 }
391 
392 static int write_callback_count = 0;
393 static int last_fd;
394 static const unsigned char *last_buf;
395 static size_t last_count;
396 
write_callback(int fd,const void * buf,size_t count)397 void write_callback(int fd, const void *buf, size_t count) {
398   write_callback_count++;
399 
400   last_fd = fd;
401   last_buf = (const unsigned char*) buf;
402   last_count = count;
403 }
404 
test_dfsan_set_write_callback()405 void test_dfsan_set_write_callback() {
406   char buf[] = "Sample chars";
407   int buf_len = strlen(buf);
408 
409   int fd = open("/dev/null", O_WRONLY);
410 
411   dfsan_set_write_callback(write_callback);
412 
413   write_callback_count = 0;
414 
415   // Callback should be invoked on every call to write().
416   int res = write(fd, buf, buf_len);
417   assert(write_callback_count == 1);
418   ASSERT_READ_ZERO_LABEL(&res, sizeof(res));
419   ASSERT_READ_ZERO_LABEL(&last_fd, sizeof(last_fd));
420   ASSERT_READ_ZERO_LABEL(last_buf, sizeof(last_buf));
421   ASSERT_READ_ZERO_LABEL(&last_count, sizeof(last_count));
422 
423   // Add a label to write() arguments.  Check that the labels are readable from
424   // the values passed to the callback.
425   dfsan_set_label(i_label, &fd, sizeof(fd));
426   dfsan_set_label(j_label, &(buf[3]), 1);
427   dfsan_set_label(k_label, &buf_len, sizeof(buf_len));
428 
429   res = write(fd, buf, buf_len);
430   assert(write_callback_count == 2);
431   ASSERT_READ_ZERO_LABEL(&res, sizeof(res));
432   ASSERT_READ_LABEL(&last_fd, sizeof(last_fd), i_label);
433   ASSERT_READ_LABEL(&last_buf[3], sizeof(last_buf[3]), j_label);
434   ASSERT_READ_LABEL(last_buf, sizeof(last_buf), j_label);
435   ASSERT_READ_LABEL(&last_count, sizeof(last_count), k_label);
436 
437   dfsan_set_write_callback(NULL);
438 }
439 
test_fgets()440 void test_fgets() {
441   char *buf = (char*) malloc(128);
442   FILE *f = fopen("/etc/passwd", "r");
443   dfsan_set_label(j_label, buf, 1);
444   char *ret = fgets(buf, sizeof(buf), f);
445   assert(ret == buf);
446   ASSERT_ZERO_LABEL(ret);
447   ASSERT_READ_ZERO_LABEL(buf, 128);
448   dfsan_set_label(j_label, &buf, sizeof(&buf));
449   ret = fgets(buf, sizeof(buf), f);
450   ASSERT_LABEL(ret, j_label);
451   fclose(f);
452 }
453 
test_getcwd()454 void test_getcwd() {
455   char buf[1024];
456   char *ptr = buf;
457   dfsan_set_label(i_label, buf + 2, 2);
458   char* ret = getcwd(buf, sizeof(buf));
459   assert(ret == buf);
460   assert(ret[0] == '/');
461   ASSERT_READ_ZERO_LABEL(buf + 2, 2);
462   dfsan_set_label(i_label, &ptr, sizeof(ptr));
463   ret = getcwd(ptr, sizeof(buf));
464   ASSERT_LABEL(ret, i_label);
465 }
466 
test_get_current_dir_name()467 void test_get_current_dir_name() {
468   char* ret = get_current_dir_name();
469   assert(ret);
470   assert(ret[0] == '/');
471   ASSERT_READ_ZERO_LABEL(ret, strlen(ret) + 1);
472 }
473 
test_gethostname()474 void test_gethostname() {
475   char buf[1024];
476   dfsan_set_label(i_label, buf + 2, 2);
477   assert(gethostname(buf, sizeof(buf)) == 0);
478   ASSERT_READ_ZERO_LABEL(buf + 2, 2);
479 }
480 
test_getrlimit()481 void test_getrlimit() {
482   struct rlimit rlim;
483   dfsan_set_label(i_label, &rlim, sizeof(rlim));
484   assert(getrlimit(RLIMIT_CPU, &rlim) == 0);
485   ASSERT_READ_ZERO_LABEL(&rlim, sizeof(rlim));
486 }
487 
test_getrusage()488 void test_getrusage() {
489   struct rusage usage;
490   dfsan_set_label(i_label, &usage, sizeof(usage));
491   assert(getrusage(RUSAGE_SELF, &usage) == 0);
492   ASSERT_READ_ZERO_LABEL(&usage, sizeof(usage));
493 }
494 
test_strcpy()495 void test_strcpy() {
496   char src[] = "hello world";
497   char dst[sizeof(src) + 2];
498   dfsan_set_label(0, src, sizeof(src));
499   dfsan_set_label(0, dst, sizeof(dst));
500   dfsan_set_label(i_label, src + 2, 1);
501   dfsan_set_label(j_label, src + 3, 1);
502   dfsan_set_label(j_label, dst + 4, 1);
503   dfsan_set_label(i_label, dst + 12, 1);
504   char *ret = strcpy(dst, src);
505   assert(ret == dst);
506   assert(strcmp(src, dst) == 0);
507   for (int i = 0; i < strlen(src) + 1; ++i) {
508     assert(dfsan_get_label(dst[i]) == dfsan_get_label(src[i]));
509   }
510   // Note: if strlen(src) + 1 were used instead to compute the first untouched
511   // byte of dest, the label would be I|J. This is because strlen() might
512   // return a non-zero label, and because by default pointer labels are not
513   // ignored on loads.
514   ASSERT_LABEL(dst[12], i_label);
515 }
516 
test_strtol()517 void test_strtol() {
518   char buf[] = "1234578910";
519   char *endptr = NULL;
520   dfsan_set_label(i_label, buf + 1, 1);
521   dfsan_set_label(j_label, buf + 10, 1);
522   long int ret = strtol(buf, &endptr, 10);
523   assert(ret == 1234578910);
524   assert(endptr == buf + 10);
525   ASSERT_LABEL(ret, i_j_label);
526 }
527 
test_strtoll()528 void test_strtoll() {
529   char buf[] = "1234578910 ";
530   char *endptr = NULL;
531   dfsan_set_label(i_label, buf + 1, 1);
532   dfsan_set_label(j_label, buf + 2, 1);
533   long long int ret = strtoll(buf, &endptr, 10);
534   assert(ret == 1234578910);
535   assert(endptr == buf + 10);
536   ASSERT_LABEL(ret, i_j_label);
537 }
538 
test_strtoul()539 void test_strtoul() {
540   char buf[] = "0xffffffffffffaa";
541   char *endptr = NULL;
542   dfsan_set_label(i_label, buf + 1, 1);
543   dfsan_set_label(j_label, buf + 2, 1);
544   long unsigned int ret = strtol(buf, &endptr, 16);
545   assert(ret == 72057594037927850);
546   assert(endptr == buf + 16);
547   ASSERT_LABEL(ret, i_j_label);
548 }
549 
test_strtoull()550 void test_strtoull() {
551   char buf[] = "0xffffffffffffffaa";
552   char *endptr = NULL;
553   dfsan_set_label(i_label, buf + 1, 1);
554   dfsan_set_label(j_label, buf + 2, 1);
555   long long unsigned int ret = strtoull(buf, &endptr, 16);
556   assert(ret == 0xffffffffffffffaa);
557   assert(endptr == buf + 18);
558   ASSERT_LABEL(ret, i_j_label);
559 }
560 
test_strtod()561 void test_strtod() {
562   char buf[] = "12345.76 foo";
563   char *endptr = NULL;
564   dfsan_set_label(i_label, buf + 1, 1);
565   dfsan_set_label(j_label, buf + 6, 1);
566   double ret = strtod(buf, &endptr);
567   assert(ret == 12345.76);
568   assert(endptr == buf + 8);
569   ASSERT_LABEL(ret, i_j_label);
570 }
571 
test_time()572 void test_time() {
573   time_t t = 0;
574   dfsan_set_label(i_label, &t, 1);
575   time_t ret = time(&t);
576   assert(ret == t);
577   assert(ret > 0);
578   ASSERT_ZERO_LABEL(t);
579 }
580 
test_inet_pton()581 void test_inet_pton() {
582   char addr4[] = "127.0.0.1";
583   dfsan_set_label(i_label, addr4 + 3, 1);
584   struct in_addr in4;
585   int ret4 = inet_pton(AF_INET, addr4, &in4);
586   assert(ret4 == 1);
587   ASSERT_READ_LABEL(&in4, sizeof(in4), i_label);
588   assert(in4.s_addr == htonl(0x7f000001));
589 
590   char addr6[] = "::1";
591   dfsan_set_label(j_label, addr6 + 3, 1);
592   struct in6_addr in6;
593   int ret6 = inet_pton(AF_INET6, addr6, &in6);
594   assert(ret6 == 1);
595   ASSERT_READ_LABEL(((char *) &in6) + sizeof(in6) - 1, 1, j_label);
596 }
597 
test_localtime_r()598 void test_localtime_r() {
599   time_t t0 = 1384800998;
600   struct tm t1;
601   dfsan_set_label(i_label, &t0, sizeof(t0));
602   struct tm* ret = localtime_r(&t0, &t1);
603   assert(ret == &t1);
604   assert(t1.tm_min == 56);
605   ASSERT_LABEL(t1.tm_mon, i_label);
606 }
607 
test_getpwuid_r()608 void test_getpwuid_r() {
609   struct passwd pwd;
610   char buf[1024];
611   struct passwd *result;
612 
613   dfsan_set_label(i_label, &pwd, 4);
614   int ret = getpwuid_r(0, &pwd, buf, sizeof(buf), &result);
615   assert(ret == 0);
616   assert(strcmp(pwd.pw_name, "root") == 0);
617   assert(result == &pwd);
618   ASSERT_READ_ZERO_LABEL(&pwd, 4);
619 }
620 
test_poll()621 void test_poll() {
622   struct pollfd fd;
623   fd.fd = 0;
624   fd.events = POLLIN;
625   dfsan_set_label(i_label, &fd.revents, sizeof(fd.revents));
626   int ret = poll(&fd, 1, 1);
627   ASSERT_ZERO_LABEL(fd.revents);
628   assert(ret >= 0);
629 }
630 
test_select()631 void test_select() {
632   struct timeval t;
633   fd_set fds;
634   t.tv_sec = 2;
635   FD_SET(0, &fds);
636   dfsan_set_label(i_label, &fds, sizeof(fds));
637   dfsan_set_label(j_label, &t, sizeof(t));
638   int ret = select(1, &fds, NULL, NULL, &t);
639   assert(ret >= 0);
640   ASSERT_ZERO_LABEL(t.tv_sec);
641   ASSERT_READ_ZERO_LABEL(&fds, sizeof(fds));
642 }
643 
test_sched_getaffinity()644 void test_sched_getaffinity() {
645   cpu_set_t mask;
646   dfsan_set_label(j_label, &mask, 1);
647   int ret = sched_getaffinity(0, sizeof(mask), &mask);
648   assert(ret == 0);
649   ASSERT_READ_ZERO_LABEL(&mask, sizeof(mask));
650 }
651 
test_sigemptyset()652 void test_sigemptyset() {
653   sigset_t set;
654   dfsan_set_label(j_label, &set, 1);
655   int ret = sigemptyset(&set);
656   assert(ret == 0);
657   ASSERT_READ_ZERO_LABEL(&set, sizeof(set));
658 }
659 
test_sigaction()660 void test_sigaction() {
661   struct sigaction oldact;
662   dfsan_set_label(j_label, &oldact, 1);
663   int ret = sigaction(SIGUSR1, NULL, &oldact);
664   assert(ret == 0);
665   ASSERT_READ_ZERO_LABEL(&oldact, sizeof(oldact));
666 }
667 
test_gettimeofday()668 void test_gettimeofday() {
669   struct timeval tv;
670   struct timezone tz;
671   dfsan_set_label(i_label, &tv, sizeof(tv));
672   dfsan_set_label(j_label, &tz, sizeof(tz));
673   int ret = gettimeofday(&tv, &tz);
674   assert(ret == 0);
675   ASSERT_READ_ZERO_LABEL(&tv, sizeof(tv));
676   ASSERT_READ_ZERO_LABEL(&tz, sizeof(tz));
677 }
678 
pthread_create_test_cb(void * p)679 void *pthread_create_test_cb(void *p) {
680   assert(p == (void *)1);
681   ASSERT_ZERO_LABEL(p);
682   return (void *)2;
683 }
684 
test_pthread_create()685 void test_pthread_create() {
686   pthread_t pt;
687   pthread_create(&pt, 0, pthread_create_test_cb, (void *)1);
688   void *cbrv;
689   pthread_join(pt, &cbrv);
690   assert(cbrv == (void *)2);
691 }
692 
dl_iterate_phdr_test_cb(struct dl_phdr_info * info,size_t size,void * data)693 int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
694                             void *data) {
695   assert(data == (void *)3);
696   ASSERT_ZERO_LABEL(info);
697   ASSERT_ZERO_LABEL(size);
698   ASSERT_ZERO_LABEL(data);
699   return 0;
700 }
701 
test_dl_iterate_phdr()702 void test_dl_iterate_phdr() {
703   dl_iterate_phdr(dl_iterate_phdr_test_cb, (void *)3);
704 }
705 
test_strrchr()706 void test_strrchr() {
707   char str1[] = "str1str1";
708   dfsan_set_label(i_label, &str1[7], 1);
709 
710   char *rv = strrchr(str1, 'r');
711   assert(rv == &str1[6]);
712 #ifdef STRICT_DATA_DEPENDENCIES
713   ASSERT_ZERO_LABEL(rv);
714 #else
715   ASSERT_LABEL(rv, i_label);
716 #endif
717 }
718 
test_strstr()719 void test_strstr() {
720   char str1[] = "str1str1";
721   dfsan_set_label(i_label, &str1[3], 1);
722   dfsan_set_label(j_label, &str1[5], 1);
723 
724   char *rv = strstr(str1, "1s");
725   assert(rv == &str1[3]);
726 #ifdef STRICT_DATA_DEPENDENCIES
727   ASSERT_ZERO_LABEL(rv);
728 #else
729   ASSERT_LABEL(rv, i_label);
730 #endif
731 
732   rv = strstr(str1, "2s");
733   assert(rv == NULL);
734 #ifdef STRICT_DATA_DEPENDENCIES
735   ASSERT_ZERO_LABEL(rv);
736 #else
737   ASSERT_LABEL(rv, i_j_label);
738 #endif
739 }
740 
test_memchr()741 void test_memchr() {
742   char str1[] = "str1";
743   dfsan_set_label(i_label, &str1[3], 1);
744   dfsan_set_label(j_label, &str1[4], 1);
745 
746   char *crv = (char *) memchr(str1, 'r', sizeof(str1));
747   assert(crv == &str1[2]);
748   ASSERT_ZERO_LABEL(crv);
749 
750   crv = (char *) memchr(str1, '1', sizeof(str1));
751   assert(crv == &str1[3]);
752 #ifdef STRICT_DATA_DEPENDENCIES
753   ASSERT_ZERO_LABEL(crv);
754 #else
755   ASSERT_LABEL(crv, i_label);
756 #endif
757 
758   crv = (char *) memchr(str1, 'x', sizeof(str1));
759   assert(!crv);
760 #ifdef STRICT_DATA_DEPENDENCIES
761   ASSERT_ZERO_LABEL(crv);
762 #else
763   ASSERT_LABEL(crv, i_j_label);
764 #endif
765 }
766 
alarm_handler(int unused)767 void alarm_handler(int unused) {
768   ;
769 }
770 
test_nanosleep()771 void test_nanosleep() {
772   struct timespec req, rem;
773   req.tv_sec = 1;
774   req.tv_nsec = 0;
775   dfsan_set_label(i_label, &rem, sizeof(rem));
776 
777   // non interrupted
778   int rv = nanosleep(&req, &rem);
779   assert(rv == 0);
780   ASSERT_ZERO_LABEL(rv);
781   ASSERT_READ_LABEL(&rem, 1, i_label);
782 
783   // interrupted by an alarm
784   signal(SIGALRM, alarm_handler);
785   req.tv_sec = 3;
786   alarm(1);
787   rv = nanosleep(&req, &rem);
788   assert(rv == -1);
789   ASSERT_ZERO_LABEL(rv);
790   ASSERT_READ_ZERO_LABEL(&rem, sizeof(rem));
791 }
792 
test_socketpair()793 void test_socketpair() {
794   int fd[2];
795 
796   dfsan_set_label(i_label, fd, sizeof(fd));
797   int rv = socketpair(PF_LOCAL, SOCK_STREAM, 0, fd);
798   assert(rv == 0);
799   ASSERT_ZERO_LABEL(rv);
800   ASSERT_READ_ZERO_LABEL(fd, sizeof(fd));
801 }
802 
test_write()803 void test_write() {
804   int fd = open("/dev/null", O_WRONLY);
805 
806   char buf[] = "a string";
807   int len = strlen(buf);
808 
809   // The result of a write always unlabeled.
810   int res = write(fd, buf, len);
811   assert(res > 0);
812   ASSERT_ZERO_LABEL(res);
813 
814   // Label all arguments to write().
815   dfsan_set_label(i_label, &(buf[3]), 1);
816   dfsan_set_label(j_label, &fd, sizeof(fd));
817   dfsan_set_label(i_label, &len, sizeof(len));
818 
819   // The value returned by write() should have no label.
820   res = write(fd, buf, len);
821   ASSERT_ZERO_LABEL(res);
822 
823   close(fd);
824 }
825 
826 template <class T>
test_sprintf_chunk(const char * expected,const char * format,T arg)827 void test_sprintf_chunk(const char* expected, const char* format, T arg) {
828   char buf[512];
829   memset(buf, 'a', sizeof(buf));
830 
831   char padded_expected[512];
832   strcpy(padded_expected, "foo ");
833   strcat(padded_expected, expected);
834   strcat(padded_expected, " bar");
835 
836   char padded_format[512];
837   strcpy(padded_format, "foo ");
838   strcat(padded_format, format);
839   strcat(padded_format, " bar");
840 
841   // Non labelled arg.
842   assert(sprintf(buf, padded_format,  arg) == strlen(padded_expected));
843   assert(strcmp(buf, padded_expected) == 0);
844   ASSERT_READ_LABEL(buf, strlen(padded_expected), 0);
845   memset(buf, 'a', sizeof(buf));
846 
847   // Labelled arg.
848   dfsan_set_label(i_label, &arg, sizeof(arg));
849   assert(sprintf(buf, padded_format,  arg) == strlen(padded_expected));
850   assert(strcmp(buf, padded_expected) == 0);
851   ASSERT_READ_LABEL(buf, 4, 0);
852   ASSERT_READ_LABEL(buf + 4, strlen(padded_expected) - 8, i_label);
853   ASSERT_READ_LABEL(buf + (strlen(padded_expected) - 4), 4, 0);
854 }
855 
test_sprintf()856 void test_sprintf() {
857   char buf[2048];
858   memset(buf, 'a', sizeof(buf));
859 
860   // Test formatting (no conversion specifier).
861   assert(sprintf(buf, "Hello world!") == 12);
862   assert(strcmp(buf, "Hello world!") == 0);
863   ASSERT_READ_LABEL(buf, sizeof(buf), 0);
864 
865   // Test for extra arguments.
866   assert(sprintf(buf, "Hello world!", 42, "hello") == 12);
867   assert(strcmp(buf, "Hello world!") == 0);
868   ASSERT_READ_LABEL(buf, sizeof(buf), 0);
869 
870   // Test formatting & label propagation (multiple conversion specifiers): %s,
871   // %d, %n, %f, and %%.
872   const char* s = "world";
873   int m = 8;
874   int d = 27;
875   dfsan_set_label(k_label, (void *) (s + 1), 2);
876   dfsan_set_label(i_label, &m, sizeof(m));
877   dfsan_set_label(j_label, &d, sizeof(d));
878   int n;
879   int r = sprintf(buf, "hello %s, %-d/%d/%d %f %% %n%d", s, 2014, m, d,
880                   12345.6781234, &n, 1000);
881   assert(r == 42);
882   assert(strcmp(buf, "hello world, 2014/8/27 12345.678123 % 1000") == 0);
883   ASSERT_READ_LABEL(buf, 7, 0);
884   ASSERT_READ_LABEL(buf + 7, 2, k_label);
885   ASSERT_READ_LABEL(buf + 9, 9, 0);
886   ASSERT_READ_LABEL(buf + 18, 1, i_label);
887   ASSERT_READ_LABEL(buf + 19, 1, 0);
888   ASSERT_READ_LABEL(buf + 20, 2, j_label);
889   ASSERT_READ_LABEL(buf + 22, 15, 0);
890   ASSERT_LABEL(r, 0);
891   assert(n == 38);
892 
893   // Test formatting & label propagation (single conversion specifier, with
894   // additional length and precision modifiers).
895   test_sprintf_chunk("-559038737", "%d", 0xdeadbeef);
896   test_sprintf_chunk("3735928559", "%u", 0xdeadbeef);
897   test_sprintf_chunk("12345", "%i", 12345);
898   test_sprintf_chunk("751", "%o", 0751);
899   test_sprintf_chunk("babe", "%x", 0xbabe);
900   test_sprintf_chunk("0000BABE", "%.8X", 0xbabe);
901   test_sprintf_chunk("-17", "%hhd", 0xdeadbeef);
902   test_sprintf_chunk("-16657", "%hd", 0xdeadbeef);
903   test_sprintf_chunk("deadbeefdeadbeef", "%lx", 0xdeadbeefdeadbeef);
904   test_sprintf_chunk("0xdeadbeefdeadbeef", "%p",
905                  (void *)  0xdeadbeefdeadbeef);
906   test_sprintf_chunk("18446744073709551615", "%ju", (intmax_t) -1);
907   test_sprintf_chunk("18446744073709551615", "%zu", (size_t) -1);
908   test_sprintf_chunk("18446744073709551615", "%tu", (size_t) -1);
909 
910   test_sprintf_chunk("0x1.f9acffa7eb6bfp-4", "%a", 0.123456);
911   test_sprintf_chunk("0X1.F9ACFFA7EB6BFP-4", "%A", 0.123456);
912   test_sprintf_chunk("0.12346", "%.5f", 0.123456);
913   test_sprintf_chunk("0.123456", "%g", 0.123456);
914   test_sprintf_chunk("1.234560e-01", "%e", 0.123456);
915   test_sprintf_chunk("1.234560E-01", "%E", 0.123456);
916   test_sprintf_chunk("0.1234567891234560", "%.16Lf",
917                      (long double) 0.123456789123456);
918 
919   test_sprintf_chunk("z", "%c", 'z');
920 
921   // %n, %s, %d, %f, and %% already tested
922 
923   // Test formatting with width passed as an argument.
924   r = sprintf(buf, "hi %*d my %*s friend %.*f", 3, 1, 6, "dear", 4, 3.14159265359);
925   assert(r == 30);
926   assert(strcmp(buf, "hi   1 my   dear friend 3.1416") == 0);
927 }
928 
test_snprintf()929 void test_snprintf() {
930   char buf[2048];
931   memset(buf, 'a', sizeof(buf));
932   dfsan_set_label(0, buf, sizeof(buf));
933   const char* s = "world";
934   int y = 2014;
935   int m = 8;
936   int d = 27;
937   dfsan_set_label(k_label, (void *) (s + 1), 2);
938   dfsan_set_label(i_label, &y, sizeof(y));
939   dfsan_set_label(j_label, &m, sizeof(m));
940   int r = snprintf(buf, 19, "hello %s, %-d/%d/%d %f", s, y, m, d,
941                    12345.6781234);
942   // The return value is the number of bytes that would have been written to
943   // the final string if enough space had been available.
944   assert(r == 35);
945   assert(memcmp(buf, "hello world, 2014/", 19) == 0);
946   ASSERT_READ_LABEL(buf, 7, 0);
947   ASSERT_READ_LABEL(buf + 7, 2, k_label);
948   ASSERT_READ_LABEL(buf + 9, 4, 0);
949   ASSERT_READ_LABEL(buf + 13, 4, i_label);
950   ASSERT_READ_LABEL(buf + 17, 2, 0);
951   ASSERT_LABEL(r, 0);
952 }
953 
main(void)954 int main(void) {
955   i_label = dfsan_create_label("i", 0);
956   j_label = dfsan_create_label("j", 0);
957   k_label = dfsan_create_label("k", 0);
958   i_j_label = dfsan_union(i_label, j_label);
959 
960   test_calloc();
961   test_clock_gettime();
962   test_ctime_r();
963   test_dfsan_set_write_callback();
964   test_dl_iterate_phdr();
965   test_dlopen();
966   test_fgets();
967   test_fstat();
968   test_get_current_dir_name();
969   test_getcwd();
970   test_gethostname();
971   test_getpwuid_r();
972   test_getrlimit();
973   test_getrusage();
974   test_gettimeofday();
975   test_inet_pton();
976   test_localtime_r();
977   test_memchr();
978   test_memcmp();
979   test_memcpy();
980   test_memset();
981   test_nanosleep();
982   test_poll();
983   test_pread();
984   test_pthread_create();
985   test_read();
986   test_sched_getaffinity();
987   test_select();
988   test_sigaction();
989   test_sigemptyset();
990   test_snprintf();
991   test_socketpair();
992   test_sprintf();
993   test_stat();
994   test_strcasecmp();
995   test_strchr();
996   test_strcmp();
997   test_strcpy();
998   test_strdup();
999   test_strlen();
1000   test_strncasecmp();
1001   test_strncmp();
1002   test_strncpy();
1003   test_strrchr();
1004   test_strstr();
1005   test_strtod();
1006   test_strtol();
1007   test_strtoll();
1008   test_strtoul();
1009   test_strtoull();
1010   test_time();
1011   test_write();
1012 }
1013