Merge https://git.kernel.org/pub/scm/devel/sparse/sparse into devel
[smatch.git] / smatch_kernel_user_data.c
blob6ebf1ed16cda0cb479a5863bdf85dd104ad34b10
1 /*
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.
25 #include "smatch.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_id;
30 static int my_call_id;
32 static struct expression *ignore_clear;
34 STATE(called);
36 struct user_fn_info {
37 const char *name;
38 int type;
39 int param;
40 const char *key;
41 const sval_t *implies_start, *implies_end;
42 func_hook *call_back;
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))
64 return NULL;
66 state = alloc_estate_whole(type);
67 estate_set_new(state);
68 return 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);
79 if (!extra)
80 return;
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))
88 return;
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);
100 if (!user)
101 return;
102 rl = rl_intersection(estate_rl(user), estate_rl(state));
103 if (rl_equiv(rl, estate_rl(user)))
104 return;
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)
115 struct symbol *type;
116 char *type_str, *member;
118 if (__in_fake_assign)
119 return;
121 if (!estate_rl(state))
122 return;
124 expr = strip_expr(expr);
125 if (!expr || expr->type != EXPR_DEREF || !expr->member)
126 return;
128 type = get_type(expr->deref);
129 if (!type || !type->ident)
130 return;
132 type_str = type_to_str(type);
133 if (!type_str)
134 return;
135 member = get_member_name(expr);
136 if (!member)
137 return;
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;
151 sval_t close_to_max;
153 if (!get_user_rl(expr, &rl))
154 return true;
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)
160 return false;
161 return true;
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)
170 return false;
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;
177 int comparison;
178 sval_t sval;
180 if (expr->op == '-' && get_user_rl(expr->left, &left_rl)) {
181 if (user_rl_capped(expr->left))
182 return true;
183 comparison = get_comparison(expr->left, expr->right);
184 if (comparison && show_special(comparison)[0] == '>')
185 return true;
186 return false;
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))
193 return true;
194 if (is_array_index_mask_nospec(expr->right))
195 return true;
196 if (is_capped(expr->left))
197 return true;
198 left_user = is_user_rl(expr->left);
199 right_user = is_user_rl(expr->right);
200 if (!left_user && !right_user)
201 return true;
203 left_capped = user_rl_capped(expr->left);
204 right_capped = user_rl_capped(expr->right);
206 if (left_user && left_capped) {
207 if (!right_user)
208 return true;
209 if (right_user && right_capped)
210 return true;
211 return false;
213 if (right_user && right_capped) {
214 if (!left_user)
215 return true;
216 return false;
218 return false;
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)))
233 return true;
235 return false;
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);
243 if (state)
244 return estate_capped(state);
246 return true;
249 bool user_rl_capped(struct expression *expr)
251 struct smatch_state *state;
252 struct range_list *rl;
253 sval_t sval;
255 expr = strip_expr(expr);
256 if (!expr)
257 return false;
258 if (get_value(expr, &sval))
259 return true;
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);
266 if (state)
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.
274 return true;
277 if (rl_to_sval(rl, &sval))
278 return true;
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;
287 sval_t sval;
289 expr = strip_expr(expr);
290 if (!expr)
291 return false;
292 if (get_value(expr, &sval))
293 return true;
295 state = get_state_expr(my_id, expr);
296 if (state)
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)
307 char *name;
308 int ret = 0;
310 name = expr_to_str(expr);
311 if (!name)
312 return 0;
313 if (strstr(name, "->attr.name"))
314 ret = 1;
315 free_string(name);
316 return ret;
319 static bool is_percent_n(struct expression *expr, int pos)
321 char *p;
322 int cnt = 0;
324 if (!expr)
325 return false;
326 if (expr->type != EXPR_STRING || !expr->string)
327 return false;
329 p = expr->string->data;
330 while (*p) {
331 if (p[0] != '%' ||
332 (p[0] == '%' && p[1] == '%')) {
333 p++;
334 continue;
336 if (pos != cnt) {
337 cnt++;
338 p++;
339 continue;
341 if (p[1] == 'n')
342 return true;
343 return false;
346 return false;
349 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
351 struct expression *str, *format, *arg;
352 int i;
354 str = get_argument_from_call_expr(expr->args, 0);
355 if (is_dev_attr_name(str))
356 return;
358 format = get_argument_from_call_expr(expr->args, 1);
359 if (is_dev_attr_name(format))
360 return;
362 i = -1;
363 FOR_EACH_PTR(expr->args, arg) {
364 i++;
365 if (i < 2)
366 continue;
367 if (is_percent_n(format, i - 2))
368 continue;
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 != '*')
377 return 0;
379 expr = strip_expr(expr->unop);
380 if (!expr)
381 return 0;
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)
390 char *name;
391 int ret = 0;
393 name = get_macro_name(expr->pos);
394 if (!name || strcmp(name, "get_user") != 0)
395 return 0;
397 name = expr_to_var(expr->right);
398 if (!name || (strcmp(name, "__val_gu") != 0 && strcmp(name, "__gu_val") != 0))
399 goto free;
400 set_user_data(expr->left, new_state(get_type(expr->left)));
401 ret = 1;
402 free:
403 free_string(name);
404 return ret;
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))
413 return true;
415 if (expr->type == EXPR_BINOP) {
416 if (state_is_new(expr->left))
417 return true;
418 if (state_is_new(expr->right))
419 return true;
421 return false;
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));
431 if (!rl)
432 return NULL;
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;
446 switch (expr->op) {
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),
459 expr->right);
460 if (!get_user_rl(binop_expr, &rl))
461 return true;
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);
475 return true;
477 return false;
480 static void handle_derefed_pointers(struct expression *expr, bool is_new)
482 expr = strip_expr(expr);
483 if (expr->type != EXPR_PREOP ||
484 expr->op != '*')
485 return;
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;
498 bool is_new = false;
500 left_type = get_type(expr->left);
501 if (left_type == &void_ctype)
502 return;
504 faked = get_faked_expression();
505 if (faked && faked == ignore_clear)
506 return;
508 /* FIXME: handle fake array assignments frob(&user_array[x]); */
510 if (faked &&
511 faked->type == EXPR_ASSIGNMENT &&
512 points_to_user_data(faked->right)) {
513 if (is_skb_data(faked->right))
514 is_new = true;
515 rl = alloc_whole_rl(left_type);
516 goto set;
519 if (faked && faked == handled)
520 return;
521 if (is_fake_call(expr->right))
522 goto clear_old_state;
523 if (handle_get_user(expr))
524 return;
525 if (points_to_user_data(expr->right) &&
526 is_struct_ptr(get_type(expr->left))) {
527 handled = expr;
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))
533 return;
534 if (expr->op != '=')
535 goto clear_old_state;
537 /* Handled by DB code */
538 if (expr->right->type == EXPR_CALL)
539 return;
541 if (faked)
542 disable_type_val_lookups();
543 get_user_rl(expr->right, &rl);
544 if (faked)
545 enable_type_val_lookups();
546 if (!rl)
547 goto clear_old_state;
549 is_capped = user_rl_capped(expr->right);
550 is_new = state_is_new(expr->right);
552 set:
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);
557 return;
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);
564 if (is_new)
565 estate_set_new(state);
566 if (is_capped)
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);
574 return;
576 clear_old_state:
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))
584 return;
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)
598 return;
599 if (left_orig && right_orig)
600 return;
602 if (left_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());
606 } else {
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;
624 struct symbol *type;
625 sval_t sval;
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))
655 return;
657 get_user_rl(left, &left_rl);
658 get_user_rl(right, &right_rl);
660 /* nothing to do */
661 if (!left_rl && !right_rl)
662 return;
663 /* if both sides are user data that's not a good limit */
664 if (left_rl && right_rl)
665 return;
667 if (left_rl)
668 user_rl = left_rl;
669 else
670 user_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);
678 switch (expr->op) {
679 case '<':
680 case SPECIAL_UNSIGNED_LT:
681 case SPECIAL_LTE:
682 case SPECIAL_UNSIGNED_LTE:
683 if (left_rl)
684 left_true = capped_state;
685 else
686 right_false = capped_state;
687 break;
688 case '>':
689 case SPECIAL_UNSIGNED_GT:
690 case SPECIAL_GTE:
691 case SPECIAL_UNSIGNED_GTE:
692 if (left_rl)
693 left_false = capped_state;
694 else
695 right_true = capped_state;
696 break;
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)
706 return;
708 if (expr->op == SPECIAL_EQUAL ||
709 expr->op == SPECIAL_NOTEQUAL) {
710 handle_eq_noteq(expr);
711 return;
714 handle_compare(expr);
717 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
719 struct expression *parent;
720 char *macro;
722 if (!expr)
723 return 0;
725 macro = get_macro_name(expr->pos);
726 if (!macro)
727 return 0;
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)
737 return 0;
740 if (strcmp(macro, "ntohl") == 0) {
741 *rl = alloc_whole_rl(&uint_ctype);
742 return 1;
744 if (strcmp(macro, "ntohs") == 0) {
745 *rl = alloc_whole_rl(&ushort_ctype);
746 return 1;
748 return 0;
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) {
756 if (tmp->sym == sym)
757 return 1;
758 } END_FOR_EACH_SM(tmp);
759 return 0;
762 bool we_pass_user_data(struct expression *call)
764 struct expression *arg;
765 struct symbol *sym;
767 FOR_EACH_PTR(call->args, arg) {
768 if (points_to_user_data(arg))
769 return true;
770 sym = expr_to_sym(arg);
771 if (!sym)
772 continue;
773 if (has_user_data(sym))
774 return true;
775 } END_FOR_EACH_PTR(arg);
777 return false;
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;
784 char buf[48];
786 if (is_fake_call(call))
787 return 0;
788 snprintf(buf, sizeof(buf), "return %p", call);
789 state = get_state(my_id, buf, NULL);
790 if (!state || !estate_rl(state))
791 return 0;
792 *rl = estate_rl(state);
793 return 1;
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)
805 int num;
807 num = pop_int(stack);
808 num = 1;
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);
820 return NULL;
823 if (expr->type == EXPR_BINOP && expr->op == '%') {
824 struct range_list *left, *right;
826 if (!get_user_rl(expr->right, &right))
827 return NULL;
828 get_absolute_rl(expr->left, &left);
829 rl = rl_binop(left, '%', right);
830 goto found;
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);
858 return NULL;
861 if (get_user_macro_rl(expr, &rl))
862 goto found;
864 if (comes_from_skb_data(expr)) {
865 rl = alloc_whole_rl(get_type(expr));
866 goto found;
869 state = get_state_expr(my_id, expr);
870 if (state && estate_rl(state)) {
871 rl = estate_rl(state);
872 goto found;
875 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
876 goto found;
878 if (expr->type == EXPR_PREOP && expr->op == '*' &&
879 points_to_user_data(expr->unop)) {
880 rl = var_to_absolute_rl(expr);
881 goto found;
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);
889 return NULL;
893 return NULL;
894 found:
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);
903 if (!expr)
904 return false;
905 if (expr->type == EXPR_BINOP && expr->op == '-' &&
906 type_is_ptr(get_type(expr->left))) {
907 return true;
909 return false;
912 int get_user_rl(struct expression *expr, struct range_list **rl)
914 int user_data, no_user_data;
916 if (!expr)
917 return 0;
919 if (__in_fake_struct_assign &&
920 !has_states(__get_cur_stree(), my_id))
921 return 0;
923 if (is_ptr_subtract(expr))
924 return 0;
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)
935 *rl = NULL;
937 return !!*rl;
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))
953 return 0;
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));
960 return 1;
963 bool is_socket_stuff(struct symbol *sym)
965 struct symbol *type;
967 /* This is a hack.
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)
984 return false;
985 type = get_real_base_type(type);
986 if (!type || type->type != SYM_STRUCT || !type->ident)
987 return false;
989 if (strcmp(type->ident->name, "sk_buff") == 0)
990 return true;
991 if (strcmp(type->ident->name, "sock") == 0)
992 return true;
993 if (strcmp(type->ident->name, "socket") == 0)
994 return true;
996 return false;
999 static void return_info_callback(int return_id, char *return_ranges,
1000 struct expression *returned_expr,
1001 int param,
1002 const char *printed_name,
1003 struct sm_state *sm)
1005 struct smatch_state *extra;
1006 struct range_list *rl;
1007 char buf[64];
1009 if (is_socket_stuff(sm->sym))
1010 return;
1011 if (is_ignored_kernel_data(printed_name))
1012 return;
1014 if (param >= 0) {
1015 if (strcmp(printed_name, "$") == 0)
1016 return;
1017 if (!param_was_set_var_sym(sm->name, sm->sym))
1018 return;
1019 if (!estate_assigned(sm->state) &&
1020 !estate_new(sm->state))
1021 return;
1023 rl = estate_rl(sm->state);
1024 if (!rl)
1025 return;
1026 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
1027 if (estate_rl(extra))
1028 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
1029 if (!rl)
1030 return;
1032 snprintf(buf, sizeof(buf), "%s%s%s",
1033 show_rl(rl),
1034 estate_capped(sm->state) ? "[c]" : "",
1035 estate_treat_untagged(sm->state) ? "[u]" : "");
1036 sql_insert_return_states(return_id, return_ranges,
1037 estate_new(sm->state) ? USER_DATA_SET : USER_DATA,
1038 param, printed_name, buf);
1041 static bool is_ignored_macro(struct position pos)
1043 const char *macro;
1045 macro = get_macro_name(pos);
1046 if (!macro)
1047 return false;
1048 if (strcmp(macro, "v4l2_subdev_call") == 0)
1049 return true;
1050 return false;
1053 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1055 struct smatch_state *state;
1056 struct range_list *rl;
1057 struct symbol *type;
1058 char buf[64];
1060 if (is_ignored_macro(call->pos))
1061 return;
1063 if (is_socket_stuff(sm->sym))
1064 return;
1065 if (is_ignored_kernel_data(printed_name))
1066 return;
1069 * Smatch uses a hack where if we get an unsigned long we say it's
1070 * both user data and it points to user data. But if we pass it to a
1071 * function which takes an int, then it's just user data. There's not
1072 * enough bytes for it to be a pointer.
1075 type = get_arg_type(call->fn, param);
1076 if (strcmp(printed_name, "$") != 0 && type && type_bits(type) < type_bits(&ptr_ctype))
1077 return;
1079 if (strcmp(sm->state->name, "") == 0)
1080 return;
1082 state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
1083 if (!state || !estate_rl(state))
1084 rl = estate_rl(sm->state);
1085 else
1086 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
1088 if (!rl)
1089 return;
1091 snprintf(buf, sizeof(buf), "%s%s%s", show_rl(rl),
1092 estate_capped(sm->state) ? "[c]" : "",
1093 estate_treat_untagged(sm->state) ? "[u]" : "");
1094 sql_insert_caller_info(call, USER_DATA, param, printed_name, buf);
1097 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1099 struct expression *arg;
1100 char *name;
1101 struct symbol *sym;
1102 struct smatch_state *state;
1104 while (expr->type == EXPR_ASSIGNMENT)
1105 expr = strip_expr(expr->right);
1106 if (expr->type != EXPR_CALL)
1107 return;
1108 if (expr == ignore_clear)
1109 return;
1111 arg = get_argument_from_call_expr(expr->args, param);
1112 if (!arg)
1113 return;
1114 name = get_variable_from_key(arg, key, &sym);
1115 if (!name || !sym)
1116 goto free;
1118 state = get_state(my_id, name, sym);
1119 if (!state)
1120 goto free;
1122 set_state(my_id, name, sym, alloc_estate_empty());
1123 free:
1124 free_string(name);
1127 static bool param_data_capped(const char *value)
1129 if (strstr(value, ",c") || strstr(value, "[c"))
1130 return true;
1131 return false;
1134 static bool param_data_treat_untagged(const char *value)
1136 if (strstr(value, ",u") || strstr(value, "[u"))
1137 return true;
1138 return false;
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;
1147 char *fullname;
1149 expr = symbol_expression(sym);
1150 fullname = get_variable_from_key(expr, key, NULL);
1151 if (!fullname)
1152 return;
1154 type = get_member_type_from_key(expr, key);
1155 if (type && type->type == SYM_STRUCT)
1156 return;
1158 if (!type)
1159 return;
1161 str_to_rl(type, value, &rl);
1162 rl = swap_mtag_seed(expr, rl);
1163 state = alloc_estate_rl(rl);
1164 if (param_data_capped(value) || is_capped(expr))
1165 estate_set_capped(state);
1166 if (param_data_treat_untagged(value) || sym->ctype.as == 5)
1167 estate_set_treat_untagged(state);
1168 set_state(my_id, fullname, sym, state);
1171 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
1173 set_state(my_call_id, "this_function", NULL, &called);
1176 static void match_syscall_definition(struct symbol *sym)
1178 struct symbol *arg;
1179 char *macro;
1180 char *name;
1181 int is_syscall = 0;
1183 macro = get_macro_name(sym->pos);
1184 if (macro &&
1185 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
1186 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1187 is_syscall = 1;
1189 name = get_function();
1190 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
1191 if (name && strncmp(name, "sys_", 4) == 0)
1192 is_syscall = 1;
1195 if (name && strncmp(name, "compat_sys_", 11) == 0)
1196 is_syscall = 1;
1198 if (!is_syscall)
1199 return;
1201 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
1202 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
1203 } END_FOR_EACH_PTR(arg);
1206 #define OLD 0
1207 #define NEW 1
1209 static void store_user_data_return(struct expression *expr, char *key, char *value, bool is_new)
1211 struct smatch_state *state;
1212 struct range_list *rl;
1213 struct symbol *type;
1214 char buf[48];
1216 if (key[0] != '$')
1217 return;
1219 type = get_type(expr);
1220 snprintf(buf, sizeof(buf), "return %p%s", expr, key + 1);
1221 call_results_to_rl(expr, type, value, &rl);
1223 state = alloc_estate_rl(rl);
1224 if (is_new)
1225 estate_set_new(state);
1227 set_state(my_id, buf, NULL, state);
1230 // FIXME: not a fan of this name, would prefer set_to_user_data() but that's
1231 // already used.
1232 void mark_as_user_data(struct expression *expr, bool isnew)
1234 struct smatch_state *state;
1236 state = alloc_estate_whole(get_type(expr));
1237 if (isnew)
1238 estate_set_new(state);
1239 set_state_expr(my_id, expr, state);
1242 static void set_to_user_data(struct expression *expr, char *key, char *value, bool is_new)
1244 struct smatch_state *state;
1245 char *name;
1246 struct symbol *sym;
1247 struct symbol *type;
1248 struct range_list *rl = NULL;
1250 type = get_member_type_from_key(expr, key);
1251 name = get_variable_from_key(expr, key, &sym);
1252 if (!name || !sym)
1253 goto free;
1255 call_results_to_rl(expr, type, value, &rl);
1257 state = alloc_estate_rl(rl);
1258 if (param_data_capped(value))
1259 estate_set_capped(state);
1260 if (param_data_treat_untagged(value))
1261 estate_set_treat_untagged(state);
1262 if (is_new)
1263 estate_set_new(state);
1264 estate_set_assigned(state);
1265 set_state(my_id, name, sym, state);
1266 free:
1267 free_string(name);
1270 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
1272 struct expression *arg;
1273 struct expression *call;
1275 call = expr;
1276 while (call->type == EXPR_ASSIGNMENT)
1277 call = strip_expr(call->right);
1278 if (call->type != EXPR_CALL)
1279 return;
1281 if (!we_pass_user_data(call))
1282 return;
1284 if (param == -1) {
1285 if (expr->type != EXPR_ASSIGNMENT) {
1286 // TODO: faked_assign this should all be handled as a fake assignment
1287 store_user_data_return(expr, key, value, OLD);
1288 return;
1290 set_to_user_data(expr->left, key, value, OLD);
1291 return;
1294 arg = get_argument_from_call_expr(call->args, param);
1295 if (!arg)
1296 return;
1297 set_to_user_data(arg, key, value, OLD);
1300 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
1302 struct expression *arg;
1304 if (param == -1) {
1305 if (expr->type != EXPR_ASSIGNMENT) {
1306 store_user_data_return(expr, key, value, NEW);
1307 return;
1309 set_to_user_data(expr->left, key, value, NEW);
1310 return;
1313 while (expr->type == EXPR_ASSIGNMENT)
1314 expr = strip_expr(expr->right);
1315 if (expr->type != EXPR_CALL)
1316 return;
1318 arg = get_argument_from_call_expr(expr->args, param);
1319 if (!arg)
1320 return;
1321 set_to_user_data(arg, key, value, NEW);
1324 static void set_param_key_user_data(struct expression *expr, const char *name,
1325 struct symbol *sym, void *data)
1327 struct expression *arg;
1329 arg = gen_expression_from_name_sym(name, sym);
1330 set_state_expr(my_id, arg, new_state(get_type(arg)));
1333 static void match_capped(struct expression *expr, const char *name, struct symbol *sym, void *info)
1335 struct smatch_state *state, *new;
1337 state = get_state(my_id, name, sym);
1338 if (!state || estate_capped(state))
1339 return;
1341 new = clone_estate(state);
1342 estate_set_capped(new);
1344 set_state(my_id, name, sym, new);
1347 void register_kernel_user_data(int id)
1349 struct user_fn_info *info;
1350 int i;
1352 my_id = id;
1354 if (option_project != PROJ_KERNEL)
1355 return;
1357 set_dynamic_states(my_id);
1359 add_unmatched_state_hook(my_id, &empty_state);
1360 add_extra_nomod_hook(&extra_nomod_hook);
1361 add_pre_merge_hook(my_id, &pre_merge_hook);
1362 add_merge_hook(my_id, &merge_estates);
1364 add_function_hook("sscanf", &match_sscanf, NULL);
1366 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1368 add_hook(&match_assign, ASSIGNMENT_HOOK);
1369 select_return_states_hook(PARAM_SET, &db_param_set);
1370 add_hook(&match_condition, CONDITION_HOOK);
1372 add_caller_info_callback(my_id, caller_info_callback);
1373 add_return_info_callback(my_id, return_info_callback);
1374 select_caller_info_hook(set_param_user_data, USER_DATA);
1375 select_return_states_hook(USER_DATA, &returns_param_user_data);
1376 select_return_states_hook(USER_DATA_SET, &returns_param_user_data_set);
1378 select_return_param_key(CAPPED_DATA, &match_capped);
1379 add_function_param_key_hook_late("memcpy", &match_capped, 2, "$", NULL);
1380 add_function_param_key_hook_late("_memcpy", &match_capped, 2, "$", NULL);
1381 add_function_param_key_hook_late("__memcpy", &match_capped, 2, "$", NULL);
1382 add_function_param_key_hook_late("memset", &match_capped, 2, "$", NULL);
1383 add_function_param_key_hook_late("_memset", &match_capped, 2, "$", NULL);
1384 add_function_param_key_hook_late("__memset", &match_capped, 2, "$", NULL);
1386 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
1387 info = &func_table[i];
1388 add_function_param_key_hook_late(info->name, &set_param_key_user_data,
1389 info->param, info->key, info);
1393 void register_kernel_user_data2(int id)
1395 my_call_id = id;
1397 if (option_project != PROJ_KERNEL)
1398 return;
1399 select_caller_info_hook(set_called, INTERNAL);