param_key: use get_name_sym_from_key() instead of return_state_to_var_sym()
[smatch.git] / smatch_kernel_user_data.c
blob33226ebc0cdf185c322924485c5ad5ee93649ad5
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 STATE(called);
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",
48 "kvm_register_read",
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))
72 return NULL;
74 state = alloc_estate_whole(type);
75 estate_set_new(state);
76 return 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);
87 if (!extra)
88 return;
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))
96 return;
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);
106 if (!user)
107 return;
108 rl = rl_intersection(estate_rl(user), estate_rl(state));
109 if (rl_equiv(rl, estate_rl(user)))
110 return;
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;
122 sval_t close_to_max;
124 if (!get_user_rl(expr, &rl))
125 return true;
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)
131 return false;
132 return true;
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)
141 return false;
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;
148 int comparison;
149 sval_t sval;
151 if (expr->op == '-' && get_user_rl(expr->left, &left_rl)) {
152 if (user_rl_capped(expr->left))
153 return true;
154 comparison = get_comparison(expr->left, expr->right);
155 if (comparison && show_special(comparison)[0] == '>')
156 return true;
157 return false;
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))
164 return true;
165 if (is_array_index_mask_nospec(expr->right))
166 return true;
167 if (is_capped(expr->left))
168 return true;
169 left_user = is_user_rl(expr->left);
170 right_user = is_user_rl(expr->right);
171 if (!left_user && !right_user)
172 return true;
174 left_capped = user_rl_capped(expr->left);
175 right_capped = user_rl_capped(expr->right);
177 if (left_user && left_capped) {
178 if (!right_user)
179 return true;
180 if (right_user && right_capped)
181 return true;
182 return false;
184 if (right_user && right_capped) {
185 if (!left_user)
186 return true;
187 return false;
189 return false;
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)))
204 return true;
206 return false;
209 bool user_rl_capped(struct expression *expr)
211 struct smatch_state *state;
212 struct range_list *rl;
213 sval_t sval;
215 expr = strip_expr(expr);
216 if (!expr)
217 return false;
218 if (get_value(expr, &sval))
219 return true;
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);
226 if (state)
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.
234 return true;
237 if (rl_to_sval(rl, &sval))
238 return true;
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;
247 sval_t sval;
249 expr = strip_expr(expr);
250 if (!expr)
251 return false;
252 if (get_value(expr, &sval))
253 return true;
255 state = get_state_expr(my_id, expr);
256 if (state)
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);
269 struct symbol *tmp;
271 if (member->ident)
272 expr = member_expression(expr, '.', member->ident);
274 FOR_EACH_PTR(base->symbol_list, tmp) {
275 struct symbol *type;
277 type = get_real_base_type(tmp);
278 if (!type)
279 continue;
281 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
282 tag_inner_struct_members(expr, tmp);
283 continue;
286 if (!tmp->ident)
287 continue;
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)
297 struct symbol *tmp;
298 struct expression *member;
299 int op = '*';
301 if (expr->type == EXPR_PREOP && expr->op == '&') {
302 expr = strip_expr(expr->unop);
303 op = '.';
306 FOR_EACH_PTR(type->symbol_list, tmp) {
307 type = get_real_base_type(tmp);
308 if (!type)
309 continue;
311 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
312 tag_inner_struct_members(expr, tmp);
313 continue;
316 if (!tmp->ident)
317 continue;
319 member = member_expression(expr, op, tmp->ident);
320 if (type->type == SYM_ARRAY) {
321 set_points_to_user_data(member);
322 } else {
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);
332 else
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)
339 struct symbol *type;
341 expr = strip_expr(expr);
343 type = get_type(expr);
344 if (!type || type->type != SYM_PTR)
345 return;
346 type = get_real_base_type(type);
347 if (!type)
348 return;
349 if (type == &void_ctype) {
350 set_state_expr(my_id, deref_expression(expr), new_state(&ulong_ctype));
351 return;
353 if (type->type == SYM_BASETYPE) {
354 if (expr->type != EXPR_PREOP && expr->op != '&')
355 set_points_to_user_data(expr);
356 tag_base_type(expr);
357 return;
359 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
360 if (expr->type != EXPR_PREOP || expr->op != '&')
361 expr = deref_expression(expr);
362 else
363 set_state_expr(my_id, deref_expression(expr), new_state(&ulong_ctype));
364 tag_struct_members(type, expr);
368 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
370 int param = PTR_INT(_param);
371 struct expression *dest;
373 func_gets_user_data = true;
375 dest = get_argument_from_call_expr(expr->args, param);
376 dest = strip_expr(dest);
377 if (!dest)
378 return;
379 tag_as_user_data(dest);
382 static int is_dev_attr_name(struct expression *expr)
384 char *name;
385 int ret = 0;
387 name = expr_to_str(expr);
388 if (!name)
389 return 0;
390 if (strstr(name, "->attr.name"))
391 ret = 1;
392 free_string(name);
393 return ret;
396 static int ends_in_n(struct expression *expr)
398 struct string *str;
400 if (!expr)
401 return 0;
402 if (expr->type != EXPR_STRING || !expr->string)
403 return 0;
405 str = expr->string;
406 if (str->length < 3)
407 return 0;
409 if (str->data[str->length - 3] == '%' &&
410 str->data[str->length - 2] == 'n')
411 return 1;
412 return 0;
415 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
417 struct expression *str, *format, *arg;
418 int i, last;
420 func_gets_user_data = true;
422 str = get_argument_from_call_expr(expr->args, 0);
423 if (is_dev_attr_name(str))
424 return;
426 format = get_argument_from_call_expr(expr->args, 1);
427 if (is_dev_attr_name(format))
428 return;
430 last = ptr_list_size((struct ptr_list *)expr->args) - 1;
432 i = -1;
433 FOR_EACH_PTR(expr->args, arg) {
434 i++;
435 if (i < 2)
436 continue;
437 if (i == last && ends_in_n(format))
438 continue;
439 tag_as_user_data(arg);
440 } END_FOR_EACH_PTR(arg);
443 static int get_rl_from_function(struct expression *expr, struct range_list **rl)
445 int i;
447 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL ||
448 !expr->fn->symbol_name || !expr->fn->symbol_name->name)
449 return 0;
451 for (i = 0; i < ARRAY_SIZE(returns_user_data); i++) {
452 if (strcmp(expr->fn->symbol_name->name, returns_user_data[i]) == 0) {
453 *rl = alloc_whole_rl(get_type(expr));
454 return 1;
457 return 0;
460 static int comes_from_skb_data(struct expression *expr)
462 expr = strip_expr(expr);
463 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
464 return 0;
466 expr = strip_expr(expr->unop);
467 if (!expr)
468 return 0;
469 if (expr->type == EXPR_BINOP && expr->op == '+')
470 expr = strip_expr(expr->left);
472 return is_skb_data(expr);
475 static int handle_get_user(struct expression *expr)
477 char *name;
478 int ret = 0;
480 name = get_macro_name(expr->pos);
481 if (!name || strcmp(name, "get_user") != 0)
482 return 0;
484 name = expr_to_var(expr->right);
485 if (!name || (strcmp(name, "__val_gu") != 0 && strcmp(name, "__gu_val") != 0))
486 goto free;
487 set_state_expr(my_id, expr->left, new_state(get_type(expr->left)));
488 ret = 1;
489 free:
490 free_string(name);
491 return ret;
494 static bool state_is_new(struct expression *expr)
496 struct smatch_state *state;
498 state = get_state_expr(my_id, expr);
499 if (estate_new(state))
500 return true;
502 if (expr->type == EXPR_BINOP) {
503 if (state_is_new(expr->left))
504 return true;
505 if (state_is_new(expr->right))
506 return true;
508 return false;
511 static bool handle_op_assign(struct expression *expr)
513 struct expression *binop_expr;
514 struct smatch_state *state;
515 struct range_list *rl;
517 switch (expr->op) {
518 case SPECIAL_ADD_ASSIGN:
519 case SPECIAL_SUB_ASSIGN:
520 case SPECIAL_AND_ASSIGN:
521 case SPECIAL_MOD_ASSIGN:
522 case SPECIAL_SHL_ASSIGN:
523 case SPECIAL_SHR_ASSIGN:
524 case SPECIAL_OR_ASSIGN:
525 case SPECIAL_XOR_ASSIGN:
526 case SPECIAL_MUL_ASSIGN:
527 case SPECIAL_DIV_ASSIGN:
528 binop_expr = binop_expression(expr->left,
529 op_remove_assign(expr->op),
530 expr->right);
531 if (!get_user_rl(binop_expr, &rl))
532 return true;
534 rl = cast_rl(get_type(expr->left), rl);
535 state = alloc_estate_rl(rl);
536 if (user_rl_capped(binop_expr))
537 estate_set_capped(state);
538 if (user_rl_treat_untagged(expr->left))
539 estate_set_treat_untagged(state);
540 if (state_is_new(binop_expr))
541 estate_set_new(state);
542 set_state_expr(my_id, expr->left, state);
543 return true;
545 return false;
548 static void match_assign(struct expression *expr)
550 struct symbol *left_type, *right_type;
551 struct range_list *rl;
552 static struct expression *handled;
553 struct smatch_state *state;
554 struct expression *faked;
555 bool is_capped = false;
556 bool is_new = false;
558 left_type = get_type(expr->left);
559 if (left_type == &void_ctype)
560 return;
562 faked = get_faked_expression();
564 /* FIXME: handle fake array assignments frob(&user_array[x]); */
566 if (is_fake_call(expr->right) && faked &&
567 faked->type == EXPR_ASSIGNMENT &&
568 points_to_user_data(faked->right)) {
569 if (is_skb_data(faked->right))
570 func_gets_user_data = true;
571 rl = alloc_whole_rl(get_type(expr->left));
572 is_new = true;
573 goto set;
576 if (faked && faked == handled)
577 return;
578 if (is_fake_call(expr->right))
579 goto clear_old_state;
580 if (handle_get_user(expr))
581 return;
582 if (points_to_user_data(expr->right) &&
583 is_struct_ptr(get_type(expr->left))) {
584 handled = expr;
585 // This should be handled by smatch_points_to_user_data.c
586 // set_points_to_user_data(expr->left);
589 if (handle_op_assign(expr))
590 return;
591 if (expr->op != '=')
592 goto clear_old_state;
594 /* Handled by DB code */
595 if (expr->right->type == EXPR_CALL || __in_fake_parameter_assign)
596 return;
598 if (!get_user_rl(expr->right, &rl))
599 goto clear_old_state;
600 is_capped = user_rl_capped(expr->right);
601 is_new = state_is_new(expr->right);
603 set:
604 if (type_is_ptr(left_type)) {
605 right_type = get_type(expr->right);
606 if (right_type && right_type->type == SYM_ARRAY)
607 set_points_to_user_data(expr->left);
608 return;
611 rl = cast_rl(left_type, rl);
612 state = alloc_estate_rl(rl);
613 if (is_new)
614 estate_set_new(state);
615 if (is_capped)
616 estate_set_capped(state);
617 if (user_rl_treat_untagged(expr->right))
618 estate_set_treat_untagged(state);
620 if (local_debug)
621 sm_msg("%s: left = '%s' user_state = '%s'", __func__, expr_to_str(expr->left), state->name);
622 set_state_expr(my_id, expr->left, state);
624 return;
626 clear_old_state:
629 * HACK ALERT!!! This should be at the start of the function. The
630 * the problem is that handling "pointer = array;" assignments is
631 * handled in this function instead of in kernel_points_to_user_data.c.
633 if (type_is_ptr(left_type))
634 return;
636 if (get_state_expr(my_id, expr->left))
637 set_state_expr(my_id, expr->left, alloc_estate_empty());
640 static void handle_eq_noteq(struct expression *expr)
642 struct smatch_state *left_orig, *right_orig;
644 left_orig = get_state_expr(my_id, expr->left);
645 right_orig = get_state_expr(my_id, expr->right);
647 if (!left_orig && !right_orig)
648 return;
649 if (left_orig && right_orig)
650 return;
652 if (left_orig) {
653 set_true_false_states_expr(my_id, expr->left,
654 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
655 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
656 } else {
657 set_true_false_states_expr(my_id, expr->right,
658 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
659 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
663 static struct range_list *strip_negatives(struct range_list *rl)
665 sval_t min = rl_min(rl);
666 sval_t minus_one = { .type = rl_type(rl), .value = -1 };
667 sval_t over = { .type = rl_type(rl), .value = INT_MAX + 1ULL };
668 sval_t max = sval_type_max(rl_type(rl));
670 if (!rl)
671 return NULL;
673 if (type_unsigned(rl_type(rl)) && type_bits(rl_type(rl)) > 31)
674 return remove_range(rl, over, max);
676 return remove_range(rl, min, minus_one);
679 static void handle_compare(struct expression *expr)
681 struct expression *left, *right;
682 struct range_list *left_rl = NULL;
683 struct range_list *right_rl = NULL;
684 struct range_list *user_rl;
685 struct smatch_state *capped_state;
686 struct smatch_state *left_true = NULL;
687 struct smatch_state *left_false = NULL;
688 struct smatch_state *right_true = NULL;
689 struct smatch_state *right_false = NULL;
690 struct symbol *type;
691 sval_t sval;
693 left = strip_expr(expr->left);
694 right = strip_expr(expr->right);
696 while (left->type == EXPR_ASSIGNMENT)
697 left = strip_expr(left->left);
700 * Conditions are mostly handled by smatch_extra.c, but there are some
701 * times where the exact values are not known so we can't do that.
703 * Normally, we might consider using smatch_capped.c to supliment smatch
704 * extra but that doesn't work when we merge unknown uncapped kernel
705 * data with unknown capped user data. The result is uncapped user
706 * data. We need to keep it separate and say that the user data is
707 * capped. In the past, I would have marked this as just regular
708 * kernel data (not user data) but we can't do that these days because
709 * we need to track user data for Spectre.
711 * The other situation which we have to handle is when we do have an
712 * int and we compare against an unknown unsigned kernel variable. In
713 * that situation we assume that the kernel data is less than INT_MAX.
714 * Otherwise then we get all sorts of array underflow false positives.
718 /* Handled in smatch_extra.c */
719 if (get_implied_value(left, &sval) ||
720 get_implied_value(right, &sval))
721 return;
723 get_user_rl(left, &left_rl);
724 get_user_rl(right, &right_rl);
726 /* nothing to do */
727 if (!left_rl && !right_rl)
728 return;
729 /* if both sides are user data that's not a good limit */
730 if (left_rl && right_rl)
731 return;
733 if (left_rl)
734 user_rl = left_rl;
735 else
736 user_rl = right_rl;
738 type = get_type(expr);
739 if (type_unsigned(type))
740 user_rl = strip_negatives(user_rl);
741 capped_state = alloc_estate_rl(user_rl);
742 estate_set_capped(capped_state);
744 switch (expr->op) {
745 case '<':
746 case SPECIAL_UNSIGNED_LT:
747 case SPECIAL_LTE:
748 case SPECIAL_UNSIGNED_LTE:
749 if (left_rl)
750 left_true = capped_state;
751 else
752 right_false = capped_state;
753 break;
754 case '>':
755 case SPECIAL_UNSIGNED_GT:
756 case SPECIAL_GTE:
757 case SPECIAL_UNSIGNED_GTE:
758 if (left_rl)
759 left_false = capped_state;
760 else
761 right_true = capped_state;
762 break;
765 set_true_false_states_expr(my_id, left, left_true, left_false);
766 set_true_false_states_expr(my_id, right, right_true, right_false);
769 static void match_condition(struct expression *expr)
771 if (expr->type != EXPR_COMPARE)
772 return;
774 if (expr->op == SPECIAL_EQUAL ||
775 expr->op == SPECIAL_NOTEQUAL) {
776 handle_eq_noteq(expr);
777 return;
780 handle_compare(expr);
783 static void match_returns_user_rl(const char *fn, struct expression *expr, void *unused)
785 func_gets_user_data = true;
788 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
790 struct expression *parent;
791 char *macro;
793 if (!expr)
794 return 0;
796 macro = get_macro_name(expr->pos);
797 if (!macro)
798 return 0;
800 /* handle ntohl(foo[i]) where "i" is trusted */
801 parent = expr_get_parent_expr(expr);
802 while (parent && parent->type != EXPR_BINOP)
803 parent = expr_get_parent_expr(parent);
804 if (parent && parent->type == EXPR_BINOP) {
805 char *parent_macro = get_macro_name(parent->pos);
807 if (parent_macro && strcmp(macro, parent_macro) == 0)
808 return 0;
811 if (strcmp(macro, "ntohl") == 0) {
812 *rl = alloc_whole_rl(&uint_ctype);
813 return 1;
815 if (strcmp(macro, "ntohs") == 0) {
816 *rl = alloc_whole_rl(&ushort_ctype);
817 return 1;
819 return 0;
822 static int has_user_data(struct symbol *sym)
824 struct sm_state *tmp;
826 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
827 if (tmp->sym == sym)
828 return 1;
829 } END_FOR_EACH_SM(tmp);
830 return 0;
833 bool we_pass_user_data(struct expression *call)
835 struct expression *arg;
836 struct symbol *sym;
838 FOR_EACH_PTR(call->args, arg) {
839 if (local_debug)
840 sm_msg("%s: arg = '%s' %s", __func__,
841 expr_to_str(arg),
842 points_to_user_data(arg) ? "user pointer" : "not");
843 if (points_to_user_data(arg))
844 return true;
845 sym = expr_to_sym(arg);
846 if (!sym)
847 continue;
848 if (has_user_data(sym))
849 return true;
850 } END_FOR_EACH_PTR(arg);
852 return false;
855 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
857 struct smatch_state *state;
858 char buf[48];
860 if (is_fake_call(call))
861 return 0;
862 snprintf(buf, sizeof(buf), "return %p", call);
863 state = get_state(my_id, buf, NULL);
864 if (!state || !estate_rl(state))
865 return 0;
866 *rl = estate_rl(state);
867 return 1;
870 struct stree *get_user_stree(void)
872 return get_all_states_stree(my_id);
875 static int user_data_flag;
876 static int no_user_data_flag;
877 struct range_list *var_user_rl(struct expression *expr)
879 struct smatch_state *state;
880 struct range_list *rl;
881 struct range_list *absolute_rl;
883 if (expr->type == EXPR_PREOP && expr->op == '&') {
884 no_user_data_flag = 1;
885 return NULL;
888 if (expr->type == EXPR_BINOP && expr->op == '%') {
889 struct range_list *left, *right;
891 if (!get_user_rl(expr->right, &right))
892 return NULL;
893 get_absolute_rl(expr->left, &left);
894 rl = rl_binop(left, '%', right);
895 goto found;
898 if (expr->type == EXPR_BINOP && expr->op == '/') {
899 struct range_list *left = NULL;
900 struct range_list *right = NULL;
901 struct range_list *abs_right;
904 * The specific bug I'm dealing with is:
906 * foo = capped_user / unknown;
908 * Instead of just saying foo is now entirely user_rl we should
909 * probably say instead that it is not at all user data.
913 get_user_rl(expr->left, &left);
914 get_user_rl(expr->right, &right);
915 get_absolute_rl(expr->right, &abs_right);
917 if (left && !right) {
918 rl = rl_binop(left, '/', abs_right);
919 if (sval_cmp(rl_max(left), rl_max(rl)) < 0)
920 no_user_data_flag = 1;
923 return NULL;
926 if (get_rl_from_function(expr, &rl))
927 goto found;
929 if (get_user_macro_rl(expr, &rl))
930 goto found;
932 if (comes_from_skb_data(expr)) {
933 rl = alloc_whole_rl(get_type(expr));
934 goto found;
937 state = get_state_expr(my_id, expr);
938 if (state && estate_rl(state)) {
939 rl = estate_rl(state);
940 goto found;
943 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
944 goto found;
946 if (expr->type == EXPR_PREOP && expr->op == '*' &&
947 points_to_user_data(expr->unop)) {
948 rl = var_to_absolute_rl(expr);
949 goto found;
952 if (is_array(expr)) {
953 struct expression *array = get_array_base(expr);
955 if (!get_state_expr(my_id, array)) {
956 no_user_data_flag = 1;
957 return NULL;
961 return NULL;
962 found:
963 user_data_flag = 1;
964 absolute_rl = var_to_absolute_rl(expr);
965 return clone_rl(rl_intersection(rl, absolute_rl));
968 static bool is_ptr_subtract(struct expression *expr)
970 expr = strip_expr(expr);
971 if (!expr)
972 return false;
973 if (expr->type == EXPR_BINOP && expr->op == '-' &&
974 type_is_ptr(get_type(expr->left))) {
975 return true;
977 return false;
980 int get_user_rl(struct expression *expr, struct range_list **rl)
982 if (is_ptr_subtract(expr))
983 return 0;
985 user_data_flag = 0;
986 no_user_data_flag = 0;
987 custom_get_absolute_rl(expr, &var_user_rl, rl);
988 if (!user_data_flag || no_user_data_flag)
989 *rl = NULL;
991 return !!*rl;
994 int is_user_rl(struct expression *expr)
996 struct range_list *tmp;
998 return get_user_rl(expr, &tmp) && tmp;
1001 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
1003 struct smatch_state *state;
1005 state = get_state(my_id, name, sym);
1006 if (state && estate_rl(state)) {
1007 *rl = estate_rl(state);
1008 return 1;
1010 return 0;
1013 static void return_info_callback(int return_id, char *return_ranges,
1014 struct expression *returned_expr,
1015 int param,
1016 const char *printed_name,
1017 struct sm_state *sm)
1019 struct smatch_state *extra;
1020 struct range_list *rl;
1021 char buf[64];
1023 if (param >= 0) {
1024 if (strcmp(printed_name, "$") == 0)
1025 return;
1026 if (!param_was_set_var_sym(sm->name, sm->sym))
1027 return;
1029 rl = estate_rl(sm->state);
1030 if (!rl)
1031 return;
1032 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
1033 if (estate_rl(extra))
1034 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
1035 if (!rl)
1036 return;
1038 snprintf(buf, sizeof(buf), "%s%s%s",
1039 show_rl(rl),
1040 estate_capped(sm->state) ? "[c]" : "",
1041 estate_treat_untagged(sm->state) ? "[u]" : "");
1042 sql_insert_return_states(return_id, return_ranges,
1043 estate_new(sm->state) ? USER_DATA_SET : USER_DATA,
1044 param, printed_name, buf);
1047 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1049 struct smatch_state *state;
1050 struct range_list *rl;
1051 struct symbol *type;
1052 char buf[64];
1054 if (local_debug)
1055 sm_msg("%s: name = '%s' sm = '%s'", __func__, printed_name, show_sm(sm));
1058 * Smatch uses a hack where if we get an unsigned long we say it's
1059 * both user data and it points to user data. But if we pass it to a
1060 * function which takes an int, then it's just user data. There's not
1061 * enough bytes for it to be a pointer.
1064 type = get_arg_type(call->fn, param);
1065 if (strcmp(printed_name, "$") != 0 && type && type_bits(type) < type_bits(&ptr_ctype))
1066 return;
1068 if (strcmp(sm->state->name, "") == 0)
1069 return;
1071 state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
1072 if (!state || !estate_rl(state))
1073 rl = estate_rl(sm->state);
1074 else
1075 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
1077 if (!rl)
1078 return;
1080 snprintf(buf, sizeof(buf), "%s%s%s", show_rl(rl),
1081 estate_capped(sm->state) ? "[c]" : "",
1082 estate_treat_untagged(sm->state) ? "[u]" : "");
1083 sql_insert_caller_info(call, USER_DATA, param, printed_name, buf);
1086 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1088 struct expression *arg;
1089 char *name;
1090 struct symbol *sym;
1091 struct smatch_state *state;
1093 while (expr->type == EXPR_ASSIGNMENT)
1094 expr = strip_expr(expr->right);
1095 if (expr->type != EXPR_CALL)
1096 return;
1098 arg = get_argument_from_call_expr(expr->args, param);
1099 if (!arg)
1100 return;
1101 name = get_variable_from_key(arg, key, &sym);
1102 if (!name || !sym)
1103 goto free;
1105 state = get_state(my_id, name, sym);
1106 if (!state)
1107 goto free;
1109 set_state(my_id, name, sym, alloc_estate_empty());
1110 free:
1111 free_string(name);
1114 static bool param_data_capped(const char *value)
1116 if (strstr(value, ",c") || strstr(value, "[c"))
1117 return true;
1118 return false;
1121 static bool param_data_treat_untagged(const char *value)
1123 if (strstr(value, ",u") || strstr(value, "[u"))
1124 return true;
1125 return false;
1128 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
1130 struct expression *expr;
1131 struct range_list *rl = NULL;
1132 struct smatch_state *state;
1133 struct symbol *type;
1134 char *fullname;
1136 expr = symbol_expression(sym);
1137 fullname = get_variable_from_key(expr, key, NULL);
1138 if (!fullname)
1139 return;
1141 type = get_member_type_from_key(expr, key);
1142 if (type && type->type == SYM_STRUCT) {
1143 sm_info("%s: user data struct. key='%s' value='%s'",
1144 __func__, key, value);
1145 return;
1148 // FIXME: This is temporary. Just run this on Thursday and then
1149 // let's make it a printf() and then delete it.
1150 if (!type) {
1151 sm_msg("%s: no type for '%s'", __func__, fullname);
1152 return;
1155 str_to_rl(type, value, &rl);
1156 rl = swap_mtag_seed(expr, rl);
1157 state = alloc_estate_rl(rl);
1158 if (param_data_capped(value) || is_capped(expr))
1159 estate_set_capped(state);
1160 if (param_data_treat_untagged(value) || sym->ctype.as == 5)
1161 estate_set_treat_untagged(state);
1162 set_state(my_id, fullname, sym, state);
1165 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
1167 set_state(my_call_id, "this_function", NULL, &called);
1170 static void match_syscall_definition(struct symbol *sym)
1172 struct symbol *arg;
1173 char *macro;
1174 char *name;
1175 int is_syscall = 0;
1177 macro = get_macro_name(sym->pos);
1178 if (macro &&
1179 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
1180 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1181 is_syscall = 1;
1183 name = get_function();
1184 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
1185 if (name && strncmp(name, "sys_", 4) == 0)
1186 is_syscall = 1;
1189 if (name && strncmp(name, "compat_sys_", 11) == 0)
1190 is_syscall = 1;
1192 if (!is_syscall)
1193 return;
1195 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
1196 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
1197 } END_FOR_EACH_PTR(arg);
1200 #define OLD 0
1201 #define NEW 1
1203 static void store_user_data_return(struct expression *expr, char *key, char *value, bool is_new)
1205 struct smatch_state *state;
1206 struct range_list *rl;
1207 struct symbol *type;
1208 char buf[48];
1210 if (key[0] != '$')
1211 return;
1213 type = get_type(expr);
1214 snprintf(buf, sizeof(buf), "return %p%s", expr, key + 1);
1215 call_results_to_rl(expr, type, value, &rl);
1217 state = alloc_estate_rl(rl);
1218 if (is_new)
1219 estate_set_new(state);
1221 set_state(my_id, buf, NULL, state);
1224 static void set_to_user_data(struct expression *expr, char *key, char *value, bool is_new)
1226 struct smatch_state *state;
1227 char *name;
1228 struct symbol *sym;
1229 struct symbol *type;
1230 struct range_list *rl = NULL;
1232 type = get_member_type_from_key(expr, key);
1233 name = get_variable_from_key(expr, key, &sym);
1234 if (!name || !sym)
1235 goto free;
1237 call_results_to_rl(expr, type, value, &rl);
1239 state = alloc_estate_rl(rl);
1240 if (param_data_capped(value))
1241 estate_set_capped(state);
1242 if (param_data_treat_untagged(value))
1243 estate_set_treat_untagged(state);
1244 if (is_new)
1245 estate_set_new(state);
1246 set_state(my_id, name, sym, state);
1247 free:
1248 free_string(name);
1251 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
1253 struct expression *arg;
1254 struct expression *call;
1256 call = expr;
1257 while (call->type == EXPR_ASSIGNMENT)
1258 call = strip_expr(call->right);
1259 if (call->type != EXPR_CALL)
1260 return;
1262 if (!we_pass_user_data(call))
1263 return;
1265 if (param == -1) {
1266 if (expr->type != EXPR_ASSIGNMENT) {
1267 store_user_data_return(expr, key, value, OLD);
1268 return;
1270 set_to_user_data(expr->left, key, value, OLD);
1271 return;
1274 arg = get_argument_from_call_expr(call->args, param);
1275 if (!arg)
1276 return;
1277 set_to_user_data(arg, key, value, OLD);
1280 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
1282 struct expression *arg;
1284 func_gets_user_data = true;
1286 if (param == -1) {
1287 if (expr->type != EXPR_ASSIGNMENT) {
1288 store_user_data_return(expr, key, value, NEW);
1289 return;
1291 set_to_user_data(expr->left, key, value, NEW);
1292 return;
1295 while (expr->type == EXPR_ASSIGNMENT)
1296 expr = strip_expr(expr->right);
1297 if (expr->type != EXPR_CALL)
1298 return;
1300 arg = get_argument_from_call_expr(expr->args, param);
1301 if (!arg)
1302 return;
1303 set_to_user_data(arg, key, value, NEW);
1306 static void returns_param_capped(struct expression *expr, int param, char *key, char *value)
1308 struct smatch_state *state, *new;
1309 struct symbol *sym;
1310 char *name;
1312 name = get_name_sym_from_key(expr, param, key, &sym);
1313 if (!name || !sym)
1314 goto free;
1316 state = get_state(my_id, name, sym);
1317 if (!state || estate_capped(state))
1318 goto free;
1320 new = clone_estate(state);
1321 estate_set_capped(new);
1323 set_state(my_id, name, sym, new);
1324 free:
1325 free_string(name);
1328 static void match_function_def(struct symbol *sym)
1330 if (is_user_data_fn(sym))
1331 func_gets_user_data = true;
1334 void register_kernel_user_data(int id)
1336 int i;
1338 my_id = id;
1340 if (option_project != PROJ_KERNEL)
1341 return;
1343 set_dynamic_states(my_id);
1345 add_function_data(&func_gets_user_data);
1346 add_hook(&match_function_def, FUNC_DEF_HOOK);
1348 add_hook(&save_start_states, AFTER_DEF_HOOK);
1349 add_hook(&free_start_states, AFTER_FUNC_HOOK);
1350 add_function_data((unsigned long *)&start_states);
1352 add_unmatched_state_hook(my_id, &empty_state);
1353 add_extra_nomod_hook(&extra_nomod_hook);
1354 add_pre_merge_hook(my_id, &pre_merge_hook);
1355 add_merge_hook(my_id, &merge_estates);
1357 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
1358 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
1359 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1360 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1361 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1362 add_function_hook("usb_control_msg", &match_user_copy, INT_PTR(6));
1364 for (i = 0; i < ARRAY_SIZE(returns_user_data); i++)
1365 add_function_hook(returns_user_data[i], &match_returns_user_rl, NULL);
1367 add_function_hook("sscanf", &match_sscanf, NULL);
1369 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1371 add_hook(&match_assign, ASSIGNMENT_HOOK);
1372 select_return_states_hook(PARAM_SET, &db_param_set);
1373 add_hook(&match_condition, CONDITION_HOOK);
1375 add_caller_info_callback(my_id, caller_info_callback);
1376 add_return_info_callback(my_id, return_info_callback);
1377 select_caller_info_hook(set_param_user_data, USER_DATA);
1378 select_return_states_hook(USER_DATA, &returns_param_user_data);
1379 select_return_states_hook(USER_DATA_SET, &returns_param_user_data_set);
1380 select_return_states_hook(CAPPED_DATA, &returns_param_capped);
1383 void register_kernel_user_data2(int id)
1385 my_call_id = id;
1387 if (option_project != PROJ_KERNEL)
1388 return;
1389 select_caller_info_hook(set_called, INTERNAL);