Print this page
11506 smatch resync


  15  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
  16  */
  17 
  18 /*
  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.
  23  */
  24 
  25 #include "smatch.h"
  26 #include "smatch_slist.h"
  27 #include "smatch_extra.h"
  28 
  29 static int my_id;
  30 static int my_call_id;
  31 
  32 STATE(called);
  33 static bool func_gets_user_data;
  34 
  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",
  44 };
  45 
  46 static const char *returns_user_data[] = {
  47         "simple_strtol", "simple_strtoll", "simple_strtoul", "simple_strtoull",
  48         "kvm_register_read", "nlmsg_data", "nla_data", "memdup_user",
  49         "kmap_atomic", "skb_network_header",
  50 };
  51 




  52 static void set_points_to_user_data(struct expression *expr);
  53 
  54 static struct stree *start_states;
  55 static struct stree_stack *saved_stack;
  56 static void save_start_states(struct statement *stmt)
  57 {
  58         start_states = clone_stree(__get_cur_stree());
  59 }
  60 
  61 static void free_start_states(void)
  62 {
  63         free_stree(&start_states);
  64 }
  65 
  66 static void match_save_states(struct expression *expr)
  67 {
  68         push_stree(&saved_stack, start_states);
  69         start_states = NULL;
  70 }
  71 
  72 static void match_restore_states(struct expression *expr)
  73 {
  74         free_stree(&start_states);
  75         start_states = pop_stree(&saved_stack);
  76 }
  77 
  78 static struct smatch_state *empty_state(struct sm_state *sm)
  79 {
  80         return alloc_estate_empty();
  81 }
  82 
  83 static void pre_merge_hook(struct sm_state *sm)
  84 {
  85         struct smatch_state *user;
  86         struct smatch_state *extra;

  87         struct range_list *rl;
  88         sval_t dummy;
  89         sval_t sval_100;
  90 
  91         sval_100.value = 100;
  92         sval_100.type = &int_ctype;
  93 
  94         user = get_state(my_id, sm->name, sm->sym);
  95         if (!user)
  96                 return;
  97         if (!__in_function_def && !estate_rl(sm->state)) {
  98                 /*
  99                  * If the one side is capped and the other side is empty then
 100                  * let's just mark it as not-user data because the information
 101                  * isn't going to be useful.  How this looks is:
 102                  *
 103                  * if (user_var > trusted)
 104                  *      user_var = trusted;  <-- empty state
 105                  * else
 106                  *      <-- capped
 107                  *
 108                  * The problem is that sometimes things are capped to a literal
 109                  * and we'd like to keep the state in that case...  Ugh.  I've
 110                  * added a check which assumes that everything less than 100 is
 111                  * probably capped against a literal.
 112                  *
 113                  */
 114                 if (is_capped_var_sym(sm->name, sm->sym) &&
 115                     sval_cmp(estate_max(user), sval_100) > 0)
 116                         set_state(my_id, sm->name, sm->sym, alloc_estate_empty());
 117                 return;
 118         }
 119         extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
 120         if (!extra || !estate_rl(extra))
 121                 return;
 122         rl = rl_intersection(estate_rl(user), estate_rl(extra));
 123         if (rl_to_sval(rl, &dummy))
 124                 rl = NULL;
 125         set_state(my_id, sm->name, sm->sym, alloc_estate_rl(clone_rl(rl)));



 126 }
 127 
 128 static void extra_nomod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
 129 {
 130         struct smatch_state *user;
 131         struct range_list *rl;
 132 
 133         user = get_state(my_id, name, sym);
 134         if (!user)
 135                 return;
 136         rl = rl_intersection(estate_rl(user), estate_rl(state));
 137         if (rl_equiv(rl, estate_rl(user)))
 138                 return;
 139         set_state(my_id, name, sym, alloc_estate_rl(rl));



 140 }
 141 






















































 142 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
 143 {
 144         struct expression *edge_member;
 145         struct symbol *base = get_real_base_type(member);
 146         struct symbol *tmp;
 147 
 148         if (member->ident)
 149                 expr = member_expression(expr, '.', member->ident);
 150 
 151         FOR_EACH_PTR(base->symbol_list, tmp) {
 152                 struct symbol *type;
 153 
 154                 type = get_real_base_type(tmp);
 155                 if (!type)
 156                         continue;
 157 
 158                 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
 159                         tag_inner_struct_members(expr, tmp);
 160                         continue;
 161                 }


 330         if (!expr->member)
 331                 return 0;
 332         if (strcmp(expr->member->name, "data") != 0)
 333                 return 0;
 334 
 335         sym = expr_to_sym(expr->deref);
 336         if (!sym)
 337                 return 0;
 338         sym = get_real_base_type(sym);
 339         if (!sym || sym->type != SYM_PTR)
 340                 return 0;
 341         sym = get_real_base_type(sym);
 342         if (!sym || sym->type != SYM_STRUCT || !sym->ident)
 343                 return 0;
 344         if (strcmp(sym->ident->name, "sk_buff") != 0)
 345                 return 0;
 346 
 347         return 1;
 348 }
 349 
















 350 static int get_rl_from_function(struct expression *expr, struct range_list **rl)
 351 {
 352         int i;
 353 
 354         if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL ||
 355             !expr->fn->symbol_name || !expr->fn->symbol_name->name)
 356                 return 0;
 357 
 358         for (i = 0; i < ARRAY_SIZE(returns_user_data); i++) {
 359                 if (strcmp(expr->fn->symbol_name->name, returns_user_data[i]) == 0) {
 360                         *rl = alloc_whole_rl(get_type(expr));
 361                         return 1;
 362                 }
 363         }
 364         return 0;
 365 }
 366 
 367 int points_to_user_data(struct expression *expr)
 368 {
 369         struct smatch_state *state;
 370         struct range_list *rl;
 371         char buf[256];
 372         struct symbol *sym;
 373         char *name;
 374         int ret = 0;
 375 
 376         expr = strip_expr(expr);
 377         if (!expr)
 378                 return 0;
 379         if (is_skb_data(expr))
 380                 return 1;


 381         if (get_rl_from_function(expr, &rl))
 382                 return 1;
 383 
 384         if (expr->type == EXPR_BINOP && expr->op == '+') {
 385                 if (points_to_user_data(expr->left))
 386                         return 1;
 387                 if (points_to_user_data(expr->right))
 388                         return 1;
 389                 return 0;
 390         }
 391 
 392         name = expr_to_var_sym(expr, &sym);
 393         if (!name || !sym)
 394                 goto free;
 395         snprintf(buf, sizeof(buf), "*%s", name);
 396         state = get_state(my_id, buf, sym);
 397         if (state && estate_rl(state))
 398                 ret = 1;
 399 free:
 400         free_string(name);
 401         return ret;
 402 }
 403 
 404 static void set_points_to_user_data(struct expression *expr)
 405 {
 406         char *name;
 407         struct symbol *sym;
 408         char buf[256];

 409 
 410         name = expr_to_var_sym(expr, &sym);
 411         if (!name || !sym)
 412                 goto free;
 413         snprintf(buf, sizeof(buf), "*%s", name);
 414         set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));





 415 free:
 416         free_string(name);
 417 }
 418 
 419 static int comes_from_skb_data(struct expression *expr)
 420 {
 421         expr = strip_expr(expr);
 422         if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
 423                 return 0;
 424 
 425         expr = strip_expr(expr->unop);
 426         if (!expr)
 427                 return 0;
 428         if (expr->type == EXPR_BINOP && expr->op == '+')
 429                 expr = strip_expr(expr->left);
 430 
 431         return is_skb_data(expr);
 432 }
 433 
 434 static int handle_struct_assignment(struct expression *expr)


 472 
 473 static int handle_get_user(struct expression *expr)
 474 {
 475         char *name;
 476         int ret = 0;
 477 
 478         name = get_macro_name(expr->pos);
 479         if (!name || strcmp(name, "get_user") != 0)
 480                 return 0;
 481 
 482         name = expr_to_var(expr->right);
 483         if (!name || strcmp(name, "__val_gu") != 0)
 484                 goto free;
 485         set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
 486         ret = 1;
 487 free:
 488         free_string(name);
 489         return ret;
 490 }
 491 

































 492 static void match_assign(struct expression *expr)
 493 {
 494         struct range_list *rl;



 495 



 496         if (is_fake_call(expr->right))
 497                 goto clear_old_state;
 498         if (handle_get_user(expr))
 499                 return;
 500         if (points_to_user_data(expr->right))

 501                 set_points_to_user_data(expr->left);

 502         if (handle_struct_assignment(expr))
 503                 return;
 504 









 505         if (!get_user_rl(expr->right, &rl))
 506                 goto clear_old_state;
 507 
 508         rl = cast_rl(get_type(expr->left), rl);
 509         set_state_expr(my_id, expr->left, alloc_estate_rl(rl));



 510 
 511         return;
 512 
 513 clear_old_state:
 514         if (get_state_expr(my_id, expr->left))
 515                 set_state_expr(my_id, expr->left, alloc_estate_empty());
 516 }
 517 
 518 static void handle_eq_noteq(struct expression *expr)
 519 {
 520         struct smatch_state *left_orig, *right_orig;
 521 
 522         left_orig = get_state_expr(my_id, expr->left);
 523         right_orig = get_state_expr(my_id, expr->right);
 524 
 525         if (!left_orig && !right_orig)
 526                 return;
 527         if (left_orig && right_orig)
 528                 return;
 529 
 530         if (left_orig) {
 531                 set_true_false_states_expr(my_id, expr->left,
 532                                 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
 533                                 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
 534         } else {
 535                 set_true_false_states_expr(my_id, expr->right,
 536                                 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
 537                                 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
 538         }
 539 }
 540 
 541 static void handle_unsigned_lt_gt(struct expression *expr)
 542 {






























 543         struct symbol *type;
 544         struct range_list *left;
 545         struct range_list *right;
 546         struct range_list *non_negative;
 547         sval_t min, minus_one;
 548 






 549         /*
 550          * conditions are mostly handled by smatch_extra.c.  The special case
 551          * here is that say you have if (user_int < unknown_u32) {
 552          * In Smatch extra we say that, We have no idea what value
 553          * unknown_u32 is so the only thin we can say for sure is that
 554          * user_int is not -1 (UINT_MAX).  But in check_user_data2.c we should
 555          * assume that unless unknown_u32 is user data, it's probably less than
 556          * INT_MAX.
 557          *













 558          */
 559 
 560         type = get_type(expr);
 561         if (!type_unsigned(type))

 562                 return;
 563 
 564         /*
 565          * Assume if (user < trusted) { ... because I am lazy and because this
 566          * is the correct way to write code.
 567          */
 568         if (!get_user_rl(expr->left, &left))
 569                 return;
 570         if (get_user_rl(expr->right, &right))

 571                 return;
 572 
 573         if (!sval_is_negative(rl_min(left)))
 574                 return;
 575         min = rl_min(left);
 576         minus_one.type = rl_type(left);
 577         minus_one.value = -1;
 578         non_negative = remove_range(left, min, minus_one);
 579 






 580         switch (expr->op) {
 581         case '<':
 582         case SPECIAL_UNSIGNED_LT:
 583         case SPECIAL_LTE:
 584         case SPECIAL_UNSIGNED_LTE:
 585                 set_true_false_states_expr(my_id, expr->left,
 586                                            alloc_estate_rl(non_negative), NULL);


 587                 break;
 588         case '>':
 589         case SPECIAL_UNSIGNED_GT:
 590         case SPECIAL_GTE:
 591         case SPECIAL_UNSIGNED_GTE:
 592                 set_true_false_states_expr(my_id, expr->left,
 593                                            NULL, alloc_estate_rl(non_negative));


 594                 break;
 595         }



 596 }
 597 
 598 static void match_condition(struct expression *expr)
 599 {
 600         if (expr->type != EXPR_COMPARE)
 601                 return;
 602 
 603         if (expr->op == SPECIAL_EQUAL ||
 604             expr->op == SPECIAL_NOTEQUAL) {
 605                 handle_eq_noteq(expr);
 606                 return;
 607         }
 608 
 609         handle_unsigned_lt_gt(expr);
 610 }
 611 
 612 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
 613 {
 614         tag_as_user_data(expr->left);
 615         set_points_to_user_data(expr->left);
 616 }
 617 
 618 static void match_returns_user_rl(const char *fn, struct expression *expr, void *unused)
 619 {
 620         func_gets_user_data = true;
 621 }
 622 
 623 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
 624 {
 625         struct expression *parent;
 626         char *macro;
 627 
 628         if (!expr)
 629                 return 0;


 637         while (parent && parent->type != EXPR_BINOP)
 638                 parent = expr_get_parent_expr(parent);
 639         if (parent && parent->type == EXPR_BINOP) {
 640                 char *parent_macro = get_macro_name(parent->pos);
 641 
 642                 if (parent_macro && strcmp(macro, parent_macro) == 0)
 643                         return 0;
 644         }
 645 
 646         if (strcmp(macro, "ntohl") == 0) {
 647                 *rl = alloc_whole_rl(&uint_ctype);
 648                 return 1;
 649         }
 650         if (strcmp(macro, "ntohs") == 0) {
 651                 *rl = alloc_whole_rl(&ushort_ctype);
 652                 return 1;
 653         }
 654         return 0;
 655 }
 656 
 657 struct db_info {
 658         struct range_list *rl;
 659         struct expression *call;
 660 };
 661 static int returned_rl_callback(void *_info, int argc, char **argv, char **azColName)
 662 {
 663         struct db_info *db_info = _info;
 664         struct range_list *rl;
 665         char *return_ranges = argv[0];
 666         char *user_ranges = argv[1];
 667         struct expression *arg;
 668         int comparison;
 669 
 670         if (argc != 2)
 671                 return 0;
 672 
 673         call_results_to_rl(db_info->call, get_type(db_info->call), user_ranges, &rl);
 674         if (str_to_comparison_arg(return_ranges, db_info->call, &comparison, &arg) &&
 675             comparison == SPECIAL_EQUAL) {
 676                 struct range_list *orig_rl;
 677 
 678                 if (!get_user_rl(arg, &orig_rl))
 679                         return 0;
 680                 rl = rl_intersection(rl, orig_rl);
 681                 if (!rl)
 682                         return 0;
 683         }
 684         db_info->rl = rl_union(db_info->rl, rl);
 685 
 686         return 0;
 687 }
 688 
 689 static int has_user_data(struct symbol *sym)
 690 {
 691         struct sm_state *tmp;
 692 
 693         FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
 694                 if (tmp->sym == sym)
 695                         return 1;
 696         } END_FOR_EACH_SM(tmp);
 697         return 0;
 698 }
 699 
 700 static int we_pass_user_data(struct expression *call)
 701 {
 702         struct expression *arg;
 703         struct symbol *sym;
 704 
 705         FOR_EACH_PTR(call->args, arg) {
 706                 sym = expr_to_sym(arg);
 707                 if (!sym)
 708                         continue;
 709                 if (has_user_data(sym))
 710                         return 1;
 711         } END_FOR_EACH_PTR(arg);
 712 
 713         return 0;
 714 }
 715 
 716 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
 717 {
 718         struct db_info db_info = {};

 719 
 720         /* for function pointers assume everything is used */
 721         if (call->fn->type != EXPR_SYMBOL)
 722                 return 0;
 723         if (is_fake_call(call))
 724                 return 0;
 725 
 726         db_info.call = call;
 727         run_sql(&returned_rl_callback, &db_info,
 728                 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
 729                 get_static_filter(call->fn->symbol), USER_DATA3_SET);
 730         if (db_info.rl) {
 731                 func_gets_user_data = true;
 732                 *rl = db_info.rl;
 733                 return 1;
 734         }
 735 
 736         run_sql(&returned_rl_callback, &db_info,
 737                 "select return, value from return_states where %s and type = %d and parameter = -1 and key = '$';",
 738                 get_static_filter(call->fn->symbol), USER_DATA3);
 739         if (db_info.rl) {
 740                 if (!we_pass_user_data(call))
 741                         return 0;
 742                 *rl = db_info.rl;
 743                 return 1;
 744         }
 745 
 746         return 0;
 747 }
 748 
 749 struct stree *get_user_stree(void)
 750 {
 751         return get_all_states_stree(my_id);
 752 }
 753 
 754 static int user_data_flag;
 755 static int no_user_data_flag;
 756 static struct range_list *var_user_rl(struct expression *expr)
 757 {
 758         struct smatch_state *state;
 759         struct range_list *rl;
 760         struct range_list *absolute_rl;
 761 





 762         if (expr->type == EXPR_BINOP && expr->op == '%') {
 763                 struct range_list *left, *right;
 764 
 765                 if (!get_user_rl(expr->right, &right))
 766                         return NULL;
 767                 get_absolute_rl(expr->left, &left);
 768                 rl = rl_binop(left, '%', right);
 769                 goto found;
 770         }
 771 
 772         if (!option_spammy && expr->type == EXPR_BINOP && expr->op == '/') {
 773                 struct range_list *left = NULL;
 774                 struct range_list *right = NULL;
 775                 struct range_list *abs_right;
 776 
 777                 /*
 778                  * The specific bug I'm dealing with is:
 779                  *
 780                  * foo = capped_user / unknown;
 781                  *
 782                  * Instead of just saying foo is now entirely user_rl we should
 783                  * probably say instead that it is not at all user data.
 784                  *
 785                  */
 786 
 787                 get_user_rl(expr->left, &left);
 788                 get_user_rl(expr->right, &right);
 789                 get_absolute_rl(expr->right, &abs_right);
 790 
 791                 if (left && !right) {
 792                         rl = rl_binop(left, '/', abs_right);


 822 
 823                 if (!get_state_expr(my_id, array)) {
 824                         no_user_data_flag = 1;
 825                         return NULL;
 826                 }
 827         }
 828 
 829         if (expr->type == EXPR_PREOP && expr->op == '*' &&
 830             is_user_rl(expr->unop)) {
 831                 rl = var_to_absolute_rl(expr);
 832                 goto found;
 833         }
 834 
 835         return NULL;
 836 found:
 837         user_data_flag = 1;
 838         absolute_rl = var_to_absolute_rl(expr);
 839         return clone_rl(rl_intersection(rl, absolute_rl));
 840 }
 841 












 842 int get_user_rl(struct expression *expr, struct range_list **rl)
 843 {



 844         user_data_flag = 0;
 845         no_user_data_flag = 0;
 846         custom_get_absolute_rl(expr, &var_user_rl, rl);
 847         if (!user_data_flag || no_user_data_flag)
 848                 *rl = NULL;
 849 
 850         return !!*rl;
 851 }
 852 
 853 int get_user_rl_spammy(struct expression *expr, struct range_list **rl)
 854 {
 855         int ret;
 856 
 857         option_spammy++;
 858         ret = get_user_rl(expr, rl);
 859         option_spammy--;
 860 
 861         return ret;
 862 }
 863 
 864 int is_user_rl(struct expression *expr)
 865 {
 866         struct range_list *tmp;
 867 
 868         return get_user_rl_spammy(expr, &tmp);
 869 }
 870 
 871 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
 872 {
 873         struct smatch_state *state;
 874 
 875         state = get_state(my_id, name, sym);
 876         if (state && estate_rl(state)) {
 877                 *rl = estate_rl(state);
 878                 return 1;
 879         }
 880         return 0;
 881 }
 882 
 883 static void match_call_info(struct expression *expr)
 884 {
 885         struct range_list *rl;












 886         struct expression *arg;
 887         struct symbol *type;
 888         int i = 0;

 889 
 890         i = -1;
 891         FOR_EACH_PTR(expr->args, arg) {
 892                 i++;
 893                 type = get_arg_type(expr->fn, i);
 894 
 895                 if (!get_user_rl(arg, &rl))
 896                         continue;
 897 
 898                 rl = cast_rl(type, rl);
 899                 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
 900         } END_FOR_EACH_PTR(arg);
 901 }
 902 
 903 static int is_struct_ptr(struct symbol *sym)
 904 {
 905         struct symbol *type;
 906 
 907         if (!sym)
 908                 return 0;
 909         type = get_real_base_type(sym);
 910         if (!type || type->type != SYM_PTR)
 911                 return 0;
 912         type = get_real_base_type(type);
 913         if (!type || type->type != SYM_STRUCT)
 914                 return 0;
 915         return 1;
 916 }
 917 
 918 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
 919 {
 920         struct smatch_state *state;
 921         struct range_list *rl;
 922         struct symbol *type;

 923 
 924         /*
 925          * Smatch uses a hack where if we get an unsigned long we say it's
 926          * both user data and it points to user data.  But if we pass it to a
 927          * function which takes an int, then it's just user data.  There's not
 928          * enough bytes for it to be a pointer.
 929          *
 930          */
 931         type = get_arg_type(call->fn, param);
 932         if (type && type_bits(type) < type_bits(&ptr_ctype))
 933                 return;
 934 
 935         if (strcmp(sm->state->name, "") == 0)
 936                 return;
 937 
 938         if (strcmp(printed_name, "*$") == 0 &&
 939             is_struct_ptr(sm->sym))
 940                 return;
 941 
 942         state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
 943         if (!state || !estate_rl(state))
 944                 rl = estate_rl(sm->state);
 945         else
 946                 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
 947 
 948         sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));





 949 }
 950 



































 951 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
 952 {
 953         struct range_list *rl = NULL;
 954         struct smatch_state *state;

 955         struct symbol *type;
 956         char fullname[256];


 957 
 958         if (strcmp(key, "*$") == 0)
 959                 snprintf(fullname, sizeof(fullname), "*%s", name);
 960         else if (strncmp(key, "$", 1) == 0)
 961                 snprintf(fullname, 256, "%s%s", name, key + 1);
 962         else
 963                 return;

 964 
 965         type = get_member_type_from_key(symbol_expression(sym), key);

 966 
 967         /* if the caller passes a void pointer with user data */
 968         if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
 969                 struct expression *expr = symbol_expression(sym);
 970 
 971                 tag_as_user_data(expr);
 972                 set_points_to_user_data(expr);













 973                 return;
 974         }


 975         str_to_rl(type, value, &rl);
 976         state = alloc_estate_rl(rl);


 977         set_state(my_id, fullname, sym, state);
 978 }
 979 
 980 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
 981 {
 982         set_state(my_call_id, "this_function", NULL, &called);
 983 }
 984 
 985 static void match_syscall_definition(struct symbol *sym)
 986 {
 987         struct symbol *arg;
 988         char *macro;
 989         char *name;
 990         int is_syscall = 0;
 991 
 992         macro = get_macro_name(sym->pos);
 993         if (macro &&
 994             (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
 995              strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
 996                 is_syscall = 1;
 997 
 998         name = get_function();
 999         if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
1000                 if (name && strncmp(name, "sys_", 4) == 0)
1001                         is_syscall = 1;
1002         }
1003 
1004         if (name && strncmp(name, "compat_sys_", 11) == 0)
1005                 is_syscall = 1;
1006 
1007         if (!is_syscall)
1008                 return;
1009 
1010         FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
1011                 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
1012         } END_FOR_EACH_PTR(arg);
1013 }
1014 
















