2 * smatch/check_user_data.c
4 * Copyright (C) 2011 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * There are a couple checks that try to see if a variable
12 * comes from the user. It would be better to unify them
13 * into one place. Also it we should follow the data down
14 * the call paths. Hence this file.
18 #include "smatch_slist.h"
25 int is_user_data(struct expression
*expr
)
27 struct state_list
*slist
= NULL
;
33 expr
= strip_expr(expr
);
38 if (expr
->type
== EXPR_BINOP
) {
39 if (is_user_data(expr
->left
))
41 if (is_user_data(expr
->right
))
45 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
46 expr
= strip_expr(expr
->unop
);
48 tmp
= get_sm_state_expr(my_id
, expr
);
50 return slist_has_state(tmp
->possible
, &user_data
);
52 name
= get_variable_from_expr_complex(expr
, &sym
);
56 slist
= get_all_states(my_id
);
57 FOR_EACH_PTR(slist
, tmp
) {
60 if (!strncmp(tmp
->name
, name
, strlen(tmp
->name
))) {
61 if (slist_has_state(tmp
->possible
, &user_data
))
65 } END_FOR_EACH_PTR(tmp
);
73 void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
77 if (strncmp(key
, "$$", 2))
79 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
80 set_state(my_id
, fullname
, sym
, &user_data
);
83 static void match_condition(struct expression
*expr
)
88 case SPECIAL_UNSIGNED_LT
:
89 case SPECIAL_UNSIGNED_LTE
:
90 if (is_user_data(expr
->left
))
91 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
92 if (is_user_data(expr
->right
))
93 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
97 case SPECIAL_UNSIGNED_GT
:
98 case SPECIAL_UNSIGNED_GTE
:
99 if (is_user_data(expr
->right
))
100 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
101 if (is_user_data(expr
->left
))
102 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
105 if (is_user_data(expr
->left
))
106 set_true_false_states_expr(my_id
, expr
->left
, &capped
, NULL
);
107 if (is_user_data(expr
->right
))
108 set_true_false_states_expr(my_id
, expr
->right
, &capped
, NULL
);
110 case SPECIAL_NOTEQUAL
:
111 if (is_user_data(expr
->left
))
112 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &capped
);
113 if (is_user_data(expr
->right
))
114 set_true_false_states_expr(my_id
, expr
->right
, NULL
, &capped
);
122 static void match_normal_assign(struct expression
*expr
)
124 if (is_user_data(expr
->left
))
125 set_state_expr(my_id
, expr
->left
, &capped
);
128 static void match_assign(struct expression
*expr
)
132 name
= get_macro_name(expr
->pos
);
133 if (!name
|| strcmp(name
, "get_user") != 0) {
134 match_normal_assign(expr
);
137 name
= get_variable_from_expr(expr
->right
, NULL
);
138 if (!name
|| strcmp(name
, "__val_gu") != 0)
140 set_state_expr(my_id
, expr
->left
, &user_data
);
145 static void match_user_copy(const char *fn
, struct expression
*expr
, void *_param
)
147 int param
= PTR_INT(_param
);
148 struct expression
*dest
;
150 dest
= get_argument_from_call_expr(expr
->args
, param
);
151 dest
= strip_expr(dest
);
154 /* the first thing I tested this on pass &foo to a function */
155 set_state_expr(my_id
, dest
, &user_data
);
156 if (dest
->type
== EXPR_PREOP
&& dest
->op
== '&') {
157 /* but normally I'd think it would pass the actual variable */
159 set_state_expr(my_id
, dest
, &user_data
);
163 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
165 set_state_expr(my_id
, expr
->left
, &user_data
);
168 static void match_assign_userdata(struct expression
*expr
)
170 if (is_user_data(expr
->right
))
171 set_state_expr(my_id
, expr
->left
, &user_data
);
174 static void match_caller_info(struct expression
*expr
)
176 struct expression
*tmp
;
180 func
= get_fnptr_name(expr
->fn
);
185 FOR_EACH_PTR(expr
->args
, tmp
) {
186 if (is_user_data(tmp
))
187 sm_msg("info: passes user_data %s %d '$$' %s", func
, i
,
188 is_static(expr
->fn
) ? "static" : "global");
190 } END_FOR_EACH_PTR(tmp
);
193 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
195 if (state
== &capped
)
197 sm_msg("info: passes user_data '%s' %d '%s' %s", fn
, param
, printed_name
, global_static
);
200 static void match_return(struct expression
*expr
)
202 if (is_user_data(expr
))
203 sm_msg("info: returns_user_data %s", global_static());
206 static int db_user_data
;
207 static int db_user_data_callback(void *unused
, int argc
, char **argv
, char **azColName
)
213 static int passes_user_data(struct expression
*expr
)
215 struct expression
*arg
;
217 FOR_EACH_PTR(expr
->args
, arg
) {
218 if (is_user_data(arg
))
220 } END_FOR_EACH_PTR(arg
);
225 static void match_call_assignment(struct expression
*expr
)
228 static char sql_filter
[1024];
230 if (expr
->right
->fn
->type
!= EXPR_SYMBOL
)
232 sym
= expr
->right
->fn
->symbol
;
236 if (!passes_user_data(expr
->right
))
239 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
240 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s' and type = %d;",
241 get_filename(), sym
->ident
->name
, USER_DATA
);
243 snprintf(sql_filter
, 1024, "function = '%s' and static = 0 and type = %d;",
244 sym
->ident
->name
, USER_DATA
);
248 run_sql(db_user_data_callback
, "select value from return_info where %s",
251 set_state_expr(my_id
, expr
->left
, &user_data
);
254 void check_user_data(int id
)
256 if (option_project
!= PROJ_KERNEL
)
259 add_definition_db_callback(set_param_user_data
, USER_DATA
);
260 add_hook(match_call_assignment
, CALL_ASSIGNMENT_HOOK
);
261 add_hook(&match_condition
, CONDITION_HOOK
);
262 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
263 add_hook(&match_assign_userdata
, ASSIGNMENT_HOOK
);
264 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
265 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
266 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
267 add_function_assign_hook("kmemdup_user", &match_user_assign_function
, NULL
);
269 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
270 add_member_info_callback(my_id
, struct_member_callback
);
271 add_hook(&match_return
, RETURN_HOOK
);