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
;
32 static struct expression
*ignore_clear
;
41 const sval_t
*implies_start
, *implies_end
;
45 static struct user_fn_info func_table
[] = {
46 { "iov_iter_count", USER_DATA
, -1, "$" },
47 { "simple_strtol", USER_DATA
, -1, "$" },
48 { "simple_strtoll", USER_DATA
, -1, "$" },
49 { "simple_strtoul", USER_DATA
, -1, "$" },
50 { "simple_strtoull", USER_DATA
, -1, "$" },
51 { "kvm_register_read", USER_DATA
, -1, "$" },
54 static struct smatch_state
*empty_state(struct sm_state
*sm
)
56 return alloc_estate_empty();
59 static struct smatch_state
*new_state(struct symbol
*type
)
61 struct smatch_state
*state
;
63 if (!type
|| type_is_ptr(type
))
66 state
= alloc_estate_whole(type
);
67 estate_set_new(state
);
71 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
73 struct smatch_state
*user
= cur
->state
;
74 struct smatch_state
*extra
;
75 struct smatch_state
*state
;
76 struct range_list
*rl
;
78 extra
= __get_state(SMATCH_EXTRA
, cur
->name
, cur
->sym
);
81 rl
= rl_intersection(estate_rl(user
), estate_rl(extra
));
82 state
= alloc_estate_rl(clone_rl(rl
));
83 if (estate_capped(user
) || is_capped_var_sym(cur
->name
, cur
->sym
))
84 estate_set_capped(state
);
85 if (estate_treat_untagged(user
))
86 estate_set_treat_untagged(state
);
87 if (estates_equiv(state
, cur
->state
))
89 if (estate_new(cur
->state
))
90 estate_set_new(state
);
91 set_state(my_id
, cur
->name
, cur
->sym
, state
);
94 static void extra_nomod_hook(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
96 struct smatch_state
*user
, *new;
97 struct range_list
*rl
;
99 user
= __get_state(my_id
, name
, sym
);
102 rl
= rl_intersection(estate_rl(user
), estate_rl(state
));
103 if (rl_equiv(rl
, estate_rl(user
)))
105 new = alloc_estate_rl(rl
);
106 if (estate_capped(user
))
107 estate_set_capped(new);
108 if (estate_treat_untagged(user
))
109 estate_set_treat_untagged(new);
110 set_state(my_id
, name
, sym
, new);
113 static void store_type_info(struct expression
*expr
, struct smatch_state
*state
)
116 char *type_str
, *member
;
118 if (__in_fake_assign
)
121 if (!estate_rl(state
))
124 expr
= strip_expr(expr
);
125 if (!expr
|| expr
->type
!= EXPR_DEREF
|| !expr
->member
)
128 type
= get_type(expr
->deref
);
129 if (!type
|| !type
->ident
)
132 type_str
= type_to_str(type
);
135 member
= get_member_name(expr
);
139 sql_insert_function_type_info(USER_DATA
, type_str
, member
, state
->name
);
142 static void set_user_data(struct expression
*expr
, struct smatch_state
*state
)
144 store_type_info(expr
, state
);
145 set_state_expr(my_id
, expr
, state
);
148 static bool user_rl_known(struct expression
*expr
)
150 struct range_list
*rl
;
153 if (!get_user_rl(expr
, &rl
))
156 close_to_max
= sval_type_max(rl_type(rl
));
157 close_to_max
.value
-= 100;
159 if (sval_cmp(rl_max(rl
), close_to_max
) >= 0)
164 static bool is_array_index_mask_nospec(struct expression
*expr
)
166 struct expression
*orig
;
168 orig
= get_assigned_expr(expr
);
169 if (!orig
|| orig
->type
!= EXPR_CALL
)
171 return sym_name_is("array_index_mask_nospec", orig
->fn
);
174 static bool binop_capped(struct expression
*expr
)
176 struct range_list
*left_rl
;
180 if (expr
->op
== '-' && get_user_rl(expr
->left
, &left_rl
)) {
181 if (user_rl_capped(expr
->left
))
183 comparison
= get_comparison(expr
->left
, expr
->right
);
184 if (comparison
&& show_special(comparison
)[0] == '>')
189 if (expr
->op
== '&' || expr
->op
== '%') {
190 bool left_user
, left_capped
, right_user
, right_capped
;
192 if (!get_value(expr
->right
, &sval
) && is_capped(expr
->right
))
194 if (is_array_index_mask_nospec(expr
->right
))
196 if (is_capped(expr
->left
))
198 left_user
= is_user_rl(expr
->left
);
199 right_user
= is_user_rl(expr
->right
);
200 if (!left_user
&& !right_user
)
203 left_capped
= user_rl_capped(expr
->left
);
204 right_capped
= user_rl_capped(expr
->right
);
206 if (left_user
&& left_capped
) {
209 if (right_user
&& right_capped
)
213 if (right_user
&& right_capped
) {
222 * Generally "capped" means that we capped it to an unknown value.
223 * This is useful because if Smatch doesn't know what the value is then
224 * we have to trust that it is correct. But if we known cap value is
225 * 100 then we can check if 100 is correct and complain if it's wrong.
227 * So then the problem is with BINOP when we take a capped variable
228 * plus a user variable which is clamped to a known range (uncapped)
229 * the result should be capped.
231 if ((user_rl_capped(expr
->left
) || user_rl_known(expr
->left
)) &&
232 (user_rl_capped(expr
->right
) || user_rl_known(expr
->right
)))
238 bool user_rl_capped_var_sym(const char *name
, struct symbol
*sym
)
240 struct smatch_state
*state
;
242 state
= get_state(my_id
, name
, sym
);
244 return estate_capped(state
);
249 bool user_rl_capped(struct expression
*expr
)
251 struct smatch_state
*state
;
252 struct range_list
*rl
;
255 expr
= strip_expr(expr
);
258 if (get_value(expr
, &sval
))
260 if (expr
->type
== EXPR_BINOP
)
261 return binop_capped(expr
);
262 if ((expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) &&
263 (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
))
264 return user_rl_capped(expr
->unop
);
265 state
= get_state_expr(my_id
, expr
);
267 return estate_capped(state
);
269 if (!get_user_rl(expr
, &rl
)) {
271 * The non user data parts of a binop are capped and
272 * also empty user rl states are capped.
277 if (rl_to_sval(rl
, &sval
))
280 return false; /* uncapped user data */
283 bool user_rl_treat_untagged(struct expression
*expr
)
285 struct smatch_state
*state
;
286 struct range_list
*rl
;
289 expr
= strip_expr(expr
);
292 if (get_value(expr
, &sval
))
295 state
= get_state_expr(my_id
, expr
);
297 return estate_treat_untagged(state
);
299 if (get_user_rl(expr
, &rl
))
300 return false; /* uncapped user data */
302 return true; /* not actually user data */
305 static int is_dev_attr_name(struct expression
*expr
)
310 name
= expr_to_str(expr
);
313 if (strstr(name
, "->attr.name"))
319 static bool is_percent_n(struct expression
*expr
, int pos
)
326 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
329 p
= expr
->string
->data
;
332 (p
[0] == '%' && p
[1] == '%')) {
349 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
351 struct expression
*str
, *format
, *arg
;
354 str
= get_argument_from_call_expr(expr
->args
, 0);
355 if (is_dev_attr_name(str
))
358 format
= get_argument_from_call_expr(expr
->args
, 1);
359 if (is_dev_attr_name(format
))
363 FOR_EACH_PTR(expr
->args
, arg
) {
367 if (is_percent_n(format
, i
- 2))
369 mark_as_user_data(deref_expression(arg
), true);
370 } END_FOR_EACH_PTR(arg
);
373 static int comes_from_skb_data(struct expression
*expr
)
375 expr
= strip_expr(expr
);
376 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
379 expr
= strip_expr(expr
->unop
);
382 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
383 expr
= strip_expr(expr
->left
);
385 return is_skb_data(expr
);
388 static int handle_get_user(struct expression
*expr
)
393 name
= get_macro_name(expr
->pos
);
394 if (!name
|| strcmp(name
, "get_user") != 0)
397 name
= expr_to_var(expr
->right
);
398 if (!name
|| (strcmp(name
, "__val_gu") != 0 && strcmp(name
, "__gu_val") != 0))
400 set_user_data(expr
->left
, new_state(get_type(expr
->left
)));
407 static bool state_is_new(struct expression
*expr
)
409 struct smatch_state
*state
;
411 state
= get_state_expr(my_id
, expr
);
412 if (estate_new(state
))
415 if (expr
->type
== EXPR_BINOP
) {
416 if (state_is_new(expr
->left
))
418 if (state_is_new(expr
->right
))
424 static struct range_list
*strip_negatives(struct range_list
*rl
)
426 sval_t min
= rl_min(rl
);
427 sval_t minus_one
= { .type
= rl_type(rl
), .value
= -1 };
428 sval_t over
= { .type
= rl_type(rl
), .value
= INT_MAX
+ 1ULL };
429 sval_t max
= sval_type_max(rl_type(rl
));
434 if (type_unsigned(rl_type(rl
)) && type_bits(rl_type(rl
)) > 31)
435 return remove_range(rl
, over
, max
);
437 return remove_range(rl
, min
, minus_one
);
440 static bool handle_op_assign(struct expression
*expr
)
442 struct expression
*binop_expr
;
443 struct smatch_state
*state
;
444 struct range_list
*rl
;
447 case SPECIAL_ADD_ASSIGN
:
448 case SPECIAL_SUB_ASSIGN
:
449 case SPECIAL_AND_ASSIGN
:
450 case SPECIAL_MOD_ASSIGN
:
451 case SPECIAL_SHL_ASSIGN
:
452 case SPECIAL_SHR_ASSIGN
:
453 case SPECIAL_OR_ASSIGN
:
454 case SPECIAL_XOR_ASSIGN
:
455 case SPECIAL_MUL_ASSIGN
:
456 case SPECIAL_DIV_ASSIGN
:
457 binop_expr
= binop_expression(expr
->left
,
458 op_remove_assign(expr
->op
),
460 if (!get_user_rl(binop_expr
, &rl
))
463 rl
= cast_rl(get_type(expr
->left
), rl
);
464 state
= alloc_estate_rl(rl
);
465 if (expr
->op
== SPECIAL_AND_ASSIGN
||
466 expr
->op
== SPECIAL_MOD_ASSIGN
||
467 user_rl_capped(binop_expr
))
468 estate_set_capped(state
);
469 if (user_rl_treat_untagged(expr
->left
))
470 estate_set_treat_untagged(state
);
471 if (state_is_new(binop_expr
))
472 estate_set_new(state
);
473 estate_set_assigned(state
);
474 set_user_data(expr
->left
, state
);
480 static void handle_derefed_pointers(struct expression
*expr
, bool is_new
)
482 expr
= strip_expr(expr
);
483 if (expr
->type
!= EXPR_PREOP
||
486 expr
= strip_expr(expr
->unop
);
487 set_array_user_ptr(expr
, is_new
);
490 static void match_assign(struct expression
*expr
)
492 struct symbol
*left_type
, *right_type
;
493 struct range_list
*rl
= NULL
;
494 static struct expression
*handled
;
495 struct smatch_state
*state
;
496 struct expression
*faked
;
497 bool is_capped
= false;
500 left_type
= get_type(expr
->left
);
501 if (left_type
== &void_ctype
)
504 faked
= get_faked_expression();
505 if (faked
&& faked
== ignore_clear
)
508 /* FIXME: handle fake array assignments frob(&user_array[x]); */
511 faked
->type
== EXPR_ASSIGNMENT
&&
512 points_to_user_data(faked
->right
)) {
513 if (is_skb_data(faked
->right
))
515 rl
= alloc_whole_rl(left_type
);
519 if (faked
&& faked
== handled
)
521 if (is_fake_call(expr
->right
))
522 goto clear_old_state
;
523 if (handle_get_user(expr
))
525 if (points_to_user_data(expr
->right
) &&
526 is_struct_ptr(get_type(expr
->left
))) {
528 // This should be handled by smatch_points_to_user_data.c
529 // set_array_user_ptr(expr->left);
532 if (handle_op_assign(expr
))
535 goto clear_old_state
;
537 /* Handled by DB code */
538 if (expr
->right
->type
== EXPR_CALL
)
542 disable_type_val_lookups();
543 get_user_rl(expr
->right
, &rl
);
545 enable_type_val_lookups();
547 goto clear_old_state
;
549 is_capped
= user_rl_capped(expr
->right
);
550 is_new
= state_is_new(expr
->right
);
553 right_type
= get_type(expr
->right
);
554 if (type_is_ptr(left_type
)) {
555 if (right_type
&& right_type
->type
== SYM_ARRAY
)
556 set_array_user_ptr(expr
->left
, is_new
);
560 rl
= cast_rl(left_type
, rl
);
561 if (is_capped
&& type_unsigned(right_type
) && type_signed(left_type
))
562 rl
= strip_negatives(rl
);
563 state
= alloc_estate_rl(rl
);
565 estate_set_new(state
);
567 estate_set_capped(state
);
568 if (user_rl_treat_untagged(expr
->right
))
569 estate_set_treat_untagged(state
);
570 estate_set_assigned(state
);
572 set_user_data(expr
->left
, state
);
573 handle_derefed_pointers(expr
->left
, is_new
);
579 * HACK ALERT!!! This should be at the start of the function. The
580 * the problem is that handling "pointer = array;" assignments is
581 * handled in this function instead of in kernel_points_to_user_data.c.
583 if (type_is_ptr(left_type
))
586 if (get_state_expr(my_id
, expr
->left
))
587 set_user_data(expr
->left
, alloc_estate_empty());
590 static void handle_eq_noteq(struct expression
*expr
)
592 struct smatch_state
*left_orig
, *right_orig
;
594 left_orig
= get_state_expr(my_id
, expr
->left
);
595 right_orig
= get_state_expr(my_id
, expr
->right
);
597 if (!left_orig
&& !right_orig
)
599 if (left_orig
&& right_orig
)
603 set_true_false_states_expr(my_id
, expr
->left
,
604 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
605 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
607 set_true_false_states_expr(my_id
, expr
->right
,
608 expr
->op
== SPECIAL_EQUAL
? alloc_estate_empty() : NULL
,
609 expr
->op
== SPECIAL_EQUAL
? NULL
: alloc_estate_empty());
613 static void handle_compare(struct expression
*expr
)
615 struct expression
*left
, *right
;
616 struct range_list
*left_rl
= NULL
;
617 struct range_list
*right_rl
= NULL
;
618 struct range_list
*user_rl
;
619 struct smatch_state
*capped_state
;
620 struct smatch_state
*left_true
= NULL
;
621 struct smatch_state
*left_false
= NULL
;
622 struct smatch_state
*right_true
= NULL
;
623 struct smatch_state
*right_false
= NULL
;
627 left
= strip_expr(expr
->left
);
628 right
= strip_expr(expr
->right
);
630 while (left
->type
== EXPR_ASSIGNMENT
)
631 left
= strip_expr(left
->left
);
634 * Conditions are mostly handled by smatch_extra.c, but there are some
635 * times where the exact values are not known so we can't do that.
637 * Normally, we might consider using smatch_capped.c to supliment smatch
638 * extra but that doesn't work when we merge unknown uncapped kernel
639 * data with unknown capped user data. The result is uncapped user
640 * data. We need to keep it separate and say that the user data is
641 * capped. In the past, I would have marked this as just regular
642 * kernel data (not user data) but we can't do that these days because
643 * we need to track user data for Spectre.
645 * The other situation which we have to handle is when we do have an
646 * int and we compare against an unknown unsigned kernel variable. In
647 * that situation we assume that the kernel data is less than INT_MAX.
648 * Otherwise then we get all sorts of array underflow false positives.
652 /* Handled in smatch_extra.c */
653 if (get_implied_value(left
, &sval
) ||
654 get_implied_value(right
, &sval
))
657 get_user_rl(left
, &left_rl
);
658 get_user_rl(right
, &right_rl
);
661 if (!left_rl
&& !right_rl
)
663 /* if both sides are user data that's not a good limit */
664 if (left_rl
&& right_rl
)
672 type
= get_type(expr
);
673 if (type_unsigned(type
))
674 user_rl
= strip_negatives(user_rl
);
675 capped_state
= alloc_estate_rl(user_rl
);
676 estate_set_capped(capped_state
);
680 case SPECIAL_UNSIGNED_LT
:
682 case SPECIAL_UNSIGNED_LTE
:
684 left_true
= capped_state
;
686 right_false
= capped_state
;
689 case SPECIAL_UNSIGNED_GT
:
691 case SPECIAL_UNSIGNED_GTE
:
693 left_false
= capped_state
;
695 right_true
= capped_state
;
699 set_true_false_states_expr(my_id
, left
, left_true
, left_false
);
700 set_true_false_states_expr(my_id
, right
, right_true
, right_false
);
703 static void match_condition(struct expression
*expr
)
705 if (expr
->type
!= EXPR_COMPARE
)
708 if (expr
->op
== SPECIAL_EQUAL
||
709 expr
->op
== SPECIAL_NOTEQUAL
) {
710 handle_eq_noteq(expr
);
714 handle_compare(expr
);
717 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
719 struct expression
*parent
;
725 macro
= get_macro_name(expr
->pos
);
729 /* handle ntohl(foo[i]) where "i" is trusted */
730 parent
= expr_get_parent_expr(expr
);
731 while (parent
&& parent
->type
!= EXPR_BINOP
)
732 parent
= expr_get_parent_expr(parent
);
733 if (parent
&& parent
->type
== EXPR_BINOP
) {
734 char *parent_macro
= get_macro_name(parent
->pos
);
736 if (parent_macro
&& strcmp(macro
, parent_macro
) == 0)
740 if (strcmp(macro
, "ntohl") == 0) {
741 *rl
= alloc_whole_rl(&uint_ctype
);
744 if (strcmp(macro
, "ntohs") == 0) {
745 *rl
= alloc_whole_rl(&ushort_ctype
);
751 static int has_user_data(struct symbol
*sym
)
753 struct sm_state
*tmp
;
755 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), tmp
) {
758 } END_FOR_EACH_SM(tmp
);
762 bool we_pass_user_data(struct expression
*call
)
764 struct expression
*arg
;
767 FOR_EACH_PTR(call
->args
, arg
) {
768 if (points_to_user_data(arg
))
770 sym
= expr_to_sym(arg
);
773 if (has_user_data(sym
))
775 } END_FOR_EACH_PTR(arg
);
780 // TODO: faked_assign this should already be handled
781 static int db_returned_user_rl(struct expression
*call
, struct range_list
**rl
)
783 struct smatch_state
*state
;
786 if (is_fake_call(call
))
788 snprintf(buf
, sizeof(buf
), "return %p", call
);
789 state
= get_state(my_id
, buf
, NULL
);
790 if (!state
|| !estate_rl(state
))
792 *rl
= estate_rl(state
);
796 struct stree
*get_user_stree(void)
798 return get_all_states_stree(my_id
);
801 static struct int_stack
*user_data_flags
, *no_user_data_flags
;
803 static void set_flag(struct int_stack
**stack
)
807 num
= pop_int(stack
);
809 push_int(stack
, num
);
812 struct range_list
*var_user_rl(struct expression
*expr
)
814 struct smatch_state
*state
;
815 struct range_list
*rl
;
816 struct range_list
*absolute_rl
;
818 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
819 set_flag(&no_user_data_flags
);
823 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
824 struct range_list
*left
, *right
;
826 if (!get_user_rl(expr
->right
, &right
))
828 get_absolute_rl(expr
->left
, &left
);
829 rl
= rl_binop(left
, '%', right
);
833 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '/') {
834 struct range_list
*left
= NULL
;
835 struct range_list
*right
= NULL
;
836 struct range_list
*abs_right
;
839 * The specific bug I'm dealing with is:
841 * foo = capped_user / unknown;
843 * Instead of just saying foo is now entirely user_rl we should
844 * probably say instead that it is not at all user data.
848 get_user_rl(expr
->left
, &left
);
849 get_user_rl(expr
->right
, &right
);
850 get_absolute_rl(expr
->right
, &abs_right
);
852 if (left
&& !right
) {
853 rl
= rl_binop(left
, '/', abs_right
);
854 if (sval_cmp(rl_max(left
), rl_max(rl
)) < 0)
855 set_flag(&no_user_data_flags
);
861 if (get_user_macro_rl(expr
, &rl
))
864 if (comes_from_skb_data(expr
)) {
865 rl
= alloc_whole_rl(get_type(expr
));
869 state
= get_state_expr(my_id
, expr
);
870 if (state
&& estate_rl(state
)) {
871 rl
= estate_rl(state
);
875 if (expr
->type
== EXPR_CALL
&& db_returned_user_rl(expr
, &rl
))
878 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*' &&
879 points_to_user_data(expr
->unop
)) {
880 rl
= var_to_absolute_rl(expr
);
884 if (is_array(expr
)) {
885 struct expression
*array
= get_array_base(expr
);
887 if (!get_state_expr(my_id
, array
)) {
888 set_flag(&no_user_data_flags
);
895 set_flag(&user_data_flags
);
896 absolute_rl
= var_to_absolute_rl(expr
);
897 return rl_intersection(rl
, absolute_rl
);
900 static bool is_ptr_subtract(struct expression
*expr
)
902 expr
= strip_expr(expr
);
905 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '-' &&
906 type_is_ptr(get_type(expr
->left
))) {
912 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
914 int user_data
, no_user_data
;
919 if (__in_fake_struct_assign
&&
920 !has_states(__get_cur_stree(), my_id
))
923 if (is_ptr_subtract(expr
))
926 push_int(&user_data_flags
, 0);
927 push_int(&no_user_data_flags
, 0);
929 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
931 user_data
= pop_int(&user_data_flags
);
932 no_user_data
= pop_int(&no_user_data_flags
);
934 if (!user_data
|| no_user_data
)
940 int is_user_rl(struct expression
*expr
)
942 struct range_list
*tmp
;
944 return get_user_rl(expr
, &tmp
) && tmp
;
947 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
949 struct smatch_state
*state
, *extra
;
951 state
= get_state(my_id
, name
, sym
);
952 if (!estate_rl(state
))
954 *rl
= estate_rl(state
);
956 extra
= get_state(SMATCH_EXTRA
, name
, sym
);
957 if (estate_rl(extra
))
958 *rl
= rl_intersection(estate_rl(state
), estate_rl(extra
));
963 bool is_socket_stuff(struct symbol
*sym
)
968 * Basically I never want to consider an skb or sk as user pointer.
969 * The skb->data is already marked as a source of user data, and if
970 * anything else is marked as user data it's almost certainly wrong.
972 * Ideally, I would figure out where this bogus data is coming from,
973 * but possibly it just was stuck in the database from previous updates
974 * and can't get cleared out without deleting all user data. Things
975 * like this gets stuck in the DB because of recursion.
977 * I could make this a temporary hack, but I keep wanting to do it so
978 * I'm just going to make it permanent. It either doesn't change
979 * anything or it makes life better.
982 type
= get_real_base_type(sym
);
983 if (!type
|| type
->type
!= SYM_PTR
)
985 type
= get_real_base_type(type
);
986 if (!type
|| type
->type
!= SYM_STRUCT
|| !type
->ident
)
989 if (strcmp(type
->ident
->name
, "sk_buff") == 0)
991 if (strcmp(type
->ident
->name
, "sock") == 0)
993 if (strcmp(type
->ident
->name
, "socket") == 0)
999 static void return_info_callback(int return_id
, char *return_ranges
,
1000 struct expression
*returned_expr
,
1002 const char *printed_name
,
1003 struct sm_state
*sm
)
1005 struct smatch_state
*extra
;
1006 struct range_list
*rl
;
1009 if (is_socket_stuff(sm
->sym
))
1011 if (is_ignored_kernel_data(printed_name
))
1015 if (strcmp(printed_name
, "$") == 0)
1017 if (!estate_assigned(sm
->state
) &&
1018 !estate_new(sm
->state
))
1021 rl
= estate_rl(sm
->state
);
1024 extra
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1025 if (estate_rl(extra
))
1026 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(extra
));
1030 snprintf(buf
, sizeof(buf
), "%s%s%s",
1032 estate_capped(sm
->state
) ? "[c]" : "",
1033 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1034 sql_insert_return_states(return_id
, return_ranges
,
1035 estate_new(sm
->state
) ? USER_DATA_SET
: USER_DATA
,
1036 param
, printed_name
, buf
);
1039 static bool is_ignored_macro(struct position pos
)
1043 macro
= get_macro_name(pos
);
1046 if (strcmp(macro
, "v4l2_subdev_call") == 0)
1051 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1053 struct smatch_state
*state
;
1054 struct range_list
*rl
;
1055 struct symbol
*type
;
1058 if (is_ignored_macro(call
->pos
))
1061 if (is_socket_stuff(sm
->sym
))
1063 if (is_ignored_kernel_data(printed_name
))
1067 * Smatch uses a hack where if we get an unsigned long we say it's
1068 * both user data and it points to user data. But if we pass it to a
1069 * function which takes an int, then it's just user data. There's not
1070 * enough bytes for it to be a pointer.
1073 type
= get_arg_type(call
->fn
, param
);
1074 if (strcmp(printed_name
, "$") != 0 && type
&& type_bits(type
) < type_bits(&ptr_ctype
))
1077 if (strcmp(sm
->state
->name
, "") == 0)
1080 state
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
1081 if (!state
|| !estate_rl(state
))
1082 rl
= estate_rl(sm
->state
);
1084 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
1089 snprintf(buf
, sizeof(buf
), "%s%s%s", show_rl(rl
),
1090 estate_capped(sm
->state
) ? "[c]" : "",
1091 estate_treat_untagged(sm
->state
) ? "[u]" : "");
1092 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, buf
);
1095 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1097 struct expression
*arg
;
1100 struct smatch_state
*state
;
1102 while (expr
->type
== EXPR_ASSIGNMENT
)
1103 expr
= strip_expr(expr
->right
);
1104 if (expr
->type
!= EXPR_CALL
)
1106 if (expr
== ignore_clear
)
1109 arg
= get_argument_from_call_expr(expr
->args
, param
);
1112 name
= get_variable_from_key(arg
, key
, &sym
);
1116 state
= get_state(my_id
, name
, sym
);
1120 set_state(my_id
, name
, sym
, alloc_estate_empty());
1125 static bool param_data_capped(const char *value
)
1127 if (strstr(value
, ",c") || strstr(value
, "[c"))
1132 static bool param_data_treat_untagged(const char *value
)
1134 if (strstr(value
, ",u") || strstr(value
, "[u"))
1139 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1141 struct expression
*expr
;
1142 struct range_list
*rl
= NULL
;
1143 struct smatch_state
*state
;
1144 struct symbol
*type
;
1147 expr
= symbol_expression(sym
);
1148 fullname
= get_variable_from_key(expr
, key
, NULL
);
1152 type
= get_member_type_from_key(expr
, key
);
1153 if (type
&& type
->type
== SYM_STRUCT
)
1159 str_to_rl(type
, value
, &rl
);
1160 rl
= swap_mtag_seed(expr
, rl
);
1161 state
= alloc_estate_rl(rl
);
1162 if (param_data_capped(value
) || is_capped(expr
))
1163 estate_set_capped(state
);
1164 if (param_data_treat_untagged(value
) || sym
->ctype
.as
== 5)
1165 estate_set_treat_untagged(state
);
1166 set_state(my_id
, fullname
, sym
, state
);
1169 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1171 set_state(my_call_id
, "this_function", NULL
, &called
);
1174 static void match_syscall_definition(struct symbol
*sym
)
1181 macro
= get_macro_name(sym
->pos
);
1183 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
1184 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1187 name
= get_function();
1188 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
1189 if (name
&& strncmp(name
, "sys_", 4) == 0)
1193 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
1199 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
1200 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
1201 } END_FOR_EACH_PTR(arg
);
1207 static void store_user_data_return(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1209 struct smatch_state
*state
;
1210 struct range_list
*rl
;
1211 struct symbol
*type
;
1217 type
= get_type(expr
);
1218 snprintf(buf
, sizeof(buf
), "return %p%s", expr
, key
+ 1);
1219 call_results_to_rl(expr
, type
, value
, &rl
);
1221 state
= alloc_estate_rl(rl
);
1223 estate_set_new(state
);
1225 set_state(my_id
, buf
, NULL
, state
);
1228 // FIXME: not a fan of this name, would prefer set_to_user_data() but that's
1230 void mark_as_user_data(struct expression
*expr
, bool isnew
)
1232 struct smatch_state
*state
;
1234 state
= alloc_estate_whole(get_type(expr
));
1236 estate_set_new(state
);
1237 set_state_expr(my_id
, expr
, state
);
1240 static void set_to_user_data(struct expression
*expr
, char *key
, char *value
, bool is_new
)
1242 struct smatch_state
*state
;
1245 struct symbol
*type
;
1246 struct range_list
*rl
= NULL
;
1248 type
= get_member_type_from_key(expr
, key
);
1249 name
= get_variable_from_key(expr
, key
, &sym
);
1253 call_results_to_rl(expr
, type
, value
, &rl
);
1255 state
= alloc_estate_rl(rl
);
1256 if (param_data_capped(value
))
1257 estate_set_capped(state
);
1258 if (param_data_treat_untagged(value
))
1259 estate_set_treat_untagged(state
);
1261 estate_set_new(state
);
1262 estate_set_assigned(state
);
1263 set_state(my_id
, name
, sym
, state
);
1268 static void returns_param_user_data(struct expression
*expr
, int param
, char *key
, char *value
)
1270 struct expression
*arg
;
1271 struct expression
*call
;
1274 while (call
->type
== EXPR_ASSIGNMENT
)
1275 call
= strip_expr(call
->right
);
1276 if (call
->type
!= EXPR_CALL
)
1279 if (!we_pass_user_data(call
))
1283 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1284 // TODO: faked_assign this should all be handled as a fake assignment
1285 store_user_data_return(expr
, key
, value
, OLD
);
1288 set_to_user_data(expr
->left
, key
, value
, OLD
);
1292 arg
= get_argument_from_call_expr(call
->args
, param
);
1295 set_to_user_data(arg
, key
, value
, OLD
);
1298 static void returns_param_user_data_set(struct expression
*expr
, int param
, char *key
, char *value
)
1300 struct expression
*arg
;
1303 if (expr
->type
!= EXPR_ASSIGNMENT
) {
1304 store_user_data_return(expr
, key
, value
, NEW
);
1307 set_to_user_data(expr
->left
, key
, value
, NEW
);
1311 while (expr
->type
== EXPR_ASSIGNMENT
)
1312 expr
= strip_expr(expr
->right
);
1313 if (expr
->type
!= EXPR_CALL
)
1316 arg
= get_argument_from_call_expr(expr
->args
, param
);
1319 set_to_user_data(arg
, key
, value
, NEW
);
1322 static void set_param_key_user_data(struct expression
*expr
, const char *name
,
1323 struct symbol
*sym
, void *data
)
1325 struct expression
*arg
;
1327 arg
= gen_expression_from_name_sym(name
, sym
);
1328 set_state_expr(my_id
, arg
, new_state(get_type(arg
)));
1331 static void match_capped(struct expression
*expr
, const char *name
, struct symbol
*sym
, void *info
)
1333 struct smatch_state
*state
, *new;
1335 state
= get_state(my_id
, name
, sym
);
1336 if (!state
|| estate_capped(state
))
1339 new = clone_estate(state
);
1340 estate_set_capped(new);
1342 set_state(my_id
, name
, sym
, new);
1345 void register_kernel_user_data(int id
)
1347 struct user_fn_info
*info
;
1352 if (option_project
!= PROJ_KERNEL
)
1355 set_dynamic_states(my_id
);
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("sscanf", &match_sscanf
, NULL
);
1364 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
1366 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1367 select_return_states_hook(PARAM_SET
, &db_param_set
);
1368 add_hook(&match_condition
, CONDITION_HOOK
);
1370 add_caller_info_callback(my_id
, caller_info_callback
);
1371 add_return_info_callback(my_id
, return_info_callback
);
1372 select_caller_info_hook(set_param_user_data
, USER_DATA
);
1373 select_return_states_hook(USER_DATA
, &returns_param_user_data
);
1374 select_return_states_hook(USER_DATA_SET
, &returns_param_user_data_set
);
1376 select_return_param_key(CAPPED_DATA
, &match_capped
);
1377 add_function_param_key_hook_late("memcpy", &match_capped
, 2, "$", NULL
);
1378 add_function_param_key_hook_late("_memcpy", &match_capped
, 2, "$", NULL
);
1379 add_function_param_key_hook_late("__memcpy", &match_capped
, 2, "$", NULL
);
1380 add_function_param_key_hook_late("memset", &match_capped
, 2, "$", NULL
);
1381 add_function_param_key_hook_late("_memset", &match_capped
, 2, "$", NULL
);
1382 add_function_param_key_hook_late("__memset", &match_capped
, 2, "$", NULL
);
1384 for (i
= 0; i
< ARRAY_SIZE(func_table
); i
++) {
1385 info
= &func_table
[i
];
1386 add_function_param_key_hook_late(info
->name
, &set_param_key_user_data
,
1387 info
->param
, info
->key
, info
);
1391 void register_kernel_user_data2(int id
)
1395 if (option_project
!= PROJ_KERNEL
)
1397 select_caller_info_hook(set_called
, INTERNAL
);