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