smatch_kernel_host_data: enable additional debug
[smatch.git] / smatch_kernel_host_data.c
blob3cbc655d96adb4e8627d5453f3a9b91d2fc30e5c
1 /*
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
18 /* The below code is an adjustment of the smatch_kernel_user_data.c to
19 * track (untrusted and potentially malicious) data received by a guest
20 * kernel via various low-level port IO, MMIO, MSR, CPUID and PCI config
21 * space access functions (see func_table for a full list) from an
22 * untrusted host/VMM under confidential computing threat model
23 * (where a guest VM does not trust the host/VMM).
24 * We call this data as 'host' data here. Being able to track host data
25 * allows creating new smatch patterns that can perform various checks
26 * on such data, i.e. verify that that there are no spectre v1 gadgets
27 * on this attack surface or even simply report locations where host data
28 * is being processed in the kernel source code (useful for a targeted code
29 * audit).
30 * Similar as smatch_kernel_user_data.c it works with
31 * smatch_points_to_host_input.c code.
33 * Copyright (C) 2022 Elena Reshetova Intel Corporation.
36 #include "smatch.h"
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 struct host_fn_info {
41 const char *name;
42 int type;
43 int param;
44 const char *key;
45 const sval_t *implies_start, *implies_end;
46 func_hook *call_back;
49 static struct host_fn_info func_table[] = {
50 /* Functions that return host data as return value */
51 { "inb", HOST_DATA, -1, "$" },
52 { "inw", HOST_DATA, -1, "$" },
53 { "inl", HOST_DATA, -1, "$" },
54 { "__inb", HOST_DATA, -1, "$" },
55 { "__inw", HOST_DATA, -1, "$" },
56 { "__inl", HOST_DATA, -1, "$" },
57 { "ioread8", HOST_DATA, -1, "$" },
58 { "ioread16", HOST_DATA, -1, "$" },
59 { "ioread32", HOST_DATA, -1, "$" },
60 { "ioread16be", HOST_DATA, -1, "$" },
61 { "ioread32be", HOST_DATA, -1, "$" },
62 { "ioread64_lo_hi", HOST_DATA, -1, "$" },
63 { "ioread64_hi_lo", HOST_DATA, -1, "$" },
64 { "ioread64be_lo_hi", HOST_DATA, -1, "$" },
65 { "ioread64be_hi_lo", HOST_DATA, -1, "$" },
66 { "iomap_readq", HOST_DATA, -1, "$" },
67 { "iomap_readb", HOST_DATA, -1, "$" },
68 { "iomap_readw", HOST_DATA, -1, "$" },
69 { "iomap_readl", HOST_DATA, -1, "$" },
70 { "readb", HOST_DATA, -1, "$" },
71 { "readw", HOST_DATA, -1, "$" },
72 { "readl", HOST_DATA, -1, "$" },
73 { "readq", HOST_DATA, -1, "$" },
74 { "__raw_readb", HOST_DATA, -1, "$" },
75 { "__raw_readw", HOST_DATA, -1, "$" },
76 { "__raw_readl", HOST_DATA, -1, "$" },
77 { "__raw_readq", HOST_DATA, -1, "$" },
78 { "__readq", HOST_DATA, -1, "$" },
79 { "__readl", HOST_DATA, -1, "$" },
80 { "__readb", HOST_DATA, -1, "$" },
81 { "__readw", HOST_DATA, -1, "$" },
82 { "native_read_msr", HOST_DATA, -1, "$" },
83 { "native_read_msr_safe", HOST_DATA, -1, "$" },
84 { "__rdmsr", HOST_DATA, -1, "$" },
85 { "paravirt_read_msr", HOST_DATA, -1, "$" },
86 { "paravirt_read_msr_safe", HOST_DATA, -1, "$" },
87 /* the below 3 apic funcs are needed
88 * since they unwrap to apic->read(reg),
89 * apic->icr_read() and *(APIC_BASE + reg)*/
90 { "apic_read", HOST_DATA, -1, "$" },
91 { "apic_icr_read", HOST_DATA, -1, "$" },
92 { "native_apic_mem_read", HOST_DATA, -1, "$" },
93 /* the below one is x86_apic_ops.io_apic_read */
94 { "io_apic_read", HOST_DATA, -1, "$" },
95 /* virtio_cread8/16/32/64 funcs are needed
96 * since they call vdev->config->get */
97 { "virtio_cread8", HOST_DATA, -1, "$" },
98 { "virtio_cread16", HOST_DATA, -1, "$" },
99 { "virtio_cread32", HOST_DATA, -1, "$" },
100 { "virtio_cread64", HOST_DATA, -1, "$" },
101 { "__virtio16_to_cpu", HOST_DATA, -1, "$" },
102 { "__virtio32_to_cpu", HOST_DATA, -1, "$" },
103 { "__virtio64_to_cpu", HOST_DATA, -1, "$" },
104 { "serial_dl_read", HOST_DATA, -1, "$" },
105 { "serial_in", HOST_DATA, -1, "$" },
106 { "serial_port_in", HOST_DATA, -1, "$" },
107 { "cpuid_eax", HOST_DATA, -1, "$" },
108 { "cpuid_ebx", HOST_DATA, -1, "$" },
109 { "cpuid_ecx", HOST_DATA, -1, "$" },
110 { "cpuid_edx", HOST_DATA, -1, "$" },
111 /* Functions that return host data as argument 1 */
112 { "memcpy_fromio", HOST_DATA, 0, "*$" },
113 /* Functions that return host data as argument 2 */
114 { "acpi_os_read_iomem", HOST_DATA, 1, "*$" },
115 { "mmio_insb", HOST_DATA, 1, "*$" },
116 { "mmio_insw", HOST_DATA, 1, "*$" },
117 { "mmio_insl", HOST_DATA, 1, "*$" },
118 { "acpi_read_bit_register", HOST_DATA, 1, "*$" },
119 /* Functions that return host data as argument 3 */
120 { "rdmsrl_on_cpu", HOST_DATA, 2, "*$" },
121 { "rdmsrl_safe_on_cpu", HOST_DATA, 2, "*$" },
122 { "__virtio_cread_many", HOST_DATA, 2, "*$" },
123 { "pci_user_read_config_word", HOST_DATA, 2, "*$" },
124 { "pci_user_read_config_dword", HOST_DATA, 2, "*$" },
125 { "pci_user_read_config_byte", HOST_DATA, 2, "*$" },
126 /* Functions that return host data as argument 4 */
127 { "pci_bus_read_config_byte", HOST_DATA, 3, "*$" },
128 { "pci_bus_read_config_word", HOST_DATA, 3, "*$" },
129 { "pci_bus_read_config_dword", HOST_DATA, 3, "*$" },
130 /* Functions that return host data as arguments 3 and 4 */
131 { "rdmsr_on_cpu", HOST_DATA, 2, "*$" },
132 { "rdmsr_on_cpu", HOST_DATA, 3, "*$" },
133 { "rdmsr_safe_on_cpu", HOST_DATA, 2, "*$" },
134 { "rdmsr_safe_on_cpu", HOST_DATA, 3, "*$" },
135 /* Functions that return host data as argument 6 */
136 { "raw_pci_read", HOST_DATA, 5, "*$" },
137 /* Functions that return host data as arguments 2-5 */
138 { "cpuid", HOST_DATA, 1, "*$" },
139 { "cpuid", HOST_DATA, 2, "*$" },
140 { "cpuid", HOST_DATA, 3, "*$" },
141 { "cpuid", HOST_DATA, 4, "*$" },
142 /* Functions that return host data as arguments 3-6 */
143 { "cpuid_count", HOST_DATA, 2, "*$" },
144 { "cpuid_count", HOST_DATA, 3, "*$" },
145 { "cpuid_count", HOST_DATA, 4, "*$" },
146 { "cpuid_count", HOST_DATA, 5, "*$" },
149 static int my_id;
150 static unsigned long func_gets_host_data;
151 static struct stree *start_states;
153 static struct smatch_state *empty_state(struct sm_state *sm)
155 return alloc_estate_empty();
158 static struct smatch_state *new_state(struct symbol *type)
160 struct smatch_state *state;
162 if (!type || type_is_ptr(type))
163 return NULL;
165 state = alloc_estate_whole(type);
166 estate_set_new(state);
167 return state;
170 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
172 struct smatch_state *kernel = cur->state;
173 struct smatch_state *extra;
174 struct smatch_state *state;
175 struct range_list *rl;
177 extra = __get_state(SMATCH_EXTRA, cur->name, cur->sym);
178 if (!extra)
179 return;
180 rl = rl_intersection(estate_rl(kernel), estate_rl(extra));
181 state = alloc_estate_rl(clone_rl(rl));
182 if (estate_capped(kernel) || is_capped_var_sym(cur->name, cur->sym))
183 estate_set_capped(state);
184 if (estates_equiv(state, cur->state))
185 return;
186 if (estate_new(cur->state))
187 estate_set_new(state);
188 set_state(my_id, cur->name, cur->sym, state);
191 static void extra_nomod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
193 struct smatch_state *host, *new;
194 struct range_list *rl;
196 host = __get_state(my_id, name, sym);
197 if (!host)
198 return;
200 rl = rl_intersection(estate_rl(host), estate_rl(state));
201 if (rl_equiv(rl, estate_rl(host)))
202 return;
203 new = alloc_estate_rl(rl);
204 if (estate_capped(host))
205 estate_set_capped(new);
206 set_state(my_id, name, sym, new);
209 static void store_type_info(struct expression *expr, struct smatch_state *state)
211 struct symbol *type;
212 char *type_str, *member;
214 if (__in_fake_assign)
215 return;
217 if (!estate_rl(state))
218 return;
220 expr = strip_expr(expr);
221 if (!expr || expr->type != EXPR_DEREF || !expr->member)
222 return;
224 type = get_type(expr->deref);
225 if (!type || !type->ident)
226 return;
228 type_str = type_to_str(type);
229 if (!type_str)
230 return;
231 member = get_member_name(expr);
232 if (!member)
233 return;
235 sql_insert_function_type_info(HOST_DATA, type_str, member, state->name);
238 static void set_host_data(struct expression *expr, struct smatch_state *state)
240 store_type_info(expr, state);
241 set_state_expr(my_id, expr, state);
244 static bool host_rl_known(struct expression *expr)
246 struct range_list *rl;
247 sval_t close_to_max;
249 if (!get_host_rl(expr, &rl))
250 return true;
252 close_to_max = sval_type_max(rl_type(rl));
253 close_to_max.value -= 100;
255 if (sval_cmp(rl_max(rl), close_to_max) >= 0)
256 return false;
257 return true;
260 static bool is_array_index_mask_nospec(struct expression *expr)
262 struct expression *orig;
264 orig = get_assigned_expr(expr);
265 if (!orig || orig->type != EXPR_CALL)
266 return false;
267 return sym_name_is("array_index_mask_nospec", orig->fn);
270 static bool binop_capped(struct expression *expr)
272 struct range_list *left_rl;
273 int comparison;
274 sval_t sval;
276 if (expr->op == '-' && get_host_rl(expr->left, &left_rl)) {
277 if (host_rl_capped(expr->left))
278 return true;
279 comparison = get_comparison(expr->left, expr->right);
280 if (comparison && show_special(comparison)[0] == '>')
281 return true;
282 return false;
285 if (expr->op == '&' || expr->op == '%') {
286 bool left_host, left_capped, right_host, right_capped;
288 if (!get_value(expr->right, &sval) && is_capped(expr->right))
289 return true;
290 if (is_array_index_mask_nospec(expr->right))
291 return true;
292 if (is_capped(expr->left))
293 return true;
294 left_host = is_host_rl(expr->left);
295 right_host = is_host_rl(expr->right);
296 if (!left_host && !right_host)
297 return true;
299 left_capped = host_rl_capped(expr->left);
300 right_capped = host_rl_capped(expr->right);
302 if (left_host && left_capped) {
303 if (!right_host)
304 return true;
305 if (right_host && right_capped)
306 return true;
307 return false;
309 if (right_host && right_capped) {
310 if (!left_host)
311 return true;
312 return false;
314 return false;
318 * Generally "capped" means that we capped it to an unknown value.
319 * This is useful because if Smatch doesn't know what the value is then
320 * we have to trust that it is correct. But if we known cap value is
321 * 100 then we can check if 100 is correct and complain if it's wrong.
323 * So then the problem is with BINOP when we take a capped variable
324 * plus a host variable which is clamped to a known range (uncapped)
325 * the result should be capped.
327 if ((host_rl_capped(expr->left) || host_rl_known(expr->left)) &&
328 (host_rl_capped(expr->right) || host_rl_known(expr->right)))
329 return true;
330 return false;
333 bool host_rl_capped(struct expression *expr)
335 struct smatch_state *state;
336 struct range_list *rl;
337 sval_t sval;
339 expr = strip_expr(expr);
340 if (!expr)
341 return false;
342 if (get_value(expr, &sval))
343 return true;
344 if (expr->type == EXPR_BINOP)
345 return binop_capped(expr);
346 if ((expr->type == EXPR_PREOP || expr->type == EXPR_POSTOP) &&
347 (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT))
348 return host_rl_capped(expr->unop);
349 state = get_state_expr(my_id, expr);
350 if (state)
351 return estate_capped(state);
353 if (!get_host_rl(expr, &rl)) {
355 * The non host data parts of a binop are capped and
356 * also empty host rl states are capped.
358 return true;
361 if (rl_to_sval(rl, &sval))
362 return true;
364 return false; /* uncapped host data */
367 static void tag_inner_struct_members(struct expression *expr, struct symbol *member)
369 struct expression *edge_member;
370 struct symbol *base = get_real_base_type(member);
371 struct symbol *tmp;
373 if (member->ident)
374 expr = member_expression(expr, '.', member->ident);
376 FOR_EACH_PTR(base->symbol_list, tmp) {
377 struct symbol *type;
379 type = get_real_base_type(tmp);
380 if (!type)
381 continue;
383 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
384 tag_inner_struct_members(expr, tmp);
385 continue;
388 if (!tmp->ident)
389 continue;
391 edge_member = member_expression(expr, '.', tmp->ident);
392 set_host_data(edge_member, new_state(type));
393 } END_FOR_EACH_PTR(tmp);
396 static void tag_struct_members(struct symbol *type, struct expression *expr)
398 struct symbol *tmp;
399 struct expression *member;
400 int op = '*';
402 if (expr->type == EXPR_PREOP && expr->op == '&') {
403 expr = strip_expr(expr->unop);
404 op = '.';
407 FOR_EACH_PTR(type->symbol_list, tmp) {
408 type = get_real_base_type(tmp);
409 if (!type)
410 continue;
412 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
413 tag_inner_struct_members(expr, tmp);
414 continue;
417 if (!tmp->ident)
418 continue;
420 member = member_expression(expr, op, tmp->ident);
421 if (type->type == SYM_ARRAY) {
422 set_points_to_host_data(member);
423 } else {
424 set_host_data(member, new_state(get_type(member)));
426 } END_FOR_EACH_PTR(tmp);
429 static void tag_base_type(struct expression *expr)
431 if (expr->type == EXPR_PREOP && expr->op == '&')
432 expr = strip_expr(expr->unop);
433 else
434 expr = deref_expression(expr);
435 set_host_data(expr, new_state(get_type(expr)));
438 static void tag_as_host_data(struct expression *expr)
440 struct symbol *type;
442 expr = strip_expr(expr);
443 type = get_type(expr);
445 if (!type)
446 return;
448 if (type->type == SYM_PTR){
449 type = get_real_base_type(type);
450 if (!type)
451 return;
452 if (type == &void_ctype) {
453 set_host_data(deref_expression(expr), new_state(&ulong_ctype));
454 return;
456 if (type->type == SYM_BASETYPE) {
457 if (expr->type != EXPR_PREOP && expr->op != '&')
458 set_points_to_host_data(expr);
459 tag_base_type(expr);
460 return;
462 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
463 if (expr->type != EXPR_PREOP || expr->op != '&')
464 expr = deref_expression(expr);
465 else
466 set_host_data(deref_expression(expr), new_state(&ulong_ctype));
467 tag_struct_members(type, expr);
469 return;
472 if (expr->type == EXPR_DEREF) {
473 type = get_type(expr->deref);
474 expr = strip_expr(expr->deref);
476 if (!type){
477 sm_msg("%s: no type\n", __func__);
478 return;
482 if (type->type == SYM_BASETYPE) {
483 if (expr->type == EXPR_PREOP && expr->op == '*')
484 set_points_to_host_data(expr->unop);
485 set_host_data(expr, new_state(get_type(expr)));
486 return;
488 if (type->type == SYM_STRUCT || type->type == SYM_UNION) {
489 set_host_data(expr, new_state(&ulong_ctype));
490 tag_struct_members(type, expr);
494 static int get_rl_from_function(struct expression *expr, struct range_list **rl)
496 if (!expr)
497 return 0;
499 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL ||
500 !expr->fn->symbol_name || !expr->fn->symbol_name->name)
501 return 0;
503 for (int i = 0; i < ARRAY_SIZE(func_table); i++) {
504 if ((strcmp(expr->fn->symbol_name->name, func_table[i].name) == 0) &&
505 (func_table[i].param == -1)) {
506 *rl = alloc_whole_rl(get_type(expr));
507 return 1;
510 return 0;
513 static bool state_is_new(struct expression *expr)
515 struct smatch_state *state;
517 state = get_state_expr(my_id, expr);
518 if (estate_new(state))
519 return true;
521 if (expr->type == EXPR_BINOP) {
522 if (state_is_new(expr->left))
523 return true;
524 if (state_is_new(expr->right))
525 return true;
527 return false;
530 static void handle_derefed_pointers(struct expression *expr)
532 expr = strip_expr(expr);
533 if (expr->type != EXPR_PREOP ||
534 expr->op != '*')
535 return;
536 expr = strip_expr(expr->unop);
537 set_points_to_host_data(expr);
540 static bool handle_op_assign(struct expression *expr)
542 struct expression *binop_expr;
543 struct smatch_state *state;
544 struct range_list *rl;
546 switch (expr->op) {
547 case SPECIAL_ADD_ASSIGN:
548 case SPECIAL_SUB_ASSIGN:
549 case SPECIAL_AND_ASSIGN:
550 case SPECIAL_MOD_ASSIGN:
551 case SPECIAL_SHL_ASSIGN:
552 case SPECIAL_SHR_ASSIGN:
553 case SPECIAL_OR_ASSIGN:
554 case SPECIAL_XOR_ASSIGN:
555 case SPECIAL_MUL_ASSIGN:
556 case SPECIAL_DIV_ASSIGN:
557 binop_expr = binop_expression(expr->left,
558 op_remove_assign(expr->op),
559 expr->right);
560 if (!get_host_rl(binop_expr, &rl))
561 return true;
562 rl = cast_rl(get_type(expr->left), rl);
563 state = alloc_estate_rl(rl);
564 if (expr->op == SPECIAL_AND_ASSIGN ||
565 expr->op == SPECIAL_MOD_ASSIGN ||
566 host_rl_capped(binop_expr))
567 estate_set_capped(state);
568 if (state_is_new(binop_expr))
569 estate_set_new(state);
570 set_host_data(expr->left, state);
571 handle_derefed_pointers(expr->left);
572 return true;
574 return false;
577 static void match_assign_host(struct expression *expr)
579 struct symbol *left_type, *right_type;
580 struct range_list *rl;
581 static struct expression *handled;
582 struct smatch_state *state;
583 struct expression *faked;
584 bool is_capped = false;
585 bool is_new = false;
587 if (!expr)
588 return;
590 left_type = get_type(expr->left);
591 if (left_type == &void_ctype)
592 return;
594 faked = get_faked_expression();
596 /* FIXME: handle fake array assignments frob(&host_array[x]); */
598 if (is_fake_call(expr->right) && faked &&
599 faked->type == EXPR_ASSIGNMENT &&
600 points_to_host_data(faked->right)) {
601 rl = alloc_whole_rl(get_type(expr->left));
602 is_new = true;
603 goto set;
606 if (faked && faked == handled)
607 return;
608 if (is_fake_call(expr->right))
609 goto clear_old_state;
610 if (points_to_host_data(expr->right) &&
611 is_struct_ptr(get_type(expr->left))) {
612 handled = expr;
613 // This should be handled by smatch_points_to_host_data.c
614 //set_points_to_host_data(expr->left);
617 if (handle_op_assign(expr))
618 return;
619 if (expr->op != '=')
620 goto clear_old_state;
622 /* Handled by DB code */
623 if (expr->right->type == EXPR_CALL)
624 return;
626 if (!get_host_rl(expr->right, &rl))
627 goto clear_old_state;
629 is_capped = host_rl_capped(expr->right);
630 is_new = state_is_new(expr->right);
632 set:
633 if (type_is_ptr(left_type)) {
634 right_type = get_type(expr->right);
635 if (right_type && right_type->type == SYM_ARRAY)
636 set_points_to_host_data(expr->left);
637 return;
640 rl = cast_rl(left_type, rl);
641 state = alloc_estate_rl(rl);
642 if (is_new)
643 estate_set_new(state);
644 if (is_capped)
645 estate_set_capped(state);
646 set_host_data(expr->left, state);
647 handle_derefed_pointers(expr->left);
648 return;
650 clear_old_state:
653 * HACK ALERT!!! This should be at the start of the function. The
654 * the problem is that handling "pointer = array;" assignments is
655 * handled in this function instead of in kernel_points_to_host_data.c.
657 if (type_is_ptr(left_type))
658 return;
660 if (get_state_expr(my_id, expr->left))
661 set_host_data(expr->left, alloc_estate_empty());
664 static void handle_eq_noteq(struct expression *expr)
666 struct smatch_state *left_orig, *right_orig;
668 left_orig = get_state_expr(my_id, expr->left);
669 right_orig = get_state_expr(my_id, expr->right);
671 if (!left_orig && !right_orig)
672 return;
673 if (left_orig && right_orig)
674 return;
676 if (left_orig) {
677 set_true_false_states_expr(my_id, expr->left,
678 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
679 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
680 } else {
681 set_true_false_states_expr(my_id, expr->right,
682 expr->op == SPECIAL_EQUAL ? alloc_estate_empty() : NULL,
683 expr->op == SPECIAL_EQUAL ? NULL : alloc_estate_empty());
687 static struct range_list *strip_negatives(struct range_list *rl)
689 sval_t min = rl_min(rl);
690 sval_t minus_one = { .type = rl_type(rl), .value = -1 };
691 sval_t over = { .type = rl_type(rl), .value = INT_MAX + 1ULL };
692 sval_t max = sval_type_max(rl_type(rl));
694 if (!rl)
695 return NULL;
697 if (type_unsigned(rl_type(rl)) && type_bits(rl_type(rl)) > 31)
698 return remove_range(rl, over, max);
700 return remove_range(rl, min, minus_one);
703 static void handle_compare(struct expression *expr)
705 struct expression *left, *right;
706 struct range_list *left_rl = NULL;
707 struct range_list *right_rl = NULL;
708 struct range_list *host_rl;
709 struct smatch_state *capped_state;
710 struct smatch_state *left_true = NULL;
711 struct smatch_state *left_false = NULL;
712 struct smatch_state *right_true = NULL;
713 struct smatch_state *right_false = NULL;
714 struct symbol *type;
715 sval_t sval;
717 left = strip_expr(expr->left);
718 right = strip_expr(expr->right);
720 while (left->type == EXPR_ASSIGNMENT)
721 left = strip_expr(left->left);
724 * Conditions are mostly handled by smatch_extra.c, but there are some
725 * times where the exact values are not known so we can't do that.
727 * Normally, we might consider using smatch_capped.c to supliment smatch
728 * extra but that doesn't work when we merge unknown uncapped kernel
729 * data with unknown capped host data. The result is uncapped host
730 * data. We need to keep it separate and say that the host data is
731 * capped. In the past, I would have marked this as just regular
732 * kernel data (not host data) but we can't do that these days because
733 * we need to track host data for Spectre.
735 * The other situation which we have to handle is when we do have an
736 * int and we compare against an unknown unsigned kernel variable. In
737 * that situation we assume that the kernel data is less than INT_MAX.
738 * Otherwise then we get all sorts of array underflow false positives.
742 /* Handled in smatch_extra.c */
743 if (get_implied_value(left, &sval) ||
744 get_implied_value(right, &sval))
745 return;
747 get_host_rl(left, &left_rl);
748 get_host_rl(right, &right_rl);
750 /* nothing to do */
751 if (!left_rl && !right_rl)
752 return;
753 /* if both sides are host data that's not a good limit */
754 if (left_rl && right_rl)
755 return;
757 if (left_rl)
758 host_rl = left_rl;
759 else
760 host_rl = right_rl;
762 type = get_type(expr);
763 if (type_unsigned(type))
764 host_rl = strip_negatives(host_rl);
765 capped_state = alloc_estate_rl(host_rl);
766 estate_set_capped(capped_state);
768 switch (expr->op) {
769 case '<':
770 case SPECIAL_UNSIGNED_LT:
771 case SPECIAL_LTE:
772 case SPECIAL_UNSIGNED_LTE:
773 if (left_rl)
774 left_true = capped_state;
775 else
776 right_false = capped_state;
777 break;
778 case '>':
779 case SPECIAL_UNSIGNED_GT:
780 case SPECIAL_GTE:
781 case SPECIAL_UNSIGNED_GTE:
782 if (left_rl)
783 left_false = capped_state;
784 else
785 right_true = capped_state;
786 break;
788 set_true_false_states_expr(my_id, left, left_true, left_false);
789 set_true_false_states_expr(my_id, right, right_true, right_false);
792 static void match_condition_host(struct expression *expr)
794 if (!expr)
795 return;
797 if (expr->type != EXPR_COMPARE)
798 return;
800 if (expr->op == SPECIAL_EQUAL ||
801 expr->op == SPECIAL_NOTEQUAL) {
802 handle_eq_noteq(expr);
803 return;
805 handle_compare(expr);
808 static void match_returns_host_rl(const char *fn, struct expression *expr, void *unused)
810 func_gets_host_data = true;
813 static int has_host_data(struct symbol *sym)
815 struct sm_state *tmp;
817 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
818 if (tmp->sym == sym)
819 return 1;
820 } END_FOR_EACH_SM(tmp);
821 return 0;
824 bool we_pass_host_data(struct expression *call)
826 struct expression *arg;
827 struct symbol *sym;
829 FOR_EACH_PTR(call->args, arg) {
830 if (points_to_host_data(arg))
831 return true;
832 sym = expr_to_sym(arg);
833 if (!sym)
834 continue;
835 if (has_host_data(sym))
836 return true;
837 } END_FOR_EACH_PTR(arg);
839 return false;
842 static int db_returned_host_rl(struct expression *call, struct range_list **rl)
844 struct smatch_state *state;
845 char buf[48];
847 if (is_fake_call(call))
848 return 0;
849 snprintf(buf, sizeof(buf), "return %p", call);
850 state = get_state(my_id, buf, NULL);
851 if (!state || !estate_rl(state))
852 return 0;
853 *rl = estate_rl(state);
854 return 1;
857 struct stree *get_host_stree(void)
859 return get_all_states_stree(my_id);
862 static int host_data_flag;
863 static int no_host_data_flag;
865 struct range_list *var_host_rl(struct expression *expr)
867 struct smatch_state *state;
868 struct range_list *rl;
869 struct range_list *absolute_rl;
871 if (expr->type == EXPR_PREOP && expr->op == '&') {
872 no_host_data_flag = 1;
873 return NULL;
876 if (expr->type == EXPR_BINOP && expr->op == '%') {
877 struct range_list *left, *right;
879 if (!get_host_rl(expr->right, &right))
880 return NULL;
881 get_absolute_rl(expr->left, &left);
882 rl = rl_binop(left, '%', right);
883 goto found;
886 if (expr->type == EXPR_BINOP && expr->op == '/') {
887 struct range_list *left = NULL;
888 struct range_list *right = NULL;
889 struct range_list *abs_right;
891 get_host_rl(expr->left, &left);
892 get_host_rl(expr->right, &right);
893 get_absolute_rl(expr->right, &abs_right);
895 if (left && !right) {
896 rl = rl_binop(left, '/', abs_right);
897 if (sval_cmp(rl_max(left), rl_max(rl)) < 0)
898 no_host_data_flag = 1;
901 return NULL;
904 if (get_rl_from_function(expr, &rl))
905 goto found;
907 state = get_state_expr(my_id, expr);
908 if (state && estate_rl(state)) {
909 rl = estate_rl(state);
910 goto found;
913 if (expr->type == EXPR_CALL && db_returned_host_rl(expr, &rl))
914 goto found;
916 if (expr->type == EXPR_PREOP && expr->op == '*' &&
917 points_to_host_data(expr->unop)) {
918 rl = var_to_absolute_rl(expr);
919 goto found;
922 if (is_array(expr)) {
923 struct expression *array = get_array_base(expr);
925 if (!get_state_expr(my_id, array)) {
926 no_host_data_flag = 1;
927 return NULL;
931 return NULL;
932 found:
933 host_data_flag = 1;
934 absolute_rl = var_to_absolute_rl(expr);
935 return clone_rl(rl_intersection(rl, absolute_rl));
938 static bool is_ptr_subtract(struct expression *expr)
940 expr = strip_expr(expr);
941 if (!expr)
942 return false;
943 if (expr->type == EXPR_BINOP && expr->op == '-' &&
944 type_is_ptr(get_type(expr->left))) {
945 return true;
947 return false;
950 int get_host_rl(struct expression *expr, struct range_list **rl)
952 if (is_ptr_subtract(expr))
953 return 0;
955 host_data_flag = 0;
956 no_host_data_flag = 0;
957 custom_get_absolute_rl(expr, &var_host_rl, rl);
958 if (!host_data_flag || no_host_data_flag)
959 *rl = NULL;
960 return !!*rl;
963 int is_host_rl(struct expression *expr)
965 struct range_list *tmp;
967 return get_host_rl(expr, &tmp) && tmp;
970 int get_host_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl)
972 struct smatch_state *state;
974 state = get_state(my_id, name, sym);
975 if (state && estate_rl(state)) {
976 *rl = estate_rl(state);
977 return 1;
979 return 0;
982 static void return_info_callback_host(int return_id, char *return_ranges,
983 struct expression *returned_expr,
984 int param,
985 const char *printed_name,
986 struct sm_state *sm)
988 struct smatch_state *extra;
989 struct range_list *rl;
990 char buf[64];
992 if (param >= 0) {
993 if (strcmp(printed_name, "$") == 0)
994 return;
995 if (!param_was_set_var_sym(sm->name, sm->sym))
996 return;
998 rl = estate_rl(sm->state);
999 if (!rl)
1000 return;
1001 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
1002 if (estate_rl(extra))
1003 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
1004 if (!rl)
1005 return;
1007 snprintf(buf, sizeof(buf), "%s%s%s",
1008 show_rl(rl),
1009 estate_capped(sm->state) ? "[c]" : "", "");
1010 sql_insert_return_states(return_id, return_ranges,
1011 estate_new(sm->state) ? HOST_DATA_SET : HOST_DATA,
1012 param, printed_name, buf);
1016 static void caller_info_callback_host(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1018 struct smatch_state *state;
1019 struct range_list *rl;
1020 struct symbol *type;
1021 char buf[64];
1024 * Smatch uses a hack where if we get an unsigned long we say it's
1025 * both host data and it points to host data. But if we pass it to a
1026 * function which takes an int, then it's just host data. There's not
1027 * enough bytes for it to be a pointer.
1030 type = get_arg_type(call->fn, param);
1031 if (strcmp(printed_name, "$") != 0 && type && type_bits(type) < type_bits(&ptr_ctype))
1032 return;
1034 if (strcmp(sm->state->name, "") == 0)
1035 return;
1037 state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
1038 if (!state || !estate_rl(state))
1039 rl = estate_rl(sm->state);
1040 else
1041 rl = rl_intersection(estate_rl(sm->state), estate_rl(state));
1043 if (!rl)
1044 return;
1046 snprintf(buf, sizeof(buf), "%s%s%s", show_rl(rl),
1047 estate_capped(sm->state) ? "[c]" : "", "");
1048 sql_insert_caller_info(call, HOST_DATA, param, printed_name, buf);
1051 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1053 struct expression *arg;
1054 char *name;
1055 struct symbol *sym;
1056 struct smatch_state *state;
1058 while (expr->type == EXPR_ASSIGNMENT)
1059 expr = strip_expr(expr->right);
1060 if (expr->type != EXPR_CALL)
1061 return;
1063 arg = get_argument_from_call_expr(expr->args, param);
1064 if (!arg)
1065 return;
1067 name = get_variable_from_key(arg, key, &sym);
1068 if (!name || !sym)
1069 goto free;
1071 state = get_state(my_id, name, sym);
1072 if (!state)
1073 goto free;
1075 set_state(my_id, name, sym, alloc_estate_empty());
1076 free:
1077 free_string(name);
1080 static bool param_data_capped(const char *value)
1082 if (strstr(value, ",c") || strstr(value, "[c"))
1083 return true;
1084 return false;
1087 static void set_param_host_data(const char *name, struct symbol *sym, char *key, char *value)
1089 struct expression *expr;
1090 struct range_list *rl = NULL;
1091 struct smatch_state *state;
1092 struct symbol *type;
1093 char *fullname;
1095 expr = symbol_expression(sym);
1096 fullname = get_variable_from_key(expr, key, NULL);
1097 if (!fullname)
1098 return;
1100 type = get_member_type_from_key(expr, key);
1101 if (type && type->type == SYM_STRUCT)
1102 return;
1104 if (!type)
1105 return;
1107 str_to_rl(type, value, &rl);
1108 rl = swap_mtag_seed(expr, rl);
1109 state = alloc_estate_rl(rl);
1110 if (param_data_capped(value) || is_capped(expr))
1111 estate_set_capped(state);
1112 set_state(my_id, fullname, sym, state);
1115 #define OLD 0
1116 #define NEW 1
1118 static void store_host_data_return(struct expression *expr, char *key, char *value, bool is_new)
1120 struct smatch_state *state;
1121 struct range_list *rl;
1122 struct symbol *type;
1123 char buf[48];
1125 if (key[0] != '$')
1126 return;
1128 type = get_type(expr);
1129 snprintf(buf, sizeof(buf), "return %p%s", expr, key + 1);
1130 call_results_to_rl(expr, type, value, &rl);
1132 state = alloc_estate_rl(rl);
1133 if (is_new)
1134 estate_set_new(state);
1135 set_state(my_id, buf, NULL, state);
1138 static void set_to_host_data(struct expression *expr, char *key, char *value, bool is_new)
1140 struct smatch_state *state;
1141 char *name;
1142 struct symbol *sym;
1143 struct symbol *type;
1144 struct range_list *rl = NULL;
1146 type = get_member_type_from_key(expr, key);
1147 name = get_variable_from_key(expr, key, &sym);
1148 if (!name || !sym)
1149 goto free;
1151 call_results_to_rl(expr, type, value, &rl);
1153 state = alloc_estate_rl(rl);
1154 if (param_data_capped(value))
1155 estate_set_capped(state);
1156 if (is_new)
1157 estate_set_new(state);
1158 set_state(my_id, name, sym, state);
1159 free:
1160 free_string(name);
1163 static void returns_param_host_data(struct expression *expr, int param, char *key, char *value)
1165 struct expression *arg;
1166 struct expression *call;
1168 call = expr;
1169 while (call->type == EXPR_ASSIGNMENT)
1170 call = strip_expr(call->right);
1171 if (call->type != EXPR_CALL)
1172 return;
1174 if (!we_pass_host_data(call))
1175 return;
1177 if (param == -1) {
1178 if (expr->type != EXPR_ASSIGNMENT) {
1179 store_host_data_return(expr, key, value, OLD);
1180 return;
1182 set_to_host_data(expr->left, key, value, OLD);
1183 return;
1186 arg = get_argument_from_call_expr(call->args, param);
1187 if (!arg)
1188 return;
1189 set_to_host_data(arg, key, value, OLD);
1192 static void returns_param_host_data_set(struct expression *expr, int param, char *key, char *value)
1194 struct expression *arg;
1196 func_gets_host_data = true;
1198 if (param == -1) {
1199 if (expr->type != EXPR_ASSIGNMENT) {
1200 store_host_data_return(expr, key, value, NEW);
1201 return;
1203 set_to_host_data(expr->left, key, value, NEW);
1204 return;
1207 while (expr->type == EXPR_ASSIGNMENT)
1208 expr = strip_expr(expr->right);
1209 if (expr->type != EXPR_CALL)
1210 return;
1212 arg = get_argument_from_call_expr(expr->args, param);
1213 if (!arg)
1214 return;
1215 set_to_host_data(arg, key, value, NEW);
1218 static void returns_param_capped_host(struct expression *expr, int param, char *key, char *value)
1220 struct smatch_state *state, *new;
1221 struct symbol *sym;
1222 char *name;
1224 name = get_name_sym_from_param_key(expr, param, key, &sym);
1225 if (!name || !sym)
1226 goto free;
1228 state = get_state(my_id, name, sym);
1229 if (!state || estate_capped(state))
1230 goto free;
1232 new = clone_estate(state);
1233 estate_set_capped(new);
1235 set_state(my_id, name, sym, new);
1236 free:
1237 free_string(name);
1240 struct param_key_data {
1241 param_key_hook *call_back;
1242 int param;
1243 const char *key;
1244 void *info;
1247 static void match_function_def(struct symbol *sym)
1249 if (is_host_data_fn(sym))
1250 func_gets_host_data = true;
1253 static void set_param_host_input_data(struct expression *expr, const char *name,
1254 struct symbol *sym, void *data)
1256 struct expression *arg;
1258 func_gets_host_data = true;
1259 arg = gen_expression_from_name_sym(name, sym);
1260 tag_as_host_data(arg);
1261 return;
1264 void register_kernel_host_data(int id)
1266 int i;
1267 struct host_fn_info *info;
1269 my_id = id;
1271 if (option_project != PROJ_KERNEL)
1272 return;
1274 set_dynamic_states(my_id);
1275 add_function_data(&func_gets_host_data);
1276 add_hook(&match_function_def, FUNC_DEF_HOOK);
1278 add_function_data((unsigned long *)&start_states);
1279 add_unmatched_state_hook(my_id, &empty_state);
1280 add_extra_nomod_hook(&extra_nomod_hook);
1281 add_pre_merge_hook(my_id, &pre_merge_hook);
1282 add_merge_hook(my_id, &merge_estates);
1285 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
1286 info = &func_table[i];
1287 add_function_param_key_hook_late(info->name, &set_param_host_input_data,
1288 info->param, info->key, info);
1291 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
1292 if (func_table[i].param == -1)
1293 add_function_hook(func_table[i].name, &match_returns_host_rl, NULL);
1296 add_hook(&match_assign_host, ASSIGNMENT_HOOK);
1297 select_return_states_hook(PARAM_SET, &db_param_set);
1298 add_hook(&match_condition_host, CONDITION_HOOK);
1300 add_caller_info_callback(my_id, caller_info_callback_host);
1301 add_return_info_callback(my_id, return_info_callback_host);
1302 select_caller_info_hook(set_param_host_data, HOST_DATA);
1303 select_return_states_hook(HOST_DATA, &returns_param_host_data);
1304 select_return_states_hook(HOST_DATA_SET, &returns_param_host_data_set);
1305 select_return_states_hook(CAPPED_DATA, &returns_param_capped_host);