Debian Codegraph @

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