1 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=experimental.security.SecuritySyntactic %s -verify
2 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -DUSE_BUILTINS -analyzer-checker=experimental.security.SecuritySyntactic %s -verify
3 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -DVARIANT -analyzer-checker=experimental.security.SecuritySyntactic %s -verify
4 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -DUSE_BUILTINS -DVARIANT -analyzer-checker=experimental.security.SecuritySyntactic %s -verify
7 # define BUILTIN(f) __builtin_ ## f
8 #else /* USE_BUILTINS */
10 #endif /* USE_BUILTINS */
12 typedef typeof(sizeof(int)) size_t;
15 // <rdar://problem/6336718> rule request: floating point used as loop
16 // condition (FLP30-C, FLP-30-CPP)
18 // For reference: https://www.securecoding.cert.org/confluence/display/seccode/FLP30-C.+Do+not+use+floating+point+variables+as+loop+counters
20 void test_float_condition() {
21 for (float x = 0.1f; x <= 1.0f; x += 0.1f) {} // expected-warning{{Variable 'x' with floating point type 'float'}}
22 for (float x = 100000001.0f; x <= 100000010.0f; x += 1.0f) {} // expected-warning{{Variable 'x' with floating point type 'float'}}
23 for (float x = 100000001.0f; x <= 100000010.0f; x++ ) {} // expected-warning{{Variable 'x' with floating point type 'float'}}
24 for (double x = 100000001.0; x <= 100000010.0; x++ ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
25 for (double x = 100000001.0; ((x)) <= 100000010.0; ((x))++ ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
27 for (double x = 100000001.0; 100000010.0 >= x; x = x + 1.0 ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
30 for (double x = 100000001.0; ((x)) <= 100000010.0; ((x))++, ++i ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
32 typedef float FooType;
33 for (FooType x = 100000001.0f; x <= 100000010.0f; x++ ) {} // expected-warning{{Variable 'x' with floating point type 'FooType'}}
36 // <rdar://problem/6335715> rule request: gets() buffer overflow
37 // Part of recommendation: 300-BSI (buildsecurityin.us-cert.gov)
38 char* gets(char *buf);
42 gets(buff); // expected-warning{{Call to function 'gets' is extremely insecure as it can always result in a buffer overflow}}
45 int getpw(unsigned int uid, char *buf);
49 getpw(2, buff); // expected-warning{{The getpw() function is dangerous as it may overflow the provided buffer. It is obsoleted by getpwuid().}}
52 // <rdar://problem/6337132> CWE-273: Failure to Check Whether Privileges Were
53 // Dropped Successfully
54 typedef unsigned int __uint32_t;
55 typedef __uint32_t __darwin_uid_t;
56 typedef __uint32_t __darwin_gid_t;
57 typedef __darwin_uid_t uid_t;
58 typedef __darwin_gid_t gid_t;
60 int setregid(gid_t, gid_t);
61 int setreuid(uid_t, uid_t);
62 extern void check(int);
67 setuid(2); // expected-warning{{The return value from the call to 'setuid' is not checked. If an error occurs in 'setuid', the following code may execute with unexpected privileges}}
68 setuid(0); // expected-warning{{The return value from the call to 'setuid' is not checked. If an error occurs in 'setuid', the following code may execute with unexpected privileges}}
72 // Currently the 'setuid' check is not flow-sensitive, and only looks
73 // at whether the function was called in a compound statement. This
74 // will lead to false negatives, but there should be no false positives.
75 int t = setuid(2); // no-warning
76 (void)setuid (2); // no-warning
78 check(setuid (2)); // no-warning
80 setreuid(2,2); // expected-warning{{The return value from the call to 'setreuid' is not checked. If an error occurs in 'setreuid', the following code may execute with unexpected privileges}}
81 setregid(2,2); // expected-warning{{The return value from the call to 'setregid' is not checked. If an error occurs in 'setregid', the following code may execute with unexpected privileges}}
84 // <rdar://problem/6337100> CWE-338: Use of cryptographically weak prng
87 double erand48(unsigned short[3]);
88 long jrand48(unsigned short[3]);
89 void lcong48(unsigned short[7]);
92 long nrand48(unsigned short[3]);
94 int rand_r(unsigned *);
101 rand(); // expected-warning{{Function 'rand' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
102 drand48(); // expected-warning{{Function 'drand48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
103 erand48(a); // expected-warning{{Function 'erand48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
104 jrand48(a); // expected-warning{{Function 'jrand48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
105 lcong48(a); // expected-warning{{Function 'lcong48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
106 lrand48(); // expected-warning{{Function 'lrand48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
107 mrand48(); // expected-warning{{Function 'mrand48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
108 nrand48(a); // expected-warning{{Function 'nrand48' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
109 rand_r(&b); // expected-warning{{Function 'rand_r' is obsolete because it implements a poor random number generator. Use 'arc4random' instead}}
110 random(); // expected-warning{{The 'random' function produces a sequence of values that an adversary may be able to predict. Use 'arc4random' instead}}
113 char *mktemp(char *buf);
116 char *x = mktemp("/tmp/zxcv"); // expected-warning{{Call to function 'mktemp' is insecure as it always creates or uses insecure temporary file}}
120 //===----------------------------------------------------------------------===
122 //===----------------------------------------------------------------------===
125 #define __strcpy_chk BUILTIN(__strcpy_chk)
126 char *__strcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen);
128 #define strcpy(a,b) __strcpy_chk(a,b,(size_t)-1)
132 #define strcpy BUILTIN(strcpy)
133 char *strcpy(char *restrict s1, const char *restrict s2);
141 strcpy(x, y); //expected-warning{{Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strncpy'. CWE-119.}}
144 //===----------------------------------------------------------------------===
146 //===----------------------------------------------------------------------===
149 #define __strcat_chk BUILTIN(__strcat_chk)
150 char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen);
152 #define strcat(a,b) __strcat_chk(a,b,(size_t)-1)
156 #define strcat BUILTIN(strcat)
157 char *strcat(char *restrict s1, const char *restrict s2);
165 strcat(x, y); //expected-warning{{Call to function 'strcat' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strncat'. CWE-119.}}
168 //===----------------------------------------------------------------------===
170 //===----------------------------------------------------------------------===
171 typedef int __int32_t;
172 typedef __int32_t pid_t;
173 pid_t vfork(void); //expected-warning{{declaration of built-in function 'vfork' requires inclusion of the header <setjmp.h>}}
176 vfork(); //expected-warning{{Call to function 'vfork' is insecure as it can lead to denial of service situations in the parent process.}}