1015 static void set_to_user_data(struct expression *expr, char *key, char *value)
1016 {

1017         char *name;
1018         struct symbol *sym;
1019         struct symbol *type;
1020         struct range_list *rl = NULL;
1021 
1022         type = get_member_type_from_key(expr, key);
1023         name = get_variable_from_key(expr, key, &sym);
1024         if (!name || !sym)
1025                 goto free;
1026 
1027         call_results_to_rl(expr, type, value, &rl);
1028 
1029         set_state(my_id, name, sym, alloc_estate_rl(rl));



1030 free:
1031         free_string(name);
1032 
1033 }
1034 
1035 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
1036 {
1037         struct expression *arg;
1038         struct expression *call;
1039 
1040         call = expr;
1041         while (call->type == EXPR_ASSIGNMENT)
1042                 call = strip_expr(call->right);
1043         if (call->type != EXPR_CALL)
1044                 return;
1045 
1046         if (!we_pass_user_data(call))
1047                 return;
1048 
1049         if (param == -1) {
1050                 if (expr->type != EXPR_ASSIGNMENT)

1051                         return;

1052                 set_to_user_data(expr->left, key, value);
1053                 return;
1054         }
1055 
1056         arg = get_argument_from_call_expr(call->args, param);
1057         if (!arg)
1058                 return;
1059         set_to_user_data(arg, key, value);
1060 }
1061 
1062 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
1063 {
1064         struct expression *arg;
1065 
1066         func_gets_user_data = true;
1067 
1068         if (param == -1) {
1069                 if (expr->type != EXPR_ASSIGNMENT)

1070                         return;

1071                 if (strcmp(key, "*$") == 0) {
1072                         set_points_to_user_data(expr->left);
1073                         tag_as_user_data(expr->left);
1074                 } else {
1075                         set_to_user_data(expr->left, key, value);
1076                 }
1077                 return;
1078         }
1079 
1080         while (expr->type == EXPR_ASSIGNMENT)
1081                 expr = strip_expr(expr->right);
1082         if (expr->type != EXPR_CALL)
1083                 return;
1084 
1085         arg = get_argument_from_call_expr(expr->args, param);
1086         if (!arg)
1087                 return;
1088         set_to_user_data(arg, key, value);
1089 }
1090 
1091 static int has_empty_state(struct sm_state *sm)
1092 {
1093         struct sm_state *tmp;
1094 
1095         FOR_EACH_PTR(sm->possible, tmp) {
1096                 if (!estate_rl(tmp->state))
1097                         return 1;
1098         } END_FOR_EACH_PTR(tmp);
1099 
1100         return 0;
1101 }
1102 
1103 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
1104 {
1105         struct sm_state *sm;
1106         struct smatch_state *start_state;
1107         struct range_list *rl;
1108         int param;
1109         char *return_str;
1110         const char *param_name;
1111         struct symbol *ret_sym;
1112         bool return_found = false;


1113 
1114         expr = strip_expr(expr);
1115         return_str = expr_to_str(expr);
1116         ret_sym = expr_to_sym(expr);
1117 
1118         FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1119                 if (has_empty_state(sm))
1120                         continue;
1121 
1122                 param = get_param_num_from_sym(sm->sym);
1123                 if (param < 0)
1124                         continue;
1125 



1126                 /* The logic here was that if we were passed in a user data then
1127                  * we don't record that.  It's like the difference between
1128                  * param_filter and param_set.  When I think about it, I'm not
1129                  * sure it actually works.  It's probably harmless because we
1130                  * checked earlier that we're not returning a parameter...
1131                  * Let's mark this as a TODO.
1132                  */
1133                 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
1134                 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
1135                         continue;
1136 
1137                 param_name = get_param_name(sm);
1138                 if (!param_name)
1139                         continue;
1140                 if (strcmp(param_name, "$") == 0)  /* The -1 param is handled after the loop */
1141                         continue;
1142 



1143                 sql_insert_return_states(return_id, return_ranges,
1144                                          func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1145                                          param, param_name, show_rl(estate_rl(sm->state)));
1146         } END_FOR_EACH_SM(sm);
1147 
1148         if (points_to_user_data(expr)) {
1149                 sql_insert_return_states(return_id, return_ranges,
1150                                          (is_skb_data(expr) || !func_gets_user_data) ?
1151                                          USER_DATA3_SET : USER_DATA3,
1152                                          -1, "*$", "");
1153                 goto free_string;
1154         }
1155 
1156 
1157         FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1158                 if (!ret_sym)
1159                         break;
1160                 if (ret_sym != sm->sym)
1161                         continue;
1162 
1163                 param_name = state_name_to_param_name(sm->name, return_str);
1164                 if (!param_name)
1165                         continue;
1166                 if (strcmp(param_name, "$") == 0)
1167                         return_found = true;





