2 * sparse/check_deference.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
14 #include "smatch_extra.h"
21 static int my_decl_id
;
22 static int my_used_id
;
24 static struct symbol
*this_func
;
26 static void match_function_def(struct symbol
*sym
)
31 static void print_args(struct expression
*expr
, int size
)
42 name
= get_variable_from_expr(expr
, &sym
);
47 FOR_EACH_PTR(this_func
->ctype
.base_type
->arguments
, arg
) {
48 arg_name
= (arg
->ident
?arg
->ident
->name
:"-");
49 if (sym
== arg
&& !strcmp(name
, arg_name
)) {
50 sm_info("param %d array index. size %d", i
, size
);
54 } END_FOR_EACH_PTR(arg
);
59 static char *alloc_num(long long num
)
61 static char buff
[256];
63 if (num
== whole_range
.min
) {
64 snprintf(buff
, 255, "min");
65 } else if (num
== whole_range
.max
) {
66 snprintf(buff
, 255, "max");
68 snprintf(buff
, 255, "(%lld)", num
);
70 snprintf(buff
, 255, "%lld", num
);
73 return alloc_sname(buff
);
76 static void delete(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
78 delete_state(my_used_id
, name
, sym
);
81 static struct smatch_state
*alloc_my_state(int val
)
83 struct smatch_state
*state
;
85 state
= malloc(sizeof(*state
));
86 state
->name
= alloc_num(val
);
87 state
->data
= malloc(sizeof(int));
88 *(int *)state
->data
= val
;
92 static void match_declaration(struct symbol
*sym
)
94 struct symbol
*base_type
;
101 name
= sym
->ident
->name
;
102 base_type
= get_base_type(sym
);
104 if (base_type
->type
== SYM_ARRAY
&& base_type
->bit_size
> 0) {
105 set_state(my_decl_id
, name
, NULL
, alloc_my_state(base_type
->bit_size
));
107 if (sym
->initializer
&&
108 sym
->initializer
->type
== EXPR_STRING
&&
109 sym
->initializer
->string
) {
110 size
= sym
->initializer
->string
->length
* 8;
111 set_state(my_decl_id
, name
, NULL
, alloc_my_state(size
));
116 static int is_last_struct_member(struct expression
*expr
)
118 struct ident
*member
;
119 struct symbol
*struct_sym
;
122 if (!expr
|| expr
->type
!= EXPR_DEREF
)
125 member
= expr
->member
;
126 struct_sym
= get_type(expr
->deref
);
129 if (struct_sym
->type
== SYM_PTR
)
130 struct_sym
= get_base_type(struct_sym
);
131 FOR_EACH_PTR_REVERSE(struct_sym
->symbol_list
, tmp
) {
132 if (tmp
->ident
== member
)
135 } END_FOR_EACH_PTR_REVERSE(tmp
);
140 static int get_array_size(struct expression
*expr
)
144 struct smatch_state
*state
;
147 tmp
= get_type(expr
);
150 if (tmp
->type
== SYM_ARRAY
) {
151 ret
= get_expression_value(tmp
->array_size
);
152 if (ret
== 1 && is_last_struct_member(expr
))
156 name
= get_variable_from_expr(expr
, NULL
);
159 state
= get_state(my_decl_id
, name
, NULL
);
160 if (!state
|| !state
->data
)
162 if (tmp
->type
== SYM_PTR
)
163 tmp
= get_base_type(tmp
);
164 if (!tmp
->ctype
.alignment
)
166 ret
= *(int *)state
->data
/ 8 / tmp
->ctype
.alignment
;
172 extern int check_assigned_expr_id
;
173 static void print_assigned_expr(struct expression
*expr
)
176 struct state_list
*slist
;
177 struct sm_state
*tmp
;
180 name
= get_variable_from_expr(expr
, NULL
);
181 slist
= get_possible_states_expr(check_assigned_expr_id
, expr
);
182 FOR_EACH_PTR(slist
, tmp
) {
183 if (tmp
->state
== &undefined
|| tmp
->state
== &merged
)
185 smatch_msg("debug: unknown initializer %s = %s", name
, show_state(tmp
->state
));
186 } END_FOR_EACH_PTR(tmp
);
191 static void array_check(struct expression
*expr
)
193 struct expression
*dest
;
195 struct expression
*offset
;
199 expr
= strip_expr(expr
);
203 dest
= get_array_name(expr
);
204 array_size
= get_array_size(dest
);
206 name
= get_variable_from_expr(dest
, NULL
);
209 // smatch_msg("debug: array '%s' unknown size", name);
210 print_assigned_expr(dest
);
214 offset
= get_array_offset(expr
);
215 if (!get_fuzzy_max(offset
, &max
)) {
216 if (getting_address())
218 name
= get_variable_from_expr(offset
, NULL
);
221 // smatch_msg("debug: offset '%s' unknown", name);
222 set_state_expr(my_used_id
, offset
, alloc_state_num(array_size
));
223 add_modification_hook(name
, &delete, NULL
);
224 print_args(offset
, array_size
);
226 } else if (array_size
<= max
) {
227 name
= get_variable_from_expr_complex(dest
, NULL
);
229 blast. smatch can't figure out glibc's strcmp __strcmp_cg()
230 so it prints an error every time you compare to a string
231 literal array with 4 or less chars. */
232 if (name
&& strcmp(name
, "__s1") && strcmp(name
, "__s2"))
233 sm_msg("error: buffer overflow '%s' %d <= %lld", name
, array_size
, max
);
238 static void match_condition(struct expression
*expr
)
242 struct state_list
*slist
;
243 struct sm_state
*tmp
;
246 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
248 if (get_implied_value(expr
->left
, &val
))
250 else if (get_implied_value(expr
->right
, &val
))
256 slist
= get_possible_states_expr(my_used_id
, expr
->right
);
258 slist
= get_possible_states_expr(my_used_id
, expr
->left
);
261 FOR_EACH_PTR(slist
, tmp
) {
262 if (tmp
->state
== &merged
)
264 boundary
= (int)tmp
->state
->data
;
266 if (boundary
< 1 && boundary
> -1) {
269 name
= get_variable_from_expr(left
?expr
->right
:expr
->left
, NULL
);
270 sm_msg("error: testing array offset '%s' after use.", name
);
273 } END_FOR_EACH_PTR(tmp
);
277 static void match_string_assignment(struct expression
*expr
)
279 struct expression
*left
;
280 struct expression
*right
;
283 left
= strip_expr(expr
->left
);
284 right
= strip_expr(expr
->right
);
285 name
= get_variable_from_expr(left
, NULL
);
288 if (right
->type
!= EXPR_STRING
|| !right
->string
)
290 set_state(my_decl_id
, name
, NULL
,
291 alloc_my_state(right
->string
->length
* 8));
296 static void match_malloc(const char *fn
, struct expression
*expr
, void *unused
)
299 struct expression
*right
;
300 struct expression
*arg
;
303 name
= get_variable_from_expr(expr
->left
, NULL
);
307 right
= strip_expr(expr
->right
);
308 arg
= get_argument_from_call_expr(right
->args
, 0);
309 if (!get_implied_value(arg
, &bytes
))
311 set_state(my_decl_id
, name
, NULL
, alloc_my_state(bytes
* 8));
316 static void match_strcpy(const char *fn
, struct expression
*expr
,
319 struct expression
*dest
;
320 struct expression
*data
;
321 char *dest_name
= NULL
;
322 char *data_name
= NULL
;
323 struct smatch_state
*dest_state
;
324 struct smatch_state
*data_state
;
328 dest
= get_argument_from_call_expr(expr
->args
, 0);
329 dest_name
= get_variable_from_expr(dest
, NULL
);
331 data
= get_argument_from_call_expr(expr
->args
, 1);
332 data_name
= get_variable_from_expr(data
, NULL
);
334 dest_state
= get_state(my_decl_id
, dest_name
, NULL
);
335 if (!dest_state
|| !dest_state
->data
)
338 data_state
= get_state(my_decl_id
, data_name
, NULL
);
339 if (!data_state
|| !data_state
->data
)
341 dest_size
= *(int *)dest_state
->data
/ 8;
342 data_size
= *(int *)data_state
->data
/ 8;
343 if (dest_size
< data_size
)
344 sm_msg("error: %s (%d) too large for %s (%d)", data_name
,
345 data_size
, dest_name
, dest_size
);
347 free_string(dest_name
);
348 free_string(data_name
);
351 static void match_limitted(const char *fn
, struct expression
*expr
,
354 struct expression
*dest
;
355 struct expression
*data
;
356 char *dest_name
= NULL
;
357 struct smatch_state
*state
;
361 dest
= get_argument_from_call_expr(expr
->args
, 0);
362 dest_name
= get_variable_from_expr(dest
, NULL
);
364 data
= get_argument_from_call_expr(expr
->args
, PTR_INT(limit_arg
));
365 if (!get_value(data
, &needed
))
367 state
= get_state(my_decl_id
, dest_name
, NULL
);
368 if (!state
|| !state
->data
)
370 has
= *(int *)state
->data
/ 8;
372 sm_msg("error: %s too small for %lld bytes.", dest_name
,
375 free_string(dest_name
);
378 static void match_array_func(const char *fn
, struct expression
*expr
, void *info
)
380 struct bound
*bound_info
= (struct bound
*)info
;
381 struct expression
*arg
;
384 arg
= get_argument_from_call_expr(expr
->args
, bound_info
->param
);
385 if (!get_implied_value(arg
, &offset
))
387 if (offset
>= bound_info
->size
)
388 sm_msg("error: buffer overflow calling %s. param %d. %lld >= %d", fn
, bound_info
->param
, offset
, bound_info
->size
);
391 static void register_array_funcs(void)
395 struct bound
*bound_info
;
397 token
= get_tokens_file("kernel.array_bounds");
400 if (token_type(token
) != TOKEN_STREAMBEGIN
)
403 while (token_type(token
) != TOKEN_STREAMEND
) {
404 bound_info
= malloc(sizeof(*bound_info
));
405 if (token_type(token
) != TOKEN_IDENT
)
407 func
= show_ident(token
->ident
);
409 if (token_type(token
) != TOKEN_NUMBER
)
411 bound_info
->param
= atoi(token
->number
);
413 if (token_type(token
) != TOKEN_NUMBER
)
415 bound_info
->size
= atoi(token
->number
);
416 add_function_hook(func
, &match_array_func
, bound_info
);
422 void check_overflow(int id
)
425 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
426 add_hook(&match_declaration
, DECLARATION_HOOK
);
427 add_hook(&array_check
, OP_HOOK
);
428 add_hook(&match_string_assignment
, ASSIGNMENT_HOOK
);
429 add_hook(&match_condition
, CONDITION_HOOK
);
430 add_function_assign_hook("malloc", &match_malloc
, NULL
);
431 add_function_hook("strcpy", &match_strcpy
, NULL
);
432 add_function_hook("strncpy", &match_limitted
, (void *)2);
433 if (option_project
== PROJ_KERNEL
) {
434 add_function_assign_hook("kmalloc", &match_malloc
, NULL
);
435 add_function_assign_hook("kzalloc", &match_malloc
, NULL
);
436 add_function_assign_hook("vmalloc", &match_malloc
, NULL
);
437 add_function_hook("copy_to_user", &match_limitted
, (void *)2);
438 add_function_hook("copy_from_user", &match_limitted
, (void *)2);
440 register_array_funcs();
443 void register_check_overflow_again(int id
)