comparison: handle preops like "if (++a == b)"
[smatch.git] / check_user_data2.c
blobf9ccb8cf55e517c53adfb7435954f9c2e654900a
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 int points_to_user_data(struct expression *expr)
147 struct smatch_state *state;
148 char buf[256];
149 struct symbol *sym;
150 char *name;
151 int ret = 0;
153 name = expr_to_var_sym(expr, &sym);
154 if (!name || !sym)
155 goto free;
156 snprintf(buf, sizeof(buf), "*%s", name);
157 state = get_state(my_id, buf, sym);
158 if (state && estate_rl(state))
159 ret = 1;
160 free:
161 free_string(name);
162 return ret;
165 static int is_skb_data(struct expression *expr)
167 struct symbol *sym;
169 expr = strip_expr(expr);
170 if (!expr || expr->type != EXPR_DEREF)
171 return 0;
173 if (!expr->member)
174 return 0;
175 if (strcmp(expr->member->name, "data") != 0)
176 return 0;
178 sym = expr_to_sym(expr->deref);
179 if (!sym)
180 return 0;
181 sym = get_real_base_type(sym);
182 if (!sym || sym->type != SYM_PTR)
183 return 0;
184 sym = get_real_base_type(sym);
185 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
186 return 0;
187 if (strcmp(sym->ident->name, "sk_buff") != 0)
188 return 0;
190 return 1;
193 static int comes_from_skb_data(struct expression *expr)
195 expr = strip_expr(expr);
196 if (!expr)
197 return 0;
199 return 0;
200 switch (expr->type) {
201 case EXPR_BINOP:
202 if (comes_from_skb_data(expr->left))
203 return 1;
204 if (comes_from_skb_data(expr->right))
205 return 1;
206 return 0;
207 case EXPR_PREOP:
208 return comes_from_skb_data(expr->unop);
209 case EXPR_DEREF:
210 if (is_skb_data(expr))
211 return 1;
212 return comes_from_skb_data(expr->deref);
213 default:
214 return 0;
219 static int handle_struct_assignment(struct expression *expr)
221 struct expression *right;
222 struct symbol *type;
224 type = get_type(expr->left);
225 if (!type || type->type != SYM_PTR)
226 return 0;
227 type = get_real_base_type(type);
228 if (!type || type->type != SYM_STRUCT)
229 return 0;
232 * Ignore struct to struct assignments because for those we look at the
233 * individual members.
235 right = strip_expr(expr->right);
236 type = get_type(right);
237 if (!type || type->type != SYM_PTR)
238 return 0;
239 type = get_real_base_type(type);
240 if (type && type->type == SYM_STRUCT)
241 return 0;
243 if (!points_to_user_data(right) && !is_skb_data(right))
244 return 0;
246 tag_as_user_data(expr->left);
247 return 1;
250 static int handle_get_user(struct expression *expr)
252 char *name;
253 int ret = 0;
255 name = get_macro_name(expr->pos);
256 if (!name || strcmp(name, "get_user") != 0)
257 return 0;
259 name = expr_to_var(expr->right);
260 if (!name || strcmp(name, "__val_gu") != 0)
261 goto free;
262 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
263 ret = 1;
264 free:
265 free_string(name);
266 return ret;
269 static void match_assign(struct expression *expr)
271 struct range_list *rl;
273 if (is_fake_call(expr->right))
274 return;
275 if (handle_struct_assignment(expr))
276 return;
277 if (handle_get_user(expr))
278 return;
280 if (expr->right->type == EXPR_CALL ||
281 !get_user_rl(expr->right, &rl))
282 goto clear_old_state;
284 rl = cast_rl(get_type(expr->left), rl);
285 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
287 return;
289 clear_old_state:
290 if (get_state_expr(my_id, expr->left))
291 set_state_expr(my_id, expr->left, alloc_estate_empty());
294 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
296 tag_as_user_data(expr->left);
299 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
301 char *macro;
303 if (!expr)
304 return 0;
305 macro = get_macro_name(expr->pos);
307 if (!macro)
308 return 0;
310 if (strcmp(macro, "ntohl") == 0) {
311 *rl = alloc_whole_rl(&uint_ctype);
312 return 1;
314 if (strcmp(macro, "ntohs") == 0) {
315 *rl = alloc_whole_rl(&ushort_ctype);
316 return 1;
318 return 0;
321 static int user_data_flag;
322 static struct range_list *var_user_rl(struct expression *expr)
324 struct smatch_state *state;
325 struct range_list *rl;
326 struct range_list *absolute_rl;
328 if (get_user_macro_rl(expr, &rl))
329 goto found;
331 if (comes_from_skb_data(expr)) {
332 rl = alloc_whole_rl(get_type(expr));
333 goto found;
336 state = get_state_expr(my_id, expr);
337 if (state && estate_rl(state)) {
338 rl = estate_rl(state);
339 goto found;
342 return NULL;
343 found:
344 user_data_flag = 1;
345 absolute_rl = var_to_absolute_rl(expr);
346 if (local_debug) {
347 sm_msg("user rl = '%s'. absolute = '%s'. intersection = '%s'",
348 show_rl(rl), show_rl(absolute_rl), show_rl((rl_intersection(rl, absolute_rl))));
350 return clone_rl(rl_intersection(rl, absolute_rl));
353 int get_user_rl(struct expression *expr, struct range_list **rl)
356 user_data_flag = 0;
357 custom_get_absolute_rl(expr, &var_user_rl, rl);
358 if (!user_data_flag) {
359 *rl = NULL;
360 return 0;
362 return 1;
365 static void match_call_info(struct expression *expr)
367 struct range_list *rl;
368 struct expression *arg;
369 int i = 0;
371 i = -1;
372 FOR_EACH_PTR(expr->args, arg) {
373 i++;
375 if (!get_user_rl(arg, &rl))
376 continue;
378 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
379 i++;
380 } END_FOR_EACH_PTR(arg);
383 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
385 struct smatch_state *state;
386 struct range_list *rl;
388 if (strcmp(sm->state->name, "") == 0)
389 return;
391 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
392 if (!state || !estate_rl(state))
393 rl = estate_rl(sm->state);
394 else
395 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
397 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
400 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
402 struct range_list *rl = NULL;
403 struct smatch_state *state;
404 struct symbol *type;
405 char fullname[256];
407 if (strcmp(key, "*$") == 0)
408 snprintf(fullname, sizeof(fullname), "*%s", name);
409 else if (strncmp(key, "$", 1) == 0)
410 snprintf(fullname, 256, "%s%s", name, key + 1);
411 else
412 return;
414 type = get_member_type_from_key(symbol_expression(sym), key);
416 /* if the caller passes a void pointer with user data */
417 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
418 struct expression *expr = symbol_expression(sym);
420 tag_as_user_data(expr);
421 return;
423 str_to_rl(type, value, &rl);
424 state = alloc_estate_rl(rl);
425 set_state(my_id, fullname, sym, state);
428 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
430 set_state(my_call_id, "this_function", NULL, &called);
433 static void match_syscall_definition(struct symbol *sym)
435 struct symbol *arg;
436 char *macro;
437 char *name;
438 int is_syscall = 0;
440 macro = get_macro_name(sym->pos);
441 if (macro &&
442 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
443 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
444 is_syscall = 1;
446 name = get_function();
447 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
448 if (name && strncmp(name, "sys_", 4) == 0)
449 is_syscall = 1;
452 if (name && strncmp(name, "compat_sys_", 11) == 0)
453 is_syscall = 1;
455 if (!is_syscall)
456 return;
458 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
459 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
460 } END_FOR_EACH_PTR(arg);
463 void check_user_data2(int id)
465 my_id = id;
467 if (option_project != PROJ_KERNEL)
468 return;
470 add_unmatched_state_hook(my_id, &empty_state);
471 add_merge_hook(my_id, &merge_estates);
473 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
474 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
475 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
476 add_function_hook("_kstrtoull", &match_user_copy, INT_PTR(2));
478 add_function_assign_hook("kmemdup_user", &match_user_assign_function, NULL);
479 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
481 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
483 add_hook(&match_assign, ASSIGNMENT_HOOK);
485 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
486 add_member_info_callback(my_id, struct_member_callback);
487 select_caller_info_hook(set_param_user_data, USER_DATA3);
490 void check_user_data3(int id)
492 my_call_id = id;
494 if (option_project != PROJ_KERNEL)
495 return;
496 select_caller_info_hook(set_called, INTERNAL);