db: handle parameter function pointers
[smatch.git] / check_user_data2.c
blob2e0904e7d5c72e2464f055871d8715c5b4eeb2a8
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);
123 static void tag_struct_members(struct symbol *type, struct expression *expr)
125 struct symbol *tmp;
126 struct expression *member;
127 int op = '*';
129 if (expr->type == EXPR_PREOP && expr->op == '&') {
130 expr = strip_expr(expr->unop);
131 op = '.';
134 FOR_EACH_PTR(type->symbol_list, tmp) {
135 type = get_real_base_type(tmp);
136 if (!type)
137 continue;
139 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
140 tag_inner_struct_members(expr, tmp);
141 continue;
144 if (!tmp->ident)
145 continue;
147 member = member_expression(expr, op, tmp->ident);
148 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
150 if (type->type == SYM_ARRAY)
151 set_points_to_user_data(member);
152 } END_FOR_EACH_PTR(tmp);
155 static void tag_base_type(struct expression *expr)
157 if (expr->type == EXPR_PREOP && expr->op == '&')
158 expr = strip_expr(expr->unop);
159 else
160 expr = deref_expression(expr);
161 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
164 static void tag_as_user_data(struct expression *expr)
166 struct symbol *type;
168 expr = strip_expr(expr);
170 type = get_type(expr);
171 if (!type || type->type != SYM_PTR)
172 return;
173 type = get_real_base_type(type);
174 if (!type)
175 return;
176 if (type == &void_ctype) {
177 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
178 return;
180 if (type->type == SYM_BASETYPE)
181 tag_base_type(expr);
182 if (type->type == SYM_STRUCT) {
183 if (expr->type != EXPR_PREOP || expr->op != '&')
184 expr = deref_expression(expr);
185 else
186 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
187 tag_struct_members(type, expr);
191 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
193 int param = PTR_INT(_param);
194 struct expression *dest;
196 dest = get_argument_from_call_expr(expr->args, param);
197 dest = strip_expr(dest);
198 if (!dest)
199 return;
200 tag_as_user_data(dest);
203 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
205 struct expression *arg;
206 int i;
208 i = -1;
209 FOR_EACH_PTR(expr->args, arg) {
210 i++;
211 if (i < 2)
212 continue;
213 tag_as_user_data(arg);
214 } END_FOR_EACH_PTR(arg);
217 static int points_to_user_data(struct expression *expr)
219 struct smatch_state *state;
220 char buf[256];
221 struct symbol *sym;
222 char *name;
223 int ret = 0;
225 expr = strip_expr(expr);
227 if (expr->type == EXPR_BINOP && expr->op == '+') {
228 if (points_to_user_data(expr->left))
229 return 1;
230 if (points_to_user_data(expr->right))
231 return 1;
232 return 0;
235 name = expr_to_var_sym(expr, &sym);
236 if (!name || !sym)
237 goto free;
238 snprintf(buf, sizeof(buf), "*%s", name);
239 state = get_state(my_id, buf, sym);
240 if (state && estate_rl(state))
241 ret = 1;
242 free:
243 free_string(name);
244 return ret;
247 static void set_points_to_user_data(struct expression *expr)
249 char *name;
250 struct symbol *sym;
251 char buf[256];
253 name = expr_to_var_sym(expr, &sym);
254 if (!name || !sym)
255 goto free;
256 snprintf(buf, sizeof(buf), "*%s", name);
257 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
258 free:
259 free_string(name);
262 static int is_skb_data(struct expression *expr)
264 struct symbol *sym;
266 expr = strip_expr(expr);
267 if (!expr || expr->type != EXPR_DEREF)
268 return 0;
270 if (!expr->member)
271 return 0;
272 if (strcmp(expr->member->name, "data") != 0)
273 return 0;
275 sym = expr_to_sym(expr->deref);
276 if (!sym)
277 return 0;
278 sym = get_real_base_type(sym);
279 if (!sym || sym->type != SYM_PTR)
280 return 0;
281 sym = get_real_base_type(sym);
282 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
283 return 0;
284 if (strcmp(sym->ident->name, "sk_buff") != 0)
285 return 0;
287 return 1;
290 static int comes_from_skb_data(struct expression *expr)
292 expr = strip_expr(expr);
293 if (!expr)
294 return 0;
296 switch (expr->type) {
297 case EXPR_BINOP:
298 if (comes_from_skb_data(expr->left))
299 return 1;
300 if (comes_from_skb_data(expr->right))
301 return 1;
302 return 0;
303 case EXPR_PREOP:
304 return comes_from_skb_data(expr->unop);
305 case EXPR_DEREF:
306 if (is_skb_data(expr))
307 return 1;
308 return comes_from_skb_data(expr->deref);
309 default:
310 return 0;
315 static int handle_struct_assignment(struct expression *expr)
317 struct expression *right;
318 struct symbol *left_type, *right_type;
320 left_type = get_type(expr->left);
321 if (!left_type || left_type->type != SYM_PTR)
322 return 0;
323 left_type = get_real_base_type(left_type);
324 if (!left_type || left_type->type != SYM_STRUCT)
325 return 0;
328 * Ignore struct to struct assignments because for those we look at the
329 * individual members.
331 right = strip_expr(expr->right);
332 right_type = get_type(right);
333 if (!right_type || right_type->type != SYM_PTR)
334 return 0;
336 /* If we are assigning struct members then normally that is handled
337 * by fake assignments, however if we cast one struct to a different
338 * of struct then we handle that here.
340 right_type = get_real_base_type(right_type);
341 if (right_type == left_type)
342 return 0;
344 if (!points_to_user_data(right) && !is_skb_data(right))
345 return 0;
347 tag_as_user_data(expr->left);
348 return 1;
351 static int handle_get_user(struct expression *expr)
353 char *name;
354 int ret = 0;
356 name = get_macro_name(expr->pos);
357 if (!name || strcmp(name, "get_user") != 0)
358 return 0;
360 name = expr_to_var(expr->right);
361 if (!name || strcmp(name, "__val_gu") != 0)
362 goto free;
363 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
364 ret = 1;
365 free:
366 free_string(name);
367 return ret;
370 static void match_assign(struct expression *expr)
372 struct range_list *rl;
374 if (is_fake_call(expr->right))
375 return;
376 if (handle_get_user(expr))
377 return;
378 if (points_to_user_data(expr->right))
379 set_points_to_user_data(expr->left);
380 if (handle_struct_assignment(expr))
381 return;
383 if (expr->right->type == EXPR_CALL ||
384 !get_user_rl(expr->right, &rl))
385 goto clear_old_state;
387 rl = cast_rl(get_type(expr->left), rl);
388 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
390 return;
392 clear_old_state:
393 if (get_state_expr(my_id, expr->left))
394 set_state_expr(my_id, expr->left, alloc_estate_empty());
397 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
399 tag_as_user_data(expr->left);
400 set_points_to_user_data(expr->left);
403 static void match_simple_strtoul(const char *fn, struct expression *expr, void *unused)
405 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
408 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
410 char *macro;
412 if (!expr)
413 return 0;
414 macro = get_macro_name(expr->pos);
416 if (!macro)
417 return 0;
419 if (strcmp(macro, "ntohl") == 0) {
420 *rl = alloc_whole_rl(&uint_ctype);
421 return 1;
423 if (strcmp(macro, "ntohs") == 0) {
424 *rl = alloc_whole_rl(&ushort_ctype);
425 return 1;
427 return 0;
430 static int user_data_flag;
431 static struct range_list *var_user_rl(struct expression *expr)
433 struct smatch_state *state;
434 struct range_list *rl;
435 struct range_list *absolute_rl;
437 if (get_user_macro_rl(expr, &rl))
438 goto found;
440 if (comes_from_skb_data(expr)) {
441 rl = alloc_whole_rl(get_type(expr));
442 goto found;
445 state = get_state_expr(my_id, expr);
446 if (state && estate_rl(state)) {
447 rl = estate_rl(state);
448 goto found;
451 return NULL;
452 found:
453 user_data_flag = 1;
454 absolute_rl = var_to_absolute_rl(expr);
455 return clone_rl(rl_intersection(rl, absolute_rl));
458 int get_user_rl(struct expression *expr, struct range_list **rl)
461 user_data_flag = 0;
462 custom_get_absolute_rl(expr, &var_user_rl, rl);
463 if (!user_data_flag) {
464 *rl = NULL;
465 return 0;
467 return 1;
470 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
472 struct smatch_state *state;
474 state = get_state(my_id, name, sym);
475 if (state && estate_rl(state)) {
476 *rl = estate_rl(state);
477 return 1;
479 return 0;
482 static void match_call_info(struct expression *expr)
484 struct range_list *rl;
485 struct expression *arg;
486 int i = 0;
488 i = -1;
489 FOR_EACH_PTR(expr->args, arg) {
490 i++;
492 if (!get_user_rl(arg, &rl))
493 continue;
495 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
496 } END_FOR_EACH_PTR(arg);
499 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
501 struct smatch_state *state;
502 struct range_list *rl;
504 if (strcmp(sm->state->name, "") == 0)
505 return;
507 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
508 if (!state || !estate_rl(state))
509 rl = estate_rl(sm->state);
510 else
511 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
513 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
516 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
518 struct range_list *rl = NULL;
519 struct smatch_state *state;
520 struct symbol *type;
521 char fullname[256];
523 if (strcmp(key, "*$") == 0)
524 snprintf(fullname, sizeof(fullname), "*%s", name);
525 else if (strncmp(key, "$", 1) == 0)
526 snprintf(fullname, 256, "%s%s", name, key + 1);
527 else
528 return;
530 type = get_member_type_from_key(symbol_expression(sym), key);
532 /* if the caller passes a void pointer with user data */
533 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
534 struct expression *expr = symbol_expression(sym);
536 tag_as_user_data(expr);
537 set_points_to_user_data(expr);
538 return;
540 str_to_rl(type, value, &rl);
541 state = alloc_estate_rl(rl);
542 set_state(my_id, fullname, sym, state);
545 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
547 set_state(my_call_id, "this_function", NULL, &called);
550 static void match_syscall_definition(struct symbol *sym)
552 struct symbol *arg;
553 char *macro;
554 char *name;
555 int is_syscall = 0;
557 macro = get_macro_name(sym->pos);
558 if (macro &&
559 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
560 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
561 is_syscall = 1;
563 name = get_function();
564 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
565 if (name && strncmp(name, "sys_", 4) == 0)
566 is_syscall = 1;
569 if (name && strncmp(name, "compat_sys_", 11) == 0)
570 is_syscall = 1;
572 if (!is_syscall)
573 return;
575 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
576 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
577 } END_FOR_EACH_PTR(arg);
580 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
582 struct expression *arg;
583 char *name;
584 struct symbol *sym;
585 struct symbol *type;
586 struct range_list *rl = NULL;
588 while (expr->type == EXPR_ASSIGNMENT)
589 expr = strip_expr(expr->right);
590 if (expr->type != EXPR_CALL)
591 return;
593 arg = get_argument_from_call_expr(expr->args, param);
594 if (!arg)
595 return;
596 type = get_member_type_from_key(arg, key);
597 name = get_variable_from_key(arg, key, &sym);
598 if (!name || !sym)
599 goto free;
601 call_results_to_rl(expr, type, value, &rl);
603 set_state(my_id, name, sym, alloc_estate_rl(rl));
604 free:
605 free_string(name);
608 static int has_empty_state(struct sm_state *sm)
610 struct sm_state *tmp;
612 FOR_EACH_PTR(sm->possible, tmp) {
613 if (!estate_rl(tmp->state))
614 return 1;
615 } END_FOR_EACH_PTR(tmp);
617 return 0;
620 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
622 struct sm_state *sm;
623 struct smatch_state *start_state;
624 int param;
625 const char *param_name;
627 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
628 if (has_empty_state(sm))
629 continue;
631 param = get_param_num_from_sym(sm->sym);
632 if (param < 0) {
633 if (expr_to_sym(expr) == sm->sym)
634 param = -1;
635 else
636 continue;
639 /* The logic here was that if we were passed in a user data then
640 * we don't record that. It's like the difference between
641 * param_filter and param_set. When I think about it, I'm not
642 * sure it actually works. It's probably harmless because we
643 * checked earlier that we're not returning a parameter...
644 * Let's mark this as a TODO.
646 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
647 if (start_state && estates_equiv(sm->state, start_state))
648 continue;
650 param_name = get_param_name(sm);
651 if (!param_name)
652 continue;
653 if (strcmp(param_name, "$") == 0)
654 continue;
656 sql_insert_return_states(return_id, return_ranges, USER_DATA3,
657 param, param_name, show_rl(estate_rl(sm->state)));
658 } END_FOR_EACH_SM(sm);
661 void check_user_data2(int id)
663 int i;
665 my_id = id;
667 if (option_project != PROJ_KERNEL)
668 return;
670 add_hook(&save_start_states, AFTER_DEF_HOOK);
671 add_hook(&free_start_states, END_FUNC_HOOK);
672 add_hook(&match_save_states, INLINE_FN_START);
673 add_hook(&match_restore_states, INLINE_FN_END);
675 add_unmatched_state_hook(my_id, &empty_state);
676 add_pre_merge_hook(my_id, &pre_merge_hook);
677 add_merge_hook(my_id, &merge_estates);
679 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
680 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
681 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
682 for (i = 0; i < ARRAY_SIZE(kstr_funcs); i++)
683 add_function_hook(kstr_funcs[i], &match_user_copy, INT_PTR(2));
685 add_function_assign_hook("simple_strtol", &match_simple_strtoul, NULL);
686 add_function_assign_hook("simple_strtoll", &match_simple_strtoul, NULL);
687 add_function_assign_hook("simple_strtoul", &match_simple_strtoul, NULL);
688 add_function_assign_hook("simple_strtoull", &match_simple_strtoul, NULL);
690 add_function_hook("sscanf", &match_sscanf, NULL);
692 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
693 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
694 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
696 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
698 add_hook(&match_assign, ASSIGNMENT_HOOK);
700 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
701 add_member_info_callback(my_id, struct_member_callback);
702 select_caller_info_hook(set_param_user_data, USER_DATA3);
703 select_return_states_hook(USER_DATA3, &returns_param_user_data);
704 add_split_return_callback(&param_set_to_user_data);
707 void check_user_data3(int id)
709 my_call_id = id;
711 if (option_project != PROJ_KERNEL)
712 return;
713 select_caller_info_hook(set_called, INTERNAL);