1168                 sql_insert_return_states(return_id, return_ranges,
1169                                          func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1170                                          -1, param_name, show_rl(estate_rl(sm->state)));
1171         } END_FOR_EACH_SM(sm);
1172 
1173 
1174         if (!return_found && get_user_rl(expr, &rl)) {


1175                 sql_insert_return_states(return_id, return_ranges,
1176                                          func_gets_user_data ? USER_DATA3_SET : USER_DATA3,
1177                                          -1, "$", show_rl(rl));
1178                 goto free_string;
1179         }
1180 
1181 free_string:










1182         free_string(return_str);
1183 }
1184 






















1185 static struct int_stack *gets_data_stack;
1186 static void match_function_def(struct symbol *sym)
1187 {
1188         func_gets_user_data = false;
1189 }
1190 
1191 static void match_inline_start(struct expression *expr)
1192 {
1193         push_int(&gets_data_stack, func_gets_user_data);
1194 }
1195 
1196 static void match_inline_end(struct expression *expr)
1197 {
1198         func_gets_user_data = pop_int(&gets_data_stack);
1199 }
1200 
1201 void register_kernel_user_data2(int id)
1202 {
1203         int i;
1204 
1205         my_id = id;
1206 
1207         if (option_project != PROJ_KERNEL)
1208                 return;
1209 


1210         add_hook(&match_function_def, FUNC_DEF_HOOK);
1211         add_hook(&match_inline_start, INLINE_FN_START);
1212         add_hook(&match_inline_end, INLINE_FN_END);
1213 
1214         add_hook(&save_start_states, AFTER_DEF_HOOK);
1215         add_hook(&free_start_states, AFTER_FUNC_HOOK);
1216         add_hook(&match_save_states, INLINE_FN_START);
1217         add_hook(&match_restore_states, INLINE_FN_END);
1218 
1219         add_unmatched_state_hook(my_id, &empty_state);
1220         add_extra_nomod_hook(&extra_nomod_hook);
1221         add_pre_merge_hook(my_id, &pre_merge_hook);
1222         add_merge_hook(my_id, &merge_estates);
1223 
1224         add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
1225         add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
1226         add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1227         for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1228                 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1229         add_function_hook("usb_control_msg", &match_user_copy, INT_PTR(6));
1230 
1231         for (i = 0; i < ARRAY_SIZE(returns_user_data); i++) {
1232                 add_function_assign_hook(returns_user_data[i], &match_user_assign_function, NULL);
1233                 add_function_hook(returns_user_data[i], &match_returns_user_rl, NULL);
1234         }
1235 
1236         add_function_hook("sscanf", &match_sscanf, NULL);
1237 
1238         add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1239 
1240         add_hook(&match_assign, ASSIGNMENT_HOOK);

1241         add_hook(&match_condition, CONDITION_HOOK);
1242 
1243         add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1244         add_member_info_callback(my_id, struct_member_callback);
1245         select_caller_info_hook(set_param_user_data, USER_DATA3);
1246         select_return_states_hook(USER_DATA3, &returns_param_user_data);
1247         select_return_states_hook(USER_DATA3_SET, &returns_param_user_data_set);

1248         add_split_return_callback(&param_set_to_user_data);
1249 }
1250 
1251 void register_kernel_user_data3(int id)
1252 {
1253         my_call_id = id;
1254 
1255         if (option_project != PROJ_KERNEL)
1256                 return;
1257         select_caller_info_hook(set_called, INTERNAL);
1258 }
1259 


  15  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
  16  */
  17 
  18 /*
  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.
  23  */
  24 
  25 #include "smatch.h"
  26 #include "smatch_slist.h"
  27 #include "smatch_extra.h"
  28 
  29 static int my_id;
  30 static int my_call_id;
  31 
  32 STATE(called);
  33 static bool func_gets_user_data;
  34 
  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",
  44 };
  45 
  46 static const char *returns_user_data[] = {
  47         "simple_strtol", "simple_strtoll", "simple_strtoul", "simple_strtoull",
  48         "kvm_register_read",

  49 };
  50 
  51 static const char *returns_pointer_to_user_data[] = {
  52         "nlmsg_data", "nla_data", "memdup_user", "kmap_atomic", "skb_network_header",
  53 };
  54 
  55 static void set_points_to_user_data(struct expression *expr);
  56 
  57 static struct stree *start_states;
  58 static struct stree_stack *saved_stack;
  59 static void save_start_states(struct statement *stmt)
  60 {
  61         start_states = clone_stree(__get_cur_stree());
  62 }
  63 
  64 static void free_start_states(void)
  65 {
  66         free_stree(&start_states);
  67 }
  68 
  69 static void match_save_states(struct expression *expr)
  70 {
  71         push_stree(&saved_stack, start_states);
  72         start_states = NULL;
  73 }
  74 
  75 static void match_restore_states(struct expression *expr)
  76 {
  77         free_stree(&start_states);
  78         start_states = pop_stree(&saved_stack);
  79 }
  80 
  81 static struct smatch_state *empty_state(struct sm_state *sm)
  82 {
  83         return alloc_estate_empty();
  84 }
  85 
  86 static void pre_merge_hook(struct sm_state *sm)
  87 {
  88         struct smatch_state *user;
  89         struct smatch_state *extra;
  90         struct smatch_state *state;
  91         struct range_list *rl;
  92         sval_t dummy;
  93         sval_t sval_100;
  94 
  95         sval_100.value = 100;
  96         sval_100.type = &int_ctype;
  97 
  98         user = __get_state(my_id, sm->name, sm->sym);
  99         if (!user || !estate_rl(user))
 100                 return;
 101         extra = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
 102         if (!extra)


















 103                 return;




 104         rl = rl_intersection(estate_rl(user), estate_rl(extra));
 105         if (rl_to_sval(rl, &dummy))
 106                 rl = NULL;
 107         state = alloc_estate_rl(clone_rl(rl));
 108         if (estate_capped(user) || is_capped_var_sym(sm->name, sm->sym))
 109                 estate_set_capped(state);
 110         set_state(my_id, sm->name, sm->sym, state);
 111 }
 112 
 113 static void extra_nomod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
 114 {
 115         struct smatch_state *user, *new;
 116         struct range_list *rl;
 117 
 118         user = __get_state(my_id, name, sym);
 119         if (!user)
 120                 return;
 121         rl = rl_intersection(estate_rl(user), estate_rl(state));
 122         if (rl_equiv(rl, estate_rl(user)))
 123                 return;
 124         new = alloc_estate_rl(rl);
 125         if (estate_capped(user))
 126                 estate_set_capped(new);
 127         set_state(my_id, name, sym, new);
 128 }
 129 
 130 static bool binop_capped(struct expression *expr)
 131 {
 132         struct range_list *left_rl;
 133         int comparison;
 134 
 135         if (expr->op == '-' && get_user_rl(expr->left, &left_rl)) {
 136                 if (user_rl_capped(expr->left))
 137                         return true;
 138                 comparison = get_comparison(expr->left, expr->right);
 139                 if (comparison && show_special(comparison)[0] == '>')
 140                         return true;
 141                 return false;
 142         }
 143 
 144         if (expr->op == '&' || expr->op == '%') {
 145                 if (is_capped(expr->left) || is_capped(expr->right))
 146                         return true;
 147                 if (user_rl_capped(expr->left) || user_rl_capped(expr->right))
 148                         return true;
 149                 return false;
 150         }
 151 
 152         if (user_rl_capped(expr->left) &&
 153             user_rl_capped(expr->right))
 154                 return true;
 155         return false;
 156 }
 157 
 158 bool user_rl_capped(struct expression *expr)
 159 {
 160         struct smatch_state *state;
 161         struct range_list *rl;
 162         sval_t sval;
 163 
 164         expr = strip_expr(expr);
 165         if (!expr)
 166                 return false;
 167         if (get_value(expr, &sval))
 168                 return true;
 169         if (expr->type == EXPR_BINOP)
 170                 return binop_capped(expr);
 171         if ((expr->type == EXPR_PREOP || expr->type == EXPR_POSTOP) &&
 172             (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT))
 173                 return user_rl_capped(expr->unop);
 174         state = get_state_expr(my_id, expr);
 175         if (state)
 176                 return estate_capped(state);
 177 
 178         if (get_user_rl(expr, &rl))
 179                 return false;  /* uncapped user data */
 180 
 181         return true;  /* not actually user data */
 182 }
 183 
 184 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
 185 {
 186         struct expression *edge_member;
 187         struct symbol *base = get_real_base_type(member);
 188         struct symbol *tmp;
 189 
 190         if (member->ident)
 191                 expr = member_expression(expr, '.', member->ident);
 192 
 193         FOR_EACH_PTR(base->symbol_list, tmp) {
 194                 struct symbol *type;
 195 
 196                 type = get_real_base_type(tmp);
 197                 if (!type)
 198                         continue;
 199 
 200                 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
 201                         tag_inner_struct_members(expr, tmp);
 202                         continue;
 203                 }


 372         if (!expr->member)
 373                 return 0;
 374         if (strcmp(expr->member->name, "data") != 0)
 375                 return 0;
 376 
 377         sym = expr_to_sym(expr->deref);
 378         if (!sym)
 379                 return 0;
 380         sym = get_real_base_type(sym);
 381         if (!sym || sym->type != SYM_PTR)
 382                 return 0;
 383         sym = get_real_base_type(sym);
 384         if (!sym || sym->type != SYM_STRUCT || !sym->ident)
 385                 return 0;
 386         if (strcmp(sym->ident->name, "sk_buff") != 0)
 387                 return 0;
 388 
 389         return 1;
 390 }
 391 
 392 static bool is_points_to_user_data_fn(struct expression *expr)
 393 {
 394         int i;
 395 
 396         expr = strip_expr(expr);
 397         if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL ||
 398             !expr->fn->symbol)
 399                 return false;
 400         expr = expr->fn;
 401         for (i = 0; i < ARRAY_SIZE(returns_pointer_to_user_data); i++) {
 402                 if (sym_name_is(returns_pointer_to_user_data[i], expr))
 403                         return true;
 404         }
 405         return false;
 406 }
 407 
 408 static int get_rl_from_function(struct expression *expr, struct range_list **rl)
 409 {
 410         int i;
 411 
 412         if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL ||
 413             !expr->fn->symbol_name || !expr->fn->symbol_name->name)
 414                 return 0;
 415 
 416         for (i = 0; i < ARRAY_SIZE(returns_user_data); i++) {
 417                 if (strcmp(expr->fn->symbol_name->name, returns_user_data[i]) == 0) {
 418                         *rl = alloc_whole_rl(get_type(expr));
 419                         return 1;
 420                 }
 421         }
 422         return 0;
 423 }
 424 
 425 int points_to_user_data(struct expression *expr)
 426 {
 427         struct smatch_state *state;
 428         struct range_list *rl;
 429         char buf[256];
 430         struct symbol *sym;
 431         char *name;
 432         int ret = 0;
 433 
 434         expr = strip_expr(expr);
 435         if (!expr)
 436                 return 0;
 437         if (is_skb_data(expr))
 438                 return 1;
 439         if (is_points_to_user_data_fn(expr))
 440                 return 1;
 441         if (get_rl_from_function(expr, &rl))
 442                 return 1;
 443 
 444         if (expr->type == EXPR_BINOP && expr->op == '+') {
 445                 if (points_to_user_data(expr->left))
 446                         return 1;
 447                 if (points_to_user_data(expr->right))
 448                         return 1;
 449                 return 0;
 450         }
 451 
 452         name = expr_to_var_sym(expr, &sym);
 453         if (!name || !sym)
 454                 goto free;
 455         snprintf(buf, sizeof(buf), "*%s", name);
 456         state = __get_state(my_id, buf, sym);
 457         if (state && estate_rl(state))
 458                 ret = 1;
 459 free:
 460         free_string(name);
 461         return ret;
 462 }
 463 
 464 static void set_points_to_user_data(struct expression *expr)
 465 {
 466         char *name;
 467         struct symbol *sym;
 468         char buf[256];
 469         struct symbol *type;
 470 
 471         name = expr_to_var_sym(expr, &sym);
 472         if (!name || !sym)
 473                 goto free;
 474         snprintf(buf, sizeof(buf), "*%s", name);
 475         type = get_type(expr);
 476         if (type && type->type == SYM_PTR)
 477                 type = get_real_base_type(type);
 478         if (!type || type->type != SYM_BASETYPE)
 479                 type = &llong_ctype;
 480         set_state(my_id, buf, sym, alloc_estate_whole(type));
 481 free:
 482         free_string(name);
 483 }
 484 
 485 static int comes_from_skb_data(struct expression *expr)
 486 {
 487         expr = strip_expr(expr);
 488         if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
 489                 return 0;
 490 
 491         expr = strip_expr(expr->unop);
 492         if (!expr)
 493                 return 0;
 494         if (expr->type == EXPR_BINOP && expr->op == '+')
 495                 expr = strip_expr(expr->left);
 496 
 497         return is_skb_data(expr);
 498 }
 499 
 500 static int handle_struct_assignment(struct expression *expr)


 538 
 539 static int handle_get_user(struct expression *expr)
 540 {
 541         char *name;
 542         int ret = 0;
 543 
 544         name = get_macro_name(expr->pos);
 545         if (!name || strcmp(name, "get_user") != 0)
 546                 return 0;
 547 
 548         name = expr_to_var(expr->right);
 549         if (!name || strcmp(name, "__val_gu") != 0)
 550                 goto free;
 551         set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
 552         ret = 1;
 553 free:
 554         free_string(name);
 555         return ret;
 556 }
 557 
 558 static bool handle_op_assign(struct expression *expr)
 559 {
 560         struct expression *binop_expr;
 561         struct smatch_state *state;
 562         struct range_list *rl;
 563 
 564         switch (expr->op) {
 565         case SPECIAL_ADD_ASSIGN:
 566         case SPECIAL_SUB_ASSIGN:
 567         case SPECIAL_AND_ASSIGN:
 568         case SPECIAL_MOD_ASSIGN:
 569         case SPECIAL_SHL_ASSIGN:
 570         case SPECIAL_SHR_ASSIGN:
 571         case SPECIAL_OR_ASSIGN:
 572         case SPECIAL_XOR_ASSIGN:
 573         case SPECIAL_MUL_ASSIGN:
 574         case SPECIAL_DIV_ASSIGN:
 575                 binop_expr = binop_expression(expr->left,
 576                                               op_remove_assign(expr->op),
 577                                               expr->right);
 578                 if (!get_user_rl(binop_expr, &rl))
 579                         return true;
 580 
 581                 rl = cast_rl(get_type(expr->left), rl);
 582                 state = alloc_estate_rl(rl);
 583                 if (user_rl_capped(binop_expr))
 584                         estate_set_capped(state);
 585                 set_state_expr(my_id, expr->left, state);
 586                 return true;
 587         }
 588         return false;
 589 }
 590 
 591 static void match_assign(struct expression *expr)
 592 {
 593         struct range_list *rl;
 594         static struct expression *handled;
 595         struct smatch_state *state;
 596         struct expression *faked;
 597 
 598         faked = get_faked_expression();
 599         if (faked && faked == handled)
 600                 return;
 601         if (is_fake_call(expr->right))
 602                 goto clear_old_state;
 603         if (handle_get_user(expr))
 604                 return;
 605         if (points_to_user_data(expr->right)) {
 606                 handled = expr;
 607                 set_points_to_user_data(expr->left);
 608         }
 609         if (handle_struct_assignment(expr))
 610                 return;
 611 
 612         if (handle_op_assign(expr))
 613                 return;
 614         if (expr->op != '=')
 615                 goto clear_old_state;
 616 
 617         /* Handled by DB code */
 618         if (expr->right->type == EXPR_CALL || __in_fake_parameter_assign)
 619                 return;
 620 
 621         if (!get_user_rl(expr->right, &rl))
 622                 goto clear_old_state;
 623 
 624         rl = cast_rl(get_type(expr->left), rl);
 625         state = alloc_estate_rl(rl);
 626         if (user_rl_capped(expr->right))
 627                 estate_set_capped(state);
 628         set_state_expr(my_id, expr->left, state);
 629 
 630         return;
 631 
 632 clear_old_state:
 633         if (get_state_expr(my_id, expr->left))
 634                 set_state_expr(my_id, expr->left, alloc_estate_empty());
 635 }
 636 
 637 static void handle_eq_noteq(struct expression *expr)
 638 {
 639         struct smatch_state *left_orig, *right_orig;
 640 
 641         left_orig = get_state_expr(my_id, expr->left);
 642         right_orig = get_state_expr(my_id, expr->right);
 643 
 644         if (!left_orig && !right_orig)
 645                 return;
 646         if (left_orig && right_orig)
 647                 return;
 648 
 649         if (left_orig) {
 650                 set_true_false_states_expr(my_id, expr->left,
 651                                 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
 652                                 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
 653         } else {
 654                 set_true_false_states_expr(my_id, expr->right,
 655                                 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
 656                                 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
 657         }
 658 }
 659 
 660 static struct range_list *strip_negatives(struct range_list *rl)
 661 {
 662         sval_t min = rl_min(rl);
 663         sval_t minus_one;
 664         sval_t over;
 665         sval_t max = sval_type_max(rl_type(rl));
 666 
 667         minus_one.type = rl_type(rl);
 668         minus_one.value = INT_MAX + 1ULL;
 669         over.type = rl_type(rl);
 670         over.value = -1;
 671 
 672         if (!rl)
 673                 return NULL;
 674 
 675         if (type_unsigned(rl_type(rl)) && type_bits(rl_type(rl)) > 31)
 676                 return remove_range(rl, over, max);
 677 
 678         return remove_range(rl, min, minus_one);
 679 }
 680 
 681 static void handle_compare(struct expression *expr)
 682 {
 683         struct expression  *left, *right;
 684         struct range_list *left_rl = NULL;
 685         struct range_list *right_rl = NULL;
 686         struct range_list *user_rl;
 687         struct smatch_state *capped_state;
 688         struct smatch_state *left_true = NULL;
 689         struct smatch_state *left_false = NULL;
 690         struct smatch_state *right_true = NULL;
 691         struct smatch_state *right_false = NULL;
 692         struct symbol *type;
 693         sval_t sval;



 694 
 695         left = strip_expr(expr->left);
 696         right = strip_expr(expr->right);
 697 
 698         while (left->type == EXPR_ASSIGNMENT)
 699                 left = strip_expr(left->left);
 700 
 701         /*
 702          * Conditions are mostly handled by smatch_extra.c, but there are some
 703          * times where the exact values are not known so we can't do that.





 704          *
 705          * Normally, we might consider using smatch_capped.c to supliment smatch
 706          * extra but that doesn't work when we merge unknown uncapped kernel
 707          * data with unknown capped user data.  The result is uncapped user
 708          * data.  We need to keep it separate and say that the user data is
 709          * capped.  In the past, I would have marked this as just regular
 710          * kernel data (not user data) but we can't do that these days because
 711          * we need to track user data for Spectre.
 712          *
 713          * The other situation which we have to handle is when we do have an
 714          * int and we compare against an unknown unsigned kernel variable.  In
 715          * that situation we assume that the kernel data is less than INT_MAX.
 716          * Otherwise then we get all sorts of array underflow false positives.
 717          *
 718          */
 719 
 720         /* Handled in smatch_extra.c */
 721         if (get_implied_value(left, &sval) ||
 722             get_implied_value(right, &sval))
 723                 return;
 724 
 725         get_user_rl(left, &left_rl);
 726         get_user_rl(right, &right_rl);
 727 
 728         /* nothing to do */
 729         if (!left_rl && !right_rl)
 730                 return;
 731         /* if both sides are user data that's not a good limit */
 732         if (left_rl && right_rl)
 733                 return;
 734 
 735         if (left_rl)
 736                 user_rl = left_rl;
 737         else
 738                 user_rl = right_rl;


 739 
 740         type = get_type(expr);
 741         if (type_unsigned(type))
 742                 user_rl = strip_negatives(user_rl);
 743         capped_state = alloc_estate_rl(user_rl);
 744         estate_set_capped(capped_state);
 745 
 746         switch (expr->op) {
 747         case '<':
 748         case SPECIAL_UNSIGNED_LT:
 749         case SPECIAL_LTE:
 750         case SPECIAL_UNSIGNED_LTE:
 751                 if (left_rl)
 752                         left_true = capped_state;
 753                 else
 754                         right_false = capped_state;
 755                 break;
 756         case '>':
 757         case SPECIAL_UNSIGNED_GT:
 758         case SPECIAL_GTE:
 759         case SPECIAL_UNSIGNED_GTE:
 760                 if (left_rl)
 761                         left_false = capped_state;
 762                 else
 763                         right_true = capped_state;
 764                 break;
 765         }
 766 
 767         set_true_false_states_expr(my_id, left, left_true, left_false);
 768         set_true_false_states_expr(my_id, right, right_true, right_false);
 769 }
 770 
 771 static void match_condition(struct expression *expr)
 772 {
 773         if (expr->type != EXPR_COMPARE)
 774                 return;
 775 
 776         if (expr->op == SPECIAL_EQUAL ||
 777             expr->op == SPECIAL_NOTEQUAL) {
 778                 handle_eq_noteq(expr);
 779                 return;
 780         }
 781 
 782         handle_compare(expr);
 783 }
 784 
 785 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
 786 {
 787         tag_as_user_data(expr->left);
 788         set_points_to_user_data(expr->left);
 789 }
 790 
 791 static void match_returns_user_rl(const char *fn, struct expression *expr, void *unused)
 792 {
 793         func_gets_user_data = true;
 794 }
 795 
 796 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
 797 {
 798         struct expression *parent;
 799         char *macro;
 800 
 801         if (!expr)
 802                 return 0;


 810         while (parent && parent->type != EXPR_BINOP)
 811                 parent = expr_get_parent_expr(parent);
 812         if (parent && parent->type == EXPR_BINOP) {
 813                 char *parent_macro = get_macro_name(parent->pos);
 814 
 815                 if (parent_macro && strcmp(macro, parent_macro) == 0)
 816                         return 0;
 817         }
 818 
 819         if (strcmp(macro, "ntohl") == 0) {
 820                 *rl = alloc_whole_rl(&uint_ctype);
 821                 return 1;
 822         }
 823         if (strcmp(macro, "ntohs") == 0) {
 824                 *rl = alloc_whole_rl(&ushort_ctype);
 825                 return 1;
 826         }
 827         return 0;
 828 }
 829 
































 830 static int has_user_data(struct symbol *sym)
 831 {
 832         struct sm_state *tmp;
 833 
 834         FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
 835                 if (tmp->sym == sym)
 836                         return 1;
 837         } END_FOR_EACH_SM(tmp);
 838         return 0;
 839 }
 840 
 841 static int we_pass_user_data(struct expression *call)
 842 {
 843         struct expression *arg;
 844         struct symbol *sym;
 845 
 846         FOR_EACH_PTR(call->args, arg) {
 847                 sym = expr_to_sym(arg);
 848                 if (!sym)
 849                         continue;
 850                 if (has_user_data(sym))
 851                         return 1;
 852         } END_FOR_EACH_PTR(arg);
 853 
 854         return 0;
 855 }
 856 
 857 static int db_returned_user_rl(struct expression *call, struct range_list **rl)
 858 {
 859         struct smatch_state *state;
 860         char buf[48];
 861 



 862         if (is_fake_call(call))
 863                 return 0;
 864         snprintf(buf, sizeof(buf), "return %p", call);
 865         state = get_state(my_id, buf, NULL);
 866         if (!state || !estate_rl(state))













 867                 return 0;
 868         *rl = estate_rl(state);
 869         return 1;



 870 }
 871 
 872 struct stree *get_user_stree(void)
 873 {
 874         return get_all_states_stree(my_id);
 875 }
 876 
 877 static int user_data_flag;
 878 static int no_user_data_flag;
 879 struct range_list *var_user_rl(struct expression *expr)
 880 {
 881         struct smatch_state *state;
 882         struct range_list *rl;
 883         struct range_list *absolute_rl;
 884 
 885         if (expr->type == EXPR_PREOP && expr->op == '&') {
 886                 no_user_data_flag = 1;
 887                 return NULL;
 888         }
 889 
 890         if (expr->type == EXPR_BINOP && expr->op == '%') {
 891                 struct range_list *left, *right;
 892 
 893                 if (!get_user_rl(expr->right, &right))
 894                         return NULL;
 895                 get_absolute_rl(expr->left, &left);
 896                 rl = rl_binop(left, '%', right);
 897                 goto found;
 898         }
 899 
 900         if (expr->type == EXPR_BINOP && expr->op == '/') {
 901                 struct range_list *left = NULL;
 902                 struct range_list *right = NULL;
 903                 struct range_list *abs_right;
 904 
 905                 /*
 906                  * The specific bug I'm dealing with is:
 907                  *
 908                  * foo = capped_user / unknown;
 909                  *
 910                  * Instead of just saying foo is now entirely user_rl we should
 911                  * probably say instead that it is not at all user data.
 912                  *
 913                  */
 914 
 915                 get_user_rl(expr->left, &left);
 916                 get_user_rl(expr->right, &right);
 917                 get_absolute_rl(expr->right, &abs_right);
 918 
 919                 if (left && !right) {
 920                         rl = rl_binop(left, '/', abs_right);


 950 
 951                 if (!get_state_expr(my_id, array)) {
 952                         no_user_data_flag = 1;
 953                         return NULL;
 954                 }
 955         }
 956 
 957         if (expr->type == EXPR_PREOP && expr->op == '*' &&
 958             is_user_rl(expr->unop)) {
 959                 rl = var_to_absolute_rl(expr);
 960                 goto found;
 961         }
 962 
 963         return NULL;
 964 found:
 965         user_data_flag = 1;
 966         absolute_rl = var_to_absolute_rl(expr);
 967         return clone_rl(rl_intersection(rl, absolute_rl));
 968 }
 969 
 970 static bool is_ptr_subtract(struct expression *expr)
 971 {
 972         expr = strip_expr(expr);
 973         if (!expr)
 974                 return false;
 975         if (expr->type == EXPR_BINOP && expr->op == '-' &&
 976             type_is_ptr(get_type(expr->left))) {
 977                 return true;
 978         }
 979         return false;
 980 }
 981 
 982 int get_user_rl(struct expression *expr, struct range_list **rl)
 983 {
 984         if (is_ptr_subtract(expr))
 985                 return 0;
 986 
 987         user_data_flag = 0;
 988         no_user_data_flag = 0;
 989         custom_get_absolute_rl(expr, &var_user_rl, rl);
 990         if (!user_data_flag || no_user_data_flag)
 991                 *rl = NULL;
 992 
 993         return !!*rl;
 994 }
 995 











 996 int is_user_rl(struct expression *expr)
 997 {
 998         struct range_list *tmp;
 999 
1000         return !!get_user_rl(expr, &tmp);
1001 }
1002 
1003 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
1004 {
1005         struct smatch_state *state;
1006 
1007         state = get_state(my_id, name, sym);
1008         if (state && estate_rl(state)) {
1009                 *rl = estate_rl(state);
1010                 return 1;
1011         }
1012         return 0;
1013 }
1014 
1015 static char *get_user_rl_str(struct expression *expr, struct symbol *type)
1016 {
1017         struct range_list *rl;
1018         static char buf[64];
1019 
1020         if (!get_user_rl(expr, &rl))
1021                 return NULL;
1022         rl = cast_rl(type, rl);
1023         snprintf(buf, sizeof(buf), "%s%s",
1024                  show_rl(rl), user_rl_capped(expr) ? "[c]" : "");
1025         return buf;
1026 }
1027 
1028 static void match_call_info(struct expression *expr)
1029 {
1030         struct expression *arg;
1031         struct symbol *type;
1032         char *str;
1033         int i;
1034 
1035         i = -1;
1036         FOR_EACH_PTR(expr->args, arg) {
1037                 i++;
1038                 type = get_arg_type(expr->fn, i);
1039                 str = get_user_rl_str(arg, type);
1040                 if (!str)
1041                         continue;
1042 
1043                 sql_insert_caller_info(expr, USER_DATA, i, "$", str);

1044         } END_FOR_EACH_PTR(arg);
1045 }
1046 
1047 static int is_struct_ptr(struct symbol *sym)
1048 {
1049         struct symbol *type;
1050 
1051         if (!sym)
1052                 return 0;
1053         type = get_real_base_type(sym);
1054         if (!type || type->type != SYM_PTR)
1055                 return 0;
1056         type = get_real_base_type(type);
1057         if (!type || type->type != SYM_STRUCT)
1058                 return 0;
1059         return 1;
1060 }
1061 
1062 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1063 {
1064         struct smatch_state *state;
1065         struct range_list *rl;
1066         struct symbol *type;
1067         char buf[64];
1068 
1069         /*
1070          * Smatch uses a hack where if we get an unsigned long we say it's
1071          * both user data and it points to user data.  But if we pass it to a
1072          * function which takes an int, then it's just user data.  There's not
1073          * enough bytes for it to be a pointer.
1074          *
1075          */
1076         type = get_arg_type(call->fn, param);
1077         if (type && type_bits(type) < type_bits(&ptr_ctype))
1078                 return;
1079 
1080         if (strcmp(sm->state->name, "") == 0)
1081                 return;
1082 
1083         if (strcmp(printed_name, "*$") == 0 &&
1084             is_struct_ptr(sm->sym))
1085                 return;
1086 
1087         state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
1088         if (!state || !estate_rl(state))
1089                 rl = estate_rl(sm->state);
1090         else
1091                 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
1092 
1093         if (!rl)
1094                 return;
1095 
1096         snprintf(buf, sizeof(buf), "%s%s", show_rl(rl),
1097                  estate_capped(sm->state) ? "[c]" : "");
1098         sql_insert_caller_info(call, USER_DATA, param, printed_name, buf);
1099 }
1100 
1101 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1102 {
1103         struct expression *arg;
1104         char *name;
1105         struct symbol *sym;
1106         struct smatch_state *state;
1107 
1108         while (expr->type == EXPR_ASSIGNMENT)
1109                 expr = strip_expr(expr->right);
1110         if (expr->type != EXPR_CALL)
1111                 return;
1112 
1113         arg = get_argument_from_call_expr(expr->args, param);
1114         if (!arg)
1115                 return;
1116         name = get_variable_from_key(arg, key, &sym);
1117         if (!name || !sym)
1118                 goto free;
1119 
1120         state = get_state(my_id, name, sym);
1121         if (!state)
1122                 goto free;
1123 
1124         set_state(my_id, name, sym, alloc_estate_empty());
1125 free:
1126         free_string(name);
1127 }
1128 
1129 static bool param_data_capped(const char *value)
1130 {
1131         if (strstr(value, ",c") || strstr(value, "[c"))
1132                 return true;
1133         return false;
1134 }
1135 
1136 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
1137 {
1138         struct range_list *rl = NULL;
1139         struct smatch_state *state;
1140         struct expression *expr;
1141         struct symbol *type;
1142         char fullname[256];
1143         char *key_orig = key;
1144         bool add_star = false;
1145 
1146         if (strcmp(key, "**$") == 0) {
1147                 snprintf(fullname, sizeof(fullname), "**%s", name);
1148         } else {
1149                 if (key[0] == '*') {
1150                         add_star = true;
1151                         key++;
1152                 }
1153 
1154                 snprintf(fullname, 256, "%s%s%s", add_star ? "*" : "", name, key + 1);
1155         }
1156 
1157         expr = symbol_expression(sym);
1158         type = get_member_type_from_key(expr, key_orig);

1159 
1160         /*
1161          * Say this function takes a struct ponter but the caller passes
1162          * this_function(skb->data).  We have two options, we could pass *$
1163          * as user data or we could pass foo->bar, foo->baz as user data.
1164          * The second option is easier to implement so we do that.
1165          *
1166          */
1167         if (strcmp(key_orig, "*$") == 0) {
1168                 struct symbol *tmp = type;
1169 
1170                 while (tmp && tmp->type == SYM_PTR)
1171                         tmp = get_real_base_type(tmp);
1172 
1173                 if (tmp && (tmp->type == SYM_STRUCT || tmp->type == SYM_UNION)) {
1174                         tag_as_user_data(symbol_expression(sym));
1175                         return;
1176                 }
1177         }
1178 
1179         str_to_rl(type, value, &rl);
1180         state = alloc_estate_rl(rl);
1181         if (param_data_capped(value) || is_capped(expr))
1182                 estate_set_capped(state);
1183         set_state(my_id, fullname, sym, state);
1184 }
1185 
1186 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
1187 {
1188         set_state(my_call_id, "this_function", NULL, &called);
1189 }
1190 
1191 static void match_syscall_definition(struct symbol *sym)
1192 {
1193         struct symbol *arg;
1194         char *macro;
1195         char *name;
1196         int is_syscall = 0;
1197 
1198         macro = get_macro_name(sym->pos);
1199         if (macro &&
1200             (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
1201              strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1202                 is_syscall = 1;
1203 
1204         name = get_function();
1205         if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
1206                 if (name && strncmp(name, "sys_", 4) == 0)
1207                         is_syscall = 1;
1208         }
1209 
1210         if (name && strncmp(name, "compat_sys_", 11) == 0)
1211                 is_syscall = 1;
1212 
1213         if (!is_syscall)
1214                 return;
1215 
1216         FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
1217                 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
1218         } END_FOR_EACH_PTR(arg);
1219 }
1220 
1221 static void store_user_data_return(struct expression *expr, char *key, char *value)
1222 {
1223         struct range_list *rl;
1224         struct symbol *type;
1225         char buf[48];
1226 
1227         if (strcmp(key, "$") != 0)
1228                 return;
1229 
1230         type = get_type(expr);
1231         snprintf(buf, sizeof(buf), "return %p", expr);
1232         call_results_to_rl(expr, type, value, &rl);
1233 
1234         set_state(my_id, buf, NULL, alloc_estate_rl(rl));
1235 }
1236 
1237 static void set_to_user_data(struct expression *expr, char *key, char *value)
1238 {
1239         struct smatch_state *state;
1240         char *name;
1241         struct symbol *sym;
1242         struct symbol *type;
1243         struct range_list *rl = NULL;
1244 
1245         type = get_member_type_from_key(expr, key);
1246         name = get_variable_from_key(expr, key, &sym);
1247         if (!name || !sym)
1248                 goto free;
1249 
1250         call_results_to_rl(expr, type, value, &rl);
1251 
1252         state = alloc_estate_rl(rl);
1253         if (param_data_capped(value))
1254                 estate_set_capped(state);
1255         set_state(my_id, name, sym, state);
1256 free:
1257         free_string(name);

1258 }
1259 
1260 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
1261 {
1262         struct expression *arg;
1263         struct expression *call;
1264 
1265         call = expr;
1266         while (call->type == EXPR_ASSIGNMENT)
1267                 call = strip_expr(call->right);
1268         if (call->type != EXPR_CALL)
1269                 return;
1270 
1271         if (!we_pass_user_data(call))
1272                 return;
1273 
1274         if (param == -1) {
1275                 if (expr->type != EXPR_ASSIGNMENT) {
1276                         store_user_data_return(expr, key, value);
1277                         return;
1278                 }
1279                 set_to_user_data(expr->left, key, value);
1280                 return;
1281         }
1282 
1283         arg = get_argument_from_call_expr(call->args, param);
1284         if (!arg)
1285                 return;
1286         set_to_user_data(arg, key, value);
1287 }
1288 
1289 static void returns_param_user_data_set(struct expression *expr, int param, char *key, char *value)
1290 {
1291         struct expression *arg;
1292 
1293         func_gets_user_data = true;
1294 
1295         if (param == -1) {
1296                 if (expr->type != EXPR_ASSIGNMENT) {
1297                         store_user_data_return(expr, key, value);
1298                         return;
1299                 }
1300                 if (strcmp(key, "*$") == 0) {
1301                         set_points_to_user_data(expr->left);
1302                         tag_as_user_data(expr->left);
1303                 } else {
1304                         set_to_user_data(expr->left, key, value);
1305                 }
1306                 return;
1307         }
1308 
1309         while (expr->type == EXPR_ASSIGNMENT)
1310                 expr = strip_expr(expr->right);
1311         if (expr->type != EXPR_CALL)
1312                 return;
1313 
1314         arg = get_argument_from_call_expr(expr->args, param);
1315         if (!arg)
1316                 return;
1317         set_to_user_data(arg, key, value);
1318 }
1319 












1320 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
1321 {
1322         struct sm_state *sm;
1323         struct smatch_state *start_state;
1324         struct range_list *rl;
1325         int param;
1326         char *return_str;
1327         const char *param_name;
1328         struct symbol *ret_sym;
1329         bool return_found = false;
1330         bool pointed_at_found = false;
1331         char buf[64];
1332 
1333         expr = strip_expr(expr);
1334         return_str = expr_to_str(expr);
1335         ret_sym = expr_to_sym(expr);
1336 
1337         FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {



1338                 param = get_param_num_from_sym(sm->sym);
1339                 if (param < 0)
1340                         continue;
1341 
1342                 if (!param_was_set_var_sym(sm->name, sm->sym))
1343                         continue;
1344 
1345                 /* The logic here was that if we were passed in a user data then
1346                  * we don't record that.  It's like the difference between
1347                  * param_filter and param_set.  When I think about it, I'm not
1348                  * sure it actually works.  It's probably harmless because we
1349                  * checked earlier that we're not returning a parameter...
1350                  * Let's mark this as a TODO.
1351                  */
1352                 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
1353                 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
1354                         continue;
1355 
1356                 param_name = get_param_name(sm);
1357                 if (!param_name)
1358                         continue;
1359                 if (strcmp(param_name, "$") == 0)  /* The -1 param is handled after the loop */
1360                         continue;
1361 
1362                 snprintf(buf, sizeof(buf), "%s%s",
1363                          show_rl(estate_rl(sm->state)),
1364                          estate_capped(sm->state) ? "[c]" : "");
1365                 sql_insert_return_states(return_id, return_ranges,
1366                                          func_gets_user_data ? USER_DATA_SET : USER_DATA,
1367                                          param, param_name, buf);
1368         } END_FOR_EACH_SM(sm);
1369 
1370         /* This if for "return foo;" where "foo->bar" is user data. */








1371         FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1372                 if (!ret_sym)
1373                         break;
1374                 if (ret_sym != sm->sym)
1375                         continue;
1376 
1377                 param_name = state_name_to_param_name(sm->name, return_str);
1378                 if (!param_name)
1379                         continue;
1380                 if (strcmp(param_name, "$") == 0)
1381                         return_found = true;
1382                 if (strcmp(param_name, "*$") == 0)
1383                         pointed_at_found = true;
1384                 snprintf(buf, sizeof(buf), "%s%s",
1385                          show_rl(estate_rl(sm->state)),
1386                          estate_capped(sm->state) ? "[c]" : "");
1387                 sql_insert_return_states(return_id, return_ranges,
1388                                          func_gets_user_data ? USER_DATA_SET : USER_DATA,
1389                                          -1, param_name, buf);
1390         } END_FOR_EACH_SM(sm);
1391 
1392         /* This if for "return ntohl(foo);" */
1393         if (!return_found && get_user_rl(expr, &rl)) {
1394                 snprintf(buf, sizeof(buf), "%s%s",
1395                          show_rl(rl), user_rl_capped(expr) ? "[c]" : "");
1396                 sql_insert_return_states(return_id, return_ranges,
1397                                          func_gets_user_data ? USER_DATA_SET : USER_DATA,
1398                                          -1, "$", buf);

1399         }
1400 
1401         /*
1402          * This is to handle things like return skb->data where we don't set a
1403          * state for that.
1404          */
1405         if (!pointed_at_found && points_to_user_data(expr)) {
1406                 sql_insert_return_states(return_id, return_ranges,
1407                                          (is_skb_data(expr) || func_gets_user_data) ?
1408                                          USER_DATA_SET : USER_DATA,
1409                                          -1, "*$", "s64min-s64max");
1410         }
1411 
1412         free_string(return_str);
1413 }
1414 
1415 static void returns_param_capped(struct expression *expr, int param, char *key, char *value)
1416 {
1417         struct smatch_state *state, *new;
1418         struct symbol *sym;
1419         char *name;
1420 
1421         name = return_state_to_var_sym(expr, param, key, &sym);
1422         if (!name || !sym)
1423                 goto free;
1424 
1425         state = get_state(my_id, name, sym);
1426         if (!state || estate_capped(state))
1427                 goto free;
1428 
1429         new = clone_estate(state);
1430         estate_set_capped(new);
1431 
1432         set_state(my_id, name, sym, new);
1433 free:
1434         free_string(name);
1435 }
1436 
1437 static struct int_stack *gets_data_stack;
1438 static void match_function_def(struct symbol *sym)
1439 {
1440         func_gets_user_data = false;
1441 }
1442 
1443 static void match_inline_start(struct expression *expr)
1444 {
1445         push_int(&gets_data_stack, func_gets_user_data);
1446 }
1447 
1448 static void match_inline_end(struct expression *expr)
1449 {
1450         func_gets_user_data = pop_int(&gets_data_stack);
1451 }
1452 
1453 void register_kernel_user_data(int id)
1454 {
1455         int i;
1456 
1457         my_id = id;
1458 
1459         if (option_project != PROJ_KERNEL)
1460                 return;
1461 
1462         set_dynamic_states(my_id);
1463 
1464         add_hook(&match_function_def, FUNC_DEF_HOOK);
1465         add_hook(&match_inline_start, INLINE_FN_START);
1466         add_hook(&match_inline_end, INLINE_FN_END);
1467 
1468         add_hook(&save_start_states, AFTER_DEF_HOOK);
1469         add_hook(&free_start_states, AFTER_FUNC_HOOK);
1470         add_hook(&match_save_states, INLINE_FN_START);
1471         add_hook(&match_restore_states, INLINE_FN_END);
1472 
1473         add_unmatched_state_hook(my_id, &empty_state);
1474         add_extra_nomod_hook(&extra_nomod_hook);
1475         add_pre_merge_hook(my_id, &pre_merge_hook);
1476         add_merge_hook(my_id, &merge_estates);
1477 
1478         add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
1479         add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
1480         add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
1481         for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
1482                 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
1483         add_function_hook("usb_control_msg", &match_user_copy, INT_PTR(6));
1484 
1485         for (i = 0; i < ARRAY_SIZE(returns_user_data); i++) {
1486                 add_function_assign_hook(returns_user_data[i], &match_user_assign_function, NULL);
1487                 add_function_hook(returns_user_data[i], &match_returns_user_rl, NULL);
1488         }
1489 
1490         add_function_hook("sscanf", &match_sscanf, NULL);
1491 
1492         add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
1493 
1494         add_hook(&match_assign, ASSIGNMENT_HOOK);
1495         select_return_states_hook(PARAM_SET, &db_param_set);
1496         add_hook(&match_condition, CONDITION_HOOK);
1497 
1498         add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1499         add_member_info_callback(my_id, struct_member_callback);
1500         select_caller_info_hook(set_param_user_data, USER_DATA);
1501         select_return_states_hook(USER_DATA, &returns_param_user_data);
1502         select_return_states_hook(USER_DATA_SET, &returns_param_user_data_set);
1503         select_return_states_hook(CAPPED_DATA, &returns_param_capped);
1504         add_split_return_callback(&param_set_to_user_data);
1505 }
1506 
1507 void register_kernel_user_data2(int id)
1508 {
1509         my_call_id = id;
1510 
1511         if (option_project != PROJ_KERNEL)
1512                 return;
1513         select_caller_info_hook(set_called, INTERNAL);
1514 }