1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
// TODO: remove need for this option
/* { dg-additional-options "-fanalyzer-checker=taint" } */
/* We need this, otherwise the warnings are emitted inside the macros, which
makes it hard to write the DejaGnu directives. */
/* { dg-additional-options " -ftrack-macro-expansion=0" } */
#include "analyzer-decls.h"
/* An assertion macro that has a call to a __noreturn__ function. */
extern void my_assert_fail (const char *expr, const char *file, int line)
__attribute__ ((__noreturn__));
#define MY_ASSERT_1(EXPR) \
do { if (!(EXPR)) my_assert_fail (#EXPR, __FILE__, __LINE__); } while (0)
int
test_not_tainted_MY_ASSERT_1 (int n)
{
MY_ASSERT_1 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return n * n;
}
int __attribute__((tainted_args))
test_tainted_MY_ASSERT_1 (int n)
{
MY_ASSERT_1 (n > 0); /* { dg-warning "use of attacked-controlled value in condition for assertion \\\[CWE-617\\\] \\\[-Wanalyzer-tainted-assertion\\\]" "warning" } */
/* { dg-message "treating 'my_assert_fail' as an assertion failure handler due to '__attribute__\\(\\(__noreturn__\\)\\)'" "final event" { target *-*-* } .-1 } */
return n * n;
}
/* An assertion macro that has a call to __builtin_unreachable. */
#define MY_ASSERT_2(EXPR) \
do { if (!(EXPR)) __builtin_unreachable (); } while (0)
int
test_not_tainted_MY_ASSERT_2 (int n)
{
MY_ASSERT_2 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return n * n;
}
int __attribute__((tainted_args))
test_tainted_MY_ASSERT_2 (int n)
{
MY_ASSERT_2 (n > 0); /* { dg-warning "-Wanalyzer-tainted-assertion" "warning" } */
/* { dg-message "treating '__builtin_unreachable' as an assertion failure handler" "final event" { target *-*-* } .-1 } */
return n * n;
}
/* An assertion macro that's preprocessed away.
The analyzer doesn't see this, so can't warn. */
#define MY_ASSERT_3(EXPR) do { } while (0)
int
test_not_tainted_MY_ASSERT_3 (int n)
{
MY_ASSERT_3 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return n * n;
}
int __attribute__((tainted_args))
test_tainted_MY_ASSERT_3 (int n)
{
MY_ASSERT_3 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return n * n;
}
/* A macro that isn't an assertion. */
extern void do_something_benign ();
#define NOT_AN_ASSERT(EXPR) \
do { if (!(EXPR)) do_something_benign (); } while (0)
int
test_not_tainted_NOT_AN_ASSERT (int n)
{
NOT_AN_ASSERT (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return n * n;
}
int __attribute__((tainted_args))
test_tainted_NOT_AN_ASSERT (int n)
{
NOT_AN_ASSERT (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return n * n;
}
/* A condition that isn't an assertion. */
int __attribute__((tainted_args))
test_tainted_condition (int n)
{
if (n > 0) /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
return 1;
else
return -1;
}
/* More complicated expressions in assertions. */
int g;
void __attribute__((tainted_args))
test_compound_condition_in_assert_1 (int n)
{
MY_ASSERT_1 ((n * 2) < (g + 3)); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_compound_condition_in_assert_2 (int x, int y)
{
MY_ASSERT_1 (x < 100 && y < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_compound_condition_in_assert_3 (int x, int y)
{
MY_ASSERT_1 (x < 100 || y < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_sanitized_expression_in_assert (int n)
{
__analyzer_dump_state ("taint", n); /* { dg-warning "state: 'tainted'" } */
if (n < 0 || n >= 100)
return;
__analyzer_dump_state ("taint", n); /* { dg-warning "state: 'stop'" } */
MY_ASSERT_1 (n < 200); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_sanitization_then_ok_assertion (unsigned n)
{
if (n >= 100)
return;
/* Shouldn't warn here, as g isn't attacker-controlled. */
MY_ASSERT_1 (g > 42); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_good_assert_then_bad_assert (unsigned n)
{
/* Shouldn't warn here, as g isn't attacker-controlled. */
MY_ASSERT_1 (g > 42); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
/* ...but n is: */
MY_ASSERT_1 (n < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_bad_assert_then_good_assert (unsigned n)
{
MY_ASSERT_1 (n < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
MY_ASSERT_1 (g > 42); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
}
/* */
void __attribute__((tainted_args))
test_zero_MY_ASSERT_1 (unsigned n)
{
if (n >= 100)
MY_ASSERT_1 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_nonzero_MY_ASSERT_1 (unsigned n)
{
if (n >= 100)
MY_ASSERT_1 (1); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_zero_MY_ASSERT_2 (unsigned n)
{
if (n >= 100)
MY_ASSERT_2 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
void __attribute__((tainted_args))
test_nonzero_MY_ASSERT_2 (unsigned n)
{
if (n >= 100)
MY_ASSERT_2 (1); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
}
/* Assertions that call a subroutine to do validity checking. */
static int
__analyzer_valid_1 (int x)
{
return x < 100;
}
void __attribute__((tainted_args))
test_assert_calling_valid_1 (int n)
{
MY_ASSERT_1 (__analyzer_valid_1 (n)); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
static int
__analyzer_valid_2 (int x)
{
return x < 100;
}
void __attribute__((tainted_args))
test_assert_calling_valid_2 (int n)
{
MY_ASSERT_1 (__analyzer_valid_2 (n)); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
static int
__analyzer_valid_3 (int x, int y)
{
if (x >= 100)
return 0;
if (y >= 100)
return 0;
return 1;
}
void __attribute__((tainted_args))
test_assert_calling_valid_3 (int a, int b)
{
MY_ASSERT_1 (__analyzer_valid_3 (a, b)); /* { dg-warning "-Wanalyzer-tainted-assertion" "TODO" { xfail *-*-* } } */
}
/* 'switch' statements with supposedly unreachable cases/defaults. */
int __attribute__((tainted_args))
test_switch_default (int n)
{
switch (n) /* { dg-message "use of attacker-controlled value for control flow" "why" } */
/* { dg-message "following 'default:' branch" "dest" { target *-*-* } .-1 } */
{
case 0:
return 5;
case 1:
return 22;
case 2:
return -1;
default:
/* The wording is rather inaccurate here. */
__builtin_unreachable (); /* { dg-warning "use of attacked-controlled value in condition for assertion" } */
}
}
int __attribute__((tainted_args))
test_switch_unhandled_case (int n)
{
switch (n) /* { dg-message "use of attacker-controlled value for control flow" "why" } */
/* { dg-message "following 'default:' branch" "dest" { target *-*-* } .-1 } */
{
case 0:
return 5;
case 1:
return 22;
case 2:
return -1;
}
/* The wording is rather inaccurate here. */
__builtin_unreachable (); /* { dg-warning "use of attacked-controlled value in condition for assertion" } */
}
int __attribute__((tainted_args))
test_switch_bogus_case_MY_ASSERT_1 (int n)
{
switch (n)
{
default:
case 0:
return 5;
case 1:
return 22;
case 2:
return -1;
case 42:
MY_ASSERT_1 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
}
int __attribute__((tainted_args))
test_switch_bogus_case_MY_ASSERT_2 (int n)
{
switch (n)
{
default:
case 0:
return 5;
case 1:
return 22;
case 2:
return -1;
case 42:
MY_ASSERT_2 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
}
int __attribute__((tainted_args))
test_switch_bogus_case_unreachable (int n)
{
switch (n)
{
default:
case 0:
return 5;
case 1:
return 22;
case 2:
return -1;
case 42:
/* This case gets optimized away before we see it. */
__builtin_unreachable ();
}
}
/* Contents of a struct. */
struct s
{
int x;
int y;
};
int __attribute__((tainted_args))
test_assert_struct (struct s *p)
{
MY_ASSERT_1 (p->x < p->y); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
}
|