Debian Codegraph @

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