xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/taint-generic.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1  -analyze -analyzer-checker=alpha.security.taint,core,alpha.security.ArrayBoundV2 -Wno-format-security -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc int scanf(const char *restrict format, ...);
4*f4a2713aSLionel Sambuc int getchar(void);
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc typedef struct _FILE FILE;
7*f4a2713aSLionel Sambuc extern FILE *stdin;
8*f4a2713aSLionel Sambuc int fscanf(FILE *restrict stream, const char *restrict format, ...);
9*f4a2713aSLionel Sambuc int sprintf(char *str, const char *format, ...);
10*f4a2713aSLionel Sambuc void setproctitle(const char *fmt, ...);
11*f4a2713aSLionel Sambuc typedef __typeof(sizeof(int)) size_t;
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc // Define string functions. Use builtin for some of them. They all default to
14*f4a2713aSLionel Sambuc // the processing in the taint checker.
15*f4a2713aSLionel Sambuc #define strcpy(dest, src) \
16*f4a2713aSLionel Sambuc   ((__builtin_object_size(dest, 0) != -1ULL) \
17*f4a2713aSLionel Sambuc    ? __builtin___strcpy_chk (dest, src, __builtin_object_size(dest, 1)) \
18*f4a2713aSLionel Sambuc    : __inline_strcpy_chk(dest, src))
19*f4a2713aSLionel Sambuc 
__inline_strcpy_chk(char * dest,const char * src)20*f4a2713aSLionel Sambuc static char *__inline_strcpy_chk (char *dest, const char *src) {
21*f4a2713aSLionel Sambuc   return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1));
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc char *stpcpy(char *restrict s1, const char *restrict s2);
24*f4a2713aSLionel Sambuc char *strncpy( char * destination, const char * source, size_t num );
25*f4a2713aSLionel Sambuc char *strndup(const char *s, size_t n);
26*f4a2713aSLionel Sambuc char *strncat(char *restrict s1, const char *restrict s2, size_t n);
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc void *malloc(size_t);
29*f4a2713aSLionel Sambuc void *calloc(size_t nmemb, size_t size);
30*f4a2713aSLionel Sambuc void bcopy(void *s1, void *s2, size_t n);
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc #define BUFSIZE 10
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc int Buffer[BUFSIZE];
bufferScanfDirect(void)35*f4a2713aSLionel Sambuc void bufferScanfDirect(void)
36*f4a2713aSLionel Sambuc {
37*f4a2713aSLionel Sambuc   int n;
38*f4a2713aSLionel Sambuc   scanf("%d", &n);
39*f4a2713aSLionel Sambuc   Buffer[n] = 1; // expected-warning {{Out of bound memory access }}
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
bufferScanfArithmetic1(int x)42*f4a2713aSLionel Sambuc void bufferScanfArithmetic1(int x) {
43*f4a2713aSLionel Sambuc   int n;
44*f4a2713aSLionel Sambuc   scanf("%d", &n);
45*f4a2713aSLionel Sambuc   int m = (n - 3);
46*f4a2713aSLionel Sambuc   Buffer[m] = 1; // expected-warning {{Out of bound memory access }}
47*f4a2713aSLionel Sambuc }
48*f4a2713aSLionel Sambuc 
bufferScanfArithmetic2(int x)49*f4a2713aSLionel Sambuc void bufferScanfArithmetic2(int x) {
50*f4a2713aSLionel Sambuc   int n;
51*f4a2713aSLionel Sambuc   scanf("%d", &n);
52*f4a2713aSLionel Sambuc   int m = 100 - (n + 3) * x;
53*f4a2713aSLionel Sambuc   Buffer[m] = 1; // expected-warning {{Out of bound memory access }}
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
bufferScanfAssignment(int x)56*f4a2713aSLionel Sambuc void bufferScanfAssignment(int x) {
57*f4a2713aSLionel Sambuc   int n;
58*f4a2713aSLionel Sambuc   scanf("%d", &n);
59*f4a2713aSLionel Sambuc   int m;
60*f4a2713aSLionel Sambuc   if (x > 0) {
61*f4a2713aSLionel Sambuc     m = n;
62*f4a2713aSLionel Sambuc     Buffer[m] = 1; // expected-warning {{Out of bound memory access }}
63*f4a2713aSLionel Sambuc   }
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc 
scanfArg()66*f4a2713aSLionel Sambuc void scanfArg() {
67*f4a2713aSLionel Sambuc   int t = 0;
68*f4a2713aSLionel Sambuc   scanf("%d", t); // expected-warning {{format specifies type 'int *' but the argument has type 'int'}}
69*f4a2713aSLionel Sambuc }
70*f4a2713aSLionel Sambuc 
bufferGetchar(int x)71*f4a2713aSLionel Sambuc void bufferGetchar(int x) {
72*f4a2713aSLionel Sambuc   int m = getchar();
73*f4a2713aSLionel Sambuc   Buffer[m] = 1;  //expected-warning {{Out of bound memory access (index is tainted)}}
74*f4a2713aSLionel Sambuc }
75*f4a2713aSLionel Sambuc 
testUncontrolledFormatString(char ** p)76*f4a2713aSLionel Sambuc void testUncontrolledFormatString(char **p) {
77*f4a2713aSLionel Sambuc   char s[80];
78*f4a2713aSLionel Sambuc   fscanf(stdin, "%s", s);
79*f4a2713aSLionel Sambuc   char buf[128];
80*f4a2713aSLionel Sambuc   sprintf(buf,s); // expected-warning {{Uncontrolled Format String}}
81*f4a2713aSLionel Sambuc   setproctitle(s, 3); // expected-warning {{Uncontrolled Format String}}
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc   // Test taint propagation through strcpy and family.
84*f4a2713aSLionel Sambuc   char scpy[80];
85*f4a2713aSLionel Sambuc   strcpy(scpy, s);
86*f4a2713aSLionel Sambuc   sprintf(buf,scpy); // expected-warning {{Uncontrolled Format String}}
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc   stpcpy(*(++p), s); // this generates __inline.
89*f4a2713aSLionel Sambuc   setproctitle(*(p), 3); // expected-warning {{Uncontrolled Format String}}
90*f4a2713aSLionel Sambuc 
91*f4a2713aSLionel Sambuc   char spcpy[80];
92*f4a2713aSLionel Sambuc   stpcpy(spcpy, s);
93*f4a2713aSLionel Sambuc   setproctitle(spcpy, 3); // expected-warning {{Uncontrolled Format String}}
94*f4a2713aSLionel Sambuc 
95*f4a2713aSLionel Sambuc   char *spcpyret;
96*f4a2713aSLionel Sambuc   spcpyret = stpcpy(spcpy, s);
97*f4a2713aSLionel Sambuc   setproctitle(spcpyret, 3); // expected-warning {{Uncontrolled Format String}}
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc   char sncpy[80];
100*f4a2713aSLionel Sambuc   strncpy(sncpy, s, 20);
101*f4a2713aSLionel Sambuc   setproctitle(sncpy, 3); // expected-warning {{Uncontrolled Format String}}
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   char *dup;
104*f4a2713aSLionel Sambuc   dup = strndup(s, 20);
105*f4a2713aSLionel Sambuc   setproctitle(dup, 3); // expected-warning {{Uncontrolled Format String}}
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc }
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc int system(const char *command);
testTaintSystemCall()110*f4a2713aSLionel Sambuc void testTaintSystemCall() {
111*f4a2713aSLionel Sambuc   char buffer[156];
112*f4a2713aSLionel Sambuc   char addr[128];
113*f4a2713aSLionel Sambuc   scanf("%s", addr);
114*f4a2713aSLionel Sambuc   system(addr); // expected-warning {{Untrusted data is passed to a system call}}
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc   // Test that spintf transfers taint.
117*f4a2713aSLionel Sambuc   sprintf(buffer, "/bin/mail %s < /tmp/email", addr);
118*f4a2713aSLionel Sambuc   system(buffer); // expected-warning {{Untrusted data is passed to a system call}}
119*f4a2713aSLionel Sambuc }
120*f4a2713aSLionel Sambuc 
testTaintSystemCall2()121*f4a2713aSLionel Sambuc void testTaintSystemCall2() {
122*f4a2713aSLionel Sambuc   // Test that snpintf transfers taint.
123*f4a2713aSLionel Sambuc   char buffern[156];
124*f4a2713aSLionel Sambuc   char addr[128];
125*f4a2713aSLionel Sambuc   scanf("%s", addr);
126*f4a2713aSLionel Sambuc   __builtin_snprintf(buffern, 10, "/bin/mail %s < /tmp/email", addr);
127*f4a2713aSLionel Sambuc   system(buffern); // expected-warning {{Untrusted data is passed to a system call}}
128*f4a2713aSLionel Sambuc }
129*f4a2713aSLionel Sambuc 
testTaintSystemCall3()130*f4a2713aSLionel Sambuc void testTaintSystemCall3() {
131*f4a2713aSLionel Sambuc   char buffern2[156];
132*f4a2713aSLionel Sambuc   int numt;
133*f4a2713aSLionel Sambuc   char addr[128];
134*f4a2713aSLionel Sambuc   scanf("%s %d", addr, &numt);
135*f4a2713aSLionel Sambuc   __builtin_snprintf(buffern2, numt, "/bin/mail %s < /tmp/email", "abcd");
136*f4a2713aSLionel Sambuc   system(buffern2); // expected-warning {{Untrusted data is passed to a system call}}
137*f4a2713aSLionel Sambuc }
138*f4a2713aSLionel Sambuc 
testTaintedBufferSize()139*f4a2713aSLionel Sambuc void testTaintedBufferSize() {
140*f4a2713aSLionel Sambuc   size_t ts;
141*f4a2713aSLionel Sambuc   scanf("%zd", &ts);
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc   int *buf1 = (int*)malloc(ts*sizeof(int)); // expected-warning {{Untrusted data is used to specify the buffer size}}
144*f4a2713aSLionel Sambuc   char *dst = (char*)calloc(ts, sizeof(char)); //expected-warning {{Untrusted data is used to specify the buffer size}}
145*f4a2713aSLionel Sambuc   bcopy(buf1, dst, ts); // expected-warning {{Untrusted data is used to specify the buffer size}}
146*f4a2713aSLionel Sambuc   __builtin_memcpy(dst, buf1, (ts + 4)*sizeof(char)); // expected-warning {{Untrusted data is used to specify the buffer size}}
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc   // If both buffers are trusted, do not issue a warning.
149*f4a2713aSLionel Sambuc   char *dst2 = (char*)malloc(ts*sizeof(char)); // expected-warning {{Untrusted data is used to specify the buffer size}}
150*f4a2713aSLionel Sambuc   strncat(dst2, dst, ts); // no-warning
151*f4a2713aSLionel Sambuc }
152*f4a2713aSLionel Sambuc 
153*f4a2713aSLionel Sambuc #define AF_UNIX   1   /* local to host (pipes) */
154*f4a2713aSLionel Sambuc #define AF_INET   2   /* internetwork: UDP, TCP, etc. */
155*f4a2713aSLionel Sambuc #define AF_LOCAL  AF_UNIX   /* backward compatibility */
156*f4a2713aSLionel Sambuc #define SOCK_STREAM 1
157*f4a2713aSLionel Sambuc int socket(int, int, int);
158*f4a2713aSLionel Sambuc size_t read(int, void *, size_t);
159*f4a2713aSLionel Sambuc int  execl(const char *, const char *, ...);
160*f4a2713aSLionel Sambuc 
testSocket()161*f4a2713aSLionel Sambuc void testSocket() {
162*f4a2713aSLionel Sambuc   int sock;
163*f4a2713aSLionel Sambuc   char buffer[100];
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc   sock = socket(AF_INET, SOCK_STREAM, 0);
166*f4a2713aSLionel Sambuc   read(sock, buffer, 100);
167*f4a2713aSLionel Sambuc   execl(buffer, "filename", 0); // expected-warning {{Untrusted data is passed to a system call}}
168*f4a2713aSLionel Sambuc 
169*f4a2713aSLionel Sambuc   sock = socket(AF_LOCAL, SOCK_STREAM, 0);
170*f4a2713aSLionel Sambuc   read(sock, buffer, 100);
171*f4a2713aSLionel Sambuc   execl(buffer, "filename", 0); // no-warning
172*f4a2713aSLionel Sambuc }
173*f4a2713aSLionel Sambuc 
testDivByZero()174*f4a2713aSLionel Sambuc int testDivByZero() {
175*f4a2713aSLionel Sambuc   int x;
176*f4a2713aSLionel Sambuc   scanf("%d", &x);
177*f4a2713aSLionel Sambuc   return 5/x; // expected-warning {{Division by a tainted value, possibly zero}}
178*f4a2713aSLionel Sambuc }
179*f4a2713aSLionel Sambuc 
180*f4a2713aSLionel Sambuc // Zero-sized VLAs.
testTaintedVLASize()181*f4a2713aSLionel Sambuc void testTaintedVLASize() {
182*f4a2713aSLionel Sambuc   int x;
183*f4a2713aSLionel Sambuc   scanf("%d", &x);
184*f4a2713aSLionel Sambuc   int vla[x]; // expected-warning{{Declared variable-length array (VLA) has tainted size}}
185*f4a2713aSLionel Sambuc }
186*f4a2713aSLionel Sambuc 
187*f4a2713aSLionel Sambuc // This computation used to take a very long time.
188*f4a2713aSLionel Sambuc #define longcmp(a,b,c) { \
189*f4a2713aSLionel Sambuc   a -= c;  a ^= c;  c += b; b -= a;  b ^= (a<<6) | (a >> (32-b));  a += c; c -= b;  c ^= b;  b += a; \
190*f4a2713aSLionel Sambuc   a -= c;  a ^= c;  c += b; b -= a;  b ^= a;  a += c; c -= b;  c ^= b;  b += a; }
191*f4a2713aSLionel Sambuc 
radar11369570_hanging(const unsigned char * arr,int l)192*f4a2713aSLionel Sambuc unsigned radar11369570_hanging(const unsigned char *arr, int l) {
193*f4a2713aSLionel Sambuc   unsigned a, b, c;
194*f4a2713aSLionel Sambuc   a = b = c = 0x9899e3 + l;
195*f4a2713aSLionel Sambuc   while (l >= 6) {
196*f4a2713aSLionel Sambuc     unsigned t;
197*f4a2713aSLionel Sambuc     scanf("%d", &t);
198*f4a2713aSLionel Sambuc     a += b;
199*f4a2713aSLionel Sambuc     a ^= a;
200*f4a2713aSLionel Sambuc     a += (arr[3] + ((unsigned) arr[2] << 8) + ((unsigned) arr[1] << 16) + ((unsigned) arr[0] << 24));
201*f4a2713aSLionel Sambuc     longcmp(a, t, c);
202*f4a2713aSLionel Sambuc     l -= 12;
203*f4a2713aSLionel Sambuc   }
204*f4a2713aSLionel Sambuc   return 5/a; // expected-warning {{Division by a tainted value, possibly zero}}
205*f4a2713aSLionel Sambuc }
206*f4a2713aSLionel Sambuc 
207*f4a2713aSLionel Sambuc // Check that we do not assert of the following code.
SymSymExprWithDiffTypes(void * p)208*f4a2713aSLionel Sambuc int SymSymExprWithDiffTypes(void* p) {
209*f4a2713aSLionel Sambuc   int i;
210*f4a2713aSLionel Sambuc   scanf("%d", &i);
211*f4a2713aSLionel Sambuc   int j = (i % (int)(long)p);
212*f4a2713aSLionel Sambuc   return 5/j; // expected-warning {{Division by a tainted value, possibly zero}}
213*f4a2713aSLionel Sambuc }
214*f4a2713aSLionel Sambuc 
215*f4a2713aSLionel Sambuc 
constraintManagerShouldTreatAsOpaque(int rhs)216*f4a2713aSLionel Sambuc void constraintManagerShouldTreatAsOpaque(int rhs) {
217*f4a2713aSLionel Sambuc   int i;
218*f4a2713aSLionel Sambuc   scanf("%d", &i);
219*f4a2713aSLionel Sambuc   // This comparison used to hit an assertion in the constraint manager,
220*f4a2713aSLionel Sambuc   // which didn't handle NonLoc sym-sym comparisons.
221*f4a2713aSLionel Sambuc   if (i < rhs)
222*f4a2713aSLionel Sambuc     return;
223*f4a2713aSLionel Sambuc   if (i < rhs)
224*f4a2713aSLionel Sambuc     *(volatile int *) 0; // no-warning
225*f4a2713aSLionel Sambuc }
226