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
);
37 static const char * kstr_funcs
[] = {
38 "kstrtoull", "kstrtoll", "kstrtoul", "kstrtol", "kstrtouint",
39 "kstrtoint", "kstrtou64", "kstrtos64", "kstrtou32", "kstrtos32",
40 "kstrtou16", "kstrtos16", "kstrtou8", "kstrtos8", "kstrtoull_from_user"
41 "kstrtoll_from_user", "kstrtoul_from_user", "kstrtol_from_user",
42 "kstrtouint_from_user", "kstrtoint_from_user", "kstrtou16_from_user",
43 "kstrtos16_from_user", "kstrtou8_from_user", "kstrtos8_from_user",
44 "kstrtou64_from_user", "kstrtos64_from_user", "kstrtou32_from_user",
45 "kstrtos32_from_user",
53 int is_user_macro(struct expression
*expr
)
56 struct range_list
*rl
;
58 macro
= get_macro_name(expr
->pos
);
62 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
))
64 if (strcmp(macro
, "ntohl") == 0)
66 if (strcmp(macro
, "ntohs") == 0)
71 static int has_user_data_state(struct expression
*expr
)
78 expr
= strip_expr(expr
);
79 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
80 expr
= strip_expr(expr
->unop
);
82 name
= expr_to_str_sym(expr
, &sym
);
87 stree
= __get_cur_stree();
88 FOR_EACH_MY_SM(my_id
, stree
, sm
) {
91 } END_FOR_EACH_SM(sm
);
95 static int passes_user_data(struct expression
*expr
)
97 struct expression
*arg
;
99 FOR_EACH_PTR(expr
->args
, arg
) {
100 if (is_user_data(arg
))
102 if (has_user_data_state(arg
))
104 } END_FOR_EACH_PTR(arg
);
109 static struct expression
*db_expr
;
110 static int db_user_data
;
111 static int db_user_data_callback(void *unused
, int argc
, char **argv
, char **azColName
)
113 if (atoi(argv
[0]) == PASSED_DATA
&& !passes_user_data(db_expr
))
119 static int is_user_fn_db(struct expression
*expr
)
122 static char sql_filter
[1024];
124 if (is_fake_call(expr
))
126 if (expr
->fn
->type
!= EXPR_SYMBOL
)
128 sym
= expr
->fn
->symbol
;
132 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
133 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
134 get_filename(), sym
->ident
->name
);
136 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
142 run_sql(db_user_data_callback
, NULL
,
143 "select value from return_states where type=%d and parameter = -1 and key = '$' and %s",
144 USER_DATA
, sql_filter
);
148 static int is_user_function(struct expression
*expr
)
150 if (expr
->type
!= EXPR_CALL
)
152 return is_user_fn_db(expr
);
155 static int is_skb_data(struct expression
*expr
)
162 name
= expr_to_var_sym(expr
, &sym
);
166 sym
= get_base_type(sym
);
167 if (!sym
|| sym
->type
!= SYM_PTR
)
169 sym
= get_base_type(sym
);
170 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
172 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
178 if (strcmp(name
+ len
- 6, "->data") == 0)
186 static int in_container_of_macro(struct expression
*expr
)
190 macro
= get_macro_name(expr
->pos
);
194 if (strcmp(macro
, "container_of") == 0)
199 static int is_user_data_state(struct expression
*expr
)
201 struct stree
*stree
= NULL
;
202 struct sm_state
*tmp
;
207 tmp
= get_sm_state_expr(my_id
, expr
);
209 if (slist_has_state(tmp
->possible
, &user_data_set
))
211 if (slist_has_state(tmp
->possible
, &user_data_passed
))
216 name
= expr_to_str_sym(expr
, &sym
);
220 stree
= __get_cur_stree();
221 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
224 if (!strncmp(tmp
->name
, name
, strlen(tmp
->name
))) {
225 if (slist_has_state(tmp
->possible
, &user_data_set
))
227 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
231 } END_FOR_EACH_SM(tmp
);
238 int is_user_data(struct expression
*expr
)
247 if (in_container_of_macro(expr
))
250 user_data
= is_user_macro(expr
);
253 user_data
= is_user_function(expr
);
256 user_data
= is_skb_data(expr
);
260 expr
= strip_expr(expr
); /* this has to come after is_user_macro() */
262 if (expr
->type
== EXPR_BINOP
) {
263 user_data
= is_user_data(expr
->left
);
268 user_data
= is_user_data(expr
->right
);
273 if (expr
->type
== EXPR_PREOP
&& (expr
->op
== '&' || expr
->op
== '*'))
274 expr
= strip_expr(expr
->unop
);
276 return is_user_data_state(expr
);
279 int implied_user_data(struct expression
*expr
, struct range_list
**rl
)
281 if (!is_user_data(expr
))
283 get_absolute_rl(expr
, rl
);
287 int is_capped_user_data(struct expression
*expr
)
291 sm
= get_sm_state_expr(my_id
, expr
);
294 if (slist_has_state(sm
->possible
, &capped
))
299 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
301 set_state(my_id
, "this_function", NULL
, &called
);
304 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
308 /* sanity check. this should always be true. */
309 if (strncmp(key
, "$", 1) != 0)
311 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
312 set_state(my_id
, fullname
, sym
, &user_data_passed
);
315 static void match_syscall_definition(struct symbol
*sym
)
322 macro
= get_macro_name(sym
->pos
);
324 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
325 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
328 name
= get_function();
329 if (!option_no_db
&& get_state(my_id
, "this_function", NULL
) != &called
) {
330 if (name
&& strncmp(name
, "sys_", 4) == 0)
334 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
340 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
341 set_state(my_id
, arg
->ident
->name
, arg
, &user_data_set
);
342 } END_FOR_EACH_PTR(arg
);
345 static void match_condition(struct expression
*expr
)
350 case SPECIAL_UNSIGNED_LT
:
351 case SPECIAL_UNSIGNED_LTE
:
352 if (is_user_data(expr
->left
))
353 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
354 if (is_user_data(expr
->right
))
355 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
359 case SPECIAL_UNSIGNED_GT
:
360 case SPECIAL_UNSIGNED_GTE
:
361 if (is_user_data(expr
->right
))
362 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
363 if (is_user_data(expr
->left
))
364 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
367 if (is_user_data(expr
->left
))
368 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
369 if (is_user_data(expr
->right
))
370 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
372 case SPECIAL_NOTEQUAL
:
373 if (is_user_data(expr
->left
))
374 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
375 if (is_user_data(expr
->right
))
376 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
383 static int handle_get_user(struct expression
*expr
)
388 name
= get_macro_name(expr
->pos
);
389 if (!name
|| strcmp(name
, "get_user") != 0)
392 name
= expr_to_var(expr
->right
);
393 if (!name
|| strcmp(name
, "__val_gu") != 0)
395 set_state_expr(my_id
, expr
->left
, &user_data_set
);
402 static void match_assign(struct expression
*expr
)
406 if (handle_get_user(expr
))
409 user_data
= is_user_data(expr
->right
);
410 if (user_data
== PASSED_DATA
)
411 set_state_expr(my_id
, expr
->left
, &user_data_passed
);
412 else if (user_data
== SET_DATA
)
413 set_state_expr(my_id
, expr
->left
, &user_data_set
);
414 else if (get_state_expr(my_id
, expr
->left
))
415 set_state_expr(my_id
, expr
->left
, &capped
);
418 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
421 struct expression
*member
;
424 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
425 expr
= strip_expr(expr
->unop
);
429 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
432 member
= member_expression(expr
, op
, tmp
->ident
);
433 set_state_expr(my_id
, member
, &user_data_set
);
434 } END_FOR_EACH_PTR(tmp
);
437 static void tag_base_type(struct expression
*expr
)
439 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
440 expr
= strip_expr(expr
->unop
);
442 expr
= deref_expression(expr
);
443 set_state_expr(my_id
, expr
, &user_data_set
);
446 static void tag_as_user_data(struct expression
*expr
)
450 expr
= strip_expr(expr
);
452 type
= get_type(expr
);
453 if (!type
|| type
->type
!= SYM_PTR
)
455 type
= get_real_base_type(type
);
458 if (type
== &void_ctype
) {
459 set_state_expr(my_id
, deref_expression(expr
), &user_data_set
);
462 if (type
->type
== SYM_BASETYPE
)
464 if (type
->type
== SYM_STRUCT
) {
465 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
466 expr
= deref_expression(expr
);
467 tag_struct_members(type
, expr
);
471 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
473 int param
= PTR_INT(_param
);
474 struct expression
*dest
;
476 dest
= get_argument_from_call_expr(expr
->args
, param
);
477 dest
= strip_expr(dest
);
480 tag_as_user_data(dest
);
483 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
485 set_state_expr(my_id
, expr
->left
, &user_data_set
);
488 static void match_caller_info(struct expression
*expr
)
490 struct expression
*tmp
;
494 FOR_EACH_PTR(expr
->args
, tmp
) {
495 if (is_user_data(tmp
))
496 sql_insert_caller_info(expr
, USER_DATA
, i
, "$", "");
498 } END_FOR_EACH_PTR(tmp
);
501 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
503 if (sm
->state
== &capped
)
505 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, "");
508 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
510 if (state
== &capped
)
512 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
, -1, printed_name
, "");
515 static void print_returned_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
518 struct sm_state
*tmp
;
521 const char *passed_or_new
;
523 user_data
= is_user_data(expr
);
524 if (user_data
== PASSED_DATA
) {
525 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
528 if (user_data
== SET_DATA
) {
529 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
533 stree
= __get_cur_stree();
535 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
536 const char *param_name
;
538 param
= get_param_num_from_sym(tmp
->sym
);
542 if (is_capped_var_sym(tmp
->name
, tmp
->sym
))
544 /* ignore states that were already USER_DATA to begin with */
545 if (get_state_stree(get_start_states(), my_id
, tmp
->name
, tmp
->sym
))
548 param_name
= get_param_name(tmp
);
549 if (!param_name
|| strcmp(param_name
, "$") == 0)
552 if (slist_has_state(tmp
->possible
, &user_data_set
))
554 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
559 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
560 param
, param_name
, passed_or_new
);
561 } END_FOR_EACH_SM(tmp
);
564 static void db_return_states_userdata(struct expression
*expr
, int param
, char *key
, char *value
)
569 if (expr
->type
== EXPR_ASSIGNMENT
&& param
== -1 && strcmp(key
, "*$") == 0) {
570 tag_as_user_data(expr
->left
);
574 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
578 set_state(my_id
, name
, sym
, &user_data_set
);
583 void check_user_data(int id
)
587 if (option_project
!= PROJ_KERNEL
)
590 select_caller_info_hook(set_called
, INTERNAL
);
591 select_caller_info_hook(set_param_user_data
, USER_DATA
);
592 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
593 add_hook(&match_condition
, CONDITION_HOOK
);
594 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
595 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
596 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
597 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
598 add_function_assign_hook("memdup_user", &match_user_assign_function
, NULL
);
599 add_function_assign_hook("kmap_atomic", &match_user_assign_function
, NULL
);
600 for (i
= 0; i
< ARRAY_SIZE(kstr_funcs
); i
++)
601 add_function_hook(kstr_funcs
[i
], &match_user_copy
, INT_PTR(2));
603 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
604 add_member_info_callback(my_id
, struct_member_callback
);
605 add_returned_member_callback(my_id
, returned_member_callback
);
606 add_split_return_callback(print_returned_user_data
);
607 select_return_states_hook(USER_DATA
, &db_return_states_userdata
);