db: caller info needs to record the -1 parameters
[smatch.git] / check_overflow.c
blobad72f148de5ce7e59b00f51cb6cf771ccbdc2f12
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(const char *name, struct symbol *sym, struct expression *expr, void *unused)
46 delete_state(my_used_id, name, sym);
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 add_modification_hook_expr(my_used_id, offset, &delete, NULL);
104 } else if (array_size <= max) {
105 const char *level = "error";
107 if (getting_address())
108 level = "warn";
110 if (definitely_just_used_as_limiter(array_expr, offset))
111 return;
113 if (!option_spammy) {
114 struct smatch_state *state;
116 state = get_state_expr(SMATCH_EXTRA, offset);
117 if (state && is_whole_range(state))
118 return;
121 name = get_variable_from_expr_complex(array_expr, NULL);
122 /* Blast. Smatch can't figure out glibc's strcmp __strcmp_cg()
123 * so it prints an error every time you compare to a string
124 * literal array with 4 or less chars.
126 if (name && strcmp(name, "__s1") && strcmp(name, "__s2")) {
127 sm_msg("%s: buffer overflow '%s' %d <= %lld",
128 level, name, array_size, max);
130 free_string(name);
134 static void match_condition(struct expression *expr)
136 int left;
137 long long val;
138 struct state_list *slist;
139 struct sm_state *tmp;
140 int boundary;
142 if (!expr || expr->type != EXPR_COMPARE)
143 return;
144 if (get_implied_value(expr->left, &val))
145 left = 1;
146 else if (get_implied_value(expr->right, &val))
147 left = 0;
148 else
149 return;
151 if (left)
152 slist = get_possible_states_expr(my_used_id, expr->right);
153 else
154 slist = get_possible_states_expr(my_used_id, expr->left);
155 if (!slist)
156 return;
157 FOR_EACH_PTR(slist, tmp) {
158 if (tmp->state == &merged)
159 continue;
160 boundary = PTR_INT(tmp->state->data);
161 boundary -= val;
162 if (boundary < 1 && boundary > -1) {
163 char *name;
165 name = get_variable_from_expr((left ? expr->right : expr->left), NULL);
166 sm_msg("error: testing array offset '%s' after use.", name);
167 return;
169 } END_FOR_EACH_PTR(tmp);
172 static void match_strcpy(const char *fn, struct expression *expr, void *unused)
174 struct expression *dest;
175 struct expression *data;
176 char *dest_name = NULL;
177 char *data_name = NULL;
178 int dest_size;
179 int data_size;
181 dest = get_argument_from_call_expr(expr->args, 0);
182 data = get_argument_from_call_expr(expr->args, 1);
183 dest_size = get_array_size_bytes(dest);
184 data_size = get_array_size_bytes(data);
186 if (!dest_size)
187 return;
189 /* If the size of both arrays is known and the destination
190 * buffer is larger than the source buffer, we're okay.
192 if (data_size && dest_size >= data_size)
193 return;
195 dest_name = get_variable_from_expr_complex(dest, NULL);
196 data_name = get_variable_from_expr_complex(data, NULL);
198 if (data_size)
199 sm_msg("error: %s() '%s' too large for '%s' (%d vs %d)",
200 fn, data_name, dest_name, data_size, dest_size);
201 else if (option_spammy)
202 sm_msg("warn: %s() '%s' of unknown size might be too large for '%s'",
203 fn, data_name, dest_name);
205 free_string(dest_name);
206 free_string(data_name);
209 static void match_snprintf(const char *fn, struct expression *expr, void *unused)
211 struct expression *dest;
212 struct expression *dest_size_expr;
213 struct expression *format_string;
214 struct expression *data;
215 char *data_name = NULL;
216 int dest_size;
217 long long limit_size;
218 char *format;
219 int data_size;
221 dest = get_argument_from_call_expr(expr->args, 0);
222 dest_size_expr = get_argument_from_call_expr(expr->args, 1);
223 format_string = get_argument_from_call_expr(expr->args, 2);
224 data = get_argument_from_call_expr(expr->args, 3);
226 dest_size = get_array_size_bytes(dest);
227 if (!get_implied_value(dest_size_expr, &limit_size))
228 return;
229 if (dest_size && dest_size < limit_size)
230 sm_msg("error: snprintf() is printing too much %lld vs %d", limit_size, dest_size);
231 format = get_variable_from_expr(format_string, NULL);
232 if (!format)
233 return;
234 if (strcmp(format, "\"%s\""))
235 goto free;
236 data_name = get_variable_from_expr_complex(data, NULL);
237 data_size = get_array_size_bytes(data);
238 if (limit_size < data_size)
239 sm_msg("error: snprintf() chops off the last chars of '%s': %d vs %lld",
240 data_name, data_size, limit_size);
241 free:
242 free_string(data_name);
243 free_string(format);
246 static void match_sprintf(const char *fn, struct expression *expr, void *unused)
248 struct expression *dest;
249 struct expression *format_string;
250 struct expression *data;
251 char *data_name = NULL;
252 int dest_size;
253 char *format;
254 int data_size;
256 dest = get_argument_from_call_expr(expr->args, 0);
257 format_string = get_argument_from_call_expr(expr->args, 1);
258 data = get_argument_from_call_expr(expr->args, 2);
260 dest_size = get_array_size_bytes(dest);
261 if (!dest_size)
262 return;
263 format = get_variable_from_expr(format_string, NULL);
264 if (!format)
265 return;
266 if (strcmp(format, "\"%s\""))
267 goto free;
268 data_name = get_variable_from_expr_complex(data, NULL);
269 data_size = get_array_size_bytes(data);
270 if (dest_size < data_size)
271 sm_msg("error: sprintf() copies too much data from '%s': %d vs %d",
272 data_name, data_size, dest_size);
273 free:
274 free_string(data_name);
275 free_string(format);
278 static void match_limited(const char *fn, struct expression *expr, void *_limiter)
280 struct limiter *limiter = (struct limiter *)_limiter;
281 struct expression *dest;
282 struct expression *data;
283 char *dest_name = NULL;
284 long long needed;
285 int has;
287 dest = get_argument_from_call_expr(expr->args, limiter->buf_arg);
288 data = get_argument_from_call_expr(expr->args, limiter->limit_arg);
289 if (!get_fuzzy_max(data, &needed))
290 return;
291 has = get_array_size_bytes(dest);
292 if (!has)
293 return;
294 if (has >= needed)
295 return;
297 dest_name = get_variable_from_expr_complex(dest, NULL);
298 sm_msg("error: %s() '%s' too small (%d vs %lld)", fn, dest_name, has, needed);
299 free_string(dest_name);
302 void check_overflow(int id)
304 my_used_id = id;
305 add_hook(&match_function_def, FUNC_DEF_HOOK);
306 add_hook(&array_check, OP_HOOK);
307 add_hook(&match_condition, CONDITION_HOOK);
308 add_function_hook("strcpy", &match_strcpy, NULL);
309 add_function_hook("snprintf", &match_snprintf, NULL);
310 add_function_hook("sprintf", &match_sprintf, NULL);
311 if (option_project == PROJ_KERNEL) {
312 add_function_hook("copy_to_user", &match_limited, &b0_l2);
313 add_function_hook("copy_to_user", &match_limited, &b1_l2);
314 add_function_hook("_copy_to_user", &match_limited, &b0_l2);
315 add_function_hook("_copy_to_user", &match_limited, &b1_l2);
316 add_function_hook("__copy_to_user", &match_limited, &b0_l2);
317 add_function_hook("__copy_to_user", &match_limited, &b1_l2);
318 add_function_hook("copy_from_user", &match_limited, &b0_l2);
319 add_function_hook("copy_from_user", &match_limited, &b1_l2);
320 add_function_hook("_copy_from_user", &match_limited, &b0_l2);
321 add_function_hook("_copy_from_user", &match_limited, &b1_l2);
322 add_function_hook("__copy_from_user", &match_limited, &b0_l2);
323 add_function_hook("__copy_from_user", &match_limited, &b1_l2);