untracked_param: functions that are too hairy are untrackable
[smatch.git] / check_user_data2.c
blobab98162ad4ef3a7974853a34b2b853bf9f8b89b6
1 /*
2 * Copyright (C) 2011 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * There are a couple checks that try to see if a variable
20 * comes from the user. It would be better to unify them
21 * into one place. Also it we should follow the data down
22 * the call paths. Hence this file.
25 #include "smatch.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_id;
30 static int my_call_id;
32 STATE(called);
34 static const char * kstr_funcs[] = {
35 "kstrtoull", "kstrtoll", "kstrtoul", "kstrtol", "kstrtouint",
36 "kstrtoint", "kstrtou64", "kstrtos64", "kstrtou32", "kstrtos32",
37 "kstrtou16", "kstrtos16", "kstrtou8", "kstrtos8", "kstrtoull_from_user"
38 "kstrtoll_from_user", "kstrtoul_from_user", "kstrtol_from_user",
39 "kstrtouint_from_user", "kstrtoint_from_user", "kstrtou16_from_user",
40 "kstrtos16_from_user", "kstrtou8_from_user", "kstrtos8_from_user",
41 "kstrtou64_from_user", "kstrtos64_from_user", "kstrtou32_from_user",
42 "kstrtos32_from_user",
45 static void set_points_to_user_data(struct expression *expr);
47 static struct stree *start_states;
48 static struct stree_stack *saved_stack;
49 static void save_start_states(struct statement *stmt)
51 start_states = clone_stree(__get_cur_stree());
54 static void free_start_states(void)
56 free_stree(&start_states);
59 static void match_save_states(struct expression *expr)
61 push_stree(&saved_stack, start_states);
62 start_states = NULL;
65 static void match_restore_states(struct expression *expr)
67 free_stree(&start_states);
68 start_states = pop_stree(&saved_stack);
71 static struct smatch_state *empty_state(struct sm_state *sm)
73 return alloc_estate_empty();
76 static void pre_merge_hook(struct sm_state *sm)
78 struct smatch_state *user;
79 struct smatch_state *extra;
80 struct range_list *rl;
82 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
83 if (!extra || !estate_rl(extra))
84 return;
85 user = get_state(my_id, sm->name, sm->sym);
86 if (!user || !estate_rl(user))
87 return;
88 rl = rl_intersection(estate_rl(user), estate_rl(extra));
89 set_state(my_id, sm->name, sm->sym, alloc_estate_rl(clone_rl(rl)));
92 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
94 struct expression *edge_member;
95 struct symbol *base = get_real_base_type(member);
96 struct symbol *tmp;
98 if (member->ident)
99 expr = member_expression(expr, '.', member->ident);
101 FOR_EACH_PTR(base->symbol_list, tmp) {
102 struct symbol *type;
104 type = get_real_base_type(tmp);
105 if (!type)
106 continue;
108 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
109 tag_inner_struct_members(expr, tmp);
110 continue;
113 if (!tmp->ident)
114 continue;
116 edge_member = member_expression(expr, '.', tmp->ident);
117 set_state_expr(my_id, edge_member, alloc_estate_whole(type));
118 } END_FOR_EACH_PTR(tmp);
121 static void tag_struct_members(struct symbol *type, struct expression *expr)
123 struct symbol *tmp;
124 struct expression *member;
125 int op = '*';
127 if (expr->type == EXPR_PREOP && expr->op == '&') {
128 expr = strip_expr(expr->unop);
129 op = '.';
132 FOR_EACH_PTR(type->symbol_list, tmp) {
133 type = get_real_base_type(tmp);
134 if (!type)
135 continue;
137 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
138 tag_inner_struct_members(expr, tmp);
139 continue;
142 if (!tmp->ident)
143 continue;
145 member = member_expression(expr, op, tmp->ident);
146 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
148 if (type->type == SYM_ARRAY)
149 set_points_to_user_data(member);
150 } END_FOR_EACH_PTR(tmp);
153 static void tag_base_type(struct expression *expr)
155 if (expr->type == EXPR_PREOP && expr->op == '&')
156 expr = strip_expr(expr->unop);
157 else
158 expr = deref_expression(expr);
159 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
162 static void tag_as_user_data(struct expression *expr)
164 struct symbol *type;
166 expr = strip_expr(expr);
168 type = get_type(expr);
169 if (!type || type->type != SYM_PTR)
170 return;
171 type = get_real_base_type(type);
172 if (!type)
173 return;
174 if (type == &void_ctype) {
175 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
176 return;
178 if (type->type == SYM_BASETYPE)
179 tag_base_type(expr);
180 if (type->type == SYM_STRUCT) {
181 if (expr->type != EXPR_PREOP || expr->op != '&')
182 expr = deref_expression(expr);
183 else
184 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
185 tag_struct_members(type, expr);
189 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
191 int param = PTR_INT(_param);
192 struct expression *dest;
194 dest = get_argument_from_call_expr(expr->args, param);
195 dest = strip_expr(dest);
196 if (!dest)
197 return;
198 tag_as_user_data(dest);
201 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
203 struct expression *arg;
204 int i;
206 i = -1;
207 FOR_EACH_PTR(expr->args, arg) {
208 i++;
209 if (i < 2)
210 continue;
211 tag_as_user_data(arg);
212 } END_FOR_EACH_PTR(arg);
215 static int points_to_user_data(struct expression *expr)
217 struct smatch_state *state;
218 char buf[256];
219 struct symbol *sym;
220 char *name;
221 int ret = 0;
223 expr = strip_expr(expr);
225 if (expr->type == EXPR_BINOP && expr->op == '+') {
226 if (points_to_user_data(expr->left))
227 return 1;
228 if (points_to_user_data(expr->right))
229 return 1;
230 return 0;
233 name = expr_to_var_sym(expr, &sym);
234 if (!name || !sym)
235 goto free;
236 snprintf(buf, sizeof(buf), "*%s", name);
237 state = get_state(my_id, buf, sym);
238 if (state && estate_rl(state))
239 ret = 1;
240 free:
241 free_string(name);
242 return ret;
245 static void set_points_to_user_data(struct expression *expr)
247 char *name;
248 struct symbol *sym;
249 char buf[256];
251 name = expr_to_var_sym(expr, &sym);
252 if (!name || !sym)
253 goto free;
254 snprintf(buf, sizeof(buf), "*%s", name);
255 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
256 free:
257 free_string(name);
260 static int is_skb_data(struct expression *expr)
262 struct symbol *sym;
264 expr = strip_expr(expr);
265 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
266 return 0;
268 expr = strip_expr(expr->unop);
269 if (expr->type == EXPR_BINOP && expr->op == '+')
270 expr = strip_expr(expr->left);
272 if (expr->type != EXPR_DEREF)
273 return 0;
275 if (!expr->member)
276 return 0;
277 if (strcmp(expr->member->name, "data") != 0)
278 return 0;
280 sym = expr_to_sym(expr->deref);
281 if (!sym)
282 return 0;
283 sym = get_real_base_type(sym);
284 if (!sym || sym->type != SYM_PTR)
285 return 0;
286 sym = get_real_base_type(sym);
287 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
288 return 0;
289 if (strcmp(sym->ident->name, "sk_buff") != 0)
290 return 0;
292 return 1;
295 static int comes_from_skb_data(struct expression *expr)
297 expr = strip_expr(expr);
298 if (!expr)
299 return 0;
301 switch (expr->type) {
302 case EXPR_BINOP:
303 if (comes_from_skb_data(expr->left))
304 return 1;
305 if (comes_from_skb_data(expr->right))
306 return 1;
307 return 0;
308 case EXPR_PREOP:
309 if (is_skb_data(expr))
310 return 1;
311 return comes_from_skb_data(expr->deref);
312 default:
313 return 0;
318 static int handle_struct_assignment(struct expression *expr)
320 struct expression *right;
321 struct symbol *left_type, *right_type;
323 left_type = get_type(expr->left);
324 if (!left_type || left_type->type != SYM_PTR)
325 return 0;
326 left_type = get_real_base_type(left_type);
327 if (!left_type || left_type->type != SYM_STRUCT)
328 return 0;
331 * Ignore struct to struct assignments because for those we look at the
332 * individual members.
334 right = strip_expr(expr->right);
335 right_type = get_type(right);
336 if (!right_type || right_type->type != SYM_PTR)
337 return 0;
339 /* If we are assigning struct members then normally that is handled
340 * by fake assignments, however if we cast one struct to a different
341 * of struct then we handle that here.
343 right_type = get_real_base_type(right_type);
344 if (right_type == left_type)
345 return 0;
347 if (!points_to_user_data(right) && !is_skb_data(right))
348 return 0;
350 tag_as_user_data(expr->left);
351 return 1;
354 static int handle_get_user(struct expression *expr)
356 char *name;
357 int ret = 0;
359 name = get_macro_name(expr->pos);
360 if (!name || strcmp(name, "get_user") != 0)
361 return 0;
363 name = expr_to_var(expr->right);
364 if (!name || strcmp(name, "__val_gu") != 0)
365 goto free;
366 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
367 ret = 1;
368 free:
369 free_string(name);
370 return ret;
373 static void match_assign(struct expression *expr)
375 struct range_list *rl;
377 if (is_fake_call(expr->right))
378 return;
379 if (handle_get_user(expr))
380 return;
381 if (points_to_user_data(expr->right))
382 set_points_to_user_data(expr->left);
383 if (handle_struct_assignment(expr))
384 return;
386 if (expr->right->type == EXPR_CALL ||
387 !get_user_rl(expr->right, &rl))
388 goto clear_old_state;
390 rl = cast_rl(get_type(expr->left), rl);
391 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
393 return;
395 clear_old_state:
396 if (get_state_expr(my_id, expr->left))
397 set_state_expr(my_id, expr->left, alloc_estate_empty());
400 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
402 tag_as_user_data(expr->left);
403 set_points_to_user_data(expr->left);
406 static void match_simple_strtoul(const char *fn, struct expression *expr, void *unused)
408 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
411 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
413 char *macro;
415 if (!expr)
416 return 0;
417 macro = get_macro_name(expr->pos);
419 if (!macro)
420 return 0;
422 if (strcmp(macro, "ntohl") == 0) {
423 *rl = alloc_whole_rl(&uint_ctype);
424 return 1;
426 if (strcmp(macro, "ntohs") == 0) {
427 *rl = alloc_whole_rl(&ushort_ctype);
428 return 1;
430 return 0;
433 static int user_data_flag;
434 static struct range_list *var_user_rl(struct expression *expr)
436 struct smatch_state *state;
437 struct range_list *rl;
438 struct range_list *absolute_rl;
440 if (get_user_macro_rl(expr, &rl))
441 goto found;
443 if (comes_from_skb_data(expr)) {
444 rl = alloc_whole_rl(get_type(expr));
445 goto found;
448 state = get_state_expr(my_id, expr);
449 if (state && estate_rl(state)) {
450 rl = estate_rl(state);
451 goto found;
454 return NULL;
455 found:
456 user_data_flag = 1;
457 absolute_rl = var_to_absolute_rl(expr);
458 return clone_rl(rl_intersection(rl, absolute_rl));
461 int get_user_rl(struct expression *expr, struct range_list **rl)
464 user_data_flag = 0;
465 custom_get_absolute_rl(expr, &var_user_rl, rl);
466 if (!user_data_flag) {
467 *rl = NULL;
468 return 0;
470 return 1;
473 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
475 struct smatch_state *state;
477 state = get_state(my_id, name, sym);
478 if (state && estate_rl(state)) {
479 *rl = estate_rl(state);
480 return 1;
482 return 0;
485 static void match_call_info(struct expression *expr)
487 struct range_list *rl;
488 struct expression *arg;
489 int i = 0;
491 i = -1;
492 FOR_EACH_PTR(expr->args, arg) {
493 i++;
495 if (!get_user_rl(arg, &rl))
496 continue;
498 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
499 } END_FOR_EACH_PTR(arg);
502 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
504 struct smatch_state *state;
505 struct range_list *rl;
507 if (strcmp(sm->state->name, "") == 0)
508 return;
510 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
511 if (!state || !estate_rl(state))
512 rl = estate_rl(sm->state);
513 else
514 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
516 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
519 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
521 struct range_list *rl = NULL;
522 struct smatch_state *state;
523 struct symbol *type;
524 char fullname[256];
526 if (strcmp(key, "*$") == 0)
527 snprintf(fullname, sizeof(fullname), "*%s", name);
528 else if (strncmp(key, "$", 1) == 0)
529 snprintf(fullname, 256, "%s%s", name, key + 1);
530 else
531 return;
533 type = get_member_type_from_key(symbol_expression(sym), key);
535 /* if the caller passes a void pointer with user data */
536 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
537 struct expression *expr = symbol_expression(sym);
539 tag_as_user_data(expr);
540 set_points_to_user_data(expr);
541 return;
543 str_to_rl(type, value, &rl);
544 state = alloc_estate_rl(rl);
545 set_state(my_id, fullname, sym, state);
548 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
550 set_state(my_call_id, "this_function", NULL, &called);
553 static void match_syscall_definition(struct symbol *sym)
555 struct symbol *arg;
556 char *macro;
557 char *name;
558 int is_syscall = 0;
560 macro = get_macro_name(sym->pos);
561 if (macro &&
562 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
563 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
564 is_syscall = 1;
566 name = get_function();
567 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
568 if (name && strncmp(name, "sys_", 4) == 0)
569 is_syscall = 1;
572 if (name && strncmp(name, "compat_sys_", 11) == 0)
573 is_syscall = 1;
575 if (!is_syscall)
576 return;
578 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
579 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
580 } END_FOR_EACH_PTR(arg);
583 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
585 struct expression *arg;
586 char *name;
587 struct symbol *sym;
588 struct symbol *type;
589 struct range_list *rl = NULL;
591 while (expr->type == EXPR_ASSIGNMENT)
592 expr = strip_expr(expr->right);
593 if (expr->type != EXPR_CALL)
594 return;
596 arg = get_argument_from_call_expr(expr->args, param);
597 if (!arg)
598 return;
599 type = get_member_type_from_key(arg, key);
600 name = get_variable_from_key(arg, key, &sym);
601 if (!name || !sym)
602 goto free;
604 call_results_to_rl(expr, type, value, &rl);
606 set_state(my_id, name, sym, alloc_estate_rl(rl));
607 free:
608 free_string(name);
611 static int has_empty_state(struct sm_state *sm)
613 struct sm_state *tmp;
615 FOR_EACH_PTR(sm->possible, tmp) {
616 if (!estate_rl(tmp->state))
617 return 1;
618 } END_FOR_EACH_PTR(tmp);
620 return 0;
623 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
625 struct sm_state *sm;
626 struct smatch_state *start_state;
627 int param;
628 const char *param_name;
630 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
631 if (has_empty_state(sm))
632 continue;
634 param = get_param_num_from_sym(sm->sym);
635 if (param < 0) {
636 if (expr_to_sym(expr) == sm->sym)
637 param = -1;
638 else
639 continue;
642 /* The logic here was that if we were passed in a user data then
643 * we don't record that. It's like the difference between
644 * param_filter and param_set. When I think about it, I'm not
645 * sure it actually works. It's probably harmless because we
646 * checked earlier that we're not returning a parameter...
647 * Let's mark this as a TODO.
649 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
650 if (start_state && estates_equiv(sm->state, start_state))
651 continue;
653 param_name = get_param_name(sm);
654 if (!param_name)
655 continue;
656 if (strcmp(param_name, "$") == 0)
657 continue;
659 sql_insert_return_states(return_id, return_ranges, USER_DATA3,
660 param, param_name, show_rl(estate_rl(sm->state)));
661 } END_FOR_EACH_SM(sm);
664 void check_user_data2(int id)
666 int i;
668 my_id = id;
670 if (option_project != PROJ_KERNEL)
671 return;
673 add_hook(&save_start_states, AFTER_DEF_HOOK);
674 add_hook(&free_start_states, AFTER_FUNC_HOOK);
675 add_hook(&match_save_states, INLINE_FN_START);
676 add_hook(&match_restore_states, INLINE_FN_END);
678 add_unmatched_state_hook(my_id, &empty_state);
679 add_pre_merge_hook(my_id, &pre_merge_hook);
680 add_merge_hook(my_id, &merge_estates);
682 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
683 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
684 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
685 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
686 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
688 add_function_assign_hook("simple_strtol", &match_simple_strtoul, NULL);
689 add_function_assign_hook("simple_strtoll", &match_simple_strtoul, NULL);
690 add_function_assign_hook("simple_strtoul", &match_simple_strtoul, NULL);
691 add_function_assign_hook("simple_strtoull", &match_simple_strtoul, NULL);
693 add_function_hook("sscanf", &match_sscanf, NULL);
695 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
696 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
697 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
699 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
701 add_hook(&match_assign, ASSIGNMENT_HOOK);
703 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
704 add_member_info_callback(my_id, struct_member_callback);
705 select_caller_info_hook(set_param_user_data, USER_DATA3);
706 select_return_states_hook(USER_DATA3, &returns_param_user_data);
707 add_split_return_callback(&param_set_to_user_data);
710 void check_user_data3(int id)
712 my_call_id = id;
714 if (option_project != PROJ_KERNEL)
715 return;
716 select_caller_info_hook(set_called, INTERNAL);