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 unsigned long 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 struct stree
*start_states
;
52 static void save_start_states(struct statement
*stmt
)
54 start_states
= clone_stree(__get_cur_stree());
57 static void free_start_states(void)
59 free_stree(&start_states
);
62 static struct smatch_state
*empty_state(struct sm_state
*sm
)
64 return alloc_estate_empty();
67 static struct smatch_state
*new_state(struct symbol
*type
)
69 struct smatch_state
*state
;
71 if (!type
|| type_is_ptr(type
))
74 state
= alloc_estate_whole(type
);
75 estate_set_new(state
);
79 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
81 struct smatch_state
*user
= cur
->state
;
82 struct smatch_state
*extra
;
83 struct smatch_state
*state
;
84 struct range_list
*rl
;
86 extra
= __get_state(SMATCH_EXTRA
, cur
->name
, cur
->sym
);
89 rl
= rl_intersection(estate_rl(user
), estate_rl(extra
));
90 state
= alloc_estate_rl(clone_rl(rl
));
91 if (estate_capped(user
) || is_capped_var_sym(cur
->name
, cur
->sym
))
92 estate_set_capped(state
);
93 if (estate_treat_untagged(user
))
94 estate_set_treat_untagged(state
);
95 if (estates_equiv(state
, cur
->state
))
97 set_state(my_id
, cur
->name
, cur
->sym
, state
);
100 static void extra_nomod_hook(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
102 struct smatch_state
*user
, *new;
103 struct range_list
*rl
;
105 user
= __get_state(my_id
, name
, sym
);
108 rl
= rl_intersection(estate_rl(user
), estate_rl(state
));
109 if (rl_equiv(rl
, estate_rl(user
)))
111 new = alloc_estate_rl(rl
);
112 if (estate_capped(user
))
113 estate_set_capped(new);
114 if (estate_treat_untagged(user
))
115 estate_set_treat_untagged(new);
116 set_state(my_id
, name
, sym
, new);
119 static void store_type_info(struct expression
*expr
, struct smatch_state
*state
)
122 char *type_str
, *member
;
124 if (__in_fake_assign
)
127 if (!estate_rl(state
))
130 expr
= strip_expr(expr
);
131 if (!expr
|| expr
->type
!= EXPR_DEREF
|| !expr
->member
)
134 type
= get_type(expr
->deref
);
135 if (!type
|| !type
->ident
)
138 type_str
= type_to_str(type
);
141 member
= get_member_name(expr
);
145 sql_insert_function_type_info(USER_DATA
, type_str
, member
, state
->name
);
148 static void set_user_data(struct expression
*expr
, struct smatch_state
*state
)
150 store_type_info(expr
, state
);
151 set_state_expr(my_id
, expr
, state
);
154 static bool user_rl_known(struct expression
*expr
)
156 struct range_list
*rl
;
159 if (!get_user_rl(expr
, &rl
))
162 close_to_max
= sval_type_max(rl_type(rl
));
163 close_to_max
.value
-= 100;
165 if (sval_cmp(rl_max(rl
), close_to_max
) >= 0)
170 static bool is_array_index_mask_nospec(struct expression
*expr
)
172 struct expression
*orig
;
174 orig
= get_assigned_expr(expr
);
175 if (!orig
|| orig
->type
!= EXPR_CALL
)
177 return sym_name_is("array_index_mask_nospec", orig
->fn
);
180 static bool binop_capped(struct expression
*expr
)
182 struct range_list
*left_rl
;
186 if (expr
->op
== '-' && get_user_rl(expr
->left
, &left_rl
)) {
187 if (user_rl_capped(expr
->left
))
189 comparison
= get_comparison(expr
->left
, expr
->right
);
190 if (comparison
&& show_special(comparison
)[0] == '>')
195 if (expr
->op
== '&' || expr
->op
== '%') {
196 bool left_user
, left_capped
, right_user
, right_capped
;
198 if (!get_value(expr
->right
, &sval
) && is_capped(expr
->right
))
200 if (is_array_index_mask_nospec(expr
->right
))
202 if (is_capped(expr
->left
))
204 left_user
= is_user_rl(expr
->left
);
205 right_user
= is_user_rl(expr
->right
);
206 if (!left_user
&& !right_user
)
209 left_capped
= user_rl_capped(expr
->left
);
210 right_capped
= user_rl_capped(expr
->right
);
212 if (left_user
&& left_capped
) {
215 if (right_user
&& right_capped
)
219 if (right_user
&& right_capped
) {
228 * Generally "capped" means that we capped it to an unknown value.
229 * This is useful because if Smatch doesn't know what the value is then
230 * we have to trust that it is correct. But if we known cap value is
231 * 100 then we can check if 100 is correct and complain if it's wrong.
233 * So then the problem is with BINOP when we take a capped variable
234 * plus a user variable which is clamped to a known range (uncapped)
235 * the result should be capped.
237 if ((user_rl_capped(expr
->left
) || user_rl_known(expr
->left
)) &&
238 (user_rl_capped(expr
->right
) || user_rl_known(expr
->right
)))
244 bool user_rl_capped(struct expression
*expr
)
246 struct smatch_state
*state
;
247 struct range_list
*rl
;
250 expr
= strip_expr(expr
);
253 if (get_value(expr
, &sval
))
255 if (expr
->type
== EXPR_BINOP
)
256 return binop_capped(expr
);
257 if ((expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) &&
258 (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
))
259 return user_rl_capped(expr
->unop
);
260 state
= get_state_expr(my_id
, expr
);
262 return estate_capped(state
);
264 if (!get_user_rl(expr
, &rl
)) {
266 * The non user data parts of a binop are capped and
267 * also empty user rl states are capped.
272 if (rl_to_sval(rl
, &sval
))
275 return false; /* uncapped user data */
278 bool user_rl_treat_untagged(struct expression
*expr
)
280 struct smatch_state
*state
;
281 struct range_list
*rl
;
284 expr
= strip_expr(expr
);
287 if (get_value(expr
, &sval
))
290 state
= get_state_expr(my_id
, expr
);
292 return estate_treat_untagged(state
);
294 if (get_user_rl(expr
, &rl
))
295 return false; /* uncapped user data */
297 return true; /* not actually user data */
300 static void tag_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
302 struct expression
*edge_member
;
303 struct symbol
*base
= get_real_base_type(member
);
307 expr
= member_expression(expr
, '.', member
->ident
);
309 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
312 type
= get_real_base_type(tmp
);
316 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
317 tag_inner_struct_members(expr
, tmp
);
324 edge_member
= member_expression(expr
, '.', tmp
->ident
);
325 set_user_data(edge_member
, new_state(type
));
326 } END_FOR_EACH_PTR(tmp
);
329 void __set_user_string(struct expression
*expr
);
330 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
333 struct expression
*member
;
336 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
337 expr
= strip_expr(expr
->unop
);
341 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
342 type
= get_real_base_type(tmp
);
346 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
347 tag_inner_struct_members(expr
, tmp
);
354 member
= member_expression(expr
, op
, tmp
->ident
);
355 if (type
->type
== SYM_ARRAY
) {
356 set_points_to_user_data(member
);
358 set_user_data(member
, new_state(get_type(member
)));
360 } END_FOR_EACH_PTR(tmp
);
363 static void tag_base_type(struct expression
*expr
)
365 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
366 expr
= strip_expr(expr
->unop
);
368 expr
= deref_expression(expr
);
369 set_user_data(expr
, new_state(get_type(expr
)));
372 static void tag_as_user_data(struct expression
*expr
)
376 expr
= strip_expr(expr
);
378 type
= get_type(expr
);
379 if (!type
|| type
->type
!= SYM_PTR
)
381 type
= get_real_base_type(type
);
384 if (type
== &void_ctype
) {
385 set_user_data(deref_expression(expr
), new_state(&ulong_ctype
));
388 if (type
->type
== SYM_BASETYPE
) {
389 if (expr
->type
!= EXPR_PREOP
&& expr
->op
!= '&')
390 set_points_to_user_data(expr
);
394 if (type
->type
== SYM_STRUCT
|| type
->type
== SYM_UNION
) {
395 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
396 expr
= deref_expression(expr
);
398 set_user_data(deref_expression(expr
), new_state(&ulong_ctype
));
399 tag_struct_members(type
, expr
);
403 static struct expression
*ignore_param_set
;
404 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
406 int param
= PTR_INT(_param
);
407 struct expression
*dest
;
409 func_gets_user_data
= true;
410 ignore_param_set
= expr
;
412 dest
= get_argument_from_call_expr(expr
->args
, param
);
413 dest
= strip_expr(dest
);
416 tag_as_user_data(dest
);
419 static int is_dev_attr_name(struct expression
*expr
)
424 name
= expr_to_str(expr
);
427 if (strstr(name
, "->attr.name"))
433 static int ends_in_n(struct expression
*expr
)
439 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
446 if (str
->data
[str
->length
- 3] == '%' &&
447 str
->data
[str
->length
- 2] == 'n')
452 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
454 struct expression
*str
, *format
, *arg
;
457 func_gets_user_data
= true;
459 str
= get_argument_from_call_expr(expr
->args
, 0);
460 if (is_dev_attr_name(str
))
463 format
= get_argument_from_call_expr(expr
->args
, 1);
464 if (is_dev_attr_name(format
))
467 last
= ptr_list_size((struct ptr_list
*)expr
->args
) - 1;
470 FOR_EACH_PTR(expr
->args
, arg
) {
474 if (i
== last
&& ends_in_n(format
))
476 tag_as_user_data(arg
);
477 } END_FOR_EACH_PTR(arg
);
480 static int get_rl_from_function(struct expression
*expr
, struct range_list
**rl
)
484 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
||
485 !expr
->fn
->symbol_name
|| !expr
->fn
->symbol_name
->name
)
488 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++) {
489 if (strcmp(expr
->fn
->symbol_name
->name
, returns_user_data
[i
]) == 0) {
490 *rl
= alloc_whole_rl(get_type(expr
));
497 static int comes_from_skb_data(struct expression
*expr
)
499 expr
= strip_expr(expr
);
500 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
503 expr
= strip_expr(expr
->unop
);
506 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
507 expr
= strip_expr(expr
->left
);
509 return is_skb_data(expr
);
512 static int handle_get_user(struct expression
*expr
)
517 name
= get_macro_name(expr
->pos
);
518 if (!name
|| strcmp(name
, "get_user") != 0)
521 name
= expr_to_var(expr
->right
);
522 if (!name
|| (strcmp(name
, "__val_gu") != 0 && strcmp(name
, "__gu_val") != 0))
524 set_user_data(expr
->left
, new_state(get_type(expr
->left
)));
531 static bool state_is_new(struct expression
*expr
)
533 struct smatch_state
*state
;
535 state
= get_state_expr(my_id
, expr
);
536 if (estate_new(state
))
539 if (expr
->type
== EXPR_BINOP
) {
540 if (state_is_new(expr
->left
))
542 if (state_is_new(expr
->right
))
548 static bool handle_op_assign(struct expression
*expr
)
550 struct expression
*binop_expr
;
551 struct smatch_state
*state
;
552 struct range_list
*rl
;
555 case SPECIAL_ADD_ASSIGN
:
556 case SPECIAL_SUB_ASSIGN
:
557 case SPECIAL_AND_ASSIGN
:
558 case SPECIAL_MOD_ASSIGN
:
559 case SPECIAL_SHL_ASSIGN
:
560 case SPECIAL_SHR_ASSIGN
:
561 case SPECIAL_OR_ASSIGN
:
562 case SPECIAL_XOR_ASSIGN
:
563 case SPECIAL_MUL_ASSIGN
:
564 case SPECIAL_DIV_ASSIGN
:
565 binop_expr
= binop_expression(expr
->left
,
566 op_remove_assign(expr
->op
),
568 if (!get_user_rl(binop_expr
, &rl
))
571 rl
= cast_rl(get_type(expr
->left
), rl
);
572 state
= alloc_estate_rl(rl
);
573 if (expr
->op
== SPECIAL_AND_ASSIGN
||
574 expr
->op
== SPECIAL_MOD_ASSIGN
||
575 user_rl_capped(binop_expr
))
576 estate_set_capped(state
);
577 if (user_rl_treat_untagged(expr
->left
))
578 estate_set_treat_untagged(state
);
579 if (state_is_new(binop_expr
))
580 estate_set_new(state
);
581 set_user_data(expr
->left
, state
);
587 static void match_assign(struct expression
*expr
)
589 struct symbol
*left_type
, *right_type
;
590 struct range_list
*rl
;
591 static struct expression
*handled
;
592 struct smatch_state
*state
;
593 struct expression
*faked
;
594 bool is_capped
= false;
597 left_type
= get_type(expr
->left
);
598 if (left_type
== &void_ctype
)
601 faked
= get_faked_expression();
603 /* FIXME: handle fake array assignments frob(&user_array[x]); */
605 if (is_fake_call(expr
->right
) && faked
&&
606 faked
->type
== EXPR_ASSIGNMENT
&&
607 points_to_user_data(faked
->right
)) {
608 if (is_skb_data(faked
->right
))
609 func_gets_user_data
= true;
610 rl
= alloc_whole_rl(get_type(expr
->left
));
615 if (faked
&& faked
== handled
)
617 if (is_fake_call(expr
->right
))
618 goto clear_old_state
;
619 if (handle_get_user(expr
))
621 if (points_to_user_data(expr
->right
) &&
622 is_struct_ptr(get_type(expr
->left
))) {
624 // This should be handled by smatch_points_to_user_data.c
625 // set_points_to_user_data(expr->left);
628 if (handle_op_assign(expr
))
631 goto clear_old_state
;
633 /* Handled by DB code */
634 if (expr
->right
->type
== EXPR_CALL
)
637 if (!get_user_rl(expr
->right
, &rl
))
638 goto clear_old_state
;
639 is_capped
= user_rl_capped(expr
->right
);
640 is_new
= state_is_new(expr
->right
);
643 if (type_is_ptr(left_type
)) {
644 right_type
= get_type(expr
->right
);
645 if (right_type
&& right_type
->type
== SYM_ARRAY
)
646 set_points_to_user_data(expr
->left
);
650 rl
= cast_rl(left_type
, rl
);
651 state
= alloc_estate_rl(rl
);
653 estate_set_new(state
);
655 estate_set_capped(state
);
656 if (user_rl_treat_untagged(expr
->right
))
657 estate_set_treat_untagged(state
);
659 set_user_data(expr
->left
, state
);
666 * HACK ALERT!!! This should be at the start of the function. The
667 * the problem is that handling "pointer = array;" assignments is
668 * handled in this function instead of in kernel_points_to_user_data.c.
670 if (type_is_ptr(left_type
))
673 if (get_state_expr(my_id
, expr
->left
))
674 set_user_data(expr
->left
, alloc_estate_empty());
677 static void handle_eq_noteq(struct expression
*expr
)
679 struct smatch_state
*left_orig
, *right_orig
;
681 left_orig
= get_state_expr(my_id
, expr
->left
);
682 right_orig
= get_state_expr(my_id
, expr
->right
);
684 if (!left_orig
&& !right_orig
)
686 if (left_orig
&& right_orig
)
690 set_true_false_states_expr(my_id
, expr
->left
,
691 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
692 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
694 set_true_false_states_expr(my_id
, expr
->right
,
695 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
696 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
700 static struct range_list
*strip_negatives(struct range_list
*rl
)
702 sval_t min
= rl_min(rl
);
703 sval_t minus_one
= { .type
= rl_type(rl
), .value
= -1 };
704 sval_t over
= { .type
= rl_type(rl
), .value
= INT_MAX
+ 1ULL };
705 sval_t max
= sval_type_max(rl_type(rl
));
710 if (type_unsigned(rl_type(rl
)) && type_bits(rl_type(rl
)) > 31)
711 return remove_range(rl
, over
, max
);
713 return remove_range(rl
, min
, minus_one
);
716 static void handle_compare(struct expression
*expr
)
718 struct expression
*left
, *right
;
719 struct range_list
*left_rl
= NULL
;
720 struct range_list
*right_rl
= NULL
;
721 struct range_list
*user_rl
;
722 struct smatch_state
*capped_state
;
723 struct smatch_state
*left_true
= NULL
;
724 struct smatch_state
*left_false
= NULL
;
725 struct smatch_state
*right_true
= NULL
;
726 struct smatch_state
*right_false
= NULL
;
730 left
= strip_expr(expr
->left
);
731 right
= strip_expr(expr
->right
);
733 while (left
->type
== EXPR_ASSIGNMENT
)
734 left
= strip_expr(left
->left
);
737 * Conditions are mostly handled by smatch_extra.c, but there are some
738 * times where the exact values are not known so we can't do that.
740 * Normally, we might consider using smatch_capped.c to supliment smatch
741 * extra but that doesn't work when we merge unknown uncapped kernel
742 * data with unknown capped user data. The result is uncapped user
743 * data. We need to keep it separate and say that the user data is
744 * capped. In the past, I would have marked this as just regular
745 * kernel data (not user data) but we can't do that these days because
746 * we need to track user data for Spectre.
748 * The other situation which we have to handle is when we do have an
749 * int and we compare against an unknown unsigned kernel variable. In
750 * that situation we assume that the kernel data is less than INT_MAX.
751 * Otherwise then we get all sorts of array underflow false positives.
755 /* Handled in smatch_extra.c */
756 if (get_implied_value(left
, &sval
) ||
757 get_implied_value(right
, &sval
))
760 get_user_rl(left
, &left_rl
);
761 get_user_rl(right
, &right_rl
);
764 if (!left_rl
&& !right_rl
)
766 /* if both sides are user data that's not a good limit */
767 if (left_rl
&& right_rl
)
775 type
= get_type(expr
);
776 if (type_unsigned(type
))
777 user_rl
= strip_negatives(user_rl
);
778 capped_state
= alloc_estate_rl(user_rl
);
779 estate_set_capped(capped_state
);
783 case SPECIAL_UNSIGNED_LT
:
785 case SPECIAL_UNSIGNED_LTE
:
787 left_true
= capped_state
;
789 right_false
= capped_state
;
792 case SPECIAL_UNSIGNED_GT
:
794 case SPECIAL_UNSIGNED_GTE
:
796 left_false
= capped_state
;
798 right_true
= capped_state
;
802 set_true_false_states_expr(my_id
, left
, left_true
, left_false
);
803 set_true_false_states_expr(my_id
, right
, right_true
, right_false
);
806 static void match_condition(struct expression
*expr
)
808 if (expr
->type
!= EXPR_COMPARE
)
811 if (expr
->op
== SPECIAL_EQUAL
||
812 expr
->op
== SPECIAL_NOTEQUAL
) {
813 handle_eq_noteq(expr
);
817 handle_compare(expr
);
820 static void match_returns_user_rl(const char *fn
, struct expression
*expr
, void *unused
)
822 func_gets_user_data
= true;
825 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
827 struct expression
*parent
;
833 macro
= get_macro_name(expr
->pos
);
837 /* handle ntohl(foo[i]) where "i" is trusted */
838 parent
= expr_get_parent_expr(expr
);
839 while (parent
&& parent
->type
!= EXPR_BINOP
)
840 parent
= expr_get_parent_expr(parent
);
841 if (parent
&& parent
->type
== EXPR_BINOP
) {
842 char *parent_macro
= get_macro_name(parent
->pos
);
844 if (parent_macro
&& strcmp(macro
, parent_macro
) == 0)
848 if (strcmp(macro
, "ntohl") == 0) {
849 *rl
= alloc_whole_rl(&uint_ctype
);
852 if (strcmp(macro
, "ntohs") == 0) {
853 *rl
= alloc_whole_rl(&ushort_ctype
);
859 static int has_user_data(struct symbol
*sym
)
861 struct sm_state
*tmp
;
863 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), tmp
) {
866 } END_FOR_EACH_SM(tmp
);
870 bool we_pass_user_data(struct expression
*call
)
872 struct expression
*arg
;
875 FOR_EACH_PTR(call
->args
, arg
) {
876 if (points_to_user_data(arg
))
878 sym
= expr_to_sym(arg
);
881 if (has_user_data(sym
))
883 } END_FOR_EACH_PTR(arg
);
888 static int db_returned_user_rl(struct expression
*call
, struct range_list
**rl
)
890 struct smatch_state
*state
;
893 if (is_fake_call(call
))
895 snprintf(buf
, sizeof(buf
), "return %p", call
);
896 state
= get_state(my_id
, buf
, NULL
);
897 if (!state
|| !estate_rl(state
))
899 *rl
= estate_rl(state
);
903 struct stree
*get_user_stree(void)
905 return get_all_states_stree(my_id
);
908 static int user_data_flag
;
909 static int no_user_data_flag
;
910 struct range_list
*var_user_rl(struct expression
*expr
)
912 struct smatch_state
*state
;
913 struct range_list
*rl
;
914 struct range_list
*absolute_rl
;
916 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
917 no_user_data_flag
= 1;
921 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
922 struct range_list
*left
, *right
;
924 if (!get_user_rl(expr
->right
, &right
))
926 get_absolute_rl(expr
->left
, &left
);
927 rl
= rl_binop(left
, '%', right
);
931 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '/') {
932 struct range_list
*left
= NULL
;
933 struct range_list
*right
= NULL
;
934 struct range_list
*abs_right
;
937 * The specific bug I'm dealing with is:
939 * foo = capped_user / unknown;
941 * Instead of just saying foo is now entirely user_rl we should
942 * probably say instead that it is not at all user data.
946 get_user_rl(expr
->left
, &left
);
947 get_user_rl(expr
->right
, &right
);
948 get_absolute_rl(expr
->right
, &abs_right
);
950 if (left
&& !right
) {
951 rl
= rl_binop(left
, '/', abs_right
);
952 if (sval_cmp(rl_max(left
), rl_max(rl
)) < 0)
953 no_user_data_flag
= 1;
959 if (get_rl_from_function(expr
, &rl
))
962 if (get_user_macro_rl(expr
, &rl
))
965 if (comes_from_skb_data(expr
)) {
966 rl
= alloc_whole_rl(get_type(expr
));
970 state
= get_state_expr(my_id
, expr
);
971 if (state
&& estate_rl(state
)) {
972 rl
= estate_rl(state
);
976 if (expr
->type
== EXPR_CALL
&& db_returned_user_rl(expr
, &rl
))
979 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*' &&
980 points_to_user_data(expr
->unop
)) {
981 rl
= var_to_absolute_rl(expr
);
985 if (is_array(expr
)) {
986 struct expression
*array
= get_array_base(expr
);
988 if (!get_state_expr(my_id
, array
)) {
989 no_user_data_flag
= 1;
997 absolute_rl
= var_to_absolute_rl(expr
);
998 return clone_rl(rl_intersection(rl
, absolute_rl
));
1001 static bool is_ptr_subtract(struct expression
*expr
)
1003 expr
= strip_expr(expr
);
1006 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '-' &&
1007 type_is_ptr(get_type(expr
->left
))) {
1013 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
1015 if (is_ptr_subtract(expr
))
1019 no_user_data_flag
= 0;
1020 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
1021 if (!user_data_flag
|| no_user_data_flag
)
1027 int is_user_rl(struct expression
*expr
)
1029 struct range_list
*tmp
;
1031 return get_user_rl(expr
, &tmp
) && tmp
;
1034 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
1036 struct smatch_state
*state
;
1038 state
= get_state(my_id
, name
, sym
);
1039 if (state
&& estate_rl(state
)) {
1040 *rl
= estate_rl(state
);
1046 static void return_info_callback(int return_id
, char *return_ranges
,
1047 struct expression
*returned_expr
,
1049 const char *printed_name
,
1050 struct sm_state
*sm
)
1052 struct smatch_state
*extra
;
1053 struct range_list
*rl
;
1057 if (strcmp(printed_name
, "$") == 0)
1059 if (!param_was_set_var_sym(sm
->name
, sm
->sym
))
1062 rl
= estate_rl(sm
->state
);
1065 extra
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1066 if (estate_rl(extra
))
1067 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(extra
));
1071 snprintf(buf
, sizeof(buf
), "%s%s%s",
1073 estate_capped(sm
->state
) ? "[c]" : "",
1074 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1075 sql_insert_return_states(return_id
, return_ranges
,
1076 estate_new(sm
->state
) ? USER_DATA_SET
: USER_DATA
,
1077 param
, printed_name
, buf
);
1080 static bool is_ignored_macro(struct position pos
)
1084 macro
= get_macro_name(pos
);
1087 if (strcmp(macro
, "v4l2_subdev_call") == 0)
1092 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1094 struct smatch_state
*state
;
1095 struct range_list
*rl
;
1096 struct symbol
*type
;
1099 if (is_ignored_macro(call
->pos
))
1103 * Smatch uses a hack where if we get an unsigned long we say it's
1104 * both user data and it points to user data. But if we pass it to a
1105 * function which takes an int, then it's just user data. There's not
1106 * enough bytes for it to be a pointer.
1109 type
= get_arg_type(call
->fn
, param
);
1110 if (strcmp(printed_name
, "$") != 0 && type
&& type_bits(type
) < type_bits(&ptr_ctype
))
1113 if (strcmp(sm
->state
->name
, "") == 0)
1116 state
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1117 if (!state
|| !estate_rl(state
))
1118 rl
= estate_rl(sm
->state
);
1120 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
1125 snprintf(buf
, sizeof(buf
), "%s%s%s", show_rl(rl
),
1126 estate_capped(sm
->state
) ? "[c]" : "",
1127 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1128 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, buf
);
1131 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1133 struct expression
*arg
;
1136 struct smatch_state
*state
;
1138 while (expr
->type
== EXPR_ASSIGNMENT
)
1139 expr
= strip_expr(expr
->right
);
1140 if (expr
->type
!= EXPR_CALL
)
1142 if (expr
== ignore_param_set
)
1145 arg
= get_argument_from_call_expr(expr
->args
, param
);
1148 name
= get_variable_from_key(arg
, key
, &sym
);
1152 state
= get_state(my_id
, name
, sym
);
1156 set_state(my_id
, name
, sym
, alloc_estate_empty());
1161 static bool param_data_capped(const char *value
)
1163 if (strstr(value
, ",c") || strstr(value
, "[c"))
1168 static bool param_data_treat_untagged(const char *value
)
1170 if (strstr(value
, ",u") || strstr(value
, "[u"))
1175 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1177 struct expression
*expr
;
1178 struct range_list
*rl
= NULL
;
1179 struct smatch_state
*state
;
1180 struct symbol
*type
;
1183 expr
= symbol_expression(sym
);
1184 fullname
= get_variable_from_key(expr
, key
, NULL
);
1188 type
= get_member_type_from_key(expr
, key
);
1189 if (type
&& type
->type
== SYM_STRUCT
)
1195 str_to_rl(type
, value
, &rl
);
1196 rl
= swap_mtag_seed(expr
, rl
);
1197 state
= alloc_estate_rl(rl
);
1198 if (param_data_capped(value
) || is_capped(expr
))
1199 estate_set_capped(state
);
1200 if (param_data_treat_untagged(value
) || sym
->ctype
.as
== 5)
1201 estate_set_treat_untagged(state
);
1202 set_state(my_id
, fullname
, sym
, state
);
1205 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1207 set_state(my_call_id
, "this_function", NULL
, &called
);
1210 static void match_syscall_definition(struct symbol
*sym
)
1217 macro
= get_macro_name(sym
->pos
);
1219 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
1220 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1223 name
= get_function();
1224 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
1225 if (name
&& strncmp(name
, "sys_", 4) == 0)
1229 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
1235 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
1236 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
1237 } END_FOR_EACH_PTR(arg
);
1243 static void store_user_data_return(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1245 struct smatch_state
*state
;
1246 struct range_list
*rl
;
1247 struct symbol
*type
;
1253 type
= get_type(expr
);
1254 snprintf(buf
, sizeof(buf
), "return %p%s", expr
, key
+ 1);
1255 call_results_to_rl(expr
, type
, value
, &rl
);
1257 state
= alloc_estate_rl(rl
);
1259 estate_set_new(state
);
1261 set_state(my_id
, buf
, NULL
, state
);
1264 static void set_to_user_data(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1266 struct smatch_state
*state
;
1269 struct symbol
*type
;
1270 struct range_list
*rl
= NULL
;
1272 type
= get_member_type_from_key(expr
, key
);
1273 name
= get_variable_from_key(expr
, key
, &sym
);
1277 call_results_to_rl(expr
, type
, value
, &rl
);
1279 state
= alloc_estate_rl(rl
);
1280 if (param_data_capped(value
))
1281 estate_set_capped(state
);
1282 if (param_data_treat_untagged(value
))
1283 estate_set_treat_untagged(state
);
1285 estate_set_new(state
);
1286 set_state(my_id
, name
, sym
, state
);
1291 static void returns_param_user_data(struct expression
*expr
, int param
, char *key
, char *value
)
1293 struct expression
*arg
;
1294 struct expression
*call
;
1297 while (call
->type
== EXPR_ASSIGNMENT
)
1298 call
= strip_expr(call
->right
);
1299 if (call
->type
!= EXPR_CALL
)
1302 if (!we_pass_user_data(call
))
1306 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1307 store_user_data_return(expr
, key
, value
, OLD
);
1310 set_to_user_data(expr
->left
, key
, value
, OLD
);
1314 arg
= get_argument_from_call_expr(call
->args
, param
);
1317 set_to_user_data(arg
, key
, value
, OLD
);
1320 static void returns_param_user_data_set(struct expression
*expr
, int param
, char *key
, char *value
)
1322 struct expression
*arg
;
1324 func_gets_user_data
= true;
1327 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1328 store_user_data_return(expr
, key
, value
, NEW
);
1331 set_to_user_data(expr
->left
, key
, value
, NEW
);
1335 while (expr
->type
== EXPR_ASSIGNMENT
)
1336 expr
= strip_expr(expr
->right
);
1337 if (expr
->type
!= EXPR_CALL
)
1340 arg
= get_argument_from_call_expr(expr
->args
, param
);
1343 set_to_user_data(arg
, key
, value
, NEW
);
1346 static void returns_param_capped(struct expression
*expr
, int param
, char *key
, char *value
)
1348 struct smatch_state
*state
, *new;
1352 name
= get_name_sym_from_param_key(expr
, param
, key
, &sym
);
1356 state
= get_state(my_id
, name
, sym
);
1357 if (!state
|| estate_capped(state
))
1360 new = clone_estate(state
);
1361 estate_set_capped(new);
1363 set_state(my_id
, name
, sym
, new);
1368 static void match_function_def(struct symbol
*sym
)
1370 if (is_user_data_fn(sym
))
1371 func_gets_user_data
= true;
1374 void register_kernel_user_data(int id
)
1380 if (option_project
!= PROJ_KERNEL
)
1383 set_dynamic_states(my_id
);
1385 add_function_data(&func_gets_user_data
);
1386 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1388 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
1389 add_hook(&free_start_states
, AFTER_FUNC_HOOK
);
1390 add_function_data((unsigned long *)&start_states
);
1392 add_unmatched_state_hook(my_id
, &empty_state
);
1393 add_extra_nomod_hook(&extra_nomod_hook
);
1394 add_pre_merge_hook(my_id
, &pre_merge_hook
);
1395 add_merge_hook(my_id
, &merge_estates
);
1397 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
1398 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
1399 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
1400 for (i
= 0; i
< ARRAY_SIZE(kstr_funcs
); i
++)
1401 add_function_hook_late(kstr_funcs
[i
], &match_user_copy
, INT_PTR(2));
1402 add_function_hook("usb_control_msg", &match_user_copy
, INT_PTR(6));
1403 add_function_hook("kvm_read_guest_virt", &match_user_copy
, INT_PTR(2));
1405 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++)
1406 add_function_hook(returns_user_data
[i
], &match_returns_user_rl
, NULL
);
1408 add_function_hook("sscanf", &match_sscanf
, NULL
);
1410 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
1412 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1413 select_return_states_hook(PARAM_SET
, &db_param_set
);
1414 add_hook(&match_condition
, CONDITION_HOOK
);
1416 add_caller_info_callback(my_id
, caller_info_callback
);
1417 add_return_info_callback(my_id
, return_info_callback
);
1418 select_caller_info_hook(set_param_user_data
, USER_DATA
);
1419 select_return_states_hook(USER_DATA
, &returns_param_user_data
);
1420 select_return_states_hook(USER_DATA_SET
, &returns_param_user_data_set
);
1421 select_return_states_hook(CAPPED_DATA
, &returns_param_capped
);
1424 void register_kernel_user_data2(int id
)
1428 if (option_project
!= PROJ_KERNEL
)
1430 select_caller_info_hook(set_called
, INTERNAL
);