sval: make sval_cast() take a type
[smatch.git] / check_user_data.c
blob978ba471fac528a80b88aea6903681c7c4d2e9d3
1 /*
2 * smatch/check_user_data.c
4 * Copyright (C) 2011 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
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.
17 #include "smatch.h"
18 #include "smatch_slist.h"
19 #include "smatch_extra.h"
21 static int my_id;
23 STATE(capped);
24 STATE(user_data);
26 static int is_user_macro(struct expression *expr)
28 return 0;
31 static int db_user_data;
32 static int db_user_data_callback(void *unused, int argc, char **argv, char **azColName)
34 db_user_data = 1;
35 return 0;
38 static int passes_user_data(struct expression *expr)
40 struct expression *arg;
42 FOR_EACH_PTR(expr->args, arg) {
43 if (is_user_data(arg))
44 return 1;
45 } END_FOR_EACH_PTR(arg);
47 return 0;
50 static int is_user_fn_db(struct expression *expr)
52 struct symbol *sym;
53 static char sql_filter[1024];
55 if (expr->fn->type != EXPR_SYMBOL)
56 return 0;
57 sym = expr->fn->symbol;
58 if (!sym)
59 return 0;
61 if (!passes_user_data(expr))
62 return 0;
64 if (sym->ctype.modifiers & MOD_STATIC) {
65 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
66 get_filename(), sym->ident->name);
67 } else {
68 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
69 sym->ident->name);
72 db_user_data = 0;
73 run_sql(db_user_data_callback, "select value from return_states where type=%d and %s",
74 USER_DATA, sql_filter);
75 return db_user_data;
78 static int is_user_function(struct expression *expr)
80 if (expr->type != EXPR_CALL)
81 return 0;
82 if (sym_name_is("kmemdup_user", expr->fn))
83 return 1;
84 return is_user_fn_db(expr);
87 static int is_skb_data(struct expression *expr)
89 struct symbol *sym;
90 char *name;
91 int len;
92 int ret = 0;
94 name = get_variable_from_expr(expr, &sym);
95 if (!name || !sym)
96 goto free;
98 sym = get_base_type(sym);
99 if (!sym || sym->type != SYM_PTR)
100 goto free;
101 sym = get_base_type(sym);
102 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
103 goto free;
104 if (strcmp(sym->ident->name, "sk_buff") != 0)
105 goto free;
107 len = strlen(name);
108 if (len < 6)
109 goto free;
110 if (strcmp(name + len - 6, "->data") == 0)
111 ret = 1;
113 free:
114 free_string(name);
115 return ret;
118 int is_user_data(struct expression *expr)
120 struct state_list *slist = NULL;
121 struct sm_state *tmp;
122 struct symbol *sym;
123 char *name;
124 int user = 0;
126 if (!expr)
127 return 0;
129 if (is_capped(expr))
130 return 0;
132 if (is_user_macro(expr))
133 return 1;
134 if (is_user_function(expr))
135 return 1;
136 if (is_skb_data(expr))
137 return 1;
139 expr = strip_expr(expr); /* this has to come after is_user_macro() */
141 if (expr->type == EXPR_BINOP) {
142 if (is_user_data(expr->left))
143 return 1;
144 if (is_user_data(expr->right))
145 return 1;
146 return 0;
148 if (expr->type == EXPR_PREOP && (expr->op == '&' || expr->op == '*'))
149 expr = strip_expr(expr->unop);
151 tmp = get_sm_state_expr(my_id, expr);
152 if (tmp)
153 return slist_has_state(tmp->possible, &user_data);
155 name = get_variable_from_expr_complex(expr, &sym);
156 if (!name || !sym)
157 goto free;
159 slist = get_all_states(my_id);
160 FOR_EACH_PTR(slist, tmp) {
161 if (tmp->sym != sym)
162 continue;
163 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
164 if (slist_has_state(tmp->possible, &user_data))
165 user = 1;
166 goto free;
168 } END_FOR_EACH_PTR(tmp);
170 free:
171 free_slist(&slist);
172 free_string(name);
173 return user;
176 void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
178 char fullname[256];
180 if (strncmp(key, "$$", 2))
181 return;
182 snprintf(fullname, 256, "%s%s", name, key + 2);
183 set_state(my_id, fullname, sym, &user_data);
186 static void match_syscall_definition(struct symbol *sym)
188 struct symbol *arg;
189 char *macro;
191 macro = get_macro_name(sym->pos);
192 if (!macro)
193 return;
194 if (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")))
195 return;
197 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
198 set_state(my_id, arg->ident->name, arg, &user_data);
199 } END_FOR_EACH_PTR(arg);
202 static void match_condition(struct expression *expr)
204 switch (expr->op) {
205 case '<':
206 case SPECIAL_LTE:
207 case SPECIAL_UNSIGNED_LT:
208 case SPECIAL_UNSIGNED_LTE:
209 if (is_user_data(expr->left))
210 set_true_false_states_expr(my_id, expr->left, &capped, NULL);
211 if (is_user_data(expr->right))
212 set_true_false_states_expr(my_id, expr->right, NULL, &capped);
213 break;
214 case '>':
215 case SPECIAL_GTE:
216 case SPECIAL_UNSIGNED_GT:
217 case SPECIAL_UNSIGNED_GTE:
218 if (is_user_data(expr->right))
219 set_true_false_states_expr(my_id, expr->right, &capped, NULL);
220 if (is_user_data(expr->left))
221 set_true_false_states_expr(my_id, expr->left, NULL, &capped);
222 break;
223 case SPECIAL_EQUAL:
224 if (is_user_data(expr->left))
225 set_true_false_states_expr(my_id, expr->left, &capped, NULL);
226 if (is_user_data(expr->right))
227 set_true_false_states_expr(my_id, expr->right, &capped, NULL);
228 break;
229 case SPECIAL_NOTEQUAL:
230 if (is_user_data(expr->left))
231 set_true_false_states_expr(my_id, expr->left, NULL, &capped);
232 if (is_user_data(expr->right))
233 set_true_false_states_expr(my_id, expr->right, NULL, &capped);
234 break;
235 default:
236 return;
240 static void match_normal_assign(struct expression *expr)
242 if (is_user_data(expr->left))
243 set_state_expr(my_id, expr->left, &capped);
246 static void match_assign(struct expression *expr)
248 char *name;
250 name = get_macro_name(expr->pos);
251 if (!name || strcmp(name, "get_user") != 0) {
252 match_normal_assign(expr);
253 return;
255 name = get_variable_from_expr(expr->right, NULL);
256 if (!name || strcmp(name, "__val_gu") != 0)
257 goto free;
258 set_state_expr(my_id, expr->left, &user_data);
259 free:
260 free_string(name);
263 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
265 int param = PTR_INT(_param);
266 struct expression *dest;
268 dest = get_argument_from_call_expr(expr->args, param);
269 dest = strip_expr(dest);
270 if (!dest)
271 return;
272 /* the first thing I tested this on pass &foo to a function */
273 set_state_expr(my_id, dest, &user_data);
274 if (dest->type == EXPR_PREOP && dest->op == '&') {
275 /* but normally I'd think it would pass the actual variable */
276 dest = dest->unop;
277 set_state_expr(my_id, dest, &user_data);
281 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
283 set_state_expr(my_id, expr->left, &user_data);
286 static void match_assign_userdata(struct expression *expr)
288 if (is_user_data(expr->right))
289 set_state_expr(my_id, expr->left, &user_data);
292 static void match_caller_info(struct expression *expr)
294 struct expression *tmp;
295 char *func;
296 int i;
298 func = get_fnptr_name(expr->fn);
299 if (!func)
300 return;
302 i = 0;
303 FOR_EACH_PTR(expr->args, tmp) {
304 if (is_user_data(tmp))
305 sm_msg("info: passes user_data '%s' %d '$$' %s", func, i,
306 is_static(expr->fn) ? "static" : "global");
307 i++;
308 } END_FOR_EACH_PTR(tmp);
311 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
313 if (state == &capped)
314 return;
315 sm_msg("info: passes user_data '%s' %d '%s' %s", fn, param, printed_name, global_static);
318 static void match_return(struct expression *expr)
320 if (is_user_data(expr)) {
321 struct range_list_sval *rl;
323 get_implied_range_list_sval(expr, &rl);
324 sm_msg("info: returns_user_data %d '%s' %s",
325 get_return_id(), show_ranges_sval(rl), global_static());
329 void check_user_data(int id)
331 if (option_project != PROJ_KERNEL)
332 return;
333 my_id = id;
334 add_definition_db_callback(set_param_user_data, USER_DATA);
335 add_hook(&match_syscall_definition, FUNC_DEF_HOOK);
336 add_hook(&match_condition, CONDITION_HOOK);
337 add_hook(&match_assign, ASSIGNMENT_HOOK);
338 add_hook(&match_assign_userdata, ASSIGNMENT_HOOK);
339 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
340 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
341 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
342 add_function_assign_hook("kmemdup_user", &match_user_assign_function, NULL);
343 if (option_info) {
344 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
345 add_member_info_callback(my_id, struct_member_callback);
346 add_hook(&match_return, RETURN_HOOK);