Debian Codegraph @

clang/test/Analysis/taint-generic.c

raw
1 // RUN: %clang_analyze_cc1 -Wno-format-security -Wno-pointer-to-int-cast \
2 // RUN: -Wno-incompatible-library-redeclaration -verify %s \
3 // RUN: -analyzer-checker=alpha.security.taint \
4 // RUN: -analyzer-checker=core \
5 // RUN: -analyzer-checker=alpha.security.ArrayBoundV2 \
6 // RUN: -analyzer-checker=debug.ExprInspection \
7 // RUN: -analyzer-config \
8 // RUN: alpha.security.taint.TaintPropagation:Config=%S/Inputs/taint-generic-config.yaml
9
10 // RUN: %clang_analyze_cc1 -Wno-format-security -Wno-pointer-to-int-cast \
11 // RUN: -Wno-incompatible-library-redeclaration -verify %s \
12 // RUN: -DFILE_IS_STRUCT \
13 // RUN: -analyzer-checker=alpha.security.taint \
14 // RUN: -analyzer-checker=core \
15 // RUN: -analyzer-checker=alpha.security.ArrayBoundV2 \
16 // RUN: -analyzer-checker=debug.ExprInspection \
17 // RUN: -analyzer-config \
18 // RUN: alpha.security.taint.TaintPropagation:Config=%S/Inputs/taint-generic-config.yaml
19
20 // RUN: not %clang_analyze_cc1 -Wno-pointer-to-int-cast \
21 // RUN: -Wno-incompatible-library-redeclaration -verify %s \
22 // RUN: -analyzer-checker=alpha.security.taint \
23 // RUN: -analyzer-checker=debug.ExprInspection \
24 // RUN: -analyzer-config \
25 // RUN: alpha.security.taint.TaintPropagation:Config=justguessit \
26 // RUN: 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID-FILE
27
28 // CHECK-INVALID-FILE: (frontend): invalid input for checker option
29 // CHECK-INVALID-FILE-SAME: 'alpha.security.taint.TaintPropagation:Config',
30 // CHECK-INVALID-FILE-SAME: that expects a valid filename instead of
31 // CHECK-INVALID-FILE-SAME: 'justguessit'
32
33 // RUN: not %clang_analyze_cc1 -Wno-incompatible-library-redeclaration \
34 // RUN: -verify %s \
35 // RUN: -analyzer-checker=alpha.security.taint \
36 // RUN: -analyzer-checker=debug.ExprInspection \
37 // RUN: -analyzer-config \
38 // RUN: alpha.security.taint.TaintPropagation:Config=%S/Inputs/taint-generic-config-ill-formed.yaml \
39 // RUN: 2>&1 | FileCheck -DMSG=%errc_EINVAL %s -check-prefix=CHECK-ILL-FORMED
40
41 // CHECK-ILL-FORMED: (frontend): invalid input for checker option
42 // CHECK-ILL-FORMED-SAME: 'alpha.security.taint.TaintPropagation:Config',
43 // CHECK-ILL-FORMED-SAME: that expects a valid yaml file: [[MSG]]
44
45 // RUN: not %clang_analyze_cc1 -Wno-incompatible-library-redeclaration \
46 // RUN: -verify %s \
47 // RUN: -analyzer-checker=alpha.security.taint \
48 // RUN: -analyzer-checker=debug.ExprInspection \
49 // RUN: -analyzer-config \
50 // RUN: alpha.security.taint.TaintPropagation:Config=%S/Inputs/taint-generic-config-invalid-arg.yaml \
51 // RUN: 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID-ARG
52
53 // CHECK-INVALID-ARG: (frontend): invalid input for checker option
54 // CHECK-INVALID-ARG-SAME: 'alpha.security.taint.TaintPropagation:Config',
55 // CHECK-INVALID-ARG-SAME: that expects an argument number for propagation
56 // CHECK-INVALID-ARG-SAME: rules greater or equal to -1
57
58 typedef long long rsize_t;
59 typedef __typeof(sizeof(int)) size_t;
60 typedef signed long long ssize_t;
61 typedef __WCHAR_TYPE__ wchar_t;
62 void clang_analyzer_isTainted_char(char);
63 void clang_analyzer_isTainted_wchar(wchar_t);
64 void clang_analyzer_isTainted_charp(char*);
65 void clang_analyzer_isTainted_int(int);
66 void clang_analyzer_dump_int(int);
67
68 int coin();
69
70 int scanf(const char *restrict format, ...);
71 char *gets(char *str);
72 char *gets_s(char *str, rsize_t n);
73 int getchar(void);
74
75 typedef struct _FILE FILE;
76 #ifdef FILE_IS_STRUCT
77 extern struct _FILE *stdin;
78 #else
79 extern FILE *stdin;
80 #endif
81
82 #define bool _Bool
83 #define NULL (void*)0
84
85 wchar_t *fgetws(wchar_t *ws, int n, FILE *stream);
86 wchar_t *wmemset(wchar_t *wcs, wchar_t wc, unsigned long n);
87 wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, size_t n);
88 wchar_t *wmemmove(wchar_t *dest, const wchar_t *src, size_t n);
89 size_t wcslen(const wchar_t *s);
90 wchar_t *wcscpy(wchar_t * dest, const wchar_t * src);
91 wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t n);
92 wchar_t *wcscat(wchar_t *dest, const wchar_t *src);
93 wchar_t *wcsncat(wchar_t *dest,const wchar_t *src, size_t n);
94 int swprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, ...);
95
96 char *getenv(const char *name);
97
98 FILE *fopen(const char *name, const char *mode);
99
100 int fscanf(FILE *restrict stream, const char *restrict format, ...);
101 int sprintf(char *str, const char *format, ...);
102 void setproctitle(const char *fmt, ...);
103 void setproctitle_init(int argc, char *argv[], char *envp[]);
104
105 // Define string functions. Use builtin for some of them. They all default to
106 // the processing in the taint checker.
107 #define strcpy(dest, src) \
108 ((__builtin_object_size(dest, 0) != -1ULL) \
109 ? __builtin___strcpy_chk (dest, src, __builtin_object_size(dest, 1)) \
110 : __inline_strcpy_chk(dest, src))
111
112 static char *__inline_strcpy_chk (char *dest, const char *src) {
113 return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1));
114 }
115 char *stpcpy(char *restrict s1, const char *restrict s2);
116 char *strncpy( char * destination, const char * source, size_t num );
117 char *strndup(const char *s, size_t n);
118 char *strncat(char *restrict s1, const char *restrict s2, size_t n);
119
120 void *malloc(size_t);
121 void *calloc(size_t nmemb, size_t size);
122 void bcopy(void *s1, void *s2, size_t n);
123
124
125 // function | pathname | filename | fd | arglist | argv[] | envp[]
126 // ===============================================================
127 // 1 execl | X | | | X | |
128 // 2 execle | X | | | X | | X
129 // 3 execlp | | X | | X | |
130 // 4 execv | X | | | | X |
131 // 5 execve | X | | | | X | X
132 // 6 execvp | | X | | | X |
133 // 7 execvpe | | X | | | X | X
134 // 8 fexecve | | | X | | X | X
135 // ===============================================================
136 // letter | | p | f | l | v | e
137 //
138 // legend:
139 // - pathname: rel/abs path to the binary
140 // - filename: file name searched in PATH to execute the binary
141 // - fd: accepts a file descriptor
142 // - arglist: accepts variadic arguments
143 // - argv: accepts a pointer to array, denoting the new argv
144 // - envp: accepts a pointer to array, denoting the new envp
145
146 int execl(const char *path, const char *arg, ...);
147 int execle(const char *path, const char *arg, ...);
148 int execlp(const char *file, const char *arg, ...);
149 int execv(const char *path, char *const argv[]);
150 int execve(const char *path, char *const argv[], char *const envp[]);
151 int execvp(const char *file, char *const argv[]);
152 int execvpe(const char *file, char *const argv[], char *const envp[]);
153 int fexecve(int fd, char *const argv[], char *const envp[]);
154 FILE *popen(const char *command, const char *type);
155 int pclose(FILE *stream);
156 int system(const char *command);
157
158
159 typedef size_t socklen_t;
160
161 struct sockaddr {
162 unsigned short sa_family;
163 char sa_data[14];
164 };
165
166 #define BUFSIZE 10
167
168 int Buffer[BUFSIZE];
169 void bufferScanfDirect(void)
170 {
171 int n;
172 scanf("%d", &n);
173 Buffer[n] = 1; // expected-warning {{Potential out of bound access }}
174 }
175
176 void bufferScanfArithmetic1(int x) {
177 int n;
178 scanf("%d", &n);
179 int m = (n - 3);
180 Buffer[m] = 1; // expected-warning {{Potential out of bound access }}
181 }
182
183 void bufferScanfArithmetic2(int x) {
184 int n;
185 scanf("%d", &n);
186 int m = 100 - (n + 3) * x;
187 Buffer[m] = 1; // expected-warning {{Potential out of bound access }}
188 }
189
190 void bufferScanfAssignment(int x) {
191 int n;
192 scanf("%d", &n);
193 int m;
194 if (x > 0) {
195 m = n;
196 Buffer[m] = 1; // expected-warning {{Potential out of bound access }}
197 }
198 }
199
200 void scanfArg(void) {
201 int t = 0;
202 scanf("%d", t); // expected-warning {{format specifies type 'int *' but the argument has type 'int'}}
203 }
204
205 void bufferGetchar(int x) {
206 int m = getchar();
207 Buffer[m] = 1; //expected-warning {{Potential out of bound access}}
208 }
209
210 extern const unsigned short int **__ctype_b_loc (void);
211 enum { _ISdigit = 2048 };
212 # define isdigit(c) ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) _ISdigit)
213
214 int isdigitImplFalsePositive(void) {
215 // If this code no longer produces a bug report, then consider removing the
216 // special case that disables buffer overflow reports coming from the isXXXXX
217 // macros in ctypes.h.
218 int c = getchar();
219 return ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) _ISdigit);
220 //expected-warning@-1 {{Potential out of bound access}}
221 }
222
223 int isdigitSuppressed(void) {
224 // Same code as above, but reports are suppressed based on macro name:
225 int c = getchar();
226 return isdigit(c); //no-warning
227 }
228
229 // Some later tests use isdigit as a function, so we need to undef it:
230 #undef isdigit
231
232 void testUncontrolledFormatString(char **p) {
233 char s[80];
234 fscanf(stdin, "%s", s);
235 char buf[128];
236 sprintf(buf,s); // expected-warning {{Uncontrolled Format String}}
237 setproctitle(s, 3); // expected-warning {{Uncontrolled Format String}}
238
239 // Test taint propagation through strcpy and family.
240 char scpy[80];
241 strcpy(scpy, s);
242 sprintf(buf,scpy); // expected-warning {{Uncontrolled Format String}}
243
244 stpcpy(*(++p), s); // this generates __inline.
245 setproctitle(*(p), 3); // expected-warning {{Uncontrolled Format String}}
246
247 char spcpy[80];
248 stpcpy(spcpy, s);
249 setproctitle(spcpy, 3); // expected-warning {{Uncontrolled Format String}}
250
251 char *spcpyret;
252 spcpyret = stpcpy(spcpy, s);
253 setproctitle(spcpyret, 3); // expected-warning {{Uncontrolled Format String}}
254
255 char sncpy[80];
256 strncpy(sncpy, s, 20);
257 setproctitle(sncpy, 3); // expected-warning {{Uncontrolled Format String}}
258
259 char *dup;
260 dup = strndup(s, 20);
261 setproctitle(dup, 3); // expected-warning {{Uncontrolled Format String}}
262
263 }
264
265 void testTaintSystemCall(void) {
266 char buffer[156];
267 char addr[128];
268 scanf("%s", addr);
269 system(addr); // expected-warning {{Untrusted data is passed to a system call}}
270
271 // Test that spintf transfers taint.
272 sprintf(buffer, "/bin/mail %s < /tmp/email", addr);
273 system(buffer); // expected-warning {{Untrusted data is passed to a system call}}
274 }
275
276 void testTaintSystemCall2(void) {
277 // Test that snpintf transfers taint.
278 char buffern[156];
279 char addr[128];
280 scanf("%s", addr);
281 __builtin_snprintf(buffern, 10, "/bin/mail %s < /tmp/email", addr);
282 // expected-warning@-1 {{'snprintf' will always be truncated; specified size is 10, but format string expands to at least 24}}
283 system(buffern); // expected-warning {{Untrusted data is passed to a system call}}
284 }
285
286 void testTaintSystemCall3(void) {
287 char buffern2[156];
288 int numt;
289 char addr[128];
290 scanf("%s %d", addr, &numt);
291 __builtin_snprintf(buffern2, numt, "/bin/mail %s < /tmp/email", "abcd");
292 system(buffern2); // expected-warning {{Untrusted data is passed to a system call}}
293 }
294
295 void testGets(void) {
296 char str[50];
297 gets(str);
298 system(str); // expected-warning {{Untrusted data is passed to a system call}}
299 }
300
301 void testGets_s(void) {
302 char str[50];
303 gets_s(str, 49);
304 system(str); // expected-warning {{Untrusted data is passed to a system call}}
305 }
306
307 void testTaintedBufferSize(void) {
308 size_t ts;
309 // The functions malloc, calloc, bcopy and memcpy are not taint sinks in the
310 // default config of GenericTaintChecker (because that would cause too many
311 // false positives).
312 // FIXME: We should generate warnings when a value passed to these functions
313 // is tainted and _can be very large_ (because that's exploitable). This
314 // functionality probably belongs to the checkers that do more detailed
315 // modeling of these functions (MallocChecker and CStringChecker).
316 scanf("%zd", &ts);
317 int *buf1 = (int*)malloc(ts*sizeof(int)); // warn here, ts is unbounded and tainted
318 char *dst = (char*)calloc(ts, sizeof(char)); // warn here, ts is unbounded tainted
319 bcopy(buf1, dst, ts); // no warning here, since the size of buf1, dst equals ts. Cannot overflow.
320 __builtin_memcpy(dst, buf1, (ts + 4)*sizeof(char)); // warn here, dst overflows (whatever the value of ts)
321
322 // If both buffers are trusted, do not issue a warning.
323 char *dst2 = (char*)malloc(ts*sizeof(char)); // warn here, ts in unbounded
324 strncat(dst2, dst, ts); // no-warning
325 }
326
327 #define AF_UNIX 1 /* local to host (pipes) */
328 #define AF_INET 2 /* internetwork: UDP, TCP, etc. */
329 #define AF_LOCAL AF_UNIX /* backward compatibility */
330 #define SOCK_STREAM 1
331 int socket(int, int, int);
332 size_t read(int, void *, size_t);
333
334 void testSocket(void) {
335 int sock;
336 char buffer[100];
337
338 sock = socket(AF_INET, SOCK_STREAM, 0);
339 read(sock, buffer, 100);
340 execl(buffer, "filename", 0); // expected-warning {{Untrusted data is passed to a system call}}
341
342 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
343 read(sock, buffer, 100);
344 execl(buffer, "filename", 0); // no-warning
345
346 sock = socket(AF_INET, SOCK_STREAM, 0);
347 // References to both buffer and &buffer as an argument should taint the argument
348 read(sock, &buffer, 100);
349 execl(buffer, "filename", 0); // expected-warning {{Untrusted data is passed to a system call}}
350 }
351
352 void testStruct(void) {
353 struct {
354 char buf[16];
355 int length;
356 } tainted;
357
358 char buffer[16];
359 int sock;
360
361 sock = socket(AF_INET, SOCK_STREAM, 0);
362 read(sock, &tainted, sizeof(tainted));
363 clang_analyzer_isTainted_int(tainted.length); // expected-warning {{YES }}
364 }
365
366 void testStructArray(void) {
367 struct {
368 int length;
369 } tainted[4];
370
371 char dstbuf[16], srcbuf[16];
372 int sock;
373
374 sock = socket(AF_INET, SOCK_STREAM, 0);
375 __builtin_memset(srcbuf, 0, sizeof(srcbuf));
376
377 read(sock, &tainted[0], sizeof(tainted));
378 clang_analyzer_isTainted_int(tainted[0].length); // expected-warning {{YES}}
379
380 __builtin_memset(&tainted, 0, sizeof(tainted));
381 read(sock, &tainted, sizeof(tainted));
382 clang_analyzer_isTainted_int(tainted[0].length); // expected-warning {{YES}}
383
384 __builtin_memset(&tainted, 0, sizeof(tainted));
385 // If we taint element 1, we should not raise an alert on taint for element 0 or element 2
386 read(sock, &tainted[1], sizeof(tainted));
387 clang_analyzer_isTainted_int(tainted[0].length); // expected-warning {{NO}}
388 clang_analyzer_isTainted_int(tainted[2].length); // expected-warning {{NO}}
389 }
390
391 void testUnion(void) {
392 union {
393 int x;
394 char y[4];
395 } tainted;
396
397 char buffer[4];
398
399 int sock = socket(AF_INET, SOCK_STREAM, 0);
400 read(sock, &tainted.y, sizeof(tainted.y));
401 // FIXME: overlapping regions aren't detected by isTainted yet
402 __builtin_memcpy(buffer, tainted.y, tainted.x);
403 }
404
405 int testDivByZero(void) {
406 int x;
407 scanf("%d", &x);
408 return 5/x; // expected-warning {{Division by a tainted value, possibly zero}}
409 }
410
411 // Zero-sized VLAs.
412 void testTaintedVLASize(void) {
413 int x;
414 scanf("%d", &x);
415 int vla[x]; // expected-warning{{Declared variable-length array (VLA) has tainted (attacker controlled) size that can be 0 or negative}}
416 }
417
418 // Tainted-sanitized VLAs.
419 void testTaintedSanitizedVLASize(void) {
420 int x;
421 scanf("%d", &x);
422 if (x<1)
423 return;
424 int vla[x]; // no-warning
425 }
426
427 int testTaintedAllocaMem() {
428 char x;
429 void * p;
430 scanf("%c", &x);
431 p = __builtin_alloca(1);
432 __builtin_memcpy(p, &x, 1);
433 return 5 / *(char*)p; // expected-warning {{Division by a tainted value, possibly zero}}
434 }
435
436 int testTaintedMallocMem() {
437 char x;
438 void * p;
439 scanf("%c", &x);
440 p = malloc(1);
441 __builtin_memcpy(p, &x, 1);
442 return 5 / *(char*)p; // expected-warning {{Division by a tainted value, possibly zero}}
443 }
444
445
446 // This computation used to take a very long time.
447 #define longcmp(a,b,c) { \
448 a -= c; a ^= c; c += b; b -= a; b ^= (a<<6) | (a >> (32-b)); a += c; c -= b; c ^= b; b += a; \
449 a -= c; a ^= c; c += b; b -= a; b ^= a; a += c; c -= b; c ^= b; b += a; }
450
451 unsigned radar11369570_hanging(const unsigned char *arr, int l) {
452 unsigned a, b, c;
453 a = b = c = 0x9899e3 + l;
454 while (l >= 6) {
455 unsigned t;
456 scanf("%d", &t);
457 a += b;
458 a ^= a;
459 a += (arr[3] + ((unsigned) arr[2] << 8) + ((unsigned) arr[1] << 16) + ((unsigned) arr[0] << 24));
460 longcmp(a, t, c);
461 l -= 12;
462 }
463 return 5/a; // FIXME: Should be a "div by tainted" warning here.
464 }
465
466 // This computation used to take a very long time.
467 void complex_taint_queries(const int *p) {
468 int tainted = 0;
469 scanf("%d", &tainted);
470
471 // Make "tmp" tainted.
472 int tmp = tainted + tainted;
473 clang_analyzer_isTainted_int(tmp); // expected-warning{{YES}}
474
475 // Make "tmp" SymExpr a lot more complicated by applying computation.
476 // This should balloon the symbol complexity.
477 tmp += p[0] + p[0];
478 tmp += p[1] + p[1];
479 tmp += p[2] + p[2];
480 clang_analyzer_dump_int(tmp); // expected-warning{{((((conj_}} symbol complexity: 8
481 clang_analyzer_isTainted_int(tmp); // expected-warning{{YES}}
482
483 tmp += p[3] + p[3];
484 clang_analyzer_dump_int(tmp); // expected-warning{{(((((conj_}} symbol complexity: 10
485 clang_analyzer_isTainted_int(tmp); // expected-warning{{NO}} 10 is already too complex to be traversed
486
487 tmp += p[4] + p[4];
488 tmp += p[5] + p[5];
489 tmp += p[6] + p[6];
490 tmp += p[7] + p[7];
491 tmp += p[8] + p[8];
492 tmp += p[9] + p[9];
493 tmp += p[10] + p[10];
494 tmp += p[11] + p[11];
495 tmp += p[12] + p[12];
496 tmp += p[13] + p[13];
497 tmp += p[14] + p[14];
498 tmp += p[15] + p[15];
499
500 // The SymExpr still holds the full history of the computation, yet, "isTainted" doesn't traverse the tree as the complexity is over the threshold.
501 clang_analyzer_dump_int(tmp);
502 // expected-warning@-1{{(((((((((((((((((conj_}} symbol complexity: 34
503 clang_analyzer_isTainted_int(tmp); // expected-warning{{NO}} FIXME: Ideally, this should still result in "tainted".
504
505 // By making it even one step more complex, then it would hit the "max-symbol-complexity"
506 // threshold and the engine would cut the SymExpr and replace it by a new conjured symbol.
507 tmp += p[16];
508 clang_analyzer_dump_int(tmp); // expected-warning{{conj_}} symbol complexity: 1
509 clang_analyzer_isTainted_int(tmp); // expected-warning{{NO}}
510 }
511
512 // Check that we do not assert of the following code.
513 int SymSymExprWithDiffTypes(void* p) {
514 int i;
515 scanf("%d", &i);
516 int j = (i % (int)(long)p);
517 return 5/j; // expected-warning {{Division by a tainted value, possibly zero}}
518 }
519
520
521 void constraintManagerShouldTreatAsOpaque(int rhs) {
522 int i;
523 scanf("%d", &i);
524 // This comparison used to hit an assertion in the constraint manager,
525 // which didn't handle NonLoc sym-sym comparisons.
526 if (i < rhs)
527 return;
528 if (i < rhs)
529 *(volatile int *) 0; // no-warning
530 }
531
532 int testSprintf_is_not_a_source(char *buf, char *msg) {
533 int x = sprintf(buf, "%s", msg); // no-warning
534 return 1 / x; // no-warning: 'sprintf' is not a taint source
535 }
536
537 int testSprintf_propagates_taint(char *buf, char *msg) {
538 scanf("%s", msg);
539 int x = sprintf(buf, "%s", msg); // propagate taint!
540 return 1 / x; // expected-warning {{Division by a tainted value, possibly zero}}
541 }
542
543 void test_wchar_apis_dont_propagate(const char *path) {
544 // strlen, wcslen, strnlen and alike intentionally don't propagate taint.
545 // See the details here: https://github.com/llvm/llvm-project/pull/66086
546 // This isn't ideal, but this is only what we have now.
547
548 FILE *f = fopen(path, "r");
549 clang_analyzer_isTainted_charp((char*)f); // expected-warning {{YES}}
550 wchar_t wbuf[10];
551 fgetws(wbuf, sizeof(wbuf)/sizeof(*wbuf), f);
552 clang_analyzer_isTainted_wchar(*wbuf); // expected-warning {{YES}}
553 int n = wcslen(wbuf);
554 clang_analyzer_isTainted_int(n); // expected-warning {{NO}}
555
556 wchar_t dst[100] = L"ABC";
557 clang_analyzer_isTainted_wchar(*dst); // expected-warning {{NO}}
558 wcsncat(dst, wbuf, sizeof(wbuf)/sizeof(*wbuf));
559 clang_analyzer_isTainted_wchar(*dst); // expected-warning {{YES}}
560
561 int m = wcslen(dst);
562 clang_analyzer_isTainted_int(m); // expected-warning {{NO}}
563 }
564
565 int scanf_s(const char *format, ...);
566 int testScanf_s_(int *out) {
567 scanf_s("%d", out);
568 return 1 / *out; // expected-warning {{Division by a tainted value, possibly zero}}
569 }
570
571 #define _IO_FILE FILE
572 int _IO_getc(_IO_FILE *__fp);
573 int testUnderscoreIO_getc(_IO_FILE *fp) {
574 char c = _IO_getc(fp);
575 return 1 / c; // expected-warning {{Division by a tainted value, possibly zero}}
576 }
577
578 char *getcwd(char *buf, size_t size);
579 int testGetcwd(char *buf, size_t size) {
580 char *c = getcwd(buf, size);
581 return system(c); // expected-warning {{Untrusted data is passed to a system call}}
582 }
583
584 char *getwd(char *buf);
585 int testGetwd(char *buf) {
586 char *c = getwd(buf);
587 return system(c); // expected-warning {{Untrusted data is passed to a system call}}
588 }
589
590 ssize_t readlink(const char *path, char *buf, size_t bufsiz);
591 int testReadlink(char *path, char *buf, size_t bufsiz) {
592 ssize_t s = readlink(path, buf, bufsiz);
593 system(buf); // expected-warning {{Untrusted data is passed to a system call}}
594 // readlink never returns 0
595 return 1 / (s + 1); // expected-warning {{Division by a tainted value, possibly zero}}
596 }
597
598 ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
599 int testReadlinkat(int dirfd, char *path, char *buf, size_t bufsiz) {
600 ssize_t s = readlinkat(dirfd, path, buf, bufsiz);
601 system(buf); // expected-warning {{Untrusted data is passed to a system call}}
602 (void)(1 / dirfd); // arg 0 is not tainted
603 system(path); // arg 1 is not tainted
604 (void)(1 / bufsiz); // arg 3 is not tainted
605 // readlinkat never returns 0
606 return 1 / (s + 1); // expected-warning {{Division by a tainted value, possibly zero}}
607 }
608
609 char *get_current_dir_name(void);
610 int testGet_current_dir_name() {
611 char *d = get_current_dir_name();
612 return system(d); // expected-warning {{Untrusted data is passed to a system call}}
613 }
614
615 int gethostname(char *name, size_t len);
616 int testGethostname(char *name, size_t len) {
617 gethostname(name, len);
618 return system(name); // expected-warning {{Untrusted data is passed to a system call}}
619 }
620
621 int getnameinfo(const struct sockaddr *restrict addr, socklen_t addrlen,
622 char *restrict host, socklen_t hostlen,
623 char *restrict serv, socklen_t servlen, int flags);
624 int testGetnameinfo(const struct sockaddr *restrict addr, socklen_t addrlen,
625 char *restrict host, socklen_t hostlen,
626 char *restrict serv, socklen_t servlen, int flags) {
627 getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
628
629 system(host); // expected-warning {{Untrusted data is passed to a system call}}
630 return system(serv); // expected-warning {{Untrusted data is passed to a system call}}
631 }
632
633 int getseuserbyname(const char *linuxuser, char **selinuxuser, char **level);
634 int testGetseuserbyname(const char *linuxuser, char **selinuxuser, char **level) {
635 getseuserbyname(linuxuser, selinuxuser, level);
636 system(selinuxuser[0]); // expected-warning {{Untrusted data is passed to a system call}}
637 return system(level[0]); // expected-warning {{Untrusted data is passed to a system call}}
638 }
639
640 typedef int gid_t;
641 int getgroups(int size, gid_t list[]);
642 int testGetgroups(int size, gid_t list[], bool flag) {
643 int result = getgroups(size, list);
644 if (flag)
645 return 1 / list[0]; // expected-warning {{Division by a tainted value, possibly zero}}
646
647 return 1 / (result + 1); // expected-warning {{Division by a tainted value, possibly zero}}
648 }
649
650 char *getlogin(void);
651 int testGetlogin() {
652 char *n = getlogin();
653 return system(n); // expected-warning {{Untrusted data is passed to a system call}}
654 }
655
656 int getlogin_r(char *buf, size_t bufsize);
657 int testGetlogin_r(char *buf, size_t bufsize) {
658 getlogin_r(buf, bufsize);
659 return system(buf); // expected-warning {{Untrusted data is passed to a system call}}
660 }
661
662 int fscanf_s(FILE *stream, const char *format, ...);
663 void testFscanf_s(const char *fname, int *d) {
664 FILE *f = fopen(fname, "r");
665 fscanf_s(f, "%d", d);
666 clang_analyzer_isTainted_int(*d); // expected-warning {{YES}}
667 }
668
669 int fread(void *buffer, size_t size, size_t count, FILE *stream);
670 void testFread(const char *fname, int *buffer, size_t size, size_t count) {
671 FILE *f = fopen(fname, "r");
672 size_t read = fread(buffer, size, count, f);
673
674 clang_analyzer_isTainted_int(*buffer); // expected-warning {{YES}}
675 clang_analyzer_isTainted_int(read); // expected-warning {{YES}}
676 }
677
678 ssize_t recv(int sockfd, void *buf, size_t len, int flags);
679 int accept(int fd, struct sockaddr *addr, socklen_t *addrlen);
680 int bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
681 int listen(int fd, int backlog);
682
683 void testRecv(int *buf, size_t len, int flags) {
684 int fd;
685 scanf("%d", &fd); // fake a tainted a file descriptor
686
687 size_t read = recv(fd, buf, len, flags);
688 clang_analyzer_isTainted_int(*buf); // expected-warning {{YES}}
689 clang_analyzer_isTainted_int(read); // expected-warning {{YES}}
690 }
691
692 ssize_t recvfrom(int sockfd, void *restrict buf, size_t len, int flags,
693 struct sockaddr *restrict src_addr,
694 socklen_t *restrict addrlen);
695 void testRecvfrom(int *restrict buf, size_t len, int flags,
696 struct sockaddr *restrict src_addr,
697 socklen_t *restrict addrlen) {
698 int fd;
699 scanf("%d", &fd); // fake a tainted a file descriptor
700
701 size_t read = recvfrom(fd, buf, len, flags, src_addr, addrlen);
702 clang_analyzer_isTainted_int(*buf); // expected-warning {{YES}}
703 clang_analyzer_isTainted_int(read); // expected-warning {{YES}}
704 }
705
706 char *ttyname(int fd);
707 void testTtyname() {
708 int fd;
709 scanf("%d", &fd); // fake a tainted a file descriptor
710
711 char *name = ttyname(fd);
712 clang_analyzer_isTainted_charp(name); // expected-warning {{YES}}
713 }
714
715 int ttyname_r(int fd, char *buf, size_t buflen);
716 void testTtyname_r(char *buf, size_t buflen) {
717 int fd;
718 scanf("%d", &fd); // fake a tainted a file descriptor
719
720 int result = ttyname_r(fd, buf, buflen);
721 clang_analyzer_isTainted_char(*buf); // expected-warning {{YES}}
722 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
723 }
724
725 char *dirname(char *path);
726 void testDirname() {
727 char buf[10];
728 scanf("%9s", buf);
729
730 char *name = dirname(buf);
731 clang_analyzer_isTainted_charp(name); // expected-warning {{YES}}
732 }
733
734 char *basename(char *path);
735 void testBasename() {
736 char buf[10];
737 scanf("%9s", buf);
738
739 char *name = basename(buf);
740 clang_analyzer_isTainted_charp(name); // expected-warning {{YES}}
741 }
742
743 int fnmatch(const char *pattern, const char *string, int flags);
744 void testFnmatch(const char *pattern, int flags) {
745 char string[10];
746 scanf("%9s", string);
747
748 int result = fnmatch(pattern, string, flags);
749 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
750 }
751
752 void *memchr(const void *s, int c, size_t n);
753 void testMemchr(int c, size_t n) {
754 char buf[10];
755 scanf("%9s", buf);
756
757 char *result = memchr(buf, c, n);
758 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
759 }
760
761 void *memrchr(const void *s, int c, size_t n);
762 void testMemrchr(int c, size_t n) {
763 char buf[10];
764 scanf("%9s", buf);
765
766 char *result = memrchr(buf, c, n);
767 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
768 }
769
770 void *rawmemchr(const void *s, int c);
771 void testRawmemchr(int c) {
772 char buf[10];
773 scanf("%9s", buf);
774
775 char *result = rawmemchr(buf, c);
776 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
777 }
778
779 int mbtowc(wchar_t *pwc, const char *s, size_t n);
780 void testMbtowc(wchar_t *pwc, size_t n) {
781 char buf[10];
782 scanf("%9s", buf);
783
784 int result = mbtowc(pwc, buf, n);
785 clang_analyzer_isTainted_char(*pwc); // expected-warning {{YES}}
786 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
787 }
788
789 int wctomb(char *s, wchar_t wc);
790 void testWctomb(char *buf) {
791 wchar_t wc = getchar();
792
793 int result = wctomb(buf, wc);
794 clang_analyzer_isTainted_char(*buf); // expected-warning {{YES}}
795 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
796 }
797
798 int wcwidth(wchar_t c);
799 void testWcwidth() {
800 wchar_t wc = getchar();
801
802 int width = wcwidth(wc);
803 clang_analyzer_isTainted_int(width); // expected-warning {{YES}}
804 }
805
806 int memcmp(const void *s1, const void *s2, size_t n);
807 void testMemcmpWithLHSTainted(size_t n, char *rhs) {
808 char lhs[10];
809 scanf("%9s", lhs);
810
811 int cmp_result = memcmp(lhs, rhs, n);
812 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
813 }
814
815 void testMemcmpWithRHSTainted(size_t n, char *lhs) {
816 char rhs[10];
817 scanf("%9s", rhs);
818
819 int cmp_result = memcmp(lhs, rhs, n);
820 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
821 }
822
823 void *memcpy(void *restrict dest, const void *restrict src, size_t n);
824 void testMemcpy(char *dst, size_t n) {
825 char src[10];
826 scanf("%9s", src);
827
828 char *result = memcpy(dst, src, n);
829
830 clang_analyzer_isTainted_char(*dst); // expected-warning {{YES}}
831 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
832 }
833
834 void *memmove(void *dest, const void *src, size_t n);
835 void testMemmove(char *dst, size_t n) {
836 char src[10];
837 scanf("%9s", src);
838
839 char *result = memmove(dst, src, n);
840
841 clang_analyzer_isTainted_char(*dst); // expected-warning {{YES}}
842 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
843 }
844
845 void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
846 void testMemmem(const void *needle, size_t needlelen) {
847 char haystack[10];
848 scanf("%9s", haystack);
849
850 char *result = memmem(haystack, 9, needle, needlelen);
851 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
852 }
853
854 char *strstr(const char *haystack, const char *needle);
855 void testStrstr(const char *needle) {
856 char haystack[10];
857 scanf("%9s", haystack);
858
859 char *result = strstr(haystack, needle);
860 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
861 }
862
863 char *strcasestr(const char *haystack, const char *needle);
864 void testStrcasestr(const char *needle) {
865 char haystack[10];
866 scanf("%9s", haystack);
867
868 char *result = strcasestr(haystack, needle);
869 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
870 }
871
872 char *strchrnul(const char *s, int c);
873 void testStrchrnul() {
874 char s[10];
875 scanf("%9s", s);
876
877 char *result = strchrnul(s, 9);
878 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
879 }
880
881 char *index(const char *s, int c);
882 void testIndex() {
883 char s[10];
884 scanf("%9s", s);
885
886 char *result = index(s, 9);
887 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
888 }
889
890 char *rindex(const char *s, int c);
891 void testRindex() {
892 char s[10];
893 scanf("%9s", s);
894
895 char *result = rindex(s, 9);
896 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
897 }
898
899 int strcmp(const char *s1, const char *s2);
900 void testStrcmpWithLHSTainted(char *rhs) {
901 char lhs[10];
902 scanf("%9s", lhs);
903
904 int cmp_result = strcmp(lhs, rhs);
905 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
906 }
907
908 void testStrcmpWithRHSTainted(char *lhs) {
909 char rhs[10];
910 scanf("%9s", rhs);
911
912 int cmp_result = strcmp(lhs, rhs);
913 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
914 }
915 int strcasecmp(const char *s1, const char *s2);
916 void testStrcasecmpWithLHSTainted(char *rhs) {
917 char lhs[10];
918 scanf("%9s", lhs);
919
920 int cmp_result = strcasecmp(lhs, rhs);
921 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
922 }
923
924 void testStrcasecmpWithRHSTainted(char *lhs) {
925 char rhs[10];
926 scanf("%9s", rhs);
927
928 int cmp_result = strcasecmp(lhs, rhs);
929 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
930 }
931 int strncmp(const char *s1, const char *s2, size_t n);
932 void testStrncmpWithLHSTainted(char *rhs, size_t n) {
933 char lhs[10];
934 scanf("%9s", lhs);
935
936 int cmp_result = strncmp(lhs, rhs, n);
937 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
938 }
939
940 void testStrncmpWithRHSTainted(char *lhs, size_t n) {
941 char rhs[10];
942 scanf("%9s", rhs);
943
944 int cmp_result = strncmp(lhs, rhs, n);
945 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
946 }
947
948 void testStrncmpWithNTainted(char *lhs, char *rhs) {
949 int n;
950 scanf("%d", &n);
951
952 int cmp_result = strncmp(lhs, rhs, n);
953 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
954 }
955
956 int strncasecmp(const char *s1, const char *s2, size_t n);
957 void testStrncasecmpWithLHSTainted(char *rhs, size_t n) {
958 char lhs[10];
959 scanf("%9s", lhs);
960
961 int cmp_result = strncmp(lhs, rhs, n);
962 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
963 }
964
965 void testStrncasecmpWithRHSTainted(char *lhs, size_t n) {
966 char rhs[10];
967 scanf("%9s", rhs);
968
969 int cmp_result = strncmp(lhs, rhs, n);
970 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
971 }
972
973 void testStrncasecmpWithNTainted(char *lhs, char *rhs) {
974 int n;
975 scanf("%d", &n);
976
977 int cmp_result = strncmp(lhs, rhs, n);
978 clang_analyzer_isTainted_int(cmp_result); // expected-warning {{YES}}
979 }
980
981 size_t strspn(const char *s, const char *accept);
982 void testStrspnFirstArgTainted(const char *accept) {
983 char s[10];
984 scanf("%9s", s);
985
986 size_t result = strspn(s, accept);
987 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
988 }
989
990 void testStrspnSecondArgTainted(const char *s) {
991 char accept[10];
992 scanf("%9s", accept);
993
994 size_t result = strspn(s, accept);
995 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
996 }
997
998 size_t strcspn(const char *s, const char *reject);
999 void testStrcspnFirstArgTainted(const char *reject) {
1000 char s[10];
1001 scanf("%9s", s);
1002
1003 size_t result = strcspn(s, reject);
1004 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
1005 }
1006
1007 void testStrcspnSecondArgTainted(const char *s) {
1008 char reject[10];
1009 scanf("%9s", reject);
1010
1011 size_t result = strcspn(s, reject);
1012 clang_analyzer_isTainted_int(result); // expected-warning {{YES}}
1013 }
1014
1015 char *strpbrk(const char *s, const char *accept);
1016 void testStrpbrk(const char *accept) {
1017 char s[10];
1018 scanf("%9s", s);
1019
1020 char *result = strpbrk(s, accept);
1021 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
1022 }
1023
1024 char *strndup(const char *s, size_t n);
1025 void testStrndup(size_t n) {
1026 char s[10];
1027 scanf("%9s", s);
1028
1029 char *result = strndup(s, n);
1030 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
1031 }
1032
1033 char *strdupa(const char *s);
1034 void testStrdupa() {
1035 char s[10];
1036 scanf("%9s", s);
1037
1038 char *result = strdupa(s);
1039 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
1040 }
1041
1042 char *strndupa(const char *s, size_t n);
1043 void testStrndupa(size_t n) {
1044 char s[10];
1045 scanf("%9s", s);
1046
1047 char *result = strndupa(s, n);
1048 clang_analyzer_isTainted_charp(result); // expected-warning {{YES}}
1049 }
1050
1051 size_t strlen(const char *s);
1052 void testStrlen_dont_propagate() {
1053 // strlen, wcslen, strnlen and alike intentionally don't propagate taint.
1054 // See the details here: https://github.com/llvm/llvm-project/pull/66086
1055 // This isn't ideal, but this is only what we have now.
1056 char s[10];
1057 scanf("%9s", s);
1058
1059 size_t result = strlen(s);
1060 // strlen propagating taint would bring in many false positives
1061 clang_analyzer_isTainted_int(result); // expected-warning {{NO}}
1062 }
1063
1064 size_t strnlen(const char *s, size_t maxlen);
1065 void testStrnlen_dont_propagate(size_t maxlen) {
1066 // strlen, wcslen, strnlen and alike intentionally don't propagate taint.
1067 // See the details here: https://github.com/llvm/llvm-project/pull/66086
1068 // This isn't ideal, but this is only what we have now.
1069 char s[10];
1070 scanf("%9s", s);
1071 size_t result = strnlen(s, maxlen);
1072 clang_analyzer_isTainted_int(result); // expected-warning {{NO}}
1073 }
1074
1075 long strtol(const char *restrict nptr, char **restrict endptr, int base);
1076 long long strtoll(const char *restrict nptr, char **restrict endptr, int base);
1077 unsigned long int strtoul(const char *nptr, char **endptr, int base);
1078 unsigned long long int strtoull(const char *nptr, char **endptr, int base);
1079 void testStrtolVariants(char **restrict endptr, int base) {
1080 char s[10];
1081 scanf("%9s", s);
1082
1083 long result_l = strtol(s, endptr, base);
1084 clang_analyzer_isTainted_int(result_l); // expected-warning {{YES}}
1085
1086 long long result_ll = strtoll(s, endptr, base);
1087 clang_analyzer_isTainted_int(result_ll); // expected-warning {{YES}}
1088
1089 unsigned long result_ul = strtoul(s, endptr, base);
1090 clang_analyzer_isTainted_int(result_ul); // expected-warning {{YES}}
1091
1092 unsigned long long result_ull = strtoull(s, endptr, base);
1093 clang_analyzer_isTainted_int(result_ull); // expected-warning {{YES}}
1094 }
1095
1096 int isalnum(int c);
1097 int isalpha(int c);
1098 int isascii(int c);
1099 int isblank(int c);
1100 int iscntrl(int c);
1101 int isdigit(int c);
1102 int isgraph(int c);
1103 int islower(int c);
1104 int isprint(int c);
1105 int ispunct(int c);
1106 int isspace(int c);
1107 int isupper(int c);
1108 int isxdigit(int c);
1109
1110 void testIsFunctions() {
1111 char c;
1112 scanf("%c", &c);
1113
1114 int alnum = isalnum(c);
1115 clang_analyzer_isTainted_int(alnum); // expected-warning {{YES}}
1116
1117 int alpha = isalpha(c);
1118 clang_analyzer_isTainted_int(alpha); // expected-warning {{YES}}
1119
1120 int ascii = isascii(c);
1121 clang_analyzer_isTainted_int(ascii); // expected-warning {{YES}}
1122
1123 int blank = isblank(c);
1124 clang_analyzer_isTainted_int(blank); // expected-warning {{YES}}
1125
1126 int cntrl = iscntrl(c);
1127 clang_analyzer_isTainted_int(cntrl); // expected-warning {{YES}}
1128
1129 int digit = isdigit(c);
1130 clang_analyzer_isTainted_int(digit); // expected-warning {{YES}}
1131
1132 int graph = isgraph(c);
1133 clang_analyzer_isTainted_int(graph); // expected-warning {{YES}}
1134
1135 int lower = islower(c);
1136 clang_analyzer_isTainted_int(lower); // expected-warning {{YES}}
1137
1138 int print = isprint(c);
1139 clang_analyzer_isTainted_int(print); // expected-warning {{YES}}
1140
1141 int punct = ispunct(c);
1142 clang_analyzer_isTainted_int(punct); // expected-warning {{YES}}
1143
1144 int space = isspace(c);
1145 clang_analyzer_isTainted_int(space); // expected-warning {{YES}}
1146
1147 int upper = isupper(c);
1148 clang_analyzer_isTainted_int(upper); // expected-warning {{YES}}
1149
1150 int xdigit = isxdigit(c);
1151 clang_analyzer_isTainted_int(xdigit); // expected-warning {{YES}}
1152 }
1153
1154 void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
1155 void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg);
1156 void testQsort() {
1157 int data[1];
1158 scanf("%d", data);
1159
1160 qsort(data, sizeof(data), sizeof(data[0]), NULL);
1161 clang_analyzer_isTainted_int(data[0]); // expected-warning {{YES}}
1162 qsort_r(data, sizeof(data), sizeof(data[0]), NULL, NULL);
1163 clang_analyzer_isTainted_int(data[0]); // expected-warning {{YES}}
1164 }
1165
1166 // Test configuration
1167 int mySource1(void);
1168 void mySource2(int*);
1169 void myScanf(const char*, ...);
1170 int myPropagator(int, int*);
1171 int mySnprintf(char*, size_t, const char*, ...);
1172 bool isOutOfRange(const int*); // const filter function
1173 void sanitizeCmd(char*); // non-const filter function
1174 void mySink(int, int, int);
1175
1176 void testConfigurationSources1(void) {
1177 int x = mySource1();
1178 Buffer[x] = 1; // expected-warning {{Potential out of bound access }}
1179 }
1180
1181 void testConfigurationSources2(void) {
1182 int x;
1183 mySource2(&x);
1184 Buffer[x] = 1; // expected-warning {{Potential out of bound access }}
1185 }
1186
1187 void testConfigurationSources3(void) {
1188 int x, y;
1189 myScanf("%d %d", &x, &y);
1190 Buffer[y] = 1; // expected-warning {{Potential out of bound access }}
1191 }
1192
1193 void testConfigurationPropagation(void) {
1194 int x = mySource1();
1195 int y;
1196 myPropagator(x, &y);
1197 Buffer[y] = 1; // expected-warning {{Potential out of bound access }}
1198 }
1199
1200 void testConfigurationFilter(void) {
1201 int x = mySource1();
1202 if (isOutOfRange(&x)) // the filter function
1203 return;
1204 Buffer[x] = 1; // no-warning
1205 }
1206
1207 void testConfigurationFilterNonConst(void) {
1208 char buffer[1000];
1209 myScanf("%s", buffer); // makes buffer tainted
1210 system(buffer); // expected-warning {{Untrusted data is passed to a system call}}
1211 }
1212
1213 void testConfigurationFilterNonConst2(void) {
1214 char buffer[1000];
1215 myScanf("%s", buffer); // makes buffer tainted
1216 sanitizeCmd(buffer); // removes taintedness
1217 system(buffer); // no-warning
1218 }
1219
1220 void testConfigurationSinks(void) {
1221 int x = mySource1();
1222 mySink(x, 1, 2);
1223 // expected-warning@-1 {{Untrusted data is passed to a user-defined sink}}
1224 mySink(1, x, 2); // no-warning
1225 mySink(1, 2, x);
1226 // expected-warning@-1 {{Untrusted data is passed to a user-defined sink}}
1227 }
1228
1229 int test_exec_like_functions() {
1230 char buf[100] = {0};
1231 scanf("%99s", buf);
1232 clang_analyzer_isTainted_char(buf[0]); // expected-warning {{YES}}
1233
1234 char *cleanArray[] = {"ENV1=V1", "ENV2=V2", NULL};
1235 char *taintedArray[] = {buf, "ENV2=V2", NULL};
1236 clang_analyzer_isTainted_char(taintedArray[0][0]); // expected-warning {{YES}}
1237 clang_analyzer_isTainted_char(*(char*)taintedArray[0]); // expected-warning {{YES}}
1238 clang_analyzer_isTainted_char(*(char*)taintedArray); // expected-warning {{NO}} We should have YES here.
1239 // FIXME: Above the triple pointer indirection will confuse the checker,
1240 // as we only check two levels. The results would be worse, if the tainted
1241 // subobject ("buf") would not be at the beginning of the enclosing object,
1242 // for the same reason.
1243
1244 switch (coin()) {
1245 default: break;
1246 // Execute `path` with all arguments after `path` until a NULL pointer
1247 // and environment from `environ'.
1248 case 0: return execl("path", "arg0", "arg1", "arg2", NULL); // no-warning
1249 case 1: return execl(buf, "arg0", "arg1", "arg2", NULL); // expected-warning {{Untrusted data is passed to a system call}}
1250 case 2: return execl("path", buf, "arg1", "arg2", NULL); // expected-warning {{Untrusted data is passed to a system call}}
1251 case 3: return execl("path", "arg0", buf, "arg2", NULL); // expected-warning {{Untrusted data is passed to a system call}}
1252 case 4: return execl("path", "arg0", "arg1", buf, NULL); // expected-warning {{Untrusted data is passed to a system call}}
1253 }
1254
1255 switch (coin()) {
1256 default: break;
1257 // Execute `path` with all arguments after `PATH` until a NULL pointer,
1258 // and the argument after that for environment.
1259 case 0: return execle("path", "arg0", "arg1", NULL, cleanArray); // no-warning
1260 case 1: return execle( buf, "arg0", "arg1", NULL, cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1261 case 2: return execle("path", buf, "arg1", NULL, cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1262 case 3: return execle("path", "arg0", buf, NULL, cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1263 case 4: return execle("path", "arg0", "arg1", NULL, buf); // expected-warning {{Untrusted data is passed to a system call}}
1264 case 5: return execle("path", "arg0", "arg1", NULL, taintedArray); // FIXME: We might wanna have a report here.
1265 }
1266
1267 switch (coin()) {
1268 default: break;
1269 // Execute `file`, searching in the `PATH' environment variable if it
1270 // contains no slashes, with all arguments after `file` until a NULL
1271 // pointer and environment from `environ'.
1272 case 0: return execlp("file", "arg0", "arg1", "arg2", NULL); // no-warning
1273 case 1: return execlp( buf, "arg0", "arg1", "arg2", NULL); // expected-warning {{Untrusted data is passed to a system call}}
1274 case 2: return execlp("file", buf, "arg1", "arg2", NULL); // expected-warning {{Untrusted data is passed to a system call}}
1275 case 3: return execlp("file", "arg0", buf, "arg2", NULL); // expected-warning {{Untrusted data is passed to a system call}}
1276 case 4: return execlp("file", "arg0", "arg1", buf, NULL); // expected-warning {{Untrusted data is passed to a system call}}
1277 }
1278
1279 switch (coin()) {
1280 default: break;
1281 // Execute `path` with arguments `ARGV` and environment from `environ'.
1282 case 0: return execv("path", /*argv=*/ cleanArray); // no-warning
1283 case 1: return execv( buf, /*argv=*/ cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1284 case 2: return execv("path", /*argv=*/taintedArray); // FIXME: We might wanna have a report here.
1285 }
1286
1287 switch (coin()) {
1288 default: break;
1289 // Replace the current process, executing `path` with arguments `ARGV`
1290 // and environment `ENVP`. `ARGV` and `ENVP` are terminated by NULL pointers.
1291 case 0: return execve("path", /*argv=*/ cleanArray, /*envp=*/cleanArray); // no-warning
1292 case 1: return execve( buf, /*argv=*/ cleanArray, /*envp=*/cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1293 case 2: return execve("path", /*argv=*/taintedArray, /*envp=*/cleanArray); // FIXME: We might wanna have a report here.
1294 case 3: return execve("path", /*argv=*/cleanArray, /*envp=*/taintedArray); // FIXME: We might wanna have a report here.
1295 }
1296
1297 switch (coin()) {
1298 default: break;
1299 // Execute `file`, searching in the `PATH' environment variable if it
1300 // contains no slashes, with arguments `ARGV` and environment from `environ'.
1301 case 0: return execvp("file", /*argv=*/ cleanArray); // no-warning
1302 case 1: return execvp( buf, /*argv=*/ cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1303 case 2: return execvp("file", /*argv=*/taintedArray); // FIXME: We might wanna have a report here.
1304 }
1305
1306 // execvpe
1307 switch (coin()) {
1308 default: break;
1309 // Execute `file`, searching in the `PATH' environment variable if it
1310 // contains no slashes, with arguments `ARGV` and environment `ENVP`.
1311 // `ARGV` and `ENVP` are terminated by NULL pointers.
1312 case 0: return execvpe("file", /*argv=*/ cleanArray, /*envp=*/ cleanArray); // no-warning
1313 case 1: return execvpe( buf, /*argv=*/ cleanArray, /*envp=*/ cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1314 case 2: return execvpe("file", /*argv=*/taintedArray, /*envp=*/ cleanArray); // FIXME: We might wanna have a report here.
1315 case 3: return execvpe("file", /*argv=*/ cleanArray, /*envp=*/taintedArray); // FIXME: We might wanna have a report here.
1316 }
1317
1318 int cleanFD = coin();
1319 int taintedFD;
1320 scanf("%d", &taintedFD);
1321 clang_analyzer_isTainted_int(taintedFD); // expected-warning {{YES}}
1322
1323 switch (coin()) {
1324 default: break;
1325 // Execute the file `FD` refers to, overlaying the running program image.
1326 // `ARGV` and `ENVP` are passed to the new program, as for `execve'.
1327 case 0: return fexecve( cleanFD, /*argv=*/ cleanArray, /*envp=*/ cleanArray); // no-warning
1328 case 1: return fexecve(taintedFD, /*argv=*/ cleanArray, /*envp=*/ cleanArray); // expected-warning {{Untrusted data is passed to a system call}}
1329 case 2: return fexecve( cleanFD, /*argv=*/taintedArray, /*envp=*/ cleanArray); // FIXME: We might wanna have a report here.
1330 case 3: return fexecve( cleanFD, /*argv=*/ cleanArray, /*envp=*/taintedArray); // FIXME: We might wanna have a report here.
1331 }
1332
1333 switch (coin()) {
1334 default: break;
1335 // Create a new stream connected to a pipe running the given `command`.
1336 case 0: return pclose(popen("command", /*mode=*/"r")); // no-warning
1337 case 1: return pclose(popen( buf, /*mode=*/"r")); // expected-warning {{Untrusted data is passed to a system call}}
1338 case 2: return pclose(popen("command", /*mode=*/buf)); // 'mode' is not a taint sink.
1339 }
1340
1341 switch (coin()) {
1342 default: break;
1343 // Execute the given line as a shell command.
1344 case 0: return system("command"); // no-warning
1345 case 1: return system( buf); // expected-warning {{Untrusted data is passed to a system call}}
1346 }
1347
1348 return 0;
1349 }
1350
1351 void testUnknownFunction(void (*foo)(void)) {
1352 foo(); // no-crash
1353 }
1354
1355 void testProctitleFalseNegative(void) {
1356 char flag[80];
1357 fscanf(stdin, "%79s", flag);
1358 char *argv[] = {"myapp", flag};
1359 // FIXME: We should have a warning below: Untrusted data passed to sink.
1360 setproctitle_init(1, argv, 0);
1361 }
1362
1363 void testProctitle2(char *real_argv[]) {
1364 char *app = getenv("APP_NAME");
1365 if (!app)
1366 return;
1367 char *argv[] = {app, "--foobar"};
1368 setproctitle_init(1, argv, 0); // expected-warning {{Untrusted data is passed to a user-defined sink}}
1369 setproctitle_init(1, real_argv, argv); // expected-warning {{Untrusted data is passed to a user-defined sink}}
1370 }
1371
1372 void testAcceptPropagates() {
1373 int listenSocket = socket(2, 1, 6);
1374 clang_analyzer_isTainted_int(listenSocket); // expected-warning {{YES}}
1375 int acceptSocket = accept(listenSocket, 0, 0);
1376 clang_analyzer_isTainted_int(acceptSocket); // expected-warning {{YES}}
1377 }