capped: multiplications can be capped
[smatch.git] / check_user_data.c
blobdfe55e7701891930e667b90e33b42cff0958fd0f
1 /*
2 * Copyright (C) 2011 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * There are a couple checks that try to see if a variable
20 * comes from the user. It would be better to unify them
21 * into one place. Also it we should follow the data down
22 * the call paths. Hence this file.
25 #include "smatch.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_id;
31 STATE(called);
33 STATE(capped);
34 STATE(user_data_passed);
35 STATE(user_data_set);
37 static const char * kstr_funcs[] = {
38 "kstrtoull", "kstrtoll", "kstrtoul", "kstrtol", "kstrtouint",
39 "kstrtoint", "kstrtou64", "kstrtos64", "kstrtou32", "kstrtos32",
40 "kstrtou16", "kstrtos16", "kstrtou8", "kstrtos8", "kstrtoull_from_user"
41 "kstrtoll_from_user", "kstrtoul_from_user", "kstrtol_from_user",
42 "kstrtouint_from_user", "kstrtoint_from_user", "kstrtou16_from_user",
43 "kstrtos16_from_user", "kstrtou8_from_user", "kstrtos8_from_user",
44 "kstrtou64_from_user", "kstrtos64_from_user", "kstrtou32_from_user",
45 "kstrtos32_from_user",
48 enum {
49 SET_DATA = 1,
50 PASSED_DATA = 2,
53 int is_user_macro(struct expression *expr)
55 char *macro;
56 struct range_list *rl;
58 macro = get_macro_name(expr->pos);
60 if (!macro)
61 return 0;
62 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl))
63 return 0;
64 if (strcmp(macro, "ntohl") == 0)
65 return SET_DATA;
66 if (strcmp(macro, "ntohs") == 0)
67 return SET_DATA;
68 return 0;
71 static int has_user_data_state(struct expression *expr)
73 struct stree *stree;
74 struct sm_state *sm;
75 struct symbol *sym;
76 char *name;
78 expr = strip_expr(expr);
79 if (expr->type == EXPR_PREOP && expr->op == '&')
80 expr = strip_expr(expr->unop);
82 name = expr_to_str_sym(expr, &sym);
83 free_string(name);
84 if (!sym)
85 return 1;
87 stree = __get_cur_stree();
88 FOR_EACH_MY_SM(my_id, stree, sm) {
89 if (sm->sym == sym)
90 return 1;
91 } END_FOR_EACH_SM(sm);
92 return 0;
95 static int passes_user_data(struct expression *expr)
97 struct expression *arg;
99 FOR_EACH_PTR(expr->args, arg) {
100 if (is_user_data(arg))
101 return 1;
102 if (has_user_data_state(arg))
103 return 1;
104 } END_FOR_EACH_PTR(arg);
106 return 0;
109 static struct expression *db_expr;
110 static int db_user_data;
111 static int db_user_data_callback(void *unused, int argc, char **argv, char **azColName)
113 if (atoi(argv[0]) == PASSED_DATA && !passes_user_data(db_expr))
114 return 0;
115 db_user_data = 1;
116 return 0;
119 static int is_user_fn_db(struct expression *expr)
121 struct symbol *sym;
122 static char sql_filter[1024];
124 if (is_fake_call(expr))
125 return 0;
126 if (expr->fn->type != EXPR_SYMBOL)
127 return 0;
128 sym = expr->fn->symbol;
129 if (!sym)
130 return 0;
132 if (sym->ctype.modifiers & MOD_STATIC) {
133 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
134 get_filename(), sym->ident->name);
135 } else {
136 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
137 sym->ident->name);
140 db_expr = expr;
141 db_user_data = 0;
142 run_sql(db_user_data_callback, NULL,
143 "select value from return_states where type=%d and parameter = -1 and key = '$' and %s",
144 USER_DATA, sql_filter);
145 return db_user_data;
148 static int is_user_function(struct expression *expr)
150 if (expr->type != EXPR_CALL)
151 return 0;
152 return is_user_fn_db(expr);
155 static int is_skb_data(struct expression *expr)
157 struct symbol *sym;
158 char *name;
159 int len;
160 int ret = 0;
162 name = expr_to_var_sym(expr, &sym);
163 if (!name || !sym)
164 goto free;
166 sym = get_base_type(sym);
167 if (!sym || sym->type != SYM_PTR)
168 goto free;
169 sym = get_base_type(sym);
170 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
171 goto free;
172 if (strcmp(sym->ident->name, "sk_buff") != 0)
173 goto free;
175 len = strlen(name);
176 if (len < 6)
177 goto free;
178 if (strcmp(name + len - 6, "->data") == 0)
179 ret = SET_DATA;
181 free:
182 free_string(name);
183 return ret;
186 static int in_container_of_macro(struct expression *expr)
188 char *macro;
190 macro = get_macro_name(expr->pos);
192 if (!macro)
193 return 0;
194 if (strcmp(macro, "container_of") == 0)
195 return 1;
196 return 0;
199 static int is_user_data_state(struct expression *expr)
201 struct stree *stree = NULL;
202 struct sm_state *tmp;
203 struct symbol *sym;
204 char *name;
205 int user = 0;
207 tmp = get_sm_state_expr(my_id, expr);
208 if (tmp) {
209 if (slist_has_state(tmp->possible, &user_data_set))
210 return SET_DATA;
211 if (slist_has_state(tmp->possible, &user_data_passed))
212 return PASSED_DATA;
213 return 0;
216 name = expr_to_str_sym(expr, &sym);
217 if (!name || !sym)
218 goto free;
220 stree = __get_cur_stree();
221 FOR_EACH_MY_SM(my_id, stree, tmp) {
222 if (tmp->sym != sym)
223 continue;
224 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
225 if (slist_has_state(tmp->possible, &user_data_set))
226 user = SET_DATA;
227 else if (slist_has_state(tmp->possible, &user_data_passed))
228 user = PASSED_DATA;
229 goto free;
231 } END_FOR_EACH_SM(tmp);
233 free:
234 free_string(name);
235 return user;
238 int is_user_data(struct expression *expr)
240 int user_data;
242 if (!expr)
243 return 0;
245 if (is_capped(expr))
246 return 0;
247 if (in_container_of_macro(expr))
248 return 0;
250 user_data = is_user_macro(expr);
251 if (user_data)
252 return user_data;
253 user_data = is_user_function(expr);
254 if (user_data)
255 return user_data;
256 user_data = is_skb_data(expr);
257 if (user_data)
258 return user_data;
260 expr = strip_expr(expr); /* this has to come after is_user_macro() */
262 if (expr->type == EXPR_BINOP) {
263 user_data = is_user_data(expr->left);
264 if (user_data)
265 return user_data;
266 if (is_array(expr))
267 return 0;
268 user_data = is_user_data(expr->right);
269 if (user_data)
270 return user_data;
271 return 0;
273 if (expr->type == EXPR_PREOP && (expr->op == '&' || expr->op == '*'))
274 expr = strip_expr(expr->unop);
276 return is_user_data_state(expr);
279 int implied_user_data(struct expression *expr, struct range_list **rl)
281 if (!is_user_data(expr))
282 return 0;
283 get_absolute_rl(expr, rl);
284 return 1;
287 int is_capped_user_data(struct expression *expr)
289 struct sm_state *sm;
291 sm = get_sm_state_expr(my_id, expr);
292 if (!sm)
293 return 0;
294 if (slist_has_state(sm->possible, &capped))
295 return 1;
296 return 0;
299 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
301 set_state(my_id, "this_function", NULL, &called);
304 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
306 char fullname[256];
308 /* sanity check. this should always be true. */
309 if (strncmp(key, "$", 1) != 0)
310 return;
311 snprintf(fullname, 256, "%s%s", name, key + 1);
312 set_state(my_id, fullname, sym, &user_data_passed);
315 static void match_syscall_definition(struct symbol *sym)
317 struct symbol *arg;
318 char *macro;
319 char *name;
320 int is_syscall = 0;
322 macro = get_macro_name(sym->pos);
323 if (macro &&
324 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
325 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
326 is_syscall = 1;
328 name = get_function();
329 if (!option_no_db && get_state(my_id, "this_function", NULL) != &called) {
330 if (name && strncmp(name, "sys_", 4) == 0)
331 is_syscall = 1;
334 if (name && strncmp(name, "compat_sys_", 11) == 0)
335 is_syscall = 1;
337 if (!is_syscall)
338 return;
340 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
341 set_state(my_id, arg->ident->name, arg, &user_data_set);
342 } END_FOR_EACH_PTR(arg);
345 static void match_condition(struct expression *expr)
347 switch (expr->op) {
348 case '<':
349 case SPECIAL_LTE:
350 case SPECIAL_UNSIGNED_LT:
351 case SPECIAL_UNSIGNED_LTE:
352 if (is_user_data(expr->left))
353 set_true_false_states_expr(my_id, expr->left, &capped, NULL);
354 if (is_user_data(expr->right))
355 set_true_false_states_expr(my_id, expr->right, NULL, &capped);
356 break;
357 case '>':
358 case SPECIAL_GTE:
359 case SPECIAL_UNSIGNED_GT:
360 case SPECIAL_UNSIGNED_GTE:
361 if (is_user_data(expr->right))
362 set_true_false_states_expr(my_id, expr->right, &capped, NULL);
363 if (is_user_data(expr->left))
364 set_true_false_states_expr(my_id, expr->left, NULL, &capped);
365 break;
366 case SPECIAL_EQUAL:
367 if (is_user_data(expr->left))
368 set_true_false_states_expr(my_id, expr->left, &capped, NULL);
369 if (is_user_data(expr->right))
370 set_true_false_states_expr(my_id, expr->right, &capped, NULL);
371 break;
372 case SPECIAL_NOTEQUAL:
373 if (is_user_data(expr->left))
374 set_true_false_states_expr(my_id, expr->left, NULL, &capped);
375 if (is_user_data(expr->right))
376 set_true_false_states_expr(my_id, expr->right, NULL, &capped);
377 break;
378 default:
379 return;
383 static int handle_get_user(struct expression *expr)
385 char *name;
386 int ret = 0;
388 name = get_macro_name(expr->pos);
389 if (!name || strcmp(name, "get_user") != 0)
390 return 0;
392 name = expr_to_var(expr->right);
393 if (!name || strcmp(name, "__val_gu") != 0)
394 goto free;
395 set_state_expr(my_id, expr->left, &user_data_set);
396 ret = 1;
397 free:
398 free_string(name);
399 return ret;
402 static void match_assign(struct expression *expr)
404 int user_data;
406 if (handle_get_user(expr))
407 return;
409 user_data = is_user_data(expr->right);
410 if (user_data == PASSED_DATA)
411 set_state_expr(my_id, expr->left, &user_data_passed);
412 else if (user_data == SET_DATA)
413 set_state_expr(my_id, expr->left, &user_data_set);
414 else if (get_state_expr(my_id, expr->left))
415 set_state_expr(my_id, expr->left, &capped);
418 static void tag_struct_members(struct symbol *type, struct expression *expr)
420 struct symbol *tmp;
421 struct expression *member;
422 int op = '*';
424 if (expr->type == EXPR_PREOP && expr->op == '&') {
425 expr = strip_expr(expr->unop);
426 op = '.';
429 FOR_EACH_PTR(type->symbol_list, tmp) {
430 if (!tmp->ident)
431 continue;
432 member = member_expression(expr, op, tmp->ident);
433 set_state_expr(my_id, member, &user_data_set);
434 } END_FOR_EACH_PTR(tmp);
437 static void tag_base_type(struct expression *expr)
439 if (expr->type == EXPR_PREOP && expr->op == '&')
440 expr = strip_expr(expr->unop);
441 else
442 expr = deref_expression(expr);
443 set_state_expr(my_id, expr, &user_data_set);
446 static void tag_as_user_data(struct expression *expr)
448 struct symbol *type;
450 expr = strip_expr(expr);
452 type = get_type(expr);
453 if (!type || type->type != SYM_PTR)
454 return;
455 type = get_real_base_type(type);
456 if (!type)
457 return;
458 if (type == &void_ctype) {
459 set_state_expr(my_id, deref_expression(expr), &user_data_set);
460 return;
462 if (type->type == SYM_BASETYPE)
463 tag_base_type(expr);
464 if (type->type == SYM_STRUCT) {
465 if (expr->type != EXPR_PREOP || expr->op != '&')
466 expr = deref_expression(expr);
467 tag_struct_members(type, expr);
471 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
473 int param = PTR_INT(_param);
474 struct expression *dest;
476 dest = get_argument_from_call_expr(expr->args, param);
477 dest = strip_expr(dest);
478 if (!dest)
479 return;
480 tag_as_user_data(dest);
483 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
485 set_state_expr(my_id, expr->left, &user_data_set);
488 static void match_caller_info(struct expression *expr)
490 struct expression *tmp;
491 int i;
493 i = 0;
494 FOR_EACH_PTR(expr->args, tmp) {
495 if (is_user_data(tmp))
496 sql_insert_caller_info(expr, USER_DATA, i, "$", "");
497 i++;
498 } END_FOR_EACH_PTR(tmp);
501 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
503 if (sm->state == &capped)
504 return;
505 sql_insert_caller_info(call, USER_DATA, param, printed_name, "");
508 static void returned_member_callback(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state)
510 if (state == &capped)
511 return;
512 sql_insert_return_states(return_id, return_ranges, USER_DATA, -1, printed_name, "");
515 static void print_returned_user_data(int return_id, char *return_ranges, struct expression *expr)
517 struct stree *stree;
518 struct sm_state *tmp;
519 int param;
520 int user_data;
521 const char *passed_or_new;
523 user_data = is_user_data(expr);
524 if (user_data == PASSED_DATA) {
525 sql_insert_return_states(return_id, return_ranges, USER_DATA,
526 -1, "$", "2");
528 if (user_data == SET_DATA) {
529 sql_insert_return_states(return_id, return_ranges, USER_DATA,
530 -1, "$", "1");
533 stree = __get_cur_stree();
535 FOR_EACH_MY_SM(my_id, stree, tmp) {
536 const char *param_name;
538 param = get_param_num_from_sym(tmp->sym);
539 if (param < 0)
540 continue;
542 if (is_capped_var_sym(tmp->name, tmp->sym))
543 continue;
544 /* ignore states that were already USER_DATA to begin with */
545 if (get_state_stree(get_start_states(), my_id, tmp->name, tmp->sym))
546 continue;
548 param_name = get_param_name(tmp);
549 if (!param_name || strcmp(param_name, "$") == 0)
550 continue;
552 if (slist_has_state(tmp->possible, &user_data_set))
553 passed_or_new = "1";
554 else if (slist_has_state(tmp->possible, &user_data_passed))
555 passed_or_new = "2";
556 else
557 continue;
559 sql_insert_return_states(return_id, return_ranges, USER_DATA,
560 param, param_name, passed_or_new);
561 } END_FOR_EACH_SM(tmp);
564 static void db_return_states_userdata(struct expression *expr, int param, char *key, char *value)
566 char *name;
567 struct symbol *sym;
569 if (expr->type == EXPR_ASSIGNMENT && param == -1 && strcmp(key, "*$") == 0) {
570 tag_as_user_data(expr->left);
571 return;
574 name = return_state_to_var_sym(expr, param, key, &sym);
575 if (!name || !sym)
576 goto free;
578 set_state(my_id, name, sym, &user_data_set);
579 free:
580 free_string(name);
583 void check_user_data(int id)
585 int i;
587 if (option_project != PROJ_KERNEL)
588 return;
589 my_id = id;
590 select_caller_info_hook(set_called, INTERNAL);
591 select_caller_info_hook(set_param_user_data, USER_DATA);
592 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
593 add_hook(&match_condition, CONDITION_HOOK);
594 add_hook(&match_assign, ASSIGNMENT_HOOK);
595 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
596 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
597 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
598 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
599 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
600 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
601 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
603 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
604 add_member_info_callback(my_id, struct_member_callback);
605 add_returned_member_callback(my_id, returned_member_callback);
606 add_split_return_callback(print_returned_user_data);
607 select_return_states_hook(USER_DATA, &db_return_states_userdata);