rosenberg: handle bit fields better
[smatch.git] / smatch_kernel_user_data.c
blob03225755d9f46b330d4aa44963e004a836d9bf5c
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 void store_type_info(struct expression *expr, struct smatch_state *state)
121 struct symbol *type;
122 char *type_str, *member;
124 if (__in_fake_assign)
125 return;
127 if (!estate_rl(state))
128 return;
130 expr = strip_expr(expr);
131 if (!expr || expr->type != EXPR_DEREF || !expr->member)
132 return;
134 type = get_type(expr->deref);
135 if (!type || !type->ident)
136 return;
138 type_str = type_to_str(type);
139 if (!type_str)
140 return;
141 member = get_member_name(expr);
142 if (!member)
143 return;
145 sql_insert_function_type_info(USER_DATA, type_str, member, state->name);
148 static void set_user_data(struct expression *expr, struct smatch_state *state)
150 store_type_info(expr, state);
151 set_state_expr(my_id, expr, state);
154 static bool user_rl_known(struct expression *expr)
156 struct range_list *rl;
157 sval_t close_to_max;
159 if (!get_user_rl(expr, &rl))
160 return true;
162 close_to_max = sval_type_max(rl_type(rl));
163 close_to_max.value -= 100;
165 if (sval_cmp(rl_max(rl), close_to_max) >= 0)
166 return false;
167 return true;
170 static bool is_array_index_mask_nospec(struct expression *expr)
172 struct expression *orig;
174 orig = get_assigned_expr(expr);
175 if (!orig || orig->type != EXPR_CALL)
176 return false;
177 return sym_name_is("array_index_mask_nospec", orig->fn);
180 static bool binop_capped(struct expression *expr)
182 struct range_list *left_rl;
183 int comparison;
184 sval_t sval;
186 if (expr->op == '-' && get_user_rl(expr->left, &left_rl)) {
187 if (user_rl_capped(expr->left))
188 return true;
189 comparison = get_comparison(expr->left, expr->right);
190 if (comparison && show_special(comparison)[0] == '>')
191 return true;
192 return false;
195 if (expr->op == '&' || expr->op == '%') {
196 bool left_user, left_capped, right_user, right_capped;
198 if (!get_value(expr->right, &sval) && is_capped(expr->right))
199 return true;
200 if (is_array_index_mask_nospec(expr->right))
201 return true;
202 if (is_capped(expr->left))
203 return true;
204 left_user = is_user_rl(expr->left);
205 right_user = is_user_rl(expr->right);
206 if (!left_user && !right_user)
207 return true;
209 left_capped = user_rl_capped(expr->left);
210 right_capped = user_rl_capped(expr->right);
212 if (left_user && left_capped) {
213 if (!right_user)
214 return true;
215 if (right_user && right_capped)
216 return true;
217 return false;
219 if (right_user && right_capped) {
220 if (!left_user)
221 return true;
222 return false;
224 return false;
228 * Generally "capped" means that we capped it to an unknown value.
229 * This is useful because if Smatch doesn't know what the value is then
230 * we have to trust that it is correct. But if we known cap value is
231 * 100 then we can check if 100 is correct and complain if it's wrong.
233 * So then the problem is with BINOP when we take a capped variable
234 * plus a user variable which is clamped to a known range (uncapped)
235 * the result should be capped.
237 if ((user_rl_capped(expr->left) || user_rl_known(expr->left)) &&
238 (user_rl_capped(expr->right) || user_rl_known(expr->right)))
239 return true;
241 return false;
244 bool user_rl_capped(struct expression *expr)
246 struct smatch_state *state;
247 struct range_list *rl;
248 sval_t sval;
250 expr = strip_expr(expr);
251 if (!expr)
252 return false;
253 if (get_value(expr, &sval))
254 return true;
255 if (expr->type == EXPR_BINOP)
256 return binop_capped(expr);
257 if ((expr->type == EXPR_PREOP || expr->type == EXPR_POSTOP) &&
258 (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT))
259 return user_rl_capped(expr->unop);
260 state = get_state_expr(my_id, expr);
261 if (state)
262 return estate_capped(state);
264 if (!get_user_rl(expr, &rl)) {
266 * The non user data parts of a binop are capped and
267 * also empty user rl states are capped.
269 return true;
272 if (rl_to_sval(rl, &sval))
273 return true;
275 return false; /* uncapped user data */
278 bool user_rl_treat_untagged(struct expression *expr)
280 struct smatch_state *state;
281 struct range_list *rl;
282 sval_t sval;
284 expr = strip_expr(expr);
285 if (!expr)
286 return false;
287 if (get_value(expr, &sval))
288 return true;
290 state = get_state_expr(my_id, expr);
291 if (state)
292 return estate_treat_untagged(state);
294 if (get_user_rl(expr, &rl))
295 return false; /* uncapped user data */
297 return true; /* not actually user data */
300 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
302 struct expression *edge_member;
303 struct symbol *base = get_real_base_type(member);
304 struct symbol *tmp;
306 if (member->ident)
307 expr = member_expression(expr, '.', member->ident);
309 FOR_EACH_PTR(base->symbol_list, tmp) {
310 struct symbol *type;
312 type = get_real_base_type(tmp);
313 if (!type)
314 continue;
316 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
317 tag_inner_struct_members(expr, tmp);
318 continue;
321 if (!tmp->ident)
322 continue;
324 edge_member = member_expression(expr, '.', tmp->ident);
325 set_user_data(edge_member, new_state(type));
326 } END_FOR_EACH_PTR(tmp);
329 void __set_user_string(struct expression *expr);
330 static void tag_struct_members(struct symbol *type, struct expression *expr)
332 struct symbol *tmp;
333 struct expression *member;
334 int op = '*';
336 if (expr->type == EXPR_PREOP && expr->op == '&') {
337 expr = strip_expr(expr->unop);
338 op = '.';
341 FOR_EACH_PTR(type->symbol_list, tmp) {
342 type = get_real_base_type(tmp);
343 if (!type)
344 continue;
346 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
347 tag_inner_struct_members(expr, tmp);
348 continue;
351 if (!tmp->ident)
352 continue;
354 member = member_expression(expr, op, tmp->ident);
355 if (type->type == SYM_ARRAY) {
356 set_points_to_user_data(member);
357 } else {
358 set_user_data(member, new_state(get_type(member)));
360 } END_FOR_EACH_PTR(tmp);
363 static void tag_base_type(struct expression *expr)
365 if (expr->type == EXPR_PREOP && expr->op == '&')
366 expr = strip_expr(expr->unop);
367 else
368 expr = deref_expression(expr);
369 set_user_data(expr, new_state(get_type(expr)));
372 static void tag_as_user_data(struct expression *expr)
374 struct symbol *type;
376 expr = strip_expr(expr);
378 type = get_type(expr);
379 if (!type || type->type != SYM_PTR)
380 return;
381 type = get_real_base_type(type);
382 if (!type)
383 return;
384 if (type == &void_ctype) {
385 set_user_data(deref_expression(expr), new_state(&ulong_ctype));
386 return;
388 if (type->type == SYM_BASETYPE) {
389 if (expr->type != EXPR_PREOP && expr->op != '&')
390 set_points_to_user_data(expr);
391 tag_base_type(expr);
392 return;
394 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
395 if (expr->type != EXPR_PREOP || expr->op != '&')
396 expr = deref_expression(expr);
397 else
398 set_user_data(deref_expression(expr), new_state(&ulong_ctype));
399 tag_struct_members(type, expr);
403 static struct expression *ignore_param_set;
404 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
406 int param = PTR_INT(_param);
407 struct expression *dest;
409 func_gets_user_data = true;
410 ignore_param_set = expr;
412 dest = get_argument_from_call_expr(expr->args, param);
413 dest = strip_expr(dest);
414 if (!dest)
415 return;
416 tag_as_user_data(dest);
419 static int is_dev_attr_name(struct expression *expr)
421 char *name;
422 int ret = 0;
424 name = expr_to_str(expr);
425 if (!name)
426 return 0;
427 if (strstr(name, "->attr.name"))
428 ret = 1;
429 free_string(name);
430 return ret;
433 static int ends_in_n(struct expression *expr)
435 struct string *str;
437 if (!expr)
438 return 0;
439 if (expr->type != EXPR_STRING || !expr->string)
440 return 0;
442 str = expr->string;
443 if (str->length < 3)
444 return 0;
446 if (str->data[str->length - 3] == '%' &&
447 str->data[str->length - 2] == 'n')
448 return 1;
449 return 0;
452 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
454 struct expression *str, *format, *arg;
455 int i, last;
457 func_gets_user_data = true;
459 str = get_argument_from_call_expr(expr->args, 0);
460 if (is_dev_attr_name(str))
461 return;
463 format = get_argument_from_call_expr(expr->args, 1);
464 if (is_dev_attr_name(format))
465 return;
467 last = ptr_list_size((struct ptr_list *)expr->args) - 1;
469 i = -1;
470 FOR_EACH_PTR(expr->args, arg) {
471 i++;
472 if (i < 2)
473 continue;
474 if (i == last && ends_in_n(format))
475 continue;
476 tag_as_user_data(arg);
477 } END_FOR_EACH_PTR(arg);
480 static int get_rl_from_function(struct expression *expr, struct range_list **rl)
482 int i;
484 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL ||
485 !expr->fn->symbol_name || !expr->fn->symbol_name->name)
486 return 0;
488 for (i = 0; i < ARRAY_SIZE(returns_user_data); i++) {
489 if (strcmp(expr->fn->symbol_name->name, returns_user_data[i]) == 0) {
490 *rl = alloc_whole_rl(get_type(expr));
491 return 1;
494 return 0;
497 static int comes_from_skb_data(struct expression *expr)
499 expr = strip_expr(expr);
500 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
501 return 0;
503 expr = strip_expr(expr->unop);
504 if (!expr)
505 return 0;
506 if (expr->type == EXPR_BINOP && expr->op == '+')
507 expr = strip_expr(expr->left);
509 return is_skb_data(expr);
512 static int handle_get_user(struct expression *expr)
514 char *name;
515 int ret = 0;
517 name = get_macro_name(expr->pos);
518 if (!name || strcmp(name, "get_user") != 0)
519 return 0;
521 name = expr_to_var(expr->right);
522 if (!name || (strcmp(name, "__val_gu") != 0 && strcmp(name, "__gu_val") != 0))
523 goto free;
524 set_user_data(expr->left, new_state(get_type(expr->left)));
525 ret = 1;
526 free:
527 free_string(name);
528 return ret;
531 static bool state_is_new(struct expression *expr)
533 struct smatch_state *state;
535 state = get_state_expr(my_id, expr);
536 if (estate_new(state))
537 return true;
539 if (expr->type == EXPR_BINOP) {
540 if (state_is_new(expr->left))
541 return true;
542 if (state_is_new(expr->right))
543 return true;
545 return false;
548 static bool handle_op_assign(struct expression *expr)
550 struct expression *binop_expr;
551 struct smatch_state *state;
552 struct range_list *rl;
554 switch (expr->op) {
555 case SPECIAL_ADD_ASSIGN:
556 case SPECIAL_SUB_ASSIGN:
557 case SPECIAL_AND_ASSIGN:
558 case SPECIAL_MOD_ASSIGN:
559 case SPECIAL_SHL_ASSIGN:
560 case SPECIAL_SHR_ASSIGN:
561 case SPECIAL_OR_ASSIGN:
562 case SPECIAL_XOR_ASSIGN:
563 case SPECIAL_MUL_ASSIGN:
564 case SPECIAL_DIV_ASSIGN:
565 binop_expr = binop_expression(expr->left,
566 op_remove_assign(expr->op),
567 expr->right);
568 if (!get_user_rl(binop_expr, &rl))
569 return true;
571 rl = cast_rl(get_type(expr->left), rl);
572 state = alloc_estate_rl(rl);
573 if (expr->op == SPECIAL_AND_ASSIGN ||
574 expr->op == SPECIAL_MOD_ASSIGN ||
575 user_rl_capped(binop_expr))
576 estate_set_capped(state);
577 if (user_rl_treat_untagged(expr->left))
578 estate_set_treat_untagged(state);
579 if (state_is_new(binop_expr))
580 estate_set_new(state);
581 set_user_data(expr->left, state);
582 return true;
584 return false;
587 static void match_assign(struct expression *expr)
589 struct symbol *left_type, *right_type;
590 struct range_list *rl;
591 static struct expression *handled;
592 struct smatch_state *state;
593 struct expression *faked;
594 bool is_capped = false;
595 bool is_new = false;
597 left_type = get_type(expr->left);
598 if (left_type == &void_ctype)
599 return;
601 faked = get_faked_expression();
603 /* FIXME: handle fake array assignments frob(&user_array[x]); */
605 if (is_fake_call(expr->right) && faked &&
606 faked->type == EXPR_ASSIGNMENT &&
607 points_to_user_data(faked->right)) {
608 if (is_skb_data(faked->right))
609 func_gets_user_data = true;
610 rl = alloc_whole_rl(get_type(expr->left));
611 is_new = true;
612 goto set;
615 if (faked && faked == handled)
616 return;
617 if (is_fake_call(expr->right))
618 goto clear_old_state;
619 if (handle_get_user(expr))
620 return;
621 if (points_to_user_data(expr->right) &&
622 is_struct_ptr(get_type(expr->left))) {
623 handled = expr;
624 // This should be handled by smatch_points_to_user_data.c
625 // set_points_to_user_data(expr->left);
628 if (handle_op_assign(expr))
629 return;
630 if (expr->op != '=')
631 goto clear_old_state;
633 /* Handled by DB code */
634 if (expr->right->type == EXPR_CALL)
635 return;
637 if (!get_user_rl(expr->right, &rl))
638 goto clear_old_state;
639 is_capped = user_rl_capped(expr->right);
640 is_new = state_is_new(expr->right);
642 set:
643 if (type_is_ptr(left_type)) {
644 right_type = get_type(expr->right);
645 if (right_type && right_type->type == SYM_ARRAY)
646 set_points_to_user_data(expr->left);
647 return;
650 rl = cast_rl(left_type, rl);
651 state = alloc_estate_rl(rl);
652 if (is_new)
653 estate_set_new(state);
654 if (is_capped)
655 estate_set_capped(state);
656 if (user_rl_treat_untagged(expr->right))
657 estate_set_treat_untagged(state);
659 set_user_data(expr->left, state);
661 return;
663 clear_old_state:
666 * HACK ALERT!!! This should be at the start of the function. The
667 * the problem is that handling "pointer = array;" assignments is
668 * handled in this function instead of in kernel_points_to_user_data.c.
670 if (type_is_ptr(left_type))
671 return;
673 if (get_state_expr(my_id, expr->left))
674 set_user_data(expr->left, alloc_estate_empty());
677 static void handle_eq_noteq(struct expression *expr)
679 struct smatch_state *left_orig, *right_orig;
681 left_orig = get_state_expr(my_id, expr->left);
682 right_orig = get_state_expr(my_id, expr->right);
684 if (!left_orig && !right_orig)
685 return;
686 if (left_orig && right_orig)
687 return;
689 if (left_orig) {
690 set_true_false_states_expr(my_id, expr->left,
691 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
692 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
693 } else {
694 set_true_false_states_expr(my_id, expr->right,
695 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
696 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
700 static struct range_list *strip_negatives(struct range_list *rl)
702 sval_t min = rl_min(rl);
703 sval_t minus_one = { .type = rl_type(rl), .value = -1 };
704 sval_t over = { .type = rl_type(rl), .value = INT_MAX + 1ULL };
705 sval_t max = sval_type_max(rl_type(rl));
707 if (!rl)
708 return NULL;
710 if (type_unsigned(rl_type(rl)) && type_bits(rl_type(rl)) > 31)
711 return remove_range(rl, over, max);
713 return remove_range(rl, min, minus_one);
716 static void handle_compare(struct expression *expr)
718 struct expression *left, *right;
719 struct range_list *left_rl = NULL;
720 struct range_list *right_rl = NULL;
721 struct range_list *user_rl;
722 struct smatch_state *capped_state;
723 struct smatch_state *left_true = NULL;
724 struct smatch_state *left_false = NULL;
725 struct smatch_state *right_true = NULL;
726 struct smatch_state *right_false = NULL;
727 struct symbol *type;
728 sval_t sval;
730 left = strip_expr(expr->left);
731 right = strip_expr(expr->right);
733 while (left->type == EXPR_ASSIGNMENT)
734 left = strip_expr(left->left);
737 * Conditions are mostly handled by smatch_extra.c, but there are some
738 * times where the exact values are not known so we can't do that.
740 * Normally, we might consider using smatch_capped.c to supliment smatch
741 * extra but that doesn't work when we merge unknown uncapped kernel
742 * data with unknown capped user data. The result is uncapped user
743 * data. We need to keep it separate and say that the user data is
744 * capped. In the past, I would have marked this as just regular
745 * kernel data (not user data) but we can't do that these days because
746 * we need to track user data for Spectre.
748 * The other situation which we have to handle is when we do have an
749 * int and we compare against an unknown unsigned kernel variable. In
750 * that situation we assume that the kernel data is less than INT_MAX.
751 * Otherwise then we get all sorts of array underflow false positives.
755 /* Handled in smatch_extra.c */
756 if (get_implied_value(left, &sval) ||
757 get_implied_value(right, &sval))
758 return;
760 get_user_rl(left, &left_rl);
761 get_user_rl(right, &right_rl);
763 /* nothing to do */
764 if (!left_rl && !right_rl)
765 return;
766 /* if both sides are user data that's not a good limit */
767 if (left_rl && right_rl)
768 return;
770 if (left_rl)
771 user_rl = left_rl;
772 else
773 user_rl = right_rl;
775 type = get_type(expr);
776 if (type_unsigned(type))
777 user_rl = strip_negatives(user_rl);
778 capped_state = alloc_estate_rl(user_rl);
779 estate_set_capped(capped_state);
781 switch (expr->op) {
782 case '<':
783 case SPECIAL_UNSIGNED_LT:
784 case SPECIAL_LTE:
785 case SPECIAL_UNSIGNED_LTE:
786 if (left_rl)
787 left_true = capped_state;
788 else
789 right_false = capped_state;
790 break;
791 case '>':
792 case SPECIAL_UNSIGNED_GT:
793 case SPECIAL_GTE:
794 case SPECIAL_UNSIGNED_GTE:
795 if (left_rl)
796 left_false = capped_state;
797 else
798 right_true = capped_state;
799 break;
802 set_true_false_states_expr(my_id, left, left_true, left_false);
803 set_true_false_states_expr(my_id, right, right_true, right_false);
806 static void match_condition(struct expression *expr)
808 if (expr->type != EXPR_COMPARE)
809 return;
811 if (expr->op == SPECIAL_EQUAL ||
812 expr->op == SPECIAL_NOTEQUAL) {
813 handle_eq_noteq(expr);
814 return;
817 handle_compare(expr);
820 static void match_returns_user_rl(const char *fn, struct expression *expr, void *unused)
822 func_gets_user_data = true;
825 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
827 struct expression *parent;
828 char *macro;
830 if (!expr)
831 return 0;
833 macro = get_macro_name(expr->pos);
834 if (!macro)
835 return 0;
837 /* handle ntohl(foo[i]) where "i" is trusted */
838 parent = expr_get_parent_expr(expr);
839 while (parent && parent->type != EXPR_BINOP)
840 parent = expr_get_parent_expr(parent);
841 if (parent && parent->type == EXPR_BINOP) {
842 char *parent_macro = get_macro_name(parent->pos);
844 if (parent_macro && strcmp(macro, parent_macro) == 0)
845 return 0;
848 if (strcmp(macro, "ntohl") == 0) {
849 *rl = alloc_whole_rl(&uint_ctype);
850 return 1;
852 if (strcmp(macro, "ntohs") == 0) {
853 *rl = alloc_whole_rl(&ushort_ctype);
854 return 1;
856 return 0;
859 static int has_user_data(struct symbol *sym)
861 struct sm_state *tmp;
863 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
864 if (tmp->sym == sym)
865 return 1;
866 } END_FOR_EACH_SM(tmp);
867 return 0;
870 bool we_pass_user_data(struct expression *call)
872 struct expression *arg;
873 struct symbol *sym;
875 FOR_EACH_PTR(call->args, arg) {
876 if (points_to_user_data(arg))
877 return true;
878 sym = expr_to_sym(arg);
879 if (!sym)
880 continue;
881 if (has_user_data(sym))
882 return true;
883 } END_FOR_EACH_PTR(arg);
885 return false;
888 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
890 struct smatch_state *state;
891 char buf[48];
893 if (is_fake_call(call))
894 return 0;
895 snprintf(buf, sizeof(buf), "return %p", call);
896 state = get_state(my_id, buf, NULL);
897 if (!state || !estate_rl(state))
898 return 0;
899 *rl = estate_rl(state);
900 return 1;
903 struct stree *get_user_stree(void)
905 return get_all_states_stree(my_id);
908 static int user_data_flag;
909 static int no_user_data_flag;
910 struct range_list *var_user_rl(struct expression *expr)
912 struct smatch_state *state;
913 struct range_list *rl;
914 struct range_list *absolute_rl;
916 if (expr->type == EXPR_PREOP && expr->op == '&') {
917 no_user_data_flag = 1;
918 return NULL;
921 if (expr->type == EXPR_BINOP && expr->op == '%') {
922 struct range_list *left, *right;
924 if (!get_user_rl(expr->right, &right))
925 return NULL;
926 get_absolute_rl(expr->left, &left);
927 rl = rl_binop(left, '%', right);
928 goto found;
931 if (expr->type == EXPR_BINOP && expr->op == '/') {
932 struct range_list *left = NULL;
933 struct range_list *right = NULL;
934 struct range_list *abs_right;
937 * The specific bug I'm dealing with is:
939 * foo = capped_user / unknown;
941 * Instead of just saying foo is now entirely user_rl we should
942 * probably say instead that it is not at all user data.
946 get_user_rl(expr->left, &left);
947 get_user_rl(expr->right, &right);
948 get_absolute_rl(expr->right, &abs_right);
950 if (left && !right) {
951 rl = rl_binop(left, '/', abs_right);
952 if (sval_cmp(rl_max(left), rl_max(rl)) < 0)
953 no_user_data_flag = 1;
956 return NULL;
959 if (get_rl_from_function(expr, &rl))
960 goto found;
962 if (get_user_macro_rl(expr, &rl))
963 goto found;
965 if (comes_from_skb_data(expr)) {
966 rl = alloc_whole_rl(get_type(expr));
967 goto found;
970 state = get_state_expr(my_id, expr);
971 if (state && estate_rl(state)) {
972 rl = estate_rl(state);
973 goto found;
976 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
977 goto found;
979 if (expr->type == EXPR_PREOP && expr->op == '*' &&
980 points_to_user_data(expr->unop)) {
981 rl = var_to_absolute_rl(expr);
982 goto found;
985 if (is_array(expr)) {
986 struct expression *array = get_array_base(expr);
988 if (!get_state_expr(my_id, array)) {
989 no_user_data_flag = 1;
990 return NULL;
994 return NULL;
995 found:
996 user_data_flag = 1;
997 absolute_rl = var_to_absolute_rl(expr);
998 return clone_rl(rl_intersection(rl, absolute_rl));
1001 static bool is_ptr_subtract(struct expression *expr)
1003 expr = strip_expr(expr);
1004 if (!expr)
1005 return false;
1006 if (expr->type == EXPR_BINOP && expr->op == '-' &&
1007 type_is_ptr(get_type(expr->left))) {
1008 return true;
1010 return false;
1013 int get_user_rl(struct expression *expr, struct range_list **rl)
1015 if (is_ptr_subtract(expr))
1016 return 0;
1018 user_data_flag = 0;
1019 no_user_data_flag = 0;
1020 custom_get_absolute_rl(expr, &var_user_rl, rl);
1021 if (!user_data_flag || no_user_data_flag)
1022 *rl = NULL;
1024 return !!*rl;
1027 int is_user_rl(struct expression *expr)
1029 struct range_list *tmp;
1031 return get_user_rl(expr, &tmp) && tmp;
1034 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
1036 struct smatch_state *state;
1038 state = get_state(my_id, name, sym);
1039 if (state && estate_rl(state)) {
1040 *rl = estate_rl(state);
1041 return 1;
1043 return 0;
1046 static void return_info_callback(int return_id, char *return_ranges,
1047 struct expression *returned_expr,
1048 int param,
1049 const char *printed_name,
1050 struct sm_state *sm)
1052 struct smatch_state *extra;
1053 struct range_list *rl;
1054 char buf[64];
1056 if (param >= 0) {
1057 if (strcmp(printed_name, "$") == 0)
1058 return;
1059 if (!param_was_set_var_sym(sm->name, sm->sym))
1060 return;
1062 rl = estate_rl(sm->state);
1063 if (!rl)
1064 return;
1065 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
1066 if (estate_rl(extra))
1067 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
1068 if (!rl)
1069 return;
1071 snprintf(buf, sizeof(buf), "%s%s%s",
1072 show_rl(rl),
1073 estate_capped(sm->state) ? "[c]" : "",
1074 estate_treat_untagged(sm->state) ? "[u]" : "");
1075 sql_insert_return_states(return_id, return_ranges,
1076 estate_new(sm->state) ? USER_DATA_SET : USER_DATA,
1077 param, printed_name, buf);
1080 static bool is_ignored_macro(struct position pos)
1082 const char *macro;
1084 macro = get_macro_name(pos);
1085 if (!macro)
1086 return false;
1087 if (strcmp(macro, "v4l2_subdev_call") == 0)
1088 return true;
1089 return false;
1092 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1094 struct smatch_state *state;
1095 struct range_list *rl;
1096 struct symbol *type;
1097 char buf[64];
1099 if (is_ignored_macro(call->pos))
1100 return;
1103 * Smatch uses a hack where if we get an unsigned long we say it's
1104 * both user data and it points to user data. But if we pass it to a
1105 * function which takes an int, then it's just user data. There's not
1106 * enough bytes for it to be a pointer.
1109 type = get_arg_type(call->fn, param);
1110 if (strcmp(printed_name, "$") != 0 && type && type_bits(type) < type_bits(&ptr_ctype))
1111 return;
1113 if (strcmp(sm->state->name, "") == 0)
1114 return;
1116 state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
1117 if (!state || !estate_rl(state))
1118 rl = estate_rl(sm->state);
1119 else
1120 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
1122 if (!rl)
1123 return;
1125 snprintf(buf, sizeof(buf), "%s%s%s", show_rl(rl),
1126 estate_capped(sm->state) ? "[c]" : "",
1127 estate_treat_untagged(sm->state) ? "[u]" : "");
1128 sql_insert_caller_info(call, USER_DATA, param, printed_name, buf);
1131 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1133 struct expression *arg;
1134 char *name;
1135 struct symbol *sym;
1136 struct smatch_state *state;
1138 while (expr->type == EXPR_ASSIGNMENT)
1139 expr = strip_expr(expr->right);
1140 if (expr->type != EXPR_CALL)
1141 return;
1142 if (expr == ignore_param_set)
1143 return;
1145 arg = get_argument_from_call_expr(expr->args, param);
1146 if (!arg)
1147 return;
1148 name = get_variable_from_key(arg, key, &sym);
1149 if (!name || !sym)
1150 goto free;
1152 state = get_state(my_id, name, sym);
1153 if (!state)
1154 goto free;
1156 set_state(my_id, name, sym, alloc_estate_empty());
1157 free:
1158 free_string(name);
1161 static bool param_data_capped(const char *value)
1163 if (strstr(value, ",c") || strstr(value, "[c"))
1164 return true;
1165 return false;
1168 static bool param_data_treat_untagged(const char *value)
1170 if (strstr(value, ",u") || strstr(value, "[u"))
1171 return true;
1172 return false;
1175 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
1177 struct expression *expr;
1178 struct range_list *rl = NULL;
1179 struct smatch_state *state;
1180 struct symbol *type;
1181 char *fullname;
1183 expr = symbol_expression(sym);
1184 fullname = get_variable_from_key(expr, key, NULL);
1185 if (!fullname)
1186 return;
1188 type = get_member_type_from_key(expr, key);
1189 if (type && type->type == SYM_STRUCT)
1190 return;
1192 if (!type)
1193 return;
1195 str_to_rl(type, value, &rl);
1196 rl = swap_mtag_seed(expr, rl);
1197 state = alloc_estate_rl(rl);
1198 if (param_data_capped(value) || is_capped(expr))
1199 estate_set_capped(state);
1200 if (param_data_treat_untagged(value) || sym->ctype.as == 5)
1201 estate_set_treat_untagged(state);
1202 set_state(my_id, fullname, sym, state);
1205 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
1207 set_state(my_call_id, "this_function", NULL, &called);
1210 static void match_syscall_definition(struct symbol *sym)
1212 struct symbol *arg;
1213 char *macro;
1214 char *name;
1215 int is_syscall = 0;
1217 macro = get_macro_name(sym->pos);
1218 if (macro &&
1219 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
1220 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1221 is_syscall = 1;
1223 name = get_function();
1224 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
1225 if (name && strncmp(name, "sys_", 4) == 0)
1226 is_syscall = 1;
1229 if (name && strncmp(name, "compat_sys_", 11) == 0)
1230 is_syscall = 1;
1232 if (!is_syscall)
1233 return;
1235 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
1236 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
1237 } END_FOR_EACH_PTR(arg);
1240 #define OLD 0
1241 #define NEW 1
1243 static void store_user_data_return(struct expression *expr, char *key, char *value, bool is_new)
1245 struct smatch_state *state;
1246 struct range_list *rl;
1247 struct symbol *type;
1248 char buf[48];
1250 if (key[0] != '$')
1251 return;
1253 type = get_type(expr);
1254 snprintf(buf, sizeof(buf), "return %p%s", expr, key + 1);
1255 call_results_to_rl(expr, type, value, &rl);
1257 state = alloc_estate_rl(rl);
1258 if (is_new)
1259 estate_set_new(state);
1261 set_state(my_id, buf, NULL, state);
1264 static void set_to_user_data(struct expression *expr, char *key, char *value, bool is_new)
1266 struct smatch_state *state;
1267 char *name;
1268 struct symbol *sym;
1269 struct symbol *type;
1270 struct range_list *rl = NULL;
1272 type = get_member_type_from_key(expr, key);
1273 name = get_variable_from_key(expr, key, &sym);
1274 if (!name || !sym)
1275 goto free;
1277 call_results_to_rl(expr, type, value, &rl);
1279 state = alloc_estate_rl(rl);
1280 if (param_data_capped(value))
1281 estate_set_capped(state);
1282 if (param_data_treat_untagged(value))
1283 estate_set_treat_untagged(state);
1284 if (is_new)
1285 estate_set_new(state);
1286 set_state(my_id, name, sym, state);
1287 free:
1288 free_string(name);
1291 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
1293 struct expression *arg;
1294 struct expression *call;
1296 call = expr;
1297 while (call->type == EXPR_ASSIGNMENT)
1298 call = strip_expr(call->right);
1299 if (call->type != EXPR_CALL)
1300 return;
1302 if (!we_pass_user_data(call))
1303 return;
1305 if (param == -1) {
1306 if (expr->type != EXPR_ASSIGNMENT) {
1307 store_user_data_return(expr, key, value, OLD);
1308 return;
1310 set_to_user_data(expr->left, key, value, OLD);
1311 return;
1314 arg = get_argument_from_call_expr(call->args, param);
1315 if (!arg)
1316 return;
1317 set_to_user_data(arg, key, value, OLD);
1320 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
1322 struct expression *arg;
1324 func_gets_user_data = true;
1326 if (param == -1) {
1327 if (expr->type != EXPR_ASSIGNMENT) {
1328 store_user_data_return(expr, key, value, NEW);
1329 return;
1331 set_to_user_data(expr->left, key, value, NEW);
1332 return;
1335 while (expr->type == EXPR_ASSIGNMENT)
1336 expr = strip_expr(expr->right);
1337 if (expr->type != EXPR_CALL)
1338 return;
1340 arg = get_argument_from_call_expr(expr->args, param);
1341 if (!arg)
1342 return;
1343 set_to_user_data(arg, key, value, NEW);
1346 static void returns_param_capped(struct expression *expr, int param, char *key, char *value)
1348 struct smatch_state *state, *new;
1349 struct symbol *sym;
1350 char *name;
1352 name = get_name_sym_from_param_key(expr, param, key, &sym);
1353 if (!name || !sym)
1354 goto free;
1356 state = get_state(my_id, name, sym);
1357 if (!state || estate_capped(state))
1358 goto free;
1360 new = clone_estate(state);
1361 estate_set_capped(new);
1363 set_state(my_id, name, sym, new);
1364 free:
1365 free_string(name);
1368 static void match_function_def(struct symbol *sym)
1370 if (is_user_data_fn(sym))
1371 func_gets_user_data = true;
1374 void register_kernel_user_data(int id)
1376 int i;
1378 my_id = id;
1380 if (option_project != PROJ_KERNEL)
1381 return;
1383 set_dynamic_states(my_id);
1385 add_function_data(&func_gets_user_data);
1386 add_hook(&match_function_def, FUNC_DEF_HOOK);
1388 add_hook(&save_start_states, AFTER_DEF_HOOK);
1389 add_hook(&free_start_states, AFTER_FUNC_HOOK);
1390 add_function_data((unsigned long *)&start_states);
1392 add_unmatched_state_hook(my_id, &empty_state);
1393 add_extra_nomod_hook(&extra_nomod_hook);
1394 add_pre_merge_hook(my_id, &pre_merge_hook);
1395 add_merge_hook(my_id, &merge_estates);
1397 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
1398 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
1399 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1400 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1401 add_function_hook_late(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1402 add_function_hook("usb_control_msg", &match_user_copy, INT_PTR(6));
1403 add_function_hook("kvm_read_guest_virt", &match_user_copy, INT_PTR(2));
1405 for (i = 0; i < ARRAY_SIZE(returns_user_data); i++)
1406 add_function_hook(returns_user_data[i], &match_returns_user_rl, NULL);
1408 add_function_hook("sscanf", &match_sscanf, NULL);
1410 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1412 add_hook(&match_assign, ASSIGNMENT_HOOK);
1413 select_return_states_hook(PARAM_SET, &db_param_set);
1414 add_hook(&match_condition, CONDITION_HOOK);
1416 add_caller_info_callback(my_id, caller_info_callback);
1417 add_return_info_callback(my_id, return_info_callback);
1418 select_caller_info_hook(set_param_user_data, USER_DATA);
1419 select_return_states_hook(USER_DATA, &returns_param_user_data);
1420 select_return_states_hook(USER_DATA_SET, &returns_param_user_data_set);
1421 select_return_states_hook(CAPPED_DATA, &returns_param_capped);
1424 void register_kernel_user_data2(int id)
1426 my_call_id = id;
1428 if (option_project != PROJ_KERNEL)
1429 return;
1430 select_caller_info_hook(set_called, INTERNAL);