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 const char *returns_user_data
[] = {
47 "simple_strtol", "simple_strtoll", "simple_strtoul", "simple_strtoull",
51 static const char *returns_pointer_to_user_data
[] = {
52 "nlmsg_data", "nla_data", "memdup_user", "kmap_atomic", "skb_network_header",
55 static void set_points_to_user_data(struct expression
*expr
);
57 static struct stree
*start_states
;
58 static struct stree_stack
*saved_stack
;
59 static void save_start_states(struct statement
*stmt
)
61 start_states
= clone_stree(__get_cur_stree());
64 static void free_start_states(void)
66 free_stree(&start_states
);
69 static void match_save_states(struct expression
*expr
)
71 push_stree(&saved_stack
, start_states
);
75 static void match_restore_states(struct expression
*expr
)
77 free_stree(&start_states
);
78 start_states
= pop_stree(&saved_stack
);
81 static struct smatch_state
*empty_state(struct sm_state
*sm
)
83 return alloc_estate_empty();
86 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
88 struct smatch_state
*user
= cur
->state
;
89 struct smatch_state
*extra
;
90 struct smatch_state
*state
;
91 struct range_list
*rl
;
93 extra
= __get_state(SMATCH_EXTRA
, cur
->name
, cur
->sym
);
96 rl
= rl_intersection(estate_rl(user
), estate_rl(extra
));
97 state
= alloc_estate_rl(clone_rl(rl
));
98 if (estate_capped(user
) || is_capped_var_sym(cur
->name
, cur
->sym
))
99 estate_set_capped(state
);
100 if (estate_treat_untagged(user
))
101 estate_set_treat_untagged(state
);
102 set_state(my_id
, cur
->name
, cur
->sym
, state
);
105 static void extra_nomod_hook(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
107 struct smatch_state
*user
, *new;
108 struct range_list
*rl
;
110 user
= __get_state(my_id
, name
, sym
);
113 rl
= rl_intersection(estate_rl(user
), estate_rl(state
));
114 if (rl_equiv(rl
, estate_rl(user
)))
116 new = alloc_estate_rl(rl
);
117 if (estate_capped(user
))
118 estate_set_capped(new);
119 if (estate_treat_untagged(user
))
120 estate_set_treat_untagged(new);
121 set_state(my_id
, name
, sym
, new);
124 static bool binop_capped(struct expression
*expr
)
126 struct range_list
*left_rl
;
129 if (expr
->op
== '-' && get_user_rl(expr
->left
, &left_rl
)) {
130 if (user_rl_capped(expr
->left
))
132 comparison
= get_comparison(expr
->left
, expr
->right
);
133 if (comparison
&& show_special(comparison
)[0] == '>')
138 if (expr
->op
== '&' || expr
->op
== '%') {
139 if (is_capped(expr
->left
) || is_capped(expr
->right
))
141 if (user_rl_capped(expr
->left
) || user_rl_capped(expr
->right
))
146 if (user_rl_capped(expr
->left
) &&
147 user_rl_capped(expr
->right
))
152 bool user_rl_capped(struct expression
*expr
)
154 struct smatch_state
*state
;
155 struct range_list
*rl
;
158 expr
= strip_expr(expr
);
161 if (get_value(expr
, &sval
))
163 if (expr
->type
== EXPR_BINOP
)
164 return binop_capped(expr
);
165 if ((expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) &&
166 (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
))
167 return user_rl_capped(expr
->unop
);
168 state
= get_state_expr(my_id
, expr
);
170 return estate_capped(state
);
172 if (get_user_rl(expr
, &rl
))
173 return false; /* uncapped user data */
175 return true; /* not actually user data */
178 bool user_rl_treat_untagged(struct expression
*expr
)
180 struct smatch_state
*state
;
181 struct range_list
*rl
;
184 expr
= strip_expr(expr
);
187 if (get_value(expr
, &sval
))
190 state
= get_state_expr(my_id
, expr
);
192 return estate_treat_untagged(state
);
194 if (get_user_rl(expr
, &rl
))
195 return false; /* uncapped user data */
197 return true; /* not actually user data */
200 static void tag_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
202 struct expression
*edge_member
;
203 struct symbol
*base
= get_real_base_type(member
);
207 expr
= member_expression(expr
, '.', member
->ident
);
209 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
212 type
= get_real_base_type(tmp
);
216 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
217 tag_inner_struct_members(expr
, tmp
);
224 edge_member
= member_expression(expr
, '.', tmp
->ident
);
225 set_state_expr(my_id
, edge_member
, alloc_estate_whole(type
));
226 } END_FOR_EACH_PTR(tmp
);
229 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
232 struct expression
*member
;
235 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
236 expr
= strip_expr(expr
->unop
);
240 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
241 type
= get_real_base_type(tmp
);
245 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
246 tag_inner_struct_members(expr
, tmp
);
253 member
= member_expression(expr
, op
, tmp
->ident
);
254 set_state_expr(my_id
, member
, alloc_estate_whole(get_type(member
)));
256 if (type
->type
== SYM_ARRAY
)
257 set_points_to_user_data(member
);
258 } END_FOR_EACH_PTR(tmp
);
261 static void tag_base_type(struct expression
*expr
)
263 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
264 expr
= strip_expr(expr
->unop
);
266 expr
= deref_expression(expr
);
267 set_state_expr(my_id
, expr
, alloc_estate_whole(get_type(expr
)));
270 static void tag_as_user_data(struct expression
*expr
)
274 expr
= strip_expr(expr
);
276 type
= get_type(expr
);
277 if (!type
|| type
->type
!= SYM_PTR
)
279 type
= get_real_base_type(type
);
282 if (type
== &void_ctype
) {
283 set_state_expr(my_id
, deref_expression(expr
), alloc_estate_whole(&ulong_ctype
));
286 if (type
->type
== SYM_BASETYPE
)
288 if (type
->type
== SYM_STRUCT
|| type
->type
== SYM_UNION
) {
289 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
290 expr
= deref_expression(expr
);
292 set_state_expr(my_id
, deref_expression(expr
), alloc_estate_whole(&ulong_ctype
));
293 tag_struct_members(type
, expr
);
297 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
299 int param
= PTR_INT(_param
);
300 struct expression
*dest
;
302 func_gets_user_data
= true;
304 dest
= get_argument_from_call_expr(expr
->args
, param
);
305 dest
= strip_expr(dest
);
308 tag_as_user_data(dest
);
311 static int is_dev_attr_name(struct expression
*expr
)
316 name
= expr_to_str(expr
);
319 if (strstr(name
, "->attr.name"))
325 static int ends_in_n(struct expression
*expr
)
331 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
338 if (str
->data
[str
->length
- 3] == '%' &&
339 str
->data
[str
->length
- 2] == 'n')
344 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
346 struct expression
*str
, *format
, *arg
;
349 func_gets_user_data
= true;
351 str
= get_argument_from_call_expr(expr
->args
, 0);
352 if (is_dev_attr_name(str
))
355 format
= get_argument_from_call_expr(expr
->args
, 1);
356 if (is_dev_attr_name(format
))
359 last
= ptr_list_size((struct ptr_list
*)expr
->args
) - 1;
362 FOR_EACH_PTR(expr
->args
, arg
) {
366 if (i
== last
&& ends_in_n(format
))
368 tag_as_user_data(arg
);
369 } END_FOR_EACH_PTR(arg
);
372 static int is_skb_data(struct expression
*expr
)
379 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
380 return is_skb_data(expr
->left
);
382 expr
= strip_expr(expr
);
385 if (expr
->type
!= EXPR_DEREF
|| expr
->op
!= '.')
390 if (strcmp(expr
->member
->name
, "data") != 0)
393 sym
= expr_to_sym(expr
->deref
);
396 sym
= get_real_base_type(sym
);
397 if (!sym
|| sym
->type
!= SYM_PTR
)
399 sym
= get_real_base_type(sym
);
400 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
402 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
408 static bool is_points_to_user_data_fn(struct expression
*expr
)
412 expr
= strip_expr(expr
);
413 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
||
417 for (i
= 0; i
< ARRAY_SIZE(returns_pointer_to_user_data
); i
++) {
418 if (sym_name_is(returns_pointer_to_user_data
[i
], expr
))
424 static int get_rl_from_function(struct expression
*expr
, struct range_list
**rl
)
428 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
||
429 !expr
->fn
->symbol_name
|| !expr
->fn
->symbol_name
->name
)
432 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++) {
433 if (strcmp(expr
->fn
->symbol_name
->name
, returns_user_data
[i
]) == 0) {
434 *rl
= alloc_whole_rl(get_type(expr
));
441 int points_to_user_data(struct expression
*expr
)
443 struct smatch_state
*state
;
444 struct range_list
*rl
;
450 expr
= strip_expr(expr
);
453 if (is_skb_data(expr
))
455 if (is_points_to_user_data_fn(expr
))
457 if (get_rl_from_function(expr
, &rl
))
460 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+') {
461 if (points_to_user_data(expr
->left
))
463 if (points_to_user_data(expr
->right
))
468 name
= expr_to_var_sym(expr
, &sym
);
471 snprintf(buf
, sizeof(buf
), "*%s", name
);
472 state
= __get_state(my_id
, buf
, sym
);
473 if (state
&& estate_rl(state
))
480 static void set_points_to_user_data(struct expression
*expr
)
487 name
= expr_to_var_sym(expr
, &sym
);
490 snprintf(buf
, sizeof(buf
), "*%s", name
);
491 type
= get_type(expr
);
492 if (type
&& type
->type
== SYM_PTR
)
493 type
= get_real_base_type(type
);
494 if (!type
|| type
->type
!= SYM_BASETYPE
)
496 set_state(my_id
, buf
, sym
, alloc_estate_whole(type
));
501 static int comes_from_skb_data(struct expression
*expr
)
503 expr
= strip_expr(expr
);
504 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
507 expr
= strip_expr(expr
->unop
);
510 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
511 expr
= strip_expr(expr
->left
);
513 return is_skb_data(expr
);
516 static int handle_struct_assignment(struct expression
*expr
)
518 struct expression
*right
;
519 struct symbol
*left_type
, *right_type
;
521 left_type
= get_type(expr
->left
);
522 if (!left_type
|| left_type
->type
!= SYM_PTR
)
524 left_type
= get_real_base_type(left_type
);
527 if (left_type
->type
!= SYM_STRUCT
&&
528 left_type
->type
!= SYM_UNION
)
532 * Ignore struct to struct assignments because for those we look at the
533 * individual members.
535 right
= strip_expr(expr
->right
);
536 right_type
= get_type(right
);
537 if (!right_type
|| right_type
->type
!= SYM_PTR
)
540 /* If we are assigning struct members then normally that is handled
541 * by fake assignments, however if we cast one struct to a different
542 * of struct then we handle that here.
544 right_type
= get_real_base_type(right_type
);
545 if (right_type
== left_type
)
548 if (!points_to_user_data(right
))
551 tag_as_user_data(expr
->left
);
555 static int handle_get_user(struct expression
*expr
)
560 name
= get_macro_name(expr
->pos
);
561 if (!name
|| strcmp(name
, "get_user") != 0)
564 name
= expr_to_var(expr
->right
);
565 if (!name
|| (strcmp(name
, "__val_gu") != 0 && strcmp(name
, "__gu_val") != 0))
567 set_state_expr(my_id
, expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
574 static bool handle_op_assign(struct expression
*expr
)
576 struct expression
*binop_expr
;
577 struct smatch_state
*state
;
578 struct range_list
*rl
;
581 case SPECIAL_ADD_ASSIGN
:
582 case SPECIAL_SUB_ASSIGN
:
583 case SPECIAL_AND_ASSIGN
:
584 case SPECIAL_MOD_ASSIGN
:
585 case SPECIAL_SHL_ASSIGN
:
586 case SPECIAL_SHR_ASSIGN
:
587 case SPECIAL_OR_ASSIGN
:
588 case SPECIAL_XOR_ASSIGN
:
589 case SPECIAL_MUL_ASSIGN
:
590 case SPECIAL_DIV_ASSIGN
:
591 binop_expr
= binop_expression(expr
->left
,
592 op_remove_assign(expr
->op
),
594 if (!get_user_rl(binop_expr
, &rl
))
597 rl
= cast_rl(get_type(expr
->left
), rl
);
598 state
= alloc_estate_rl(rl
);
599 if (user_rl_capped(binop_expr
))
600 estate_set_capped(state
);
601 if (user_rl_treat_untagged(expr
->left
))
602 estate_set_treat_untagged(state
);
603 set_state_expr(my_id
, expr
->left
, state
);
609 static void match_assign(struct expression
*expr
)
611 struct range_list
*rl
;
612 static struct expression
*handled
;
613 struct smatch_state
*state
;
614 struct expression
*faked
;
616 faked
= get_faked_expression();
617 if (faked
&& faked
== handled
)
619 if (is_fake_call(expr
->right
))
620 goto clear_old_state
;
621 if (handle_get_user(expr
))
623 if (points_to_user_data(expr
->right
)) {
625 set_points_to_user_data(expr
->left
);
627 if (handle_struct_assignment(expr
))
630 if (handle_op_assign(expr
))
633 goto clear_old_state
;
635 /* Handled by DB code */
636 if (expr
->right
->type
== EXPR_CALL
|| __in_fake_parameter_assign
)
639 if (!get_user_rl(expr
->right
, &rl
))
640 goto clear_old_state
;
642 rl
= cast_rl(get_type(expr
->left
), rl
);
643 state
= alloc_estate_rl(rl
);
645 if (user_rl_capped(expr
->right
))
646 estate_set_capped(state
);
647 if (user_rl_treat_untagged(expr
->right
))
648 estate_set_treat_untagged(state
);
649 set_state_expr(my_id
, expr
->left
, state
);
654 if (get_state_expr(my_id
, expr
->left
))
655 set_state_expr(my_id
, expr
->left
, alloc_estate_empty());
658 static void handle_eq_noteq(struct expression
*expr
)
660 struct smatch_state
*left_orig
, *right_orig
;
662 left_orig
= get_state_expr(my_id
, expr
->left
);
663 right_orig
= get_state_expr(my_id
, expr
->right
);
665 if (!left_orig
&& !right_orig
)
667 if (left_orig
&& right_orig
)
671 set_true_false_states_expr(my_id
, expr
->left
,
672 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
673 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
675 set_true_false_states_expr(my_id
, expr
->right
,
676 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
677 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
681 static struct range_list
*strip_negatives(struct range_list
*rl
)
683 sval_t min
= rl_min(rl
);
684 sval_t minus_one
= { .type
= rl_type(rl
), .value
= -1 };
685 sval_t over
= { .type
= rl_type(rl
), .value
= INT_MAX
+ 1ULL };
686 sval_t max
= sval_type_max(rl_type(rl
));
691 if (type_unsigned(rl_type(rl
)) && type_bits(rl_type(rl
)) > 31)
692 return remove_range(rl
, over
, max
);
694 return remove_range(rl
, min
, minus_one
);
697 static void handle_compare(struct expression
*expr
)
699 struct expression
*left
, *right
;
700 struct range_list
*left_rl
= NULL
;
701 struct range_list
*right_rl
= NULL
;
702 struct range_list
*user_rl
;
703 struct smatch_state
*capped_state
;
704 struct smatch_state
*left_true
= NULL
;
705 struct smatch_state
*left_false
= NULL
;
706 struct smatch_state
*right_true
= NULL
;
707 struct smatch_state
*right_false
= NULL
;
711 left
= strip_expr(expr
->left
);
712 right
= strip_expr(expr
->right
);
714 while (left
->type
== EXPR_ASSIGNMENT
)
715 left
= strip_expr(left
->left
);
718 * Conditions are mostly handled by smatch_extra.c, but there are some
719 * times where the exact values are not known so we can't do that.
721 * Normally, we might consider using smatch_capped.c to supliment smatch
722 * extra but that doesn't work when we merge unknown uncapped kernel
723 * data with unknown capped user data. The result is uncapped user
724 * data. We need to keep it separate and say that the user data is
725 * capped. In the past, I would have marked this as just regular
726 * kernel data (not user data) but we can't do that these days because
727 * we need to track user data for Spectre.
729 * The other situation which we have to handle is when we do have an
730 * int and we compare against an unknown unsigned kernel variable. In
731 * that situation we assume that the kernel data is less than INT_MAX.
732 * Otherwise then we get all sorts of array underflow false positives.
736 /* Handled in smatch_extra.c */
737 if (get_implied_value(left
, &sval
) ||
738 get_implied_value(right
, &sval
))
741 get_user_rl(left
, &left_rl
);
742 get_user_rl(right
, &right_rl
);
745 if (!left_rl
&& !right_rl
)
747 /* if both sides are user data that's not a good limit */
748 if (left_rl
&& right_rl
)
756 type
= get_type(expr
);
757 if (type_unsigned(type
))
758 user_rl
= strip_negatives(user_rl
);
759 capped_state
= alloc_estate_rl(user_rl
);
760 estate_set_capped(capped_state
);
764 case SPECIAL_UNSIGNED_LT
:
766 case SPECIAL_UNSIGNED_LTE
:
768 left_true
= capped_state
;
770 right_false
= capped_state
;
773 case SPECIAL_UNSIGNED_GT
:
775 case SPECIAL_UNSIGNED_GTE
:
777 left_false
= capped_state
;
779 right_true
= capped_state
;
783 set_true_false_states_expr(my_id
, left
, left_true
, left_false
);
784 set_true_false_states_expr(my_id
, right
, right_true
, right_false
);
787 static void match_condition(struct expression
*expr
)
789 if (expr
->type
!= EXPR_COMPARE
)
792 if (expr
->op
== SPECIAL_EQUAL
||
793 expr
->op
== SPECIAL_NOTEQUAL
) {
794 handle_eq_noteq(expr
);
798 handle_compare(expr
);
801 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
803 tag_as_user_data(expr
->left
);
804 set_points_to_user_data(expr
->left
);
807 static void match_returns_user_rl(const char *fn
, struct expression
*expr
, void *unused
)
809 func_gets_user_data
= true;
812 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
814 struct expression
*parent
;
820 macro
= get_macro_name(expr
->pos
);
824 /* handle ntohl(foo[i]) where "i" is trusted */
825 parent
= expr_get_parent_expr(expr
);
826 while (parent
&& parent
->type
!= EXPR_BINOP
)
827 parent
= expr_get_parent_expr(parent
);
828 if (parent
&& parent
->type
== EXPR_BINOP
) {
829 char *parent_macro
= get_macro_name(parent
->pos
);
831 if (parent_macro
&& strcmp(macro
, parent_macro
) == 0)
835 if (strcmp(macro
, "ntohl") == 0) {
836 *rl
= alloc_whole_rl(&uint_ctype
);
839 if (strcmp(macro
, "ntohs") == 0) {
840 *rl
= alloc_whole_rl(&ushort_ctype
);
846 static int has_user_data(struct symbol
*sym
)
848 struct sm_state
*tmp
;
850 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), tmp
) {
853 } END_FOR_EACH_SM(tmp
);
857 static int we_pass_user_data(struct expression
*call
)
859 struct expression
*arg
;
862 FOR_EACH_PTR(call
->args
, arg
) {
863 sym
= expr_to_sym(arg
);
866 if (has_user_data(sym
))
868 } END_FOR_EACH_PTR(arg
);
873 static int db_returned_user_rl(struct expression
*call
, struct range_list
**rl
)
875 struct smatch_state
*state
;
878 if (is_fake_call(call
))
880 snprintf(buf
, sizeof(buf
), "return %p", call
);
881 state
= get_state(my_id
, buf
, NULL
);
882 if (!state
|| !estate_rl(state
))
884 *rl
= estate_rl(state
);
888 struct stree
*get_user_stree(void)
890 return get_all_states_stree(my_id
);
893 static int user_data_flag
;
894 static int no_user_data_flag
;
895 struct range_list
*var_user_rl(struct expression
*expr
)
897 struct smatch_state
*state
;
898 struct range_list
*rl
;
899 struct range_list
*absolute_rl
;
901 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
902 no_user_data_flag
= 1;
906 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
907 struct range_list
*left
, *right
;
909 if (!get_user_rl(expr
->right
, &right
))
911 get_absolute_rl(expr
->left
, &left
);
912 rl
= rl_binop(left
, '%', right
);
916 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '/') {
917 struct range_list
*left
= NULL
;
918 struct range_list
*right
= NULL
;
919 struct range_list
*abs_right
;
922 * The specific bug I'm dealing with is:
924 * foo = capped_user / unknown;
926 * Instead of just saying foo is now entirely user_rl we should
927 * probably say instead that it is not at all user data.
931 get_user_rl(expr
->left
, &left
);
932 get_user_rl(expr
->right
, &right
);
933 get_absolute_rl(expr
->right
, &abs_right
);
935 if (left
&& !right
) {
936 rl
= rl_binop(left
, '/', abs_right
);
937 if (sval_cmp(rl_max(left
), rl_max(rl
)) < 0)
938 no_user_data_flag
= 1;
944 if (get_rl_from_function(expr
, &rl
))
947 if (get_user_macro_rl(expr
, &rl
))
950 if (comes_from_skb_data(expr
)) {
951 rl
= alloc_whole_rl(get_type(expr
));
955 state
= get_state_expr(my_id
, expr
);
956 if (state
&& estate_rl(state
)) {
957 rl
= estate_rl(state
);
961 if (expr
->type
== EXPR_CALL
&& db_returned_user_rl(expr
, &rl
))
964 if (is_array(expr
)) {
965 struct expression
*array
= get_array_base(expr
);
967 if (!get_state_expr(my_id
, array
)) {
968 no_user_data_flag
= 1;
973 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*' &&
974 is_user_rl(expr
->unop
)) {
975 rl
= var_to_absolute_rl(expr
);
982 absolute_rl
= var_to_absolute_rl(expr
);
983 return clone_rl(rl_intersection(rl
, absolute_rl
));
986 static bool is_ptr_subtract(struct expression
*expr
)
988 expr
= strip_expr(expr
);
991 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '-' &&
992 type_is_ptr(get_type(expr
->left
))) {
998 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
1000 if (is_ptr_subtract(expr
))
1004 no_user_data_flag
= 0;
1005 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
1006 if (!user_data_flag
|| no_user_data_flag
)
1012 int is_user_rl(struct expression
*expr
)
1014 struct range_list
*tmp
;
1016 return !!get_user_rl(expr
, &tmp
);
1019 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
1021 struct smatch_state
*state
;
1023 state
= get_state(my_id
, name
, sym
);
1024 if (state
&& estate_rl(state
)) {
1025 *rl
= estate_rl(state
);
1031 static char *get_user_rl_str(struct expression
*expr
, struct symbol
*type
)
1033 struct range_list
*rl
;
1034 static char buf
[64];
1036 if (!get_user_rl(expr
, &rl
))
1038 rl
= cast_rl(type
, rl
);
1039 snprintf(buf
, sizeof(buf
), "%s%s%s",
1041 user_rl_capped(expr
) ? "[c]" : "",
1042 user_rl_treat_untagged(expr
) ? "[u]" : "");
1046 static void match_call_info(struct expression
*expr
)
1048 struct expression
*arg
;
1049 struct symbol
*type
;
1054 FOR_EACH_PTR(expr
->args
, arg
) {
1056 type
= get_arg_type(expr
->fn
, i
);
1057 str
= get_user_rl_str(arg
, type
);
1061 sql_insert_caller_info(expr
, USER_DATA
, i
, "$", str
);
1062 } END_FOR_EACH_PTR(arg
);
1065 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1067 struct smatch_state
*state
;
1068 struct range_list
*rl
;
1069 struct symbol
*type
;
1073 * Smatch uses a hack where if we get an unsigned long we say it's
1074 * both user data and it points to user data. But if we pass it to a
1075 * function which takes an int, then it's just user data. There's not
1076 * enough bytes for it to be a pointer.
1079 type
= get_arg_type(call
->fn
, param
);
1080 if (type
&& type_bits(type
) < type_bits(&ptr_ctype
))
1083 if (strcmp(sm
->state
->name
, "") == 0)
1086 if (strcmp(printed_name
, "*$") == 0 &&
1087 is_struct_ptr(sm
->sym
))
1090 state
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1091 if (!state
|| !estate_rl(state
))
1092 rl
= estate_rl(sm
->state
);
1094 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
1099 snprintf(buf
, sizeof(buf
), "%s%s%s", show_rl(rl
),
1100 estate_capped(sm
->state
) ? "[c]" : "",
1101 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1102 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, buf
);
1105 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1107 struct expression
*arg
;
1110 struct smatch_state
*state
;
1112 while (expr
->type
== EXPR_ASSIGNMENT
)
1113 expr
= strip_expr(expr
->right
);
1114 if (expr
->type
!= EXPR_CALL
)
1117 arg
= get_argument_from_call_expr(expr
->args
, param
);
1120 name
= get_variable_from_key(arg
, key
, &sym
);
1124 state
= get_state(my_id
, name
, sym
);
1128 set_state(my_id
, name
, sym
, alloc_estate_empty());
1133 static bool param_data_capped(const char *value
)
1135 if (strstr(value
, ",c") || strstr(value
, "[c"))
1140 static bool param_data_treat_untagged(const char *value
)
1142 if (strstr(value
, ",u") || strstr(value
, "[u"))
1147 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1149 struct range_list
*rl
= NULL
;
1150 struct smatch_state
*state
;
1151 struct expression
*expr
;
1152 struct symbol
*type
;
1154 char *key_orig
= key
;
1155 bool add_star
= false;
1157 if (strcmp(key
, "**$") == 0) {
1158 snprintf(fullname
, sizeof(fullname
), "**%s", name
);
1160 if (key
[0] == '*') {
1165 snprintf(fullname
, 256, "%s%s%s", add_star
? "*" : "", name
, key
+ 1);
1168 expr
= symbol_expression(sym
);
1169 type
= get_member_type_from_key(expr
, key_orig
);
1172 * Say this function takes a struct ponter but the caller passes
1173 * this_function(skb->data). We have two options, we could pass *$
1174 * as user data or we could pass foo->bar, foo->baz as user data.
1175 * The second option is easier to implement so we do that.
1178 if (strcmp(key_orig
, "*$") == 0) {
1179 struct symbol
*tmp
= type
;
1181 while (tmp
&& tmp
->type
== SYM_PTR
)
1182 tmp
= get_real_base_type(tmp
);
1184 if (tmp
&& (tmp
->type
== SYM_STRUCT
|| tmp
->type
== SYM_UNION
)) {
1185 tag_as_user_data(symbol_expression(sym
));
1190 str_to_rl(type
, value
, &rl
);
1191 state
= alloc_estate_rl(rl
);
1192 if (param_data_capped(value
) || is_capped(expr
))
1193 estate_set_capped(state
);
1194 if (param_data_treat_untagged(value
) || sym
->ctype
.as
== 5)
1195 estate_set_treat_untagged(state
);
1196 set_state(my_id
, fullname
, sym
, state
);
1199 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1201 set_state(my_call_id
, "this_function", NULL
, &called
);
1204 static void match_syscall_definition(struct symbol
*sym
)
1211 macro
= get_macro_name(sym
->pos
);
1213 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
1214 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1217 name
= get_function();
1218 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
1219 if (name
&& strncmp(name
, "sys_", 4) == 0)
1223 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
1229 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
1230 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
1231 } END_FOR_EACH_PTR(arg
);
1234 static void store_user_data_return(struct expression
*expr
, char *key
, char *value
)
1236 struct range_list
*rl
;
1237 struct symbol
*type
;
1240 if (strcmp(key
, "$") != 0)
1243 type
= get_type(expr
);
1244 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1245 call_results_to_rl(expr
, type
, value
, &rl
);
1247 set_state(my_id
, buf
, NULL
, alloc_estate_rl(rl
));
1250 static void set_to_user_data(struct expression
*expr
, char *key
, char *value
)
1252 struct smatch_state
*state
;
1255 struct symbol
*type
;
1256 struct range_list
*rl
= NULL
;
1258 type
= get_member_type_from_key(expr
, key
);
1259 name
= get_variable_from_key(expr
, key
, &sym
);
1263 call_results_to_rl(expr
, type
, value
, &rl
);
1265 state
= alloc_estate_rl(rl
);
1266 if (param_data_capped(value
))
1267 estate_set_capped(state
);
1268 if (param_data_treat_untagged(value
))
1269 estate_set_treat_untagged(state
);
1270 set_state(my_id
, name
, sym
, state
);
1275 static void returns_param_user_data(struct expression
*expr
, int param
, char *key
, char *value
)
1277 struct expression
*arg
;
1278 struct expression
*call
;
1281 while (call
->type
== EXPR_ASSIGNMENT
)
1282 call
= strip_expr(call
->right
);
1283 if (call
->type
!= EXPR_CALL
)
1286 if (!we_pass_user_data(call
))
1290 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1291 store_user_data_return(expr
, key
, value
);
1294 set_to_user_data(expr
->left
, key
, value
);
1298 arg
= get_argument_from_call_expr(call
->args
, param
);
1301 set_to_user_data(arg
, key
, value
);
1304 static void returns_param_user_data_set(struct expression
*expr
, int param
, char *key
, char *value
)
1306 struct expression
*arg
;
1308 func_gets_user_data
= true;
1311 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1312 store_user_data_return(expr
, key
, value
);
1315 if (strcmp(key
, "*$") == 0) {
1316 set_points_to_user_data(expr
->left
);
1317 tag_as_user_data(expr
->left
);
1319 set_to_user_data(expr
->left
, key
, value
);
1324 while (expr
->type
== EXPR_ASSIGNMENT
)
1325 expr
= strip_expr(expr
->right
);
1326 if (expr
->type
!= EXPR_CALL
)
1329 arg
= get_argument_from_call_expr(expr
->args
, param
);
1332 set_to_user_data(arg
, key
, value
);
1335 static void param_set_to_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
1337 struct sm_state
*sm
;
1338 struct smatch_state
*start_state
;
1339 struct range_list
*rl
;
1342 const char *param_name
;
1343 struct symbol
*ret_sym
;
1344 bool return_found
= false;
1345 bool pointed_at_found
= false;
1348 expr
= strip_expr(expr
);
1349 return_str
= expr_to_str(expr
);
1350 ret_sym
= expr_to_sym(expr
);
1352 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
1353 param
= get_param_num_from_sym(sm
->sym
);
1357 if (!param_was_set_var_sym(sm
->name
, sm
->sym
))
1360 /* The logic here was that if we were passed in a user data then
1361 * we don't record that. It's like the difference between
1362 * param_filter and param_set. When I think about it, I'm not
1363 * sure it actually works. It's probably harmless because we
1364 * checked earlier that we're not returning a parameter...
1365 * Let's mark this as a TODO.
1367 start_state
= get_state_stree(start_states
, my_id
, sm
->name
, sm
->sym
);
1368 if (start_state
&& rl_equiv(estate_rl(sm
->state
), estate_rl(start_state
)))
1371 param_name
= get_param_name(sm
);
1374 if (strcmp(param_name
, "$") == 0) /* The -1 param is handled after the loop */
1377 snprintf(buf
, sizeof(buf
), "%s%s%s",
1378 show_rl(estate_rl(sm
->state
)),
1379 estate_capped(sm
->state
) ? "[c]" : "",
1380 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1381 sql_insert_return_states(return_id
, return_ranges
,
1382 func_gets_user_data
? USER_DATA_SET
: USER_DATA
,
1383 param
, param_name
, buf
);
1384 } END_FOR_EACH_SM(sm
);
1386 /* This if for "return foo;" where "foo->bar" is user data. */
1387 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
1390 if (ret_sym
!= sm
->sym
)
1393 param_name
= state_name_to_param_name(sm
->name
, return_str
);
1396 if (strcmp(param_name
, "$") == 0)
1397 return_found
= true;
1398 if (strcmp(param_name
, "*$") == 0)
1399 pointed_at_found
= true;
1400 snprintf(buf
, sizeof(buf
), "%s%s%s",
1401 show_rl(estate_rl(sm
->state
)),
1402 estate_capped(sm
->state
) ? "[c]" : "",
1403 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1404 sql_insert_return_states(return_id
, return_ranges
,
1405 func_gets_user_data
? USER_DATA_SET
: USER_DATA
,
1406 -1, param_name
, buf
);
1407 } END_FOR_EACH_SM(sm
);
1409 /* This if for "return ntohl(foo);" */
1410 if (!return_found
&& get_user_rl(expr
, &rl
)) {
1411 snprintf(buf
, sizeof(buf
), "%s%s%s",
1413 user_rl_capped(expr
) ? "[c]" : "",
1414 user_rl_treat_untagged(expr
) ? "[u]" : "");
1415 sql_insert_return_states(return_id
, return_ranges
,
1416 func_gets_user_data
? USER_DATA_SET
: USER_DATA
,
1421 * This is to handle things like return skb->data where we don't set a
1424 if (!pointed_at_found
&& points_to_user_data(expr
)) {
1425 sql_insert_return_states(return_id
, return_ranges
,
1426 (is_skb_data(expr
) || func_gets_user_data
) ?
1427 USER_DATA_SET
: USER_DATA
,
1428 -1, "*$", "s64min-s64max");
1431 free_string(return_str
);
1434 static void returns_param_capped(struct expression
*expr
, int param
, char *key
, char *value
)
1436 struct smatch_state
*state
, *new;
1440 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1444 state
= get_state(my_id
, name
, sym
);
1445 if (!state
|| estate_capped(state
))
1448 new = clone_estate(state
);
1449 estate_set_capped(new);
1451 set_state(my_id
, name
, sym
, new);
1456 static struct int_stack
*gets_data_stack
;
1457 static void match_function_def(struct symbol
*sym
)
1459 func_gets_user_data
= false;
1462 static void match_inline_start(struct expression
*expr
)
1464 push_int(&gets_data_stack
, func_gets_user_data
);
1467 static void match_inline_end(struct expression
*expr
)
1469 func_gets_user_data
= pop_int(&gets_data_stack
);
1472 void register_kernel_user_data(int id
)
1478 if (option_project
!= PROJ_KERNEL
)
1481 set_dynamic_states(my_id
);
1483 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1484 add_hook(&match_inline_start
, INLINE_FN_START
);
1485 add_hook(&match_inline_end
, INLINE_FN_END
);
1487 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
1488 add_hook(&free_start_states
, AFTER_FUNC_HOOK
);
1489 add_hook(&match_save_states
, INLINE_FN_START
);
1490 add_hook(&match_restore_states
, INLINE_FN_END
);
1492 add_unmatched_state_hook(my_id
, &empty_state
);
1493 add_extra_nomod_hook(&extra_nomod_hook
);
1494 add_pre_merge_hook(my_id
, &pre_merge_hook
);
1495 add_merge_hook(my_id
, &merge_estates
);
1497 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
1498 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
1499 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
1500 for (i
= 0; i
< ARRAY_SIZE(kstr_funcs
); i
++)
1501 add_function_hook(kstr_funcs
[i
], &match_user_copy
, INT_PTR(2));
1502 add_function_hook("usb_control_msg", &match_user_copy
, INT_PTR(6));
1504 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++) {
1505 add_function_assign_hook(returns_user_data
[i
], &match_user_assign_function
, NULL
);
1506 add_function_hook(returns_user_data
[i
], &match_returns_user_rl
, NULL
);
1509 add_function_hook("sscanf", &match_sscanf
, NULL
);
1511 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
1513 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1514 select_return_states_hook(PARAM_SET
, &db_param_set
);
1515 add_hook(&match_condition
, CONDITION_HOOK
);
1517 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1518 add_member_info_callback(my_id
, struct_member_callback
);
1519 select_caller_info_hook(set_param_user_data
, USER_DATA
);
1520 select_return_states_hook(USER_DATA
, &returns_param_user_data
);
1521 select_return_states_hook(USER_DATA_SET
, &returns_param_user_data_set
);
1522 select_return_states_hook(CAPPED_DATA
, &returns_param_capped
);
1523 add_split_return_callback(¶m_set_to_user_data
);
1526 void register_kernel_user_data2(int id
)
1530 if (option_project
!= PROJ_KERNEL
)
1532 select_caller_info_hook(set_called
, INTERNAL
);