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"
32 STATE(user_data_passed
);
40 int is_user_macro(struct expression
*expr
)
43 struct range_list
*rl
;
45 macro
= get_macro_name(expr
->pos
);
49 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
))
51 if (strcmp(macro
, "ntohl") == 0)
53 if (strcmp(macro
, "ntohs") == 0)
58 static int has_user_data_state(struct expression
*expr
, struct state_list
*my_slist
)
64 expr
= strip_expr(expr
);
65 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
66 expr
= strip_expr(expr
->unop
);
68 name
= expr_to_str_sym(expr
, &sym
);
73 FOR_EACH_PTR(my_slist
, sm
) {
76 } END_FOR_EACH_PTR(sm
);
80 static int passes_user_data(struct expression
*expr
)
82 struct state_list
*slist
;
83 struct expression
*arg
;
85 slist
= get_all_states(my_id
);
86 FOR_EACH_PTR(expr
->args
, arg
) {
87 if (is_user_data(arg
))
89 if (has_user_data_state(arg
, slist
))
91 } END_FOR_EACH_PTR(arg
);
96 static struct expression
*db_expr
;
97 static int db_user_data
;
98 static int db_user_data_callback(void *unused
, int argc
, char **argv
, char **azColName
)
100 if (atoi(argv
[0]) == PASSED_DATA
&& !passes_user_data(db_expr
))
106 static int is_user_fn_db(struct expression
*expr
)
109 static char sql_filter
[1024];
111 if (is_fake_call(expr
))
113 if (expr
->fn
->type
!= EXPR_SYMBOL
)
115 sym
= expr
->fn
->symbol
;
119 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
120 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
121 get_filename(), sym
->ident
->name
);
123 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
129 run_sql(db_user_data_callback
,
130 "select value from return_states where type=%d and parameter = -1 and key = '$$' and %s",
131 USER_DATA
, sql_filter
);
135 static int is_user_function(struct expression
*expr
)
137 if (expr
->type
!= EXPR_CALL
)
139 return is_user_fn_db(expr
);
142 static int is_skb_data(struct expression
*expr
)
149 name
= expr_to_var_sym(expr
, &sym
);
153 sym
= get_base_type(sym
);
154 if (!sym
|| sym
->type
!= SYM_PTR
)
156 sym
= get_base_type(sym
);
157 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
159 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
165 if (strcmp(name
+ len
- 6, "->data") == 0)
173 static int in_container_of_macro(struct expression
*expr
)
177 macro
= get_macro_name(expr
->pos
);
181 if (strcmp(macro
, "container_of") == 0)
186 static int is_user_data_state(struct expression
*expr
)
188 struct state_list
*slist
= NULL
;
189 struct sm_state
*tmp
;
194 tmp
= get_sm_state_expr(my_id
, expr
);
196 if (slist_has_state(tmp
->possible
, &user_data_set
))
198 if (slist_has_state(tmp
->possible
, &user_data_passed
))
203 name
= expr_to_str_sym(expr
, &sym
);
207 slist
= get_all_states(my_id
);
208 FOR_EACH_PTR(slist
, tmp
) {
211 if (!strncmp(tmp
->name
, name
, strlen(tmp
->name
))) {
212 if (slist_has_state(tmp
->possible
, &user_data_set
))
214 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
218 } END_FOR_EACH_PTR(tmp
);
226 int is_user_data(struct expression
*expr
)
235 if (in_container_of_macro(expr
))
238 user_data
= is_user_macro(expr
);
241 user_data
= is_user_function(expr
);
244 user_data
= is_skb_data(expr
);
248 expr
= strip_expr(expr
); /* this has to come after is_user_macro() */
250 if (expr
->type
== EXPR_BINOP
) {
251 user_data
= is_user_data(expr
->left
);
256 user_data
= is_user_data(expr
->right
);
261 if (expr
->type
== EXPR_PREOP
&& (expr
->op
== '&' || expr
->op
== '*'))
262 expr
= strip_expr(expr
->unop
);
264 return is_user_data_state(expr
);
267 int is_capped_user_data(struct expression
*expr
)
271 sm
= get_sm_state_expr(my_id
, expr
);
274 if (slist_has_state(sm
->possible
, &capped
))
279 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
283 /* sanity check. this should always be true. */
284 if (strncmp(key
, "$$", 2) != 0)
286 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
287 set_state(my_id
, fullname
, sym
, &user_data_passed
);
290 static void match_syscall_definition(struct symbol
*sym
)
295 macro
= get_macro_name(sym
->pos
);
298 if (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) != 0 &&
299 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) != 0)
302 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
303 set_state(my_id
, arg
->ident
->name
, arg
, &user_data_set
);
304 } END_FOR_EACH_PTR(arg
);
307 static void match_condition(struct expression
*expr
)
312 case SPECIAL_UNSIGNED_LT
:
313 case SPECIAL_UNSIGNED_LTE
:
314 if (is_user_data(expr
->left
))
315 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
316 if (is_user_data(expr
->right
))
317 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
321 case SPECIAL_UNSIGNED_GT
:
322 case SPECIAL_UNSIGNED_GTE
:
323 if (is_user_data(expr
->right
))
324 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
325 if (is_user_data(expr
->left
))
326 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
329 if (is_user_data(expr
->left
))
330 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
331 if (is_user_data(expr
->right
))
332 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
334 case SPECIAL_NOTEQUAL
:
335 if (is_user_data(expr
->left
))
336 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
337 if (is_user_data(expr
->right
))
338 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
345 static int handle_get_user(struct expression
*expr
)
350 name
= get_macro_name(expr
->pos
);
351 if (!name
|| strcmp(name
, "get_user") != 0)
354 name
= expr_to_var(expr
->right
);
355 if (!name
|| strcmp(name
, "__val_gu") != 0)
357 set_state_expr(my_id
, expr
->left
, &user_data_set
);
364 static void match_assign(struct expression
*expr
)
368 if (handle_get_user(expr
))
371 user_data
= is_user_data(expr
->right
);
372 if (user_data
== PASSED_DATA
)
373 set_state_expr(my_id
, expr
->left
, &user_data_passed
);
374 else if (user_data
== SET_DATA
)
375 set_state_expr(my_id
, expr
->left
, &user_data_set
);
376 else if (get_state_expr(my_id
, expr
->left
))
377 set_state_expr(my_id
, expr
->left
, &capped
);
380 static void tag_struct_members(struct symbol
*type
, struct expression
*expr
)
383 struct expression
*member
;
386 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
387 expr
= strip_expr(expr
->unop
);
391 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
394 member
= member_expression(expr
, op
, tmp
->ident
);
395 set_state_expr(my_id
, member
, &user_data_set
);
396 } END_FOR_EACH_PTR(tmp
);
399 static void tag_base_type(struct expression
*expr
)
401 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
402 expr
= strip_expr(expr
->unop
);
404 expr
= deref_expression(expr
);
405 set_state_expr(my_id
, expr
, &user_data_set
);
408 static void tag_as_user_data(struct expression
*expr
)
412 expr
= strip_expr(expr
);
414 type
= get_type(expr
);
415 if (!type
|| type
->type
!= SYM_PTR
)
417 type
= get_real_base_type(type
);
420 if (type
== &void_ctype
) {
421 set_state_expr(my_id
, deref_expression(expr
), &user_data_set
);
424 if (type
->type
== SYM_BASETYPE
)
426 if (type
->type
== SYM_STRUCT
) {
427 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
428 expr
= deref_expression(expr
);
429 tag_struct_members(type
, expr
);
433 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
435 int param
= PTR_INT(_param
);
436 struct expression
*dest
;
438 dest
= get_argument_from_call_expr(expr
->args
, param
);
439 dest
= strip_expr(dest
);
442 tag_as_user_data(dest
);
445 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
447 set_state_expr(my_id
, expr
->left
, &user_data_set
);
450 static void match_caller_info(struct expression
*expr
)
452 struct expression
*tmp
;
456 FOR_EACH_PTR(expr
->args
, tmp
) {
457 if (is_user_data(tmp
))
458 sql_insert_caller_info(expr
, USER_DATA
, i
, "$$", "");
460 } END_FOR_EACH_PTR(tmp
);
463 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
465 if (state
== &capped
)
467 sql_insert_caller_info(call
, USER_DATA
, param
, printed_name
, "");
470 static void returned_member_callback(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
)
472 if (state
== &capped
)
474 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
, -1, printed_name
, "");
477 static void print_returned_user_data(int return_id
, char *return_ranges
, struct expression
*expr
)
479 struct state_list
*my_slist
;
480 struct sm_state
*tmp
;
483 const char *passed_or_new
;
485 user_data
= is_user_data(expr
);
486 if (user_data
== PASSED_DATA
) {
487 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
490 if (user_data
== SET_DATA
) {
491 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
495 my_slist
= get_all_states(my_id
);
497 FOR_EACH_PTR(my_slist
, tmp
) {
498 const char *param_name
;
500 param
= get_param_num_from_sym(tmp
->sym
);
504 if (is_capped_var_sym(tmp
->name
, tmp
->sym
))
506 /* ignore states that were already USER_DATA to begin with */
507 if (get_state_slist(get_start_states(), my_id
, tmp
->name
, tmp
->sym
))
510 param_name
= get_param_name(tmp
);
511 if (!param_name
|| strcmp(param_name
, "$$") == 0)
514 if (slist_has_state(tmp
->possible
, &user_data_set
))
516 else if (slist_has_state(tmp
->possible
, &user_data_passed
))
521 sql_insert_return_states(return_id
, return_ranges
, USER_DATA
,
522 param
, param_name
, passed_or_new
);
523 } END_FOR_EACH_PTR(tmp
);
525 free_slist(&my_slist
);
528 static void db_return_states_userdata(struct expression
*expr
, int param
, char *key
, char *value
)
533 if (expr
->type
== EXPR_ASSIGNMENT
&& param
== -1 && strcmp(key
, "*$$") == 0) {
534 tag_as_user_data(expr
->left
);
538 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
542 set_state(my_id
, name
, sym
, &user_data_set
);
547 void check_user_data(int id
)
549 if (option_project
!= PROJ_KERNEL
)
552 select_caller_info_hook(set_param_user_data
, USER_DATA
);
553 add_hook(&match_syscall_definition
, FUNC_DEF_HOOK
);
554 add_hook(&match_condition
, CONDITION_HOOK
);
555 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
556 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
557 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
558 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
559 add_function_assign_hook("kmemdup_user", &match_user_assign_function
, NULL
);
561 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
562 add_member_info_callback(my_id
, struct_member_callback
);
563 add_returned_member_callback(my_id
, returned_member_callback
);
564 add_split_return_callback(print_returned_user_data
);
565 select_return_states_hook(USER_DATA
, &db_return_states_userdata
);