const supernode *node,
const gcall *call,
tree callee_fndecl) const;
+ void check_for_tainted_divisor (sm_context *sm_ctxt,
+ const supernode *node,
+ const gassign *assign) const;
public:
/* State for a "tainted" value: unsanitized data potentially under an
case ROUND_MOD_EXPR:
case RDIV_EXPR:
case EXACT_DIV_EXPR:
- {
- tree divisor = gimple_assign_rhs2 (assign);;
- state_t state = sm_ctxt->get_state (stmt, divisor);
- enum bounds b;
- if (get_taint (state, TREE_TYPE (divisor), &b))
- {
- tree diag_divisor = sm_ctxt->get_diagnostic_tree (divisor);
- sm_ctxt->warn (node, stmt, divisor,
- new tainted_divisor (*this, diag_divisor, b));
- sm_ctxt->set_next_state (stmt, divisor, m_stop);
- }
- }
+ check_for_tainted_divisor (sm_ctxt, node, assign);
break;
}
}
}
}
+/* Complain if ASSIGN (a division operation) has a tainted divisor
+ that could be zero. */
+
+void
+taint_state_machine::check_for_tainted_divisor (sm_context *sm_ctxt,
+ const supernode *node,
+ const gassign *assign) const
+{
+ const region_model *old_model = sm_ctxt->get_old_region_model ();
+ if (!old_model)
+ return;
+
+ tree divisor_expr = gimple_assign_rhs2 (assign);;
+ const svalue *divisor_sval = old_model->get_rvalue (divisor_expr, NULL);
+
+ state_t state = sm_ctxt->get_state (assign, divisor_sval);
+ enum bounds b;
+ if (get_taint (state, TREE_TYPE (divisor_expr), &b))
+ {
+ const svalue *zero_sval
+ = old_model->get_manager ()->get_or_create_int_cst
+ (TREE_TYPE (divisor_expr), 0);
+ tristate ts
+ = old_model->eval_condition (divisor_sval, NE_EXPR, zero_sval);
+ if (ts.is_true ())
+ /* The divisor is known to not equal 0: don't warn. */
+ return;
+
+ tree diag_divisor = sm_ctxt->get_diagnostic_tree (divisor_expr);
+ sm_ctxt->warn (node, assign, divisor_expr,
+ new tainted_divisor (*this, diag_divisor, b));
+ sm_ctxt->set_next_state (assign, divisor_sval, m_stop);
+ }
+}
+
} // anonymous namespace
/* Internal interface to this file. */
#include "analyzer/program-point.h"
#include "analyzer/store.h"
#include "analyzer/svalue.h"
+#include "analyzer/program-state.h"
#if ENABLE_ANALYZER
return sm_obj;
}
+/* class sm_context. */
+
+const region_model *
+sm_context::get_old_region_model () const
+{
+ if (const program_state *old_state = get_old_program_state ())
+ return old_state->m_region_model;
+ else
+ return NULL;
+}
+
/* Create instances of the various state machines, each using LOGGER,
and populate OUT with them. */
virtual const program_state *get_old_program_state () const = 0;
virtual const program_state *get_new_program_state () const = 0;
+ const region_model *get_old_region_model () const;
+
protected:
sm_context (int sm_idx, const state_machine &sm)
: m_sm_idx (sm_idx), m_sm (sm) {}
fread (&s, sizeof (s), 1, f);
return s.a % s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
}
+
+/* We shouldn't complain if the divisor has been checked for zero. */
+
+int test_checked_ne_zero (FILE *f)
+{
+ struct st1 s;
+ fread (&s, sizeof (s), 1, f);
+ if (s.b)
+ return s.a / s.b; /* { dg-bogus "divisor" } */
+ else
+ return 0;
+}
+
+int test_checked_gt_zero (FILE *f)
+{
+ struct st1 s;
+ fread (&s, sizeof (s), 1, f);
+ if (s.b > 0)
+ return s.a / s.b; /* { dg-bogus "divisor" } */
+ else
+ return 0;
+}
+
+int test_checked_lt_zero (FILE *f)
+{
+ struct st1 s;
+ fread (&s, sizeof (s), 1, f);
+ if (s.b < 0)
+ return s.a / s.b; /* { dg-bogus "divisor" } */
+ else
+ return 0;
+}
+
+/* We should complain if the check on the divisor still allows it to be
+ zero. */
+
+int test_checked_ge_zero (FILE *f)
+{
+ struct st1 s;
+ fread (&s, sizeof (s), 1, f);
+ if (s.b >= 0)
+ return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
+ else
+ return 0;
+}
+
+int test_checked_le_zero (FILE *f)
+{
+ struct st1 s;
+ fread (&s, sizeof (s), 1, f);
+ if (s.b <= 0)
+ return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
+ else
+ return 0;
+}
+
+int test_checked_eq_zero (FILE *f)
+{
+ struct st1 s;
+ fread (&s, sizeof (s), 1, f);
+ /* Wrong sense of test. */
+ if (s.b != 0)
+ return 0;
+ else
+ return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
+}