2 * smatch/check_user_data.c
4 * Copyright (C) 2011 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * There are a couple checks that try to see if a variable
12 * comes from the user. It would be better to unify them
13 * into one place. Also it we should follow the data down
14 * the call paths. Hence this file.
18 #include "smatch_slist.h"
19 #include "smatch_extra.h"
24 STATE(user_data_passed
);
32 int is_user_macro(struct expression
*expr
)
35 struct range_list
*rl
;
37 macro
= get_macro_name(expr
->pos
);
41 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
))
43 if (strcmp(macro
, "ntohl") == 0)
45 if (strcmp(macro
, "ntohs") == 0)
50 static int has_user_data_state(struct expression
*expr
, struct state_list
*my_slist
)
56 expr
= strip_expr(expr
);
57 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
58 expr
= strip_expr(expr
->unop
);
60 name
= expr_to_str_sym(expr
, &sym
);
65 FOR_EACH_PTR(my_slist
, sm
) {
68 } END_FOR_EACH_PTR(sm
);
72 static int passes_user_data(struct expression
*expr
)
74 struct state_list
*slist
;
75 struct expression
*arg
;
77 slist
= get_all_states(my_id
);
78 FOR_EACH_PTR(expr
->args
, arg
) {
79 if (is_user_data(arg
))
81 if (has_user_data_state(arg
, slist
))
83 } END_FOR_EACH_PTR(arg
);
88 static struct expression
*db_expr
;
89 static int db_user_data
;
90 static int db_user_data_callback(void *unused
, int argc
, char **argv
, char **azColName
)
92 if (atoi(argv
[0]) == PASSED_DATA
&& !passes_user_data(db_expr
))
98 static int is_user_fn_db(struct expression
*expr
)
101 static char sql_filter
[1024];
103 if (is_fake_call(expr
))
105 if (expr
->fn
->type
!= EXPR_SYMBOL
)
107 sym
= expr
->fn
->symbol
;
111 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
112 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
113 get_filename(), sym
->ident
->name
);
115 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
121 run_sql(db_user_data_callback
,
122 "select value from return_states where type=%d and parameter = -1 and key = '$$' and %s",
123 USER_DATA
, sql_filter
);
127 static int is_user_function(struct expression
*expr
)
129 if (expr
->type
!= EXPR_CALL
)
131 return is_user_fn_db(expr
);
134 static int is_skb_data(struct expression
*expr
)
141 name
= expr_to_var_sym(expr
, &sym
);
145 sym
= get_base_type(sym
);
146 if (!sym
|| sym
->type
!= SYM_PTR
)
148 sym
= get_base_type(sym
);
149 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
151 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
157 if (strcmp(name
+ len
- 6, "->data") == 0)
165 static int in_container_of_macro(struct expression
*expr
)
169 macro
= get_macro_name(expr
->pos
);
173 if (strcmp(macro
, "container_of") == 0)
178 static int is_user_data_state(struct expression
*expr
)
180 struct state_list
*slist
= NULL
;
181 struct sm_state
*tmp
;
186 tmp
= get_sm_state_expr(my_id
, expr
);
188 if (slist_has_state(tmp
->possible
, &user_data_set
))
190 if (slist_has_state(tmp
->possible
, &user_data_passed
))
195 name
= expr_to_str_sym(expr
, &sym
);
199 slist
= get_all_states(my_id
);
200 FOR_EACH_PTR(slist
, tmp
) {
203 if (!strncmp(tmp
->name
, name
, strlen(tmp
->name
))) {
204 if (slist_has_state(tmp
->possible
, &user_data_set
))
206 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
210 } END_FOR_EACH_PTR(tmp
);
218 int is_user_data(struct expression
*expr
)
227 if (in_container_of_macro(expr
))
230 user_data
= is_user_macro(expr
);
233 user_data
= is_user_function(expr
);
236 user_data
= is_skb_data(expr
);
240 expr
= strip_expr(expr
); /* this has to come after is_user_macro() */
242 if (expr
->type
== EXPR_BINOP
) {
243 user_data
= is_user_data(expr
->left
);
248 user_data
= is_user_data(expr
->right
);
253 if (expr
->type
== EXPR_PREOP
&& (expr
->op
== '&' || expr
->op
== '*'))
254 expr
= strip_expr(expr
->unop
);
256 return is_user_data_state(expr
);
259 int is_capped_user_data(struct expression
*expr
)
263 sm
= get_sm_state_expr(my_id
, expr
);
266 if (slist_has_state(sm
->possible
, &capped
))
271 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
275 /* sanity check. this should always be true. */
276 if (strncmp(key
, "$$", 2) != 0)
278 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
279 set_state(my_id
, fullname
, sym
, &user_data_passed
);
282 static void match_syscall_definition(struct symbol
*sym
)
287 macro
= get_macro_name(sym
->pos
);
290 if (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) != 0 &&
291 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) != 0)
294 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
295 set_state(my_id
, arg
->ident
->name
, arg
, &user_data_set
);
296 } END_FOR_EACH_PTR(arg
);
299 static void match_condition(struct expression
*expr
)
304 case SPECIAL_UNSIGNED_LT
:
305 case SPECIAL_UNSIGNED_LTE
:
306 if (is_user_data(expr
->left
))
307 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
308 if (is_user_data(expr
->right
))
309 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
313 case SPECIAL_UNSIGNED_GT
:
314 case SPECIAL_UNSIGNED_GTE
:
315 if (is_user_data(expr
->right
))
316 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
317 if (is_user_data(expr
->left
))
318 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
321 if (is_user_data(expr
->left
))
322 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
323 if (is_user_data(expr
->right
))
324 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
326 case SPECIAL_NOTEQUAL
:
327 if (is_user_data(expr
->left
))
328 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
329 if (is_user_data(expr
->right
))
330 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
337 static int handle_get_user(struct expression
*expr
)
342 name
= get_macro_name(expr
->pos
);
343 if (!name
|| strcmp(name
, "get_user") != 0)
346 name
= expr_to_var(expr
->right
);
347 if (!name
|| strcmp(name
, "__val_gu") != 0)
349 set_state_expr(my_id
, expr
->left
, &user_data_set
);
356 static void match_assign(struct expression
*expr
)
360 if (handle_get_user(expr
))
363 user_data
= is_user_data(expr
->right
);
364 if (user_data
== PASSED_DATA
)
365 set_state_expr(my_id
, expr
->left
, &user_data_passed
);
366 else if (user_data
== SET_DATA
)
367 set_state_expr(my_id
, expr
->left
, &user_data_set
);
368 else if (get_state_expr(my_id
, expr
->left
))
369 set_state_expr(my_id
, expr
->left
, &capped
);
372 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
375 struct expression
*member
;
378 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
379 expr
= strip_expr(expr
->unop
);
383 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
386 member
= member_expression(expr
, op
, tmp
->ident
);
387 set_state_expr(my_id
, member
, &user_data_set
);
388 } END_FOR_EACH_PTR(tmp
);
391 static void tag_base_type(struct expression
*expr
)
393 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
394 expr
= strip_expr(expr
->unop
);
396 expr
= deref_expression(expr
);
397 set_state_expr(my_id
, expr
, &user_data_set
);
400 static void tag_as_user_data(struct expression
*expr
)
404 expr
= strip_expr(expr
);
406 type
= get_type(expr
);
407 if (!type
|| type
->type
!= SYM_PTR
)
409 type
= get_real_base_type(type
);
412 if (type
== &void_ctype
) {
413 set_state_expr(my_id
, deref_expression(expr
), &user_data_set
);
416 if (type
->type
== SYM_BASETYPE
)
418 if (type
->type
== SYM_STRUCT
) {
419 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
420 expr
= deref_expression(expr
);
421 tag_struct_members(type
, expr
);
425 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
427 int param
= PTR_INT(_param
);
428 struct expression
*dest
;
430 dest
= get_argument_from_call_expr(expr
->args
, param
);
431 dest
= strip_expr(dest
);
434 tag_as_user_data(dest
);
437 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
439 set_state_expr(my_id
, expr
->left
, &user_data_set
);
442 static void match_caller_info(struct expression
*expr
)
444 struct expression
*tmp
;
448 FOR_EACH_PTR(expr
->args
, tmp
) {
449 if (is_user_data(tmp
))
450 sql_insert_caller_info(expr
, USER_DATA
, i
, "$$", "");
452 } END_FOR_EACH_PTR(tmp
);
455 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
457 if (state
== &capped
)
459 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, "");
462 static void returned_member_callback(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
)
464 if (state
== &capped
)
466 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
, -1, printed_name
, "");
469 static void print_returned_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
471 struct state_list
*my_slist
;
472 struct sm_state
*tmp
;
475 const char *passed_or_new
;
477 user_data
= is_user_data(expr
);
478 if (user_data
== PASSED_DATA
) {
479 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
482 if (user_data
== SET_DATA
) {
483 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
487 my_slist
= get_all_states(my_id
);
489 FOR_EACH_PTR(my_slist
, tmp
) {
490 const char *param_name
;
492 param
= get_param_num_from_sym(tmp
->sym
);
496 if (is_capped_var_sym(tmp
->name
, tmp
->sym
))
498 /* ignore states that were already USER_DATA to begin with */
499 if (get_state_slist(get_start_states(), my_id
, tmp
->name
, tmp
->sym
))
502 param_name
= get_param_name(tmp
);
503 if (!param_name
|| strcmp(param_name
, "$$") == 0)
506 if (slist_has_state(tmp
->possible
, &user_data_set
))
508 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
513 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
514 param
, param_name
, passed_or_new
);
515 } END_FOR_EACH_PTR(tmp
);
517 free_slist(&my_slist
);
520 static void db_return_states_userdata(struct expression
*expr
, int param
, char *key
, char *value
)
525 if (expr
->type
== EXPR_ASSIGNMENT
&& param
== -1 && strcmp(key
, "*$$") == 0) {
526 tag_as_user_data(expr
->left
);
530 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
534 set_state(my_id
, name
, sym
, &user_data_set
);
539 void check_user_data(int id
)
541 if (option_project
!= PROJ_KERNEL
)
544 select_caller_info_hook(set_param_user_data
, USER_DATA
);
545 add_hook(&match_syscall_definition
, FUNC_DEF_HOOK
);
546 add_hook(&match_condition
, CONDITION_HOOK
);
547 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
548 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
549 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
550 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
551 add_function_assign_hook("kmemdup_user", &match_user_assign_function
, NULL
);
553 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
554 add_member_info_callback(my_id
, struct_member_callback
);
555 add_returned_member_callback(my_id
, returned_member_callback
);
556 add_split_return_callback(print_returned_user_data
);
557 select_return_states_hook(USER_DATA
, &db_return_states_userdata
);