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"
30 static int my_call_id
;
33 static bool func_gets_user_data
;
35 static const char * kstr_funcs
[] = {
36 "kstrtoull", "kstrtoll", "kstrtoul", "kstrtol", "kstrtouint",
37 "kstrtoint", "kstrtou64", "kstrtos64", "kstrtou32", "kstrtos32",
38 "kstrtou16", "kstrtos16", "kstrtou8", "kstrtos8", "kstrtoull_from_user"
39 "kstrtoll_from_user", "kstrtoul_from_user", "kstrtol_from_user",
40 "kstrtouint_from_user", "kstrtoint_from_user", "kstrtou16_from_user",
41 "kstrtos16_from_user", "kstrtou8_from_user", "kstrtos8_from_user",
42 "kstrtou64_from_user", "kstrtos64_from_user", "kstrtou32_from_user",
43 "kstrtos32_from_user",
46 static void set_points_to_user_data(struct expression
*expr
);
48 static struct stree
*start_states
;
49 static struct stree_stack
*saved_stack
;
50 static void save_start_states(struct statement
*stmt
)
52 start_states
= clone_stree(__get_cur_stree());
55 static void free_start_states(void)
57 free_stree(&start_states
);
60 static void match_save_states(struct expression
*expr
)
62 push_stree(&saved_stack
, start_states
);
66 static void match_restore_states(struct expression
*expr
)
68 free_stree(&start_states
);
69 start_states
= pop_stree(&saved_stack
);
72 static struct smatch_state
*empty_state(struct sm_state
*sm
)
74 return alloc_estate_empty();
77 static void pre_merge_hook(struct sm_state
*sm
)
79 struct smatch_state
*user
;
80 struct smatch_state
*extra
;
81 struct range_list
*rl
;
83 extra
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
84 if (!extra
|| !estate_rl(extra
))
86 user
= get_state(my_id
, sm
->name
, sm
->sym
);
87 if (!user
|| !estate_rl(user
))
89 rl
= rl_intersection(estate_rl(user
), estate_rl(extra
));
90 set_state(my_id
, sm
->name
, sm
->sym
, alloc_estate_rl(clone_rl(rl
)));
93 static void tag_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
95 struct expression
*edge_member
;
96 struct symbol
*base
= get_real_base_type(member
);
100 expr
= member_expression(expr
, '.', member
->ident
);
102 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
105 type
= get_real_base_type(tmp
);
109 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
110 tag_inner_struct_members(expr
, tmp
);
117 edge_member
= member_expression(expr
, '.', tmp
->ident
);
118 set_state_expr(my_id
, edge_member
, alloc_estate_whole(type
));
119 } END_FOR_EACH_PTR(tmp
);
122 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
125 struct expression
*member
;
128 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
129 expr
= strip_expr(expr
->unop
);
133 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
134 type
= get_real_base_type(tmp
);
138 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
139 tag_inner_struct_members(expr
, tmp
);
146 member
= member_expression(expr
, op
, tmp
->ident
);
147 set_state_expr(my_id
, member
, alloc_estate_whole(get_type(member
)));
149 if (type
->type
== SYM_ARRAY
)
150 set_points_to_user_data(member
);
151 } END_FOR_EACH_PTR(tmp
);
154 static void tag_base_type(struct expression
*expr
)
156 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
157 expr
= strip_expr(expr
->unop
);
159 expr
= deref_expression(expr
);
160 set_state_expr(my_id
, expr
, alloc_estate_whole(get_type(expr
)));
163 static void tag_as_user_data(struct expression
*expr
)
167 expr
= strip_expr(expr
);
169 type
= get_type(expr
);
170 if (!type
|| type
->type
!= SYM_PTR
)
172 type
= get_real_base_type(type
);
175 if (type
== &void_ctype
) {
176 set_state_expr(my_id
, deref_expression(expr
), alloc_estate_whole(&ulong_ctype
));
179 if (type
->type
== SYM_BASETYPE
)
181 if (type
->type
== SYM_STRUCT
) {
182 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
183 expr
= deref_expression(expr
);
185 set_state_expr(my_id
, deref_expression(expr
), alloc_estate_whole(&ulong_ctype
));
186 tag_struct_members(type
, expr
);
190 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
192 int param
= PTR_INT(_param
);
193 struct expression
*dest
;
195 func_gets_user_data
= true;
197 dest
= get_argument_from_call_expr(expr
->args
, param
);
198 dest
= strip_expr(dest
);
201 tag_as_user_data(dest
);
204 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
206 struct expression
*arg
;
209 func_gets_user_data
= true;
212 FOR_EACH_PTR(expr
->args
, arg
) {
216 tag_as_user_data(arg
);
217 } END_FOR_EACH_PTR(arg
);
220 static int points_to_user_data(struct expression
*expr
)
222 struct smatch_state
*state
;
228 expr
= strip_expr(expr
);
230 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+') {
231 if (points_to_user_data(expr
->left
))
233 if (points_to_user_data(expr
->right
))
238 name
= expr_to_var_sym(expr
, &sym
);
241 snprintf(buf
, sizeof(buf
), "*%s", name
);
242 state
= get_state(my_id
, buf
, sym
);
243 if (state
&& estate_rl(state
))
250 static void set_points_to_user_data(struct expression
*expr
)
256 name
= expr_to_var_sym(expr
, &sym
);
259 snprintf(buf
, sizeof(buf
), "*%s", name
);
260 set_state(my_id
, buf
, sym
, alloc_estate_whole(&llong_ctype
));
265 static int is_skb_data(struct expression
*expr
)
269 expr
= strip_expr(expr
);
270 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
273 expr
= strip_expr(expr
->unop
);
274 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
275 expr
= strip_expr(expr
->left
);
277 if (expr
->type
!= EXPR_DEREF
)
282 if (strcmp(expr
->member
->name
, "data") != 0)
285 sym
= expr_to_sym(expr
->deref
);
288 sym
= get_real_base_type(sym
);
289 if (!sym
|| sym
->type
!= SYM_PTR
)
291 sym
= get_real_base_type(sym
);
292 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
294 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
300 static int comes_from_skb_data(struct expression
*expr
)
302 expr
= strip_expr(expr
);
306 switch (expr
->type
) {
308 if (comes_from_skb_data(expr
->left
))
310 if (comes_from_skb_data(expr
->right
))
314 if (is_skb_data(expr
))
316 return comes_from_skb_data(expr
->deref
);
323 static int handle_struct_assignment(struct expression
*expr
)
325 struct expression
*right
;
326 struct symbol
*left_type
, *right_type
;
328 left_type
= get_type(expr
->left
);
329 if (!left_type
|| left_type
->type
!= SYM_PTR
)
331 left_type
= get_real_base_type(left_type
);
332 if (!left_type
|| left_type
->type
!= SYM_STRUCT
)
336 * Ignore struct to struct assignments because for those we look at the
337 * individual members.
339 right
= strip_expr(expr
->right
);
340 right_type
= get_type(right
);
341 if (!right_type
|| right_type
->type
!= SYM_PTR
)
344 /* If we are assigning struct members then normally that is handled
345 * by fake assignments, however if we cast one struct to a different
346 * of struct then we handle that here.
348 right_type
= get_real_base_type(right_type
);
349 if (right_type
== left_type
)
352 if (!points_to_user_data(right
) && !is_skb_data(right
))
355 tag_as_user_data(expr
->left
);
359 static int handle_get_user(struct expression
*expr
)
364 name
= get_macro_name(expr
->pos
);
365 if (!name
|| strcmp(name
, "get_user") != 0)
368 name
= expr_to_var(expr
->right
);
369 if (!name
|| strcmp(name
, "__val_gu") != 0)
371 set_state_expr(my_id
, expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
378 static void match_assign(struct expression
*expr
)
380 struct range_list
*rl
;
382 if (is_fake_call(expr
->right
))
384 if (handle_get_user(expr
))
386 if (points_to_user_data(expr
->right
))
387 set_points_to_user_data(expr
->left
);
388 if (handle_struct_assignment(expr
))
391 if (!get_user_rl(expr
->right
, &rl
))
392 goto clear_old_state
;
394 rl
= cast_rl(get_type(expr
->left
), rl
);
395 set_state_expr(my_id
, expr
->left
, alloc_estate_rl(rl
));
400 if (get_state_expr(my_id
, expr
->left
))
401 set_state_expr(my_id
, expr
->left
, alloc_estate_empty());
404 static void match_condition(struct expression
*expr
)
406 struct smatch_state
*left_orig
= NULL
;
407 struct smatch_state
*right_orig
= NULL
;
409 if (expr
->type
!= EXPR_COMPARE
|| expr
->op
!= SPECIAL_EQUAL
)
412 left_orig
= get_state_expr(my_id
, expr
->left
);
413 right_orig
= get_state_expr(my_id
, expr
->right
);
415 if (!left_orig
&& !right_orig
)
417 if (left_orig
&& right_orig
)
421 set_true_false_states_expr(my_id
, expr
->left
, alloc_estate_empty(), NULL
);
423 set_true_false_states_expr(my_id
, expr
->right
, alloc_estate_empty(), NULL
);
426 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
428 func_gets_user_data
= true;
430 tag_as_user_data(expr
->left
);
431 set_points_to_user_data(expr
->left
);
434 static void match_simple_strtoul(const char *fn
, struct expression
*expr
, void *unused
)
436 func_gets_user_data
= true;
438 set_state_expr(my_id
, expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
441 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
447 macro
= get_macro_name(expr
->pos
);
452 if (strcmp(macro
, "ntohl") == 0) {
453 *rl
= alloc_whole_rl(&uint_ctype
);
456 if (strcmp(macro
, "ntohs") == 0) {
457 *rl
= alloc_whole_rl(&ushort_ctype
);
464 struct range_list
*rl
;
465 struct expression
*call
;
467 static int returned_rl_callback(void *_info
, int argc
, char **argv
, char **azColName
)
469 struct db_info
*db_info
= _info
;
470 struct range_list
*rl
;
471 char *return_ranges
= argv
[0];
472 char *user_ranges
= argv
[1];
473 struct expression
*arg
;
479 call_results_to_rl(db_info
->call
, get_type(db_info
->call
), user_ranges
, &rl
);
480 if (str_to_comparison_arg(return_ranges
, db_info
->call
, &comparison
, &arg
) &&
481 comparison
== SPECIAL_EQUAL
) {
482 struct range_list
*orig_rl
;
484 if (!get_user_rl(arg
, &orig_rl
))
486 rl
= rl_intersection(rl
, orig_rl
);
490 db_info
->rl
= rl_union(db_info
->rl
, rl
);
495 static int has_user_data(struct symbol
*sym
)
497 struct sm_state
*tmp
;
499 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), tmp
) {
502 } END_FOR_EACH_SM(tmp
);
506 static int we_pass_user_data(struct expression
*call
)
508 struct expression
*arg
;
511 FOR_EACH_PTR(call
->args
, arg
) {
512 sym
= expr_to_sym(arg
);
515 if (has_user_data(sym
))
517 } END_FOR_EACH_PTR(arg
);
522 static int db_returned_user_rl(struct expression
*call
, struct range_list
**rl
)
524 struct db_info db_info
= {};
526 /* for function pointers assume everything is used */
527 if (call
->fn
->type
!= EXPR_SYMBOL
)
529 if (is_fake_call(call
))
533 run_sql(&returned_rl_callback
, &db_info
,
534 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
535 get_static_filter(call
->fn
->symbol
), USER_DATA3_SET
);
537 func_gets_user_data
= true;
542 run_sql(&returned_rl_callback
, &db_info
,
543 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
544 get_static_filter(call
->fn
->symbol
), USER_DATA3
);
546 if (!we_pass_user_data(call
))
548 func_gets_user_data
= true;
556 static int user_data_flag
;
557 static struct range_list
*var_user_rl(struct expression
*expr
)
559 struct smatch_state
*state
;
560 struct range_list
*rl
;
561 struct range_list
*absolute_rl
;
563 if (get_user_macro_rl(expr
, &rl
))
566 if (comes_from_skb_data(expr
)) {
567 rl
= alloc_whole_rl(get_type(expr
));
571 state
= get_state_expr(my_id
, expr
);
572 if (state
&& estate_rl(state
)) {
573 rl
= estate_rl(state
);
577 if (expr
->type
== EXPR_CALL
&& db_returned_user_rl(expr
, &rl
))
583 absolute_rl
= var_to_absolute_rl(expr
);
584 return clone_rl(rl_intersection(rl
, absolute_rl
));
587 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
591 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
592 if (!user_data_flag
) {
599 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
601 struct smatch_state
*state
;
603 state
= get_state(my_id
, name
, sym
);
604 if (state
&& estate_rl(state
)) {
605 *rl
= estate_rl(state
);
611 static void match_call_info(struct expression
*expr
)
613 struct range_list
*rl
;
614 struct expression
*arg
;
618 FOR_EACH_PTR(expr
->args
, arg
) {
621 if (!get_user_rl(arg
, &rl
))
624 sql_insert_caller_info(expr
, USER_DATA3
, i
, "$", show_rl(rl
));
625 } END_FOR_EACH_PTR(arg
);
628 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
630 struct smatch_state
*state
;
631 struct range_list
*rl
;
633 if (strcmp(sm
->state
->name
, "") == 0)
636 state
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
637 if (!state
|| !estate_rl(state
))
638 rl
= estate_rl(sm
->state
);
640 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
642 sql_insert_caller_info(call
, USER_DATA3
, param
, printed_name
, show_rl(rl
));
645 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
647 struct range_list
*rl
= NULL
;
648 struct smatch_state
*state
;
652 if (strcmp(key
, "*$") == 0)
653 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
654 else if (strncmp(key
, "$", 1) == 0)
655 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
659 type
= get_member_type_from_key(symbol_expression(sym
), key
);
661 /* if the caller passes a void pointer with user data */
662 if (strcmp(key
, "*$") == 0 && type
&& type
!= &void_ctype
) {
663 struct expression
*expr
= symbol_expression(sym
);
665 tag_as_user_data(expr
);
666 set_points_to_user_data(expr
);
669 str_to_rl(type
, value
, &rl
);
670 state
= alloc_estate_rl(rl
);
671 set_state(my_id
, fullname
, sym
, state
);
674 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
676 set_state(my_call_id
, "this_function", NULL
, &called
);
679 static void match_syscall_definition(struct symbol
*sym
)
686 macro
= get_macro_name(sym
->pos
);
688 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
689 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
692 name
= get_function();
693 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
694 if (name
&& strncmp(name
, "sys_", 4) == 0)
698 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
704 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
705 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
706 } END_FOR_EACH_PTR(arg
);
709 static void set_to_user_data(struct expression
*expr
, char *key
, char *value
)
714 struct range_list
*rl
= NULL
;
716 type
= get_member_type_from_key(expr
, key
);
717 name
= get_variable_from_key(expr
, key
, &sym
);
721 call_results_to_rl(expr
, type
, value
, &rl
);
723 set_state(my_id
, name
, sym
, alloc_estate_rl(rl
));
729 static void returns_param_user_data(struct expression
*expr
, int param
, char *key
, char *value
)
731 struct expression
*arg
;
732 struct expression
*call
;
735 while (call
->type
== EXPR_ASSIGNMENT
)
736 call
= strip_expr(call
->right
);
737 if (call
->type
!= EXPR_CALL
)
740 if (!we_pass_user_data(call
))
744 if (expr
->type
!= EXPR_ASSIGNMENT
)
746 set_to_user_data(expr
->left
, key
, value
);
750 arg
= get_argument_from_call_expr(call
->args
, param
);
753 set_to_user_data(arg
, key
, value
);
756 static void returns_param_user_data_set(struct expression
*expr
, int param
, char *key
, char *value
)
758 struct expression
*arg
;
760 func_gets_user_data
= true;
763 if (expr
->type
!= EXPR_ASSIGNMENT
)
765 set_to_user_data(expr
->left
, key
, value
);
769 while (expr
->type
== EXPR_ASSIGNMENT
)
770 expr
= strip_expr(expr
->right
);
771 if (expr
->type
!= EXPR_CALL
)
774 arg
= get_argument_from_call_expr(expr
->args
, param
);
777 set_to_user_data(arg
, key
, value
);
780 static int has_empty_state(struct sm_state
*sm
)
782 struct sm_state
*tmp
;
784 FOR_EACH_PTR(sm
->possible
, tmp
) {
785 if (!estate_rl(tmp
->state
))
787 } END_FOR_EACH_PTR(tmp
);
792 static void param_set_to_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
795 struct smatch_state
*start_state
;
796 struct range_list
*rl
;
798 const char *param_name
;
800 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
801 if (has_empty_state(sm
))
804 param
= get_param_num_from_sym(sm
->sym
);
806 if (expr_to_sym(expr
) == sm
->sym
)
812 /* The logic here was that if we were passed in a user data then
813 * we don't record that. It's like the difference between
814 * param_filter and param_set. When I think about it, I'm not
815 * sure it actually works. It's probably harmless because we
816 * checked earlier that we're not returning a parameter...
817 * Let's mark this as a TODO.
819 start_state
= get_state_stree(start_states
, my_id
, sm
->name
, sm
->sym
);
820 if (start_state
&& estates_equiv(sm
->state
, start_state
))
823 param_name
= get_param_name(sm
);
826 if (strcmp(param_name
, "$") == 0)
829 sql_insert_return_states(return_id
, return_ranges
,
830 func_gets_user_data
? USER_DATA3_SET
: USER_DATA3
,
831 param
, param_name
, show_rl(estate_rl(sm
->state
)));
832 } END_FOR_EACH_SM(sm
);
834 if (get_user_rl(expr
, &rl
)) {
835 sql_insert_return_states(return_id
, return_ranges
,
836 func_gets_user_data
? USER_DATA3_SET
: USER_DATA3
,
837 -1, "$", show_rl(rl
));
841 static void match_function_def(struct symbol
*sym
)
843 func_gets_user_data
= false;
846 void check_user_data2(int id
)
852 if (option_project
!= PROJ_KERNEL
)
855 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
857 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
858 add_hook(&free_start_states
, AFTER_FUNC_HOOK
);
859 add_hook(&match_save_states
, INLINE_FN_START
);
860 add_hook(&match_restore_states
, INLINE_FN_END
);
862 add_unmatched_state_hook(my_id
, &empty_state
);
863 add_pre_merge_hook(my_id
, &pre_merge_hook
);
864 add_merge_hook(my_id
, &merge_estates
);
866 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
867 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
868 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
869 for (i
= 0; i
< ARRAY_SIZE(kstr_funcs
); i
++)
870 add_function_hook(kstr_funcs
[i
], &match_user_copy
, INT_PTR(2));
872 add_function_assign_hook("simple_strtol", &match_simple_strtoul
, NULL
);
873 add_function_assign_hook("simple_strtoll", &match_simple_strtoul
, NULL
);
874 add_function_assign_hook("simple_strtoul", &match_simple_strtoul
, NULL
);
875 add_function_assign_hook("simple_strtoull", &match_simple_strtoul
, NULL
);
877 add_function_hook("sscanf", &match_sscanf
, NULL
);
879 add_function_assign_hook("memdup_user", &match_user_assign_function
, NULL
);
880 add_function_assign_hook("kmap_atomic", &match_user_assign_function
, NULL
);
881 add_function_assign_hook("skb_network_header", &match_user_assign_function
, NULL
);
883 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
885 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
886 add_hook(&match_condition
, CONDITION_HOOK
);
888 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
889 add_member_info_callback(my_id
, struct_member_callback
);
890 select_caller_info_hook(set_param_user_data
, USER_DATA3
);
891 select_return_states_hook(USER_DATA3
, &returns_param_user_data
);
892 select_return_states_hook(USER_DATA3_SET
, &returns_param_user_data_set
);
893 add_split_return_callback(¶m_set_to_user_data
);
896 void check_user_data3(int id
)
900 if (option_project
!= PROJ_KERNEL
)
902 select_caller_info_hook(set_called
, INTERNAL
);