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
)
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
, &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 } else if (array_size
<= max
) {
104 const char *level
= "error";
106 if (getting_address())
109 if (definitely_just_used_as_limiter(array_expr
, offset
))
112 if (!option_spammy
) {
113 struct smatch_state
*state
;
115 state
= get_state_expr(SMATCH_EXTRA
, offset
);
116 if (state
&& is_whole_range(state
))
120 name
= get_variable_from_expr_complex(array_expr
, NULL
);
121 /* Blast. Smatch can't figure out glibc's strcmp __strcmp_cg()
122 * so it prints an error every time you compare to a string
123 * literal array with 4 or less chars.
125 if (name
&& strcmp(name
, "__s1") && strcmp(name
, "__s2")) {
126 sm_msg("%s: buffer overflow '%s' %d <= %lld",
127 level
, name
, array_size
, max
);
133 static void match_condition(struct expression
*expr
)
137 struct state_list
*slist
;
138 struct sm_state
*tmp
;
141 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
143 if (get_macro_name(expr
->pos
))
145 if (get_implied_value(expr
->left
, &val
))
147 else if (get_implied_value(expr
->right
, &val
))
153 slist
= get_possible_states_expr(my_used_id
, expr
->right
);
155 slist
= get_possible_states_expr(my_used_id
, expr
->left
);
158 FOR_EACH_PTR(slist
, tmp
) {
159 if (tmp
->state
== &merged
|| tmp
->state
== &undefined
)
161 boundary
= PTR_INT(tmp
->state
->data
);
163 if (boundary
< 1 && boundary
> -1) {
166 name
= get_variable_from_expr((left
? expr
->right
: expr
->left
), NULL
);
167 sm_msg("error: testing array offset '%s' after use.", name
);
170 } END_FOR_EACH_PTR(tmp
);
173 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
175 struct expression
*dest
;
176 struct expression
*data
;
177 char *dest_name
= NULL
;
178 char *data_name
= NULL
;
182 dest
= get_argument_from_call_expr(expr
->args
, 0);
183 data
= get_argument_from_call_expr(expr
->args
, 1);
184 dest_size
= get_array_size_bytes(dest
);
185 data_size
= get_array_size_bytes(data
);
190 /* If the size of both arrays is known and the destination
191 * buffer is larger than the source buffer, we're okay.
193 if (data_size
&& dest_size
>= data_size
)
196 dest_name
= get_variable_from_expr_complex(dest
, NULL
);
197 data_name
= get_variable_from_expr_complex(data
, NULL
);
200 sm_msg("error: %s() '%s' too large for '%s' (%d vs %d)",
201 fn
, data_name
, dest_name
, data_size
, dest_size
);
202 else if (option_spammy
)
203 sm_msg("warn: %s() '%s' of unknown size might be too large for '%s'",
204 fn
, data_name
, dest_name
);
206 free_string(dest_name
);
207 free_string(data_name
);
210 static void match_snprintf(const char *fn
, struct expression
*expr
, void *unused
)
212 struct expression
*dest
;
213 struct expression
*dest_size_expr
;
214 struct expression
*format_string
;
215 struct expression
*data
;
216 char *data_name
= NULL
;
218 long long limit_size
;
222 dest
= get_argument_from_call_expr(expr
->args
, 0);
223 dest_size_expr
= get_argument_from_call_expr(expr
->args
, 1);
224 format_string
= get_argument_from_call_expr(expr
->args
, 2);
225 data
= get_argument_from_call_expr(expr
->args
, 3);
227 dest_size
= get_array_size_bytes(dest
);
228 if (!get_implied_value(dest_size_expr
, &limit_size
))
230 if (dest_size
&& dest_size
< limit_size
)
231 sm_msg("error: snprintf() is printing too much %lld vs %d", limit_size
, dest_size
);
232 format
= get_variable_from_expr(format_string
, NULL
);
235 if (strcmp(format
, "\"%s\""))
237 data_name
= get_variable_from_expr_complex(data
, NULL
);
238 data_size
= get_array_size_bytes(data
);
239 if (limit_size
< data_size
)
240 sm_msg("error: snprintf() chops off the last chars of '%s': %d vs %lld",
241 data_name
, data_size
, limit_size
);
243 free_string(data_name
);
247 static void match_sprintf(const char *fn
, struct expression
*expr
, void *unused
)
249 struct expression
*dest
;
250 struct expression
*format_string
;
251 struct expression
*data
;
252 char *data_name
= NULL
;
257 dest
= get_argument_from_call_expr(expr
->args
, 0);
258 format_string
= get_argument_from_call_expr(expr
->args
, 1);
259 data
= get_argument_from_call_expr(expr
->args
, 2);
261 dest_size
= get_array_size_bytes(dest
);
264 format
= get_variable_from_expr(format_string
, NULL
);
267 if (strcmp(format
, "\"%s\""))
269 data_name
= get_variable_from_expr_complex(data
, NULL
);
270 data_size
= get_array_size_bytes(data
);
271 if (dest_size
< data_size
)
272 sm_msg("error: sprintf() copies too much data from '%s': %d vs %d",
273 data_name
, data_size
, dest_size
);
275 free_string(data_name
);
279 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
281 struct limiter
*limiter
= (struct limiter
*)_limiter
;
282 struct expression
*dest
;
283 struct expression
*data
;
284 char *dest_name
= NULL
;
288 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
289 data
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
290 if (!get_fuzzy_max(data
, &needed
))
292 has
= get_array_size_bytes(dest
);
298 dest_name
= get_variable_from_expr_complex(dest
, NULL
);
299 sm_msg("error: %s() '%s' too small (%d vs %lld)", fn
, dest_name
, has
, needed
);
300 free_string(dest_name
);
303 static void register_funcs_from_file(void)
309 struct limiter
*limiter
;
311 snprintf(name
, 256, "%s.sizeof_param", option_project_str
);
313 token
= get_tokens_file(name
);
316 if (token_type(token
) != TOKEN_STREAMBEGIN
)
319 while (token_type(token
) != TOKEN_STREAMEND
) {
320 if (token_type(token
) != TOKEN_IDENT
)
322 func
= show_ident(token
->ident
);
325 if (token_type(token
) != TOKEN_NUMBER
)
327 size
= atoi(token
->number
);
330 if (token_type(token
) != TOKEN_NUMBER
)
332 buf
= atoi(token
->number
);
334 limiter
= malloc(sizeof(*limiter
));
335 limiter
->limit_arg
= size
;
336 limiter
->buf_arg
= buf
;
338 add_function_hook(func
, &match_limited
, limiter
);
345 void check_overflow(int id
)
348 register_funcs_from_file();
349 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
350 add_hook(&array_check
, OP_HOOK
);
351 add_hook(&match_condition
, CONDITION_HOOK
);
352 add_function_hook("strcpy", &match_strcpy
, NULL
);
353 add_function_hook("snprintf", &match_snprintf
, NULL
);
354 add_function_hook("sprintf", &match_sprintf
, NULL
);
355 add_function_hook("memcmp", &match_limited
, &b0_l2
);
356 add_function_hook("memcmp", &match_limited
, &b1_l2
);
357 add_modification_hook(my_used_id
, &delete);
358 if (option_project
== PROJ_KERNEL
) {
359 add_function_hook("copy_to_user", &match_limited
, &b0_l2
);
360 add_function_hook("copy_to_user", &match_limited
, &b1_l2
);
361 add_function_hook("_copy_to_user", &match_limited
, &b0_l2
);
362 add_function_hook("_copy_to_user", &match_limited
, &b1_l2
);
363 add_function_hook("__copy_to_user", &match_limited
, &b0_l2
);
364 add_function_hook("__copy_to_user", &match_limited
, &b1_l2
);
365 add_function_hook("copy_from_user", &match_limited
, &b0_l2
);
366 add_function_hook("copy_from_user", &match_limited
, &b1_l2
);
367 add_function_hook("_copy_from_user", &match_limited
, &b0_l2
);
368 add_function_hook("_copy_from_user", &match_limited
, &b1_l2
);
369 add_function_hook("__copy_from_user", &match_limited
, &b0_l2
);
370 add_function_hook("__copy_from_user", &match_limited
, &b1_l2
);