Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/taint-read-index-1.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 struct foo
6 {
7 signed int i;
8 char buf[256];
9 };
10
11 char test_1(FILE *f)
12 {
13 struct foo tmp;
14
15 if (1 == fread(&tmp, sizeof(tmp), 1, f)) { /* { dg-message "\\(\[0-9\]+\\) 'tmp' gets an unchecked value here" "event: tmp gets unchecked value" { xfail *-*-* } } */
16 /* { dg-message "\\(\[0-9\]+\\) following 'true' branch\\.\\.\\." "event: following true branch" { target *-*-* } .-1 } */
17 /* BUG: the following array lookup trusts that the input data's index is
18 in the range 0 <= i < 256; otherwise it's accessing the stack */
19 return tmp.buf[tmp.i]; // { dg-warning "use of attacker-controlled value 'tmp.i' in array lookup without bounds checking" "warning" } */
20 /* { dg-message "23: \\(\[0-9\]+\\) \\.\\.\\.to here" "event: to here" { target *-*-* } .-1 } */
21 /* { dg-message "23: \\(\[0-9\]+\\) 'tmp.i' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.i has an unchecked value" { xfail *-*-* } .-2 } */
22 /* { dg-message "\\(\[0-9\]+\\) use of attacker-controlled value 'tmp.i' in array lookup without bounds checking" "final event" { target *-*-* } .-3 } */
23
24 // TOOD: better messages for state changes
25 }
26 return 0;
27 }
28
29 char test_2(struct foo *f, int i)
30 {
31 /* not a bug: the data is not known to be tainted: */
32 return f->buf[f->i];
33 }
34
35 char test_3(FILE *f)
36 {
37 struct foo tmp;
38
39 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
40 if (tmp.i >= 0 && tmp.i < 256) {
41 /* not a bug: the access is guarded by upper and lower bounds: */
42 return tmp.buf[tmp.i];
43 }
44 }
45 return 0;
46 }
47
48 char test_4(FILE *f)
49 {
50 struct foo tmp;
51
52 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
53 if (tmp.i >= 0) { /* { dg-message "'tmp.i' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.i has an unchecked value" { xfail *-*-* } } */
54 /* { dg-message "'tmp.i' has its lower bound checked here" "event: lower bound checked" { xfail *-*-* } .-1 } */
55 return tmp.buf[tmp.i]; /* { dg-warning "use of attacker-controlled value 'tmp.i' in array lookup without upper-bounds checking" "warning" } */
56 }
57 }
58 return 0;
59 }
60
61 char test_5(FILE *f)
62 {
63 struct foo tmp;
64
65 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
66 if (tmp.i < 256) { /* { dg-message "'tmp.i' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.i has an unchecked value" { xfail *-*-* } } */
67 /* { dg-message "'tmp.i' has its upper bound checked here" "event: upper bound checked" { xfail *-*-* } .-1 } */
68 return tmp.buf[tmp.i]; /* { dg-warning "use of attacker-controlled value 'tmp.i' in array lookup without checking for negative" "warning" } */
69 }
70 }
71 return 0;
72 }
73
74 /* unsigned types have a natural lower bound of 0 */
75 struct bar
76 {
77 unsigned int i;
78 char buf[256];
79 };
80
81 char test_6(FILE *f)
82 {
83 struct bar tmp;
84
85 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
86 return tmp.buf[tmp.i]; /* { dg-warning "use of attacker-controlled value 'tmp.i' in array lookup without upper-bounds checking" } */
87 }
88 return 0;
89 }
90
91 char test_7(FILE *f)
92 {
93 struct bar tmp;
94
95 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
96 if (tmp.i >= 0) {
97 return tmp.buf[tmp.i]; /* { dg-warning "use of attacker-controlled value 'tmp.i' in array lookup without upper-bounds checking" } */
98 }
99 }
100 return 0;
101 }
102
103 char test_8(FILE *f)
104 {
105 struct bar tmp;
106
107 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
108 if (tmp.i < 256) {
109 /* not a bug: has an upper bound, and an implicit lower bound: */
110 return tmp.buf[tmp.i];
111 }
112 }
113 return 0;
114 }
115
116 char test_9(FILE *f)
117 {
118 struct foo tmp;
119
120 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
121 if (tmp.i == 42) {
122 /* not a bug: tmp.i compared against a specific value: */
123 return tmp.buf[tmp.i]; /* { dg-bogus "attacker-controlled" "" { xfail *-*-* } } */
124 // TODO: xfail
125 }
126 }
127 return 0;
128 }