Print this page
11972 resync smatch


  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;


 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                 }


 529         if (right_type == left_type)
 530                 return 0;
 531 
 532         if (!points_to_user_data(right))
 533                 return 0;
 534 
 535         tag_as_user_data(expr->left);
 536         return 1;
 537 }
 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;


 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;


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);


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         }


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;


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) {


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)


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 }



  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 *cur, struct sm_state *other)
  87 {
  88         struct smatch_state *user = cur->state;
  89         struct smatch_state *extra;
  90         struct smatch_state *state;
  91         struct range_list *rl;


  92 
  93         extra = __get_state(SMATCH_EXTRA, cur->name, cur->sym);






  94         if (!extra)
  95                 return;
  96         rl = rl_intersection(estate_rl(user), estate_rl(extra));


  97         state = alloc_estate_rl(clone_rl(rl));
  98         if (estate_capped(user) || is_capped_var_sym(cur->name, cur->sym))
  99                 estate_set_capped(state);
 100         if (estate_treat_untagged(user))
 101                 estate_set_treat_untagged(state);
 102         set_state(my_id, cur->name, cur->sym, state);
 103 }
 104 
 105 static void extra_nomod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
 106 {
 107         struct smatch_state *user, *new;
 108         struct range_list *rl;
 109 
 110         user = __get_state(my_id, name, sym);
 111         if (!user)
 112                 return;
 113         rl = rl_intersection(estate_rl(user), estate_rl(state));
 114         if (rl_equiv(rl, estate_rl(user)))
 115                 return;
 116         new = alloc_estate_rl(rl);
 117         if (estate_capped(user))
 118                 estate_set_capped(new);
 119         if (estate_treat_untagged(user))
 120                 estate_set_treat_untagged(new);
 121         set_state(my_id, name, sym, new);
 122 }
 123 
 124 static bool binop_capped(struct expression *expr)
 125 {
 126         struct range_list *left_rl;
 127         int comparison;
 128 
 129         if (expr->op == '-' && get_user_rl(expr->left, &left_rl)) {
 130                 if (user_rl_capped(expr->left))
 131                         return true;
 132                 comparison = get_comparison(expr->left, expr->right);
 133                 if (comparison && show_special(comparison)[0] == '>')
 134                         return true;
 135                 return false;
 136         }
 137 
 138         if (expr->op == '&' || expr->op == '%') {
 139                 if (is_capped(expr->left) || is_capped(expr->right))
 140                         return true;


 158         expr = strip_expr(expr);
 159         if (!expr)
 160                 return false;
 161         if (get_value(expr, &sval))
 162                 return true;
 163         if (expr->type == EXPR_BINOP)
 164                 return binop_capped(expr);
 165         if ((expr->type == EXPR_PREOP || expr->type == EXPR_POSTOP) &&
 166             (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT))
 167                 return user_rl_capped(expr->unop);
 168         state = get_state_expr(my_id, expr);
 169         if (state)
 170                 return estate_capped(state);
 171 
 172         if (get_user_rl(expr, &rl))
 173                 return false;  /* uncapped user data */
 174 
 175         return true;  /* not actually user data */
 176 }
 177 
 178 bool user_rl_treat_untagged(struct expression *expr)
 179 {
 180         struct smatch_state *state;
 181         struct range_list *rl;
 182         sval_t sval;
 183 
 184         expr = strip_expr(expr);
 185         if (!expr)
 186                 return false;
 187         if (get_value(expr, &sval))
 188                 return true;
 189 
 190         state = get_state_expr(my_id, expr);
 191         if (state)
 192                 return estate_treat_untagged(state);
 193 
 194         if (get_user_rl(expr, &rl))
 195                 return false;  /* uncapped user data */
 196 
 197         return true;  /* not actually user data */
 198 }
 199 
 200 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
 201 {
 202         struct expression *edge_member;
 203         struct symbol *base = get_real_base_type(member);
 204         struct symbol *tmp;
 205 
 206         if (member->ident)
 207                 expr = member_expression(expr, '.', member->ident);
 208 
 209         FOR_EACH_PTR(base->symbol_list, tmp) {
 210                 struct symbol *type;
 211 
 212                 type = get_real_base_type(tmp);
 213                 if (!type)
 214                         continue;
 215 
 216                 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
 217                         tag_inner_struct_members(expr, tmp);
 218                         continue;
 219                 }


 545         if (right_type == left_type)
 546                 return 0;
 547 
 548         if (!points_to_user_data(right))
 549                 return 0;
 550 
 551         tag_as_user_data(expr->left);
 552         return 1;
 553 }
 554 
 555 static int handle_get_user(struct expression *expr)
 556 {
 557         char *name;
 558         int ret = 0;
 559 
 560         name = get_macro_name(expr->pos);
 561         if (!name || strcmp(name, "get_user") != 0)
 562                 return 0;
 563 
 564         name = expr_to_var(expr->right);
 565         if (!name || (strcmp(name, "__val_gu") != 0 && strcmp(name, "__gu_val") != 0))
 566                 goto free;
 567         set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
 568         ret = 1;
 569 free:
 570         free_string(name);
 571         return ret;
 572 }
 573 
 574 static bool handle_op_assign(struct expression *expr)
 575 {
 576         struct expression *binop_expr;
 577         struct smatch_state *state;
 578         struct range_list *rl;
 579 
 580         switch (expr->op) {
 581         case SPECIAL_ADD_ASSIGN:
 582         case SPECIAL_SUB_ASSIGN:
 583         case SPECIAL_AND_ASSIGN:
 584         case SPECIAL_MOD_ASSIGN:
 585         case SPECIAL_SHL_ASSIGN:
 586         case SPECIAL_SHR_ASSIGN:
 587         case SPECIAL_OR_ASSIGN:
 588         case SPECIAL_XOR_ASSIGN:
 589         case SPECIAL_MUL_ASSIGN:
 590         case SPECIAL_DIV_ASSIGN:
 591                 binop_expr = binop_expression(expr->left,
 592                                               op_remove_assign(expr->op),
 593                                               expr->right);
 594                 if (!get_user_rl(binop_expr, &rl))
 595                         return true;
 596 
 597                 rl = cast_rl(get_type(expr->left), rl);
 598                 state = alloc_estate_rl(rl);
 599                 if (user_rl_capped(binop_expr))
 600                         estate_set_capped(state);
 601                 if (user_rl_treat_untagged(expr->left))
 602                         estate_set_treat_untagged(state);
 603                 set_state_expr(my_id, expr->left, state);
 604                 return true;
 605         }
 606         return false;
 607 }
 608 
 609 static void match_assign(struct expression *expr)
 610 {
 611         struct range_list *rl;
 612         static struct expression *handled;
 613         struct smatch_state *state;
 614         struct expression *faked;
 615 
 616         faked = get_faked_expression();
 617         if (faked && faked == handled)
 618                 return;
 619         if (is_fake_call(expr->right))
 620                 goto clear_old_state;
 621         if (handle_get_user(expr))
 622                 return;


 624                 handled = expr;
 625                 set_points_to_user_data(expr->left);
 626         }
 627         if (handle_struct_assignment(expr))
 628                 return;
 629 
 630         if (handle_op_assign(expr))
 631                 return;
 632         if (expr->op != '=')
 633                 goto clear_old_state;
 634 
 635         /* Handled by DB code */
 636         if (expr->right->type == EXPR_CALL || __in_fake_parameter_assign)
 637                 return;
 638 
 639         if (!get_user_rl(expr->right, &rl))
 640                 goto clear_old_state;
 641 
 642         rl = cast_rl(get_type(expr->left), rl);
 643         state = alloc_estate_rl(rl);
 644 
 645         if (user_rl_capped(expr->right))
 646                 estate_set_capped(state);
 647         if (user_rl_treat_untagged(expr->right))
 648                 estate_set_treat_untagged(state);
 649         set_state_expr(my_id, expr->left, state);
 650 
 651         return;
 652 
 653 clear_old_state:
 654         if (get_state_expr(my_id, expr->left))
 655                 set_state_expr(my_id, expr->left, alloc_estate_empty());
 656 }
 657 
 658 static void handle_eq_noteq(struct expression *expr)
 659 {
 660         struct smatch_state *left_orig, *right_orig;
 661 
 662         left_orig = get_state_expr(my_id, expr->left);
 663         right_orig = get_state_expr(my_id, expr->right);
 664 
 665         if (!left_orig && !right_orig)
 666                 return;
 667         if (left_orig && right_orig)
 668                 return;
 669 
 670         if (left_orig) {
 671                 set_true_false_states_expr(my_id, expr->left,
 672                                 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
 673                                 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
 674         } else {
 675                 set_true_false_states_expr(my_id, expr->right,
 676                                 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
 677                                 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
 678         }
 679 }
 680 
 681 static struct range_list *strip_negatives(struct range_list *rl)
 682 {
 683         sval_t min = rl_min(rl);
 684         sval_t minus_one = { .type = rl_type(rl), .value = -1 };
 685         sval_t over = { .type = rl_type(rl), .value = INT_MAX + 1ULL };
 686         sval_t max = sval_type_max(rl_type(rl));
 687 





 688         if (!rl)
 689                 return NULL;
 690 
 691         if (type_unsigned(rl_type(rl)) && type_bits(rl_type(rl)) > 31)
 692                 return remove_range(rl, over, max);
 693 
 694         return remove_range(rl, min, minus_one);
 695 }
 696 
 697 static void handle_compare(struct expression *expr)
 698 {
 699         struct expression  *left, *right;
 700         struct range_list *left_rl = NULL;
 701         struct range_list *right_rl = NULL;
 702         struct range_list *user_rl;
 703         struct smatch_state *capped_state;
 704         struct smatch_state *left_true = NULL;
 705         struct smatch_state *left_false = NULL;
 706         struct smatch_state *right_true = NULL;
 707         struct smatch_state *right_false = NULL;


1019 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
1020 {
1021         struct smatch_state *state;
1022 
1023         state = get_state(my_id, name, sym);
1024         if (state && estate_rl(state)) {
1025                 *rl = estate_rl(state);
1026                 return 1;
1027         }
1028         return 0;
1029 }
1030 
1031 static char *get_user_rl_str(struct expression *expr, struct symbol *type)
1032 {
1033         struct range_list *rl;
1034         static char buf[64];
1035 
1036         if (!get_user_rl(expr, &rl))
1037                 return NULL;
1038         rl = cast_rl(type, rl);
1039         snprintf(buf, sizeof(buf), "%s%s%s",
1040                  show_rl(rl),
1041                  user_rl_capped(expr) ? "[c]" : "",
1042                  user_rl_treat_untagged(expr) ? "[u]" : "");
1043         return buf;
1044 }
1045 
1046 static void match_call_info(struct expression *expr)
1047 {
1048         struct expression *arg;
1049         struct symbol *type;
1050         char *str;
1051         int i;
1052 
1053         i = -1;
1054         FOR_EACH_PTR(expr->args, arg) {
1055                 i++;
1056                 type = get_arg_type(expr->fn, i);
1057                 str = get_user_rl_str(arg, type);
1058                 if (!str)
1059                         continue;
1060 
1061                 sql_insert_caller_info(expr, USER_DATA, i, "$", str);
1062         } END_FOR_EACH_PTR(arg);


1094         type = get_arg_type(call->fn, param);
1095         if (type && type_bits(type) < type_bits(&ptr_ctype))
1096                 return;
1097 
1098         if (strcmp(sm->state->name, "") == 0)
1099                 return;
1100 
1101         if (strcmp(printed_name, "*$") == 0 &&
1102             is_struct_ptr(sm->sym))
1103                 return;
1104 
1105         state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
1106         if (!state || !estate_rl(state))
1107                 rl = estate_rl(sm->state);
1108         else
1109                 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
1110 
1111         if (!rl)
1112                 return;
1113 
1114         snprintf(buf, sizeof(buf), "%s%s%s", show_rl(rl),
1115                  estate_capped(sm->state) ? "[c]" : "",
1116                  estate_treat_untagged(sm->state) ? "[u]" : "");
1117         sql_insert_caller_info(call, USER_DATA, param, printed_name, buf);
1118 }
1119 
1120 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1121 {
1122         struct expression *arg;
1123         char *name;
1124         struct symbol *sym;
1125         struct smatch_state *state;
1126 
1127         while (expr->type == EXPR_ASSIGNMENT)
1128                 expr = strip_expr(expr->right);
1129         if (expr->type != EXPR_CALL)
1130                 return;
1131 
1132         arg = get_argument_from_call_expr(expr->args, param);
1133         if (!arg)
1134                 return;
1135         name = get_variable_from_key(arg, key, &sym);
1136         if (!name || !sym)
1137                 goto free;
1138 
1139         state = get_state(my_id, name, sym);
1140         if (!state)
1141                 goto free;
1142 
1143         set_state(my_id, name, sym, alloc_estate_empty());
1144 free:
1145         free_string(name);
1146 }
1147 
1148 static bool param_data_capped(const char *value)
1149 {
1150         if (strstr(value, ",c") || strstr(value, "[c"))
1151                 return true;
1152         return false;
1153 }
1154 
1155 static bool param_data_treat_untagged(const char *value)
1156 {
1157         if (strstr(value, ",u") || strstr(value, "[u"))
1158                 return true;
1159         return false;
1160 }
1161 
1162 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
1163 {
1164         struct range_list *rl = NULL;
1165         struct smatch_state *state;
1166         struct expression *expr;
1167         struct symbol *type;
1168         char fullname[256];
1169         char *key_orig = key;
1170         bool add_star = false;
1171 
1172         if (strcmp(key, "**$") == 0) {
1173                 snprintf(fullname, sizeof(fullname), "**%s", name);
1174         } else {
1175                 if (key[0] == '*') {
1176                         add_star = true;
1177                         key++;
1178                 }
1179 
1180                 snprintf(fullname, 256, "%s%s%s", add_star ? "*" : "", name, key + 1);
1181         }


1189          * as user data or we could pass foo->bar, foo->baz as user data.
1190          * The second option is easier to implement so we do that.
1191          *
1192          */
1193         if (strcmp(key_orig, "*$") == 0) {
1194                 struct symbol *tmp = type;
1195 
1196                 while (tmp && tmp->type == SYM_PTR)
1197                         tmp = get_real_base_type(tmp);
1198 
1199                 if (tmp && (tmp->type == SYM_STRUCT || tmp->type == SYM_UNION)) {
1200                         tag_as_user_data(symbol_expression(sym));
1201                         return;
1202                 }
1203         }
1204 
1205         str_to_rl(type, value, &rl);
1206         state = alloc_estate_rl(rl);
1207         if (param_data_capped(value) || is_capped(expr))
1208                 estate_set_capped(state);
1209         if (param_data_treat_untagged(value) || sym->ctype.as == 5)
1210                 estate_set_treat_untagged(state);
1211         set_state(my_id, fullname, sym, state);
1212 }
1213 
1214 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
1215 {
1216         set_state(my_call_id, "this_function", NULL, &called);
1217 }
1218 
1219 static void match_syscall_definition(struct symbol *sym)
1220 {
1221         struct symbol *arg;
1222         char *macro;
1223         char *name;
1224         int is_syscall = 0;
1225 
1226         macro = get_macro_name(sym->pos);
1227         if (macro &&
1228             (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
1229              strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
1230                 is_syscall = 1;


1263 }
1264 
1265 static void set_to_user_data(struct expression *expr, char *key, char *value)
1266 {
1267         struct smatch_state *state;
1268         char *name;
1269         struct symbol *sym;
1270         struct symbol *type;
1271         struct range_list *rl = NULL;
1272 
1273         type = get_member_type_from_key(expr, key);
1274         name = get_variable_from_key(expr, key, &sym);
1275         if (!name || !sym)
1276                 goto free;
1277 
1278         call_results_to_rl(expr, type, value, &rl);
1279 
1280         state = alloc_estate_rl(rl);
1281         if (param_data_capped(value))
1282                 estate_set_capped(state);
1283         if (param_data_treat_untagged(value))
1284                 estate_set_treat_untagged(state);
1285         set_state(my_id, name, sym, state);
1286 free:
1287         free_string(name);
1288 }
1289 
1290 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
1291 {
1292         struct expression *arg;
1293         struct expression *call;
1294 
1295         call = expr;
1296         while (call->type == EXPR_ASSIGNMENT)
1297                 call = strip_expr(call->right);
1298         if (call->type != EXPR_CALL)
1299                 return;
1300 
1301         if (!we_pass_user_data(call))
1302                 return;
1303 
1304         if (param == -1) {


1372                 if (!param_was_set_var_sym(sm->name, sm->sym))
1373                         continue;
1374 
1375                 /* The logic here was that if we were passed in a user data then
1376                  * we don't record that.  It's like the difference between
1377                  * param_filter and param_set.  When I think about it, I'm not
1378                  * sure it actually works.  It's probably harmless because we
1379                  * checked earlier that we're not returning a parameter...
1380                  * Let's mark this as a TODO.
1381                  */
1382                 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
1383                 if (start_state && rl_equiv(estate_rl(sm->state), estate_rl(start_state)))
1384                         continue;
1385 
1386                 param_name = get_param_name(sm);
1387                 if (!param_name)
1388                         continue;
1389                 if (strcmp(param_name, "$") == 0)  /* The -1 param is handled after the loop */
1390                         continue;
1391 
1392                 snprintf(buf, sizeof(buf), "%s%s%s",
1393                          show_rl(estate_rl(sm->state)),
1394                          estate_capped(sm->state) ? "[c]" : "",
1395                          estate_treat_untagged(sm->state) ? "[u]" : "");
1396                 sql_insert_return_states(return_id, return_ranges,
1397                                          func_gets_user_data ? USER_DATA_SET : USER_DATA,
1398                                          param, param_name, buf);
1399         } END_FOR_EACH_SM(sm);
1400 
1401         /* This if for "return foo;" where "foo->bar" is user data. */
1402         FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1403                 if (!ret_sym)
1404                         break;
1405                 if (ret_sym != sm->sym)
1406                         continue;
1407 
1408                 param_name = state_name_to_param_name(sm->name, return_str);
1409                 if (!param_name)
1410                         continue;
1411                 if (strcmp(param_name, "$") == 0)
1412                         return_found = true;
1413                 if (strcmp(param_name, "*$") == 0)
1414                         pointed_at_found = true;
1415                 snprintf(buf, sizeof(buf), "%s%s%s",
1416                          show_rl(estate_rl(sm->state)),
1417                          estate_capped(sm->state) ? "[c]" : "",
1418                          estate_treat_untagged(sm->state) ? "[u]" : "");
1419                 sql_insert_return_states(return_id, return_ranges,
1420                                          func_gets_user_data ? USER_DATA_SET : USER_DATA,
1421                                          -1, param_name, buf);
1422         } END_FOR_EACH_SM(sm);
1423 
1424         /* This if for "return ntohl(foo);" */
1425         if (!return_found && get_user_rl(expr, &rl)) {
1426                 snprintf(buf, sizeof(buf), "%s%s%s",
1427                          show_rl(rl),
1428                          user_rl_capped(expr) ? "[c]" : "",
1429                          user_rl_treat_untagged(expr) ? "[u]" : "");
1430                 sql_insert_return_states(return_id, return_ranges,
1431                                          func_gets_user_data ? USER_DATA_SET : USER_DATA,
1432                                          -1, "$", buf);
1433         }
1434 
1435         /*
1436          * This is to handle things like return skb->data where we don't set a
1437          * state for that.
1438          */
1439         if (!pointed_at_found && points_to_user_data(expr)) {
1440                 sql_insert_return_states(return_id, return_ranges,
1441                                          (is_skb_data(expr) || func_gets_user_data) ?
1442                                          USER_DATA_SET : USER_DATA,
1443                                          -1, "*$", "s64min-s64max");
1444         }
1445 
1446         free_string(return_str);
1447 }
1448 
1449 static void returns_param_capped(struct expression *expr, int param, char *key, char *value)


1529         select_return_states_hook(PARAM_SET, &db_param_set);
1530         add_hook(&match_condition, CONDITION_HOOK);
1531 
1532         add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1533         add_member_info_callback(my_id, struct_member_callback);
1534         select_caller_info_hook(set_param_user_data, USER_DATA);
1535         select_return_states_hook(USER_DATA, &returns_param_user_data);
1536         select_return_states_hook(USER_DATA_SET, &returns_param_user_data_set);
1537         select_return_states_hook(CAPPED_DATA, &returns_param_capped);
1538         add_split_return_callback(&param_set_to_user_data);
1539 }
1540 
1541 void register_kernel_user_data2(int id)
1542 {
1543         my_call_id = id;
1544 
1545         if (option_project != PROJ_KERNEL)
1546                 return;
1547         select_caller_info_hook(set_called, INTERNAL);
1548 }
1549