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 struct stree
*start_states
;
52 static struct stree_stack
*saved_stack
;
53 static void save_start_states(struct statement
*stmt
)
55 start_states
= clone_stree(__get_cur_stree());
58 static void free_start_states(void)
60 free_stree(&start_states
);
63 static void match_save_states(struct expression
*expr
)
65 push_stree(&saved_stack
, start_states
);
69 static void match_restore_states(struct expression
*expr
)
71 free_stree(&start_states
);
72 start_states
= pop_stree(&saved_stack
);
75 static struct smatch_state
*empty_state(struct sm_state
*sm
)
77 return alloc_estate_empty();
80 static struct smatch_state
*new_state(struct symbol
*type
)
82 struct smatch_state
*state
;
84 if (!type
|| type_is_ptr(type
))
87 state
= alloc_estate_whole(type
);
88 estate_set_new(state
);
92 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
94 struct smatch_state
*user
= cur
->state
;
95 struct smatch_state
*extra
;
96 struct smatch_state
*state
;
97 struct range_list
*rl
;
99 extra
= __get_state(SMATCH_EXTRA
, cur
->name
, cur
->sym
);
102 rl
= rl_intersection(estate_rl(user
), estate_rl(extra
));
103 state
= alloc_estate_rl(clone_rl(rl
));
104 if (estate_capped(user
) || is_capped_var_sym(cur
->name
, cur
->sym
))
105 estate_set_capped(state
);
106 if (estate_treat_untagged(user
))
107 estate_set_treat_untagged(state
);
108 if (estates_equiv(state
, cur
->state
))
110 set_state(my_id
, cur
->name
, cur
->sym
, state
);
113 static void extra_nomod_hook(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
115 struct smatch_state
*user
, *new;
116 struct range_list
*rl
;
118 user
= __get_state(my_id
, name
, sym
);
121 rl
= rl_intersection(estate_rl(user
), estate_rl(state
));
122 if (rl_equiv(rl
, estate_rl(user
)))
124 new = alloc_estate_rl(rl
);
125 if (estate_capped(user
))
126 estate_set_capped(new);
127 if (estate_treat_untagged(user
))
128 estate_set_treat_untagged(new);
129 set_state(my_id
, name
, sym
, new);
132 static bool user_rl_known(struct expression
*expr
)
134 struct range_list
*rl
;
137 if (!get_user_rl(expr
, &rl
))
140 close_to_max
= sval_type_max(rl_type(rl
));
141 close_to_max
.value
-= 100;
143 if (sval_cmp(rl_max(rl
), close_to_max
) >= 0)
148 static bool is_array_index_mask_nospec(struct expression
*expr
)
150 struct expression
*orig
;
152 orig
= get_assigned_expr(expr
);
153 if (!orig
|| orig
->type
!= EXPR_CALL
)
155 return sym_name_is("array_index_mask_nospec", orig
->fn
);
158 static bool binop_capped(struct expression
*expr
)
160 struct range_list
*left_rl
;
164 if (expr
->op
== '-' && get_user_rl(expr
->left
, &left_rl
)) {
165 if (user_rl_capped(expr
->left
))
167 comparison
= get_comparison(expr
->left
, expr
->right
);
168 if (comparison
&& show_special(comparison
)[0] == '>')
173 if (expr
->op
== '&' || expr
->op
== '%') {
174 bool left_user
, left_capped
, right_user
, right_capped
;
176 if (!get_value(expr
->right
, &sval
) && is_capped(expr
->right
))
178 if (is_array_index_mask_nospec(expr
->right
))
180 if (is_capped(expr
->left
))
182 left_user
= is_user_rl(expr
->left
);
183 right_user
= is_user_rl(expr
->right
);
184 if (!left_user
&& !right_user
)
187 left_capped
= user_rl_capped(expr
->left
);
188 right_capped
= user_rl_capped(expr
->right
);
190 if (left_user
&& left_capped
) {
193 if (right_user
&& right_capped
)
197 if (right_user
&& right_capped
) {
206 * Generally "capped" means that we capped it to an unknown value.
207 * This is useful because if Smatch doesn't know what the value is then
208 * we have to trust that it is correct. But if we known cap value is
209 * 100 then we can check if 100 is correct and complain if it's wrong.
211 * So then the problem is with BINOP when we take a capped variable
212 * plus a user variable which is clamped to a known range (uncapped)
213 * the result should be capped.
215 if ((user_rl_capped(expr
->left
) || user_rl_known(expr
->left
)) &&
216 (user_rl_capped(expr
->right
) || user_rl_known(expr
->right
)))
222 bool user_rl_capped(struct expression
*expr
)
224 struct smatch_state
*state
;
225 struct range_list
*rl
;
228 expr
= strip_expr(expr
);
231 if (get_value(expr
, &sval
))
233 if (expr
->type
== EXPR_BINOP
)
234 return binop_capped(expr
);
235 if ((expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) &&
236 (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
))
237 return user_rl_capped(expr
->unop
);
238 state
= get_state_expr(my_id
, expr
);
240 return estate_capped(state
);
242 if (!get_user_rl(expr
, &rl
)) {
244 * The non user data parts of a binop are capped and
245 * also empty user rl states are capped.
250 if (rl_to_sval(rl
, &sval
))
253 return false; /* uncapped user data */
256 bool user_rl_treat_untagged(struct expression
*expr
)
258 struct smatch_state
*state
;
259 struct range_list
*rl
;
262 expr
= strip_expr(expr
);
265 if (get_value(expr
, &sval
))
268 state
= get_state_expr(my_id
, expr
);
270 return estate_treat_untagged(state
);
272 if (get_user_rl(expr
, &rl
))
273 return false; /* uncapped user data */
275 return true; /* not actually user data */
278 static void tag_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
280 struct expression
*edge_member
;
281 struct symbol
*base
= get_real_base_type(member
);
285 expr
= member_expression(expr
, '.', member
->ident
);
287 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
290 type
= get_real_base_type(tmp
);
294 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
295 tag_inner_struct_members(expr
, tmp
);
302 edge_member
= member_expression(expr
, '.', tmp
->ident
);
303 set_state_expr(my_id
, edge_member
, new_state(type
));
304 } END_FOR_EACH_PTR(tmp
);
307 void __set_user_string(struct expression
*expr
);
308 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
311 struct expression
*member
;
314 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
315 expr
= strip_expr(expr
->unop
);
319 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
320 type
= get_real_base_type(tmp
);
324 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
325 tag_inner_struct_members(expr
, tmp
);
332 member
= member_expression(expr
, op
, tmp
->ident
);
333 if (type
->type
== SYM_ARRAY
) {
334 set_points_to_user_data(member
);
336 set_state_expr(my_id
, member
, new_state(get_type(member
)));
338 } END_FOR_EACH_PTR(tmp
);
341 static void tag_base_type(struct expression
*expr
)
343 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
344 expr
= strip_expr(expr
->unop
);
346 expr
= deref_expression(expr
);
347 set_state_expr(my_id
, expr
, new_state(get_type(expr
)));
350 static void tag_as_user_data(struct expression
*expr
)
354 expr
= strip_expr(expr
);
356 type
= get_type(expr
);
357 if (!type
|| type
->type
!= SYM_PTR
)
359 type
= get_real_base_type(type
);
362 if (type
== &void_ctype
) {
363 set_state_expr(my_id
, deref_expression(expr
), new_state(&ulong_ctype
));
366 if (type
->type
== SYM_BASETYPE
) {
367 if (expr
->type
!= EXPR_PREOP
&& expr
->op
!= '&')
368 set_points_to_user_data(expr
);
372 if (type
->type
== SYM_STRUCT
|| type
->type
== SYM_UNION
) {
373 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
374 expr
= deref_expression(expr
);
376 set_state_expr(my_id
, deref_expression(expr
), new_state(&ulong_ctype
));
377 tag_struct_members(type
, expr
);
381 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
383 int param
= PTR_INT(_param
);
384 struct expression
*dest
;
386 func_gets_user_data
= true;
388 dest
= get_argument_from_call_expr(expr
->args
, param
);
389 dest
= strip_expr(dest
);
392 tag_as_user_data(dest
);
395 static int is_dev_attr_name(struct expression
*expr
)
400 name
= expr_to_str(expr
);
403 if (strstr(name
, "->attr.name"))
409 static int ends_in_n(struct expression
*expr
)
415 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
422 if (str
->data
[str
->length
- 3] == '%' &&
423 str
->data
[str
->length
- 2] == 'n')
428 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
430 struct expression
*str
, *format
, *arg
;
433 func_gets_user_data
= true;
435 str
= get_argument_from_call_expr(expr
->args
, 0);
436 if (is_dev_attr_name(str
))
439 format
= get_argument_from_call_expr(expr
->args
, 1);
440 if (is_dev_attr_name(format
))
443 last
= ptr_list_size((struct ptr_list
*)expr
->args
) - 1;
446 FOR_EACH_PTR(expr
->args
, arg
) {
450 if (i
== last
&& ends_in_n(format
))
452 tag_as_user_data(arg
);
453 } END_FOR_EACH_PTR(arg
);
456 static int get_rl_from_function(struct expression
*expr
, struct range_list
**rl
)
460 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
||
461 !expr
->fn
->symbol_name
|| !expr
->fn
->symbol_name
->name
)
464 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++) {
465 if (strcmp(expr
->fn
->symbol_name
->name
, returns_user_data
[i
]) == 0) {
466 *rl
= alloc_whole_rl(get_type(expr
));
473 static int comes_from_skb_data(struct expression
*expr
)
475 expr
= strip_expr(expr
);
476 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
479 expr
= strip_expr(expr
->unop
);
482 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
483 expr
= strip_expr(expr
->left
);
485 return is_skb_data(expr
);
488 static int handle_get_user(struct expression
*expr
)
493 name
= get_macro_name(expr
->pos
);
494 if (!name
|| strcmp(name
, "get_user") != 0)
497 name
= expr_to_var(expr
->right
);
498 if (!name
|| (strcmp(name
, "__val_gu") != 0 && strcmp(name
, "__gu_val") != 0))
500 set_state_expr(my_id
, expr
->left
, new_state(get_type(expr
->left
)));
507 static bool state_is_new(struct expression
*expr
)
509 struct smatch_state
*state
;
511 state
= get_state_expr(my_id
, expr
);
512 if (estate_new(state
))
515 if (expr
->type
== EXPR_BINOP
) {
516 if (state_is_new(expr
->left
))
518 if (state_is_new(expr
->right
))
524 static bool handle_op_assign(struct expression
*expr
)
526 struct expression
*binop_expr
;
527 struct smatch_state
*state
;
528 struct range_list
*rl
;
531 case SPECIAL_ADD_ASSIGN
:
532 case SPECIAL_SUB_ASSIGN
:
533 case SPECIAL_AND_ASSIGN
:
534 case SPECIAL_MOD_ASSIGN
:
535 case SPECIAL_SHL_ASSIGN
:
536 case SPECIAL_SHR_ASSIGN
:
537 case SPECIAL_OR_ASSIGN
:
538 case SPECIAL_XOR_ASSIGN
:
539 case SPECIAL_MUL_ASSIGN
:
540 case SPECIAL_DIV_ASSIGN
:
541 binop_expr
= binop_expression(expr
->left
,
542 op_remove_assign(expr
->op
),
544 if (!get_user_rl(binop_expr
, &rl
))
547 rl
= cast_rl(get_type(expr
->left
), rl
);
548 state
= alloc_estate_rl(rl
);
549 if (user_rl_capped(binop_expr
))
550 estate_set_capped(state
);
551 if (user_rl_treat_untagged(expr
->left
))
552 estate_set_treat_untagged(state
);
553 if (state_is_new(binop_expr
))
554 estate_set_new(state
);
555 set_state_expr(my_id
, expr
->left
, state
);
561 static void match_assign(struct expression
*expr
)
563 struct symbol
*left_type
, *right_type
;
564 struct range_list
*rl
;
565 static struct expression
*handled
;
566 struct smatch_state
*state
;
567 struct expression
*faked
;
568 bool is_capped
= false;
571 left_type
= get_type(expr
->left
);
572 if (left_type
== &void_ctype
)
575 faked
= get_faked_expression();
577 /* FIXME: handle fake array assignments frob(&user_array[x]); */
579 if (is_fake_call(expr
->right
) && faked
&&
580 faked
->type
== EXPR_ASSIGNMENT
&&
581 points_to_user_data(faked
->right
)) {
582 if (is_skb_data(faked
->right
))
583 func_gets_user_data
= true;
584 rl
= alloc_whole_rl(get_type(expr
->left
));
589 if (faked
&& faked
== handled
)
591 if (is_fake_call(expr
->right
))
592 goto clear_old_state
;
593 if (handle_get_user(expr
))
595 if (points_to_user_data(expr
->right
) &&
596 is_struct_ptr(get_type(expr
->left
))) {
598 // This should be handled by smatch_points_to_user_data.c
599 // set_points_to_user_data(expr->left);
602 if (handle_op_assign(expr
))
605 goto clear_old_state
;
607 /* Handled by DB code */
608 if (expr
->right
->type
== EXPR_CALL
|| __in_fake_parameter_assign
)
611 if (!get_user_rl(expr
->right
, &rl
))
612 goto clear_old_state
;
613 is_capped
= user_rl_capped(expr
->right
);
614 is_new
= state_is_new(expr
->right
);
617 if (type_is_ptr(left_type
)) {
618 right_type
= get_type(expr
->right
);
619 if (right_type
&& right_type
->type
== SYM_ARRAY
)
620 set_points_to_user_data(expr
->left
);
624 rl
= cast_rl(left_type
, rl
);
625 state
= alloc_estate_rl(rl
);
627 estate_set_new(state
);
629 estate_set_capped(state
);
630 if (user_rl_treat_untagged(expr
->right
))
631 estate_set_treat_untagged(state
);
634 sm_msg("%s: left = '%s' user_state = '%s'", __func__
, expr_to_str(expr
->left
), state
->name
);
635 set_state_expr(my_id
, expr
->left
, state
);
642 * HACK ALERT!!! This should be at the start of the function. The
643 * the problem is that handling "pointer = array;" assignments is
644 * handled in this function instead of in kernel_points_to_user_data.c.
646 if (type_is_ptr(left_type
))
649 if (get_state_expr(my_id
, expr
->left
))
650 set_state_expr(my_id
, expr
->left
, alloc_estate_empty());
653 static void handle_eq_noteq(struct expression
*expr
)
655 struct smatch_state
*left_orig
, *right_orig
;
657 left_orig
= get_state_expr(my_id
, expr
->left
);
658 right_orig
= get_state_expr(my_id
, expr
->right
);
660 if (!left_orig
&& !right_orig
)
662 if (left_orig
&& right_orig
)
666 set_true_false_states_expr(my_id
, expr
->left
,
667 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
668 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
670 set_true_false_states_expr(my_id
, expr
->right
,
671 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
672 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
676 static struct range_list
*strip_negatives(struct range_list
*rl
)
678 sval_t min
= rl_min(rl
);
679 sval_t minus_one
= { .type
= rl_type(rl
), .value
= -1 };
680 sval_t over
= { .type
= rl_type(rl
), .value
= INT_MAX
+ 1ULL };
681 sval_t max
= sval_type_max(rl_type(rl
));
686 if (type_unsigned(rl_type(rl
)) && type_bits(rl_type(rl
)) > 31)
687 return remove_range(rl
, over
, max
);
689 return remove_range(rl
, min
, minus_one
);
692 static void handle_compare(struct expression
*expr
)
694 struct expression
*left
, *right
;
695 struct range_list
*left_rl
= NULL
;
696 struct range_list
*right_rl
= NULL
;
697 struct range_list
*user_rl
;
698 struct smatch_state
*capped_state
;
699 struct smatch_state
*left_true
= NULL
;
700 struct smatch_state
*left_false
= NULL
;
701 struct smatch_state
*right_true
= NULL
;
702 struct smatch_state
*right_false
= NULL
;
706 left
= strip_expr(expr
->left
);
707 right
= strip_expr(expr
->right
);
709 while (left
->type
== EXPR_ASSIGNMENT
)
710 left
= strip_expr(left
->left
);
713 * Conditions are mostly handled by smatch_extra.c, but there are some
714 * times where the exact values are not known so we can't do that.
716 * Normally, we might consider using smatch_capped.c to supliment smatch
717 * extra but that doesn't work when we merge unknown uncapped kernel
718 * data with unknown capped user data. The result is uncapped user
719 * data. We need to keep it separate and say that the user data is
720 * capped. In the past, I would have marked this as just regular
721 * kernel data (not user data) but we can't do that these days because
722 * we need to track user data for Spectre.
724 * The other situation which we have to handle is when we do have an
725 * int and we compare against an unknown unsigned kernel variable. In
726 * that situation we assume that the kernel data is less than INT_MAX.
727 * Otherwise then we get all sorts of array underflow false positives.
731 /* Handled in smatch_extra.c */
732 if (get_implied_value(left
, &sval
) ||
733 get_implied_value(right
, &sval
))
736 get_user_rl(left
, &left_rl
);
737 get_user_rl(right
, &right_rl
);
740 if (!left_rl
&& !right_rl
)
742 /* if both sides are user data that's not a good limit */
743 if (left_rl
&& right_rl
)
751 type
= get_type(expr
);
752 if (type_unsigned(type
))
753 user_rl
= strip_negatives(user_rl
);
754 capped_state
= alloc_estate_rl(user_rl
);
755 estate_set_capped(capped_state
);
759 case SPECIAL_UNSIGNED_LT
:
761 case SPECIAL_UNSIGNED_LTE
:
763 left_true
= capped_state
;
765 right_false
= capped_state
;
768 case SPECIAL_UNSIGNED_GT
:
770 case SPECIAL_UNSIGNED_GTE
:
772 left_false
= capped_state
;
774 right_true
= capped_state
;
778 set_true_false_states_expr(my_id
, left
, left_true
, left_false
);
779 set_true_false_states_expr(my_id
, right
, right_true
, right_false
);
782 static void match_condition(struct expression
*expr
)
784 if (expr
->type
!= EXPR_COMPARE
)
787 if (expr
->op
== SPECIAL_EQUAL
||
788 expr
->op
== SPECIAL_NOTEQUAL
) {
789 handle_eq_noteq(expr
);
793 handle_compare(expr
);
796 static void match_returns_user_rl(const char *fn
, struct expression
*expr
, void *unused
)
798 func_gets_user_data
= true;
801 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
803 struct expression
*parent
;
809 macro
= get_macro_name(expr
->pos
);
813 /* handle ntohl(foo[i]) where "i" is trusted */
814 parent
= expr_get_parent_expr(expr
);
815 while (parent
&& parent
->type
!= EXPR_BINOP
)
816 parent
= expr_get_parent_expr(parent
);
817 if (parent
&& parent
->type
== EXPR_BINOP
) {
818 char *parent_macro
= get_macro_name(parent
->pos
);
820 if (parent_macro
&& strcmp(macro
, parent_macro
) == 0)
824 if (strcmp(macro
, "ntohl") == 0) {
825 *rl
= alloc_whole_rl(&uint_ctype
);
828 if (strcmp(macro
, "ntohs") == 0) {
829 *rl
= alloc_whole_rl(&ushort_ctype
);
835 static int has_user_data(struct symbol
*sym
)
837 struct sm_state
*tmp
;
839 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), tmp
) {
842 } END_FOR_EACH_SM(tmp
);
846 bool we_pass_user_data(struct expression
*call
)
848 struct expression
*arg
;
851 FOR_EACH_PTR(call
->args
, arg
) {
853 sm_msg("%s: arg = '%s' %s", __func__
,
855 points_to_user_data(arg
) ? "user pointer" : "not");
856 if (points_to_user_data(arg
))
858 sym
= expr_to_sym(arg
);
861 if (has_user_data(sym
))
863 } END_FOR_EACH_PTR(arg
);
868 static int db_returned_user_rl(struct expression
*call
, struct range_list
**rl
)
870 struct smatch_state
*state
;
873 if (is_fake_call(call
))
875 snprintf(buf
, sizeof(buf
), "return %p", call
);
876 state
= get_state(my_id
, buf
, NULL
);
877 if (!state
|| !estate_rl(state
))
879 *rl
= estate_rl(state
);
883 struct stree
*get_user_stree(void)
885 return get_all_states_stree(my_id
);
888 static int user_data_flag
;
889 static int no_user_data_flag
;
890 struct range_list
*var_user_rl(struct expression
*expr
)
892 struct smatch_state
*state
;
893 struct range_list
*rl
;
894 struct range_list
*absolute_rl
;
896 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
897 no_user_data_flag
= 1;
901 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
902 struct range_list
*left
, *right
;
904 if (!get_user_rl(expr
->right
, &right
))
906 get_absolute_rl(expr
->left
, &left
);
907 rl
= rl_binop(left
, '%', right
);
911 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '/') {
912 struct range_list
*left
= NULL
;
913 struct range_list
*right
= NULL
;
914 struct range_list
*abs_right
;
917 * The specific bug I'm dealing with is:
919 * foo = capped_user / unknown;
921 * Instead of just saying foo is now entirely user_rl we should
922 * probably say instead that it is not at all user data.
926 get_user_rl(expr
->left
, &left
);
927 get_user_rl(expr
->right
, &right
);
928 get_absolute_rl(expr
->right
, &abs_right
);
930 if (left
&& !right
) {
931 rl
= rl_binop(left
, '/', abs_right
);
932 if (sval_cmp(rl_max(left
), rl_max(rl
)) < 0)
933 no_user_data_flag
= 1;
939 if (get_rl_from_function(expr
, &rl
))
942 if (get_user_macro_rl(expr
, &rl
))
945 if (comes_from_skb_data(expr
)) {
946 rl
= alloc_whole_rl(get_type(expr
));
950 state
= get_state_expr(my_id
, expr
);
951 if (state
&& estate_rl(state
)) {
952 rl
= estate_rl(state
);
956 if (expr
->type
== EXPR_CALL
&& db_returned_user_rl(expr
, &rl
))
959 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*' &&
960 points_to_user_data(expr
->unop
)) {
961 rl
= var_to_absolute_rl(expr
);
965 if (is_array(expr
)) {
966 struct expression
*array
= get_array_base(expr
);
968 if (!get_state_expr(my_id
, array
)) {
969 no_user_data_flag
= 1;
977 absolute_rl
= var_to_absolute_rl(expr
);
978 return clone_rl(rl_intersection(rl
, absolute_rl
));
981 static bool is_ptr_subtract(struct expression
*expr
)
983 expr
= strip_expr(expr
);
986 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '-' &&
987 type_is_ptr(get_type(expr
->left
))) {
993 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
995 if (is_ptr_subtract(expr
))
999 no_user_data_flag
= 0;
1000 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
1001 if (!user_data_flag
|| no_user_data_flag
)
1007 int is_user_rl(struct expression
*expr
)
1009 struct range_list
*tmp
;
1011 return get_user_rl(expr
, &tmp
) && tmp
;
1014 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
1016 struct smatch_state
*state
;
1018 state
= get_state(my_id
, name
, sym
);
1019 if (state
&& estate_rl(state
)) {
1020 *rl
= estate_rl(state
);
1026 static void return_info_callback(int return_id
, char *return_ranges
,
1027 struct expression
*returned_expr
,
1029 const char *printed_name
,
1030 struct sm_state
*sm
)
1032 struct smatch_state
*extra
;
1033 struct range_list
*rl
;
1037 if (strcmp(printed_name
, "$") == 0)
1039 if (!param_was_set_var_sym(sm
->name
, sm
->sym
))
1042 rl
= estate_rl(sm
->state
);
1045 extra
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1046 if (estate_rl(extra
))
1047 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(extra
));
1051 snprintf(buf
, sizeof(buf
), "%s%s%s",
1053 estate_capped(sm
->state
) ? "[c]" : "",
1054 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1055 sql_insert_return_states(return_id
, return_ranges
,
1056 estate_new(sm
->state
) ? USER_DATA_SET
: USER_DATA
,
1057 param
, printed_name
, buf
);
1060 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1062 struct smatch_state
*state
;
1063 struct range_list
*rl
;
1064 struct symbol
*type
;
1068 sm_msg("%s: name = '%s' sm = '%s'", __func__
, printed_name
, show_sm(sm
));
1071 * Smatch uses a hack where if we get an unsigned long we say it's
1072 * both user data and it points to user data. But if we pass it to a
1073 * function which takes an int, then it's just user data. There's not
1074 * enough bytes for it to be a pointer.
1077 type
= get_arg_type(call
->fn
, param
);
1078 if (strcmp(printed_name
, "$") != 0 && type
&& type_bits(type
) < type_bits(&ptr_ctype
))
1081 if (strcmp(sm
->state
->name
, "") == 0)
1084 state
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1085 if (!state
|| !estate_rl(state
))
1086 rl
= estate_rl(sm
->state
);
1088 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
1093 snprintf(buf
, sizeof(buf
), "%s%s%s", show_rl(rl
),
1094 estate_capped(sm
->state
) ? "[c]" : "",
1095 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1096 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, buf
);
1099 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1101 struct expression
*arg
;
1104 struct smatch_state
*state
;
1106 while (expr
->type
== EXPR_ASSIGNMENT
)
1107 expr
= strip_expr(expr
->right
);
1108 if (expr
->type
!= EXPR_CALL
)
1111 arg
= get_argument_from_call_expr(expr
->args
, param
);
1114 name
= get_variable_from_key(arg
, key
, &sym
);
1118 state
= get_state(my_id
, name
, sym
);
1122 set_state(my_id
, name
, sym
, alloc_estate_empty());
1127 static bool param_data_capped(const char *value
)
1129 if (strstr(value
, ",c") || strstr(value
, "[c"))
1134 static bool param_data_treat_untagged(const char *value
)
1136 if (strstr(value
, ",u") || strstr(value
, "[u"))
1141 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1143 struct expression
*expr
;
1144 struct range_list
*rl
= NULL
;
1145 struct smatch_state
*state
;
1146 struct symbol
*type
;
1149 expr
= symbol_expression(sym
);
1150 fullname
= get_variable_from_key(expr
, key
, NULL
);
1154 type
= get_member_type_from_key(expr
, key
);
1155 if (type
&& type
->type
== SYM_STRUCT
) {
1156 sm_info("%s: user data struct. key='%s' value='%s'",
1157 __func__
, key
, value
);
1161 // FIXME: This is temporary. Just run this on Thursday and then
1162 // let's make it a printf() and then delete it.
1164 sm_msg("%s: no type for '%s'", __func__
, fullname
);
1168 str_to_rl(type
, value
, &rl
);
1169 rl
= swap_mtag_seed(expr
, rl
);
1170 state
= alloc_estate_rl(rl
);
1171 if (param_data_capped(value
) || is_capped(expr
))
1172 estate_set_capped(state
);
1173 if (param_data_treat_untagged(value
) || sym
->ctype
.as
== 5)
1174 estate_set_treat_untagged(state
);
1175 set_state(my_id
, fullname
, sym
, state
);
1178 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1180 set_state(my_call_id
, "this_function", NULL
, &called
);
1183 static void match_syscall_definition(struct symbol
*sym
)
1190 macro
= get_macro_name(sym
->pos
);
1192 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
1193 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1196 name
= get_function();
1197 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
1198 if (name
&& strncmp(name
, "sys_", 4) == 0)
1202 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
1208 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
1209 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
1210 } END_FOR_EACH_PTR(arg
);
1216 static void store_user_data_return(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1218 struct smatch_state
*state
;
1219 struct range_list
*rl
;
1220 struct symbol
*type
;
1226 type
= get_type(expr
);
1227 snprintf(buf
, sizeof(buf
), "return %p%s", expr
, key
+ 1);
1228 call_results_to_rl(expr
, type
, value
, &rl
);
1230 state
= alloc_estate_rl(rl
);
1232 estate_set_new(state
);
1234 set_state(my_id
, buf
, NULL
, state
);
1237 static void set_to_user_data(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1239 struct smatch_state
*state
;
1242 struct symbol
*type
;
1243 struct range_list
*rl
= NULL
;
1245 type
= get_member_type_from_key(expr
, key
);
1246 name
= get_variable_from_key(expr
, key
, &sym
);
1250 call_results_to_rl(expr
, type
, value
, &rl
);
1252 state
= alloc_estate_rl(rl
);
1253 if (param_data_capped(value
))
1254 estate_set_capped(state
);
1255 if (param_data_treat_untagged(value
))
1256 estate_set_treat_untagged(state
);
1258 estate_set_new(state
);
1259 set_state(my_id
, name
, sym
, state
);
1264 static void returns_param_user_data(struct expression
*expr
, int param
, char *key
, char *value
)
1266 struct expression
*arg
;
1267 struct expression
*call
;
1270 while (call
->type
== EXPR_ASSIGNMENT
)
1271 call
= strip_expr(call
->right
);
1272 if (call
->type
!= EXPR_CALL
)
1275 if (!we_pass_user_data(call
))
1279 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1280 store_user_data_return(expr
, key
, value
, OLD
);
1283 set_to_user_data(expr
->left
, key
, value
, OLD
);
1287 arg
= get_argument_from_call_expr(call
->args
, param
);
1290 set_to_user_data(arg
, key
, value
, OLD
);
1293 static void returns_param_user_data_set(struct expression
*expr
, int param
, char *key
, char *value
)
1295 struct expression
*arg
;
1297 func_gets_user_data
= true;
1300 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1301 store_user_data_return(expr
, key
, value
, NEW
);
1304 set_to_user_data(expr
->left
, key
, value
, NEW
);
1308 while (expr
->type
== EXPR_ASSIGNMENT
)
1309 expr
= strip_expr(expr
->right
);
1310 if (expr
->type
!= EXPR_CALL
)
1313 arg
= get_argument_from_call_expr(expr
->args
, param
);
1316 set_to_user_data(arg
, key
, value
, NEW
);
1319 static void returns_param_capped(struct expression
*expr
, int param
, char *key
, char *value
)
1321 struct smatch_state
*state
, *new;
1325 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1329 state
= get_state(my_id
, name
, sym
);
1330 if (!state
|| estate_capped(state
))
1333 new = clone_estate(state
);
1334 estate_set_capped(new);
1336 set_state(my_id
, name
, sym
, new);
1341 static struct int_stack
*gets_data_stack
;
1342 static void match_function_def(struct symbol
*sym
)
1344 func_gets_user_data
= false;
1346 if (is_user_data_fn(sym
))
1347 func_gets_user_data
= true;
1350 static void match_inline_start(struct expression
*expr
)
1352 push_int(&gets_data_stack
, func_gets_user_data
);
1355 static void match_inline_end(struct expression
*expr
)
1357 func_gets_user_data
= pop_int(&gets_data_stack
);
1360 void register_kernel_user_data(int id
)
1366 if (option_project
!= PROJ_KERNEL
)
1369 set_dynamic_states(my_id
);
1371 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1372 add_hook(&match_inline_start
, INLINE_FN_START
);
1373 add_hook(&match_inline_end
, INLINE_FN_END
);
1375 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
1376 add_hook(&free_start_states
, AFTER_FUNC_HOOK
);
1377 add_hook(&match_save_states
, INLINE_FN_START
);
1378 add_hook(&match_restore_states
, INLINE_FN_END
);
1380 add_unmatched_state_hook(my_id
, &empty_state
);
1381 add_extra_nomod_hook(&extra_nomod_hook
);
1382 add_pre_merge_hook(my_id
, &pre_merge_hook
);
1383 add_merge_hook(my_id
, &merge_estates
);
1385 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
1386 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
1387 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
1388 for (i
= 0; i
< ARRAY_SIZE(kstr_funcs
); i
++)
1389 add_function_hook(kstr_funcs
[i
], &match_user_copy
, INT_PTR(2));
1390 add_function_hook("usb_control_msg", &match_user_copy
, INT_PTR(6));
1392 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++)
1393 add_function_hook(returns_user_data
[i
], &match_returns_user_rl
, NULL
);
1395 add_function_hook("sscanf", &match_sscanf
, NULL
);
1397 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
1399 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1400 select_return_states_hook(PARAM_SET
, &db_param_set
);
1401 add_hook(&match_condition
, CONDITION_HOOK
);
1403 add_caller_info_callback(my_id
, caller_info_callback
);
1404 add_return_info_callback(my_id
, return_info_callback
);
1405 select_caller_info_hook(set_param_user_data
, USER_DATA
);
1406 select_return_states_hook(USER_DATA
, &returns_param_user_data
);
1407 select_return_states_hook(USER_DATA_SET
, &returns_param_user_data_set
);
1408 select_return_states_hook(CAPPED_DATA
, &returns_param_capped
);
1411 void register_kernel_user_data2(int id
)
1415 if (option_project
!= PROJ_KERNEL
)
1417 select_caller_info_hook(set_called
, INTERNAL
);