comparison: add some debug output
[smatch.git] / check_user_data2.c
blob1e86cd5378ecb1f29f3f9959119fc17989961352
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 bool 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 void set_points_to_user_data(struct expression *expr);
48 static struct stree *start_states;
49 static struct stree_stack *saved_stack;
50 static void save_start_states(struct statement *stmt)
52 start_states = clone_stree(__get_cur_stree());
55 static void free_start_states(void)
57 free_stree(&start_states);
60 static void match_save_states(struct expression *expr)
62 push_stree(&saved_stack, start_states);
63 start_states = NULL;
66 static void match_restore_states(struct expression *expr)
68 free_stree(&start_states);
69 start_states = pop_stree(&saved_stack);
72 static struct smatch_state *empty_state(struct sm_state *sm)
74 return alloc_estate_empty();
77 static void pre_merge_hook(struct sm_state *sm)
79 struct smatch_state *user;
80 struct smatch_state *extra;
81 struct range_list *rl;
82 sval_t dummy;
83 sval_t sval_100 = {
84 .type = &int_ctype,
85 .value = 100,
88 user = get_state(my_id, sm->name, sm->sym);
89 if (!user)
90 return;
91 if (!estate_rl(sm->state)) {
93 * If the one side is capped and the other side is empty then
94 * let's just mark it as not-user data because the information
95 * isn't going to be useful. How this looks is:
97 * if (user_var > trusted)
98 * user_var = trusted; <-- empty state
99 * else
100 * <-- capped
102 * The problem is that sometimes things are capped to a literal
103 * and we'd like to keep the state in that case... Ugh. I've
104 * added a check which assumes that everything less than 100 is
105 * probably capped against a literal.
108 if (is_capped_var_sym(sm->name, sm->sym) &&
109 sval_cmp(estate_max(user), sval_100) > 0)
110 set_state(my_id, sm->name, sm->sym, alloc_estate_empty());
111 return;
113 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
114 if (!extra || !estate_rl(extra))
115 return;
116 rl = rl_intersection(estate_rl(user), estate_rl(extra));
117 if (rl_to_sval(rl, &dummy))
118 rl = NULL;
119 set_state(my_id, sm->name, sm->sym, alloc_estate_rl(clone_rl(rl)));
122 static void extra_nomod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
124 struct smatch_state *user;
125 struct range_list *rl;
127 user = get_state(my_id, name, sym);
128 if (!user)
129 return;
130 rl = rl_intersection(estate_rl(user), estate_rl(state));
131 if (rl_equiv(rl, estate_rl(user)))
132 return;
133 set_state(my_id, name, sym, alloc_estate_rl(rl));
136 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
138 struct expression *edge_member;
139 struct symbol *base = get_real_base_type(member);
140 struct symbol *tmp;
142 if (member->ident)
143 expr = member_expression(expr, '.', member->ident);
145 FOR_EACH_PTR(base->symbol_list, tmp) {
146 struct symbol *type;
148 type = get_real_base_type(tmp);
149 if (!type)
150 continue;
152 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
153 tag_inner_struct_members(expr, tmp);
154 continue;
157 if (!tmp->ident)
158 continue;
160 edge_member = member_expression(expr, '.', tmp->ident);
161 set_state_expr(my_id, edge_member, alloc_estate_whole(type));
162 } END_FOR_EACH_PTR(tmp);
165 static void tag_struct_members(struct symbol *type, struct expression *expr)
167 struct symbol *tmp;
168 struct expression *member;
169 int op = '*';
171 if (expr->type == EXPR_PREOP && expr->op == '&') {
172 expr = strip_expr(expr->unop);
173 op = '.';
176 FOR_EACH_PTR(type->symbol_list, tmp) {
177 type = get_real_base_type(tmp);
178 if (!type)
179 continue;
181 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
182 tag_inner_struct_members(expr, tmp);
183 continue;
186 if (!tmp->ident)
187 continue;
189 member = member_expression(expr, op, tmp->ident);
190 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
192 if (type->type == SYM_ARRAY)
193 set_points_to_user_data(member);
194 } END_FOR_EACH_PTR(tmp);
197 static void tag_base_type(struct expression *expr)
199 if (expr->type == EXPR_PREOP && expr->op == '&')
200 expr = strip_expr(expr->unop);
201 else
202 expr = deref_expression(expr);
203 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
206 static void tag_as_user_data(struct expression *expr)
208 struct symbol *type;
210 expr = strip_expr(expr);
212 type = get_type(expr);
213 if (!type || type->type != SYM_PTR)
214 return;
215 type = get_real_base_type(type);
216 if (!type)
217 return;
218 if (type == &void_ctype) {
219 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
220 return;
222 if (type->type == SYM_BASETYPE)
223 tag_base_type(expr);
224 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
225 if (expr->type != EXPR_PREOP || expr->op != '&')
226 expr = deref_expression(expr);
227 else
228 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
229 tag_struct_members(type, expr);
233 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
235 int param = PTR_INT(_param);
236 struct expression *dest;
238 func_gets_user_data = true;
240 dest = get_argument_from_call_expr(expr->args, param);
241 dest = strip_expr(dest);
242 if (!dest)
243 return;
244 tag_as_user_data(dest);
247 static int is_dev_attr_name(struct expression *expr)
249 char *name;
250 int ret = 0;
252 name = expr_to_str(expr);
253 if (!name)
254 return 0;
255 if (strstr(name, "->attr.name"))
256 ret = 1;
257 free_string(name);
258 return ret;
261 static int ends_in_n(struct expression *expr)
263 struct string *str;
265 if (!expr)
266 return 0;
267 if (expr->type != EXPR_STRING || !expr->string)
268 return 0;
270 str = expr->string;
271 if (str->length < 3)
272 return 0;
274 if (str->data[str->length - 3] == '%' &&
275 str->data[str->length - 2] == 'n')
276 return 1;
277 return 0;
280 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
282 struct expression *str, *format, *arg;
283 int i, last;
285 func_gets_user_data = true;
287 str = get_argument_from_call_expr(expr->args, 0);
288 if (is_dev_attr_name(str))
289 return;
291 format = get_argument_from_call_expr(expr->args, 1);
292 if (is_dev_attr_name(format))
293 return;
295 last = ptr_list_size((struct ptr_list *)expr->args) - 1;
297 i = -1;
298 FOR_EACH_PTR(expr->args, arg) {
299 i++;
300 if (i < 2)
301 continue;
302 if (i == last && ends_in_n(format))
303 continue;
304 tag_as_user_data(arg);
305 } END_FOR_EACH_PTR(arg);
308 static int is_skb_data(struct expression *expr)
310 struct symbol *sym;
312 if (!expr)
313 return 0;
315 if (expr->type == EXPR_BINOP && expr->op == '+')
316 return is_skb_data(expr->left);
318 expr = strip_expr(expr);
319 if (!expr)
320 return 0;
321 if (expr->type != EXPR_DEREF || expr->op != '.')
322 return 0;
324 if (!expr->member)
325 return 0;
326 if (strcmp(expr->member->name, "data") != 0)
327 return 0;
329 sym = expr_to_sym(expr->deref);
330 if (!sym)
331 return 0;
332 sym = get_real_base_type(sym);
333 if (!sym || sym->type != SYM_PTR)
334 return 0;
335 sym = get_real_base_type(sym);
336 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
337 return 0;
338 if (strcmp(sym->ident->name, "sk_buff") != 0)
339 return 0;
341 return 1;
344 static int points_to_user_data(struct expression *expr)
346 struct smatch_state *state;
347 char buf[256];
348 struct symbol *sym;
349 char *name;
350 int ret = 0;
352 expr = strip_expr(expr);
353 if (!expr)
354 return 0;
355 if (is_skb_data(expr))
356 return 1;
358 if (expr->type == EXPR_BINOP && expr->op == '+') {
359 if (points_to_user_data(expr->left))
360 return 1;
361 if (points_to_user_data(expr->right))
362 return 1;
363 return 0;
366 name = expr_to_var_sym(expr, &sym);
367 if (!name || !sym)
368 goto free;
369 snprintf(buf, sizeof(buf), "*%s", name);
370 state = get_state(my_id, buf, sym);
371 if (state && estate_rl(state))
372 ret = 1;
373 free:
374 free_string(name);
375 return ret;
378 static void set_points_to_user_data(struct expression *expr)
380 char *name;
381 struct symbol *sym;
382 char buf[256];
384 name = expr_to_var_sym(expr, &sym);
385 if (!name || !sym)
386 goto free;
387 snprintf(buf, sizeof(buf), "*%s", name);
388 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
389 free:
390 free_string(name);
393 static int comes_from_skb_data(struct expression *expr)
395 expr = strip_expr(expr);
396 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
397 return 0;
399 expr = strip_expr(expr->unop);
400 if (!expr)
401 return 0;
402 if (expr->type == EXPR_BINOP && expr->op == '+')
403 expr = strip_expr(expr->left);
405 return is_skb_data(expr);
408 static int handle_struct_assignment(struct expression *expr)
410 struct expression *right;
411 struct symbol *left_type, *right_type;
413 left_type = get_type(expr->left);
414 if (!left_type || left_type->type != SYM_PTR)
415 return 0;
416 left_type = get_real_base_type(left_type);
417 if (!left_type)
418 return 0;
419 if (left_type->type != SYM_STRUCT &&
420 left_type->type != SYM_UNION)
421 return 0;
424 * Ignore struct to struct assignments because for those we look at the
425 * individual members.
427 right = strip_expr(expr->right);
428 right_type = get_type(right);
429 if (!right_type || right_type->type != SYM_PTR)
430 return 0;
432 /* If we are assigning struct members then normally that is handled
433 * by fake assignments, however if we cast one struct to a different
434 * of struct then we handle that here.
436 right_type = get_real_base_type(right_type);
437 if (right_type == left_type)
438 return 0;
440 if (!points_to_user_data(right))
441 return 0;
443 tag_as_user_data(expr->left);
444 return 1;
447 static int handle_get_user(struct expression *expr)
449 char *name;
450 int ret = 0;
452 name = get_macro_name(expr->pos);
453 if (!name || strcmp(name, "get_user") != 0)
454 return 0;
456 name = expr_to_var(expr->right);
457 if (!name || strcmp(name, "__val_gu") != 0)
458 goto free;
459 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
460 ret = 1;
461 free:
462 free_string(name);
463 return ret;
466 static void match_assign(struct expression *expr)
468 struct range_list *rl;
470 if (is_fake_call(expr->right))
471 return;
472 if (handle_get_user(expr))
473 return;
474 if (points_to_user_data(expr->right))
475 set_points_to_user_data(expr->left);
476 if (handle_struct_assignment(expr))
477 return;
479 if (!get_user_rl(expr->right, &rl))
480 goto clear_old_state;
482 rl = cast_rl(get_type(expr->left), rl);
483 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
485 return;
487 clear_old_state:
488 if (get_state_expr(my_id, expr->left))
489 set_state_expr(my_id, expr->left, alloc_estate_empty());
492 static void handle_eq_noteq(struct expression *expr)
494 struct smatch_state *left_orig, *right_orig;
496 left_orig = get_state_expr(my_id, expr->left);
497 right_orig = get_state_expr(my_id, expr->right);
499 if (!left_orig && !right_orig)
500 return;
501 if (left_orig && right_orig)
502 return;
504 if (left_orig) {
505 set_true_false_states_expr(my_id, expr->left,
506 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
507 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
508 } else {
509 set_true_false_states_expr(my_id, expr->right,
510 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
511 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
515 static void handle_unsigned_lt_gt(struct expression *expr)
517 struct symbol *type;
518 struct range_list *left;
519 struct range_list *right;
520 struct range_list *non_negative;
521 sval_t min, minus_one;
524 * conditions are mostly handled by smatch_extra.c. The special case
525 * here is that say you have if (user_int < unknown_u32) {
526 * In Smatch extra we say that, We have no idea what value
527 * unknown_u32 is so the only thin we can say for sure is that
528 * user_int is not -1 (UINT_MAX). But in check_user_data2.c we should
529 * assume that unless unknown_u32 is user data, it's probably less than
530 * INT_MAX.
534 type = get_type(expr);
535 if (!type_unsigned(type))
536 return;
539 * Assume if (user < trusted) { ... because I am lazy and because this
540 * is the correct way to write code.
542 if (!get_user_rl(expr->left, &left))
543 return;
544 if (get_user_rl(expr->right, &right))
545 return;
547 if (!sval_is_negative(rl_min(left)))
548 return;
549 min = rl_min(left);
550 minus_one.type = rl_type(left);
551 minus_one.value = -1;
552 non_negative = remove_range(left, min, minus_one);
554 switch (expr->op) {
555 case '<':
556 case SPECIAL_UNSIGNED_LT:
557 case SPECIAL_LTE:
558 case SPECIAL_UNSIGNED_LTE:
559 set_true_false_states_expr(my_id, expr->left,
560 alloc_estate_rl(non_negative), NULL);
561 break;
562 case '>':
563 case SPECIAL_UNSIGNED_GT:
564 case SPECIAL_GTE:
565 case SPECIAL_UNSIGNED_GTE:
566 set_true_false_states_expr(my_id, expr->left,
567 NULL, alloc_estate_rl(non_negative));
568 break;
572 static void match_condition(struct expression *expr)
574 if (expr->type != EXPR_COMPARE)
575 return;
577 if (expr->op == SPECIAL_EQUAL ||
578 expr->op == SPECIAL_NOTEQUAL) {
579 handle_eq_noteq(expr);
580 return;
583 handle_unsigned_lt_gt(expr);
586 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
588 func_gets_user_data = true;
590 tag_as_user_data(expr->left);
591 set_points_to_user_data(expr->left);
594 static void match_simple_strtoul(const char *fn, struct expression *expr, void *unused)
596 func_gets_user_data = true;
598 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
601 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
603 struct expression *parent;
604 char *macro;
606 if (!expr)
607 return 0;
609 macro = get_macro_name(expr->pos);
610 if (!macro)
611 return 0;
613 /* handle ntohl(foo[i]) where "i" is trusted */
614 parent = expr_get_parent_expr(expr);
615 while (parent && parent->type != EXPR_BINOP)
616 parent = expr_get_parent_expr(parent);
617 if (parent && parent->type == EXPR_BINOP) {
618 char *parent_macro = get_macro_name(parent->pos);
620 if (parent_macro && strcmp(macro, parent_macro) == 0)
621 return 0;
624 if (strcmp(macro, "ntohl") == 0) {
625 *rl = alloc_whole_rl(&uint_ctype);
626 return 1;
628 if (strcmp(macro, "ntohs") == 0) {
629 *rl = alloc_whole_rl(&ushort_ctype);
630 return 1;
632 return 0;
635 struct db_info {
636 struct range_list *rl;
637 struct expression *call;
639 static int returned_rl_callback(void *_info, int argc, char **argv, char **azColName)
641 struct db_info *db_info = _info;
642 struct range_list *rl;
643 char *return_ranges = argv[0];
644 char *user_ranges = argv[1];
645 struct expression *arg;
646 int comparison;
648 if (argc != 2)
649 return 0;
651 call_results_to_rl(db_info->call, get_type(db_info->call), user_ranges, &rl);
652 if (str_to_comparison_arg(return_ranges, db_info->call, &comparison, &arg) &&
653 comparison == SPECIAL_EQUAL) {
654 struct range_list *orig_rl;
656 if (!get_user_rl(arg, &orig_rl))
657 return 0;
658 rl = rl_intersection(rl, orig_rl);
659 if (!rl)
660 return 0;
662 db_info->rl = rl_union(db_info->rl, rl);
664 return 0;
667 static int has_user_data(struct symbol *sym)
669 struct sm_state *tmp;
671 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
672 if (tmp->sym == sym)
673 return 1;
674 } END_FOR_EACH_SM(tmp);
675 return 0;
678 static int we_pass_user_data(struct expression *call)
680 struct expression *arg;
681 struct symbol *sym;
683 FOR_EACH_PTR(call->args, arg) {
684 sym = expr_to_sym(arg);
685 if (!sym)
686 continue;
687 if (has_user_data(sym))
688 return 1;
689 } END_FOR_EACH_PTR(arg);
691 return 0;
694 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
696 struct db_info db_info = {};
698 /* for function pointers assume everything is used */
699 if (call->fn->type != EXPR_SYMBOL)
700 return 0;
701 if (is_fake_call(call))
702 return 0;
704 db_info.call = call;
705 run_sql(&returned_rl_callback, &db_info,
706 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
707 get_static_filter(call->fn->symbol), USER_DATA3_SET);
708 if (db_info.rl) {
709 func_gets_user_data = true;
710 *rl = db_info.rl;
711 return 1;
714 run_sql(&returned_rl_callback, &db_info,
715 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
716 get_static_filter(call->fn->symbol), USER_DATA3);
717 if (db_info.rl) {
718 if (!we_pass_user_data(call))
719 return 0;
720 *rl = db_info.rl;
721 return 1;
724 return 0;
727 static int user_data_flag;
728 static int no_user_data_flag;
729 static struct range_list *var_user_rl(struct expression *expr)
731 struct smatch_state *state;
732 struct range_list *rl;
733 struct range_list *absolute_rl;
735 if (expr->type == EXPR_BINOP && expr->op == '%') {
736 struct range_list *left, *right;
738 if (!get_user_rl(expr->right, &right))
739 return NULL;
740 get_absolute_rl(expr->left, &left);
741 rl = rl_binop(left, '%', right);
742 goto found;
745 if (!option_spammy && expr->type == EXPR_BINOP && expr->op == '/') {
746 struct range_list *left = NULL;
747 struct range_list *right = NULL;
748 struct range_list *abs_right;
751 * The specific bug I'm dealing with is:
753 * foo = capped_user / unknown;
755 * Instead of just saying foo is now entirely user_rl we should
756 * probably say instead that it is not at all user data.
760 get_user_rl(expr->left, &left);
761 get_user_rl(expr->right, &right);
762 get_absolute_rl(expr->right, &abs_right);
764 if (left && !right) {
765 rl = rl_binop(left, '/', abs_right);
766 if (sval_cmp(rl_max(left), rl_max(rl)) < 0)
767 no_user_data_flag = 1;
770 return NULL;
773 if (get_user_macro_rl(expr, &rl))
774 goto found;
776 if (comes_from_skb_data(expr)) {
777 rl = alloc_whole_rl(get_type(expr));
778 goto found;
781 state = get_state_expr(my_id, expr);
782 if (state && estate_rl(state)) {
783 rl = estate_rl(state);
784 goto found;
787 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
788 goto found;
790 if (is_array(expr)) {
791 struct expression *array = get_array_base(expr);
793 if (!get_state_expr(my_id, array)) {
794 no_user_data_flag = 1;
795 return NULL;
799 return NULL;
800 found:
801 user_data_flag = 1;
802 absolute_rl = var_to_absolute_rl(expr);
803 return clone_rl(rl_intersection(rl, absolute_rl));
806 int get_user_rl(struct expression *expr, struct range_list **rl)
808 user_data_flag = 0;
809 no_user_data_flag = 0;
810 custom_get_absolute_rl(expr, &var_user_rl, rl);
811 if (!user_data_flag || no_user_data_flag)
812 *rl = NULL;
814 return !!*rl;
817 int get_user_rl_spammy(struct expression *expr, struct range_list **rl)
819 int ret;
821 option_spammy++;
822 ret = get_user_rl(expr, rl);
823 option_spammy--;
825 return ret;
828 int is_user_rl(struct expression *expr)
830 struct range_list *tmp;
832 return get_user_rl_spammy(expr, &tmp);
835 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
837 struct smatch_state *state;
839 state = get_state(my_id, name, sym);
840 if (state && estate_rl(state)) {
841 *rl = estate_rl(state);
842 return 1;
844 return 0;
847 static void match_call_info(struct expression *expr)
849 struct range_list *rl;
850 struct expression *arg;
851 int i = 0;
853 i = -1;
854 FOR_EACH_PTR(expr->args, arg) {
855 i++;
857 if (!get_user_rl(arg, &rl))
858 continue;
860 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
861 } END_FOR_EACH_PTR(arg);
864 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
866 struct smatch_state *state;
867 struct range_list *rl;
869 if (strcmp(sm->state->name, "") == 0)
870 return;
872 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
873 if (!state || !estate_rl(state))
874 rl = estate_rl(sm->state);
875 else
876 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
878 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
881 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
883 struct range_list *rl = NULL;
884 struct smatch_state *state;
885 struct symbol *type;
886 char fullname[256];
888 if (strcmp(key, "*$") == 0)
889 snprintf(fullname, sizeof(fullname), "*%s", name);
890 else if (strncmp(key, "$", 1) == 0)
891 snprintf(fullname, 256, "%s%s", name, key + 1);
892 else
893 return;
895 type = get_member_type_from_key(symbol_expression(sym), key);
897 /* if the caller passes a void pointer with user data */
898 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
899 struct expression *expr = symbol_expression(sym);
901 tag_as_user_data(expr);
902 set_points_to_user_data(expr);
903 return;
905 str_to_rl(type, value, &rl);
906 state = alloc_estate_rl(rl);
907 set_state(my_id, fullname, sym, state);
910 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
912 set_state(my_call_id, "this_function", NULL, &called);
915 static void match_syscall_definition(struct symbol *sym)
917 struct symbol *arg;
918 char *macro;
919 char *name;
920 int is_syscall = 0;
922 macro = get_macro_name(sym->pos);
923 if (macro &&
924 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
925 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
926 is_syscall = 1;
928 name = get_function();
929 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
930 if (name && strncmp(name, "sys_", 4) == 0)
931 is_syscall = 1;
934 if (name && strncmp(name, "compat_sys_", 11) == 0)
935 is_syscall = 1;
937 if (!is_syscall)
938 return;
940 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
941 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
942 } END_FOR_EACH_PTR(arg);
945 static void set_to_user_data(struct expression *expr, char *key, char *value)
947 char *name;
948 struct symbol *sym;
949 struct symbol *type;
950 struct range_list *rl = NULL;
952 type = get_member_type_from_key(expr, key);
953 name = get_variable_from_key(expr, key, &sym);
954 if (!name || !sym)
955 goto free;
957 call_results_to_rl(expr, type, value, &rl);
959 set_state(my_id, name, sym, alloc_estate_rl(rl));
960 free:
961 free_string(name);
965 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
967 struct expression *arg;
968 struct expression *call;
970 call = expr;
971 while (call->type == EXPR_ASSIGNMENT)
972 call = strip_expr(call->right);
973 if (call->type != EXPR_CALL)
974 return;
976 if (!we_pass_user_data(call))
977 return;
979 if (param == -1) {
980 if (expr->type != EXPR_ASSIGNMENT)
981 return;
982 set_to_user_data(expr->left, key, value);
983 return;
986 arg = get_argument_from_call_expr(call->args, param);
987 if (!arg)
988 return;
989 set_to_user_data(arg, key, value);
992 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
994 struct expression *arg;
996 func_gets_user_data = true;
998 if (param == -1) {
999 if (expr->type != EXPR_ASSIGNMENT)
1000 return;
1001 if (strcmp(key, "*$") == 0) {
1002 set_points_to_user_data(expr->left);
1003 tag_as_user_data(expr->left);
1004 } else {
1005 set_to_user_data(expr->left, key, value);
1007 return;
1010 while (expr->type == EXPR_ASSIGNMENT)
1011 expr = strip_expr(expr->right);
1012 if (expr->type != EXPR_CALL)
1013 return;
1015 arg = get_argument_from_call_expr(expr->args, param);
1016 if (!arg)
1017 return;
1018 set_to_user_data(arg, key, value);
1021 static int has_empty_state(struct sm_state *sm)
1023 struct sm_state *tmp;
1025 FOR_EACH_PTR(sm->possible, tmp) {
1026 if (!estate_rl(tmp->state))
1027 return 1;
1028 } END_FOR_EACH_PTR(tmp);
1030 return 0;
1033 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
1035 struct sm_state *sm;
1036 struct smatch_state *start_state;
1037 struct range_list *rl;
1038 int param;
1039 char *return_str;
1040 const char *param_name;
1042 expr = strip_expr(expr);
1043 return_str = expr_to_str(expr);
1045 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1046 if (has_empty_state(sm))
1047 continue;
1049 param = get_param_num_from_sym(sm->sym);
1050 if (param < 0) {
1051 if (expr_to_sym(expr) == sm->sym)
1052 param = -1;
1053 else
1054 continue;
1057 /* The logic here was that if we were passed in a user data then
1058 * we don't record that. It's like the difference between
1059 * param_filter and param_set. When I think about it, I'm not
1060 * sure it actually works. It's probably harmless because we
1061 * checked earlier that we're not returning a parameter...
1062 * Let's mark this as a TODO.
1064 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
1065 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
1066 continue;
1068 if (param == -1)
1069 param_name = state_name_to_param_name(sm->name, return_str);
1070 else
1071 param_name = get_param_name(sm);
1072 if (!param_name)
1073 continue;
1074 if (strcmp(param_name, "$") == 0) /* The -1 param is handled after the loop */
1075 continue;
1077 sql_insert_return_states(return_id, return_ranges,
1078 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1079 param, param_name, show_rl(estate_rl(sm->state)));
1080 } END_FOR_EACH_SM(sm);
1082 if (points_to_user_data(expr)) {
1083 sql_insert_return_states(return_id, return_ranges,
1084 (is_skb_data(expr) || !func_gets_user_data) ?
1085 USER_DATA3_SET : USER_DATA3,
1086 -1, "*$", "");
1087 } else if (get_user_rl(expr, &rl)) {
1088 sql_insert_return_states(return_id, return_ranges,
1089 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1090 -1, "$", show_rl(rl));
1093 free_string(return_str);
1096 static struct int_stack *gets_data_stack;
1097 static void match_function_def(struct symbol *sym)
1099 func_gets_user_data = false;
1102 static void match_inline_start(struct expression *expr)
1104 push_int(&gets_data_stack, func_gets_user_data);
1107 static void match_inline_end(struct expression *expr)
1109 func_gets_user_data = pop_int(&gets_data_stack);
1112 void check_user_data2(int id)
1114 int i;
1116 my_id = id;
1118 if (option_project != PROJ_KERNEL)
1119 return;
1121 add_hook(&match_function_def, FUNC_DEF_HOOK);
1122 add_hook(&match_inline_start, INLINE_FN_START);
1123 add_hook(&match_inline_end, INLINE_FN_END);
1125 add_hook(&save_start_states, AFTER_DEF_HOOK);
1126 add_hook(&free_start_states, AFTER_FUNC_HOOK);
1127 add_hook(&match_save_states, INLINE_FN_START);
1128 add_hook(&match_restore_states, INLINE_FN_END);
1130 add_unmatched_state_hook(my_id, &empty_state);
1131 add_extra_nomod_hook(&extra_nomod_hook);
1132 add_pre_merge_hook(my_id, &pre_merge_hook);
1133 add_merge_hook(my_id, &merge_estates);
1135 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
1136 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
1137 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1138 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1139 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1141 add_function_assign_hook("simple_strtol", &match_simple_strtoul, NULL);
1142 add_function_assign_hook("simple_strtoll", &match_simple_strtoul, NULL);
1143 add_function_assign_hook("simple_strtoul", &match_simple_strtoul, NULL);
1144 add_function_assign_hook("simple_strtoull", &match_simple_strtoul, NULL);
1146 add_function_hook("sscanf", &match_sscanf, NULL);
1148 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
1149 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
1150 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
1152 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1154 add_hook(&match_assign, ASSIGNMENT_HOOK);
1155 add_hook(&match_condition, CONDITION_HOOK);
1157 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1158 add_member_info_callback(my_id, struct_member_callback);
1159 select_caller_info_hook(set_param_user_data, USER_DATA3);
1160 select_return_states_hook(USER_DATA3, &returns_param_user_data);
1161 select_return_states_hook(USER_DATA3_SET, &returns_param_user_data_set);
1162 add_split_return_callback(&param_set_to_user_data);
1165 void check_user_data3(int id)
1167 my_call_id = id;
1169 if (option_project != PROJ_KERNEL)
1170 return;
1171 select_caller_info_hook(set_called, INTERNAL);