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.
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
30 static int my_call_id
;
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
);
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
);
70 expr
= member_expression(expr
, '.', member
->ident
);
72 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
75 type
= get_real_base_type(tmp
);
79 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
80 tag_inner_struct_members(expr
, tmp
);
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
)
97 struct expression
*member
;
100 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
101 expr
= strip_expr(expr
->unop
);
105 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
106 type
= get_real_base_type(tmp
);
110 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
111 tag_inner_struct_members(expr
, tmp
);
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
);
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
)
136 expr
= strip_expr(expr
);
138 type
= get_type(expr
);
139 if (!type
|| type
->type
!= SYM_PTR
)
141 type
= get_real_base_type(type
);
144 if (type
== &void_ctype
) {
145 set_state_expr(my_id
, deref_expression(expr
), alloc_estate_whole(&ulong_ctype
));
148 if (type
->type
== SYM_BASETYPE
)
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
);
166 tag_as_user_data(dest
);
169 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
171 struct expression
*arg
;
175 FOR_EACH_PTR(expr
->args
, arg
) {
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
;
191 expr
= strip_expr(expr
);
193 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+') {
194 if (points_to_user_data(expr
->left
))
196 if (points_to_user_data(expr
->right
))
201 name
= expr_to_var_sym(expr
, &sym
);
204 snprintf(buf
, sizeof(buf
), "*%s", name
);
205 state
= get_state(my_id
, buf
, sym
);
206 if (state
&& estate_rl(state
))
213 static void set_points_to_user_data(struct expression
*expr
)
219 name
= expr_to_var_sym(expr
, &sym
);
222 snprintf(buf
, sizeof(buf
), "*%s", name
);
223 set_state(my_id
, buf
, sym
, alloc_estate_whole(&llong_ctype
));
228 static int is_skb_data(struct expression
*expr
)
232 expr
= strip_expr(expr
);
233 if (!expr
|| expr
->type
!= EXPR_DEREF
)
238 if (strcmp(expr
->member
->name
, "data") != 0)
241 sym
= expr_to_sym(expr
->deref
);
244 sym
= get_real_base_type(sym
);
245 if (!sym
|| sym
->type
!= SYM_PTR
)
247 sym
= get_real_base_type(sym
);
248 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
250 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
256 static int comes_from_skb_data(struct expression
*expr
)
258 expr
= strip_expr(expr
);
262 switch (expr
->type
) {
264 if (comes_from_skb_data(expr
->left
))
266 if (comes_from_skb_data(expr
->right
))
270 return comes_from_skb_data(expr
->unop
);
272 if (is_skb_data(expr
))
274 return comes_from_skb_data(expr
->deref
);
281 static int handle_struct_assignment(struct expression
*expr
)
283 struct expression
*right
;
286 type
= get_type(expr
->left
);
287 if (!type
|| type
->type
!= SYM_PTR
)
289 type
= get_real_base_type(type
);
290 if (!type
|| type
->type
!= SYM_STRUCT
)
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
)
302 /* structs are handled else where */
303 type
= get_real_base_type(type
);
304 if (type
&& type
->type
== SYM_STRUCT
)
307 if (!points_to_user_data(right
) && !is_skb_data(right
))
310 tag_as_user_data(expr
->left
);
314 static int handle_get_user(struct expression
*expr
)
319 name
= get_macro_name(expr
->pos
);
320 if (!name
|| strcmp(name
, "get_user") != 0)
323 name
= expr_to_var(expr
->right
);
324 if (!name
|| strcmp(name
, "__val_gu") != 0)
326 set_state_expr(my_id
, expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
333 static void match_assign(struct expression
*expr
)
335 struct range_list
*rl
;
337 if (is_fake_call(expr
->right
))
339 if (handle_get_user(expr
))
341 if (points_to_user_data(expr
->right
))
342 set_points_to_user_data(expr
->left
);
343 if (handle_struct_assignment(expr
))
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
));
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
)
372 macro
= get_macro_name(expr
->pos
);
377 if (strcmp(macro
, "ntohl") == 0) {
378 *rl
= alloc_whole_rl(&uint_ctype
);
381 if (strcmp(macro
, "ntohs") == 0) {
382 *rl
= alloc_whole_rl(&ushort_ctype
);
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
))
398 if (comes_from_skb_data(expr
)) {
399 rl
= alloc_whole_rl(get_type(expr
));
403 state
= get_state_expr(my_id
, expr
);
404 if (state
&& estate_rl(state
)) {
405 rl
= estate_rl(state
);
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
)
420 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
421 if (!user_data_flag
) {
428 static void match_call_info(struct expression
*expr
)
430 struct range_list
*rl
;
431 struct expression
*arg
;
435 FOR_EACH_PTR(expr
->args
, arg
) {
438 if (!get_user_rl(arg
, &rl
))
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)
453 state
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
454 if (!state
|| !estate_rl(state
))
455 rl
= estate_rl(sm
->state
);
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
;
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);
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
);
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
)
503 macro
= get_macro_name(sym
->pos
);
505 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
506 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
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)
515 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
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
;
532 struct range_list
*rl
= NULL
;
534 while (expr
->type
== EXPR_ASSIGNMENT
)
535 expr
= strip_expr(expr
->right
);
536 if (expr
->type
!= EXPR_CALL
)
539 arg
= get_argument_from_call_expr(expr
->args
, param
);
542 type
= get_member_type_from_key(arg
, key
);
543 name
= get_variable_from_key(arg
, key
, &sym
);
547 call_results_to_rl(expr
, type
, value
, &rl
);
549 set_state(my_id
, name
, sym
, alloc_estate_rl(rl
));
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
))
561 } END_FOR_EACH_PTR(tmp
);
566 static void param_set_to_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
569 struct smatch_state
*start_state
;
571 const char *param_name
;
573 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
574 if (has_empty_state(sm
))
578 param
= get_param_num_from_sym(sm
->sym
);
580 if (expr_to_sym(expr
) == sm
->sym
)
587 start_state
= get_state_stree(start_states
, my_id
, sm
->name
, sm
->sym
);
588 if (start_state
&& estates_equiv(sm
->state
, start_state
))
592 param_name
= get_param_name(sm
);
595 if (strcmp(param_name
, "$") == 0)
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
)
607 if (option_project
!= PROJ_KERNEL
)
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(¶m_set_to_user_data
);
640 void check_user_data3(int id
)
644 if (option_project
!= PROJ_KERNEL
)
646 select_caller_info_hook(set_called
, INTERNAL
);