Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/taint-write-offset-1.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 struct foo
6 {
7 ssize_t offset;
8 };
9
10 char *p;
11
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 *(p + tmp.offset) = 42; // { dg-warning "use of attacker-controlled value 'tmp.offset' as offset without bounds checking" "warning" } */
21 /* { dg-message "\\(\[0-9\]+\\) \\.\\.\\.to here" "event: to here" { target *-*-* } .-1 } */
22 /* { dg-message "\\(\[0-9\]+\\) 'tmp.offset' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.offset has an unchecked value" { xfail *-*-* } .-2 } */
23 /* { dg-message "\\(\[0-9\]+\\) use of attacker-controlled value 'tmp.offset' as offset 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)
31 {
32 /* not a bug: the data is not known to be tainted: */
33 *(p + f->offset) = 42;
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.offset >= 0 && tmp.offset < 256) {
42 /* not a bug: the access is guarded by upper and lower bounds: */
43 *(p + tmp.offset) = 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.offset >= 0) { /* { dg-message "'tmp.offset' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.offset has an unchecked value" { xfail *-*-* } } */
55 /* { dg-message "'tmp.offset' has its lower bound checked here" "event: lower bound checked" { xfail *-*-* } .-1 } */
56 *(p + tmp.offset) = 42; /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset 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.offset < 256) { /* { dg-message "'tmp.offset' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.offset has an unchecked value" { xfail *-*-* } } */
68 /* { dg-message "'tmp.offset' has its upper bound checked here" "event: upper bound checked" { xfail *-*-* } .-1 } */
69 *(p + tmp.offset) = 42; /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset without lower-bounds checking" "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 offset;
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 *(p + tmp.offset) = 42; /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset 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.offset >= 0) {
98 *(p + tmp.offset) = 42; /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset 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.offset < 256) {
110 /* not a bug: has an upper bound, and an implicit lower bound: */
111 *(p + tmp.offset) = 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.offset == 42) {
123 /* not a bug: tmp.offset compared against a specific value: */
124 *(p + tmp.offset) = 42; /* { dg-bogus "tainted" "" { xfail *-*-* } } */
125 // TODO: xfail
126 }
127 }
128 return 0;
129 }