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 name
= expr_to_var_sym(expr
, &sym
);
194 snprintf(buf
, sizeof(buf
), "*%s", name
);
195 state
= get_state(my_id
, buf
, sym
);
196 if (state
&& estate_rl(state
))
203 static int is_skb_data(struct expression
*expr
)
207 expr
= strip_expr(expr
);
208 if (!expr
|| expr
->type
!= EXPR_DEREF
)
213 if (strcmp(expr
->member
->name
, "data") != 0)
216 sym
= expr_to_sym(expr
->deref
);
219 sym
= get_real_base_type(sym
);
220 if (!sym
|| sym
->type
!= SYM_PTR
)
222 sym
= get_real_base_type(sym
);
223 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
225 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
231 static int comes_from_skb_data(struct expression
*expr
)
233 expr
= strip_expr(expr
);
237 switch (expr
->type
) {
239 if (comes_from_skb_data(expr
->left
))
241 if (comes_from_skb_data(expr
->right
))
245 return comes_from_skb_data(expr
->unop
);
247 if (is_skb_data(expr
))
249 return comes_from_skb_data(expr
->deref
);
256 static int handle_struct_assignment(struct expression
*expr
)
258 struct expression
*right
;
261 type
= get_type(expr
->left
);
262 if (!type
|| type
->type
!= SYM_PTR
)
264 type
= get_real_base_type(type
);
265 if (!type
|| type
->type
!= SYM_STRUCT
)
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
)
276 type
= get_real_base_type(type
);
277 if (type
&& type
->type
== SYM_STRUCT
)
280 if (!points_to_user_data(right
) && !is_skb_data(right
))
283 tag_as_user_data(expr
->left
);
287 static int handle_get_user(struct expression
*expr
)
292 name
= get_macro_name(expr
->pos
);
293 if (!name
|| strcmp(name
, "get_user") != 0)
296 name
= expr_to_var(expr
->right
);
297 if (!name
|| strcmp(name
, "__val_gu") != 0)
299 set_state_expr(my_id
, expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
306 static void match_assign(struct expression
*expr
)
308 struct range_list
*rl
;
310 if (is_fake_call(expr
->right
))
312 if (handle_struct_assignment(expr
))
314 if (handle_get_user(expr
))
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
));
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
)
342 macro
= get_macro_name(expr
->pos
);
347 if (strcmp(macro
, "ntohl") == 0) {
348 *rl
= alloc_whole_rl(&uint_ctype
);
351 if (strcmp(macro
, "ntohs") == 0) {
352 *rl
= alloc_whole_rl(&ushort_ctype
);
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
))
368 if (comes_from_skb_data(expr
)) {
369 rl
= alloc_whole_rl(get_type(expr
));
373 state
= get_state_expr(my_id
, expr
);
374 if (state
&& estate_rl(state
)) {
375 rl
= estate_rl(state
);
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
)
390 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
391 if (!user_data_flag
) {
398 static void match_call_info(struct expression
*expr
)
400 struct range_list
*rl
;
401 struct expression
*arg
;
405 FOR_EACH_PTR(expr
->args
, arg
) {
408 if (!get_user_rl(arg
, &rl
))
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)
423 state
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
424 if (!state
|| !estate_rl(state
))
425 rl
= estate_rl(sm
->state
);
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
;
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);
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
);
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
)
472 macro
= get_macro_name(sym
->pos
);
474 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
475 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
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)
484 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
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
;
501 struct range_list
*rl
= NULL
;
503 while (expr
->type
== EXPR_ASSIGNMENT
)
504 expr
= strip_expr(expr
->right
);
505 if (expr
->type
!= EXPR_CALL
)
508 arg
= get_argument_from_call_expr(expr
->args
, param
);
511 type
= get_member_type_from_key(arg
, key
);
512 name
= get_variable_from_key(arg
, key
, &sym
);
516 call_results_to_rl(expr
, type
, value
, &rl
);
518 set_state(my_id
, name
, sym
, alloc_estate_rl(rl
));
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
))
530 } END_FOR_EACH_PTR(tmp
);
535 static void param_set_to_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
538 struct smatch_state
*start_state
;
540 const char *param_name
;
542 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
543 if (has_empty_state(sm
))
547 param
= get_param_num_from_sym(sm
->sym
);
551 start_state
= get_state_stree(start_states
, my_id
, sm
->name
, sm
->sym
);
552 if (start_state
&& estates_equiv(sm
->state
, start_state
))
556 param_name
= get_param_name(sm
);
559 if (strcmp(param_name
, "$") == 0)
562 sql_insert_return_states(return_id
, return_ranges
, USER_DATA3
,
563 param
, param_name
, show_rl(estate_rl(sm
->state
)));
564 } END_FOR_EACH_SM(sm
);
567 void check_user_data2(int id
)
571 if (option_project
!= PROJ_KERNEL
)
574 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
575 add_hook(&free_start_states
, END_FUNC_HOOK
);
576 add_hook(&match_save_states
, INLINE_FN_START
);
577 add_hook(&match_restore_states
, INLINE_FN_END
);
579 add_unmatched_state_hook(my_id
, &empty_state
);
580 add_merge_hook(my_id
, &merge_estates
);
582 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
583 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
584 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
585 add_function_hook("_kstrtoull", &match_user_copy
, INT_PTR(2));
587 add_function_hook("sscanf", &match_sscanf
, NULL
);
589 add_function_assign_hook("kmemdup_user", &match_user_assign_function
, NULL
);
590 add_function_assign_hook("kmap_atomic", &match_user_assign_function
, NULL
);
592 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
594 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
596 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
597 add_member_info_callback(my_id
, struct_member_callback
);
598 select_caller_info_hook(set_param_user_data
, USER_DATA3
);
599 select_return_states_hook(USER_DATA3
, &returns_param_user_data
);
600 add_split_return_callback(¶m_set_to_user_data
);
603 void check_user_data3(int id
)
607 if (option_project
!= PROJ_KERNEL
)
609 select_caller_info_hook(set_called
, INTERNAL
);