implied: the base slist is not special
[smatch.git] / check_overflow.c
blobe306ede9e109fa3eab0a563e39a46e76813dd761
1 /*
2 * smatch/check_overflow.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include "parse.h"
12 #include "smatch.h"
13 #include "smatch_slist.h"
14 #include "smatch_extra.h"
16 struct bound {
17 int param;
18 int size;
22 * This check has two smatch IDs.
23 * my_used_id - keeps a record of array offsets that have been used.
24 * If the code checks that they are within bounds later on,
25 * we complain about using an array offset before checking
26 * that it is within bounds.
28 static int my_used_id;
30 static struct symbol *this_func;
32 static void match_function_def(struct symbol *sym)
34 this_func = sym;
37 struct limiter {
38 int buf_arg;
39 int limit_arg;
41 static struct limiter b0_l2 = {0, 2};
42 static struct limiter b1_l2 = {1, 2};
44 static void delete(struct sm_state *sm)
46 set_state(my_used_id, sm->name, sm->sym, &undefined);
49 static int definitely_just_used_as_limiter(struct expression *array, struct expression *offset)
51 long long val;
52 struct expression *tmp;
53 int step = 0;
54 int dot_ops = 0;
56 if (!get_implied_value(offset, &val))
57 return 0;
58 if (get_array_size(array) != val)
59 return 0;
61 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
62 if (step == 0) {
63 step = 1;
64 continue;
66 if (tmp->type == EXPR_PREOP && tmp->op == '(')
67 continue;
68 if (tmp->op == '.' && !dot_ops++)
69 continue;
70 if (step == 1 && tmp->op == '&') {
71 step = 2;
72 continue;
74 if (step == 2 && tmp->type == EXPR_COMPARE)
75 return 1;
76 return 0;
77 } END_FOR_EACH_PTR_REVERSE(tmp);
78 return 0;
81 static void array_check(struct expression *expr)
83 struct expression *array_expr;
84 int array_size;
85 struct expression *offset;
86 long long max;
87 char *name;
89 expr = strip_expr(expr);
90 if (!is_array(expr))
91 return;
93 array_expr = strip_parens(expr->unop->left);
94 array_size = get_array_size(array_expr);
95 if (!array_size || array_size == 1)
96 return;
98 offset = get_array_offset(expr);
99 if (!get_fuzzy_max(offset, &max)) {
100 if (getting_address())
101 return;
102 set_state_expr(my_used_id, offset, alloc_state_num(array_size));
103 } else if (array_size <= max) {
104 const char *level = "error";
106 if (getting_address())
107 level = "warn";
109 if (definitely_just_used_as_limiter(array_expr, offset))
110 return;
112 if (!option_spammy) {
113 struct smatch_state *state;
115 state = get_state_expr(SMATCH_EXTRA, offset);
116 if (state && is_whole_range(state))
117 return;
120 name = get_variable_from_expr_complex(array_expr, NULL);
121 /* Blast. Smatch can't figure out glibc's strcmp __strcmp_cg()
122 * so it prints an error every time you compare to a string
123 * literal array with 4 or less chars.
125 if (name && strcmp(name, "__s1") && strcmp(name, "__s2")) {
126 sm_msg("%s: buffer overflow '%s' %d <= %lld",
127 level, name, array_size, max);
129 free_string(name);
133 static void match_condition(struct expression *expr)
135 int left;
136 long long val;
137 struct state_list *slist;
138 struct sm_state *tmp;
139 int boundary;
141 if (!expr || expr->type != EXPR_COMPARE)
142 return;
143 if (get_macro_name(expr->pos))
144 return;
145 if (get_implied_value(expr->left, &val))
146 left = 1;
147 else if (get_implied_value(expr->right, &val))
148 left = 0;
149 else
150 return;
152 if (left)
153 slist = get_possible_states_expr(my_used_id, expr->right);
154 else
155 slist = get_possible_states_expr(my_used_id, expr->left);
156 if (!slist)
157 return;
158 FOR_EACH_PTR(slist, tmp) {
159 if (tmp->state == &merged || tmp->state == &undefined)
160 continue;
161 boundary = PTR_INT(tmp->state->data);
162 boundary -= val;
163 if (boundary < 1 && boundary > -1) {
164 char *name;
166 name = get_variable_from_expr((left ? expr->right : expr->left), NULL);
167 sm_msg("error: testing array offset '%s' after use.", name);
168 return;
170 } END_FOR_EACH_PTR(tmp);
173 static void match_strcpy(const char *fn, struct expression *expr, void *unused)
175 struct expression *dest;
176 struct expression *data;
177 char *dest_name = NULL;
178 char *data_name = NULL;
179 int dest_size;
180 int data_size;
182 dest = get_argument_from_call_expr(expr->args, 0);
183 data = get_argument_from_call_expr(expr->args, 1);
184 dest_size = get_array_size_bytes(dest);
185 data_size = get_array_size_bytes(data);
187 if (!dest_size)
188 return;
190 /* If the size of both arrays is known and the destination
191 * buffer is larger than the source buffer, we're okay.
193 if (data_size && dest_size >= data_size)
194 return;
196 dest_name = get_variable_from_expr_complex(dest, NULL);
197 data_name = get_variable_from_expr_complex(data, NULL);
199 if (data_size)
200 sm_msg("error: %s() '%s' too large for '%s' (%d vs %d)",
201 fn, data_name, dest_name, data_size, dest_size);
202 else if (option_spammy)
203 sm_msg("warn: %s() '%s' of unknown size might be too large for '%s'",
204 fn, data_name, dest_name);
206 free_string(dest_name);
207 free_string(data_name);
210 static void match_snprintf(const char *fn, struct expression *expr, void *unused)
212 struct expression *dest;
213 struct expression *dest_size_expr;
214 struct expression *format_string;
215 struct expression *data;
216 char *data_name = NULL;
217 int dest_size;
218 long long limit_size;
219 char *format;
220 int data_size;
222 dest = get_argument_from_call_expr(expr->args, 0);
223 dest_size_expr = get_argument_from_call_expr(expr->args, 1);
224 format_string = get_argument_from_call_expr(expr->args, 2);
225 data = get_argument_from_call_expr(expr->args, 3);
227 dest_size = get_array_size_bytes(dest);
228 if (!get_implied_value(dest_size_expr, &limit_size))
229 return;
230 if (dest_size && dest_size < limit_size)
231 sm_msg("error: snprintf() is printing too much %lld vs %d", limit_size, dest_size);
232 format = get_variable_from_expr(format_string, NULL);
233 if (!format)
234 return;
235 if (strcmp(format, "\"%s\""))
236 goto free;
237 data_name = get_variable_from_expr_complex(data, NULL);
238 data_size = get_array_size_bytes(data);
239 if (limit_size < data_size)
240 sm_msg("error: snprintf() chops off the last chars of '%s': %d vs %lld",
241 data_name, data_size, limit_size);
242 free:
243 free_string(data_name);
244 free_string(format);
247 static void match_sprintf(const char *fn, struct expression *expr, void *unused)
249 struct expression *dest;
250 struct expression *format_string;
251 struct expression *data;
252 char *data_name = NULL;
253 int dest_size;
254 char *format;
255 int data_size;
257 dest = get_argument_from_call_expr(expr->args, 0);
258 format_string = get_argument_from_call_expr(expr->args, 1);
259 data = get_argument_from_call_expr(expr->args, 2);
261 dest_size = get_array_size_bytes(dest);
262 if (!dest_size)
263 return;
264 format = get_variable_from_expr(format_string, NULL);
265 if (!format)
266 return;
267 if (strcmp(format, "\"%s\""))
268 goto free;
269 data_name = get_variable_from_expr_complex(data, NULL);
270 data_size = get_array_size_bytes(data);
271 if (dest_size < data_size)
272 sm_msg("error: sprintf() copies too much data from '%s': %d vs %d",
273 data_name, data_size, dest_size);
274 free:
275 free_string(data_name);
276 free_string(format);
279 static void match_limited(const char *fn, struct expression *expr, void *_limiter)
281 struct limiter *limiter = (struct limiter *)_limiter;
282 struct expression *dest;
283 struct expression *data;
284 char *dest_name = NULL;
285 long long needed;
286 int has;
288 dest = get_argument_from_call_expr(expr->args, limiter->buf_arg);
289 data = get_argument_from_call_expr(expr->args, limiter->limit_arg);
290 if (!get_fuzzy_max(data, &needed))
291 return;
292 has = get_array_size_bytes(dest);
293 if (!has)
294 return;
295 if (has >= needed)
296 return;
298 dest_name = get_variable_from_expr_complex(dest, NULL);
299 sm_msg("error: %s() '%s' too small (%d vs %lld)", fn, dest_name, has, needed);
300 free_string(dest_name);
303 static void register_funcs_from_file(void)
305 char name[256];
306 struct token *token;
307 const char *func;
308 int size, buf;
309 struct limiter *limiter;
311 snprintf(name, 256, "%s.sizeof_param", option_project_str);
312 name[255] = '\0';
313 token = get_tokens_file(name);
314 if (!token)
315 return;
316 if (token_type(token) != TOKEN_STREAMBEGIN)
317 return;
318 token = token->next;
319 while (token_type(token) != TOKEN_STREAMEND) {
320 if (token_type(token) != TOKEN_IDENT)
321 return;
322 func = show_ident(token->ident);
324 token = token->next;
325 if (token_type(token) != TOKEN_NUMBER)
326 return;
327 size = atoi(token->number);
329 token = token->next;
330 if (token_type(token) != TOKEN_NUMBER)
331 return;
332 buf = atoi(token->number);
334 limiter = malloc(sizeof(*limiter));
335 limiter->limit_arg = size;
336 limiter->buf_arg = buf;
338 add_function_hook(func, &match_limited, limiter);
340 token = token->next;
342 clear_token_alloc();
345 void check_overflow(int id)
347 my_used_id = id;
348 register_funcs_from_file();
349 add_hook(&match_function_def, FUNC_DEF_HOOK);
350 add_hook(&array_check, OP_HOOK);
351 add_hook(&match_condition, CONDITION_HOOK);
352 add_function_hook("strcpy", &match_strcpy, NULL);
353 add_function_hook("snprintf", &match_snprintf, NULL);
354 add_function_hook("sprintf", &match_sprintf, NULL);
355 add_function_hook("memcmp", &match_limited, &b0_l2);
356 add_function_hook("memcmp", &match_limited, &b1_l2);
357 add_modification_hook(my_used_id, &delete);
358 if (option_project == PROJ_KERNEL) {
359 add_function_hook("copy_to_user", &match_limited, &b0_l2);
360 add_function_hook("copy_to_user", &match_limited, &b1_l2);
361 add_function_hook("_copy_to_user", &match_limited, &b0_l2);
362 add_function_hook("_copy_to_user", &match_limited, &b1_l2);
363 add_function_hook("__copy_to_user", &match_limited, &b0_l2);
364 add_function_hook("__copy_to_user", &match_limited, &b1_l2);
365 add_function_hook("copy_from_user", &match_limited, &b0_l2);
366 add_function_hook("copy_from_user", &match_limited, &b1_l2);
367 add_function_hook("_copy_from_user", &match_limited, &b0_l2);
368 add_function_hook("_copy_from_user", &match_limited, &b1_l2);
369 add_function_hook("__copy_from_user", &match_limited, &b0_l2);
370 add_function_hook("__copy_from_user", &match_limited, &b1_l2);