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 bool user_rl_known(struct expression
*expr
)
121 struct range_list
*rl
;
124 if (!get_user_rl(expr
, &rl
))
127 close_to_max
= sval_type_max(rl_type(rl
));
128 close_to_max
.value
-= 100;
130 if (sval_cmp(rl_max(rl
), close_to_max
) >= 0)
135 static bool is_array_index_mask_nospec(struct expression
*expr
)
137 struct expression
*orig
;
139 orig
= get_assigned_expr(expr
);
140 if (!orig
|| orig
->type
!= EXPR_CALL
)
142 return sym_name_is("array_index_mask_nospec", orig
->fn
);
145 static bool binop_capped(struct expression
*expr
)
147 struct range_list
*left_rl
;
151 if (expr
->op
== '-' && get_user_rl(expr
->left
, &left_rl
)) {
152 if (user_rl_capped(expr
->left
))
154 comparison
= get_comparison(expr
->left
, expr
->right
);
155 if (comparison
&& show_special(comparison
)[0] == '>')
160 if (expr
->op
== '&' || expr
->op
== '%') {
161 bool left_user
, left_capped
, right_user
, right_capped
;
163 if (!get_value(expr
->right
, &sval
) && is_capped(expr
->right
))
165 if (is_array_index_mask_nospec(expr
->right
))
167 if (is_capped(expr
->left
))
169 left_user
= is_user_rl(expr
->left
);
170 right_user
= is_user_rl(expr
->right
);
171 if (!left_user
&& !right_user
)
174 left_capped
= user_rl_capped(expr
->left
);
175 right_capped
= user_rl_capped(expr
->right
);
177 if (left_user
&& left_capped
) {
180 if (right_user
&& right_capped
)
184 if (right_user
&& right_capped
) {
193 * Generally "capped" means that we capped it to an unknown value.
194 * This is useful because if Smatch doesn't know what the value is then
195 * we have to trust that it is correct. But if we known cap value is
196 * 100 then we can check if 100 is correct and complain if it's wrong.
198 * So then the problem is with BINOP when we take a capped variable
199 * plus a user variable which is clamped to a known range (uncapped)
200 * the result should be capped.
202 if ((user_rl_capped(expr
->left
) || user_rl_known(expr
->left
)) &&
203 (user_rl_capped(expr
->right
) || user_rl_known(expr
->right
)))
209 bool user_rl_capped(struct expression
*expr
)
211 struct smatch_state
*state
;
212 struct range_list
*rl
;
215 expr
= strip_expr(expr
);
218 if (get_value(expr
, &sval
))
220 if (expr
->type
== EXPR_BINOP
)
221 return binop_capped(expr
);
222 if ((expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) &&
223 (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
))
224 return user_rl_capped(expr
->unop
);
225 state
= get_state_expr(my_id
, expr
);
227 return estate_capped(state
);
229 if (!get_user_rl(expr
, &rl
)) {
231 * The non user data parts of a binop are capped and
232 * also empty user rl states are capped.
237 if (rl_to_sval(rl
, &sval
))
240 return false; /* uncapped user data */
243 bool user_rl_treat_untagged(struct expression
*expr
)
245 struct smatch_state
*state
;
246 struct range_list
*rl
;
249 expr
= strip_expr(expr
);
252 if (get_value(expr
, &sval
))
255 state
= get_state_expr(my_id
, expr
);
257 return estate_treat_untagged(state
);
259 if (get_user_rl(expr
, &rl
))
260 return false; /* uncapped user data */
262 return true; /* not actually user data */
265 static void tag_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
267 struct expression
*edge_member
;
268 struct symbol
*base
= get_real_base_type(member
);
272 expr
= member_expression(expr
, '.', member
->ident
);
274 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
277 type
= get_real_base_type(tmp
);
281 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
282 tag_inner_struct_members(expr
, tmp
);
289 edge_member
= member_expression(expr
, '.', tmp
->ident
);
290 set_state_expr(my_id
, edge_member
, new_state(type
));
291 } END_FOR_EACH_PTR(tmp
);
294 void __set_user_string(struct expression
*expr
);
295 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
298 struct expression
*member
;
301 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
302 expr
= strip_expr(expr
->unop
);
306 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
307 type
= get_real_base_type(tmp
);
311 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
312 tag_inner_struct_members(expr
, tmp
);
319 member
= member_expression(expr
, op
, tmp
->ident
);
320 if (type
->type
== SYM_ARRAY
) {
321 set_points_to_user_data(member
);
323 set_state_expr(my_id
, member
, new_state(get_type(member
)));
325 } END_FOR_EACH_PTR(tmp
);
328 static void tag_base_type(struct expression
*expr
)
330 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
331 expr
= strip_expr(expr
->unop
);
333 expr
= deref_expression(expr
);
334 set_state_expr(my_id
, expr
, new_state(get_type(expr
)));
337 static void tag_as_user_data(struct expression
*expr
)
341 expr
= strip_expr(expr
);
343 type
= get_type(expr
);
344 if (!type
|| type
->type
!= SYM_PTR
)
346 type
= get_real_base_type(type
);
349 if (type
== &void_ctype
) {
350 set_state_expr(my_id
, deref_expression(expr
), new_state(&ulong_ctype
));
353 if (type
->type
== SYM_BASETYPE
) {
354 if (expr
->type
!= EXPR_PREOP
&& expr
->op
!= '&')
355 set_points_to_user_data(expr
);
359 if (type
->type
== SYM_STRUCT
|| type
->type
== SYM_UNION
) {
360 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
361 expr
= deref_expression(expr
);
363 set_state_expr(my_id
, deref_expression(expr
), new_state(&ulong_ctype
));
364 tag_struct_members(type
, expr
);
368 static struct expression
*ignore_param_set
;
369 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
371 int param
= PTR_INT(_param
);
372 struct expression
*dest
;
374 func_gets_user_data
= true;
375 ignore_param_set
= expr
;
377 dest
= get_argument_from_call_expr(expr
->args
, param
);
378 dest
= strip_expr(dest
);
381 tag_as_user_data(dest
);
384 static int is_dev_attr_name(struct expression
*expr
)
389 name
= expr_to_str(expr
);
392 if (strstr(name
, "->attr.name"))
398 static int ends_in_n(struct expression
*expr
)
404 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
411 if (str
->data
[str
->length
- 3] == '%' &&
412 str
->data
[str
->length
- 2] == 'n')
417 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
419 struct expression
*str
, *format
, *arg
;
422 func_gets_user_data
= true;
424 str
= get_argument_from_call_expr(expr
->args
, 0);
425 if (is_dev_attr_name(str
))
428 format
= get_argument_from_call_expr(expr
->args
, 1);
429 if (is_dev_attr_name(format
))
432 last
= ptr_list_size((struct ptr_list
*)expr
->args
) - 1;
435 FOR_EACH_PTR(expr
->args
, arg
) {
439 if (i
== last
&& ends_in_n(format
))
441 tag_as_user_data(arg
);
442 } END_FOR_EACH_PTR(arg
);
445 static int get_rl_from_function(struct expression
*expr
, struct range_list
**rl
)
449 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
||
450 !expr
->fn
->symbol_name
|| !expr
->fn
->symbol_name
->name
)
453 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++) {
454 if (strcmp(expr
->fn
->symbol_name
->name
, returns_user_data
[i
]) == 0) {
455 *rl
= alloc_whole_rl(get_type(expr
));
462 static int comes_from_skb_data(struct expression
*expr
)
464 expr
= strip_expr(expr
);
465 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
468 expr
= strip_expr(expr
->unop
);
471 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
472 expr
= strip_expr(expr
->left
);
474 return is_skb_data(expr
);
477 static int handle_get_user(struct expression
*expr
)
482 name
= get_macro_name(expr
->pos
);
483 if (!name
|| strcmp(name
, "get_user") != 0)
486 name
= expr_to_var(expr
->right
);
487 if (!name
|| (strcmp(name
, "__val_gu") != 0 && strcmp(name
, "__gu_val") != 0))
489 set_state_expr(my_id
, expr
->left
, new_state(get_type(expr
->left
)));
496 static bool state_is_new(struct expression
*expr
)
498 struct smatch_state
*state
;
500 state
= get_state_expr(my_id
, expr
);
501 if (estate_new(state
))
504 if (expr
->type
== EXPR_BINOP
) {
505 if (state_is_new(expr
->left
))
507 if (state_is_new(expr
->right
))
513 static bool handle_op_assign(struct expression
*expr
)
515 struct expression
*binop_expr
;
516 struct smatch_state
*state
;
517 struct range_list
*rl
;
520 case SPECIAL_ADD_ASSIGN
:
521 case SPECIAL_SUB_ASSIGN
:
522 case SPECIAL_AND_ASSIGN
:
523 case SPECIAL_MOD_ASSIGN
:
524 case SPECIAL_SHL_ASSIGN
:
525 case SPECIAL_SHR_ASSIGN
:
526 case SPECIAL_OR_ASSIGN
:
527 case SPECIAL_XOR_ASSIGN
:
528 case SPECIAL_MUL_ASSIGN
:
529 case SPECIAL_DIV_ASSIGN
:
530 binop_expr
= binop_expression(expr
->left
,
531 op_remove_assign(expr
->op
),
533 if (!get_user_rl(binop_expr
, &rl
))
536 rl
= cast_rl(get_type(expr
->left
), rl
);
537 state
= alloc_estate_rl(rl
);
538 if (expr
->op
== SPECIAL_AND_ASSIGN
||
539 expr
->op
== SPECIAL_MOD_ASSIGN
||
540 user_rl_capped(binop_expr
))
541 estate_set_capped(state
);
542 if (user_rl_treat_untagged(expr
->left
))
543 estate_set_treat_untagged(state
);
544 if (state_is_new(binop_expr
))
545 estate_set_new(state
);
546 set_state_expr(my_id
, expr
->left
, state
);
552 static void match_assign(struct expression
*expr
)
554 struct symbol
*left_type
, *right_type
;
555 struct range_list
*rl
;
556 static struct expression
*handled
;
557 struct smatch_state
*state
;
558 struct expression
*faked
;
559 bool is_capped
= false;
562 left_type
= get_type(expr
->left
);
563 if (left_type
== &void_ctype
)
566 faked
= get_faked_expression();
568 /* FIXME: handle fake array assignments frob(&user_array[x]); */
570 if (is_fake_call(expr
->right
) && faked
&&
571 faked
->type
== EXPR_ASSIGNMENT
&&
572 points_to_user_data(faked
->right
)) {
573 if (is_skb_data(faked
->right
))
574 func_gets_user_data
= true;
575 rl
= alloc_whole_rl(get_type(expr
->left
));
580 if (faked
&& faked
== handled
)
582 if (is_fake_call(expr
->right
))
583 goto clear_old_state
;
584 if (handle_get_user(expr
))
586 if (points_to_user_data(expr
->right
) &&
587 is_struct_ptr(get_type(expr
->left
))) {
589 // This should be handled by smatch_points_to_user_data.c
590 // set_points_to_user_data(expr->left);
593 if (handle_op_assign(expr
))
596 goto clear_old_state
;
598 /* Handled by DB code */
599 if (expr
->right
->type
== EXPR_CALL
|| __in_fake_parameter_assign
)
602 if (!get_user_rl(expr
->right
, &rl
))
603 goto clear_old_state
;
604 is_capped
= user_rl_capped(expr
->right
);
605 is_new
= state_is_new(expr
->right
);
608 if (type_is_ptr(left_type
)) {
609 right_type
= get_type(expr
->right
);
610 if (right_type
&& right_type
->type
== SYM_ARRAY
)
611 set_points_to_user_data(expr
->left
);
615 rl
= cast_rl(left_type
, rl
);
616 state
= alloc_estate_rl(rl
);
618 estate_set_new(state
);
620 estate_set_capped(state
);
621 if (user_rl_treat_untagged(expr
->right
))
622 estate_set_treat_untagged(state
);
624 set_state_expr(my_id
, expr
->left
, state
);
631 * HACK ALERT!!! This should be at the start of the function. The
632 * the problem is that handling "pointer = array;" assignments is
633 * handled in this function instead of in kernel_points_to_user_data.c.
635 if (type_is_ptr(left_type
))
638 if (get_state_expr(my_id
, expr
->left
))
639 set_state_expr(my_id
, expr
->left
, alloc_estate_empty());
642 static void handle_eq_noteq(struct expression
*expr
)
644 struct smatch_state
*left_orig
, *right_orig
;
646 left_orig
= get_state_expr(my_id
, expr
->left
);
647 right_orig
= get_state_expr(my_id
, expr
->right
);
649 if (!left_orig
&& !right_orig
)
651 if (left_orig
&& right_orig
)
655 set_true_false_states_expr(my_id
, expr
->left
,
656 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
657 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
659 set_true_false_states_expr(my_id
, expr
->right
,
660 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
661 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
665 static struct range_list
*strip_negatives(struct range_list
*rl
)
667 sval_t min
= rl_min(rl
);
668 sval_t minus_one
= { .type
= rl_type(rl
), .value
= -1 };
669 sval_t over
= { .type
= rl_type(rl
), .value
= INT_MAX
+ 1ULL };
670 sval_t max
= sval_type_max(rl_type(rl
));
675 if (type_unsigned(rl_type(rl
)) && type_bits(rl_type(rl
)) > 31)
676 return remove_range(rl
, over
, max
);
678 return remove_range(rl
, min
, minus_one
);
681 static void handle_compare(struct expression
*expr
)
683 struct expression
*left
, *right
;
684 struct range_list
*left_rl
= NULL
;
685 struct range_list
*right_rl
= NULL
;
686 struct range_list
*user_rl
;
687 struct smatch_state
*capped_state
;
688 struct smatch_state
*left_true
= NULL
;
689 struct smatch_state
*left_false
= NULL
;
690 struct smatch_state
*right_true
= NULL
;
691 struct smatch_state
*right_false
= NULL
;
695 left
= strip_expr(expr
->left
);
696 right
= strip_expr(expr
->right
);
698 while (left
->type
== EXPR_ASSIGNMENT
)
699 left
= strip_expr(left
->left
);
702 * Conditions are mostly handled by smatch_extra.c, but there are some
703 * times where the exact values are not known so we can't do that.
705 * Normally, we might consider using smatch_capped.c to supliment smatch
706 * extra but that doesn't work when we merge unknown uncapped kernel
707 * data with unknown capped user data. The result is uncapped user
708 * data. We need to keep it separate and say that the user data is
709 * capped. In the past, I would have marked this as just regular
710 * kernel data (not user data) but we can't do that these days because
711 * we need to track user data for Spectre.
713 * The other situation which we have to handle is when we do have an
714 * int and we compare against an unknown unsigned kernel variable. In
715 * that situation we assume that the kernel data is less than INT_MAX.
716 * Otherwise then we get all sorts of array underflow false positives.
720 /* Handled in smatch_extra.c */
721 if (get_implied_value(left
, &sval
) ||
722 get_implied_value(right
, &sval
))
725 get_user_rl(left
, &left_rl
);
726 get_user_rl(right
, &right_rl
);
729 if (!left_rl
&& !right_rl
)
731 /* if both sides are user data that's not a good limit */
732 if (left_rl
&& right_rl
)
740 type
= get_type(expr
);
741 if (type_unsigned(type
))
742 user_rl
= strip_negatives(user_rl
);
743 capped_state
= alloc_estate_rl(user_rl
);
744 estate_set_capped(capped_state
);
748 case SPECIAL_UNSIGNED_LT
:
750 case SPECIAL_UNSIGNED_LTE
:
752 left_true
= capped_state
;
754 right_false
= capped_state
;
757 case SPECIAL_UNSIGNED_GT
:
759 case SPECIAL_UNSIGNED_GTE
:
761 left_false
= capped_state
;
763 right_true
= capped_state
;
767 set_true_false_states_expr(my_id
, left
, left_true
, left_false
);
768 set_true_false_states_expr(my_id
, right
, right_true
, right_false
);
771 static void match_condition(struct expression
*expr
)
773 if (expr
->type
!= EXPR_COMPARE
)
776 if (expr
->op
== SPECIAL_EQUAL
||
777 expr
->op
== SPECIAL_NOTEQUAL
) {
778 handle_eq_noteq(expr
);
782 handle_compare(expr
);
785 static void match_returns_user_rl(const char *fn
, struct expression
*expr
, void *unused
)
787 func_gets_user_data
= true;
790 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
792 struct expression
*parent
;
798 macro
= get_macro_name(expr
->pos
);
802 /* handle ntohl(foo[i]) where "i" is trusted */
803 parent
= expr_get_parent_expr(expr
);
804 while (parent
&& parent
->type
!= EXPR_BINOP
)
805 parent
= expr_get_parent_expr(parent
);
806 if (parent
&& parent
->type
== EXPR_BINOP
) {
807 char *parent_macro
= get_macro_name(parent
->pos
);
809 if (parent_macro
&& strcmp(macro
, parent_macro
) == 0)
813 if (strcmp(macro
, "ntohl") == 0) {
814 *rl
= alloc_whole_rl(&uint_ctype
);
817 if (strcmp(macro
, "ntohs") == 0) {
818 *rl
= alloc_whole_rl(&ushort_ctype
);
824 static int has_user_data(struct symbol
*sym
)
826 struct sm_state
*tmp
;
828 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), tmp
) {
831 } END_FOR_EACH_SM(tmp
);
835 bool we_pass_user_data(struct expression
*call
)
837 struct expression
*arg
;
840 FOR_EACH_PTR(call
->args
, arg
) {
841 if (points_to_user_data(arg
))
843 sym
= expr_to_sym(arg
);
846 if (has_user_data(sym
))
848 } END_FOR_EACH_PTR(arg
);
853 static int db_returned_user_rl(struct expression
*call
, struct range_list
**rl
)
855 struct smatch_state
*state
;
858 if (is_fake_call(call
))
860 snprintf(buf
, sizeof(buf
), "return %p", call
);
861 state
= get_state(my_id
, buf
, NULL
);
862 if (!state
|| !estate_rl(state
))
864 *rl
= estate_rl(state
);
868 struct stree
*get_user_stree(void)
870 return get_all_states_stree(my_id
);
873 static int user_data_flag
;
874 static int no_user_data_flag
;
875 struct range_list
*var_user_rl(struct expression
*expr
)
877 struct smatch_state
*state
;
878 struct range_list
*rl
;
879 struct range_list
*absolute_rl
;
881 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
882 no_user_data_flag
= 1;
886 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
887 struct range_list
*left
, *right
;
889 if (!get_user_rl(expr
->right
, &right
))
891 get_absolute_rl(expr
->left
, &left
);
892 rl
= rl_binop(left
, '%', right
);
896 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '/') {
897 struct range_list
*left
= NULL
;
898 struct range_list
*right
= NULL
;
899 struct range_list
*abs_right
;
902 * The specific bug I'm dealing with is:
904 * foo = capped_user / unknown;
906 * Instead of just saying foo is now entirely user_rl we should
907 * probably say instead that it is not at all user data.
911 get_user_rl(expr
->left
, &left
);
912 get_user_rl(expr
->right
, &right
);
913 get_absolute_rl(expr
->right
, &abs_right
);
915 if (left
&& !right
) {
916 rl
= rl_binop(left
, '/', abs_right
);
917 if (sval_cmp(rl_max(left
), rl_max(rl
)) < 0)
918 no_user_data_flag
= 1;
924 if (get_rl_from_function(expr
, &rl
))
927 if (get_user_macro_rl(expr
, &rl
))
930 if (comes_from_skb_data(expr
)) {
931 rl
= alloc_whole_rl(get_type(expr
));
935 state
= get_state_expr(my_id
, expr
);
936 if (state
&& estate_rl(state
)) {
937 rl
= estate_rl(state
);
941 if (expr
->type
== EXPR_CALL
&& db_returned_user_rl(expr
, &rl
))
944 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*' &&
945 points_to_user_data(expr
->unop
)) {
946 rl
= var_to_absolute_rl(expr
);
950 if (is_array(expr
)) {
951 struct expression
*array
= get_array_base(expr
);
953 if (!get_state_expr(my_id
, array
)) {
954 no_user_data_flag
= 1;
962 absolute_rl
= var_to_absolute_rl(expr
);
963 return clone_rl(rl_intersection(rl
, absolute_rl
));
966 static bool is_ptr_subtract(struct expression
*expr
)
968 expr
= strip_expr(expr
);
971 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '-' &&
972 type_is_ptr(get_type(expr
->left
))) {
978 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
980 if (is_ptr_subtract(expr
))
984 no_user_data_flag
= 0;
985 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
986 if (!user_data_flag
|| no_user_data_flag
)
992 int is_user_rl(struct expression
*expr
)
994 struct range_list
*tmp
;
996 return get_user_rl(expr
, &tmp
) && tmp
;
999 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
1001 struct smatch_state
*state
;
1003 state
= get_state(my_id
, name
, sym
);
1004 if (state
&& estate_rl(state
)) {
1005 *rl
= estate_rl(state
);
1011 static void return_info_callback(int return_id
, char *return_ranges
,
1012 struct expression
*returned_expr
,
1014 const char *printed_name
,
1015 struct sm_state
*sm
)
1017 struct smatch_state
*extra
;
1018 struct range_list
*rl
;
1022 if (strcmp(printed_name
, "$") == 0)
1024 if (!param_was_set_var_sym(sm
->name
, sm
->sym
))
1027 rl
= estate_rl(sm
->state
);
1030 extra
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1031 if (estate_rl(extra
))
1032 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(extra
));
1036 snprintf(buf
, sizeof(buf
), "%s%s%s",
1038 estate_capped(sm
->state
) ? "[c]" : "",
1039 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1040 sql_insert_return_states(return_id
, return_ranges
,
1041 estate_new(sm
->state
) ? USER_DATA_SET
: USER_DATA
,
1042 param
, printed_name
, buf
);
1045 static bool is_ignored_macro(struct position pos
)
1049 macro
= get_macro_name(pos
);
1052 if (strcmp(macro
, "v4l2_subdev_call") == 0)
1057 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1059 struct smatch_state
*state
;
1060 struct range_list
*rl
;
1061 struct symbol
*type
;
1064 if (is_ignored_macro(call
->pos
))
1068 * Smatch uses a hack where if we get an unsigned long we say it's
1069 * both user data and it points to user data. But if we pass it to a
1070 * function which takes an int, then it's just user data. There's not
1071 * enough bytes for it to be a pointer.
1074 type
= get_arg_type(call
->fn
, param
);
1075 if (strcmp(printed_name
, "$") != 0 && type
&& type_bits(type
) < type_bits(&ptr_ctype
))
1078 if (strcmp(sm
->state
->name
, "") == 0)
1081 state
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1082 if (!state
|| !estate_rl(state
))
1083 rl
= estate_rl(sm
->state
);
1085 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
1090 snprintf(buf
, sizeof(buf
), "%s%s%s", show_rl(rl
),
1091 estate_capped(sm
->state
) ? "[c]" : "",
1092 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1093 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, buf
);
1096 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1098 struct expression
*arg
;
1101 struct smatch_state
*state
;
1103 while (expr
->type
== EXPR_ASSIGNMENT
)
1104 expr
= strip_expr(expr
->right
);
1105 if (expr
->type
!= EXPR_CALL
)
1107 if (expr
== ignore_param_set
)
1110 arg
= get_argument_from_call_expr(expr
->args
, param
);
1113 name
= get_variable_from_key(arg
, key
, &sym
);
1117 state
= get_state(my_id
, name
, sym
);
1121 set_state(my_id
, name
, sym
, alloc_estate_empty());
1126 static bool param_data_capped(const char *value
)
1128 if (strstr(value
, ",c") || strstr(value
, "[c"))
1133 static bool param_data_treat_untagged(const char *value
)
1135 if (strstr(value
, ",u") || strstr(value
, "[u"))
1140 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1142 struct expression
*expr
;
1143 struct range_list
*rl
= NULL
;
1144 struct smatch_state
*state
;
1145 struct symbol
*type
;
1148 expr
= symbol_expression(sym
);
1149 fullname
= get_variable_from_key(expr
, key
, NULL
);
1153 type
= get_member_type_from_key(expr
, key
);
1154 if (type
&& type
->type
== SYM_STRUCT
)
1160 str_to_rl(type
, value
, &rl
);
1161 rl
= swap_mtag_seed(expr
, rl
);
1162 state
= alloc_estate_rl(rl
);
1163 if (param_data_capped(value
) || is_capped(expr
))
1164 estate_set_capped(state
);
1165 if (param_data_treat_untagged(value
) || sym
->ctype
.as
== 5)
1166 estate_set_treat_untagged(state
);
1167 set_state(my_id
, fullname
, sym
, state
);
1170 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1172 set_state(my_call_id
, "this_function", NULL
, &called
);
1175 static void match_syscall_definition(struct symbol
*sym
)
1182 macro
= get_macro_name(sym
->pos
);
1184 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
1185 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1188 name
= get_function();
1189 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
1190 if (name
&& strncmp(name
, "sys_", 4) == 0)
1194 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
1200 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
1201 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
1202 } END_FOR_EACH_PTR(arg
);
1208 static void store_user_data_return(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1210 struct smatch_state
*state
;
1211 struct range_list
*rl
;
1212 struct symbol
*type
;
1218 type
= get_type(expr
);
1219 snprintf(buf
, sizeof(buf
), "return %p%s", expr
, key
+ 1);
1220 call_results_to_rl(expr
, type
, value
, &rl
);
1222 state
= alloc_estate_rl(rl
);
1224 estate_set_new(state
);
1226 set_state(my_id
, buf
, NULL
, state
);
1229 static void set_to_user_data(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1231 struct smatch_state
*state
;
1234 struct symbol
*type
;
1235 struct range_list
*rl
= NULL
;
1237 type
= get_member_type_from_key(expr
, key
);
1238 name
= get_variable_from_key(expr
, key
, &sym
);
1242 call_results_to_rl(expr
, type
, value
, &rl
);
1244 state
= alloc_estate_rl(rl
);
1245 if (param_data_capped(value
))
1246 estate_set_capped(state
);
1247 if (param_data_treat_untagged(value
))
1248 estate_set_treat_untagged(state
);
1250 estate_set_new(state
);
1251 set_state(my_id
, name
, sym
, state
);
1256 static void returns_param_user_data(struct expression
*expr
, int param
, char *key
, char *value
)
1258 struct expression
*arg
;
1259 struct expression
*call
;
1262 while (call
->type
== EXPR_ASSIGNMENT
)
1263 call
= strip_expr(call
->right
);
1264 if (call
->type
!= EXPR_CALL
)
1267 if (!we_pass_user_data(call
))
1271 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1272 store_user_data_return(expr
, key
, value
, OLD
);
1275 set_to_user_data(expr
->left
, key
, value
, OLD
);
1279 arg
= get_argument_from_call_expr(call
->args
, param
);
1282 set_to_user_data(arg
, key
, value
, OLD
);
1285 static void returns_param_user_data_set(struct expression
*expr
, int param
, char *key
, char *value
)
1287 struct expression
*arg
;
1289 func_gets_user_data
= true;
1292 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1293 store_user_data_return(expr
, key
, value
, NEW
);
1296 set_to_user_data(expr
->left
, key
, value
, NEW
);
1300 while (expr
->type
== EXPR_ASSIGNMENT
)
1301 expr
= strip_expr(expr
->right
);
1302 if (expr
->type
!= EXPR_CALL
)
1305 arg
= get_argument_from_call_expr(expr
->args
, param
);
1308 set_to_user_data(arg
, key
, value
, NEW
);
1311 static void returns_param_capped(struct expression
*expr
, int param
, char *key
, char *value
)
1313 struct smatch_state
*state
, *new;
1317 name
= get_name_sym_from_key(expr
, param
, key
, &sym
);
1321 state
= get_state(my_id
, name
, sym
);
1322 if (!state
|| estate_capped(state
))
1325 new = clone_estate(state
);
1326 estate_set_capped(new);
1328 set_state(my_id
, name
, sym
, new);
1333 static void match_function_def(struct symbol
*sym
)
1335 if (is_user_data_fn(sym
))
1336 func_gets_user_data
= true;
1339 void register_kernel_user_data(int id
)
1345 if (option_project
!= PROJ_KERNEL
)
1348 set_dynamic_states(my_id
);
1350 add_function_data(&func_gets_user_data
);
1351 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1353 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
1354 add_hook(&free_start_states
, AFTER_FUNC_HOOK
);
1355 add_function_data((unsigned long *)&start_states
);
1357 add_unmatched_state_hook(my_id
, &empty_state
);
1358 add_extra_nomod_hook(&extra_nomod_hook
);
1359 add_pre_merge_hook(my_id
, &pre_merge_hook
);
1360 add_merge_hook(my_id
, &merge_estates
);
1362 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
1363 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
1364 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
1365 for (i
= 0; i
< ARRAY_SIZE(kstr_funcs
); i
++)
1366 add_function_hook_late(kstr_funcs
[i
], &match_user_copy
, INT_PTR(2));
1367 add_function_hook("usb_control_msg", &match_user_copy
, INT_PTR(6));
1368 add_function_hook("kvm_read_guest_virt", &match_user_copy
, INT_PTR(2));
1370 for (i
= 0; i
< ARRAY_SIZE(returns_user_data
); i
++)
1371 add_function_hook(returns_user_data
[i
], &match_returns_user_rl
, NULL
);
1373 add_function_hook("sscanf", &match_sscanf
, NULL
);
1375 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
1377 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1378 select_return_states_hook(PARAM_SET
, &db_param_set
);
1379 add_hook(&match_condition
, CONDITION_HOOK
);
1381 add_caller_info_callback(my_id
, caller_info_callback
);
1382 add_return_info_callback(my_id
, return_info_callback
);
1383 select_caller_info_hook(set_param_user_data
, USER_DATA
);
1384 select_return_states_hook(USER_DATA
, &returns_param_user_data
);
1385 select_return_states_hook(USER_DATA_SET
, &returns_param_user_data_set
);
1386 select_return_states_hook(CAPPED_DATA
, &returns_param_capped
);
1389 void register_kernel_user_data2(int id
)
1393 if (option_project
!= PROJ_KERNEL
)
1395 select_caller_info_hook(set_called
, INTERNAL
);