2 * Copyright (C) 2011 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * There are a couple checks that try to see if a variable
20 * comes from the user. It would be better to unify them
21 * into one place. Also it we should follow the data down
22 * the call paths. Hence this file.
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
34 STATE(user_data_passed
);
42 int is_user_macro(struct expression
*expr
)
45 struct range_list
*rl
;
47 macro
= get_macro_name(expr
->pos
);
51 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
))
53 if (strcmp(macro
, "ntohl") == 0)
55 if (strcmp(macro
, "ntohs") == 0)
60 static int has_user_data_state(struct expression
*expr
)
67 expr
= strip_expr(expr
);
68 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
69 expr
= strip_expr(expr
->unop
);
71 name
= expr_to_str_sym(expr
, &sym
);
76 stree
= __get_cur_stree();
77 FOR_EACH_MY_SM(my_id
, stree
, sm
) {
80 } END_FOR_EACH_SM(sm
);
84 static int passes_user_data(struct expression
*expr
)
86 struct expression
*arg
;
88 FOR_EACH_PTR(expr
->args
, arg
) {
89 if (is_user_data(arg
))
91 if (has_user_data_state(arg
))
93 } END_FOR_EACH_PTR(arg
);
98 static struct expression
*db_expr
;
99 static int db_user_data
;
100 static int db_user_data_callback(void *unused
, int argc
, char **argv
, char **azColName
)
102 if (atoi(argv
[0]) == PASSED_DATA
&& !passes_user_data(db_expr
))
108 static int is_user_fn_db(struct expression
*expr
)
111 static char sql_filter
[1024];
113 if (is_fake_call(expr
))
115 if (expr
->fn
->type
!= EXPR_SYMBOL
)
117 sym
= expr
->fn
->symbol
;
121 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
122 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
123 get_filename(), sym
->ident
->name
);
125 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
131 run_sql(db_user_data_callback
, NULL
,
132 "select value from return_states where type=%d and parameter = -1 and key = '$' and %s",
133 USER_DATA
, sql_filter
);
137 static int is_user_function(struct expression
*expr
)
139 if (expr
->type
!= EXPR_CALL
)
141 return is_user_fn_db(expr
);
144 static int is_skb_data(struct expression
*expr
)
151 name
= expr_to_var_sym(expr
, &sym
);
155 sym
= get_base_type(sym
);
156 if (!sym
|| sym
->type
!= SYM_PTR
)
158 sym
= get_base_type(sym
);
159 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
161 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
167 if (strcmp(name
+ len
- 6, "->data") == 0)
175 static int in_container_of_macro(struct expression
*expr
)
179 macro
= get_macro_name(expr
->pos
);
183 if (strcmp(macro
, "container_of") == 0)
188 static int is_user_data_state(struct expression
*expr
)
190 struct stree
*stree
= NULL
;
191 struct sm_state
*tmp
;
196 tmp
= get_sm_state_expr(my_id
, expr
);
198 if (slist_has_state(tmp
->possible
, &user_data_set
))
200 if (slist_has_state(tmp
->possible
, &user_data_passed
))
205 name
= expr_to_str_sym(expr
, &sym
);
209 stree
= __get_cur_stree();
210 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
213 if (!strncmp(tmp
->name
, name
, strlen(tmp
->name
))) {
214 if (slist_has_state(tmp
->possible
, &user_data_set
))
216 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
220 } END_FOR_EACH_SM(tmp
);
227 int is_user_data(struct expression
*expr
)
236 if (in_container_of_macro(expr
))
239 user_data
= is_user_macro(expr
);
242 user_data
= is_user_function(expr
);
245 user_data
= is_skb_data(expr
);
249 expr
= strip_expr(expr
); /* this has to come after is_user_macro() */
251 if (expr
->type
== EXPR_BINOP
) {
252 user_data
= is_user_data(expr
->left
);
257 user_data
= is_user_data(expr
->right
);
262 if (expr
->type
== EXPR_PREOP
&& (expr
->op
== '&' || expr
->op
== '*'))
263 expr
= strip_expr(expr
->unop
);
265 return is_user_data_state(expr
);
268 int implied_user_data(struct expression
*expr
, struct range_list
**rl
)
270 if (!is_user_data(expr
))
272 get_absolute_rl(expr
, rl
);
276 int is_capped_user_data(struct expression
*expr
)
280 sm
= get_sm_state_expr(my_id
, expr
);
283 if (slist_has_state(sm
->possible
, &capped
))
288 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
290 set_state(my_id
, "this_function", NULL
, &called
);
293 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
297 /* sanity check. this should always be true. */
298 if (strncmp(key
, "$", 1) != 0)
300 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
301 set_state(my_id
, fullname
, sym
, &user_data_passed
);
304 static void match_syscall_definition(struct symbol
*sym
)
311 macro
= get_macro_name(sym
->pos
);
313 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
314 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
317 name
= get_function();
318 if (!option_no_db
&& get_state(my_id
, "this_function", NULL
) != &called
) {
319 if (name
&& strncmp(name
, "sys_", 4) == 0)
323 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
329 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
330 set_state(my_id
, arg
->ident
->name
, arg
, &user_data_set
);
331 } END_FOR_EACH_PTR(arg
);
334 static void match_condition(struct expression
*expr
)
339 case SPECIAL_UNSIGNED_LT
:
340 case SPECIAL_UNSIGNED_LTE
:
341 if (is_user_data(expr
->left
))
342 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
343 if (is_user_data(expr
->right
))
344 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
348 case SPECIAL_UNSIGNED_GT
:
349 case SPECIAL_UNSIGNED_GTE
:
350 if (is_user_data(expr
->right
))
351 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
352 if (is_user_data(expr
->left
))
353 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
356 if (is_user_data(expr
->left
))
357 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
358 if (is_user_data(expr
->right
))
359 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
361 case SPECIAL_NOTEQUAL
:
362 if (is_user_data(expr
->left
))
363 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
364 if (is_user_data(expr
->right
))
365 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
372 static int handle_get_user(struct expression
*expr
)
377 name
= get_macro_name(expr
->pos
);
378 if (!name
|| strcmp(name
, "get_user") != 0)
381 name
= expr_to_var(expr
->right
);
382 if (!name
|| strcmp(name
, "__val_gu") != 0)
384 set_state_expr(my_id
, expr
->left
, &user_data_set
);
391 static void match_assign(struct expression
*expr
)
395 if (handle_get_user(expr
))
398 user_data
= is_user_data(expr
->right
);
399 if (user_data
== PASSED_DATA
)
400 set_state_expr(my_id
, expr
->left
, &user_data_passed
);
401 else if (user_data
== SET_DATA
)
402 set_state_expr(my_id
, expr
->left
, &user_data_set
);
403 else if (get_state_expr(my_id
, expr
->left
))
404 set_state_expr(my_id
, expr
->left
, &capped
);
407 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
410 struct expression
*member
;
413 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
414 expr
= strip_expr(expr
->unop
);
418 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
421 member
= member_expression(expr
, op
, tmp
->ident
);
422 set_state_expr(my_id
, member
, &user_data_set
);
423 } END_FOR_EACH_PTR(tmp
);
426 static void tag_base_type(struct expression
*expr
)
428 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
429 expr
= strip_expr(expr
->unop
);
431 expr
= deref_expression(expr
);
432 set_state_expr(my_id
, expr
, &user_data_set
);
435 static void tag_as_user_data(struct expression
*expr
)
439 expr
= strip_expr(expr
);
441 type
= get_type(expr
);
442 if (!type
|| type
->type
!= SYM_PTR
)
444 type
= get_real_base_type(type
);
447 if (type
== &void_ctype
) {
448 set_state_expr(my_id
, deref_expression(expr
), &user_data_set
);
451 if (type
->type
== SYM_BASETYPE
)
453 if (type
->type
== SYM_STRUCT
) {
454 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
455 expr
= deref_expression(expr
);
456 tag_struct_members(type
, expr
);
460 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
462 int param
= PTR_INT(_param
);
463 struct expression
*dest
;
465 dest
= get_argument_from_call_expr(expr
->args
, param
);
466 dest
= strip_expr(dest
);
469 tag_as_user_data(dest
);
472 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
474 set_state_expr(my_id
, expr
->left
, &user_data_set
);
477 static void match_caller_info(struct expression
*expr
)
479 struct expression
*tmp
;
483 FOR_EACH_PTR(expr
->args
, tmp
) {
484 if (is_user_data(tmp
))
485 sql_insert_caller_info(expr
, USER_DATA
, i
, "$", "");
487 } END_FOR_EACH_PTR(tmp
);
490 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
492 if (sm
->state
== &capped
)
494 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, "");
497 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
499 if (state
== &capped
)
501 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
, -1, printed_name
, "");
504 static void print_returned_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
507 struct sm_state
*tmp
;
510 const char *passed_or_new
;
512 user_data
= is_user_data(expr
);
513 if (user_data
== PASSED_DATA
) {
514 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
517 if (user_data
== SET_DATA
) {
518 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
522 stree
= __get_cur_stree();
524 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
525 const char *param_name
;
527 param
= get_param_num_from_sym(tmp
->sym
);
531 if (is_capped_var_sym(tmp
->name
, tmp
->sym
))
533 /* ignore states that were already USER_DATA to begin with */
534 if (get_state_stree(get_start_states(), my_id
, tmp
->name
, tmp
->sym
))
537 param_name
= get_param_name(tmp
);
538 if (!param_name
|| strcmp(param_name
, "$") == 0)
541 if (slist_has_state(tmp
->possible
, &user_data_set
))
543 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
548 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
549 param
, param_name
, passed_or_new
);
550 } END_FOR_EACH_SM(tmp
);
553 static void db_return_states_userdata(struct expression
*expr
, int param
, char *key
, char *value
)
558 if (expr
->type
== EXPR_ASSIGNMENT
&& param
== -1 && strcmp(key
, "*$") == 0) {
559 tag_as_user_data(expr
->left
);
563 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
567 set_state(my_id
, name
, sym
, &user_data_set
);
572 void check_user_data(int id
)
574 if (option_project
!= PROJ_KERNEL
)
577 select_caller_info_hook(set_called
, INTERNAL
);
578 select_caller_info_hook(set_param_user_data
, USER_DATA
);
579 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
580 add_hook(&match_condition
, CONDITION_HOOK
);
581 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
582 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
583 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
584 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
585 add_function_assign_hook("memdup_user", &match_user_assign_function
, NULL
);
586 add_function_assign_hook("kmap_atomic", &match_user_assign_function
, NULL
);
587 add_function_hook("_kstrtoull", &match_user_copy
, INT_PTR(2));
589 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
590 add_member_info_callback(my_id
, struct_member_callback
);
591 add_returned_member_callback(my_id
, returned_member_callback
);
592 add_split_return_callback(print_returned_user_data
);
593 select_return_states_hook(USER_DATA
, &db_return_states_userdata
);