validation: correct some output
[smatch.git] / check_user_data2.c
blob11952ce8b5091646af2ca4699cf2be0494d1ceb8
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 switch (expr->type) {
200 case EXPR_BINOP:
201 if (comes_from_skb_data(expr->left))
202 return 1;
203 if (comes_from_skb_data(expr->right))
204 return 1;
205 return 0;
206 case EXPR_PREOP:
207 return comes_from_skb_data(expr->unop);
208 case EXPR_DEREF:
209 if (is_skb_data(expr))
210 return 1;
211 return comes_from_skb_data(expr->deref);
212 default:
213 return 0;
218 static int handle_struct_assignment(struct expression *expr)
220 struct expression *right;
221 struct symbol *type;
223 type = get_type(expr->left);
224 if (!type || type->type != SYM_PTR)
225 return 0;
226 type = get_real_base_type(type);
227 if (!type || type->type != SYM_STRUCT)
228 return 0;
231 * Ignore struct to struct assignments because for those we look at the
232 * individual members.
234 right = strip_expr(expr->right);
235 type = get_type(right);
236 if (!type || type->type != SYM_PTR)
237 return 0;
238 type = get_real_base_type(type);
239 if (type && type->type == SYM_STRUCT)
240 return 0;
242 if (!points_to_user_data(right) && !is_skb_data(right))
243 return 0;
245 tag_as_user_data(expr->left);
246 return 1;
249 static int handle_get_user(struct expression *expr)
251 char *name;
252 int ret = 0;
254 name = get_macro_name(expr->pos);
255 if (!name || strcmp(name, "get_user") != 0)
256 return 0;
258 name = expr_to_var(expr->right);
259 if (!name || strcmp(name, "__val_gu") != 0)
260 goto free;
261 set_state_expr(my_id, expr->left, alloc_estate_whole(get_type(expr->left)));
262 ret = 1;
263 free:
264 free_string(name);
265 return ret;
268 static void match_assign(struct expression *expr)
270 struct range_list *rl;
272 if (is_fake_call(expr->right))
273 return;
274 if (handle_struct_assignment(expr))
275 return;
276 if (handle_get_user(expr))
277 return;
279 if (expr->right->type == EXPR_CALL ||
280 !get_user_rl(expr->right, &rl))
281 goto clear_old_state;
283 rl = cast_rl(get_type(expr->left), rl);
284 set_state_expr(my_id, expr->left, alloc_estate_rl(rl));
286 return;
288 clear_old_state:
289 if (get_state_expr(my_id, expr->left))
290 set_state_expr(my_id, expr->left, alloc_estate_empty());
293 static void match_user_assign_function(const char *fn, struct expression *expr, void *unused)
295 tag_as_user_data(expr->left);
298 static int get_user_macro_rl(struct expression *expr, struct range_list **rl)
300 char *macro;
302 if (!expr)
303 return 0;
304 macro = get_macro_name(expr->pos);
306 if (!macro)
307 return 0;
309 if (strcmp(macro, "ntohl") == 0) {
310 *rl = alloc_whole_rl(&uint_ctype);
311 return 1;
313 if (strcmp(macro, "ntohs") == 0) {
314 *rl = alloc_whole_rl(&ushort_ctype);
315 return 1;
317 return 0;
320 static int user_data_flag;
321 static struct range_list *var_user_rl(struct expression *expr)
323 struct smatch_state *state;
324 struct range_list *rl;
325 struct range_list *absolute_rl;
327 if (get_user_macro_rl(expr, &rl))
328 goto found;
330 if (comes_from_skb_data(expr)) {
331 rl = alloc_whole_rl(get_type(expr));
332 goto found;
335 state = get_state_expr(my_id, expr);
336 if (state && estate_rl(state)) {
337 rl = estate_rl(state);
338 goto found;
341 return NULL;
342 found:
343 user_data_flag = 1;
344 absolute_rl = var_to_absolute_rl(expr);
345 return clone_rl(rl_intersection(rl, absolute_rl));
348 int get_user_rl(struct expression *expr, struct range_list **rl)
351 user_data_flag = 0;
352 custom_get_absolute_rl(expr, &var_user_rl, rl);
353 if (!user_data_flag) {
354 *rl = NULL;
355 return 0;
357 return 1;
360 static void match_call_info(struct expression *expr)
362 struct range_list *rl;
363 struct expression *arg;
364 int i = 0;
366 i = -1;
367 FOR_EACH_PTR(expr->args, arg) {
368 i++;
370 if (!get_user_rl(arg, &rl))
371 continue;
373 sql_insert_caller_info(expr, USER_DATA3, i, "$", show_rl(rl));
374 } END_FOR_EACH_PTR(arg);
377 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
379 struct smatch_state *state;
380 struct range_list *rl;
382 if (strcmp(sm->state->name, "") == 0)
383 return;
385 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
386 if (!state || !estate_rl(state))
387 rl = estate_rl(sm->state);
388 else
389 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
391 sql_insert_caller_info(call, USER_DATA3, param, printed_name, show_rl(rl));
394 static void set_param_user_data(const char *name, struct symbol *sym, char *key, char *value)
396 struct range_list *rl = NULL;
397 struct smatch_state *state;
398 struct symbol *type;
399 char fullname[256];
401 if (strcmp(key, "*$") == 0)
402 snprintf(fullname, sizeof(fullname), "*%s", name);
403 else if (strncmp(key, "$", 1) == 0)
404 snprintf(fullname, 256, "%s%s", name, key + 1);
405 else
406 return;
408 type = get_member_type_from_key(symbol_expression(sym), key);
410 /* if the caller passes a void pointer with user data */
411 if (strcmp(key, "*$") == 0 && type && type != &void_ctype) {
412 struct expression *expr = symbol_expression(sym);
414 tag_as_user_data(expr);
415 return;
417 str_to_rl(type, value, &rl);
418 state = alloc_estate_rl(rl);
419 set_state(my_id, fullname, sym, state);
422 static void set_called(const char *name, struct symbol *sym, char *key, char *value)
424 set_state(my_call_id, "this_function", NULL, &called);
427 static void match_syscall_definition(struct symbol *sym)
429 struct symbol *arg;
430 char *macro;
431 char *name;
432 int is_syscall = 0;
434 macro = get_macro_name(sym->pos);
435 if (macro &&
436 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
437 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
438 is_syscall = 1;
440 name = get_function();
441 if (!option_no_db && get_state(my_call_id, "this_function", NULL) != &called) {
442 if (name && strncmp(name, "sys_", 4) == 0)
443 is_syscall = 1;
446 if (name && strncmp(name, "compat_sys_", 11) == 0)
447 is_syscall = 1;
449 if (!is_syscall)
450 return;
452 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
453 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
454 } END_FOR_EACH_PTR(arg);
457 void check_user_data2(int id)
459 my_id = id;
461 if (option_project != PROJ_KERNEL)
462 return;
464 add_unmatched_state_hook(my_id, &empty_state);
465 add_merge_hook(my_id, &merge_estates);
467 add_function_hook("copy_from_user", &match_user_copy, INT_PTR(0));
468 add_function_hook("__copy_from_user", &match_user_copy, INT_PTR(0));
469 add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
470 add_function_hook("_kstrtoull", &match_user_copy, INT_PTR(2));
472 add_function_assign_hook("kmemdup_user", &match_user_assign_function, NULL);
473 add_function_assign_hook("kmap_atomic", &match_user_assign_function, NULL);
475 add_hook(&match_syscall_definition, AFTER_DEF_HOOK);
477 add_hook(&match_assign, ASSIGNMENT_HOOK);
479 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
480 add_member_info_callback(my_id, struct_member_callback);
481 select_caller_info_hook(set_param_user_data, USER_DATA3);
484 void check_user_data3(int id)
486 my_call_id = id;
488 if (option_project != PROJ_KERNEL)
489 return;
490 select_caller_info_hook(set_called, INTERNAL);