2 * Copyright (C) 2013 Oracle.
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
22 #include "smatch_slist.h"
23 #include "smatch_extra.h"
25 static int my_strlen_id
;
27 * The trick with the my_equiv_id is that if we have:
29 * We don't know at that point what the strlen() is but we know it's equivalent
30 * to "foo" so maybe we can find the value of "foo" later.
32 static int my_equiv_id
;
34 static struct smatch_state
*size_to_estate(int size
)
38 sval
.type
= &int_ctype
;
41 return alloc_estate_sval(sval
);
44 static struct smatch_state
*unknown_strlen(void)
46 return alloc_estate_sval(int_minus_one
);
49 static struct smatch_state
*unmatched_strlen_state(struct sm_state
*sm
)
51 return unknown_strlen();
54 static bool handle_plus_plus(struct sm_state
*sm
, struct expression
*mod_expr
)
56 struct range_list
*rl
;
60 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
62 if (mod_expr
->op
!= SPECIAL_INCREMENT
)
67 if (!estate_rl(sm
->state
) || estate_min(sm
->state
).value
<= 0)
70 rl
= rl_binop(estate_rl(sm
->state
), '-', alloc_rl(int_one
, int_one
));
71 set_state(sm
->owner
, sm
->name
, sm
->sym
, alloc_estate_rl(rl
));
75 static void set_strlen_undefined(struct sm_state
*sm
, struct expression
*mod_expr
)
77 if (handle_plus_plus(sm
, mod_expr
))
79 set_state(sm
->owner
, sm
->name
, sm
->sym
, unknown_strlen());
82 static void match_string_assignment(struct expression
*expr
)
84 struct range_list
*rl
;
88 if (!get_implied_strlen(expr
->right
, &rl
))
90 set_state_expr(my_strlen_id
, expr
->left
, alloc_estate_rl(clone_rl(rl
)));
93 bool is_strlen(struct expression
*expr
)
95 if (!expr
|| expr
->type
!= EXPR_CALL
)
98 if (sym_name_is("strlen", expr
->fn
) ||
99 sym_name_is("__builtin_strlen", expr
->fn
) ||
100 sym_name_is("__fortify_strlen", expr
->fn
))
106 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
108 struct expression
*right
;
109 struct expression
*str
;
110 struct expression
*len_expr
;
112 struct smatch_state
*state
;
114 right
= strip_expr(expr
->right
);
115 str
= get_argument_from_call_expr(right
->args
, 0);
116 len_expr
= strip_expr(expr
->left
);
118 len_name
= expr_to_var(len_expr
);
122 state
= __alloc_smatch_state(0);
123 state
->name
= len_name
;
124 state
->data
= len_expr
;
126 set_state_expr(my_equiv_id
, str
, state
);
129 static void match_strlen_condition(struct expression
*expr
)
131 struct expression
*left
;
132 struct expression
*right
;
133 struct expression
*str
= NULL
;
135 int strlen_right
= 0;
137 struct smatch_state
*true_state
= NULL
;
138 struct smatch_state
*false_state
= NULL
;
141 expr
= strip_expr(expr
);
142 if (expr
->type
!= EXPR_COMPARE
)
145 left
= strip_expr(expr
->left
);
146 right
= strip_expr(expr
->right
);
148 if (left
->type
== EXPR_CALL
&& is_strlen(left
)) {
149 str
= get_argument_from_call_expr(left
->args
, 0);
152 if (right
->type
== EXPR_CALL
&& is_strlen(right
)) {
153 str
= get_argument_from_call_expr(right
->args
, 0);
157 if (!strlen_left
&& !strlen_right
)
159 if (strlen_left
&& strlen_right
)
164 if (!get_value(right
, &sval
))
167 op
= flip_comparison(op
);
168 if (!get_value(left
, &sval
))
174 case SPECIAL_UNSIGNED_LT
:
175 true_state
= size_to_estate(sval
.value
- 1);
178 case SPECIAL_UNSIGNED_LTE
:
179 true_state
= size_to_estate(sval
.value
);
182 true_state
= size_to_estate(sval
.value
);
184 case SPECIAL_NOTEQUAL
:
185 false_state
= size_to_estate(sval
.value
);
188 case SPECIAL_UNSIGNED_GTE
:
189 false_state
= size_to_estate(sval
.value
- 1);
192 case SPECIAL_UNSIGNED_GT
:
193 false_state
= size_to_estate(sval
.value
);
197 set_true_false_states_expr(my_strlen_id
, str
, true_state
, false_state
);
200 static void match_snprintf(const char *fn
, struct expression
*expr
, void *unused
)
202 struct expression
*dest
;
203 struct expression
*dest_size_expr
;
206 dest
= get_argument_from_call_expr(expr
->args
, 0);
207 dest_size_expr
= get_argument_from_call_expr(expr
->args
, 1);
209 if (!get_implied_value(dest_size_expr
, &limit_size
))
212 if (limit_size
.value
<= 0)
215 set_state_expr(my_strlen_id
, dest
, size_to_estate(limit_size
.value
- 1));
218 static void match_strlcpycat(const char *fn
, struct expression
*expr
, void *unused
)
220 struct expression
*dest
;
221 struct expression
*src
;
222 struct expression
*limit_expr
;
223 struct smatch_state
*state
;
227 dest
= get_argument_from_call_expr(expr
->args
, 0);
228 src
= get_argument_from_call_expr(expr
->args
, 1);
229 limit_expr
= get_argument_from_call_expr(expr
->args
, 2);
231 src_len
= get_size_from_strlen(src
);
233 if (!get_implied_max(limit_expr
, &limit
))
235 if (limit
.value
< 0 || limit
.value
> INT_MAX
)
237 if (src_len
!= 0 && strcmp(fn
, "strcpy") == 0 && src_len
< limit
.value
)
238 limit
.value
= src_len
;
240 state
= alloc_estate_range(int_zero
, sval_type_val(&int_ctype
, limit
.value
- 1));
241 set_state_expr(my_strlen_id
, dest
, state
);
244 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
246 struct expression
*dest
;
247 struct expression
*src
;
248 struct range_list
*rl
;
250 dest
= get_argument_from_call_expr(expr
->args
, 0);
251 src
= get_argument_from_call_expr(expr
->args
, 1);
253 if (!get_implied_strlen(src
, &rl
))
256 set_state_expr(my_strlen_id
, dest
, alloc_estate_rl(rl
));
259 static void match_str_chr(struct expression
*expr
, const char *name
, struct symbol
*sym
, void *data
)
261 struct expression
*call
, *orig
;
262 struct smatch_state
*state
;
263 struct range_list
*rl
;
267 while (call
&& call
->type
== EXPR_ASSIGNMENT
)
268 call
= strip_expr(expr
->right
);
269 if (!call
|| call
->type
!= EXPR_CALL
)
272 orig
= get_argument_from_call_expr(call
->args
, 0);
273 if (!get_implied_strlen(orig
, &rl
))
280 state
= alloc_estate_range(int_one
, max
);
281 set_state(my_strlen_id
, name
, sym
, state
);
284 static int get_strlen_from_string(struct expression
*expr
, struct range_list
**rl
)
289 len
= expr
->string
->length
;
290 sval
= sval_type_val(&int_ctype
, len
- 1);
291 *rl
= alloc_rl(sval
, sval
);
296 static int get_strlen_from_state(struct expression
*expr
, struct range_list
**rl
)
298 struct smatch_state
*state
;
300 state
= get_state_expr(my_strlen_id
, expr
);
303 *rl
= estate_rl(state
);
307 static int get_strlen_from_equiv(struct expression
*expr
, struct range_list
**rl
)
309 struct smatch_state
*state
;
311 state
= get_state_expr(my_equiv_id
, expr
);
312 if (!state
|| !state
->data
)
314 if (!get_implied_rl((struct expression
*)state
->data
, rl
))
320 * This returns the strlen() without the NUL char.
322 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
)
327 expr
= strip_expr(expr
);
328 if (expr
->type
== EXPR_STRING
)
329 return get_strlen_from_string(expr
, rl
);
331 if (get_strlen_from_state(expr
, rl
))
333 if (get_strlen_from_equiv(expr
, rl
))
338 int get_size_from_strlen(struct expression
*expr
)
340 struct range_list
*rl
;
343 if (!get_implied_strlen(expr
, &rl
))
346 if (sval_is_negative(max
) || sval_is_max(max
))
349 return max
.value
+ 1; /* add one because strlen doesn't include the NULL */
352 void set_param_strlen(const char *name
, struct symbol
*sym
, char *key
, char *value
)
354 struct range_list
*rl
= NULL
;
355 struct smatch_state
*state
;
358 if (strncmp(key
, "$", 1) != 0)
361 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
363 str_to_rl(&int_ctype
, value
, &rl
);
364 if (!rl
|| is_whole_rl(rl
))
366 state
= alloc_estate_rl(rl
);
367 set_state(my_strlen_id
, fullname
, sym
, state
);
370 static void match_call(struct expression
*expr
)
372 struct expression
*arg
;
373 struct range_list
*rl
;
377 FOR_EACH_PTR(expr
->args
, arg
) {
378 if (!get_implied_strlen(arg
, &rl
))
380 if (!is_whole_rl(rl
))
381 sql_insert_caller_info(expr
, STR_LEN
, i
, "$", show_rl(rl
));
383 } END_FOR_EACH_PTR(arg
);
386 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
388 if (sm
->state
== &merged
)
390 sql_insert_caller_info(call
, STR_LEN
, param
, printed_name
, sm
->state
->name
);
393 void register_strlen(int id
)
397 set_dynamic_states(my_strlen_id
);
399 add_unmatched_state_hook(my_strlen_id
, &unmatched_strlen_state
);
401 select_caller_info_hook(set_param_strlen
, STR_LEN
);
402 add_hook(&match_string_assignment
, ASSIGNMENT_HOOK
);
404 add_modification_hook(my_strlen_id
, &set_strlen_undefined
);
405 add_merge_hook(my_strlen_id
, &merge_estates
);
406 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
407 add_member_info_callback(my_strlen_id
, struct_member_callback
);
408 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
410 add_function_hook("snprintf", &match_snprintf
, NULL
);
412 add_function_hook("strscpy", &match_strlcpycat
, NULL
);
413 add_function_hook("strlcpy", &match_strlcpycat
, NULL
);
414 add_function_hook("strlcat", &match_strlcpycat
, NULL
);
415 add_function_hook("strcpy", &match_strcpy
, NULL
);
417 * I would have made strchr only apply for success returns but some
418 * arches (arm64) implement strchr outside the kernel so we don't know
419 * the return values. Having a NULL with a strlen is fine because if
420 * someone uses the NULL then we're already in trouble.
422 add_function_param_key_hook("strchr", match_str_chr
, -1, "$", NULL
);
424 add_function_hook("__builtin_strcpy", &match_strcpy
, NULL
);
427 void register_strlen_equiv(int id
)
430 set_dynamic_states(my_equiv_id
);
431 add_function_assign_hook("strlen", &match_strlen
, NULL
);
432 add_function_assign_hook("__builtin_strlen", &match_strlen
, NULL
);
433 add_function_assign_hook("__fortify_strlen", &match_strlen
, NULL
);
434 add_modification_hook(my_equiv_id
, &set_undefined
);