extra: add extra_nomod_hooks
[smatch.git] / check_user_data2.c
blobf8aaac85e86500509a2e561e67d521daefaa7ed9
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;
84 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
85 if (!extra || !estate_rl(extra))
86 return;
87 user = get_state(my_id, sm->name, sm->sym);
88 if (!user || !estate_rl(user))
89 return;
90 rl = rl_intersection(estate_rl(user), estate_rl(extra));
91 if (rl_to_sval(rl, &dummy))
92 rl = NULL;
93 set_state(my_id, sm->name, sm->sym, alloc_estate_rl(clone_rl(rl)));
96 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
98 struct expression *edge_member;
99 struct symbol *base = get_real_base_type(member);
100 struct symbol *tmp;
102 if (member->ident)
103 expr = member_expression(expr, '.', member->ident);
105 FOR_EACH_PTR(base->symbol_list, tmp) {
106 struct symbol *type;
108 type = get_real_base_type(tmp);
109 if (!type)
110 continue;
112 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
113 tag_inner_struct_members(expr, tmp);
114 continue;
117 if (!tmp->ident)
118 continue;
120 edge_member = member_expression(expr, '.', tmp->ident);
121 set_state_expr(my_id, edge_member, alloc_estate_whole(type));
122 } END_FOR_EACH_PTR(tmp);
125 static void tag_struct_members(struct symbol *type, struct expression *expr)
127 struct symbol *tmp;
128 struct expression *member;
129 int op = '*';
131 if (expr->type == EXPR_PREOP && expr->op == '&') {
132 expr = strip_expr(expr->unop);
133 op = '.';
136 FOR_EACH_PTR(type->symbol_list, tmp) {
137 type = get_real_base_type(tmp);
138 if (!type)
139 continue;
141 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
142 tag_inner_struct_members(expr, tmp);
143 continue;
146 if (!tmp->ident)
147 continue;
149 member = member_expression(expr, op, tmp->ident);
150 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
152 if (type->type == SYM_ARRAY)
153 set_points_to_user_data(member);
154 } END_FOR_EACH_PTR(tmp);
157 static void tag_base_type(struct expression *expr)
159 if (expr->type == EXPR_PREOP && expr->op == '&')
160 expr = strip_expr(expr->unop);
161 else
162 expr = deref_expression(expr);
163 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
166 static void tag_as_user_data(struct expression *expr)
168 struct symbol *type;
170 expr = strip_expr(expr);
172 type = get_type(expr);
173 if (!type || type->type != SYM_PTR)
174 return;
175 type = get_real_base_type(type);
176 if (!type)
177 return;
178 if (type == &void_ctype) {
179 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
180 return;
182 if (type->type == SYM_BASETYPE)
183 tag_base_type(expr);
184 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
185 if (expr->type != EXPR_PREOP || expr->op != '&')
186 expr = deref_expression(expr);
187 else
188 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
189 tag_struct_members(type, expr);
193 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
195 int param = PTR_INT(_param);
196 struct expression *dest;
198 func_gets_user_data = true;
200 dest = get_argument_from_call_expr(expr->args, param);
201 dest = strip_expr(dest);
202 if (!dest)
203 return;
204 tag_as_user_data(dest);
207 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
209 struct expression *arg;
210 int i;
212 func_gets_user_data = true;
214 i = -1;
215 FOR_EACH_PTR(expr->args, arg) {
216 i++;
217 if (i < 2)
218 continue;
219 tag_as_user_data(arg);
220 } END_FOR_EACH_PTR(arg);
223 static int points_to_user_data(struct expression *expr)
225 struct smatch_state *state;
226 char buf[256];
227 struct symbol *sym;
228 char *name;
229 int ret = 0;
231 expr = strip_expr(expr);
233 if (expr->type == EXPR_BINOP && expr->op == '+') {
234 if (points_to_user_data(expr->left))
235 return 1;
236 if (points_to_user_data(expr->right))
237 return 1;
238 return 0;
241 name = expr_to_var_sym(expr, &sym);
242 if (!name || !sym)
243 goto free;
244 snprintf(buf, sizeof(buf), "*%s", name);
245 state = get_state(my_id, buf, sym);
246 if (state && estate_rl(state))
247 ret = 1;
248 free:
249 free_string(name);
250 return ret;
253 static void set_points_to_user_data(struct expression *expr)
255 char *name;
256 struct symbol *sym;
257 char buf[256];
259 name = expr_to_var_sym(expr, &sym);
260 if (!name || !sym)
261 goto free;
262 snprintf(buf, sizeof(buf), "*%s", name);
263 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
264 free:
265 free_string(name);
268 static int is_skb_data(struct expression *expr)
270 struct symbol *sym;
272 if (expr->type == EXPR_BINOP && expr->op == '+')
273 return is_skb_data(expr->left);
275 expr = strip_expr(expr);
276 if (!expr)
277 return 0;
278 if (expr->type != EXPR_DEREF || expr->op != '.')
279 return 0;
281 if (!expr->member)
282 return 0;
283 if (strcmp(expr->member->name, "data") != 0)
284 return 0;
286 sym = expr_to_sym(expr->deref);
287 if (!sym)
288 return 0;
289 sym = get_real_base_type(sym);
290 if (!sym || sym->type != SYM_PTR)
291 return 0;
292 sym = get_real_base_type(sym);
293 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
294 return 0;
295 if (strcmp(sym->ident->name, "sk_buff") != 0)
296 return 0;
298 return 1;
301 static int comes_from_skb_data(struct expression *expr)
303 expr = strip_expr(expr);
304 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
305 return 0;
307 expr = strip_expr(expr->unop);
308 if (!expr)
309 return 0;
310 if (expr->type == EXPR_BINOP && expr->op == '+')
311 expr = strip_expr(expr->left);
313 return is_skb_data(expr);
316 static int handle_struct_assignment(struct expression *expr)
318 struct expression *right;
319 struct symbol *left_type, *right_type;
321 left_type = get_type(expr->left);
322 if (!left_type || left_type->type != SYM_PTR)
323 return 0;
324 left_type = get_real_base_type(left_type);
325 if (!left_type)
326 return 0;
327 if (left_type->type != SYM_STRUCT &&
328 left_type->type != SYM_UNION)
329 return 0;
332 * Ignore struct to struct assignments because for those we look at the
333 * individual members.
335 right = strip_expr(expr->right);
336 right_type = get_type(right);
337 if (!right_type || right_type->type != SYM_PTR)
338 return 0;
340 /* If we are assigning struct members then normally that is handled
341 * by fake assignments, however if we cast one struct to a different
342 * of struct then we handle that here.
344 right_type = get_real_base_type(right_type);
345 if (right_type == left_type)
346 return 0;
348 if (!points_to_user_data(right) && !is_skb_data(right))
349 return 0;
351 tag_as_user_data(expr->left);
352 return 1;
355 static int handle_get_user(struct expression *expr)
357 char *name;
358 int ret = 0;
360 name = get_macro_name(expr->pos);
361 if (!name || strcmp(name, "get_user") != 0)
362 return 0;
364 name = expr_to_var(expr->right);
365 if (!name || strcmp(name, "__val_gu") != 0)
366 goto free;
367 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
368 ret = 1;
369 free:
370 free_string(name);
371 return ret;
374 static void match_assign(struct expression *expr)
376 struct range_list *rl;
378 if (is_fake_call(expr->right))
379 return;
380 if (handle_get_user(expr))
381 return;
382 if (points_to_user_data(expr->right))
383 set_points_to_user_data(expr->left);
384 if (handle_struct_assignment(expr))
385 return;
387 if (!get_user_rl(expr->right, &rl))
388 goto clear_old_state;
390 rl = cast_rl(get_type(expr->left), rl);
391 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
393 return;
395 clear_old_state:
396 if (get_state_expr(my_id, expr->left))
397 set_state_expr(my_id, expr->left, alloc_estate_empty());
400 static void handle_eq_noteq(struct expression *expr)
402 struct smatch_state *left_orig, *right_orig;
404 left_orig = get_state_expr(my_id, expr->left);
405 right_orig = get_state_expr(my_id, expr->right);
407 if (!left_orig && !right_orig)
408 return;
409 if (left_orig && right_orig)
410 return;
412 if (left_orig) {
413 set_true_false_states_expr(my_id, expr->left,
414 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
415 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
416 } else {
417 set_true_false_states_expr(my_id, expr->right,
418 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
419 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
423 static void handle_unsigned_lt_gt(struct expression *expr)
425 struct symbol *type;
426 struct range_list *left;
427 struct range_list *right;
428 struct range_list *non_negative;
429 sval_t min, minus_one;
432 * conditions are mostly handled by smatch_extra.c. The special case
433 * here is that say you have if (user_int < unknown_u32) {
434 * In Smatch extra we say that, We have no idea what value
435 * unknown_u32 is so the only thin we can say for sure is that
436 * user_int is not -1 (UINT_MAX). But in check_user_data2.c we should
437 * assume that unless unknown_u32 is user data, it's probably less than
438 * INT_MAX.
442 type = get_type(expr);
443 if (!type_unsigned(type))
444 return;
447 * Assume if (user < trusted) { ... because I am lazy and because this
448 * is the correct way to write code.
450 if (!get_user_rl(expr->left, &left))
451 return;
452 if (get_user_rl(expr->right, &right))
453 return;
455 if (!sval_is_negative(rl_min(left)))
456 return;
457 min = rl_min(left);
458 minus_one.type = rl_type(left);
459 minus_one.value = -1;
460 non_negative = remove_range(left, min, minus_one);
462 switch (expr->op) {
463 case '<':
464 case SPECIAL_UNSIGNED_LT:
465 case SPECIAL_LTE:
466 case SPECIAL_UNSIGNED_LTE:
467 set_true_false_states_expr(my_id, expr->left,
468 alloc_estate_rl(non_negative), NULL);
469 break;
470 case '>':
471 case SPECIAL_UNSIGNED_GT:
472 case SPECIAL_GTE:
473 case SPECIAL_UNSIGNED_GTE:
474 set_true_false_states_expr(my_id, expr->left,
475 NULL, alloc_estate_rl(non_negative));
476 break;
480 static void match_condition(struct expression *expr)
482 if (expr->type != EXPR_COMPARE)
483 return;
485 if (expr->op == SPECIAL_EQUAL ||
486 expr->op == SPECIAL_NOTEQUAL) {
487 handle_eq_noteq(expr);
488 return;
491 handle_unsigned_lt_gt(expr);
494 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
496 func_gets_user_data = true;
498 tag_as_user_data(expr->left);
499 set_points_to_user_data(expr->left);
502 static void match_simple_strtoul(const char *fn, struct expression *expr, void *unused)
504 func_gets_user_data = true;
506 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
509 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
511 char *macro;
513 if (!expr)
514 return 0;
515 macro = get_macro_name(expr->pos);
517 if (!macro)
518 return 0;
520 if (strcmp(macro, "ntohl") == 0) {
521 *rl = alloc_whole_rl(&uint_ctype);
522 return 1;
524 if (strcmp(macro, "ntohs") == 0) {
525 *rl = alloc_whole_rl(&ushort_ctype);
526 return 1;
528 return 0;
531 struct db_info {
532 struct range_list *rl;
533 struct expression *call;
535 static int returned_rl_callback(void *_info, int argc, char **argv, char **azColName)
537 struct db_info *db_info = _info;
538 struct range_list *rl;
539 char *return_ranges = argv[0];
540 char *user_ranges = argv[1];
541 struct expression *arg;
542 int comparison;
544 if (argc != 2)
545 return 0;
547 call_results_to_rl(db_info->call, get_type(db_info->call), user_ranges, &rl);
548 if (str_to_comparison_arg(return_ranges, db_info->call, &comparison, &arg) &&
549 comparison == SPECIAL_EQUAL) {
550 struct range_list *orig_rl;
552 if (!get_user_rl(arg, &orig_rl))
553 return 0;
554 rl = rl_intersection(rl, orig_rl);
555 if (!rl)
556 return 0;
558 db_info->rl = rl_union(db_info->rl, rl);
560 return 0;
563 static int has_user_data(struct symbol *sym)
565 struct sm_state *tmp;
567 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
568 if (tmp->sym == sym)
569 return 1;
570 } END_FOR_EACH_SM(tmp);
571 return 0;
574 static int we_pass_user_data(struct expression *call)
576 struct expression *arg;
577 struct symbol *sym;
579 FOR_EACH_PTR(call->args, arg) {
580 sym = expr_to_sym(arg);
581 if (!sym)
582 continue;
583 if (has_user_data(sym))
584 return 1;
585 } END_FOR_EACH_PTR(arg);
587 return 0;
590 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
592 struct db_info db_info = {};
594 /* for function pointers assume everything is used */
595 if (call->fn->type != EXPR_SYMBOL)
596 return 0;
597 if (is_fake_call(call))
598 return 0;
600 db_info.call = call;
601 run_sql(&returned_rl_callback, &db_info,
602 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
603 get_static_filter(call->fn->symbol), USER_DATA3_SET);
604 if (db_info.rl) {
605 func_gets_user_data = true;
606 *rl = db_info.rl;
607 return 1;
610 run_sql(&returned_rl_callback, &db_info,
611 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
612 get_static_filter(call->fn->symbol), USER_DATA3);
613 if (db_info.rl) {
614 if (!we_pass_user_data(call))
615 return 0;
616 *rl = db_info.rl;
617 return 1;
620 return 0;
623 static int user_data_flag;
624 static struct range_list *var_user_rl(struct expression *expr)
626 struct smatch_state *state;
627 struct range_list *rl;
628 struct range_list *absolute_rl;
630 if (expr->type == EXPR_BINOP && expr->op == '%') {
631 struct range_list *left, *right;
633 if (!get_user_rl(expr->right, &right))
634 return NULL;
635 get_absolute_rl(expr->left, &left);
636 rl = rl_binop(left, '%', right);
637 goto found;
640 if (get_user_macro_rl(expr, &rl))
641 goto found;
643 if (comes_from_skb_data(expr)) {
644 rl = alloc_whole_rl(get_type(expr));
645 goto found;
648 state = get_state_expr(my_id, expr);
649 if (state && estate_rl(state)) {
650 rl = estate_rl(state);
651 goto found;
654 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
655 goto found;
657 return NULL;
658 found:
659 user_data_flag = 1;
660 absolute_rl = var_to_absolute_rl(expr);
661 return clone_rl(rl_intersection(rl, absolute_rl));
664 int get_user_rl(struct expression *expr, struct range_list **rl)
667 user_data_flag = 0;
668 custom_get_absolute_rl(expr, &var_user_rl, rl);
669 if (!user_data_flag) {
670 *rl = NULL;
671 return 0;
673 return 1;
676 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
678 struct smatch_state *state;
680 state = get_state(my_id, name, sym);
681 if (state && estate_rl(state)) {
682 *rl = estate_rl(state);
683 return 1;
685 return 0;
688 static void match_call_info(struct expression *expr)
690 struct range_list *rl;
691 struct expression *arg;
692 int i = 0;
694 i = -1;
695 FOR_EACH_PTR(expr->args, arg) {
696 i++;
698 if (!get_user_rl(arg, &rl))
699 continue;
701 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
702 } END_FOR_EACH_PTR(arg);
705 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
707 struct smatch_state *state;
708 struct range_list *rl;
710 if (strcmp(sm->state->name, "") == 0)
711 return;
713 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
714 if (!state || !estate_rl(state))
715 rl = estate_rl(sm->state);
716 else
717 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
719 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
722 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
724 struct range_list *rl = NULL;
725 struct smatch_state *state;
726 struct symbol *type;
727 char fullname[256];
729 if (strcmp(key, "*$") == 0)
730 snprintf(fullname, sizeof(fullname), "*%s", name);
731 else if (strncmp(key, "$", 1) == 0)
732 snprintf(fullname, 256, "%s%s", name, key + 1);
733 else
734 return;
736 type = get_member_type_from_key(symbol_expression(sym), key);
738 /* if the caller passes a void pointer with user data */
739 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
740 struct expression *expr = symbol_expression(sym);
742 tag_as_user_data(expr);
743 set_points_to_user_data(expr);
744 return;
746 str_to_rl(type, value, &rl);
747 state = alloc_estate_rl(rl);
748 set_state(my_id, fullname, sym, state);
751 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
753 set_state(my_call_id, "this_function", NULL, &called);
756 static void match_syscall_definition(struct symbol *sym)
758 struct symbol *arg;
759 char *macro;
760 char *name;
761 int is_syscall = 0;
763 macro = get_macro_name(sym->pos);
764 if (macro &&
765 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
766 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
767 is_syscall = 1;
769 name = get_function();
770 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
771 if (name && strncmp(name, "sys_", 4) == 0)
772 is_syscall = 1;
775 if (name && strncmp(name, "compat_sys_", 11) == 0)
776 is_syscall = 1;
778 if (!is_syscall)
779 return;
781 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
782 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
783 } END_FOR_EACH_PTR(arg);
786 static void set_to_user_data(struct expression *expr, char *key, char *value)
788 char *name;
789 struct symbol *sym;
790 struct symbol *type;
791 struct range_list *rl = NULL;
793 type = get_member_type_from_key(expr, key);
794 name = get_variable_from_key(expr, key, &sym);
795 if (!name || !sym)
796 goto free;
798 call_results_to_rl(expr, type, value, &rl);
800 set_state(my_id, name, sym, alloc_estate_rl(rl));
801 free:
802 free_string(name);
806 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
808 struct expression *arg;
809 struct expression *call;
811 call = expr;
812 while (call->type == EXPR_ASSIGNMENT)
813 call = strip_expr(call->right);
814 if (call->type != EXPR_CALL)
815 return;
817 if (!we_pass_user_data(call))
818 return;
820 if (param == -1) {
821 if (expr->type != EXPR_ASSIGNMENT)
822 return;
823 set_to_user_data(expr->left, key, value);
824 return;
827 arg = get_argument_from_call_expr(call->args, param);
828 if (!arg)
829 return;
830 set_to_user_data(arg, key, value);
833 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
835 struct expression *arg;
837 func_gets_user_data = true;
839 if (param == -1) {
840 if (expr->type != EXPR_ASSIGNMENT)
841 return;
842 set_to_user_data(expr->left, key, value);
843 return;
846 while (expr->type == EXPR_ASSIGNMENT)
847 expr = strip_expr(expr->right);
848 if (expr->type != EXPR_CALL)
849 return;
851 arg = get_argument_from_call_expr(expr->args, param);
852 if (!arg)
853 return;
854 set_to_user_data(arg, key, value);
857 static int has_empty_state(struct sm_state *sm)
859 struct sm_state *tmp;
861 FOR_EACH_PTR(sm->possible, tmp) {
862 if (!estate_rl(tmp->state))
863 return 1;
864 } END_FOR_EACH_PTR(tmp);
866 return 0;
869 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
871 struct sm_state *sm;
872 struct smatch_state *start_state;
873 struct range_list *rl;
874 int param;
875 const char *param_name;
877 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
878 if (has_empty_state(sm))
879 continue;
881 param = get_param_num_from_sym(sm->sym);
882 if (param < 0) {
883 if (expr_to_sym(expr) == sm->sym)
884 param = -1;
885 else
886 continue;
889 /* The logic here was that if we were passed in a user data then
890 * we don't record that. It's like the difference between
891 * param_filter and param_set. When I think about it, I'm not
892 * sure it actually works. It's probably harmless because we
893 * checked earlier that we're not returning a parameter...
894 * Let's mark this as a TODO.
896 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
897 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
898 continue;
900 param_name = get_param_name(sm);
901 if (!param_name)
902 continue;
903 if (strcmp(param_name, "$") == 0)
904 continue;
906 sql_insert_return_states(return_id, return_ranges,
907 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
908 param, param_name, show_rl(estate_rl(sm->state)));
909 } END_FOR_EACH_SM(sm);
911 if (get_user_rl(expr, &rl)) {
912 sql_insert_return_states(return_id, return_ranges,
913 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
914 -1, "$", show_rl(rl));
918 static struct int_stack *gets_data_stack;
919 static void match_function_def(struct symbol *sym)
921 func_gets_user_data = false;
924 static void match_inline_start(struct expression *expr)
926 push_int(&gets_data_stack, func_gets_user_data);
929 static void match_inline_end(struct expression *expr)
931 func_gets_user_data = pop_int(&gets_data_stack);
934 void check_user_data2(int id)
936 int i;
938 my_id = id;
940 if (option_project != PROJ_KERNEL)
941 return;
943 add_hook(&match_function_def, FUNC_DEF_HOOK);
944 add_hook(&match_inline_start, INLINE_FN_START);
945 add_hook(&match_inline_end, INLINE_FN_END);
947 add_hook(&save_start_states, AFTER_DEF_HOOK);
948 add_hook(&free_start_states, AFTER_FUNC_HOOK);
949 add_hook(&match_save_states, INLINE_FN_START);
950 add_hook(&match_restore_states, INLINE_FN_END);
952 add_unmatched_state_hook(my_id, &empty_state);
953 add_pre_merge_hook(my_id, &pre_merge_hook);
954 add_merge_hook(my_id, &merge_estates);
956 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
957 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
958 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
959 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
960 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
962 add_function_assign_hook("simple_strtol", &match_simple_strtoul, NULL);
963 add_function_assign_hook("simple_strtoll", &match_simple_strtoul, NULL);
964 add_function_assign_hook("simple_strtoul", &match_simple_strtoul, NULL);
965 add_function_assign_hook("simple_strtoull", &match_simple_strtoul, NULL);
967 add_function_hook("sscanf", &match_sscanf, NULL);
969 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
970 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
971 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
973 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
975 add_hook(&match_assign, ASSIGNMENT_HOOK);
976 add_hook(&match_condition, CONDITION_HOOK);
978 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
979 add_member_info_callback(my_id, struct_member_callback);
980 select_caller_info_hook(set_param_user_data, USER_DATA3);
981 select_return_states_hook(USER_DATA3, &returns_param_user_data);
982 select_return_states_hook(USER_DATA3_SET, &returns_param_user_data_set);
983 add_split_return_callback(&param_set_to_user_data);
986 void check_user_data3(int id)
988 my_call_id = id;
990 if (option_project != PROJ_KERNEL)
991 return;
992 select_caller_info_hook(set_called, INTERNAL);