security-syntax-checks.m revision 65a81a92eba8dace6f2381f83f9dfcbf7b848ab7
1// RUN: clang-cc -triple i386-apple-darwin10 -analyze -warn-security-syntactic %s -verify
2
3// <rdar://problem/6336718> rule request: floating point used as loop 
4//  condition (FLP30-C, FLP-30-CPP)
5//
6// For reference: https://www.securecoding.cert.org/confluence/display/seccode/FLP30-C.+Do+not+use+floating+point+variables+as+loop+counters
7//
8void test_float_condition() {
9  for (float x = 0.1f; x <= 1.0f; x += 0.1f) {} // expected-warning{{Variable 'x' with floating point type 'float'}}
10  for (float x = 100000001.0f; x <= 100000010.0f; x += 1.0f) {} // expected-warning{{Variable 'x' with floating point type 'float'}}
11  for (float x = 100000001.0f; x <= 100000010.0f; x++ ) {} // expected-warning{{Variable 'x' with floating point type 'float'}}
12  for (double x = 100000001.0; x <= 100000010.0; x++ ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
13  for (double x = 100000001.0; ((x)) <= 100000010.0; ((x))++ ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
14  
15  for (double x = 100000001.0; 100000010.0 >= x; x = x + 1.0 ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
16  
17  int i = 0;
18  for (double x = 100000001.0; ((x)) <= 100000010.0; ((x))++, ++i ) {} // expected-warning{{Variable 'x' with floating point type 'double'}}
19  
20  typedef float FooType;
21  for (FooType x = 100000001.0f; x <= 100000010.0f; x++ ) {} // expected-warning{{Variable 'x' with floating point type 'FooType'}}
22}
23
24// <rdar://problem/6335715> rule request: gets() buffer overflow
25// Part of recommendation: 300-BSI (buildsecurityin.us-cert.gov)
26char* gets(char *buf);
27
28void test_gets() {
29  char buff[1024];
30  gets(buff); // expected-warning{{Call to function 'gets' is extremely insecure as it can always result in a buffer overflow}}
31}
32
33// <rdar://problem/6337132> CWE-273: Failure to Check Whether Privileges Were
34//  Dropped Successfully
35typedef unsigned int __uint32_t;
36typedef __uint32_t __darwin_uid_t;
37typedef __uint32_t __darwin_gid_t;
38typedef __darwin_uid_t uid_t;
39typedef __darwin_gid_t gid_t;
40int setuid(uid_t);
41int setregid(gid_t, gid_t);
42int setreuid(uid_t, uid_t);
43extern void check(int);
44
45void test_setuid() 
46{
47  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}}
48  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}}
49  if (setuid (2) != 0)
50    abort();
51
52  // Currently the 'setuid' check is not flow-sensitive, and only looks
53  // at whether the function was called in a compound statement.  This
54  // will lead to false negatives, but there should be no false positives.
55  int t = setuid(2);  // no-warning
56  (void)setuid (2); // no-warning
57
58  check(setuid (2)); // no-warning
59
60  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}}
61  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}}
62}
63