Documentation: make me less confused
[smatch.git] / check_user_data2.c
blob645bd876cf476c7aaf89a736ef61f4e7760fc61a
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 struct stree *start_states;
35 static struct stree_stack *saved_stack;
36 static void save_start_states(struct statement *stmt)
38 start_states = clone_stree(__get_cur_stree());
41 static void free_start_states(void)
43 free_stree(&start_states);
46 static void match_save_states(struct expression *expr)
48 push_stree(&saved_stack, start_states);
49 start_states = NULL;
52 static void match_restore_states(struct expression *expr)
54 free_stree(&start_states);
55 start_states = pop_stree(&saved_stack);
58 static struct smatch_state *empty_state(struct sm_state *sm)
60 return alloc_estate_empty();
63 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
65 struct expression *edge_member;
66 struct symbol *base = get_real_base_type(member);
67 struct symbol *tmp;
69 if (member->ident)
70 expr = member_expression(expr, '.', member->ident);
72 FOR_EACH_PTR(base->symbol_list, tmp) {
73 struct symbol *type;
75 type = get_real_base_type(tmp);
76 if (!type)
77 continue;
79 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
80 tag_inner_struct_members(expr, tmp);
81 continue;
84 if (!tmp->ident)
85 continue;
87 edge_member = member_expression(expr, '.', tmp->ident);
88 set_state_expr(my_id, edge_member, alloc_estate_whole(type));
89 } END_FOR_EACH_PTR(tmp);
94 static void tag_struct_members(struct symbol *type, struct expression *expr)
96 struct symbol *tmp;
97 struct expression *member;
98 int op = '*';
100 if (expr->type == EXPR_PREOP && expr->op == '&') {
101 expr = strip_expr(expr->unop);
102 op = '.';
105 FOR_EACH_PTR(type->symbol_list, tmp) {
106 type = get_real_base_type(tmp);
107 if (!type)
108 continue;
110 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
111 tag_inner_struct_members(expr, tmp);
112 continue;
115 if (!tmp->ident)
116 continue;
118 member = member_expression(expr, op, tmp->ident);
119 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
120 } END_FOR_EACH_PTR(tmp);
123 static void tag_base_type(struct expression *expr)
125 if (expr->type == EXPR_PREOP && expr->op == '&')
126 expr = strip_expr(expr->unop);
127 else
128 expr = deref_expression(expr);
129 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
132 static void tag_as_user_data(struct expression *expr)
134 struct symbol *type;
136 expr = strip_expr(expr);
138 type = get_type(expr);
139 if (!type || type->type != SYM_PTR)
140 return;
141 type = get_real_base_type(type);
142 if (!type)
143 return;
144 if (type == &void_ctype) {
145 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
146 return;
148 if (type->type == SYM_BASETYPE)
149 tag_base_type(expr);
150 if (type->type == SYM_STRUCT) {
151 if (expr->type != EXPR_PREOP || expr->op != '&')
152 expr = deref_expression(expr);
153 tag_struct_members(type, expr);
157 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
159 int param = PTR_INT(_param);
160 struct expression *dest;
162 dest = get_argument_from_call_expr(expr->args, param);
163 dest = strip_expr(dest);
164 if (!dest)
165 return;
166 tag_as_user_data(dest);
169 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
171 struct expression *arg;
172 int i;
174 i = -1;
175 FOR_EACH_PTR(expr->args, arg) {
176 i++;
177 if (i < 2)
178 continue;
179 tag_as_user_data(arg);
180 } END_FOR_EACH_PTR(arg);
183 static int points_to_user_data(struct expression *expr)
185 struct smatch_state *state;
186 char buf[256];
187 struct symbol *sym;
188 char *name;
189 int ret = 0;
191 name = expr_to_var_sym(expr, &sym);
192 if (!name || !sym)
193 goto free;
194 snprintf(buf, sizeof(buf), "*%s", name);
195 state = get_state(my_id, buf, sym);
196 if (state && estate_rl(state))
197 ret = 1;
198 free:
199 free_string(name);
200 return ret;
203 static int is_skb_data(struct expression *expr)
205 struct symbol *sym;
207 expr = strip_expr(expr);
208 if (!expr || expr->type != EXPR_DEREF)
209 return 0;
211 if (!expr->member)
212 return 0;
213 if (strcmp(expr->member->name, "data") != 0)
214 return 0;
216 sym = expr_to_sym(expr->deref);
217 if (!sym)
218 return 0;
219 sym = get_real_base_type(sym);
220 if (!sym || sym->type != SYM_PTR)
221 return 0;
222 sym = get_real_base_type(sym);
223 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
224 return 0;
225 if (strcmp(sym->ident->name, "sk_buff") != 0)
226 return 0;
228 return 1;
231 static int comes_from_skb_data(struct expression *expr)
233 expr = strip_expr(expr);
234 if (!expr)
235 return 0;
237 switch (expr->type) {
238 case EXPR_BINOP:
239 if (comes_from_skb_data(expr->left))
240 return 1;
241 if (comes_from_skb_data(expr->right))
242 return 1;
243 return 0;
244 case EXPR_PREOP:
245 return comes_from_skb_data(expr->unop);
246 case EXPR_DEREF:
247 if (is_skb_data(expr))
248 return 1;
249 return comes_from_skb_data(expr->deref);
250 default:
251 return 0;
256 static int handle_struct_assignment(struct expression *expr)
258 struct expression *right;
259 struct symbol *type;
261 type = get_type(expr->left);
262 if (!type || type->type != SYM_PTR)
263 return 0;
264 type = get_real_base_type(type);
265 if (!type || type->type != SYM_STRUCT)
266 return 0;
269 * Ignore struct to struct assignments because for those we look at the
270 * individual members.
272 right = strip_expr(expr->right);
273 type = get_type(right);
274 if (!type || type->type != SYM_PTR)
275 return 0;
276 type = get_real_base_type(type);
277 if (type && type->type == SYM_STRUCT)
278 return 0;
280 if (!points_to_user_data(right) && !is_skb_data(right))
281 return 0;
283 tag_as_user_data(expr->left);
284 return 1;
287 static int handle_get_user(struct expression *expr)
289 char *name;
290 int ret = 0;
292 name = get_macro_name(expr->pos);
293 if (!name || strcmp(name, "get_user") != 0)
294 return 0;
296 name = expr_to_var(expr->right);
297 if (!name || strcmp(name, "__val_gu") != 0)
298 goto free;
299 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
300 ret = 1;
301 free:
302 free_string(name);
303 return ret;
306 static void match_assign(struct expression *expr)
308 struct range_list *rl;
310 if (is_fake_call(expr->right))
311 return;
312 if (handle_struct_assignment(expr))
313 return;
314 if (handle_get_user(expr))
315 return;
317 if (expr->right->type == EXPR_CALL ||
318 !get_user_rl(expr->right, &rl))
319 goto clear_old_state;
321 rl = cast_rl(get_type(expr->left), rl);
322 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
324 return;
326 clear_old_state:
327 if (get_state_expr(my_id, expr->left))
328 set_state_expr(my_id, expr->left, alloc_estate_empty());
331 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
333 tag_as_user_data(expr->left);
336 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
338 char *macro;
340 if (!expr)
341 return 0;
342 macro = get_macro_name(expr->pos);
344 if (!macro)
345 return 0;
347 if (strcmp(macro, "ntohl") == 0) {
348 *rl = alloc_whole_rl(&uint_ctype);
349 return 1;
351 if (strcmp(macro, "ntohs") == 0) {
352 *rl = alloc_whole_rl(&ushort_ctype);
353 return 1;
355 return 0;
358 static int user_data_flag;
359 static struct range_list *var_user_rl(struct expression *expr)
361 struct smatch_state *state;
362 struct range_list *rl;
363 struct range_list *absolute_rl;
365 if (get_user_macro_rl(expr, &rl))
366 goto found;
368 if (comes_from_skb_data(expr)) {
369 rl = alloc_whole_rl(get_type(expr));
370 goto found;
373 state = get_state_expr(my_id, expr);
374 if (state && estate_rl(state)) {
375 rl = estate_rl(state);
376 goto found;
379 return NULL;
380 found:
381 user_data_flag = 1;
382 absolute_rl = var_to_absolute_rl(expr);
383 return clone_rl(rl_intersection(rl, absolute_rl));
386 int get_user_rl(struct expression *expr, struct range_list **rl)
389 user_data_flag = 0;
390 custom_get_absolute_rl(expr, &var_user_rl, rl);
391 if (!user_data_flag) {
392 *rl = NULL;
393 return 0;
395 return 1;
398 static void match_call_info(struct expression *expr)
400 struct range_list *rl;
401 struct expression *arg;
402 int i = 0;
404 i = -1;
405 FOR_EACH_PTR(expr->args, arg) {
406 i++;
408 if (!get_user_rl(arg, &rl))
409 continue;
411 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
412 } END_FOR_EACH_PTR(arg);
415 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
417 struct smatch_state *state;
418 struct range_list *rl;
420 if (strcmp(sm->state->name, "") == 0)
421 return;
423 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
424 if (!state || !estate_rl(state))
425 rl = estate_rl(sm->state);
426 else
427 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
429 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
432 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
434 struct range_list *rl = NULL;
435 struct smatch_state *state;
436 struct symbol *type;
437 char fullname[256];
439 if (strcmp(key, "*$") == 0)
440 snprintf(fullname, sizeof(fullname), "*%s", name);
441 else if (strncmp(key, "$", 1) == 0)
442 snprintf(fullname, 256, "%s%s", name, key + 1);
443 else
444 return;
446 type = get_member_type_from_key(symbol_expression(sym), key);
448 /* if the caller passes a void pointer with user data */
449 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
450 struct expression *expr = symbol_expression(sym);
452 tag_as_user_data(expr);
453 return;
455 str_to_rl(type, value, &rl);
456 state = alloc_estate_rl(rl);
457 set_state(my_id, fullname, sym, state);
460 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
462 set_state(my_call_id, "this_function", NULL, &called);
465 static void match_syscall_definition(struct symbol *sym)
467 struct symbol *arg;
468 char *macro;
469 char *name;
470 int is_syscall = 0;
472 macro = get_macro_name(sym->pos);
473 if (macro &&
474 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
475 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
476 is_syscall = 1;
478 name = get_function();
479 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
480 if (name && strncmp(name, "sys_", 4) == 0)
481 is_syscall = 1;
484 if (name && strncmp(name, "compat_sys_", 11) == 0)
485 is_syscall = 1;
487 if (!is_syscall)
488 return;
490 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
491 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
492 } END_FOR_EACH_PTR(arg);
495 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
497 struct expression *arg;
498 char *name;
499 struct symbol *sym;
500 struct symbol *type;
501 struct range_list *rl = NULL;
503 while (expr->type == EXPR_ASSIGNMENT)
504 expr = strip_expr(expr->right);
505 if (expr->type != EXPR_CALL)
506 return;
508 arg = get_argument_from_call_expr(expr->args, param);
509 if (!arg)
510 return;
511 type = get_member_type_from_key(arg, key);
512 name = get_variable_from_key(arg, key, &sym);
513 if (!name || !sym)
514 goto free;
516 call_results_to_rl(expr, type, value, &rl);
518 set_state(my_id, name, sym, alloc_estate_rl(rl));
519 free:
520 free_string(name);
523 static int has_empty_state(struct sm_state *sm)
525 struct sm_state *tmp;
527 FOR_EACH_PTR(sm->possible, tmp) {
528 if (!estate_rl(tmp->state))
529 return 1;
530 } END_FOR_EACH_PTR(tmp);
532 return 0;
535 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
537 struct sm_state *sm;
538 struct smatch_state *start_state;
539 int param;
540 const char *param_name;
542 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
543 if (has_empty_state(sm))
544 continue;
547 param = get_param_num_from_sym(sm->sym);
548 if (param < 0) {
549 if (expr_to_sym(expr) == sm->sym)
550 param = -1;
551 else
552 continue;
556 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
557 if (start_state && estates_equiv(sm->state, start_state))
558 continue;
561 param_name = get_param_name(sm);
562 if (!param_name)
563 continue;
564 if (strcmp(param_name, "$") == 0)
565 continue;
567 sql_insert_return_states(return_id, return_ranges, USER_DATA3,
568 param, param_name, show_rl(estate_rl(sm->state)));
569 } END_FOR_EACH_SM(sm);
572 void check_user_data2(int id)
574 my_id = id;
576 if (option_project != PROJ_KERNEL)
577 return;
579 add_hook(&save_start_states, AFTER_DEF_HOOK);
580 add_hook(&free_start_states, END_FUNC_HOOK);
581 add_hook(&match_save_states, INLINE_FN_START);
582 add_hook(&match_restore_states, INLINE_FN_END);
584 add_unmatched_state_hook(my_id, &empty_state);
585 add_merge_hook(my_id, &merge_estates);
587 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
588 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
589 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
590 add_function_hook("_kstrtoull", &match_user_copy, INT_PTR(2));
592 add_function_hook("sscanf", &match_sscanf, NULL);
594 add_function_assign_hook("kmemdup_user", &match_user_assign_function, NULL);
595 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
597 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
599 add_hook(&match_assign, ASSIGNMENT_HOOK);
601 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
602 add_member_info_callback(my_id, struct_member_callback);
603 select_caller_info_hook(set_param_user_data, USER_DATA3);
604 select_return_states_hook(USER_DATA3, &returns_param_user_data);
605 add_split_return_callback(&param_set_to_user_data);
608 void check_user_data3(int id)
610 my_call_id = id;
612 if (option_project != PROJ_KERNEL)
613 return;
614 select_caller_info_hook(set_called, INTERNAL);