Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/taint-alloc-1.c

raw
1 #include "analyzer-decls.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 struct foo
7 {
8 size_t sz;
9 };
10
11 /* malloc with tainted size. */
12
13 void *test_1 (FILE *f)
14 {
15 struct foo tmp;
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 __analyzer_dump_state ("taint", tmp.sz); /* { dg-warning "state: 'tainted'" } */
19 /* { dg-message "\\(\[0-9\]+\\) \\.\\.\\.to here" "event: to here" { target *-*-* } .-1 } */
20
21 return malloc (tmp.sz); /* { dg-warning "use of attacker-controlled value 'tmp\\.sz' as allocation size without upper-bounds checking" "warning" } */
22 /* { dg-message "23: \\(\[0-9\]+\\) 'tmp.i' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.i has an unchecked value" { xfail *-*-* } .-1 } */
23 /* { dg-message "\\(\[0-9\]+\\) use of attacker-controlled value 'tmp\\.sz' as allocation size without upper-bounds checking" "final event" { target *-*-* } .-2 } */
24 /* { dg-message "heap-based allocation" "memory space" { target *-*-* } .-3 } */
25
26 // TOOD: better messages for state changes
27 }
28 return 0;
29 }
30
31 /* VLA with tainted size. */
32
33 void *test_2 (FILE *f)
34 {
35 struct foo tmp;
36 if (1 == fread(&tmp, sizeof(tmp), 1, f)) { /* { dg-message "\\(\[0-9\]+\\) 'tmp' gets an unchecked value here" "event: tmp gets unchecked value" { xfail *-*-* } } */
37 /* { dg-message "\\(\[0-9\]+\\) following 'true' branch\\.\\.\\." "event: following true branch" { target *-*-* } .-1 } */
38 __analyzer_dump_state ("taint", tmp.sz); /* { dg-warning "state: 'tainted'" } */
39 /* { dg-message "\\(\[0-9\]+\\) \\.\\.\\.to here" "event: to here" { target *-*-* } .-1 } */
40
41 /* VLA with tainted size. */
42 {
43 char buf[tmp.sz]; /* { dg-warning "use of attacker-controlled value 'tmp\\.sz' as allocation size without upper-bounds checking" "warning" } */
44 /* { dg-message "\\(\[0-9\]+\\) 'tmp.i' has an unchecked value here \\(from 'tmp'\\)" "event: tmp.i has an unchecked value" { xfail *-*-* } .-1 } */
45 /* { dg-message "\\(\[0-9\]+\\) use of attacker-controlled value 'tmp\\.sz' as allocation size without upper-bounds checking" "final event" { target *-*-* } .-2 } */
46 /* { dg-message "stack-based allocation" "memory space" { target *-*-* } .-3 } */
47 fread (buf, tmp.sz, 1, f);
48 }
49
50 // TOOD: better messages for state changes
51 }
52 return 0;
53 }
54
55 void *test_3 (FILE *f)
56 {
57 int num;
58 fread (&num, sizeof (int), 1, f);
59 __analyzer_dump_state ("taint", num); /* { dg-warning "state: 'tainted'" } */
60 __analyzer_dump_state ("taint", num * 16); /* { dg-warning "state: 'tainted'" } */
61 __analyzer_dump_state ("taint", (size_t)(num * 16)); /* { dg-warning "state: 'tainted'" } */
62 return malloc (num * 16); /* { dg-warning "use of attacker-controlled value 'num \\* 16' as allocation size without upper-bounds checking" } */
63 }