-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_index_overflow.c
404 lines (334 loc) · 9.21 KB
/
check_index_overflow.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (C) 2010 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include <stdlib.h>
#include "parse.h"
#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"
static int loop_id;
STATE(loop_end);
static int definitely_just_used_as_limiter(struct expression *array, struct expression *offset)
{
sval_t sval;
struct expression *tmp;
if (!get_implied_value(offset, &sval))
return 0;
if (get_array_size(array) != sval.value)
return 0;
tmp = array;
while ((tmp = expr_get_parent_expr(tmp))) {
if (tmp->type == EXPR_PREOP && tmp->op == '&')
return 1;
}
return 0;
}
static int fake_get_hard_max(struct expression *expr, sval_t *sval)
{
struct range_list *implied_rl;
if (!get_hard_max(expr, sval))
return 0;
/*
* The problem is that hard_max doesn't care about minimums
* properly. So if you give it thing like:
* err = (-10)-(-1)
* __smatch_hard_max(-err);
*
* Then it returns s32max instead of 10.
*/
if (get_implied_rl(expr, &implied_rl) &&
sval_cmp(rl_max(implied_rl), *sval) < 0)
*sval = rl_max(implied_rl);
return 1;
}
static int get_the_max(struct expression *expr, sval_t *sval)
{
struct range_list *rl;
if (get_hard_max(expr, sval)) {
struct range_list *implied_rl;
/*
* The problem is that hard_max doesn't care about minimums
* properly. So if you give it thing like:
* err = (-10)-(-1)
* __smatch_hard_max(-err);
*
* Then it returns s32max instead of 10.
*/
if (get_implied_rl(expr, &implied_rl) &&
sval_cmp(rl_max(implied_rl), *sval) < 0)
*sval = rl_max(implied_rl);
return 1;
}
if (!option_spammy)
return 0;
/* Fixme: use fuzzy max */
if (!get_user_rl(expr, &rl))
return 0;
if (rl_max(rl).uvalue > sval_type_max(rl_type(rl)).uvalue - 4 &&
is_capped(expr))
return 0;
*sval = rl_max(rl);
return 1;
}
static int common_false_positives(struct expression *array, sval_t max)
{
char *name;
int ret;
name = expr_to_str(array);
/* Smatch can't figure out glibc's strcmp __strcmp_cg()
* so it prints an error every time you compare to a string
* literal array with 4 or less chars.
*/
if (name &&
(strcmp(name, "__s1") == 0 || strcmp(name, "__s2") == 0)) {
ret = 1;
goto free;
}
/* Ugh... People are saying that Smatch still barfs on glibc strcmp()
* functions.
*/
if (array) {
char *macro;
/* why is this again??? */
if (array->type == EXPR_STRING &&
max.value == array->string->length) {
ret = 1;
goto free;
}
macro = get_macro_name(array->pos);
if (macro && max.uvalue < 4 &&
(strcmp(macro, "strcmp") == 0 ||
strcmp(macro, "strncmp") == 0 ||
strcmp(macro, "streq") == 0 ||
strcmp(macro, "strneq") == 0 ||
strcmp(macro, "strsep") == 0)) {
ret = 1;
goto free;
}
}
/*
* passing WORK_CPU_UNBOUND is idiomatic but Smatch doesn't understand
* how it's used so it causes a bunch of false positives.
*/
if (option_project == PROJ_KERNEL && name &&
strcmp(name, "__per_cpu_offset") == 0) {
ret = 1;
goto free;
}
ret = 0;
free:
free_string(name);
return ret;
}
static int is_subtract(struct expression *expr)
{
struct expression *tmp;
int cnt = 0;
expr = strip_expr(expr);
while ((tmp = get_assigned_expr(expr))) {
expr = strip_expr(tmp);
if (++cnt > 5)
break;
}
if (expr->type == EXPR_BINOP && expr->op == '-')
return 1;
return 0;
}
static int constraint_met(struct expression *array_expr, struct expression *offset)
{
char *data_str, *required, *unmet;
int ret = 0;
data_str = get_constraint_str(array_expr);
if (!data_str)
return 0;
required = get_required_constraint(data_str);
if (!required)
goto free_data_str;
unmet = unmet_constraint(array_expr, offset);
if (!unmet)
ret = 1;
free_string(unmet);
free_string(required);
free_data_str:
free_string(data_str);
return ret;
}
static bool is_zero_size_memcpy(struct expression *expr, int size, struct range_list *rl)
{
struct expression *parent;
/*
* Often times we have code like this:
* memcpy(array[idx], src, size)
* In this example if "idx == ARRAY_SIZE()" then "size" is zero so
* nothing is copied and the code is fine and Smatch should not
* print a warning even though the idx is one element out of bounds.
*
* TODO: if we wanted to be very accurate we could find the length
* expression and assume() that offset == rl_max() and then test that
* the length expression is zero. But that seems like a lot of work.
* HashtagLazy.
*/
if (rl_max(rl).value != size)
return false;
parent = expr;
while ((parent = expr_get_parent_expr(parent))) {
if (parent->type == EXPR_PREOP &&
(parent->op == '(' || parent->op == '&'))
continue;
if (parent->type == EXPR_CAST)
continue;
break;
}
if (!parent || parent->type != EXPR_CALL ||
parent->fn->type != EXPR_SYMBOL || !parent->fn->symbol_name)
return false;
if (strstr(parent->fn->symbol_name->name, "memcpy") ||
strstr(parent->fn->symbol_name->name, "memset"))
return true;
return false;
}
static int should_warn(struct expression *expr)
{
struct expression *array_expr;
struct range_list *abs_rl;
sval_t hard_max = { .type = &int_ctype, };
sval_t fuzzy_max = { .type = &int_ctype, };
int array_size;
struct expression *offset;
sval_t max;
expr = strip_expr(expr);
if (!is_array(expr))
return 0;
if (is_impossible_path())
return 0;
array_expr = get_array_base(expr);
array_size = get_array_size(array_expr);
if (!array_size || array_size == 1)
return 0;
offset = get_array_offset(expr);
get_absolute_rl(offset, &abs_rl);
fake_get_hard_max(offset, &hard_max);
get_fuzzy_max(offset, &fuzzy_max);
if (!get_the_max(offset, &max))
return 0;
if (array_size > max.value)
return 0;
if (buf_comparison_index_ok(expr))
return 0;
if (constraint_met(array_expr, offset))
return 0;
if (array_size > rl_max(abs_rl).uvalue)
return 0;
if (definitely_just_used_as_limiter(array_expr, offset))
return 0;
array_expr = strip_expr(array_expr);
if (common_false_positives(array_expr, max))
return 0;
if (impossibly_high_comparison(offset))
return 0;
if (is_zero_size_memcpy(expr, array_size, abs_rl))
return 0;
return 1;
}
static int is_because_of_no_break(struct expression *offset)
{
if (get_state_expr(loop_id, offset) == &loop_end)
return 1;
return 0;
}
static void array_check(struct expression *expr)
{
struct expression *array_expr;
struct range_list *abs_rl;
struct range_list *user_rl = NULL;
sval_t hard_max = { .type = &int_ctype, };
sval_t fuzzy_max = { .type = &int_ctype, };
int array_size;
struct expression *array_size_value, *comparison;
struct expression *offset;
sval_t max;
char *name;
int no_break = 0;
if (!should_warn(expr))
return;
expr = strip_expr(expr);
array_expr = get_array_base(expr);
array_size = get_array_size(array_expr);
offset = get_array_offset(expr);
/*
* Perhaps if the offset is out of bounds that means a constraint
* applies or maybe it means we are on an impossible path. So test
* again based on that assumption.
*
*/
array_size_value = value_expr(array_size);
comparison = compare_expression(offset, SPECIAL_GTE, array_size_value);
if (assume(comparison)) {
if (!should_warn(expr)) {
end_assume();
return;
}
no_break = is_because_of_no_break(offset);
end_assume();
}
get_absolute_rl(offset, &abs_rl);
get_user_rl(offset, &user_rl);
fake_get_hard_max(offset, &hard_max);
get_fuzzy_max(offset, &fuzzy_max);
array_expr = strip_expr(array_expr);
name = expr_to_str(array_expr);
if (user_rl)
max = rl_max(user_rl);
else
max = rl_max(abs_rl);
if (!option_spammy && is_subtract(offset))
return;
if (no_break) {
sm_error("buffer overflow '%s' %d <= %s (assuming for loop doesn't break)",
name, array_size, sval_to_str(max));
} else if (user_rl) {
sm_error("buffer overflow '%s' %d <= %s user_rl='%s'%s",
name, array_size, sval_to_str(max), show_rl(user_rl),
is_subtract(offset) ? " subtract" : "");
} else {
sm_error("buffer overflow '%s' %d <= %s%s",
name, array_size, sval_to_str(max),
is_subtract(offset) ? " subtract" : "");
}
free_string(name);
}
void check_index_overflow(int id)
{
add_hook(&array_check, OP_HOOK);
}
static void match_condition(struct expression *expr)
{
struct statement *stmt;
if (expr->type != EXPR_COMPARE)
return;
if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
return;
stmt = expr_get_parent_stmt(expr);
if (!stmt || stmt->type != STMT_ITERATOR)
return;
set_true_false_states_expr(loop_id, expr->left, NULL, &loop_end);
}
void check_index_overflow_loop_marker(int id)
{
loop_id = id;
add_hook(&match_condition, CONDITION_HOOK);
add_modification_hook(loop_id, &set_undefined);
}