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(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
46 delete_state(my_used_id
, name
, sym
);
49 static int definitely_just_used_as_limiter(struct expression
*array
, struct expression
*offset
)
52 struct expression
*tmp
;
56 if (!get_implied_value(offset
, &val
))
58 if (get_array_size(array
) != val
)
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
)
77 } END_FOR_EACH_PTR_REVERSE(tmp
);
81 static void array_check(struct expression
*expr
)
83 struct expression
*array_expr
;
85 struct expression
*offset
;
89 expr
= strip_expr(expr
);
93 array_expr
= strip_parens(expr
->unop
->left
);
94 array_size
= get_array_size(array_expr
);
95 if (!array_size
|| array_size
== 1)
98 offset
= get_array_offset(expr
);
99 if (!get_fuzzy_max(offset
, &max
)) {
100 if (getting_address())
102 set_state_expr(my_used_id
, offset
, alloc_state_num(array_size
));
103 add_modification_hook_expr(my_used_id
, offset
, &delete, NULL
);
104 } else if (array_size
<= max
) {
105 const char *level
= "error";
107 if (getting_address())
110 if (definitely_just_used_as_limiter(array_expr
, offset
))
113 if (!option_spammy
) {
114 struct smatch_state
*state
;
116 state
= get_state_expr(SMATCH_EXTRA
, offset
);
117 if (state
&& is_whole_range(state
))
121 name
= get_variable_from_expr_complex(array_expr
, NULL
);
122 /* Blast. Smatch can't figure out glibc's strcmp __strcmp_cg()
123 * so it prints an error every time you compare to a string
124 * literal array with 4 or less chars.
126 if (name
&& strcmp(name
, "__s1") && strcmp(name
, "__s2")) {
127 sm_msg("%s: buffer overflow '%s' %d <= %lld",
128 level
, name
, array_size
, max
);
134 static void match_condition(struct expression
*expr
)
138 struct state_list
*slist
;
139 struct sm_state
*tmp
;
142 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
144 if (get_implied_value(expr
->left
, &val
))
146 else if (get_implied_value(expr
->right
, &val
))
152 slist
= get_possible_states_expr(my_used_id
, expr
->right
);
154 slist
= get_possible_states_expr(my_used_id
, expr
->left
);
157 FOR_EACH_PTR(slist
, tmp
) {
158 if (tmp
->state
== &merged
)
160 boundary
= PTR_INT(tmp
->state
->data
);
162 if (boundary
< 1 && boundary
> -1) {
165 name
= get_variable_from_expr((left
? expr
->right
: expr
->left
), NULL
);
166 sm_msg("error: testing array offset '%s' after use.", name
);
169 } END_FOR_EACH_PTR(tmp
);
172 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
174 struct expression
*dest
;
175 struct expression
*data
;
176 char *dest_name
= NULL
;
177 char *data_name
= NULL
;
181 dest
= get_argument_from_call_expr(expr
->args
, 0);
182 data
= get_argument_from_call_expr(expr
->args
, 1);
183 dest_size
= get_array_size_bytes(dest
);
184 data_size
= get_array_size_bytes(data
);
189 /* If the size of both arrays is known and the destination
190 * buffer is larger than the source buffer, we're okay.
192 if (data_size
&& dest_size
>= data_size
)
195 dest_name
= get_variable_from_expr_complex(dest
, NULL
);
196 data_name
= get_variable_from_expr_complex(data
, NULL
);
199 sm_msg("error: %s() '%s' too large for '%s' (%d vs %d)",
200 fn
, data_name
, dest_name
, data_size
, dest_size
);
201 else if (option_spammy
)
202 sm_msg("warn: %s() '%s' of unknown size might be too large for '%s'",
203 fn
, data_name
, dest_name
);
205 free_string(dest_name
);
206 free_string(data_name
);
209 static void match_snprintf(const char *fn
, struct expression
*expr
, void *unused
)
211 struct expression
*dest
;
212 struct expression
*dest_size_expr
;
213 struct expression
*format_string
;
214 struct expression
*data
;
215 char *data_name
= NULL
;
217 long long limit_size
;
221 dest
= get_argument_from_call_expr(expr
->args
, 0);
222 dest_size_expr
= get_argument_from_call_expr(expr
->args
, 1);
223 format_string
= get_argument_from_call_expr(expr
->args
, 2);
224 data
= get_argument_from_call_expr(expr
->args
, 3);
226 dest_size
= get_array_size_bytes(dest
);
227 if (!get_implied_value(dest_size_expr
, &limit_size
))
229 if (dest_size
&& dest_size
< limit_size
)
230 sm_msg("error: snprintf() is printing too much %lld vs %d", limit_size
, dest_size
);
231 format
= get_variable_from_expr(format_string
, NULL
);
234 if (strcmp(format
, "\"%s\""))
236 data_name
= get_variable_from_expr_complex(data
, NULL
);
237 data_size
= get_array_size_bytes(data
);
238 if (limit_size
< data_size
)
239 sm_msg("error: snprintf() chops off the last chars of '%s': %d vs %lld",
240 data_name
, data_size
, limit_size
);
242 free_string(data_name
);
246 static void match_sprintf(const char *fn
, struct expression
*expr
, void *unused
)
248 struct expression
*dest
;
249 struct expression
*format_string
;
250 struct expression
*data
;
251 char *data_name
= NULL
;
256 dest
= get_argument_from_call_expr(expr
->args
, 0);
257 format_string
= get_argument_from_call_expr(expr
->args
, 1);
258 data
= get_argument_from_call_expr(expr
->args
, 2);
260 dest_size
= get_array_size_bytes(dest
);
263 format
= get_variable_from_expr(format_string
, NULL
);
266 if (strcmp(format
, "\"%s\""))
268 data_name
= get_variable_from_expr_complex(data
, NULL
);
269 data_size
= get_array_size_bytes(data
);
270 if (dest_size
< data_size
)
271 sm_msg("error: sprintf() copies too much data from '%s': %d vs %d",
272 data_name
, data_size
, dest_size
);
274 free_string(data_name
);
278 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
280 struct limiter
*limiter
= (struct limiter
*)_limiter
;
281 struct expression
*dest
;
282 struct expression
*data
;
283 char *dest_name
= NULL
;
287 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
288 data
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
289 if (!get_fuzzy_max(data
, &needed
))
291 has
= get_array_size_bytes(dest
);
297 dest_name
= get_variable_from_expr_complex(dest
, NULL
);
298 sm_msg("error: %s() '%s' too small (%d vs %lld)", fn
, dest_name
, has
, needed
);
299 free_string(dest_name
);
302 void check_overflow(int id
)
305 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
306 add_hook(&array_check
, OP_HOOK
);
307 add_hook(&match_condition
, CONDITION_HOOK
);
308 add_function_hook("strcpy", &match_strcpy
, NULL
);
309 add_function_hook("snprintf", &match_snprintf
, NULL
);
310 add_function_hook("sprintf", &match_sprintf
, NULL
);
311 if (option_project
== PROJ_KERNEL
) {
312 add_function_hook("copy_to_user", &match_limited
, &b0_l2
);
313 add_function_hook("copy_to_user", &match_limited
, &b1_l2
);
314 add_function_hook("_copy_to_user", &match_limited
, &b0_l2
);
315 add_function_hook("_copy_to_user", &match_limited
, &b1_l2
);
316 add_function_hook("__copy_to_user", &match_limited
, &b0_l2
);
317 add_function_hook("__copy_to_user", &match_limited
, &b1_l2
);
318 add_function_hook("copy_from_user", &match_limited
, &b0_l2
);
319 add_function_hook("copy_from_user", &match_limited
, &b1_l2
);
320 add_function_hook("_copy_from_user", &match_limited
, &b0_l2
);
321 add_function_hook("_copy_from_user", &match_limited
, &b1_l2
);
322 add_function_hook("__copy_from_user", &match_limited
, &b0_l2
);
323 add_function_hook("__copy_from_user", &match_limited
, &b1_l2
);