1 // RUN: %clang_cc1 -analyze -analyzer-checker=alpha.security.taint,core,alpha.security.ArrayBoundV2 -analyzer-output=text -verify %s
3 // This file is for testing enhanced diagnostics produced by the GenericTaintChecker
5 typedef __typeof(sizeof(int)) size_t;
7 typedef struct _IO_FILE
FILE;
9 int scanf(const char *restrict format
, ...);
10 int system(const char *command
);
11 char* getenv( const char* env_var
);
12 size_t strlen( const char* str
);
13 int atoi( const char* str
);
14 void *malloc(size_t size
);
15 void free( void *ptr
);
16 char *fgets(char *str
, int n
, FILE *stream
);
19 void taintDiagnostic(void)
22 scanf("%s", buf
); // expected-note {{Taint originated here}}
23 // expected-note@-1 {{Taint propagated to the 2nd argument}}
24 system(buf
); // expected-warning {{Untrusted data is passed to a system call}} // expected-note {{Untrusted data is passed to a system call (CERT/STR02-C. Sanitize data passed to complex subsystems)}}
27 int taintDiagnosticOutOfBound(void) {
29 int Array
[] = {1, 2, 3, 4, 5};
30 scanf("%d", &index
); // expected-note {{Taint originated here}}
31 // expected-note@-1 {{Taint propagated to the 2nd argument}}
32 return Array
[index
]; // expected-warning {{Out of bound memory access (index is tainted)}}
33 // expected-note@-1 {{Out of bound memory access (index is tainted)}}
36 int taintDiagnosticDivZero(int operand
) {
37 scanf("%d", &operand
); // expected-note {{Value assigned to 'operand'}}
38 // expected-note@-1 {{Taint originated here}}
39 // expected-note@-2 {{Taint propagated to the 2nd argument}}
40 return 10 / operand
; // expected-warning {{Division by a tainted value, possibly zero}}
41 // expected-note@-1 {{Division by a tainted value, possibly zero}}
44 void taintDiagnosticVLA(void) {
46 scanf("%d", &x
); // expected-note {{Value assigned to 'x'}}
47 // expected-note@-1 {{Taint originated here}}
48 // expected-note@-2 {{Taint propagated to the 2nd argument}}
49 int vla
[x
]; // expected-warning {{Declared variable-length array (VLA) has tainted size}}
50 // expected-note@-1 {{Declared variable-length array (VLA) has tainted size}}
54 // Tests if the originated note is correctly placed even if the path is
55 // propagating through variables and expressions
56 char *taintDiagnosticPropagation(){
58 char *size
=getenv("SIZE"); // expected-note {{Taint originated here}}
59 // expected-note@-1 {{Taint propagated to the return value}}
60 if (size
){ // expected-note {{Assuming 'size' is non-null}}
61 // expected-note@-1 {{Taking true branch}}
62 pathbuf
=(char*) malloc(atoi(size
)); // expected-warning{{Untrusted data is used to specify the buffer size}}
63 // expected-note@-1{{Untrusted data is used to specify the buffer size}}
64 // expected-note@-2 {{Taint propagated to the return value}}
70 // Taint origin should be marked correctly even if there are multiple taint
71 // sources in the function
72 char *taintDiagnosticPropagation2(){
74 char *user_env2
=getenv("USER_ENV_VAR2");//unrelated taint source
75 char *size
=getenv("SIZE"); // expected-note {{Taint originated here}}
76 // expected-note@-1 {{Taint propagated to the return value}}
77 char *user_env
=getenv("USER_ENV_VAR");//unrelated taint source
78 if (size
){ // expected-note {{Assuming 'size' is non-null}}
79 // expected-note@-1 {{Taking true branch}}
80 pathbuf
=(char*) malloc(atoi(size
)+1); // expected-warning{{Untrusted data is used to specify the buffer size}}
81 // expected-note@-1{{Untrusted data is used to specify the buffer size}}
82 // expected-note@-2 {{Taint propagated to the return value}}
90 fgets(buf
, sizeof(buf
), stdin
);// expected-note {{Taint originated here}}
91 // expected-note@-1 {{Taint propagated to the 1st argument}}
92 system(buf
);// expected-warning {{Untrusted data is passed to a system call}}
93 // expected-note@-1 {{Untrusted data is passed to a system call (CERT/STR02-C. Sanitize data passed to complex subsystems)}}
97 void multipleTaintSources(void) {
99 scanf("%d", &x
); // expected-note {{Taint originated here}}
100 // expected-note@-1 {{Taint propagated to the 2nd argument}}
101 scanf("%d", &y
); // expected-note {{Taint originated here}}
102 // expected-note@-1 {{Taint propagated to the 2nd argument}}
104 int* ptr
= (int*) malloc(y
+ x
); // expected-warning {{Untrusted data is used to specify the buffer size}}
105 // expected-note@-1{{Untrusted data is used to specify the buffer size}}
109 void multipleTaintedArgs(void) {
111 scanf("%d %d", &x
, &y
); // expected-note {{Taint originated here}}
112 // expected-note@-1 {{Taint propagated to the 2nd argument, 3rd argument}}
113 int* ptr
= (int*) malloc(x
+ y
); // expected-warning {{Untrusted data is used to specify the buffer size}}
114 // expected-note@-1{{Untrusted data is used to specify the buffer size}}