Debian Codegraph @

gcc/analyzer/sm-taint.cc

raw
1 /* A state machine for tracking "taint": unsanitized uses
2 of data potentially under an attacker's control.
3
4 Copyright (C) 2019-2025 Free Software Foundation, Inc.
5 Contributed by David Malcolm <[email protected]>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #define INCLUDE_VECTOR
25 #include "system.h"
26 #include "coretypes.h"
27 #include "make-unique.h"
28 #include "tree.h"
29 #include "function.h"
30 #include "basic-block.h"
31 #include "gimple.h"
32 #include "options.h"
33 #include "diagnostic-core.h"
34 #include "diagnostic-path.h"
35 #include "analyzer/analyzer.h"
36 #include "analyzer/analyzer-logging.h"
37 #include "gimple-iterator.h"
38 #include "ordered-hash-map.h"
39 #include "cgraph.h"
40 #include "cfg.h"
41 #include "digraph.h"
42 #include "stringpool.h"
43 #include "attribs.h"
44 #include "fold-const.h"
45 #include "analyzer/supergraph.h"
46 #include "analyzer/call-string.h"
47 #include "analyzer/program-point.h"
48 #include "analyzer/store.h"
49 #include "analyzer/region-model.h"
50 #include "analyzer/sm.h"
51 #include "analyzer/program-state.h"
52 #include "analyzer/pending-diagnostic.h"
53 #include "analyzer/constraint-manager.h"
54 #include "diagnostic-format-sarif.h"
55 #include "gcc-urlifier.h"
56
57 #if ENABLE_ANALYZER
58
59 namespace ana {
60
61 namespace {
62
63 /* An enum for describing tainted values. */
64
65 enum bounds
66 {
67 /* This tainted value has no upper or lower bound. */
68 BOUNDS_NONE,
69
70 /* This tainted value has an upper bound but not lower bound. */
71 BOUNDS_UPPER,
72
73 /* This tainted value has a lower bound but no upper bound. */
74 BOUNDS_LOWER
75 };
76
77 static const char *
78 bounds_to_str (enum bounds b)
79 {
80 switch (b)
81 {
82 default:
83 gcc_unreachable ();
84 case BOUNDS_NONE:
85 return "BOUNDS_NONE";
86 case BOUNDS_UPPER:
87 return "BOUNDS_UPPER";
88 case BOUNDS_LOWER:
89 return "BOUNDS_LOWER";
90 }
91 }
92
93 /* An experimental state machine, for tracking "taint": unsanitized uses
94 of data potentially under an attacker's control. */
95
96 class taint_state_machine : public state_machine
97 {
98 public:
99 taint_state_machine (logger *logger);
100
101 bool inherited_state_p () const final override { return true; }
102
103 state_t alt_get_inherited_state (const sm_state_map &map,
104 const svalue *sval,
105 const extrinsic_state &ext_state)
106 const final override;
107
108 bool
109 has_alt_get_inherited_state_p () const final override
110 {
111 return true;
112 }
113
114 bool on_stmt (sm_context &sm_ctxt,
115 const supernode *node,
116 const gimple *stmt) const final override;
117
118 void on_condition (sm_context &sm_ctxt,
119 const supernode *node,
120 const gimple *stmt,
121 const svalue *lhs,
122 enum tree_code op,
123 const svalue *rhs) const final override;
124 void on_bounded_ranges (sm_context &sm_ctxt,
125 const supernode *node,
126 const gimple *stmt,
127 const svalue &sval,
128 const bounded_ranges &ranges) const final override;
129
130 bool can_purge_p (state_t s) const final override;
131
132 bool get_taint (state_t s, tree type, enum bounds *out) const;
133
134 state_t combine_states (state_t s0, state_t s1) const;
135
136 private:
137 void check_control_flow_arg_for_taint (sm_context &sm_ctxt,
138 const gimple *stmt,
139 tree expr) const;
140
141 void check_for_tainted_size_arg (sm_context &sm_ctxt,
142 const supernode *node,
143 const gcall *call,
144 tree callee_fndecl) const;
145 void check_for_tainted_divisor (sm_context &sm_ctxt,
146 const supernode *node,
147 const gassign *assign) const;
148
149 public:
150 /* State for a "tainted" value: unsanitized data potentially under an
151 attacker's control. */
152 state_t m_tainted;
153
154 /* State for a "tainted" value that has a lower bound. */
155 state_t m_has_lb;
156
157 /* State for a "tainted" value that has an upper bound. */
158 state_t m_has_ub;
159
160 /* Stop state, for a value we don't want to track any more. */
161 state_t m_stop;
162
163 /* Global state, for when the last condition had tainted arguments. */
164 state_t m_tainted_control_flow;
165 };
166
167 /* Class for diagnostics relating to taint_state_machine. */
168
169 class taint_diagnostic : public pending_diagnostic
170 {
171 public:
172 taint_diagnostic (const taint_state_machine &sm, tree arg,
173 enum bounds has_bounds)
174 : m_sm (sm), m_arg (arg), m_has_bounds (has_bounds)
175 {}
176
177 bool subclass_equal_p (const pending_diagnostic &base_other) const override
178 {
179 const taint_diagnostic &other = (const taint_diagnostic &)base_other;
180 return (same_tree_p (m_arg, other.m_arg)
181 && m_has_bounds == other.m_has_bounds);
182 }
183
184 bool
185 describe_state_change (pretty_printer &pp,
186 const evdesc::state_change &change) override
187 {
188 if (change.m_new_state == m_sm.m_tainted)
189 {
190 if (change.m_origin)
191 {
192 pp_printf (&pp,
193 "%qE has an unchecked value here (from %qE)",
194 change.m_expr, change.m_origin);
195 return true;
196 }
197 else
198 {
199 pp_printf (&pp,
200 "%qE gets an unchecked value here",
201 change.m_expr);
202 return true;
203 }
204 }
205 else if (change.m_new_state == m_sm.m_has_lb)
206 {
207 pp_printf (&pp,
208 "%qE has its lower bound checked here",
209 change.m_expr);
210 return true;
211 }
212 else if (change.m_new_state == m_sm.m_has_ub)
213 {
214 pp_printf (&pp,
215 "%qE has its upper bound checked here",
216 change.m_expr);
217 return true;
218 }
219 return false;
220 }
221
222 diagnostic_event::meaning
223 get_meaning_for_state_change (const evdesc::state_change &change)
224 const final override
225 {
226 if (change.m_new_state == m_sm.m_tainted)
227 return diagnostic_event::meaning (diagnostic_event::VERB_acquire,
228 diagnostic_event::NOUN_taint);
229 return diagnostic_event::meaning ();
230 }
231
232 void maybe_add_sarif_properties (sarif_object &result_obj)
233 const override
234 {
235 sarif_property_bag &props = result_obj.get_or_create_properties ();
236 #define PROPERTY_PREFIX "gcc/analyzer/taint_diagnostic/"
237 props.set (PROPERTY_PREFIX "arg", tree_to_json (m_arg));
238 props.set_string (PROPERTY_PREFIX "has_bounds",
239 bounds_to_str (m_has_bounds));
240 #undef PROPERTY_PREFIX
241 }
242
243 protected:
244 const taint_state_machine &m_sm;
245 tree m_arg;
246 enum bounds m_has_bounds;
247 };
248
249 /* Concrete taint_diagnostic subclass for reporting attacker-controlled
250 array index. */
251
252 class tainted_array_index : public taint_diagnostic
253 {
254 public:
255 tainted_array_index (const taint_state_machine &sm, tree arg,
256 enum bounds has_bounds)
257 : taint_diagnostic (sm, arg, has_bounds)
258 {}
259
260 const char *get_kind () const final override { return "tainted_array_index"; }
261
262 int get_controlling_option () const final override
263 {
264 return OPT_Wanalyzer_tainted_array_index;
265 }
266
267 bool emit (diagnostic_emission_context &ctxt) final override
268 {
269 /* CWE-129: "Improper Validation of Array Index". */
270 ctxt.add_cwe (129);
271 if (m_arg)
272 switch (m_has_bounds)
273 {
274 default:
275 gcc_unreachable ();
276 case BOUNDS_NONE:
277 return ctxt.warn ("use of attacker-controlled value %qE"
278 " in array lookup without bounds checking",
279 m_arg);
280 break;
281 case BOUNDS_UPPER:
282 return ctxt.warn ("use of attacker-controlled value %qE"
283 " in array lookup without checking for negative",
284 m_arg);
285 break;
286 case BOUNDS_LOWER:
287 return ctxt.warn ("use of attacker-controlled value %qE"
288 " in array lookup without upper-bounds checking",
289 m_arg);
290 break;
291 }
292 else
293 switch (m_has_bounds)
294 {
295 default:
296 gcc_unreachable ();
297 case BOUNDS_NONE:
298 return ctxt.warn ("use of attacker-controlled value"
299 " in array lookup without bounds checking");
300 break;
301 case BOUNDS_UPPER:
302 return ctxt.warn ("use of attacker-controlled value"
303 " in array lookup without checking for"
304 " negative");
305 break;
306 case BOUNDS_LOWER:
307 return ctxt.warn ("use of attacker-controlled value"
308 " in array lookup without upper-bounds"
309 " checking");
310 break;
311 }
312 }
313
314 bool
315 describe_final_event (pretty_printer &pp,
316 const evdesc::final_event &) final override
317 {
318 if (m_arg)
319 switch (m_has_bounds)
320 {
321 default:
322 gcc_unreachable ();
323 case BOUNDS_NONE:
324 {
325 pp_printf (&pp,
326 "use of attacker-controlled value %qE in array lookup"
327 " without bounds checking",
328 m_arg);
329 return true;
330 }
331 case BOUNDS_UPPER:
332 {
333 pp_printf (&pp,
334 "use of attacker-controlled value %qE"
335 " in array lookup without checking for negative",
336 m_arg);
337 return true;
338 }
339 case BOUNDS_LOWER:
340 {
341 pp_printf (&pp,
342 "use of attacker-controlled value %qE"
343 " in array lookup without upper-bounds checking",
344 m_arg);
345 return true;
346 }
347 }
348 else
349 switch (m_has_bounds)
350 {
351 default:
352 gcc_unreachable ();
353 case BOUNDS_NONE:
354 {
355 pp_printf (&pp,
356 "use of attacker-controlled value in array lookup"
357 " without bounds checking");
358 return true;
359 }
360 case BOUNDS_UPPER:
361 {
362 pp_printf (&pp,
363 "use of attacker-controlled value"
364 " in array lookup without checking for negative");
365 return true;
366 }
367 case BOUNDS_LOWER:
368 {
369 pp_printf (&pp,
370 "use of attacker-controlled value"
371 " in array lookup without upper-bounds checking");
372 return true;
373 }
374 }
375 }
376 };
377
378 /* Concrete taint_diagnostic subclass for reporting attacker-controlled
379 pointer offset. */
380
381 class tainted_offset : public taint_diagnostic
382 {
383 public:
384 tainted_offset (const taint_state_machine &sm, tree arg,
385 enum bounds has_bounds,
386 const svalue *offset)
387 : taint_diagnostic (sm, arg, has_bounds),
388 m_offset (offset)
389 {}
390
391 const char *get_kind () const final override { return "tainted_offset"; }
392
393 int get_controlling_option () const final override
394 {
395 return OPT_Wanalyzer_tainted_offset;
396 }
397
398 bool emit (diagnostic_emission_context &ctxt) final override
399 {
400 /* CWE-823: "Use of Out-of-range Pointer Offset". */
401 ctxt.add_cwe (823);
402 if (m_arg)
403 switch (m_has_bounds)
404 {
405 default:
406 gcc_unreachable ();
407 case BOUNDS_NONE:
408 return ctxt.warn ("use of attacker-controlled value %qE as offset"
409 " without bounds checking",
410 m_arg);
411 break;
412 case BOUNDS_UPPER:
413 return ctxt.warn ("use of attacker-controlled value %qE as offset"
414 " without lower-bounds checking",
415 m_arg);
416 break;
417 case BOUNDS_LOWER:
418 return ctxt.warn ("use of attacker-controlled value %qE as offset"
419 " without upper-bounds checking",
420 m_arg);
421 break;
422 }
423 else
424 switch (m_has_bounds)
425 {
426 default:
427 gcc_unreachable ();
428 case BOUNDS_NONE:
429 return ctxt.warn ("use of attacker-controlled value as offset"
430 " without bounds checking");
431 break;
432 case BOUNDS_UPPER:
433 return ctxt.warn ("use of attacker-controlled value as offset"
434 " without lower-bounds checking");
435 break;
436 case BOUNDS_LOWER:
437 return ctxt.warn ("use of attacker-controlled value as offset"
438 " without upper-bounds checking");
439 break;
440 }
441 }
442
443 bool
444 describe_final_event (pretty_printer &pp,
445 const evdesc::final_event &) final override
446 {
447 if (m_arg)
448 switch (m_has_bounds)
449 {
450 default:
451 gcc_unreachable ();
452 case BOUNDS_NONE:
453 {
454 pp_printf (&pp,
455 "use of attacker-controlled value %qE"
456 " as offset without bounds checking",
457 m_arg);
458 return true;
459 }
460 case BOUNDS_UPPER:
461 {
462 pp_printf (&pp,
463 "use of attacker-controlled value %qE"
464 " as offset without lower-bounds checking",
465 m_arg);
466 return true;
467 }
468 case BOUNDS_LOWER:
469 {
470 pp_printf (&pp,
471 "use of attacker-controlled value %qE"
472 " as offset without upper-bounds checking",
473 m_arg);
474 return true;
475 }
476 }
477 else
478 switch (m_has_bounds)
479 {
480 default:
481 gcc_unreachable ();
482 case BOUNDS_NONE:
483 {
484 pp_printf (&pp,
485 "use of attacker-controlled value"
486 " as offset without bounds checking");
487 return true;
488 }
489 case BOUNDS_UPPER:
490 {
491 pp_printf (&pp,
492 "use of attacker-controlled value"
493 " as offset without lower-bounds"
494 " checking");
495 return true;
496 }
497 case BOUNDS_LOWER:
498 {
499 pp_printf (&pp,
500 "use of attacker-controlled value"
501 " as offset without upper-bounds"
502 " checking");
503 return true;
504 }
505 }
506 }
507
508 void maybe_add_sarif_properties (sarif_object &result_obj)
509 const final override
510 {
511 taint_diagnostic::maybe_add_sarif_properties (result_obj);
512 sarif_property_bag &props = result_obj.get_or_create_properties ();
513 #define PROPERTY_PREFIX "gcc/analyzer/tainted_offset/"
514 props.set (PROPERTY_PREFIX "offset", m_offset->to_json ());
515 #undef PROPERTY_PREFIX
516 }
517
518 private:
519 const svalue *m_offset;
520 };
521
522 /* Concrete taint_diagnostic subclass for reporting attacker-controlled
523 size. */
524
525 class tainted_size : public taint_diagnostic
526 {
527 public:
528 tainted_size (const taint_state_machine &sm, tree arg,
529 enum bounds has_bounds)
530 : taint_diagnostic (sm, arg, has_bounds)
531 {}
532
533 const char *get_kind () const override { return "tainted_size"; }
534
535 int get_controlling_option () const final override
536 {
537 return OPT_Wanalyzer_tainted_size;
538 }
539
540 bool emit (diagnostic_emission_context &ctxt) override
541 {
542 /* "CWE-129: Improper Validation of Array Index". */
543 ctxt.add_cwe (129);
544 if (m_arg)
545 switch (m_has_bounds)
546 {
547 default:
548 gcc_unreachable ();
549 case BOUNDS_NONE:
550 return ctxt.warn ("use of attacker-controlled value %qE as size"
551 " without bounds checking",
552 m_arg);
553 break;
554 case BOUNDS_UPPER:
555 return ctxt.warn ("use of attacker-controlled value %qE as size"
556 " without lower-bounds checking",
557 m_arg);
558 break;
559 case BOUNDS_LOWER:
560 return ctxt.warn ("use of attacker-controlled value %qE as size"
561 " without upper-bounds checking",
562 m_arg);
563 break;
564 }
565 else
566 switch (m_has_bounds)
567 {
568 default:
569 gcc_unreachable ();
570 case BOUNDS_NONE:
571 return ctxt.warn ("use of attacker-controlled value as size"
572 " without bounds checking");
573 break;
574 case BOUNDS_UPPER:
575 return ctxt.warn ("use of attacker-controlled value as size"
576 " without lower-bounds checking");
577 break;
578 case BOUNDS_LOWER:
579 return ctxt.warn ("use of attacker-controlled value as size"
580 " without upper-bounds checking");
581 break;
582 }
583 }
584
585 bool
586 describe_final_event (pretty_printer &pp,
587 const evdesc::final_event &) final override
588 {
589 if (m_arg)
590 switch (m_has_bounds)
591 {
592 default:
593 gcc_unreachable ();
594 case BOUNDS_NONE:
595 pp_printf (&pp,
596 "use of attacker-controlled value %qE"
597 " as size without bounds checking",
598 m_arg);
599 return true;
600 case BOUNDS_UPPER:
601 pp_printf (&pp,
602 "use of attacker-controlled value %qE"
603 " as size without lower-bounds checking",
604 m_arg);
605 return true;
606 case BOUNDS_LOWER:
607 pp_printf (&pp,
608 "use of attacker-controlled value %qE"
609 " as size without upper-bounds checking",
610 m_arg);
611 return true;
612 }
613 else
614 switch (m_has_bounds)
615 {
616 default:
617 gcc_unreachable ();
618 case BOUNDS_NONE:
619 pp_printf (&pp,
620 "use of attacker-controlled value"
621 " as size without bounds checking");
622 return true;
623 case BOUNDS_UPPER:
624 pp_printf (&pp,
625 "use of attacker-controlled value"
626 " as size without lower-bounds checking");
627 return true;
628 case BOUNDS_LOWER:
629 pp_printf (&pp,
630 "use of attacker-controlled value"
631 " as size without upper-bounds checking");
632 return true;
633 }
634 }
635 };
636
637 /* Subclass of tainted_size for reporting on tainted size values
638 passed to an external function annotated with attribute "access". */
639
640 class tainted_access_attrib_size : public tainted_size
641 {
642 public:
643 tainted_access_attrib_size (const taint_state_machine &sm, tree arg,
644 enum bounds has_bounds, tree callee_fndecl,
645 unsigned size_argno, const char *access_str)
646 : tainted_size (sm, arg, has_bounds),
647 m_callee_fndecl (callee_fndecl),
648 m_size_argno (size_argno), m_access_str (access_str)
649 {
650 }
651
652 const char *get_kind () const override
653 {
654 return "tainted_access_attrib_size";
655 }
656
657 bool emit (diagnostic_emission_context &ctxt) final override
658 {
659 bool warned = tainted_size::emit (ctxt);
660 if (warned)
661 {
662 auto_urlify_attributes sentinel;
663 inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
664 "parameter %i of %qD marked as a size via attribute %qs",
665 m_size_argno + 1, m_callee_fndecl, m_access_str);
666 }
667 return warned;
668 }
669
670 private:
671 tree m_callee_fndecl;
672 unsigned m_size_argno;
673 const char *m_access_str;
674 };
675
676 /* Concrete taint_diagnostic subclass for reporting attacker-controlled
677 divisor (so that an attacker can trigger a divide by zero). */
678
679 class tainted_divisor : public taint_diagnostic
680 {
681 public:
682 tainted_divisor (const taint_state_machine &sm, tree arg,
683 enum bounds has_bounds)
684 : taint_diagnostic (sm, arg, has_bounds)
685 {}
686
687 const char *get_kind () const final override { return "tainted_divisor"; }
688
689 int get_controlling_option () const final override
690 {
691 return OPT_Wanalyzer_tainted_divisor;
692 }
693
694 bool emit (diagnostic_emission_context &ctxt) final override
695 {
696 /* CWE-369: "Divide By Zero". */
697 ctxt.add_cwe (369);
698 if (m_arg)
699 return ctxt.warn ("use of attacker-controlled value %qE as divisor"
700 " without checking for zero",
701 m_arg);
702 else
703 return ctxt.warn ("use of attacker-controlled value as divisor"
704 " without checking for zero");
705 }
706
707 bool
708 describe_final_event (pretty_printer &pp,
709 const evdesc::final_event &) final override
710 {
711 if (m_arg)
712 pp_printf (&pp,
713 "use of attacker-controlled value %qE as divisor"
714 " without checking for zero",
715 m_arg);
716 else
717 pp_printf (&pp,
718 "use of attacker-controlled value as divisor"
719 " without checking for zero");
720 return true;
721 }
722 };
723
724 /* Concrete taint_diagnostic subclass for reporting attacker-controlled
725 size of a dynamic allocation. */
726
727 class tainted_allocation_size : public taint_diagnostic
728 {
729 public:
730 tainted_allocation_size (const taint_state_machine &sm, tree arg,
731 const svalue *size_in_bytes,
732 enum bounds has_bounds, enum memory_space mem_space)
733 : taint_diagnostic (sm, arg, has_bounds),
734 m_size_in_bytes (size_in_bytes),
735 m_mem_space (mem_space)
736 {
737 }
738
739 const char *get_kind () const final override
740 {
741 return "tainted_allocation_size";
742 }
743
744 bool subclass_equal_p (const pending_diagnostic &base_other) const override
745 {
746 if (!taint_diagnostic::subclass_equal_p (base_other))
747 return false;
748 const tainted_allocation_size &other
749 = (const tainted_allocation_size &)base_other;
750 return m_mem_space == other.m_mem_space;
751 }
752
753 int get_controlling_option () const final override
754 {
755 return OPT_Wanalyzer_tainted_allocation_size;
756 }
757
758 bool emit (diagnostic_emission_context &ctxt) final override
759 {
760 /* "CWE-789: Memory Allocation with Excessive Size Value". */
761 ctxt.add_cwe (789);
762
763 bool warned;
764 if (m_arg)
765 switch (m_has_bounds)
766 {
767 default:
768 gcc_unreachable ();
769 case BOUNDS_NONE:
770 warned = ctxt.warn ("use of attacker-controlled value %qE as"
771 " allocation size without bounds checking",
772 m_arg);
773 break;
774 case BOUNDS_UPPER:
775 warned = ctxt.warn ("use of attacker-controlled value %qE as"
776 " allocation size without"
777 " lower-bounds checking",
778 m_arg);
779 break;
780 case BOUNDS_LOWER:
781 warned = ctxt.warn ("use of attacker-controlled value %qE as"
782 " allocation size without"
783 " upper-bounds checking",
784 m_arg);
785 break;
786 }
787 else
788 switch (m_has_bounds)
789 {
790 default:
791 gcc_unreachable ();
792 case BOUNDS_NONE:
793 warned = ctxt.warn ("use of attacker-controlled value as"
794 " allocation size without bounds"
795 " checking");
796 break;
797 case BOUNDS_UPPER:
798 warned = ctxt.warn ("use of attacker-controlled value as"
799 " allocation size without"
800 " lower-bounds checking");
801 break;
802 case BOUNDS_LOWER:
803 warned = ctxt.warn ("use of attacker-controlled value as"
804 " allocation size without"
805 " upper-bounds checking");
806 break;
807 }
808 if (warned)
809 {
810 const location_t loc = ctxt.get_location ();
811 switch (m_mem_space)
812 {
813 default:
814 break;
815 case MEMSPACE_STACK:
816 inform (loc, "stack-based allocation");
817 break;
818 case MEMSPACE_HEAP:
819 inform (loc, "heap-based allocation");
820 break;
821 }
822 }
823 return warned;
824 }
825
826 bool
827 describe_final_event (pretty_printer &pp,
828 const evdesc::final_event &) final override
829 {
830 if (m_arg)
831 switch (m_has_bounds)
832 {
833 default:
834 gcc_unreachable ();
835 case BOUNDS_NONE:
836 pp_printf (&pp,
837 "use of attacker-controlled value %qE as allocation size"
838 " without bounds checking",
839 m_arg);
840 return true;
841 case BOUNDS_UPPER:
842 pp_printf (&pp,
843 "use of attacker-controlled value %qE as allocation size"
844 " without lower-bounds checking",
845 m_arg);
846 return true;
847 case BOUNDS_LOWER:
848 pp_printf (&pp,
849 "use of attacker-controlled value %qE as allocation size"
850 " without upper-bounds checking",
851 m_arg);
852 return true;
853 }
854 else
855 switch (m_has_bounds)
856 {
857 default:
858 gcc_unreachable ();
859 case BOUNDS_NONE:
860 pp_printf (&pp,
861 "use of attacker-controlled value as allocation size"
862 " without bounds checking");
863 return true;
864 case BOUNDS_UPPER:
865 pp_printf (&pp,
866 "use of attacker-controlled value as allocation size"
867 " without lower-bounds checking");
868 return true;
869 case BOUNDS_LOWER:
870 pp_printf (&pp,
871 "use of attacker-controlled value as allocation size"
872 " without upper-bounds checking");
873 return true;
874 }
875 }
876
877 void maybe_add_sarif_properties (sarif_object &result_obj)
878 const final override
879 {
880 taint_diagnostic::maybe_add_sarif_properties (result_obj);
881 sarif_property_bag &props = result_obj.get_or_create_properties ();
882 #define PROPERTY_PREFIX "gcc/analyzer/tainted_allocation_size/"
883 props.set (PROPERTY_PREFIX "size_in_bytes", m_size_in_bytes->to_json ());
884 #undef PROPERTY_PREFIX
885 }
886
887 private:
888 const svalue *m_size_in_bytes;
889 enum memory_space m_mem_space;
890 };
891
892 /* Concrete taint_diagnostic subclass for reporting attacker-controlled
893 value being used as part of the condition of an assertion. */
894
895 class tainted_assertion : public taint_diagnostic
896 {
897 public:
898 tainted_assertion (const taint_state_machine &sm, tree arg,
899 tree assert_failure_fndecl)
900 : taint_diagnostic (sm, arg, BOUNDS_NONE),
901 m_assert_failure_fndecl (assert_failure_fndecl)
902 {
903 gcc_assert (m_assert_failure_fndecl);
904 }
905
906 const char *get_kind () const final override
907 {
908 return "tainted_assertion";
909 }
910
911 bool subclass_equal_p (const pending_diagnostic &base_other) const override
912 {
913 if (!taint_diagnostic::subclass_equal_p (base_other))
914 return false;
915 const tainted_assertion &other
916 = (const tainted_assertion &)base_other;
917 return m_assert_failure_fndecl == other.m_assert_failure_fndecl;
918 }
919
920 int get_controlling_option () const final override
921 {
922 return OPT_Wanalyzer_tainted_assertion;
923 }
924
925 bool emit (diagnostic_emission_context &ctxt) final override
926 {
927 /* "CWE-617: Reachable Assertion". */
928 ctxt.add_cwe (617);
929
930 return ctxt.warn ("use of attacked-controlled value in"
931 " condition for assertion");
932 }
933
934 location_t fixup_location (location_t loc,
935 bool primary) const final override
936 {
937 if (primary)
938 /* For the primary location we want to avoid being in e.g. the
939 <assert.h> system header, since this would suppress the
940 diagnostic. */
941 return expansion_point_location_if_in_system_header (loc);
942 else if (in_system_header_at (loc))
943 /* For events, we want to show the implemenation of the assert
944 macro when we're describing them. */
945 return linemap_resolve_location (line_table, loc,
946 LRK_SPELLING_LOCATION,
947 NULL);
948 else
949 return pending_diagnostic::fixup_location (loc, primary);
950 }
951
952 bool
953 describe_state_change (pretty_printer &pp,
954 const evdesc::state_change &change) override
955 {
956 if (change.m_new_state == m_sm.m_tainted_control_flow)
957 {
958 pp_printf (&pp,
959 "use of attacker-controlled value for control flow");
960 return true;
961 }
962 return taint_diagnostic::describe_state_change (pp, change);
963 }
964
965 bool
966 describe_final_event (pretty_printer &pp,
967 const evdesc::final_event &) final override
968 {
969 if (mention_noreturn_attribute_p ())
970 pp_printf (&pp,
971 "treating %qE as an assertion failure handler"
972 " due to %<__attribute__((__noreturn__))%>",
973 m_assert_failure_fndecl);
974 else
975 pp_printf (&pp,
976 "treating %qE as an assertion failure handler",
977 m_assert_failure_fndecl);
978 return true;
979 }
980
981 private:
982 bool mention_noreturn_attribute_p () const
983 {
984 if (fndecl_built_in_p (m_assert_failure_fndecl, BUILT_IN_UNREACHABLE))
985 return false;
986 return true;
987 }
988
989 tree m_assert_failure_fndecl;
990 };
991
992 /* taint_state_machine's ctor. */
993
994 taint_state_machine::taint_state_machine (logger *logger)
995 : state_machine ("taint", logger),
996 m_tainted (add_state ("tainted")),
997 m_has_lb (add_state ("has_lb")),
998 m_has_ub (add_state ("has_ub")),
999 m_stop (add_state ("stop")),
1000 m_tainted_control_flow (add_state ("tainted-control-flow"))
1001 {
1002 }
1003
1004 state_machine::state_t
1005 taint_state_machine::alt_get_inherited_state (const sm_state_map &map,
1006 const svalue *sval,
1007 const extrinsic_state &ext_state)
1008 const
1009 {
1010 switch (sval->get_kind ())
1011 {
1012 default:
1013 break;
1014 case SK_UNARYOP:
1015 {
1016 const unaryop_svalue *unaryop_sval
1017 = as_a <const unaryop_svalue *> (sval);
1018 enum tree_code op = unaryop_sval->get_op ();
1019 const svalue *arg = unaryop_sval->get_arg ();
1020 switch (op)
1021 {
1022 case NOP_EXPR:
1023 {
1024 state_t arg_state = map.get_state (arg, ext_state);
1025 return arg_state;
1026 }
1027 default:
1028 break;
1029 }
1030 }
1031 break;
1032 case SK_BINOP:
1033 {
1034 const binop_svalue *binop_sval = as_a <const binop_svalue *> (sval);
1035 enum tree_code op = binop_sval->get_op ();
1036 const svalue *arg0 = binop_sval->get_arg0 ();
1037 const svalue *arg1 = binop_sval->get_arg1 ();
1038 switch (op)
1039 {
1040 default:
1041 break;
1042
1043 case EQ_EXPR:
1044 case GE_EXPR:
1045 case LE_EXPR:
1046 case NE_EXPR:
1047 case GT_EXPR:
1048 case LT_EXPR:
1049 case UNORDERED_EXPR:
1050 case ORDERED_EXPR:
1051 case PLUS_EXPR:
1052 case MINUS_EXPR:
1053 case MULT_EXPR:
1054 case POINTER_PLUS_EXPR:
1055 case TRUNC_DIV_EXPR:
1056 {
1057 state_t arg0_state = map.get_state (arg0, ext_state);
1058 state_t arg1_state = map.get_state (arg1, ext_state);
1059 return combine_states (arg0_state, arg1_state);
1060 }
1061 break;
1062
1063 case TRUNC_MOD_EXPR:
1064 {
1065 /* The left-hand side of X % Y can be sanitized by
1066 the operation. */
1067 return map.get_state (arg1, ext_state);
1068 }
1069 break;
1070
1071 case BIT_AND_EXPR:
1072 case RSHIFT_EXPR:
1073 return NULL;
1074 }
1075 }
1076 break;
1077 }
1078 return NULL;
1079 }
1080
1081 /* Return true iff FNDECL should be considered to be an assertion failure
1082 handler by -Wanalyzer-tainted-assertion. */
1083
1084 static bool
1085 is_assertion_failure_handler_p (tree fndecl)
1086 {
1087 // i.e. "noreturn"
1088 if (TREE_THIS_VOLATILE (fndecl))
1089 return true;
1090
1091 return false;
1092 }
1093
1094 /* Implementation of state_machine::on_stmt vfunc for taint_state_machine. */
1095
1096 bool
1097 taint_state_machine::on_stmt (sm_context &sm_ctxt,
1098 const supernode *node,
1099 const gimple *stmt) const
1100 {
1101 if (const gcall *call = dyn_cast <const gcall *> (stmt))
1102 if (tree callee_fndecl = sm_ctxt.get_fndecl_for_call (call))
1103 {
1104 if (is_named_call_p (callee_fndecl, "fread", call, 4))
1105 {
1106 tree arg = gimple_call_arg (call, 0);
1107
1108 sm_ctxt.on_transition (node, stmt, arg, m_start, m_tainted);
1109
1110 /* Dereference an ADDR_EXPR. */
1111 // TODO: should the engine do this?
1112 if (TREE_CODE (arg) == ADDR_EXPR)
1113 sm_ctxt.on_transition (node, stmt, TREE_OPERAND (arg, 0),
1114 m_start, m_tainted);
1115 return true;
1116 }
1117
1118 /* External function with "access" attribute. */
1119 if (sm_ctxt.unknown_side_effects_p ())
1120 check_for_tainted_size_arg (sm_ctxt, node, call, callee_fndecl);
1121
1122 if (is_assertion_failure_handler_p (callee_fndecl)
1123 && sm_ctxt.get_global_state () == m_tainted_control_flow)
1124 {
1125 sm_ctxt.warn (node, call, NULL_TREE,
1126 make_unique<tainted_assertion> (*this, NULL_TREE,
1127 callee_fndecl));
1128 }
1129 }
1130 // TODO: ...etc; many other sources of untrusted data
1131
1132 if (const gassign *assign = dyn_cast <const gassign *> (stmt))
1133 {
1134 enum tree_code op = gimple_assign_rhs_code (assign);
1135
1136 switch (op)
1137 {
1138 default:
1139 break;
1140 case TRUNC_DIV_EXPR:
1141 case CEIL_DIV_EXPR:
1142 case FLOOR_DIV_EXPR:
1143 case ROUND_DIV_EXPR:
1144 case TRUNC_MOD_EXPR:
1145 case CEIL_MOD_EXPR:
1146 case FLOOR_MOD_EXPR:
1147 case ROUND_MOD_EXPR:
1148 case RDIV_EXPR:
1149 case EXACT_DIV_EXPR:
1150 check_for_tainted_divisor (sm_ctxt, node, assign);
1151 break;
1152 }
1153 }
1154
1155 if (const gcond *cond = dyn_cast <const gcond *> (stmt))
1156 {
1157 /* Reset the state of "tainted-control-flow" before each
1158 control flow statement, so that only the last one before
1159 an assertion-failure-handler counts. */
1160 sm_ctxt.set_global_state (m_start);
1161 check_control_flow_arg_for_taint (sm_ctxt, cond, gimple_cond_lhs (cond));
1162 check_control_flow_arg_for_taint (sm_ctxt, cond, gimple_cond_rhs (cond));
1163 }
1164
1165 if (const gswitch *switch_ = dyn_cast <const gswitch *> (stmt))
1166 {
1167 /* Reset the state of "tainted-control-flow" before each
1168 control flow statement, so that only the last one before
1169 an assertion-failure-handler counts. */
1170 sm_ctxt.set_global_state (m_start);
1171 check_control_flow_arg_for_taint (sm_ctxt, switch_,
1172 gimple_switch_index (switch_));
1173 }
1174
1175 return false;
1176 }
1177
1178 /* If EXPR is tainted, mark this execution path with the
1179 "tainted-control-flow" global state, in case we're about
1180 to call an assertion-failure-handler. */
1181
1182 void
1183 taint_state_machine::check_control_flow_arg_for_taint (sm_context &sm_ctxt,
1184 const gimple *stmt,
1185 tree expr) const
1186 {
1187 const region_model *old_model = sm_ctxt.get_old_region_model ();
1188 const svalue *sval = old_model->get_rvalue (expr, NULL);
1189 state_t state = sm_ctxt.get_state (stmt, sval);
1190 enum bounds b;
1191 if (get_taint (state, TREE_TYPE (expr), &b))
1192 sm_ctxt.set_global_state (m_tainted_control_flow);
1193 }
1194
1195 /* Implementation of state_machine::on_condition vfunc for taint_state_machine.
1196 Potentially transition state 'tainted' to 'has_ub' or 'has_lb',
1197 and states 'has_ub' and 'has_lb' to 'stop'. */
1198
1199 void
1200 taint_state_machine::on_condition (sm_context &sm_ctxt,
1201 const supernode *node,
1202 const gimple *stmt,
1203 const svalue *lhs,
1204 enum tree_code op,
1205 const svalue *rhs) const
1206 {
1207 if (stmt == NULL)
1208 return;
1209
1210 if (lhs->get_kind () == SK_UNKNOWN
1211 || rhs->get_kind () == SK_UNKNOWN)
1212 {
1213 /* If we have a comparison against UNKNOWN, then
1214 we've presumably hit the svalue complexity limit,
1215 and we don't know what is being sanitized.
1216 Give up on any taint already found on this execution path. */
1217 // TODO: warn about this
1218 if (get_logger ())
1219 get_logger ()->log ("comparison against UNKNOWN; removing all taint");
1220 sm_ctxt.clear_all_per_svalue_state ();
1221 return;
1222 }
1223
1224 /* Strip away casts before considering LHS and RHS, to increase the
1225 chance of detecting places where sanitization of a value may have
1226 happened. */
1227 if (const svalue *inner = lhs->maybe_undo_cast ())
1228 lhs = inner;
1229 if (const svalue *inner = rhs->maybe_undo_cast ())
1230 rhs = inner;
1231
1232 // TODO
1233 switch (op)
1234 {
1235 //case NE_EXPR:
1236 //case EQ_EXPR:
1237 case GE_EXPR:
1238 case GT_EXPR:
1239 {
1240 /* (LHS >= RHS) or (LHS > RHS)
1241 LHS gains a lower bound
1242 RHS gains an upper bound. */
1243 sm_ctxt.on_transition (node, stmt, lhs, m_tainted, m_has_lb);
1244 sm_ctxt.on_transition (node, stmt, lhs, m_has_ub, m_stop);
1245 sm_ctxt.on_transition (node, stmt, rhs, m_tainted, m_has_ub);
1246 sm_ctxt.on_transition (node, stmt, rhs, m_has_lb, m_stop);
1247 }
1248 break;
1249 case LE_EXPR:
1250 case LT_EXPR:
1251 {
1252 /* Detect where build_range_check has optimized
1253 (c>=low) && (c<=high)
1254 into
1255 (c-low>=0) && (c-low<=high-low)
1256 and thus into:
1257 (unsigned)(c - low) <= (unsigned)(high-low). */
1258 if (const binop_svalue *binop_sval
1259 = lhs->dyn_cast_binop_svalue ())
1260 {
1261 const svalue *inner_lhs = binop_sval->get_arg0 ();
1262 enum tree_code inner_op = binop_sval->get_op ();
1263 const svalue *inner_rhs = binop_sval->get_arg1 ();
1264 if (const svalue *before_cast = inner_lhs->maybe_undo_cast ())
1265 inner_lhs = before_cast;
1266 if (tree outer_rhs_cst = rhs->maybe_get_constant ())
1267 if (tree inner_rhs_cst = inner_rhs->maybe_get_constant ())
1268 if (inner_op == PLUS_EXPR
1269 && TREE_CODE (inner_rhs_cst) == INTEGER_CST
1270 && TREE_CODE (outer_rhs_cst) == INTEGER_CST
1271 && TYPE_UNSIGNED (TREE_TYPE (inner_rhs_cst))
1272 && TYPE_UNSIGNED (TREE_TYPE (outer_rhs_cst)))
1273 {
1274 /* We have
1275 (unsigned)(INNER_LHS + CST_A) </<= UNSIGNED_CST_B
1276 and thus an optimized test of INNER_LHS (before any
1277 cast to unsigned) against a range.
1278 Transition any of the tainted states to the stop state.
1279 We have to special-case this here rather than in
1280 region_model::on_condition since we can't apply
1281 both conditions simultaneously (we'd have a transition
1282 from the old state to has_lb, then a transition from
1283 the old state *again* to has_ub). */
1284 state_t old_state
1285 = sm_ctxt.get_state (stmt, inner_lhs);
1286 if (old_state == m_tainted
1287 || old_state == m_has_lb
1288 || old_state == m_has_ub)
1289 sm_ctxt.set_next_state (stmt, inner_lhs, m_stop);
1290 return;
1291 }
1292 }
1293
1294 /* (LHS <= RHS) or (LHS < RHS)
1295 LHS gains an upper bound
1296 RHS gains a lower bound. */
1297 sm_ctxt.on_transition (node, stmt, lhs, m_tainted, m_has_ub);
1298 sm_ctxt.on_transition (node, stmt, lhs, m_has_lb, m_stop);
1299 sm_ctxt.on_transition (node, stmt, rhs, m_tainted, m_has_lb);
1300 sm_ctxt.on_transition (node, stmt, rhs, m_has_ub, m_stop);
1301 }
1302 break;
1303 default:
1304 break;
1305 }
1306 }
1307
1308 /* Implementation of state_machine::on_bounded_ranges vfunc for
1309 taint_state_machine, for handling switch statement cases.
1310 Potentially transition state 'tainted' to 'has_ub' or 'has_lb',
1311 and states 'has_ub' and 'has_lb' to 'stop'. */
1312
1313 void
1314 taint_state_machine::on_bounded_ranges (sm_context &sm_ctxt,
1315 const supernode *,
1316 const gimple *stmt,
1317 const svalue &sval,
1318 const bounded_ranges &ranges) const
1319 {
1320 gcc_assert (!ranges.empty_p ());
1321 gcc_assert (ranges.get_count () > 0);
1322
1323 /* We have one or more ranges; this could be a "default:", or one or
1324 more single or range cases.
1325
1326 Look at the overall endpoints to see if the ranges impose any lower
1327 bounds or upper bounds beyond those of the underlying numeric type. */
1328
1329 tree lowest_bound = ranges.get_range (0).m_lower;
1330 tree highest_bound = ranges.get_range (ranges.get_count () - 1).m_upper;
1331 gcc_assert (lowest_bound);
1332 gcc_assert (highest_bound);
1333
1334 bool ranges_have_lb
1335 = (lowest_bound != TYPE_MIN_VALUE (TREE_TYPE (lowest_bound)));
1336 bool ranges_have_ub
1337 = (highest_bound != TYPE_MAX_VALUE (TREE_TYPE (highest_bound)));
1338
1339 if (!ranges_have_lb && !ranges_have_ub)
1340 return;
1341
1342 /* We have new bounds from the ranges; combine them with any
1343 existing bounds on SVAL. */
1344 state_t old_state = sm_ctxt.get_state (stmt, &sval);
1345 if (old_state == m_tainted)
1346 {
1347 if (ranges_have_lb && ranges_have_ub)
1348 sm_ctxt.set_next_state (stmt, &sval, m_stop);
1349 else if (ranges_have_lb)
1350 sm_ctxt.set_next_state (stmt, &sval, m_has_lb);
1351 else if (ranges_have_ub)
1352 sm_ctxt.set_next_state (stmt, &sval, m_has_ub);
1353 }
1354 else if (old_state == m_has_ub && ranges_have_lb)
1355 sm_ctxt.set_next_state (stmt, &sval, m_stop);
1356 else if (old_state == m_has_lb && ranges_have_ub)
1357 sm_ctxt.set_next_state (stmt, &sval, m_stop);
1358 }
1359
1360 bool
1361 taint_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const
1362 {
1363 if (s == m_has_lb || s == m_has_ub)
1364 return false;
1365
1366 return true;
1367 }
1368
1369 /* If STATE is a tainted state, write the bounds to *OUT and return true.
1370 Otherwise return false.
1371 Use the signedness of TYPE to determine if "has_ub" is tainted. */
1372
1373 bool
1374 taint_state_machine::get_taint (state_t state, tree type,
1375 enum bounds *out) const
1376 {
1377 /* Unsigned types have an implicit lower bound. */
1378 bool is_unsigned = false;
1379 if (type)
1380 if (INTEGRAL_TYPE_P (type))
1381 is_unsigned = TYPE_UNSIGNED (type);
1382
1383 /* Can't use a switch as the states are non-const. */
1384 if (state == m_tainted)
1385 {
1386 *out = is_unsigned ? BOUNDS_LOWER : BOUNDS_NONE;
1387 return true;
1388 }
1389 else if (state == m_has_lb)
1390 {
1391 *out = BOUNDS_LOWER;
1392 return true;
1393 }
1394 else if (state == m_has_ub && !is_unsigned)
1395 {
1396 /* Missing lower bound. */
1397 *out = BOUNDS_UPPER;
1398 return true;
1399 }
1400 return false;
1401 }
1402
1403 /* Find the most tainted state of S0 and S1. */
1404
1405 state_machine::state_t
1406 taint_state_machine::combine_states (state_t s0, state_t s1) const
1407 {
1408 gcc_assert (s0);
1409 gcc_assert (s1);
1410 if (s0 == s1)
1411 return s0;
1412 if (s0 == m_tainted || s1 == m_tainted)
1413 return m_tainted;
1414 if (s0 == m_start)
1415 return s1;
1416 if (s1 == m_start)
1417 return s0;
1418 if (s0 == m_stop)
1419 return s1;
1420 if (s1 == m_stop)
1421 return s0;
1422 /* The only remaining combinations are one of has_ub and has_lb
1423 (in either order). */
1424 gcc_assert ((s0 == m_has_lb && s1 == m_has_ub)
1425 || (s0 == m_has_ub && s1 == m_has_lb));
1426 return m_tainted;
1427 }
1428
1429 /* Check for calls to external functions marked with
1430 __attribute__((access)) with a size-index: complain about
1431 tainted values passed as a size to such a function. */
1432
1433 void
1434 taint_state_machine::check_for_tainted_size_arg (sm_context &sm_ctxt,
1435 const supernode *node,
1436 const gcall *call,
1437 tree callee_fndecl) const
1438 {
1439 tree fntype = TREE_TYPE (callee_fndecl);
1440 if (!fntype)
1441 return;
1442
1443 if (!TYPE_ATTRIBUTES (fntype))
1444 return;
1445
1446 /* Initialize a map of attribute access specifications for arguments
1447 to the function call. */
1448 rdwr_map rdwr_idx;
1449 init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
1450
1451 unsigned argno = 0;
1452
1453 for (tree iter = TYPE_ARG_TYPES (fntype); iter;
1454 iter = TREE_CHAIN (iter), ++argno)
1455 {
1456 const attr_access* access = rdwr_idx.get (argno);
1457 if (!access)
1458 continue;
1459
1460 /* Ignore any duplicate entry in the map for the size argument. */
1461 if (access->ptrarg != argno)
1462 continue;
1463
1464 if (access->sizarg == UINT_MAX)
1465 continue;
1466
1467 tree size_arg = gimple_call_arg (call, access->sizarg);
1468
1469 state_t state = sm_ctxt.get_state (call, size_arg);
1470 enum bounds b;
1471 if (get_taint (state, TREE_TYPE (size_arg), &b))
1472 {
1473 const char* const access_str =
1474 TREE_STRING_POINTER (access->to_external_string ());
1475 tree diag_size = sm_ctxt.get_diagnostic_tree (size_arg);
1476 sm_ctxt.warn (node, call, size_arg,
1477 make_unique<tainted_access_attrib_size>
1478 (*this, diag_size, b,
1479 callee_fndecl,
1480 access->sizarg,
1481 access_str));
1482 }
1483 }
1484 }
1485
1486 /* Complain if ASSIGN (a division operation) has a tainted divisor
1487 that could be zero. */
1488
1489 void
1490 taint_state_machine::check_for_tainted_divisor (sm_context &sm_ctxt,
1491 const supernode *node,
1492 const gassign *assign) const
1493 {
1494 const region_model *old_model = sm_ctxt.get_old_region_model ();
1495 if (!old_model)
1496 return;
1497
1498 tree divisor_expr = gimple_assign_rhs2 (assign);;
1499
1500 /* Until we track conditions on floating point values, we can't check to
1501 see if they've been checked against zero. */
1502 if (!INTEGRAL_TYPE_P (TREE_TYPE (divisor_expr)))
1503 return;
1504
1505 const svalue *divisor_sval = old_model->get_rvalue (divisor_expr, NULL);
1506
1507 state_t state = sm_ctxt.get_state (assign, divisor_sval);
1508 enum bounds b;
1509 if (get_taint (state, TREE_TYPE (divisor_expr), &b))
1510 {
1511 const svalue *zero_sval
1512 = old_model->get_manager ()->get_or_create_int_cst
1513 (TREE_TYPE (divisor_expr), 0);
1514 tristate ts
1515 = old_model->eval_condition (divisor_sval, NE_EXPR, zero_sval);
1516 if (ts.is_true ())
1517 /* The divisor is known to not equal 0: don't warn. */
1518 return;
1519
1520 tree diag_divisor = sm_ctxt.get_diagnostic_tree (divisor_expr);
1521 sm_ctxt.warn (node, assign, divisor_expr,
1522 make_unique <tainted_divisor> (*this, diag_divisor, b));
1523 sm_ctxt.set_next_state (assign, divisor_sval, m_stop);
1524 }
1525 }
1526
1527 } // anonymous namespace
1528
1529 /* Internal interface to this file. */
1530
1531 state_machine *
1532 make_taint_state_machine (logger *logger)
1533 {
1534 return new taint_state_machine (logger);
1535 }
1536
1537 /* A closed concrete range. */
1538
1539 class concrete_range
1540 {
1541 public:
1542 /* Return true iff THIS is fully within OTHER
1543 i.e.
1544 - m_min must be >= OTHER.m_min
1545 - m_max must be <= OTHER.m_max. */
1546 bool within_p (const concrete_range &other) const
1547 {
1548 if (compare_constants (m_min, GE_EXPR, other.m_min).is_true ())
1549 if (compare_constants (m_max, LE_EXPR, other.m_max).is_true ())
1550 return true;
1551 return false;
1552 }
1553
1554 tree m_min;
1555 tree m_max;
1556 };
1557
1558 /* Attempt to get a closed concrete range for SVAL based on types.
1559 If found, write to *OUT and return true.
1560 Otherwise return false. */
1561
1562 static bool
1563 get_possible_range (const svalue *sval, concrete_range *out)
1564 {
1565 if (const svalue *inner = sval->maybe_undo_cast ())
1566 {
1567 concrete_range inner_range;
1568 if (!get_possible_range (inner, &inner_range))
1569 return false;
1570
1571 if (sval->get_type ()
1572 && inner->get_type ()
1573 && INTEGRAL_TYPE_P (sval->get_type ())
1574 && INTEGRAL_TYPE_P (inner->get_type ())
1575 && TYPE_UNSIGNED (inner->get_type ())
1576 && (TYPE_PRECISION (sval->get_type ())
1577 > TYPE_PRECISION (inner->get_type ())))
1578 {
1579 /* We have a cast from an unsigned type to a wider integral type.
1580 Assuming this is zero-extension, we can inherit the range from
1581 the inner type. */
1582 enum tree_code op = ((const unaryop_svalue *)sval)->get_op ();
1583 out->m_min = fold_unary (op, sval->get_type (), inner_range.m_min);
1584 out->m_max = fold_unary (op, sval->get_type (), inner_range.m_max);
1585 return true;
1586 }
1587 }
1588
1589 if (sval->get_type ()
1590 && INTEGRAL_TYPE_P (sval->get_type ()))
1591 {
1592 out->m_min = TYPE_MIN_VALUE (sval->get_type ());
1593 out->m_max = TYPE_MAX_VALUE (sval->get_type ());
1594 return true;
1595 }
1596
1597 return false;
1598 }
1599
1600 /* Determine if it's possible for tainted array access ELEMENT_REG to
1601 actually be a problem.
1602
1603 Check here for index being from e.g. unsigned char when the array
1604 contains >= 255 elements.
1605
1606 Return true if out-of-bounds is possible, false if it's impossible
1607 (for suppressing false positives). */
1608
1609 static bool
1610 index_can_be_out_of_bounds_p (const element_region *element_reg)
1611 {
1612 const svalue *index = element_reg->get_index ();
1613 const region *array_reg = element_reg->get_parent_region ();
1614
1615 if (array_reg->get_type ()
1616 && TREE_CODE (array_reg->get_type ()) == ARRAY_TYPE
1617 && TYPE_DOMAIN (array_reg->get_type ())
1618 && INTEGRAL_TYPE_P (TYPE_DOMAIN (array_reg->get_type ())))
1619 {
1620 concrete_range valid_index_range;
1621 valid_index_range.m_min
1622 = TYPE_MIN_VALUE (TYPE_DOMAIN (array_reg->get_type ()));
1623 valid_index_range.m_max
1624 = TYPE_MAX_VALUE (TYPE_DOMAIN (array_reg->get_type ()));
1625
1626 concrete_range possible_index_range;
1627 if (get_possible_range (index, &possible_index_range))
1628 if (possible_index_range.within_p (valid_index_range))
1629 return false;
1630 }
1631
1632 return true;
1633 }
1634
1635 /* Complain to CTXT if accessing REG leads could lead to arbitrary
1636 memory access under an attacker's control (due to taint). */
1637
1638 void
1639 region_model::check_region_for_taint (const region *reg,
1640 enum access_direction,
1641 region_model_context *ctxt) const
1642 {
1643 gcc_assert (reg);
1644 gcc_assert (ctxt);
1645
1646 LOG_SCOPE (ctxt->get_logger ());
1647
1648 sm_state_map *smap;
1649 const state_machine *sm;
1650 unsigned sm_idx;
1651 if (!ctxt->get_taint_map (&smap, &sm, &sm_idx))
1652 return;
1653
1654 gcc_assert (smap);
1655 gcc_assert (sm);
1656
1657 const taint_state_machine &taint_sm = (const taint_state_machine &)*sm;
1658
1659 const extrinsic_state *ext_state = ctxt->get_ext_state ();
1660 if (!ext_state)
1661 return;
1662
1663 const region *iter_region = reg;
1664 while (iter_region)
1665 {
1666 switch (iter_region->get_kind ())
1667 {
1668 default:
1669 break;
1670
1671 case RK_ELEMENT:
1672 {
1673 const element_region *element_reg
1674 = (const element_region *)iter_region;
1675 const svalue *index = element_reg->get_index ();
1676 const state_machine::state_t
1677 state = smap->get_state (index, *ext_state);
1678 gcc_assert (state);
1679 enum bounds b;
1680 if (taint_sm.get_taint (state, index->get_type (), &b))
1681 {
1682 if (index_can_be_out_of_bounds_p (element_reg))
1683 {
1684 tree arg = get_representative_tree (index);
1685 ctxt->warn (make_unique<tainted_array_index> (taint_sm,
1686 arg, b));
1687 }
1688 else if (ctxt->get_logger ())
1689 ctxt->get_logger ()->log ("rejecting tainted_array_index as"
1690 " out of bounds is not possible");
1691 }
1692 }
1693 break;
1694
1695 case RK_OFFSET:
1696 {
1697 const offset_region *offset_reg
1698 = (const offset_region *)iter_region;
1699 const svalue *offset = offset_reg->get_byte_offset ();
1700 const state_machine::state_t
1701 state = smap->get_state (offset, *ext_state);
1702 gcc_assert (state);
1703 /* Handle implicit cast to sizetype. */
1704 tree effective_type = offset->get_type ();
1705 if (const svalue *cast = offset->maybe_undo_cast ())
1706 if (cast->get_type ())
1707 effective_type = cast->get_type ();
1708 enum bounds b;
1709 if (taint_sm.get_taint (state, effective_type, &b))
1710 {
1711 tree arg = get_representative_tree (offset);
1712 ctxt->warn (make_unique<tainted_offset> (taint_sm, arg, b,
1713 offset));
1714 }
1715 }
1716 break;
1717
1718 case RK_SIZED:
1719 {
1720 const sized_region *sized_reg
1721 = (const sized_region *)iter_region;
1722 const svalue *size_sval = sized_reg->get_byte_size_sval (m_mgr);
1723 const state_machine::state_t
1724 state = smap->get_state (size_sval, *ext_state);
1725 gcc_assert (state);
1726 enum bounds b;
1727 if (taint_sm.get_taint (state, size_sval->get_type (), &b))
1728 {
1729 tree arg = get_representative_tree (size_sval);
1730 ctxt->warn (make_unique<tainted_size> (taint_sm, arg, b));
1731 }
1732 }
1733 break;
1734 }
1735
1736 iter_region = iter_region->get_parent_region ();
1737 }
1738 }
1739
1740 /* Complain to CTXT about a tainted allocation size if SIZE_IN_BYTES is
1741 under an attacker's control (due to taint), where the allocation
1742 is happening within MEM_SPACE. */
1743
1744 void
1745 region_model::check_dynamic_size_for_taint (enum memory_space mem_space,
1746 const svalue *size_in_bytes,
1747 region_model_context *ctxt) const
1748 {
1749 gcc_assert (size_in_bytes);
1750 gcc_assert (ctxt);
1751
1752 LOG_SCOPE (ctxt->get_logger ());
1753
1754 sm_state_map *smap;
1755 const state_machine *sm;
1756 unsigned sm_idx;
1757 if (!ctxt->get_taint_map (&smap, &sm, &sm_idx))
1758 return;
1759
1760 gcc_assert (smap);
1761 gcc_assert (sm);
1762
1763 const taint_state_machine &taint_sm = (const taint_state_machine &)*sm;
1764
1765 const extrinsic_state *ext_state = ctxt->get_ext_state ();
1766 if (!ext_state)
1767 return;
1768
1769 const state_machine::state_t
1770 state = smap->get_state (size_in_bytes, *ext_state);
1771 gcc_assert (state);
1772 enum bounds b;
1773 if (taint_sm.get_taint (state, size_in_bytes->get_type (), &b))
1774 {
1775 tree arg = get_representative_tree (size_in_bytes);
1776 ctxt->warn (make_unique<tainted_allocation_size>
1777 (taint_sm, arg, size_in_bytes, b, mem_space));
1778 }
1779 }
1780
1781 /* Mark SVAL as TAINTED. CTXT must be non-NULL. */
1782
1783 void
1784 region_model::mark_as_tainted (const svalue *sval,
1785 region_model_context *ctxt)
1786 {
1787 gcc_assert (sval);
1788 gcc_assert (ctxt);
1789
1790 sm_state_map *smap;
1791 const state_machine *sm;
1792 unsigned sm_idx;
1793 if (!ctxt->get_taint_map (&smap, &sm, &sm_idx))
1794 return;
1795
1796 gcc_assert (smap);
1797 gcc_assert (sm);
1798
1799 const taint_state_machine &taint_sm = (const taint_state_machine &)*sm;
1800
1801 const extrinsic_state *ext_state = ctxt->get_ext_state ();
1802 if (!ext_state)
1803 return;
1804
1805 smap->set_state (this, sval, taint_sm.m_tainted, NULL, *ext_state);
1806 }
1807
1808 /* Return true if SVAL could possibly be attacker-controlled. */
1809
1810 bool
1811 region_model_context::possibly_tainted_p (const svalue *sval)
1812 {
1813 sm_state_map *smap;
1814 const state_machine *sm;
1815 unsigned sm_idx;
1816 if (!get_taint_map (&smap, &sm, &sm_idx))
1817 return false;
1818
1819 const taint_state_machine &taint_sm = (const taint_state_machine &)*sm;
1820
1821 const extrinsic_state *ext_state = get_ext_state ();
1822 if (!ext_state)
1823 return false;
1824
1825 const state_machine::state_t state = smap->get_state (sval, *ext_state);
1826 gcc_assert (state);
1827
1828 return (state == taint_sm.m_tainted
1829 || state == taint_sm.m_has_lb
1830 || state == taint_sm.m_has_ub);
1831 }
1832
1833 } // namespace ana
1834
1835 #endif /* #if ENABLE_ANALYZER */