struct_assignment: handle pointer to pointer assignments better
[smatch.git] / smatch_points_to_user_data.c
blob918c5b18c4ee3f2fe3e0c76f1de4a5fef13fc15f
1 /*
2 * Copyright (C) 2020 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
19 * The problem here is that we can have:
21 * p = skb->data;
23 * In the olden days we would just set "*p = 0-255" which meant that it pointed
24 * to user data. But then if we say "if (*p == 11) {" that means that "*p" is
25 * not user data any more, so then "*(p + 1)" is marked as not user data but it
26 * is.
28 * So now we've separated out the stuff that points to a user_buf from the other
29 * user data.
31 * There is a further complication because what if "p" points to a struct? In
32 * that case all the struct members are handled by smatch_kernel_user_data.c
33 * but we still need to keep in mind that "*(p + 1)" is user data. I'm not
34 * totally 100% sure how this will work.
36 * Generally a user pointer should be a void pointer, or an array etc. But if
37 * it points to a struct that can only be used for pointer math.
41 #include "smatch.h"
42 #include "smatch_slist.h"
43 #include "smatch_extra.h"
45 static int my_id;
46 STATE(user_data);
47 STATE(user_data_set);
49 struct user_fn_info {
50 const char *name;
51 int type;
52 int param;
53 const char *key;
56 // Old stuff that was here, but I no longer believe is user data
57 // kmap_atomic()
58 // skb_network_header
60 // add_function_hook("memcpy_fromiovec", &match_user_copy, INT_PTR(0));
61 // add_function_hook("usb_control_msg", &match_user_copy, INT_PTR(6));
63 static struct user_fn_info func_table[] = {
64 { "copy_from_user", USER_PTR_SET, 0, "$" },
65 { "__copy_from_user", USER_PTR_SET, 0, "$" },
66 { "kvm_read_guest_virt", USER_PTR_SET, 2, "$" },
67 { "vpu_iface_receive_msg", USER_PTR_SET, 1, "$" },
68 { "xdr_stream_decode_u32", USER_PTR_SET, 1, "$" },
70 { "(struct ksmbd_transport_ops)->read", USER_PTR_SET, 1, "$" },
71 { "nlmsg_data", USER_PTR_SET, -1, "$" },
72 { "nla_data", USER_PTR_SET, -1, "$" },
73 { "memdup_user", USER_PTR_SET, -1, "$" },
74 { "cfg80211_find_elem_match", USER_PTR_SET, -1, "$" },
75 { "ieee80211_bss_get_elem", USER_PTR_SET, -1, "$" },
76 { "cfg80211_find_elem", USER_PTR_SET, -1, "$" },
77 { "ieee80211_bss_get_ie", USER_PTR_SET, -1, "$" },
79 { "brcmf_fweh_dequeue_event", USER_PTR_SET, -1, "&$->emsg" },
80 { "wilc_wlan_rxq_remove", USER_PTR_SET, -1, "$->buffer" },
81 { "cfg80211_find_vendor_ie", USER_PTR_SET, -1, "$" },
83 { "xdr_inline_decode", USER_PTR_SET, -1, "$" },
84 { "ntfs_read_run_nb", USER_PTR_SET, 3, "$" },
85 { "ntfs_read_bh", USER_PTR_SET, 3, "(0<~$0)" },
87 { "kstrtoull", USER_PTR_SET, 2, "$" },
88 { "kstrtoll", USER_PTR_SET, 2, "$" },
89 { "kstrtoul", USER_PTR_SET, 2, "$" },
90 { "kstrtol", USER_PTR_SET, 2, "$" },
91 { "kstrtoint", USER_PTR_SET, 2, "$" },
92 { "kstrtou64", USER_PTR_SET, 2, "$" },
93 { "kstrtos64", USER_PTR_SET, 2, "$" },
94 { "kstrtou32", USER_PTR_SET, 2, "$" },
95 { "kstrtos32", USER_PTR_SET, 2, "$" },
96 { "kstrtou16", USER_PTR_SET, 2, "$" },
97 { "kstrtos16", USER_PTR_SET, 2, "$" },
98 { "kstrtou8", USER_PTR_SET, 2, "$" },
99 { "kstrtos8", USER_PTR_SET, 2, "$" },
100 { "kstrtoull_from_user", USER_PTR_SET, 2, "$" },
101 { "kstrtoll_from_user", USER_PTR_SET, 2, "$" },
102 { "kstrtoul_from_user", USER_PTR_SET, 2, "$" },
103 { "kstrtol_from_user", USER_PTR_SET, 2, "$" },
104 { "kstrtouint_from_user", USER_PTR_SET, 2, "$" },
105 { "kstrtoint_from_user", USER_PTR_SET, 2, "$" },
106 { "kstrtou16_from_user", USER_PTR_SET, 2, "$" },
107 { "kstrtos16_from_user", USER_PTR_SET, 2, "$" },
108 { "kstrtou8_from_user", USER_PTR_SET, 2, "$" },
109 { "kstrtos8_from_user", USER_PTR_SET, 2, "$" },
110 { "kstrtou64_from_user", USER_PTR_SET, 2, "$" },
111 { "kstrtos64_from_user", USER_PTR_SET, 2, "$" },
112 { "kstrtou32_from_user", USER_PTR_SET, 2, "$" },
113 { "kstrtos32_from_user", USER_PTR_SET, 2, "$" },
116 static struct user_fn_info call_table[] = {
117 { "__handle_ksmbd_work", USER_DATA, 0, "$->request_buf" },
120 bool is_skb_data(struct expression *expr)
122 struct symbol *sym;
124 expr = strip_expr(expr);
125 if (!expr)
126 return false;
127 if (expr->type != EXPR_DEREF)
128 return false;
130 if (!expr->member)
131 return false;
132 if (strcmp(expr->member->name, "data") != 0)
133 return false;
135 sym = get_type(expr->deref);
136 if (!sym)
137 return false;
138 if (sym->type == SYM_PTR)
139 sym = get_real_base_type(sym);
140 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
141 return false;
142 if (strcmp(sym->ident->name, "sk_buff") != 0)
143 return false;
145 return true;
148 static bool is_array_of_user_data(struct expression *expr)
150 struct expression *deref;
151 struct symbol *type;
153 if (expr->type == EXPR_PREOP && expr->op == '&') {
154 expr = strip_expr(expr->unop);
155 if (expr->type == EXPR_PREOP && expr->op == '*')
156 expr = strip_expr(expr->unop);
159 /* This is for array elements &foo->data[4] */
160 if (expr->type == EXPR_BINOP && expr->op == '+') {
161 if (points_to_user_data(expr->left))
162 return true;
163 if (points_to_user_data(expr->right))
164 return true;
167 /* This is for if you have: foo = skb->data; frob(foo->array); */
168 type = get_type(expr);
169 if (!type || type->type != SYM_ARRAY)
170 return false;
172 if (expr->type != EXPR_DEREF)
173 return false;
174 deref = strip_expr(expr->deref);
175 if (deref->type != EXPR_PREOP || deref->op != '*')
176 return false;
177 deref = strip_expr(deref->unop);
178 return points_to_user_data(deref);
181 static struct expression *remove_addr_stuff(struct expression *expr)
183 /* take "&foo->bar" and return "foo" */
184 expr = strip_expr(expr);
185 if (expr->type != EXPR_PREOP || expr->op != '&')
186 return expr;
187 expr = strip_expr(expr->unop);
188 while (expr && expr->type == EXPR_DEREF) {
189 expr = strip_expr(expr->deref);
190 if (expr->op == '.')
191 continue;
192 else
193 break;
195 if (expr->type == EXPR_PREOP && expr->op == '*')
196 expr = strip_expr(expr->unop);
197 return expr;
200 static bool math_points_to_user_data(struct expression *expr)
202 struct sm_state *sm;
204 // TODO: is_array_of_user_data() should probably be handled here
206 if (expr->type == EXPR_BINOP && expr->op == '+')
207 return math_points_to_user_data(expr->left);
209 expr = remove_addr_stuff(expr);
211 sm = get_sm_state_expr(my_id, expr);
212 if (!sm)
213 return false;
214 if (slist_has_state(sm->possible, &user_data) ||
215 slist_has_state(sm->possible, &user_data_set))
216 return true;
217 return false;
220 bool points_to_user_data(struct expression *expr)
222 struct sm_state *sm;
224 expr = strip_expr(expr);
225 if (!expr)
226 return false;
228 if (expr->type == EXPR_POSTOP)
229 expr = strip_expr(expr->unop);
231 if (is_fake_call(expr))
232 return false;
234 if (expr->type == EXPR_ASSIGNMENT)
235 return points_to_user_data(expr->left);
237 if (is_array_of_user_data(expr))
238 return true;
240 if (expr->type == EXPR_BINOP && expr->op == '+')
241 return math_points_to_user_data(expr);
243 if (is_skb_data(expr))
244 return true;
246 // FIXME if you have a struct pointer p then p->foo should be handled
247 // by smatch_kernel_user_data.c but if you have (p + 1)->foo then this
248 // should be handled here.
249 sm = get_sm_state_expr(my_id, expr);
250 if (!sm)
251 return false;
252 if (slist_has_state(sm->possible, &user_data) ||
253 slist_has_state(sm->possible, &user_data_set))
254 return true;
255 return false;
258 void set_array_user_ptr(struct expression *expr, bool is_new)
260 struct expression *tmp;
262 tmp = get_assigned_expr(expr);
263 if (tmp)
264 set_state_expr(my_id, tmp, is_new ? &user_data_set : &user_data);
265 set_state_expr(my_id, expr, is_new ? &user_data_set : &user_data);
268 static void match_assign(struct expression *expr)
270 if (is_fake_call(expr->right))
271 return;
273 if (!is_ptr_type(get_type(expr->left)))
274 return;
276 if (points_to_user_data(expr->right)) {
277 // FIXME: if the types are different then mark the stuff on
278 // the left as user data.
279 set_state_expr(my_id, expr->left, &user_data);
280 return;
283 // FIXME: just use a modification hook
284 if (get_state_expr(my_id, expr->left))
285 set_state_expr(my_id, expr->left, &undefined);
288 static void match_memcpy(const char *fn, struct expression *expr, void *_unused)
290 struct expression *dest, *src;
292 dest = get_argument_from_call_expr(expr->args, 0);
293 src = get_argument_from_call_expr(expr->args, 1);
295 if (points_to_user_data(src)) {
296 set_state_expr(my_id, expr->left, &user_data_set);
297 return;
300 if (get_state_expr(my_id, dest))
301 set_state_expr(my_id, dest, &undefined);
304 static void fake_assign_helper(struct expression *expr, void *data)
306 struct expression *left = expr->left;
307 struct symbol *type;
308 bool set = data;
310 type = get_type(left);
311 if (!type)
312 return;
313 if (type->type == SYM_BASETYPE)
314 mark_as_user_data(left, set);
315 else if (type->type == SYM_ARRAY)
316 set_array_user_ptr(left, set);
319 static void returns_user_ptr_helper(struct expression *expr, const char *name, struct symbol *sym, bool set)
321 struct expression *call, *arg;
323 call = expr;
324 while (call && call->type == EXPR_ASSIGNMENT)
325 call = strip_expr(expr->right);
326 if (!call || call->type != EXPR_CALL)
327 return;
329 if (!set && !we_pass_user_data(call))
330 return;
332 arg = gen_expression_from_name_sym(name, sym);
333 if (!arg)
334 return;
336 create_recursive_fake_assignments(deref_expression(arg), &fake_assign_helper, INT_PTR(set));
338 if (arg->type == EXPR_PREOP && arg->op == '&') {
339 struct symbol *type;
341 type = get_type(arg->unop);
342 if (!type || type->type != SYM_ARRAY)
343 return;
346 set_state_expr(my_id, arg, set ? &user_data_set : &user_data);
349 static void returns_user_ptr(struct expression *expr, const char *name, struct symbol *sym, void *data)
351 returns_user_ptr_helper(expr, name, sym, false);
354 static void returns_user_ptr_set(struct expression *expr, const char *name, struct symbol *sym, void *data)
356 returns_user_ptr_helper(expr, name, sym, true);
359 static void set_param_user_ptr(const char *name, struct symbol *sym, char *value)
361 set_state(my_id, name, sym, &user_data);
364 static void set_caller_param_key_user_ptr(struct expression *expr, const char *name,
365 struct symbol *sym, void *data)
367 set_state(my_id, name, sym, &user_data);
370 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
372 if (is_socket_stuff(sm->sym))
373 return;
375 if (!slist_has_state(sm->possible, &user_data) &&
376 !slist_has_state(sm->possible, &user_data_set))
377 return;
379 sql_insert_caller_info(call, USER_PTR, param, printed_name, "");
382 static void return_info_callback(int return_id, char *return_ranges,
383 struct expression *returned_expr,
384 int param,
385 const char *printed_name,
386 struct sm_state *sm)
388 int type;
390 /* is this even possible? */
391 if (strcmp(printed_name, "&$") == 0)
392 return;
394 if (is_socket_stuff(sm->sym))
395 return;
397 if (param >= 0) {
398 if (!slist_has_state(sm->possible, &user_data_set))
399 return;
400 type = USER_PTR_SET;
401 } else {
402 if (slist_has_state(sm->possible, &user_data_set))
403 type = USER_PTR_SET;
404 else if (slist_has_state(sm->possible, &user_data))
405 type = USER_PTR;
406 else
407 return;
409 if (parent_is_gone_var_sym(sm->name, sm->sym))
410 return;
412 sql_insert_return_states(return_id, return_ranges, type,
413 param, printed_name, "");
416 void register_points_to_user_data(int id)
418 struct user_fn_info *info;
419 int i;
421 my_id = id;
423 if (option_project != PROJ_KERNEL)
424 return;
426 add_hook(&match_assign, ASSIGNMENT_HOOK);
428 add_function_hook("memcpy", &match_memcpy, NULL);
429 add_function_hook("__memcpy", &match_memcpy, NULL);
431 add_caller_info_callback(my_id, caller_info_callback);
432 add_return_info_callback(my_id, return_info_callback);
434 select_caller_name_sym(set_param_user_ptr, USER_PTR);
435 for (i = 0; i < ARRAY_SIZE(call_table); i++) {
436 info = &call_table[i];
437 add_function_param_key_hook_early(info->name,
438 &set_caller_param_key_user_ptr,
439 info->param, info->key, info);
442 select_return_param_key(USER_PTR, &returns_user_ptr);
443 select_return_param_key(USER_PTR_SET, &returns_user_ptr_set);
444 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
445 info = &func_table[i];
446 add_function_param_key_hook_late(info->name,
447 &returns_user_ptr_set,
448 info->param, info->key, info);