Anna Zaks | 9f03b62 | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=experimental.security.taint,experimental.security.ArrayBoundV2 -Wno-format-security -verify %s |
Anna Zaks | 9b0970f | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 2 | |
| 3 | int scanf(const char *restrict format, ...); |
| 4 | int getchar(void); |
| 5 | |
Anna Zaks | 1fb826a | 2012-01-12 02:22:34 +0000 | [diff] [blame^] | 6 | typedef struct _FILE FILE; |
| 7 | extern FILE *stdin; |
| 8 | int fscanf(FILE *restrict stream, const char *restrict format, ...); |
| 9 | int sprintf(char *str, const char *format, ...); |
| 10 | void setproctitle(const char *fmt, ...); |
| 11 | typedef __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 | |
| 20 | static char *__inline_strcpy_chk (char *dest, const char *src) { |
| 21 | return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1)); |
| 22 | } |
| 23 | char *stpcpy(char *restrict s1, const char *restrict s2); |
| 24 | char *strncpy( char * destination, const char * source, size_t num ); |
| 25 | |
Anna Zaks | 9b0970f | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 26 | #define BUFSIZE 10 |
| 27 | |
| 28 | int Buffer[BUFSIZE]; |
Anna Zaks | 3881c69 | 2011-11-28 20:43:40 +0000 | [diff] [blame] | 29 | void bufferScanfDirect(void) |
Anna Zaks | 9b0970f | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 30 | { |
| 31 | int n; |
| 32 | scanf("%d", &n); |
| 33 | Buffer[n] = 1; // expected-warning {{Out of bound memory access }} |
| 34 | } |
Anna Zaks | 0d339d0 | 2011-11-17 23:07:28 +0000 | [diff] [blame] | 35 | |
| 36 | void bufferScanfArithmetic1(int x) { |
| 37 | int n; |
| 38 | scanf("%d", &n); |
| 39 | int m = (n - 3); |
| 40 | Buffer[m] = 1; // expected-warning {{Out of bound memory access }} |
| 41 | } |
| 42 | |
| 43 | void bufferScanfArithmetic2(int x) { |
| 44 | int n; |
| 45 | scanf("%d", &n); |
Anna Zaks | 3881c69 | 2011-11-28 20:43:40 +0000 | [diff] [blame] | 46 | int m = 100 / (n + 3) * x; |
Anna Zaks | 0d339d0 | 2011-11-17 23:07:28 +0000 | [diff] [blame] | 47 | Buffer[m] = 1; // expected-warning {{Out of bound memory access }} |
| 48 | } |
Anna Zaks | 8f4caf5 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 49 | |
Anna Zaks | 3881c69 | 2011-11-28 20:43:40 +0000 | [diff] [blame] | 50 | void bufferScanfAssignment(int x) { |
| 51 | int n; |
| 52 | scanf("%d", &n); |
| 53 | int m; |
| 54 | if (x > 0) { |
| 55 | m = n; |
| 56 | Buffer[m] = 1; // expected-warning {{Out of bound memory access }} |
| 57 | } |
| 58 | } |
| 59 | |
Anna Zaks | 8f4caf5 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 60 | void scanfArg() { |
| 61 | int t; |
Anna Zaks | e3d250e | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 62 | scanf("%d", t); // expected-warning {{conversion specifies type 'int *' but the argument has type 'int'}} |
Anna Zaks | 8f4caf5 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 63 | } |
Anna Zaks | 3881c69 | 2011-11-28 20:43:40 +0000 | [diff] [blame] | 64 | |
| 65 | void bufferGetchar(int x) { |
| 66 | int m = getchar(); |
| 67 | Buffer[m] = 1; //expected-warning {{Out of bound memory access }} |
| 68 | } |
Anna Zaks | 9f03b62 | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 69 | |
Anna Zaks | 1fb826a | 2012-01-12 02:22:34 +0000 | [diff] [blame^] | 70 | void testUncontrolledFormatString(char **p) { |
Anna Zaks | 9f03b62 | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 71 | char s[80]; |
| 72 | fscanf(stdin, "%s", s); |
| 73 | char buf[128]; |
| 74 | sprintf(buf,s); // expected-warning {{Uncontrolled Format String}} |
| 75 | setproctitle(s, 3); // expected-warning {{Uncontrolled Format String}} |
Anna Zaks | 1fb826a | 2012-01-12 02:22:34 +0000 | [diff] [blame^] | 76 | |
| 77 | // Test taint propagation through strcpy and family. |
| 78 | char scpy[80]; |
| 79 | strcpy(scpy, s); |
| 80 | sprintf(buf,scpy); // expected-warning {{Uncontrolled Format String}} |
| 81 | |
| 82 | char spcpy[80]; |
| 83 | stpcpy(spcpy, s); |
| 84 | setproctitle(spcpy, 3); // expected-warning {{Uncontrolled Format String}} |
| 85 | |
| 86 | char sncpy[80]; |
| 87 | strncpy(sncpy, s, 20); |
| 88 | setproctitle(sncpy, 3); // expected-warning {{Uncontrolled Format String}} |
Anna Zaks | 9f03b62 | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 89 | } |