type: make sval_type_max() default to "long long"
[smatch.git] / check_user_data2.c
blob0e21ba3f5c296984a27e2717e66b6b7382903f56
1 /*
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.
25 #include "smatch.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_id;
30 static int my_call_id;
32 STATE(called);
34 static struct smatch_state *empty_state(struct sm_state *sm)
36 return alloc_estate_empty();
39 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
41 struct expression *edge_member;
42 struct symbol *base = get_real_base_type(member);
43 struct symbol *tmp;
45 if (member->ident)
46 expr = member_expression(expr, '.', member->ident);
48 FOR_EACH_PTR(base->symbol_list, tmp) {
49 struct symbol *type;
51 type = get_real_base_type(tmp);
52 if (!type)
53 continue;
55 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
56 tag_inner_struct_members(expr, tmp);
57 continue;
60 if (!tmp->ident)
61 continue;
63 edge_member = member_expression(expr, '.', tmp->ident);
64 set_state_expr(my_id, edge_member, alloc_estate_whole(type));
65 } END_FOR_EACH_PTR(tmp);
70 static void tag_struct_members(struct symbol *type, struct expression *expr)
72 struct symbol *tmp;
73 struct expression *member;
74 int op = '*';
76 if (expr->type == EXPR_PREOP && expr->op == '&') {
77 expr = strip_expr(expr->unop);
78 op = '.';
81 FOR_EACH_PTR(type->symbol_list, tmp) {
82 type = get_real_base_type(tmp);
83 if (!type)
84 continue;
86 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
87 tag_inner_struct_members(expr, tmp);
88 continue;
91 if (!tmp->ident)
92 continue;
94 member = member_expression(expr, op, tmp->ident);
95 set_state_expr(my_id, member, alloc_estate_whole(get_type(member)));
96 } END_FOR_EACH_PTR(tmp);
99 static void tag_base_type(struct expression *expr)
101 if (expr->type == EXPR_PREOP && expr->op == '&')
102 expr = strip_expr(expr->unop);
103 else
104 expr = deref_expression(expr);
105 set_state_expr(my_id, expr, alloc_estate_whole(get_type(expr)));
108 static void tag_as_user_data(struct expression *expr)
110 struct symbol *type;
112 expr = strip_expr(expr);
114 type = get_type(expr);
115 if (!type || type->type != SYM_PTR)
116 return;
117 type = get_real_base_type(type);
118 if (!type)
119 return;
120 if (type == &void_ctype) {
121 set_state_expr(my_id, deref_expression(expr), alloc_estate_whole(&ulong_ctype));
122 return;
124 if (type->type == SYM_BASETYPE)
125 tag_base_type(expr);
126 if (type->type == SYM_STRUCT) {
127 if (expr->type != EXPR_PREOP || expr->op != '&')
128 expr = deref_expression(expr);
129 tag_struct_members(type, expr);
133 static void match_user_copy(const char *fn, struct expression *expr, void *_param)
135 int param = PTR_INT(_param);
136 struct expression *dest;
138 dest = get_argument_from_call_expr(expr->args, param);
139 dest = strip_expr(dest);
140 if (!dest)
141 return;
142 tag_as_user_data(dest);
145 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
147 struct expression *arg;
148 int i;
150 i = -1;
151 FOR_EACH_PTR(expr->args, arg) {
152 i++;
153 if (i < 2)
154 continue;
155 tag_as_user_data(arg);
156 } END_FOR_EACH_PTR(arg);
159 static int points_to_user_data(struct expression *expr)
161 struct smatch_state *state;
162 char buf[256];
163 struct symbol *sym;
164 char *name;
165 int ret = 0;
167 name = expr_to_var_sym(expr, &sym);
168 if (!name || !sym)
169 goto free;
170 snprintf(buf, sizeof(buf), "*%s", name);
171 state = get_state(my_id, buf, sym);
172 if (state && estate_rl(state))
173 ret = 1;
174 free:
175 free_string(name);
176 return ret;
179 static int is_skb_data(struct expression *expr)
181 struct symbol *sym;
183 expr = strip_expr(expr);
184 if (!expr || expr->type != EXPR_DEREF)
185 return 0;
187 if (!expr->member)
188 return 0;
189 if (strcmp(expr->member->name, "data") != 0)
190 return 0;
192 sym = expr_to_sym(expr->deref);
193 if (!sym)
194 return 0;
195 sym = get_real_base_type(sym);
196 if (!sym || sym->type != SYM_PTR)
197 return 0;
198 sym = get_real_base_type(sym);
199 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
200 return 0;
201 if (strcmp(sym->ident->name, "sk_buff") != 0)
202 return 0;
204 return 1;
207 static int comes_from_skb_data(struct expression *expr)
209 expr = strip_expr(expr);
210 if (!expr)
211 return 0;
213 switch (expr->type) {
214 case EXPR_BINOP:
215 if (comes_from_skb_data(expr->left))
216 return 1;
217 if (comes_from_skb_data(expr->right))
218 return 1;
219 return 0;
220 case EXPR_PREOP:
221 return comes_from_skb_data(expr->unop);
222 case EXPR_DEREF:
223 if (is_skb_data(expr))
224 return 1;
225 return comes_from_skb_data(expr->deref);
226 default:
227 return 0;
232 static int handle_struct_assignment(struct expression *expr)
234 struct expression *right;
235 struct symbol *type;
237 type = get_type(expr->left);
238 if (!type || type->type != SYM_PTR)
239 return 0;
240 type = get_real_base_type(type);
241 if (!type || type->type != SYM_STRUCT)
242 return 0;
245 * Ignore struct to struct assignments because for those we look at the
246 * individual members.
248 right = strip_expr(expr->right);
249 type = get_type(right);
250 if (!type || type->type != SYM_PTR)
251 return 0;
252 type = get_real_base_type(type);
253 if (type && type->type == SYM_STRUCT)
254 return 0;
256 if (!points_to_user_data(right) && !is_skb_data(right))
257 return 0;
259 tag_as_user_data(expr->left);
260 return 1;
263 static int handle_get_user(struct expression *expr)
265 char *name;
266 int ret = 0;
268 name = get_macro_name(expr->pos);
269 if (!name || strcmp(name, "get_user") != 0)
270 return 0;
272 name = expr_to_var(expr->right);
273 if (!name || strcmp(name, "__val_gu") != 0)
274 goto free;
275 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
276 ret = 1;
277 free:
278 free_string(name);
279 return ret;
282 static void match_assign(struct expression *expr)
284 struct range_list *rl;
286 if (is_fake_call(expr->right))
287 return;
288 if (handle_struct_assignment(expr))
289 return;
290 if (handle_get_user(expr))
291 return;
293 if (expr->right->type == EXPR_CALL ||
294 !get_user_rl(expr->right, &rl))
295 goto clear_old_state;
297 rl = cast_rl(get_type(expr->left), rl);
298 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
300 return;
302 clear_old_state:
303 if (get_state_expr(my_id, expr->left))
304 set_state_expr(my_id, expr->left, alloc_estate_empty());
307 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
309 tag_as_user_data(expr->left);
312 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
314 char *macro;
316 if (!expr)
317 return 0;
318 macro = get_macro_name(expr->pos);
320 if (!macro)
321 return 0;
323 if (strcmp(macro, "ntohl") == 0) {
324 *rl = alloc_whole_rl(&uint_ctype);
325 return 1;
327 if (strcmp(macro, "ntohs") == 0) {
328 *rl = alloc_whole_rl(&ushort_ctype);
329 return 1;
331 return 0;
334 static int user_data_flag;
335 static struct range_list *var_user_rl(struct expression *expr)
337 struct smatch_state *state;
338 struct range_list *rl;
339 struct range_list *absolute_rl;
341 if (get_user_macro_rl(expr, &rl))
342 goto found;
344 if (comes_from_skb_data(expr)) {
345 rl = alloc_whole_rl(get_type(expr));
346 goto found;
349 state = get_state_expr(my_id, expr);
350 if (state && estate_rl(state)) {
351 rl = estate_rl(state);
352 goto found;
355 return NULL;
356 found:
357 user_data_flag = 1;
358 absolute_rl = var_to_absolute_rl(expr);
359 return clone_rl(rl_intersection(rl, absolute_rl));
362 int get_user_rl(struct expression *expr, struct range_list **rl)
365 user_data_flag = 0;
366 custom_get_absolute_rl(expr, &var_user_rl, rl);
367 if (!user_data_flag) {
368 *rl = NULL;
369 return 0;
371 return 1;
374 static void match_call_info(struct expression *expr)
376 struct range_list *rl;
377 struct expression *arg;
378 int i = 0;
380 i = -1;
381 FOR_EACH_PTR(expr->args, arg) {
382 i++;
384 if (!get_user_rl(arg, &rl))
385 continue;
387 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
388 } END_FOR_EACH_PTR(arg);
391 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
393 struct smatch_state *state;
394 struct range_list *rl;
396 if (strcmp(sm->state->name, "") == 0)
397 return;
399 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
400 if (!state || !estate_rl(state))
401 rl = estate_rl(sm->state);
402 else
403 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
405 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
408 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
410 struct range_list *rl = NULL;
411 struct smatch_state *state;
412 struct symbol *type;
413 char fullname[256];
415 if (strcmp(key, "*$") == 0)
416 snprintf(fullname, sizeof(fullname), "*%s", name);
417 else if (strncmp(key, "$", 1) == 0)
418 snprintf(fullname, 256, "%s%s", name, key + 1);
419 else
420 return;
422 type = get_member_type_from_key(symbol_expression(sym), key);
424 /* if the caller passes a void pointer with user data */
425 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
426 struct expression *expr = symbol_expression(sym);
428 tag_as_user_data(expr);
429 return;
431 str_to_rl(type, value, &rl);
432 state = alloc_estate_rl(rl);
433 set_state(my_id, fullname, sym, state);
436 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
438 set_state(my_call_id, "this_function", NULL, &called);
441 static void match_syscall_definition(struct symbol *sym)
443 struct symbol *arg;
444 char *macro;
445 char *name;
446 int is_syscall = 0;
448 macro = get_macro_name(sym->pos);
449 if (macro &&
450 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
451 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
452 is_syscall = 1;
454 name = get_function();
455 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
456 if (name && strncmp(name, "sys_", 4) == 0)
457 is_syscall = 1;
460 if (name && strncmp(name, "compat_sys_", 11) == 0)
461 is_syscall = 1;
463 if (!is_syscall)
464 return;
466 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
467 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
468 } END_FOR_EACH_PTR(arg);
471 void check_user_data2(int id)
473 my_id = id;
475 if (option_project != PROJ_KERNEL)
476 return;
478 add_unmatched_state_hook(my_id, &empty_state);
479 add_merge_hook(my_id, &merge_estates);
481 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
482 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
483 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
484 add_function_hook("_kstrtoull", &match_user_copy, INT_PTR(2));
486 add_function_hook("sscanf", &match_sscanf, NULL);
488 add_function_assign_hook("kmemdup_user", &match_user_assign_function, NULL);
489 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
491 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
493 add_hook(&match_assign, ASSIGNMENT_HOOK);
495 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
496 add_member_info_callback(my_id, struct_member_callback);
497 select_caller_info_hook(set_param_user_data, USER_DATA3);
500 void check_user_data3(int id)
502 my_call_id = id;
504 if (option_project != PROJ_KERNEL)
505 return;
506 select_caller_info_hook(set_called, INTERNAL);