blob: fe27070026bd57556e1aaf1009a5159f4589a711 [file] [log] [blame]
Ted Kremenekcdc3a892012-08-24 20:39:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.security.taint,core,alpha.security.ArrayBoundV2 -Wno-format-security -verify %s
Anna Zaks9b0970f2011-11-16 19:58:17 +00002
3int scanf(const char *restrict format, ...);
4int getchar(void);
5
Anna Zaks1fb826a2012-01-12 02:22:34 +00006typedef struct _FILE FILE;
7extern FILE *stdin;
8int fscanf(FILE *restrict stream, const char *restrict format, ...);
9int sprintf(char *str, const char *format, ...);
10void setproctitle(const char *fmt, ...);
11typedef __typeof(sizeof(int)) size_t;
12
13// Define string functions. Use builtin for some of them. They all default to
14// the processing in the taint checker.
15#define strcpy(dest, src) \
16 ((__builtin_object_size(dest, 0) != -1ULL) \
17 ? __builtin___strcpy_chk (dest, src, __builtin_object_size(dest, 1)) \
18 : __inline_strcpy_chk(dest, src))
19
20static char *__inline_strcpy_chk (char *dest, const char *src) {
21 return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1));
22}
23char *stpcpy(char *restrict s1, const char *restrict s2);
24char *strncpy( char * destination, const char * source, size_t num );
Anna Zaks9b0c7492012-01-18 02:45:07 +000025char *strndup(const char *s, size_t n);
Anna Zaks4e462212012-01-18 02:45:11 +000026char *strncat(char *restrict s1, const char *restrict s2, size_t n);
27
28void *malloc(size_t);
29void *calloc(size_t nmemb, size_t size);
30void bcopy(void *s1, void *s2, size_t n);
Anna Zaks1fb826a2012-01-12 02:22:34 +000031
Anna Zaks9b0970f2011-11-16 19:58:17 +000032#define BUFSIZE 10
33
34int Buffer[BUFSIZE];
Anna Zaks3881c692011-11-28 20:43:40 +000035void bufferScanfDirect(void)
Anna Zaks9b0970f2011-11-16 19:58:17 +000036{
37 int n;
38 scanf("%d", &n);
39 Buffer[n] = 1; // expected-warning {{Out of bound memory access }}
40}
Anna Zaks0d339d02011-11-17 23:07:28 +000041
42void bufferScanfArithmetic1(int x) {
43 int n;
44 scanf("%d", &n);
45 int m = (n - 3);
46 Buffer[m] = 1; // expected-warning {{Out of bound memory access }}
47}
48
49void bufferScanfArithmetic2(int x) {
50 int n;
51 scanf("%d", &n);
Anna Zaks02019f72012-01-20 20:28:31 +000052 int m = 100 - (n + 3) * x;
Anna Zaks0d339d02011-11-17 23:07:28 +000053 Buffer[m] = 1; // expected-warning {{Out of bound memory access }}
54}
Anna Zaks8f4caf52011-11-18 02:26:36 +000055
Anna Zaks3881c692011-11-28 20:43:40 +000056void bufferScanfAssignment(int x) {
57 int n;
58 scanf("%d", &n);
59 int m;
60 if (x > 0) {
61 m = n;
62 Buffer[m] = 1; // expected-warning {{Out of bound memory access }}
63 }
64}
65
Anna Zaks8f4caf52011-11-18 02:26:36 +000066void scanfArg() {
Anna Zaks02019f72012-01-20 20:28:31 +000067 int t = 0;
Ted Kremenekce506ae2012-01-20 21:52:58 +000068 scanf("%d", t); // expected-warning {{format specifies type 'int *' but the argument has type 'int'}}
Anna Zaks8f4caf52011-11-18 02:26:36 +000069}
Anna Zaks3881c692011-11-28 20:43:40 +000070
71void bufferGetchar(int x) {
72 int m = getchar();
Anna Zaks3bfd6d72012-01-21 05:07:33 +000073 Buffer[m] = 1; //expected-warning {{Out of bound memory access (index is tainted)}}
Anna Zaks3881c692011-11-28 20:43:40 +000074}
Anna Zaks9f03b622012-01-07 02:33:10 +000075
Anna Zaks1fb826a2012-01-12 02:22:34 +000076void testUncontrolledFormatString(char **p) {
Anna Zaks9f03b622012-01-07 02:33:10 +000077 char s[80];
78 fscanf(stdin, "%s", s);
79 char buf[128];
80 sprintf(buf,s); // expected-warning {{Uncontrolled Format String}}
81 setproctitle(s, 3); // expected-warning {{Uncontrolled Format String}}
Anna Zaks1fb826a2012-01-12 02:22:34 +000082
83 // Test taint propagation through strcpy and family.
84 char scpy[80];
85 strcpy(scpy, s);
86 sprintf(buf,scpy); // expected-warning {{Uncontrolled Format String}}
87
Anna Zaksb71d1572012-01-13 00:56:55 +000088 stpcpy(*(++p), s); // this generates __inline.
89 setproctitle(*(p), 3); // expected-warning {{Uncontrolled Format String}}
90
Anna Zaks1fb826a2012-01-12 02:22:34 +000091 char spcpy[80];
92 stpcpy(spcpy, s);
93 setproctitle(spcpy, 3); // expected-warning {{Uncontrolled Format String}}
94
Anna Zaks9b0c7492012-01-18 02:45:07 +000095 char *spcpyret;
96 spcpyret = stpcpy(spcpy, s);
97 setproctitle(spcpyret, 3); // expected-warning {{Uncontrolled Format String}}
98
Anna Zaks1fb826a2012-01-12 02:22:34 +000099 char sncpy[80];
100 strncpy(sncpy, s, 20);
101 setproctitle(sncpy, 3); // expected-warning {{Uncontrolled Format String}}
Anna Zaks9b0c7492012-01-18 02:45:07 +0000102
103 char *dup;
104 dup = strndup(s, 20);
105 setproctitle(dup, 3); // expected-warning {{Uncontrolled Format String}}
106
Anna Zaks9f03b622012-01-07 02:33:10 +0000107}
Anna Zaks8568ee72012-01-14 02:48:40 +0000108
109int system(const char *command);
110void testTaintSystemCall() {
111 char buffer[156];
112 char addr[128];
113 scanf("%s", addr);
Anna Zaks5fdadf42012-02-22 02:35:58 +0000114 system(addr); // expected-warning {{Untrusted data is passed to a system call}}
Anna Zaks9b0c7492012-01-18 02:45:07 +0000115
116 // Test that spintf transfers taint.
117 sprintf(buffer, "/bin/mail %s < /tmp/email", addr);
Anna Zaks5fdadf42012-02-22 02:35:58 +0000118 system(buffer); // expected-warning {{Untrusted data is passed to a system call}}
Anna Zaks9b0c7492012-01-18 02:45:07 +0000119}
Anna Zaks4e462212012-01-18 02:45:11 +0000120
Anna Zaks9b0c7492012-01-18 02:45:07 +0000121void testTaintSystemCall2() {
122 // Test that snpintf transfers taint.
123 char buffern[156];
124 char addr[128];
125 scanf("%s", addr);
126 __builtin_snprintf(buffern, 10, "/bin/mail %s < /tmp/email", addr);
Anna Zaks5fdadf42012-02-22 02:35:58 +0000127 system(buffern); // expected-warning {{Untrusted data is passed to a system call}}
Anna Zaks9b0c7492012-01-18 02:45:07 +0000128}
Anna Zaks4e462212012-01-18 02:45:11 +0000129
Anna Zaks9b0c7492012-01-18 02:45:07 +0000130void testTaintSystemCall3() {
131 char buffern2[156];
132 int numt;
133 char addr[128];
134 scanf("%s %d", addr, &numt);
135 __builtin_snprintf(buffern2, numt, "/bin/mail %s < /tmp/email", "abcd");
Anna Zaks5fdadf42012-02-22 02:35:58 +0000136 system(buffern2); // expected-warning {{Untrusted data is passed to a system call}}
Anna Zaks8568ee72012-01-14 02:48:40 +0000137}
Anna Zaks4e462212012-01-18 02:45:11 +0000138
139void testTaintedBufferSize() {
140 size_t ts;
141 scanf("%zd", &ts);
142
Anna Zaks5fdadf42012-02-22 02:35:58 +0000143 int *buf1 = (int*)malloc(ts*sizeof(int)); // expected-warning {{Untrusted data is used to specify the buffer size}}
144 char *dst = (char*)calloc(ts, sizeof(char)); //expected-warning {{Untrusted data is used to specify the buffer size}}
145 bcopy(buf1, dst, ts); // expected-warning {{Untrusted data is used to specify the buffer size}}
146 __builtin_memcpy(dst, buf1, (ts + 4)*sizeof(char)); // expected-warning {{Untrusted data is used to specify the buffer size}}
Anna Zaks4e462212012-01-18 02:45:11 +0000147
148 // If both buffers are trusted, do not issue a warning.
Anna Zaks5fdadf42012-02-22 02:35:58 +0000149 char *dst2 = (char*)malloc(ts*sizeof(char)); // expected-warning {{Untrusted data is used to specify the buffer size}}
Anna Zaks4e462212012-01-18 02:45:11 +0000150 strncat(dst2, dst, ts); // no-warning
Anna Zaks4e462212012-01-18 02:45:11 +0000151}
Anna Zaks2bf8fd82012-01-20 00:11:19 +0000152
153#define AF_UNIX 1 /* local to host (pipes) */
154#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
155#define AF_LOCAL AF_UNIX /* backward compatibility */
156#define SOCK_STREAM 1
157int socket(int, int, int);
158size_t read(int, void *, size_t);
159int execl(const char *, const char *, ...);
160
161void testSocket() {
162 int sock;
163 char buffer[100];
164
165 sock = socket(AF_INET, SOCK_STREAM, 0);
166 read(sock, buffer, 100);
Anna Zaks5fdadf42012-02-22 02:35:58 +0000167 execl(buffer, "filename", 0); // expected-warning {{Untrusted data is passed to a system call}}
Anna Zaks2bf8fd82012-01-20 00:11:19 +0000168
169 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
170 read(sock, buffer, 100);
171 execl(buffer, "filename", 0); // no-warning
172}
173
Anna Zaks02019f72012-01-20 20:28:31 +0000174int testDivByZero() {
175 int x;
176 scanf("%d", &x);
177 return 5/x; // expected-warning {{Division by a tainted value, possibly zero}}
178}
Anna Zaks3bfd6d72012-01-21 05:07:33 +0000179
180// Zero-sized VLAs.
181void testTaintedVLASize() {
182 int x;
183 scanf("%d", &x);
184 int vla[x]; // expected-warning{{Declared variable-length array (VLA) has tainted size}}
185}
Anna Zaksbaeaa9a2012-05-03 02:13:50 +0000186
187// This computation used to take a very long time.
188#define longcmp(a,b,c) { \
189 a -= c; a ^= c; c += b; b -= a; b ^= (a<<6) | (a >> (32-b)); a += c; c -= b; c ^= b; b += a; \
190 a -= c; a ^= c; c += b; b -= a; b ^= a; a += c; c -= b; c ^= b; b += a; }
191
192unsigned radar11369570_hanging(const unsigned char *arr, int l) {
193 unsigned a, b, c;
194 a = b = c = 0x9899e3 + l;
195 while (l >= 6) {
196 unsigned t;
197 scanf("%d", &t);
198 a += b;
199 a ^= a;
200 a += (arr[3] + ((unsigned) arr[2] << 8) + ((unsigned) arr[1] << 16) + ((unsigned) arr[0] << 24));
201 longcmp(a, t, c);
202 l -= 12;
203 }
204 return 5/a; // expected-warning {{Division by a tainted value, possibly zero}}
205}
Anna Zaksda396032012-05-03 02:13:53 +0000206
207// Check that we do not assert of the following code.
208int SymSymExprWithDiffTypes(void* p) {
209 int i;
210 scanf("%d", &i);
211 int j = (i % (int)(long)p);
212 return 5/j; // expected-warning {{Division by a tainted value, possibly zero}}
213}
214
Jordan Rose8f7bfb42013-03-24 20:25:22 +0000215
216void constraintManagerShouldTreatAsOpaque(int rhs) {
217 int i;
218 scanf("%d", &i);
219 // This comparison used to hit an assertion in the constraint manager,
220 // which didn't handle NonLoc sym-sym comparisons.
221 if (i < rhs)
222 return;
223 if (i < rhs)
224 *(volatile int *) 0; // no-warning
225}