db/constraints_required.schema: add missing semi-colon
[smatch.git] / check_user_data2.c
blob29f7b051f1411bcd4126ba4c57ad15d43fa7faaf
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 void match_sscanf(const char *fn, struct expression *expr, void *unused)
249 struct expression *arg;
250 int i;
252 func_gets_user_data = true;
254 i = -1;
255 FOR_EACH_PTR(expr->args, arg) {
256 i++;
257 if (i < 2)
258 continue;
259 tag_as_user_data(arg);
260 } END_FOR_EACH_PTR(arg);
263 static int is_skb_data(struct expression *expr)
265 struct symbol *sym;
267 if (!expr)
268 return 0;
270 if (expr->type == EXPR_BINOP && expr->op == '+')
271 return is_skb_data(expr->left);
273 expr = strip_expr(expr);
274 if (!expr)
275 return 0;
276 if (expr->type != EXPR_DEREF || expr->op != '.')
277 return 0;
279 if (!expr->member)
280 return 0;
281 if (strcmp(expr->member->name, "data") != 0)
282 return 0;
284 sym = expr_to_sym(expr->deref);
285 if (!sym)
286 return 0;
287 sym = get_real_base_type(sym);
288 if (!sym || sym->type != SYM_PTR)
289 return 0;
290 sym = get_real_base_type(sym);
291 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
292 return 0;
293 if (strcmp(sym->ident->name, "sk_buff") != 0)
294 return 0;
296 return 1;
299 static int points_to_user_data(struct expression *expr)
301 struct smatch_state *state;
302 char buf[256];
303 struct symbol *sym;
304 char *name;
305 int ret = 0;
307 expr = strip_expr(expr);
308 if (!expr)
309 return 0;
310 if (is_skb_data(expr))
311 return 1;
313 if (expr->type == EXPR_BINOP && expr->op == '+') {
314 if (points_to_user_data(expr->left))
315 return 1;
316 if (points_to_user_data(expr->right))
317 return 1;
318 return 0;
321 name = expr_to_var_sym(expr, &sym);
322 if (!name || !sym)
323 goto free;
324 snprintf(buf, sizeof(buf), "*%s", name);
325 state = get_state(my_id, buf, sym);
326 if (state && estate_rl(state))
327 ret = 1;
328 free:
329 free_string(name);
330 return ret;
333 static void set_points_to_user_data(struct expression *expr)
335 char *name;
336 struct symbol *sym;
337 char buf[256];
339 name = expr_to_var_sym(expr, &sym);
340 if (!name || !sym)
341 goto free;
342 snprintf(buf, sizeof(buf), "*%s", name);
343 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
344 free:
345 free_string(name);
348 static int comes_from_skb_data(struct expression *expr)
350 expr = strip_expr(expr);
351 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
352 return 0;
354 expr = strip_expr(expr->unop);
355 if (!expr)
356 return 0;
357 if (expr->type == EXPR_BINOP && expr->op == '+')
358 expr = strip_expr(expr->left);
360 return is_skb_data(expr);
363 static int handle_struct_assignment(struct expression *expr)
365 struct expression *right;
366 struct symbol *left_type, *right_type;
368 left_type = get_type(expr->left);
369 if (!left_type || left_type->type != SYM_PTR)
370 return 0;
371 left_type = get_real_base_type(left_type);
372 if (!left_type)
373 return 0;
374 if (left_type->type != SYM_STRUCT &&
375 left_type->type != SYM_UNION)
376 return 0;
379 * Ignore struct to struct assignments because for those we look at the
380 * individual members.
382 right = strip_expr(expr->right);
383 right_type = get_type(right);
384 if (!right_type || right_type->type != SYM_PTR)
385 return 0;
387 /* If we are assigning struct members then normally that is handled
388 * by fake assignments, however if we cast one struct to a different
389 * of struct then we handle that here.
391 right_type = get_real_base_type(right_type);
392 if (right_type == left_type)
393 return 0;
395 if (!points_to_user_data(right))
396 return 0;
398 tag_as_user_data(expr->left);
399 return 1;
402 static int handle_get_user(struct expression *expr)
404 char *name;
405 int ret = 0;
407 name = get_macro_name(expr->pos);
408 if (!name || strcmp(name, "get_user") != 0)
409 return 0;
411 name = expr_to_var(expr->right);
412 if (!name || strcmp(name, "__val_gu") != 0)
413 goto free;
414 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
415 ret = 1;
416 free:
417 free_string(name);
418 return ret;
421 static void match_assign(struct expression *expr)
423 struct range_list *rl;
425 if (is_fake_call(expr->right))
426 return;
427 if (handle_get_user(expr))
428 return;
429 if (points_to_user_data(expr->right))
430 set_points_to_user_data(expr->left);
431 if (handle_struct_assignment(expr))
432 return;
434 if (!get_user_rl(expr->right, &rl))
435 goto clear_old_state;
437 rl = cast_rl(get_type(expr->left), rl);
438 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
440 return;
442 clear_old_state:
443 if (get_state_expr(my_id, expr->left))
444 set_state_expr(my_id, expr->left, alloc_estate_empty());
447 static void handle_eq_noteq(struct expression *expr)
449 struct smatch_state *left_orig, *right_orig;
451 left_orig = get_state_expr(my_id, expr->left);
452 right_orig = get_state_expr(my_id, expr->right);
454 if (!left_orig && !right_orig)
455 return;
456 if (left_orig && right_orig)
457 return;
459 if (left_orig) {
460 set_true_false_states_expr(my_id, expr->left,
461 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
462 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
463 } else {
464 set_true_false_states_expr(my_id, expr->right,
465 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
466 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
470 static void handle_unsigned_lt_gt(struct expression *expr)
472 struct symbol *type;
473 struct range_list *left;
474 struct range_list *right;
475 struct range_list *non_negative;
476 sval_t min, minus_one;
479 * conditions are mostly handled by smatch_extra.c. The special case
480 * here is that say you have if (user_int < unknown_u32) {
481 * In Smatch extra we say that, We have no idea what value
482 * unknown_u32 is so the only thin we can say for sure is that
483 * user_int is not -1 (UINT_MAX). But in check_user_data2.c we should
484 * assume that unless unknown_u32 is user data, it's probably less than
485 * INT_MAX.
489 type = get_type(expr);
490 if (!type_unsigned(type))
491 return;
494 * Assume if (user < trusted) { ... because I am lazy and because this
495 * is the correct way to write code.
497 if (!get_user_rl(expr->left, &left))
498 return;
499 if (get_user_rl(expr->right, &right))
500 return;
502 if (!sval_is_negative(rl_min(left)))
503 return;
504 min = rl_min(left);
505 minus_one.type = rl_type(left);
506 minus_one.value = -1;
507 non_negative = remove_range(left, min, minus_one);
509 switch (expr->op) {
510 case '<':
511 case SPECIAL_UNSIGNED_LT:
512 case SPECIAL_LTE:
513 case SPECIAL_UNSIGNED_LTE:
514 set_true_false_states_expr(my_id, expr->left,
515 alloc_estate_rl(non_negative), NULL);
516 break;
517 case '>':
518 case SPECIAL_UNSIGNED_GT:
519 case SPECIAL_GTE:
520 case SPECIAL_UNSIGNED_GTE:
521 set_true_false_states_expr(my_id, expr->left,
522 NULL, alloc_estate_rl(non_negative));
523 break;
527 static void match_condition(struct expression *expr)
529 if (expr->type != EXPR_COMPARE)
530 return;
532 if (expr->op == SPECIAL_EQUAL ||
533 expr->op == SPECIAL_NOTEQUAL) {
534 handle_eq_noteq(expr);
535 return;
538 handle_unsigned_lt_gt(expr);
541 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
543 func_gets_user_data = true;
545 tag_as_user_data(expr->left);
546 set_points_to_user_data(expr->left);
549 static void match_simple_strtoul(const char *fn, struct expression *expr, void *unused)
551 func_gets_user_data = true;
553 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
556 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
558 struct expression *parent;
559 char *macro;
561 if (!expr)
562 return 0;
564 macro = get_macro_name(expr->pos);
565 if (!macro)
566 return 0;
568 /* handle ntohl(foo[i]) where "i" is trusted */
569 parent = expr_get_parent_expr(expr);
570 if (parent && parent->type == EXPR_BINOP) {
571 char *parent_macro = get_macro_name(parent->pos);
573 if (parent_macro && strcmp(macro, parent_macro) == 0)
574 return 0;
577 if (strcmp(macro, "ntohl") == 0) {
578 *rl = alloc_whole_rl(&uint_ctype);
579 return 1;
581 if (strcmp(macro, "ntohs") == 0) {
582 *rl = alloc_whole_rl(&ushort_ctype);
583 return 1;
585 return 0;
588 struct db_info {
589 struct range_list *rl;
590 struct expression *call;
592 static int returned_rl_callback(void *_info, int argc, char **argv, char **azColName)
594 struct db_info *db_info = _info;
595 struct range_list *rl;
596 char *return_ranges = argv[0];
597 char *user_ranges = argv[1];
598 struct expression *arg;
599 int comparison;
601 if (argc != 2)
602 return 0;
604 call_results_to_rl(db_info->call, get_type(db_info->call), user_ranges, &rl);
605 if (str_to_comparison_arg(return_ranges, db_info->call, &comparison, &arg) &&
606 comparison == SPECIAL_EQUAL) {
607 struct range_list *orig_rl;
609 if (!get_user_rl(arg, &orig_rl))
610 return 0;
611 rl = rl_intersection(rl, orig_rl);
612 if (!rl)
613 return 0;
615 db_info->rl = rl_union(db_info->rl, rl);
617 return 0;
620 static int has_user_data(struct symbol *sym)
622 struct sm_state *tmp;
624 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
625 if (tmp->sym == sym)
626 return 1;
627 } END_FOR_EACH_SM(tmp);
628 return 0;
631 static int we_pass_user_data(struct expression *call)
633 struct expression *arg;
634 struct symbol *sym;
636 FOR_EACH_PTR(call->args, arg) {
637 sym = expr_to_sym(arg);
638 if (!sym)
639 continue;
640 if (has_user_data(sym))
641 return 1;
642 } END_FOR_EACH_PTR(arg);
644 return 0;
647 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
649 struct db_info db_info = {};
651 /* for function pointers assume everything is used */
652 if (call->fn->type != EXPR_SYMBOL)
653 return 0;
654 if (is_fake_call(call))
655 return 0;
657 db_info.call = call;
658 run_sql(&returned_rl_callback, &db_info,
659 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
660 get_static_filter(call->fn->symbol), USER_DATA3_SET);
661 if (db_info.rl) {
662 func_gets_user_data = true;
663 *rl = db_info.rl;
664 return 1;
667 run_sql(&returned_rl_callback, &db_info,
668 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
669 get_static_filter(call->fn->symbol), USER_DATA3);
670 if (db_info.rl) {
671 if (!we_pass_user_data(call))
672 return 0;
673 *rl = db_info.rl;
674 return 1;
677 return 0;
680 static int user_data_flag;
681 static int no_user_data_flag;
682 static struct range_list *var_user_rl(struct expression *expr)
684 struct smatch_state *state;
685 struct range_list *rl;
686 struct range_list *absolute_rl;
688 if (expr->type == EXPR_BINOP && expr->op == '%') {
689 struct range_list *left, *right;
691 if (!get_user_rl(expr->right, &right))
692 return NULL;
693 get_absolute_rl(expr->left, &left);
694 rl = rl_binop(left, '%', right);
695 goto found;
698 if (expr->type == EXPR_BINOP && expr->op == '/') {
699 struct range_list *left = NULL;
700 struct range_list *right = NULL;
701 struct range_list *abs_right;
704 * The specific bug I'm dealing with is:
706 * foo = capped_user / unknown;
708 * Instead of just saying foo is now entirely user_rl we should
709 * probably say instead that it is not at all user data.
713 get_user_rl(expr->left, &left);
714 get_user_rl(expr->right, &right);
715 get_absolute_rl(expr->right, &abs_right);
717 if (left && !right) {
718 rl = rl_binop(left, '/', abs_right);
719 if (sval_cmp(rl_max(left), rl_max(rl)) < 0)
720 no_user_data_flag = 1;
723 return NULL;
726 if (get_user_macro_rl(expr, &rl))
727 goto found;
729 if (comes_from_skb_data(expr)) {
730 rl = alloc_whole_rl(get_type(expr));
731 goto found;
734 state = get_state_expr(my_id, expr);
735 if (state && estate_rl(state)) {
736 rl = estate_rl(state);
737 goto found;
740 if (expr->type == EXPR_CALL && db_returned_user_rl(expr, &rl))
741 goto found;
743 if (is_array(expr)) {
744 struct expression *array = get_array_base(expr);
746 if (!get_state_expr(my_id, array)) {
747 no_user_data_flag = 1;
748 return NULL;
752 return NULL;
753 found:
754 user_data_flag = 1;
755 absolute_rl = var_to_absolute_rl(expr);
756 return clone_rl(rl_intersection(rl, absolute_rl));
759 int get_user_rl(struct expression *expr, struct range_list **rl)
762 user_data_flag = 0;
763 no_user_data_flag = 0;
764 custom_get_absolute_rl(expr, &var_user_rl, rl);
765 if (!user_data_flag || no_user_data_flag || !*rl) {
766 *rl = NULL;
767 return 0;
769 return 1;
772 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
774 struct smatch_state *state;
776 state = get_state(my_id, name, sym);
777 if (state && estate_rl(state)) {
778 *rl = estate_rl(state);
779 return 1;
781 return 0;
784 static void match_call_info(struct expression *expr)
786 struct range_list *rl;
787 struct expression *arg;
788 int i = 0;
790 i = -1;
791 FOR_EACH_PTR(expr->args, arg) {
792 i++;
794 if (!get_user_rl(arg, &rl))
795 continue;
797 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
798 } END_FOR_EACH_PTR(arg);
801 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
803 struct smatch_state *state;
804 struct range_list *rl;
806 if (strcmp(sm->state->name, "") == 0)
807 return;
809 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
810 if (!state || !estate_rl(state))
811 rl = estate_rl(sm->state);
812 else
813 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
815 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
818 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
820 struct range_list *rl = NULL;
821 struct smatch_state *state;
822 struct symbol *type;
823 char fullname[256];
825 if (strcmp(key, "*$") == 0)
826 snprintf(fullname, sizeof(fullname), "*%s", name);
827 else if (strncmp(key, "$", 1) == 0)
828 snprintf(fullname, 256, "%s%s", name, key + 1);
829 else
830 return;
832 type = get_member_type_from_key(symbol_expression(sym), key);
834 /* if the caller passes a void pointer with user data */
835 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
836 struct expression *expr = symbol_expression(sym);
838 tag_as_user_data(expr);
839 set_points_to_user_data(expr);
840 return;
842 str_to_rl(type, value, &rl);
843 state = alloc_estate_rl(rl);
844 set_state(my_id, fullname, sym, state);
847 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
849 set_state(my_call_id, "this_function", NULL, &called);
852 static void match_syscall_definition(struct symbol *sym)
854 struct symbol *arg;
855 char *macro;
856 char *name;
857 int is_syscall = 0;
859 macro = get_macro_name(sym->pos);
860 if (macro &&
861 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
862 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
863 is_syscall = 1;
865 name = get_function();
866 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
867 if (name && strncmp(name, "sys_", 4) == 0)
868 is_syscall = 1;
871 if (name && strncmp(name, "compat_sys_", 11) == 0)
872 is_syscall = 1;
874 if (!is_syscall)
875 return;
877 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
878 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
879 } END_FOR_EACH_PTR(arg);
882 static void set_to_user_data(struct expression *expr, char *key, char *value)
884 char *name;
885 struct symbol *sym;
886 struct symbol *type;
887 struct range_list *rl = NULL;
889 type = get_member_type_from_key(expr, key);
890 name = get_variable_from_key(expr, key, &sym);
891 if (!name || !sym)
892 goto free;
894 call_results_to_rl(expr, type, value, &rl);
896 set_state(my_id, name, sym, alloc_estate_rl(rl));
897 free:
898 free_string(name);
902 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
904 struct expression *arg;
905 struct expression *call;
907 call = expr;
908 while (call->type == EXPR_ASSIGNMENT)
909 call = strip_expr(call->right);
910 if (call->type != EXPR_CALL)
911 return;
913 if (!we_pass_user_data(call))
914 return;
916 if (param == -1) {
917 if (expr->type != EXPR_ASSIGNMENT)
918 return;
919 set_to_user_data(expr->left, key, value);
920 return;
923 arg = get_argument_from_call_expr(call->args, param);
924 if (!arg)
925 return;
926 set_to_user_data(arg, key, value);
929 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
931 struct expression *arg;
933 func_gets_user_data = true;
935 if (param == -1) {
936 if (expr->type != EXPR_ASSIGNMENT)
937 return;
938 if (strcmp(key, "*$") == 0) {
939 set_points_to_user_data(expr->left);
940 tag_as_user_data(expr->left);
941 } else {
942 set_to_user_data(expr->left, key, value);
944 return;
947 while (expr->type == EXPR_ASSIGNMENT)
948 expr = strip_expr(expr->right);
949 if (expr->type != EXPR_CALL)
950 return;
952 arg = get_argument_from_call_expr(expr->args, param);
953 if (!arg)
954 return;
955 set_to_user_data(arg, key, value);
958 static int has_empty_state(struct sm_state *sm)
960 struct sm_state *tmp;
962 FOR_EACH_PTR(sm->possible, tmp) {
963 if (!estate_rl(tmp->state))
964 return 1;
965 } END_FOR_EACH_PTR(tmp);
967 return 0;
970 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
972 struct sm_state *sm;
973 struct smatch_state *start_state;
974 struct range_list *rl;
975 int param;
976 char *return_str;
977 const char *param_name;
979 expr = strip_expr(expr);
980 return_str = expr_to_str(expr);
982 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
983 if (has_empty_state(sm))
984 continue;
986 param = get_param_num_from_sym(sm->sym);
987 if (param < 0) {
988 if (expr_to_sym(expr) == sm->sym)
989 param = -1;
990 else
991 continue;
994 /* The logic here was that if we were passed in a user data then
995 * we don't record that. It's like the difference between
996 * param_filter and param_set. When I think about it, I'm not
997 * sure it actually works. It's probably harmless because we
998 * checked earlier that we're not returning a parameter...
999 * Let's mark this as a TODO.
1001 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
1002 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
1003 continue;
1005 if (param == -1)
1006 param_name = state_name_to_param_name(sm->name, return_str);
1007 else
1008 param_name = get_param_name(sm);
1009 if (!param_name)
1010 continue;
1011 if (strcmp(param_name, "$") == 0) /* The -1 param is handled after the loop */
1012 continue;
1014 sql_insert_return_states(return_id, return_ranges,
1015 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1016 param, param_name, show_rl(estate_rl(sm->state)));
1017 } END_FOR_EACH_SM(sm);
1019 if (points_to_user_data(expr)) {
1020 sql_insert_return_states(return_id, return_ranges,
1021 (is_skb_data(expr) || !func_gets_user_data) ?
1022 USER_DATA3_SET : USER_DATA3,
1023 -1, "*$", "");
1024 } else if (get_user_rl(expr, &rl)) {
1025 sql_insert_return_states(return_id, return_ranges,
1026 func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1027 -1, "$", show_rl(rl));
1030 free_string(return_str);
1033 static struct int_stack *gets_data_stack;
1034 static void match_function_def(struct symbol *sym)
1036 func_gets_user_data = false;
1039 static void match_inline_start(struct expression *expr)
1041 push_int(&gets_data_stack, func_gets_user_data);
1044 static void match_inline_end(struct expression *expr)
1046 func_gets_user_data = pop_int(&gets_data_stack);
1049 void check_user_data2(int id)
1051 int i;
1053 my_id = id;
1055 if (option_project != PROJ_KERNEL)
1056 return;
1058 add_hook(&match_function_def, FUNC_DEF_HOOK);
1059 add_hook(&match_inline_start, INLINE_FN_START);
1060 add_hook(&match_inline_end, INLINE_FN_END);
1062 add_hook(&save_start_states, AFTER_DEF_HOOK);
1063 add_hook(&free_start_states, AFTER_FUNC_HOOK);
1064 add_hook(&match_save_states, INLINE_FN_START);
1065 add_hook(&match_restore_states, INLINE_FN_END);
1067 add_unmatched_state_hook(my_id, &empty_state);
1068 add_extra_nomod_hook(&extra_nomod_hook);
1069 add_pre_merge_hook(my_id, &pre_merge_hook);
1070 add_merge_hook(my_id, &merge_estates);
1072 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
1073 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
1074 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1075 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1076 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1078 add_function_assign_hook("simple_strtol", &match_simple_strtoul, NULL);
1079 add_function_assign_hook("simple_strtoll", &match_simple_strtoul, NULL);
1080 add_function_assign_hook("simple_strtoul", &match_simple_strtoul, NULL);
1081 add_function_assign_hook("simple_strtoull", &match_simple_strtoul, NULL);
1083 add_function_hook("sscanf", &match_sscanf, NULL);
1085 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
1086 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
1087 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
1089 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1091 add_hook(&match_assign, ASSIGNMENT_HOOK);
1092 add_hook(&match_condition, CONDITION_HOOK);
1094 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1095 add_member_info_callback(my_id, struct_member_callback);
1096 select_caller_info_hook(set_param_user_data, USER_DATA3);
1097 select_return_states_hook(USER_DATA3, &returns_param_user_data);
1098 select_return_states_hook(USER_DATA3_SET, &returns_param_user_data_set);
1099 add_split_return_callback(&param_set_to_user_data);
1102 void check_user_data3(int id)
1104 my_call_id = id;
1106 if (option_project != PROJ_KERNEL)
1107 return;
1108 select_caller_info_hook(set_called, INTERNAL);