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.
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
30 static int my_call_id
;
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
);
46 expr
= member_expression(expr
, '.', member
->ident
);
48 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
51 type
= get_real_base_type(tmp
);
55 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
56 tag_inner_struct_members(expr
, tmp
);
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
)
73 struct expression
*member
;
76 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
77 expr
= strip_expr(expr
->unop
);
81 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
82 type
= get_real_base_type(tmp
);
86 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
87 tag_inner_struct_members(expr
, tmp
);
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
);
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
)
112 expr
= strip_expr(expr
);
114 type
= get_type(expr
);
115 if (!type
|| type
->type
!= SYM_PTR
)
117 type
= get_real_base_type(type
);
120 if (type
== &void_ctype
) {
121 set_state_expr(my_id
, deref_expression(expr
), alloc_estate_whole(&ulong_ctype
));
124 if (type
->type
== SYM_BASETYPE
)
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
);
142 tag_as_user_data(dest
);
145 static int points_to_user_data(struct expression
*expr
)
147 struct smatch_state
*state
;
153 name
= expr_to_var_sym(expr
, &sym
);
156 snprintf(buf
, sizeof(buf
), "*%s", name
);
157 state
= get_state(my_id
, buf
, sym
);
158 if (state
&& estate_rl(state
))
165 static int is_skb_data(struct expression
*expr
)
169 expr
= strip_expr(expr
);
170 if (!expr
|| expr
->type
!= EXPR_DEREF
)
175 if (strcmp(expr
->member
->name
, "data") != 0)
178 sym
= expr_to_sym(expr
->deref
);
181 sym
= get_real_base_type(sym
);
182 if (!sym
|| sym
->type
!= SYM_PTR
)
184 sym
= get_real_base_type(sym
);
185 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
187 if (strcmp(sym
->ident
->name
, "sk_buff") != 0)
193 static int comes_from_skb_data(struct expression
*expr
)
195 expr
= strip_expr(expr
);
200 switch (expr
->type
) {
202 if (comes_from_skb_data(expr
->left
))
204 if (comes_from_skb_data(expr
->right
))
208 return comes_from_skb_data(expr
->unop
);
210 if (is_skb_data(expr
))
212 return comes_from_skb_data(expr
->deref
);
219 static int handle_struct_assignment(struct expression
*expr
)
221 struct expression
*right
;
224 type
= get_type(expr
->left
);
225 if (!type
|| type
->type
!= SYM_PTR
)
227 type
= get_real_base_type(type
);
228 if (!type
|| type
->type
!= SYM_STRUCT
)
232 * Ignore struct to struct assignments because for those we look at the
233 * individual members.
235 right
= strip_expr(expr
->right
);
236 type
= get_type(right
);
237 if (!type
|| type
->type
!= SYM_PTR
)
239 type
= get_real_base_type(type
);
240 if (type
&& type
->type
== SYM_STRUCT
)
243 if (!points_to_user_data(right
) && !is_skb_data(right
))
246 tag_as_user_data(expr
->left
);
250 static int handle_get_user(struct expression
*expr
)
255 name
= get_macro_name(expr
->pos
);
256 if (!name
|| strcmp(name
, "get_user") != 0)
259 name
= expr_to_var(expr
->right
);
260 if (!name
|| strcmp(name
, "__val_gu") != 0)
262 set_state_expr(my_id
, expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
269 static void match_assign(struct expression
*expr
)
271 struct range_list
*rl
;
273 if (is_fake_call(expr
->right
))
275 if (handle_struct_assignment(expr
))
277 if (handle_get_user(expr
))
280 if (expr
->right
->type
== EXPR_CALL
||
281 !get_user_rl(expr
->right
, &rl
))
282 goto clear_old_state
;
284 rl
= cast_rl(get_type(expr
->left
), rl
);
285 set_state_expr(my_id
, expr
->left
, alloc_estate_rl(rl
));
290 if (get_state_expr(my_id
, expr
->left
))
291 set_state_expr(my_id
, expr
->left
, alloc_estate_empty());
294 static void match_user_assign_function(const char *fn
, struct expression
*expr
, void *unused
)
296 tag_as_user_data(expr
->left
);
299 static int get_user_macro_rl(struct expression
*expr
, struct range_list
**rl
)
305 macro
= get_macro_name(expr
->pos
);
310 if (strcmp(macro
, "ntohl") == 0) {
311 *rl
= alloc_whole_rl(&uint_ctype
);
314 if (strcmp(macro
, "ntohs") == 0) {
315 *rl
= alloc_whole_rl(&ushort_ctype
);
321 static int user_data_flag
;
322 static struct range_list
*var_user_rl(struct expression
*expr
)
324 struct smatch_state
*state
;
325 struct range_list
*rl
;
326 struct range_list
*absolute_rl
;
328 if (get_user_macro_rl(expr
, &rl
))
331 if (comes_from_skb_data(expr
)) {
332 rl
= alloc_whole_rl(get_type(expr
));
336 state
= get_state_expr(my_id
, expr
);
337 if (state
&& estate_rl(state
)) {
338 rl
= estate_rl(state
);
345 absolute_rl
= var_to_absolute_rl(expr
);
346 return clone_rl(rl_intersection(rl
, absolute_rl
));
349 int get_user_rl(struct expression
*expr
, struct range_list
**rl
)
353 custom_get_absolute_rl(expr
, &var_user_rl
, rl
);
354 if (!user_data_flag
) {
361 static void match_call_info(struct expression
*expr
)
363 struct range_list
*rl
;
364 struct expression
*arg
;
368 FOR_EACH_PTR(expr
->args
, arg
) {
371 if (!get_user_rl(arg
, &rl
))
374 sql_insert_caller_info(expr
, USER_DATA3
, i
, "$", show_rl(rl
));
376 } END_FOR_EACH_PTR(arg
);
379 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
381 struct smatch_state
*state
;
382 struct range_list
*rl
;
384 if (strcmp(sm
->state
->name
, "") == 0)
387 state
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
388 if (!state
|| !estate_rl(state
))
389 rl
= estate_rl(sm
->state
);
391 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(state
));
393 sql_insert_caller_info(call
, USER_DATA3
, param
, printed_name
, show_rl(rl
));
396 static void set_param_user_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
398 struct range_list
*rl
= NULL
;
399 struct smatch_state
*state
;
403 if (strcmp(key
, "*$") == 0)
404 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
405 else if (strncmp(key
, "$", 1) == 0)
406 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
410 type
= get_member_type_from_key(symbol_expression(sym
), key
);
412 /* if the caller passes a void pointer with user data */
413 if (strcmp(key
, "*$") == 0 && type
&& type
!= &void_ctype
) {
414 struct expression
*expr
= symbol_expression(sym
);
416 tag_as_user_data(expr
);
419 str_to_rl(type
, value
, &rl
);
420 state
= alloc_estate_rl(rl
);
421 set_state(my_id
, fullname
, sym
, state
);
424 static void set_called(const char *name
, struct symbol
*sym
, char *key
, char *value
)
426 set_state(my_call_id
, "this_function", NULL
, &called
);
429 static void match_syscall_definition(struct symbol
*sym
)
436 macro
= get_macro_name(sym
->pos
);
438 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
439 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
442 name
= get_function();
443 if (!option_no_db
&& get_state(my_call_id
, "this_function", NULL
) != &called
) {
444 if (name
&& strncmp(name
, "sys_", 4) == 0)
448 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
454 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
455 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
456 } END_FOR_EACH_PTR(arg
);
459 void check_user_data2(int id
)
463 if (option_project
!= PROJ_KERNEL
)
466 add_unmatched_state_hook(my_id
, &empty_state
);
467 add_merge_hook(my_id
, &merge_estates
);
469 add_function_hook("copy_from_user", &match_user_copy
, INT_PTR(0));
470 add_function_hook("__copy_from_user", &match_user_copy
, INT_PTR(0));
471 add_function_hook("memcpy_fromiovec", &match_user_copy
, INT_PTR(0));
472 add_function_hook("_kstrtoull", &match_user_copy
, INT_PTR(2));
474 add_function_assign_hook("kmemdup_user", &match_user_assign_function
, NULL
);
475 add_function_assign_hook("kmap_atomic", &match_user_assign_function
, NULL
);
477 add_hook(&match_syscall_definition
, AFTER_DEF_HOOK
);
479 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
481 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
482 add_member_info_callback(my_id
, struct_member_callback
);
483 select_caller_info_hook(set_param_user_data
, USER_DATA3
);
486 void check_user_data3(int id
)
490 if (option_project
!= PROJ_KERNEL
)
492 select_caller_info_hook(set_called
, INTERNAL
);