assigned_expr: handle fake assignments better
[smatch.git] / check_user_data2.c
blobb8b795f121f157e1590f92751c8963846ebac04e
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 extra_nomod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
98 struct smatch_state *user;
99 struct range_list *rl;
101 user = get_state(my_id, name, sym);
102 if (!user)
103 return;
104 rl = rl_intersection(estate_rl(user), estate_rl(state));
105 if (rl_equiv(rl, estate_rl(user)))
106 return;
107 set_state(my_id, name, sym, alloc_estate_rl(rl));
110 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
112 struct expression *edge_member;
113 struct symbol *base = get_real_base_type(member);
114 struct symbol *tmp;
116 if (member->ident)
117 expr = member_expression(expr, '.', member->ident);
119 FOR_EACH_PTR(base->symbol_list, tmp) {
120 struct symbol *type;
122 type = get_real_base_type(tmp);
123 if (!type)
124 continue;
126 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
127 tag_inner_struct_members(expr, tmp);
128 continue;
131 if (!tmp->ident)
132 continue;
134 edge_member = member_expression(expr, '.', tmp->ident);
135 set_state_expr(my_id, edge_member, alloc_estate_whole(type));
136 } END_FOR_EACH_PTR(tmp);
139 static void tag_struct_members(struct symbol *type, struct expression *expr)
141 struct symbol *tmp;
142 struct expression *member;
143 int op = '*';
145 if (expr->type == EXPR_PREOP && expr->op == '&') {
146 expr = strip_expr(expr->unop);
147 op = '.';
150 FOR_EACH_PTR(type->symbol_list, tmp) {
151 type = get_real_base_type(tmp);
152 if (!type)
153 continue;
155 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
156 tag_inner_struct_members(expr, tmp);
157 continue;
160 if (!tmp->ident)
161 continue;
163 member = member_expression(expr, op, tmp->ident);
164 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
166 if (type->type == SYM_ARRAY)
167 set_points_to_user_data(member);
168 } END_FOR_EACH_PTR(tmp);
171 static void tag_base_type(struct expression *expr)
173 if (expr->type == EXPR_PREOP && expr->op == '&')
174 expr = strip_expr(expr->unop);
175 else
176 expr = deref_expression(expr);
177 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
180 static void tag_as_user_data(struct expression *expr)
182 struct symbol *type;
184 expr = strip_expr(expr);
186 type = get_type(expr);
187 if (!type || type->type != SYM_PTR)
188 return;
189 type = get_real_base_type(type);
190 if (!type)
191 return;
192 if (type == &void_ctype) {
193 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
194 return;
196 if (type->type == SYM_BASETYPE)
197 tag_base_type(expr);
198 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
199 if (expr->type != EXPR_PREOP || expr->op != '&')
200 expr = deref_expression(expr);
201 else
202 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
203 tag_struct_members(type, expr);
207 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
209 int param = PTR_INT(_param);
210 struct expression *dest;
212 func_gets_user_data = true;
214 dest = get_argument_from_call_expr(expr->args, param);
215 dest = strip_expr(dest);
216 if (!dest)
217 return;
218 tag_as_user_data(dest);
221 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
223 struct expression *arg;
224 int i;
226 func_gets_user_data = true;
228 i = -1;
229 FOR_EACH_PTR(expr->args, arg) {
230 i++;
231 if (i < 2)
232 continue;
233 tag_as_user_data(arg);
234 } END_FOR_EACH_PTR(arg);
237 static int is_skb_data(struct expression *expr)
239 struct symbol *sym;
241 if (!expr)
242 return 0;
244 if (expr->type == EXPR_BINOP && expr->op == '+')
245 return is_skb_data(expr->left);
247 expr = strip_expr(expr);
248 if (!expr)
249 return 0;
250 if (expr->type != EXPR_DEREF || expr->op != '.')
251 return 0;
253 if (!expr->member)
254 return 0;
255 if (strcmp(expr->member->name, "data") != 0)
256 return 0;
258 sym = expr_to_sym(expr->deref);
259 if (!sym)
260 return 0;
261 sym = get_real_base_type(sym);
262 if (!sym || sym->type != SYM_PTR)
263 return 0;
264 sym = get_real_base_type(sym);
265 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
266 return 0;
267 if (strcmp(sym->ident->name, "sk_buff") != 0)
268 return 0;
270 return 1;
273 static int points_to_user_data(struct expression *expr)
275 struct smatch_state *state;
276 char buf[256];
277 struct symbol *sym;
278 char *name;
279 int ret = 0;
281 expr = strip_expr(expr);
282 if (!expr)
283 return 0;
284 if (is_skb_data(expr))
285 return 1;
287 if (expr->type == EXPR_BINOP && expr->op == '+') {
288 if (points_to_user_data(expr->left))
289 return 1;
290 if (points_to_user_data(expr->right))
291 return 1;
292 return 0;
295 name = expr_to_var_sym(expr, &sym);
296 if (!name || !sym)
297 goto free;
298 snprintf(buf, sizeof(buf), "*%s", name);
299 state = get_state(my_id, buf, sym);
300 if (state && estate_rl(state))
301 ret = 1;
302 free:
303 free_string(name);
304 return ret;
307 static void set_points_to_user_data(struct expression *expr)
309 char *name;
310 struct symbol *sym;
311 char buf[256];
313 name = expr_to_var_sym(expr, &sym);
314 if (!name || !sym)
315 goto free;
316 snprintf(buf, sizeof(buf), "*%s", name);
317 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
318 free:
319 free_string(name);
322 static int comes_from_skb_data(struct expression *expr)
324 expr = strip_expr(expr);
325 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
326 return 0;
328 expr = strip_expr(expr->unop);
329 if (!expr)
330 return 0;
331 if (expr->type == EXPR_BINOP && expr->op == '+')
332 expr = strip_expr(expr->left);
334 return is_skb_data(expr);
337 static int handle_struct_assignment(struct expression *expr)
339 struct expression *right;
340 struct symbol *left_type, *right_type;
342 left_type = get_type(expr->left);
343 if (!left_type || left_type->type != SYM_PTR)
344 return 0;
345 left_type = get_real_base_type(left_type);
346 if (!left_type)
347 return 0;
348 if (left_type->type != SYM_STRUCT &&
349 left_type->type != SYM_UNION)
350 return 0;
353 * Ignore struct to struct assignments because for those we look at the
354 * individual members.
356 right = strip_expr(expr->right);
357 right_type = get_type(right);
358 if (!right_type || right_type->type != SYM_PTR)
359 return 0;
361 /* If we are assigning struct members then normally that is handled
362 * by fake assignments, however if we cast one struct to a different
363 * of struct then we handle that here.
365 right_type = get_real_base_type(right_type);
366 if (right_type == left_type)
367 return 0;
369 if (!points_to_user_data(right))
370 return 0;
372 tag_as_user_data(expr->left);
373 return 1;
376 static int handle_get_user(struct expression *expr)
378 char *name;
379 int ret = 0;
381 name = get_macro_name(expr->pos);
382 if (!name || strcmp(name, "get_user") != 0)
383 return 0;
385 name = expr_to_var(expr->right);
386 if (!name || strcmp(name, "__val_gu") != 0)
387 goto free;
388 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
389 ret = 1;
390 free:
391 free_string(name);
392 return ret;
395 static void match_assign(struct expression *expr)
397 struct range_list *rl;
399 if (is_fake_call(expr->right))
400 return;
401 if (handle_get_user(expr))
402 return;
403 if (points_to_user_data(expr->right))
404 set_points_to_user_data(expr->left);
405 if (handle_struct_assignment(expr))
406 return;
408 if (!get_user_rl(expr->right, &rl))
409 goto clear_old_state;
411 rl = cast_rl(get_type(expr->left), rl);
412 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
414 return;
416 clear_old_state:
417 if (get_state_expr(my_id, expr->left))
418 set_state_expr(my_id, expr->left, alloc_estate_empty());
421 static void handle_eq_noteq(struct expression *expr)
423 struct smatch_state *left_orig, *right_orig;
425 left_orig = get_state_expr(my_id, expr->left);
426 right_orig = get_state_expr(my_id, expr->right);
428 if (!left_orig && !right_orig)
429 return;
430 if (left_orig && right_orig)
431 return;
433 if (left_orig) {
434 set_true_false_states_expr(my_id, expr->left,
435 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
436 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
437 } else {
438 set_true_false_states_expr(my_id, expr->right,
439 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
440 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
444 static void handle_unsigned_lt_gt(struct expression *expr)
446 struct symbol *type;
447 struct range_list *left;
448 struct range_list *right;
449 struct range_list *non_negative;
450 sval_t min, minus_one;
453 * conditions are mostly handled by smatch_extra.c. The special case
454 * here is that say you have if (user_int < unknown_u32) {
455 * In Smatch extra we say that, We have no idea what value
456 * unknown_u32 is so the only thin we can say for sure is that
457 * user_int is not -1 (UINT_MAX). But in check_user_data2.c we should
458 * assume that unless unknown_u32 is user data, it's probably less than
459 * INT_MAX.
463 type = get_type(expr);
464 if (!type_unsigned(type))
465 return;
468 * Assume if (user < trusted) { ... because I am lazy and because this
469 * is the correct way to write code.
471 if (!get_user_rl(expr->left, &left))
472 return;
473 if (get_user_rl(expr->right, &right))
474 return;
476 if (!sval_is_negative(rl_min(left)))
477 return;
478 min = rl_min(left);
479 minus_one.type = rl_type(left);
480 minus_one.value = -1;
481 non_negative = remove_range(left, min, minus_one);
483 switch (expr->op) {
484 case '<':
485 case SPECIAL_UNSIGNED_LT:
486 case SPECIAL_LTE:
487 case SPECIAL_UNSIGNED_LTE:
488 set_true_false_states_expr(my_id, expr->left,
489 alloc_estate_rl(non_negative), NULL);
490 break;
491 case '>':
492 case SPECIAL_UNSIGNED_GT:
493 case SPECIAL_GTE:
494 case SPECIAL_UNSIGNED_GTE:
495 set_true_false_states_expr(my_id, expr->left,
496 NULL, alloc_estate_rl(non_negative));
497 break;
501 static void match_condition(struct expression *expr)
503 if (expr->type != EXPR_COMPARE)
504 return;
506 if (expr->op == SPECIAL_EQUAL ||
507 expr->op == SPECIAL_NOTEQUAL) {
508 handle_eq_noteq(expr);
509 return;
512 handle_unsigned_lt_gt(expr);
515 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
517 func_gets_user_data = true;
519 tag_as_user_data(expr->left);
520 set_points_to_user_data(expr->left);
523 static void match_simple_strtoul(const char *fn, struct expression *expr, void *unused)
525 func_gets_user_data = true;
527 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
530 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
532 char *macro;
534 if (!expr)
535 return 0;
536 macro = get_macro_name(expr->pos);
538 if (!macro)
539 return 0;
541 if (strcmp(macro, "ntohl") == 0) {
542 *rl = alloc_whole_rl(&uint_ctype);
543 return 1;
545 if (strcmp(macro, "ntohs") == 0) {
546 *rl = alloc_whole_rl(&ushort_ctype);
547 return 1;
549 return 0;
552 struct db_info {
553 struct range_list *rl;
554 struct expression *call;
556 static int returned_rl_callback(void *_info, int argc, char **argv, char **azColName)
558 struct db_info *db_info = _info;
559 struct range_list *rl;
560 char *return_ranges = argv[0];
561 char *user_ranges = argv[1];
562 struct expression *arg;
563 int comparison;
565 if (argc != 2)
566 return 0;
568 call_results_to_rl(db_info->call, get_type(db_info->call), user_ranges, &rl);
569 if (str_to_comparison_arg(return_ranges, db_info->call, &comparison, &arg) &&
570 comparison == SPECIAL_EQUAL) {
571 struct range_list *orig_rl;
573 if (!get_user_rl(arg, &orig_rl))
574 return 0;
575 rl = rl_intersection(rl, orig_rl);
576 if (!rl)
577 return 0;
579 db_info->rl = rl_union(db_info->rl, rl);
581 return 0;
584 static int has_user_data(struct symbol *sym)
586 struct sm_state *tmp;
588 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
589 if (tmp->sym == sym)
590 return 1;
591 } END_FOR_EACH_SM(tmp);
592 return 0;
595 static int we_pass_user_data(struct expression *call)
597 struct expression *arg;
598 struct symbol *sym;
600 FOR_EACH_PTR(call->args, arg) {
601 sym = expr_to_sym(arg);
602 if (!sym)
603 continue;
604 if (has_user_data(sym))
605 return 1;
606 } END_FOR_EACH_PTR(arg);
608 return 0;
611 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
613 struct db_info db_info = {};
615 /* for function pointers assume everything is used */
616 if (call->fn->type != EXPR_SYMBOL)
617 return 0;
618 if (is_fake_call(call))
619 return 0;
621 db_info.call = call;
622 run_sql(&returned_rl_callback, &db_info,
623 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
624 get_static_filter(call->fn->symbol), USER_DATA3_SET);
625 if (db_info.rl) {
626 func_gets_user_data = true;
627 *rl = db_info.rl;
628 return 1;
631 run_sql(&returned_rl_callback, &db_info,
632 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
633 get_static_filter(call->fn->symbol), USER_DATA3);
634 if (db_info.rl) {
635 if (!we_pass_user_data(call))
636 return 0;
637 *rl = db_info.rl;
638 return 1;
641 return 0;
644 static int user_data_flag;
645 static struct range_list *var_user_rl(struct expression *expr)
647 struct smatch_state *state;
648 struct range_list *rl;
649 struct range_list *absolute_rl;
651 if (expr->type == EXPR_BINOP && expr->op == '%') {
652 struct range_list *left, *right;
654 if (!get_user_rl(expr->right, &right))
655 return NULL;
656 get_absolute_rl(expr->left, &left);
657 rl = rl_binop(left, '%', right);
658 goto found;
661 if (get_user_macro_rl(expr, &rl))
662 goto found;
664 if (comes_from_skb_data(expr)) {
665 rl = alloc_whole_rl(get_type(expr));
666 goto found;
669 state = get_state_expr(my_id, expr);
670 if (state && estate_rl(state)) {
671 rl = estate_rl(state);
672 goto found;
675 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
676 goto found;
678 return NULL;
679 found:
680 user_data_flag = 1;
681 absolute_rl = var_to_absolute_rl(expr);
682 return clone_rl(rl_intersection(rl, absolute_rl));
685 int get_user_rl(struct expression *expr, struct range_list **rl)
688 user_data_flag = 0;
689 custom_get_absolute_rl(expr, &var_user_rl, rl);
690 if (!user_data_flag || !*rl) {
691 *rl = NULL;
692 return 0;
694 return 1;
697 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
699 struct smatch_state *state;
701 state = get_state(my_id, name, sym);
702 if (state && estate_rl(state)) {
703 *rl = estate_rl(state);
704 return 1;
706 return 0;
709 static void match_call_info(struct expression *expr)
711 struct range_list *rl;
712 struct expression *arg;
713 int i = 0;
715 i = -1;
716 FOR_EACH_PTR(expr->args, arg) {
717 i++;
719 if (!get_user_rl(arg, &rl))
720 continue;
722 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
723 } END_FOR_EACH_PTR(arg);
726 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
728 struct smatch_state *state;
729 struct range_list *rl;
731 if (strcmp(sm->state->name, "") == 0)
732 return;
734 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
735 if (!state || !estate_rl(state))
736 rl = estate_rl(sm->state);
737 else
738 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
740 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
743 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
745 struct range_list *rl = NULL;
746 struct smatch_state *state;
747 struct symbol *type;
748 char fullname[256];
750 if (strcmp(key, "*$") == 0)
751 snprintf(fullname, sizeof(fullname), "*%s", name);
752 else if (strncmp(key, "$", 1) == 0)
753 snprintf(fullname, 256, "%s%s", name, key + 1);
754 else
755 return;
757 type = get_member_type_from_key(symbol_expression(sym), key);
759 /* if the caller passes a void pointer with user data */
760 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
761 struct expression *expr = symbol_expression(sym);
763 tag_as_user_data(expr);
764 set_points_to_user_data(expr);
765 return;
767 str_to_rl(type, value, &rl);
768 state = alloc_estate_rl(rl);
769 set_state(my_id, fullname, sym, state);
772 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
774 set_state(my_call_id, "this_function", NULL, &called);
777 static void match_syscall_definition(struct symbol *sym)
779 struct symbol *arg;
780 char *macro;
781 char *name;
782 int is_syscall = 0;
784 macro = get_macro_name(sym->pos);
785 if (macro &&
786 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
787 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
788 is_syscall = 1;
790 name = get_function();
791 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
792 if (name && strncmp(name, "sys_", 4) == 0)
793 is_syscall = 1;
796 if (name && strncmp(name, "compat_sys_", 11) == 0)
797 is_syscall = 1;
799 if (!is_syscall)
800 return;
802 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
803 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
804 } END_FOR_EACH_PTR(arg);
807 static void set_to_user_data(struct expression *expr, char *key, char *value)
809 char *name;
810 struct symbol *sym;
811 struct symbol *type;
812 struct range_list *rl = NULL;
814 type = get_member_type_from_key(expr, key);
815 name = get_variable_from_key(expr, key, &sym);
816 if (!name || !sym)
817 goto free;
819 call_results_to_rl(expr, type, value, &rl);
821 set_state(my_id, name, sym, alloc_estate_rl(rl));
822 free:
823 free_string(name);
827 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
829 struct expression *arg;
830 struct expression *call;
832 call = expr;
833 while (call->type == EXPR_ASSIGNMENT)
834 call = strip_expr(call->right);
835 if (call->type != EXPR_CALL)
836 return;
838 if (!we_pass_user_data(call))
839 return;
841 if (param == -1) {
842 if (expr->type != EXPR_ASSIGNMENT)
843 return;
844 set_to_user_data(expr->left, key, value);
845 return;
848 arg = get_argument_from_call_expr(call->args, param);
849 if (!arg)
850 return;
851 set_to_user_data(arg, key, value);
854 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
856 struct expression *arg;
858 func_gets_user_data = true;
860 if (param == -1) {
861 if (expr->type != EXPR_ASSIGNMENT)
862 return;
863 if (strcmp(key, "*$") == 0) {
864 set_points_to_user_data(expr->left);
865 tag_as_user_data(expr->left);
866 } else {
867 set_to_user_data(expr->left, key, value);
869 return;
872 while (expr->type == EXPR_ASSIGNMENT)
873 expr = strip_expr(expr->right);
874 if (expr->type != EXPR_CALL)
875 return;
877 arg = get_argument_from_call_expr(expr->args, param);
878 if (!arg)
879 return;
880 set_to_user_data(arg, key, value);
883 static int has_empty_state(struct sm_state *sm)
885 struct sm_state *tmp;
887 FOR_EACH_PTR(sm->possible, tmp) {
888 if (!estate_rl(tmp->state))
889 return 1;
890 } END_FOR_EACH_PTR(tmp);
892 return 0;
895 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
897 struct sm_state *sm;
898 struct smatch_state *start_state;
899 struct range_list *rl;
900 int param;
901 char *return_str;
902 const char *param_name;
904 expr = strip_expr(expr);
905 return_str = expr_to_str(expr);
907 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
908 if (has_empty_state(sm))
909 continue;
911 param = get_param_num_from_sym(sm->sym);
912 if (param < 0) {
913 if (expr_to_sym(expr) == sm->sym)
914 param = -1;
915 else
916 continue;
919 /* The logic here was that if we were passed in a user data then
920 * we don't record that. It's like the difference between
921 * param_filter and param_set. When I think about it, I'm not
922 * sure it actually works. It's probably harmless because we
923 * checked earlier that we're not returning a parameter...
924 * Let's mark this as a TODO.
926 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
927 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
928 continue;
930 if (param == -1)
931 param_name = state_name_to_param_name(sm->name, return_str);
932 else
933 param_name = get_param_name(sm);
934 if (!param_name)
935 continue;
936 if (strcmp(param_name, "$") == 0) /* The -1 param is handled after the loop */
937 continue;
939 sql_insert_return_states(return_id, return_ranges,
940 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
941 param, param_name, show_rl(estate_rl(sm->state)));
942 } END_FOR_EACH_SM(sm);
944 if (points_to_user_data(expr)) {
945 sql_insert_return_states(return_id, return_ranges,
946 (is_skb_data(expr) || !func_gets_user_data) ?
947 USER_DATA3_SET : USER_DATA3,
948 -1, "*$", "");
949 } else if (get_user_rl(expr, &rl)) {
950 sql_insert_return_states(return_id, return_ranges,
951 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
952 -1, "$", show_rl(rl));
955 free_string(return_str);
958 static struct int_stack *gets_data_stack;
959 static void match_function_def(struct symbol *sym)
961 func_gets_user_data = false;
964 static void match_inline_start(struct expression *expr)
966 push_int(&gets_data_stack, func_gets_user_data);
969 static void match_inline_end(struct expression *expr)
971 func_gets_user_data = pop_int(&gets_data_stack);
974 void check_user_data2(int id)
976 int i;
978 my_id = id;
980 if (option_project != PROJ_KERNEL)
981 return;
983 add_hook(&match_function_def, FUNC_DEF_HOOK);
984 add_hook(&match_inline_start, INLINE_FN_START);
985 add_hook(&match_inline_end, INLINE_FN_END);
987 add_hook(&save_start_states, AFTER_DEF_HOOK);
988 add_hook(&free_start_states, AFTER_FUNC_HOOK);
989 add_hook(&match_save_states, INLINE_FN_START);
990 add_hook(&match_restore_states, INLINE_FN_END);
992 add_unmatched_state_hook(my_id, &empty_state);
993 add_extra_nomod_hook(&extra_nomod_hook);
994 add_pre_merge_hook(my_id, &pre_merge_hook);
995 add_merge_hook(my_id, &merge_estates);
997 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
998 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
999 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1000 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1001 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1003 add_function_assign_hook("simple_strtol", &match_simple_strtoul, NULL);
1004 add_function_assign_hook("simple_strtoll", &match_simple_strtoul, NULL);
1005 add_function_assign_hook("simple_strtoul", &match_simple_strtoul, NULL);
1006 add_function_assign_hook("simple_strtoull", &match_simple_strtoul, NULL);
1008 add_function_hook("sscanf", &match_sscanf, NULL);
1010 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
1011 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
1012 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
1014 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1016 add_hook(&match_assign, ASSIGNMENT_HOOK);
1017 add_hook(&match_condition, CONDITION_HOOK);
1019 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1020 add_member_info_callback(my_id, struct_member_callback);
1021 select_caller_info_hook(set_param_user_data, USER_DATA3);
1022 select_return_states_hook(USER_DATA3, &returns_param_user_data);
1023 select_return_states_hook(USER_DATA3_SET, &returns_param_user_data_set);
1024 add_split_return_callback(&param_set_to_user_data);
1027 void check_user_data3(int id)
1029 my_call_id = id;
1031 if (option_project != PROJ_KERNEL)
1032 return;
1033 select_caller_info_hook(set_called, INTERNAL);