pointer_math: ALIGN is not bytes, but offsetof() and PAGE_SIZE are
[smatch.git] / check_user_data2.c
blob9babece99bbeac4b2ad2af395d15372e6f876e38
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 expr = strip_expr(expr);
193 if (expr->type == EXPR_BINOP && expr->op == '+') {
194 if (points_to_user_data(expr->left))
195 return 1;
196 if (points_to_user_data(expr->right))
197 return 1;
198 return 0;
201 name = expr_to_var_sym(expr, &sym);
202 if (!name || !sym)
203 goto free;
204 snprintf(buf, sizeof(buf), "*%s", name);
205 state = get_state(my_id, buf, sym);
206 if (state && estate_rl(state))
207 ret = 1;
208 free:
209 free_string(name);
210 return ret;
213 static void set_points_to_user_data(struct expression *expr)
215 char *name;
216 struct symbol *sym;
217 char buf[256];
219 name = expr_to_var_sym(expr, &sym);
220 if (!name || !sym)
221 goto free;
222 snprintf(buf, sizeof(buf), "*%s", name);
223 set_state(my_id, buf, sym, alloc_estate_whole(&llong_ctype));
224 free:
225 free_string(name);
228 static int is_skb_data(struct expression *expr)
230 struct symbol *sym;
232 expr = strip_expr(expr);
233 if (!expr || expr->type != EXPR_DEREF)
234 return 0;
236 if (!expr->member)
237 return 0;
238 if (strcmp(expr->member->name, "data") != 0)
239 return 0;
241 sym = expr_to_sym(expr->deref);
242 if (!sym)
243 return 0;
244 sym = get_real_base_type(sym);
245 if (!sym || sym->type != SYM_PTR)
246 return 0;
247 sym = get_real_base_type(sym);
248 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
249 return 0;
250 if (strcmp(sym->ident->name, "sk_buff") != 0)
251 return 0;
253 return 1;
256 static int comes_from_skb_data(struct expression *expr)
258 expr = strip_expr(expr);
259 if (!expr)
260 return 0;
262 switch (expr->type) {
263 case EXPR_BINOP:
264 if (comes_from_skb_data(expr->left))
265 return 1;
266 if (comes_from_skb_data(expr->right))
267 return 1;
268 return 0;
269 case EXPR_PREOP:
270 return comes_from_skb_data(expr->unop);
271 case EXPR_DEREF:
272 if (is_skb_data(expr))
273 return 1;
274 return comes_from_skb_data(expr->deref);
275 default:
276 return 0;
281 static int handle_struct_assignment(struct expression *expr)
283 struct expression *right;
284 struct symbol *type;
286 type = get_type(expr->left);
287 if (!type || type->type != SYM_PTR)
288 return 0;
289 type = get_real_base_type(type);
290 if (!type || type->type != SYM_STRUCT)
291 return 0;
294 * Ignore struct to struct assignments because for those we look at the
295 * individual members.
297 right = strip_expr(expr->right);
298 type = get_type(right);
299 if (!type || type->type != SYM_PTR)
300 return 0;
302 /* structs are handled else where */
303 type = get_real_base_type(type);
304 if (type && type->type == SYM_STRUCT)
305 return 0;
307 if (!points_to_user_data(right) && !is_skb_data(right))
308 return 0;
310 tag_as_user_data(expr->left);
311 return 1;
314 static int handle_get_user(struct expression *expr)
316 char *name;
317 int ret = 0;
319 name = get_macro_name(expr->pos);
320 if (!name || strcmp(name, "get_user") != 0)
321 return 0;
323 name = expr_to_var(expr->right);
324 if (!name || strcmp(name, "__val_gu") != 0)
325 goto free;
326 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
327 ret = 1;
328 free:
329 free_string(name);
330 return ret;
333 static void match_assign(struct expression *expr)
335 struct range_list *rl;
337 if (is_fake_call(expr->right))
338 return;
339 if (handle_get_user(expr))
340 return;
341 if (points_to_user_data(expr->right))
342 set_points_to_user_data(expr->left);
343 if (handle_struct_assignment(expr))
344 return;
346 if (expr->right->type == EXPR_CALL ||
347 !get_user_rl(expr->right, &rl))
348 goto clear_old_state;
350 rl = cast_rl(get_type(expr->left), rl);
351 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
353 return;
355 clear_old_state:
356 if (get_state_expr(my_id, expr->left))
357 set_state_expr(my_id, expr->left, alloc_estate_empty());
360 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
362 tag_as_user_data(expr->left);
363 set_points_to_user_data(expr->left);
366 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
368 char *macro;
370 if (!expr)
371 return 0;
372 macro = get_macro_name(expr->pos);
374 if (!macro)
375 return 0;
377 if (strcmp(macro, "ntohl") == 0) {
378 *rl = alloc_whole_rl(&uint_ctype);
379 return 1;
381 if (strcmp(macro, "ntohs") == 0) {
382 *rl = alloc_whole_rl(&ushort_ctype);
383 return 1;
385 return 0;
388 static int user_data_flag;
389 static struct range_list *var_user_rl(struct expression *expr)
391 struct smatch_state *state;
392 struct range_list *rl;
393 struct range_list *absolute_rl;
395 if (get_user_macro_rl(expr, &rl))
396 goto found;
398 if (comes_from_skb_data(expr)) {
399 rl = alloc_whole_rl(get_type(expr));
400 goto found;
403 state = get_state_expr(my_id, expr);
404 if (state && estate_rl(state)) {
405 rl = estate_rl(state);
406 goto found;
409 return NULL;
410 found:
411 user_data_flag = 1;
412 absolute_rl = var_to_absolute_rl(expr);
413 return clone_rl(rl_intersection(rl, absolute_rl));
416 int get_user_rl(struct expression *expr, struct range_list **rl)
419 user_data_flag = 0;
420 custom_get_absolute_rl(expr, &var_user_rl, rl);
421 if (!user_data_flag) {
422 *rl = NULL;
423 return 0;
425 return 1;
428 static void match_call_info(struct expression *expr)
430 struct range_list *rl;
431 struct expression *arg;
432 int i = 0;
434 i = -1;
435 FOR_EACH_PTR(expr->args, arg) {
436 i++;
438 if (!get_user_rl(arg, &rl))
439 continue;
441 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
442 } END_FOR_EACH_PTR(arg);
445 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
447 struct smatch_state *state;
448 struct range_list *rl;
450 if (strcmp(sm->state->name, "") == 0)
451 return;
453 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
454 if (!state || !estate_rl(state))
455 rl = estate_rl(sm->state);
456 else
457 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
459 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
462 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
464 struct range_list *rl = NULL;
465 struct smatch_state *state;
466 struct symbol *type;
467 char fullname[256];
469 if (strcmp(key, "*$") == 0)
470 snprintf(fullname, sizeof(fullname), "*%s", name);
471 else if (strncmp(key, "$", 1) == 0)
472 snprintf(fullname, 256, "%s%s", name, key + 1);
473 else
474 return;
476 type = get_member_type_from_key(symbol_expression(sym), key);
478 /* if the caller passes a void pointer with user data */
479 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
480 struct expression *expr = symbol_expression(sym);
482 tag_as_user_data(expr);
483 set_points_to_user_data(expr);
484 return;
486 str_to_rl(type, value, &rl);
487 state = alloc_estate_rl(rl);
488 set_state(my_id, fullname, sym, state);
491 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
493 set_state(my_call_id, "this_function", NULL, &called);
496 static void match_syscall_definition(struct symbol *sym)
498 struct symbol *arg;
499 char *macro;
500 char *name;
501 int is_syscall = 0;
503 macro = get_macro_name(sym->pos);
504 if (macro &&
505 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
506 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
507 is_syscall = 1;
509 name = get_function();
510 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
511 if (name && strncmp(name, "sys_", 4) == 0)
512 is_syscall = 1;
515 if (name && strncmp(name, "compat_sys_", 11) == 0)
516 is_syscall = 1;
518 if (!is_syscall)
519 return;
521 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
522 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
523 } END_FOR_EACH_PTR(arg);
526 static void returns_param_user_data(struct expression *expr, int param, char *key, char *value)
528 struct expression *arg;
529 char *name;
530 struct symbol *sym;
531 struct symbol *type;
532 struct range_list *rl = NULL;
534 while (expr->type == EXPR_ASSIGNMENT)
535 expr = strip_expr(expr->right);
536 if (expr->type != EXPR_CALL)
537 return;
539 arg = get_argument_from_call_expr(expr->args, param);
540 if (!arg)
541 return;
542 type = get_member_type_from_key(arg, key);
543 name = get_variable_from_key(arg, key, &sym);
544 if (!name || !sym)
545 goto free;
547 call_results_to_rl(expr, type, value, &rl);
549 set_state(my_id, name, sym, alloc_estate_rl(rl));
550 free:
551 free_string(name);
554 static int has_empty_state(struct sm_state *sm)
556 struct sm_state *tmp;
558 FOR_EACH_PTR(sm->possible, tmp) {
559 if (!estate_rl(tmp->state))
560 return 1;
561 } END_FOR_EACH_PTR(tmp);
563 return 0;
566 static void param_set_to_user_data(int return_id, char *return_ranges, struct expression *expr)
568 struct sm_state *sm;
569 struct smatch_state *start_state;
570 int param;
571 const char *param_name;
573 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
574 if (has_empty_state(sm))
575 continue;
578 param = get_param_num_from_sym(sm->sym);
579 if (param < 0) {
580 if (expr_to_sym(expr) == sm->sym)
581 param = -1;
582 else
583 continue;
587 start_state = get_state_stree(start_states, my_id, sm->name, sm->sym);
588 if (start_state && estates_equiv(sm->state, start_state))
589 continue;
592 param_name = get_param_name(sm);
593 if (!param_name)
594 continue;
595 if (strcmp(param_name, "$") == 0)
596 continue;
598 sql_insert_return_states(return_id, return_ranges, USER_DATA3,
599 param, param_name, show_rl(estate_rl(sm->state)));
600 } END_FOR_EACH_SM(sm);
603 void check_user_data2(int id)
605 my_id = id;
607 if (option_project != PROJ_KERNEL)
608 return;
610 add_hook(&save_start_states, AFTER_DEF_HOOK);
611 add_hook(&free_start_states, END_FUNC_HOOK);
612 add_hook(&match_save_states, INLINE_FN_START);
613 add_hook(&match_restore_states, INLINE_FN_END);
615 add_unmatched_state_hook(my_id, &empty_state);
616 add_merge_hook(my_id, &merge_estates);
618 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
619 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
620 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
621 add_function_hook("_kstrtoull", &match_user_copy, INT_PTR(2));
623 add_function_hook("sscanf", &match_sscanf, NULL);
625 add_function_assign_hook("memdup_user", &match_user_assign_function, NULL);
626 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
627 add_function_assign_hook("skb_network_header", &match_user_assign_function, NULL);
629 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
631 add_hook(&match_assign, ASSIGNMENT_HOOK);
633 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
634 add_member_info_callback(my_id, struct_member_callback);
635 select_caller_info_hook(set_param_user_data, USER_DATA3);
636 select_return_states_hook(USER_DATA3, &returns_param_user_data);
637 add_split_return_callback(&param_set_to_user_data);
640 void check_user_data3(int id)
642 my_call_id = id;
644 if (option_project != PROJ_KERNEL)
645 return;
646 select_caller_info_hook(set_called, INTERNAL);