extra: revert the mod_expr changes for modify expression hooks
[smatch.git] / check_rosenberg.c
blob05f7b0eed23156b5d2003800b44f1cd0cdd389b1
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
18 /* Does a search for Dan Rosenberg style info leaks */
20 /* fixme: struct includes a struct with a hole in it */
21 /* function is called that clears the struct */
23 #include "scope.h"
24 #include "smatch.h"
25 #include "smatch_function_hashtable.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_whole_id;
30 static int my_member_id;
31 static int skb_put_id;
33 STATE(cleared);
35 static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
37 struct symbol *type;
39 type = get_real_base_type(sym);
40 if (!type || type->type != SYM_STRUCT)
41 return;
43 if (!cur_func_sym)
44 return;
46 if (name && strstr(name, "->"))
47 return;
49 set_state(my_member_id, name, sym, state);
52 static void print_holey_warning(struct expression *data, const char *member)
54 char *name;
56 name = expr_to_str(data);
57 if (member) {
58 sm_warning("check that '%s' doesn't leak information (struct has a hole after '%s')",
59 name, member);
60 } else {
61 sm_warning("check that '%s' doesn't leak information (struct has holes)",
62 name);
64 free_string(name);
67 static int check_struct(struct expression *expr, struct symbol *type)
69 struct symbol *base_type, *prev_type;
70 struct symbol *tmp, *prev;
71 int align;
73 if (type->ctype.alignment == 1)
74 return 0;
76 align = 0;
77 prev = NULL;
78 prev_type = NULL;
79 FOR_EACH_PTR(type->symbol_list, tmp) {
80 base_type = get_real_base_type(tmp);
81 if (base_type && base_type->type == SYM_STRUCT) {
82 if (check_struct(expr, base_type))
83 return 1;
85 if (base_type && base_type->type == SYM_BITFIELD &&
86 prev_type && prev_type->type == SYM_BITFIELD)
87 goto next;
89 if (!tmp->ctype.alignment) {
90 sm_perror("cannot determine the alignment here");
91 } else if (align % tmp->ctype.alignment) {
93 print_holey_warning(expr, prev->ident ? prev->ident->name : "<unknown>");
94 return 1;
97 next:
98 if (base_type == &bool_ctype)
99 align += 1;
100 else if (type_bits(tmp) <= 0)
101 align = 0;
102 else
103 align += type_bytes(tmp);
105 prev = tmp;
106 prev_type = base_type;
107 } END_FOR_EACH_PTR(tmp);
109 // FIXME: this isn't the correct fix. See sbni_siocdevprivate().
110 if (prev_type && prev_type->type == SYM_BITFIELD)
111 return 0;
112 if (align % type->ctype.alignment) {
113 sm_msg("%s: tmp='%s' align=%d ctype.align=%ld type='%s'", __func__,
114 tmp->ident ? tmp->ident->name : "<unknown>",
115 align, tmp->ctype.alignment,
116 type_to_str(get_real_base_type(tmp)));
118 print_holey_warning(expr, (prev && prev->ident) ? prev->ident->name : "<unknown>");
119 return 1;
122 return 0;
125 static int warn_on_holey_struct(struct expression *expr)
127 struct symbol *type;
128 type = get_type(expr);
129 if (!type || type->type != SYM_STRUCT)
130 return 0;
132 return check_struct(expr, type);
135 static int has_global_scope(struct expression *expr)
137 struct symbol *sym;
139 if (expr->type != EXPR_SYMBOL)
140 return FALSE;
141 sym = expr->symbol;
142 if (!sym)
143 return FALSE;
144 return toplevel(sym->scope);
147 static void match_clear(const char *fn, struct expression *expr, void *_arg_no)
149 struct expression *ptr, *tmp;
150 int arg_no = PTR_INT(_arg_no);
152 ptr = get_argument_from_call_expr(expr->args, arg_no);
153 if (!ptr)
154 return;
155 tmp = get_assigned_expr(ptr);
156 if (tmp)
157 ptr = tmp;
158 ptr = strip_expr(ptr);
159 if (ptr->type != EXPR_PREOP || ptr->op != '&')
160 return;
161 ptr = strip_expr(ptr->unop);
162 set_state_expr(my_whole_id, ptr, &cleared);
165 static int was_memset(struct expression *expr)
167 if (get_state_expr(my_whole_id, expr) == &cleared)
168 return 1;
169 return 0;
172 static int member_initialized(char *name, struct symbol *outer, struct symbol *member, int pointer)
174 char buf[256];
175 struct symbol *base;
177 base = get_base_type(member);
178 if (!base || base->type != SYM_BASETYPE || !member->ident)
179 return FALSE;
181 if (pointer)
182 snprintf(buf, 256, "%s->%s", name, member->ident->name);
183 else
184 snprintf(buf, 256, "%s.%s", name, member->ident->name);
186 if (get_state(my_member_id, buf, outer))
187 return TRUE;
189 return FALSE;
192 static int member_uninitialized(char *name, struct symbol *outer, struct symbol *member, int pointer)
194 char buf[256];
195 struct symbol *base;
196 struct sm_state *sm;
198 base = get_base_type(member);
199 if (!base || base->type != SYM_BASETYPE || !member->ident)
200 return FALSE;
202 if (pointer)
203 snprintf(buf, 256, "%s->%s", name, member->ident->name);
204 else
205 snprintf(buf, 256, "%s.%s", name, member->ident->name);
207 sm = get_sm_state(my_member_id, buf, outer);
208 if (sm && !slist_has_state(sm->possible, &undefined))
209 return FALSE;
211 sm_warning("check that '%s' doesn't leak information", buf);
212 return TRUE;
215 static int check_members_initialized(struct expression *expr)
217 char *name;
218 struct symbol *outer;
219 struct symbol *sym;
220 struct symbol *tmp;
221 int pointer = 0;
222 int printed = 0;
224 sym = get_type(expr);
225 if (sym && sym->type == SYM_PTR) {
226 pointer = 1;
227 sym = get_real_base_type(sym);
229 if (!sym)
230 return 0;
231 if (sym->type != SYM_STRUCT)
232 return 0;
234 name = expr_to_var_sym(expr, &outer);
237 * check that at least one member was set. If all of them were not set
238 * it's more likely a problem in the check than a problem in the kernel
239 * code.
241 FOR_EACH_PTR(sym->symbol_list, tmp) {
242 if (member_initialized(name, outer, tmp, pointer))
243 goto check;
244 } END_FOR_EACH_PTR(tmp);
245 goto out;
247 check:
248 FOR_EACH_PTR(sym->symbol_list, tmp) {
249 if (member_uninitialized(name, outer, tmp, pointer)) {
250 printed = 1;
251 goto out;
253 } END_FOR_EACH_PTR(tmp);
254 out:
255 free_string(name);
256 return printed;
259 static void check_was_initialized(struct expression *data)
261 data = strip_expr(data);
262 if (!data)
263 return;
264 if (data->type == EXPR_PREOP && data->op == '&')
265 data = strip_expr(data->unop);
266 if (data->type != EXPR_SYMBOL)
267 return;
269 if (has_global_scope(data))
270 return;
271 if (was_memset(data))
272 return;
273 if (warn_on_holey_struct(data))
274 return;
275 check_members_initialized(data);
278 static void check_skb_put(struct expression *data)
280 data = strip_expr(data);
281 if (!data)
282 return;
283 if (data->type == EXPR_PREOP && data->op == '&')
284 data = strip_expr(data->unop);
286 if (was_memset(data))
287 return;
288 if (warn_on_holey_struct(data))
289 return;
290 check_members_initialized(data);
293 static void match_copy_to_user(const char *fn, struct expression *expr, void *_arg)
295 int arg = PTR_INT(_arg);
296 struct expression *data;
298 data = get_argument_from_call_expr(expr->args, arg);
299 data = strip_expr(data);
300 if (!data)
301 return;
302 if (data->type != EXPR_PREOP || data->op != '&')
303 return;
304 check_was_initialized(data);
307 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
309 while (expr->type == EXPR_ASSIGNMENT)
310 expr = strip_expr(expr->right);
311 if (expr->type != EXPR_CALL)
312 return;
314 match_clear(NULL, expr, INT_PTR(param));
317 static struct smatch_state *alloc_expr_state(struct expression *expr)
319 struct smatch_state *state;
320 char *name;
322 name = expr_to_str(expr);
323 if (!name)
324 return NULL;
326 state = __alloc_smatch_state(0);
327 expr = strip_expr(expr);
328 state->name = alloc_sname(name);
329 free_string(name);
330 state->data = expr;
331 return state;
334 static void match_skb_put(const char *fn, struct expression *expr, void *unused)
336 struct symbol *type;
337 struct smatch_state *state;
339 type = get_type(expr->left);
340 type = get_real_base_type(type);
341 if (!type || type->type != SYM_STRUCT)
342 return;
343 state = alloc_expr_state(expr->left);
344 set_state_expr(skb_put_id, expr->left, state);
347 static void match_return_skb_put(struct expression *expr)
349 struct sm_state *sm;
350 struct stree *stree;
352 if (is_error_return(expr))
353 return;
355 stree = __get_cur_stree();
357 FOR_EACH_MY_SM(skb_put_id, stree, sm) {
358 check_skb_put(sm->state->data);
359 } END_FOR_EACH_SM(sm);
362 static void register_clears_argument(void)
364 struct token *token;
365 const char *func;
366 int arg;
368 token = get_tokens_file("kernel.clears_argument");
369 if (!token)
370 return;
371 if (token_type(token) != TOKEN_STREAMBEGIN)
372 return;
373 token = token->next;
374 while (token_type(token) != TOKEN_STREAMEND) {
375 if (token_type(token) != TOKEN_IDENT)
376 return;
377 func = show_ident(token->ident);
378 token = token->next;
379 if (token_type(token) != TOKEN_NUMBER)
380 return;
381 arg = atoi(token->number);
383 add_function_hook(func, &match_clear, INT_PTR(arg));
384 token = token->next;
386 clear_token_alloc();
389 static void register_copy_funcs_from_file(void)
391 struct token *token;
392 const char *func;
393 int arg;
395 token = get_tokens_file("kernel.rosenberg_funcs");
396 if (!token)
397 return;
398 if (token_type(token) != TOKEN_STREAMBEGIN)
399 return;
400 token = token->next;
401 while (token_type(token) != TOKEN_STREAMEND) {
402 if (token_type(token) != TOKEN_IDENT)
403 return;
404 func = show_ident(token->ident);
405 token = token->next;
406 if (token_type(token) != TOKEN_NUMBER)
407 return;
408 arg = atoi(token->number);
409 add_function_hook(func, &match_copy_to_user, INT_PTR(arg));
410 token = token->next;
412 clear_token_alloc();
415 void check_rosenberg(int id)
417 if (option_project != PROJ_KERNEL)
418 return;
419 my_whole_id = id;
421 add_function_hook("memset", &match_clear, INT_PTR(0));
422 add_function_hook("memcpy", &match_clear, INT_PTR(0));
423 add_function_hook("memzero", &match_clear, INT_PTR(0));
424 add_function_hook("__memset", &match_clear, INT_PTR(0));
425 add_function_hook("__memcpy", &match_clear, INT_PTR(0));
426 add_function_hook("__memzero", &match_clear, INT_PTR(0));
427 add_function_hook("__builtin_memset", &match_clear, INT_PTR(0));
428 add_function_hook("__builtin_memcpy", &match_clear, INT_PTR(0));
430 register_clears_argument();
431 select_return_states_hook(BUF_CLEARED, &db_param_cleared);
433 register_copy_funcs_from_file();
436 void check_rosenberg2(int id)
438 if (option_project != PROJ_KERNEL)
439 return;
441 my_member_id = id;
442 set_dynamic_states(my_member_id);
443 add_extra_mod_hook(&extra_mod_hook);
446 void check_rosenberg3(int id)
448 if (option_project != PROJ_KERNEL)
449 return;
451 skb_put_id = id;
452 set_dynamic_states(skb_put_id);
453 add_function_assign_hook("skb_put", &match_skb_put, NULL);
454 add_hook(&match_return_skb_put, RETURN_HOOK);