2 * smatch/check_overflow.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
14 #include "smatch_extra.h"
22 * This check has two smatch IDs.
23 * my_used_id - keeps a record of array offsets that have been used.
24 * If the code checks that they are within bounds later on,
25 * we complain about using an array offset before checking
26 * that it is within bounds.
28 static int my_used_id
;
30 static struct symbol
*this_func
;
32 static void match_function_def(struct symbol
*sym
)
41 static struct limiter b0_l2
= {0, 2};
42 static struct limiter b1_l2
= {1, 2};
44 static void delete(struct sm_state
*sm
, struct expression
*mod_expr
)
46 set_state(my_used_id
, sm
->name
, sm
->sym
, &undefined
);
49 static int definitely_just_used_as_limiter(struct expression
*array
, struct expression
*offset
)
52 struct expression
*tmp
;
56 if (!get_implied_value(offset
, &sval
))
58 if (get_array_size(array
) != sval
.value
)
61 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
66 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
68 if (tmp
->op
== '.' && !dot_ops
++)
70 if (step
== 1 && tmp
->op
== '&') {
74 if (step
== 2 && tmp
->type
== EXPR_COMPARE
)
76 if (step
== 2 && tmp
->type
== EXPR_ASSIGNMENT
)
79 } END_FOR_EACH_PTR_REVERSE(tmp
);
83 static int get_the_max(struct expression
*expr
, sval_t
*sval
)
85 if (get_hard_max(expr
, sval
))
89 if (get_fuzzy_max(expr
, sval
))
91 if (is_user_data(expr
))
92 return get_absolute_max(expr
, sval
);
96 static int common_false_positives(char *name
)
101 /* Smatch can't figure out glibc's strcmp __strcmp_cg()
102 * so it prints an error every time you compare to a string
103 * literal array with 4 or less chars.
105 if (strcmp(name
, "__s1") == 0 || strcmp(name
, "__s2") == 0)
109 * passing WORK_CPU_UNBOUND is idiomatic but Smatch doesn't understand
110 * how it's used so it causes a bunch of false positives.
112 if (option_project
== PROJ_KERNEL
&&
113 strcmp(name
, "__per_cpu_offset") == 0)
118 static void array_check(struct expression
*expr
)
120 struct expression
*array_expr
;
122 struct expression
*offset
;
126 expr
= strip_expr(expr
);
130 array_expr
= strip_parens(expr
->unop
->left
);
131 array_size
= get_array_size(array_expr
);
132 if (!array_size
|| array_size
== 1)
135 offset
= get_array_offset(expr
);
136 if (!get_the_max(offset
, &max
)) {
137 if (getting_address())
139 if (is_capped(offset
))
141 set_state_expr(my_used_id
, offset
, alloc_state_num(array_size
));
142 } else if (array_size
<= max
.value
) {
143 const char *level
= "error";
145 if (getting_address())
148 if (definitely_just_used_as_limiter(array_expr
, offset
))
151 name
= expr_to_str(array_expr
);
152 if (!common_false_positives(name
)) {
153 sm_msg("%s: buffer overflow '%s' %d <= %s",
154 level
, name
, array_size
, sval_to_str(max
));
160 static void match_condition(struct expression
*expr
)
164 struct state_list
*slist
;
165 struct sm_state
*tmp
;
168 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
170 if (get_macro_name(expr
->pos
))
172 if (get_implied_value(expr
->left
, &sval
))
174 else if (get_implied_value(expr
->right
, &sval
))
180 slist
= get_possible_states_expr(my_used_id
, expr
->right
);
182 slist
= get_possible_states_expr(my_used_id
, expr
->left
);
185 FOR_EACH_PTR(slist
, tmp
) {
186 if (tmp
->state
== &merged
|| tmp
->state
== &undefined
)
188 boundary
= PTR_INT(tmp
->state
->data
);
189 boundary
-= sval
.value
;
190 if (boundary
< 1 && boundary
> -1) {
193 name
= expr_to_var(left
? expr
->right
: expr
->left
);
194 sm_msg("error: testing array offset '%s' after use.", name
);
197 } END_FOR_EACH_PTR(tmp
);
200 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
202 struct expression
*dest
;
203 struct expression
*data
;
204 char *dest_name
= NULL
;
205 char *data_name
= NULL
;
209 dest
= get_argument_from_call_expr(expr
->args
, 0);
210 data
= get_argument_from_call_expr(expr
->args
, 1);
211 dest_size
= get_array_size_bytes(dest
);
212 data_size
= get_array_size_bytes(data
);
217 /* If the size of both arrays is known and the destination
218 * buffer is larger than the source buffer, we're okay.
220 if (data_size
&& dest_size
>= data_size
)
223 dest_name
= expr_to_str(dest
);
224 data_name
= expr_to_str(data
);
227 sm_msg("error: %s() '%s' too large for '%s' (%d vs %d)",
228 fn
, data_name
, dest_name
, data_size
, dest_size
);
229 else if (option_spammy
)
230 sm_msg("warn: %s() '%s' of unknown size might be too large for '%s'",
231 fn
, data_name
, dest_name
);
233 free_string(dest_name
);
234 free_string(data_name
);
237 static void match_snprintf(const char *fn
, struct expression
*expr
, void *unused
)
239 struct expression
*dest
;
240 struct expression
*dest_size_expr
;
241 struct expression
*format_string
;
242 struct expression
*data
;
243 char *data_name
= NULL
;
249 dest
= get_argument_from_call_expr(expr
->args
, 0);
250 dest_size_expr
= get_argument_from_call_expr(expr
->args
, 1);
251 format_string
= get_argument_from_call_expr(expr
->args
, 2);
252 data
= get_argument_from_call_expr(expr
->args
, 3);
254 dest_size
= get_array_size_bytes(dest
);
255 if (!get_implied_value(dest_size_expr
, &limit_size
))
257 if (dest_size
&& dest_size
< limit_size
.value
)
258 sm_msg("error: snprintf() is printing too much %s vs %d",
259 sval_to_str(limit_size
), dest_size
);
260 format
= expr_to_var(format_string
);
263 if (strcmp(format
, "\"%s\""))
265 data_name
= expr_to_str(data
);
266 data_size
= get_array_size_bytes(data
);
267 if (limit_size
.value
< data_size
)
268 sm_msg("error: snprintf() chops off the last chars of '%s': %d vs %s",
269 data_name
, data_size
, sval_to_str(limit_size
));
271 free_string(data_name
);
275 static void match_sprintf(const char *fn
, struct expression
*expr
, void *unused
)
277 struct expression
*dest
;
278 struct expression
*format_string
;
279 struct expression
*data
;
280 char *data_name
= NULL
;
285 dest
= get_argument_from_call_expr(expr
->args
, 0);
286 format_string
= get_argument_from_call_expr(expr
->args
, 1);
287 data
= get_argument_from_call_expr(expr
->args
, 2);
289 dest_size
= get_array_size_bytes(dest
);
292 format
= expr_to_var(format_string
);
295 if (strcmp(format
, "\"%s\""))
297 data_name
= expr_to_str(data
);
298 data_size
= get_array_size_bytes(data
);
299 if (dest_size
< data_size
)
300 sm_msg("error: sprintf() copies too much data from '%s': %d vs %d",
301 data_name
, data_size
, dest_size
);
303 free_string(data_name
);
307 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
309 struct limiter
*limiter
= (struct limiter
*)_limiter
;
310 struct expression
*dest
;
311 struct expression
*data
;
312 char *dest_name
= NULL
;
316 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
317 data
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
318 if (!get_the_max(data
, &needed
))
320 has
= get_array_size_bytes_max(dest
);
323 if (has
>= needed
.value
)
326 dest_name
= expr_to_str(dest
);
327 sm_msg("error: %s() '%s' too small (%d vs %s)", fn
, dest_name
, has
, sval_to_str(needed
));
328 free_string(dest_name
);
331 static void db_returns_buf_size(struct expression
*expr
, int param
, char *unused
, char *math
)
333 struct expression
*call
;
334 struct symbol
*left_type
, *right_type
;
338 if (expr
->type
!= EXPR_ASSIGNMENT
)
340 right_type
= get_pointer_type(expr
->right
);
341 if (!right_type
|| right_type
->bit_size
!= -1)
344 call
= strip_expr(expr
->right
);
345 left_type
= get_pointer_type(expr
->left
);
347 if (!parse_call_math(call
, math
, &sval
) || sval
.value
== 0)
351 bytes
= bits_to_bytes(left_type
->bit_size
);
354 if (sval
.uvalue
>= bytes
)
356 sm_msg("error: not allocating enough data %d vs %s", bytes
, sval_to_str(sval
));
359 static void register_funcs_from_file(void)
365 struct limiter
*limiter
;
367 snprintf(name
, 256, "%s.sizeof_param", option_project_str
);
369 token
= get_tokens_file(name
);
372 if (token_type(token
) != TOKEN_STREAMBEGIN
)
375 while (token_type(token
) != TOKEN_STREAMEND
) {
376 if (token_type(token
) != TOKEN_IDENT
)
378 func
= show_ident(token
->ident
);
381 if (token_type(token
) != TOKEN_NUMBER
)
383 size
= atoi(token
->number
);
386 if (token_type(token
) != TOKEN_NUMBER
)
388 buf
= atoi(token
->number
);
390 limiter
= malloc(sizeof(*limiter
));
391 limiter
->limit_arg
= size
;
392 limiter
->buf_arg
= buf
;
394 add_function_hook(func
, &match_limited
, limiter
);
401 void check_overflow(int id
)
404 register_funcs_from_file();
405 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
406 add_hook(&array_check
, OP_HOOK
);
407 add_hook(&match_condition
, CONDITION_HOOK
);
408 add_function_hook("strcpy", &match_strcpy
, NULL
);
409 add_function_hook("snprintf", &match_snprintf
, NULL
);
410 add_function_hook("sprintf", &match_sprintf
, NULL
);
411 add_function_hook("memcmp", &match_limited
, &b0_l2
);
412 add_function_hook("memcmp", &match_limited
, &b1_l2
);
413 select_return_states_hook(BUF_SIZE
, &db_returns_buf_size
);
414 add_modification_hook(my_used_id
, &delete);
415 if (option_project
== PROJ_KERNEL
) {
416 add_function_hook("copy_to_user", &match_limited
, &b0_l2
);
417 add_function_hook("copy_to_user", &match_limited
, &b1_l2
);
418 add_function_hook("_copy_to_user", &match_limited
, &b0_l2
);
419 add_function_hook("_copy_to_user", &match_limited
, &b1_l2
);
420 add_function_hook("__copy_to_user", &match_limited
, &b0_l2
);
421 add_function_hook("__copy_to_user", &match_limited
, &b1_l2
);
422 add_function_hook("copy_from_user", &match_limited
, &b0_l2
);
423 add_function_hook("copy_from_user", &match_limited
, &b1_l2
);
424 add_function_hook("_copy_from_user", &match_limited
, &b0_l2
);
425 add_function_hook("_copy_from_user", &match_limited
, &b1_l2
);
426 add_function_hook("__copy_from_user", &match_limited
, &b0_l2
);
427 add_function_hook("__copy_from_user", &match_limited
, &b1_l2
);