Debian Codegraph @

clang/test/Analysis/taint-generic.cpp

raw
1 // RUN: %clang_analyze_cc1 -std=c++11 -Wno-format-security \
2 // RUN: -analyzer-checker=core,optin.taint,security.ArrayBound,debug.ExprInspection \
3 // RUN: -analyzer-config optin.taint.TaintPropagation:Config=%S/Inputs/taint-generic-config.yaml \
4 // RUN: -verify %s
5
6 template <typename T> void clang_analyzer_isTainted(T);
7
8 #define BUFSIZE 10
9 int Buffer[BUFSIZE];
10
11 int scanf(const char*, ...);
12 template <typename T = int> T mySource1();
13 int mySource3();
14
15 typedef struct _FILE FILE;
16 extern "C" {
17 extern FILE *stdin;
18 }
19 int fscanf(FILE *stream, const char *format, ...);
20
21 bool isOutOfRange2(const int*);
22
23 void mySink2(int);
24
25 // Test configuration
26 namespace myNamespace {
27 void scanf(const char*, ...);
28 void myScanf(const char*, ...);
29 int mySource3();
30
31 bool isOutOfRange(const int*);
32 bool isOutOfRange2(const int*);
33
34 void mySink(int, int, int);
35 void mySink2(int);
36 }
37
38 namespace myAnotherNamespace {
39 int mySource3();
40
41 bool isOutOfRange2(const int*);
42
43 void mySink2(int);
44 }
45
46 void testConfigurationNamespacePropagation1() {
47 int x;
48 // The built-in functions should be matched only for functions in
49 // the global namespace
50 myNamespace::scanf("%d", &x);
51 Buffer[x] = 1; // no-warning
52
53 scanf("%d", &x);
54 Buffer[x] = 1; // expected-warning {{Potential out of bound access }}
55 }
56
57 void testConfigurationNamespacePropagation2() {
58 int x = mySource3();
59 Buffer[x] = 1; // no-warning
60
61 int y = myNamespace::mySource3();
62 Buffer[y] = 1; // expected-warning {{Potential out of bound access }}
63 }
64
65 void testConfigurationNamespacePropagation3() {
66 int x = myAnotherNamespace::mySource3();
67 Buffer[x] = 1; // expected-warning {{Potential out of bound access }}
68 }
69
70 void testConfigurationNamespacePropagation4() {
71 int x;
72 // Configured functions without scope should match for all function.
73 myNamespace::myScanf("%d", &x);
74 Buffer[x] = 1; // expected-warning {{Potential out of bound access }}
75 }
76
77 void testConfigurationNamespaceFilter1() {
78 int x = mySource1();
79 if (myNamespace::isOutOfRange2(&x))
80 return;
81 Buffer[x] = 1; // no-warning
82
83 int y = mySource1();
84 if (isOutOfRange2(&y))
85 return;
86 Buffer[y] = 1; // expected-warning {{Potential out of bound access }}
87 }
88
89 void testConfigurationNamespaceFilter2() {
90 int x = mySource1();
91 if (myAnotherNamespace::isOutOfRange2(&x))
92 return;
93 Buffer[x] = 1; // no-warning
94 }
95
96 void testConfigurationNamespaceFilter3() {
97 int x = mySource1();
98 if (myNamespace::isOutOfRange(&x))
99 return;
100 Buffer[x] = 1; // no-warning
101 }
102
103 void testConfigurationNamespaceSink1() {
104 int x = mySource1();
105 mySink2(x); // no-warning
106
107 int y = mySource1();
108 myNamespace::mySink2(y);
109 // expected-warning@-1 {{Untrusted data is passed to a user-defined sink}}
110 }
111
112 void testConfigurationNamespaceSink2() {
113 int x = mySource1();
114 myAnotherNamespace::mySink2(x);
115 // expected-warning@-1 {{Untrusted data is passed to a user-defined sink}}
116 }
117
118 void testConfigurationNamespaceSink3() {
119 int x = mySource1();
120 myNamespace::mySink(x, 0, 1);
121 // expected-warning@-1 {{Untrusted data is passed to a user-defined sink}}
122 }
123
124 struct Foo {
125 void scanf(const char*, int*);
126 void myMemberScanf(const char*, int*);
127 };
128
129 void testConfigurationMemberFunc() {
130 int x;
131 Foo foo;
132 foo.scanf("%d", &x);
133 Buffer[x] = 1; // no-warning
134
135 foo.myMemberScanf("%d", &x);
136 Buffer[x] = 1; // expected-warning {{Potential out of bound access }}
137 }
138
139 void testReadingFromStdin(char **p) {
140 int n;
141 fscanf(stdin, "%d", &n);
142 Buffer[n] = 1; // expected-warning {{Potential out of bound access }}
143 }
144
145 namespace gh114270 {
146 class Empty {};
147 class Aggr {
148 public:
149 int data;
150 };
151
152 void top() {
153 int Int = mySource1<int>();
154 clang_analyzer_isTainted(Int); // expected-warning {{YES}}
155
156 // It's fine to not propagate taint to empty classes, since they don't have any data members.
157 Empty E = mySource1<Empty>();
158 clang_analyzer_isTainted(E); // expected-warning {{NO}}
159
160 Aggr A = mySource1<Aggr>();
161 // FIXME Ideally, both A and A.data should be tainted. However, the
162 // implementation used by e5ac9145ba29 ([analyzer][taint] Recognize
163 // tainted LazyCompoundVals (4/4) (#115919), 2024-11-15) led to FPs and
164 // FNs in various scenarios and had to be reverted to fix #153782.
165 clang_analyzer_isTainted(A); // expected-warning {{NO}}
166 clang_analyzer_isTainted(A.data); // expected-warning {{YES}}
167 }
168 } // namespace gh114270
169
170
171 namespace format_attribute {
172 __attribute__((__format__ (__printf__, 1, 2)))
173 void log_freefunc(const char *fmt, ...);
174
175 void test_format_attribute_freefunc() {
176 int n;
177 fscanf(stdin, "%d", &n); // Get a tainted value.
178
179 log_freefunc("This number is suspicious: %d\n", n); // no-warning
180 }
181
182 struct Foo {
183 // When the format attribute is applied to a method, argumet '1' is the
184 // implicit `this`, so e.g. in this case argument '2' specifies `fmt`.
185 // Specifying '1' instead of '2' would produce a compilation error:
186 // "format attribute cannot specify the implicit this argument as the format string"
187 __attribute__((__format__ (__printf__, 2, 3)))
188 void log_method(const char *fmt, ...);
189
190 void test_format_attribute_method() {
191 int n;
192 fscanf(stdin, "%d", &n); // Get a tainted value.
193
194 // The analyzer used to misinterpret the parameter indices in the format
195 // attribute when the format attribute is applied to a method.
196 log_method("This number is suspicious: %d\n", n); // no-warning
197 }
198
199 __attribute__((__format__ (__printf__, 1, 2)))
200 static void log_static_method(const char *fmt, ...);
201
202 void test_format_attribute_static_method() {
203 int n;
204 fscanf(stdin, "%d", &n); // Get a tainted value.
205
206 log_static_method("This number is suspicious: %d\n", n); // no-warning
207 }
208 };
209
210 } // namespace format_attribute