CBMC
taint_analysis.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Taint Analysis
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "taint_analysis.h"
13 
14 #include <iostream>
15 #include <fstream>
16 
17 #include <util/invariant.h>
18 #include <util/json.h>
19 #include <util/message.h>
20 #include <util/pointer_expr.h>
21 #include <util/prefix.h>
22 #include <util/simplify_expr.h>
23 #include <util/std_code.h>
24 #include <util/string_constant.h>
25 
27 
29 
30 #include "taint_parser.h"
31 
33 {
34 public:
35  explicit taint_analysist(message_handlert &message_handler)
36  : log(message_handler)
37  {
38  }
39 
40  bool operator()(
41  const std::string &taint_file_name,
42  const symbol_tablet &,
44  bool show_full,
45  const optionalt<std::string> &json_file_name);
46 
47 protected:
51 
52  void instrument(const namespacet &, goto_functionst &);
54 };
55 
57  const namespacet &ns,
58  goto_functionst &goto_functions)
59 {
60  for(auto &function : goto_functions.function_map)
61  instrument(ns, function.second);
62 }
63 
65  const namespacet &ns,
66  goto_functionst::goto_functiont &goto_function)
67 {
68  for(goto_programt::instructionst::iterator
69  it=goto_function.body.instructions.begin();
70  it!=goto_function.body.instructions.end();
71  it++)
72  {
73  const goto_programt::instructiont &instruction=*it;
74 
75  goto_programt insert_before, insert_after;
76 
77  if(instruction.is_function_call())
78  {
79  const exprt &function = instruction.call_function();
80 
81  if(function.id() == ID_symbol)
82  {
83  const irep_idt &identifier = to_symbol_expr(function).get_identifier();
84 
85  std::set<irep_idt> identifiers;
86 
87  identifiers.insert(identifier);
88 
89  irep_idt class_id = function.get(ID_C_class);
90  if(class_id.empty())
91  {
92  }
93  else
94  {
95  std::string suffix = std::string(
96  id2string(identifier), class_id.size(), std::string::npos);
97 
98  class_hierarchyt::idst parents =
100  for(const auto &p : parents)
101  identifiers.insert(id2string(p) + suffix);
102  }
103 
104  for(const auto &rule : taint.rules)
105  {
106  bool match = false;
107  for(const auto &i : identifiers)
108  {
109  if(
110  i == rule.function_identifier ||
111  has_prefix(
112  id2string(i),
113  "java::" + id2string(rule.function_identifier) + ":"))
114  {
115  match = true;
116  break;
117  }
118  }
119 
120  if(match)
121  {
122  log.debug() << "MATCH " << rule.id << " on " << identifier
123  << messaget::eom;
124 
125  exprt where = nil_exprt();
126 
127  const code_typet &code_type = to_code_type(function.type());
128 
129  bool have_this = !code_type.parameters().empty() &&
130  code_type.parameters().front().get_bool(ID_C_this);
131 
132  switch(rule.where)
133  {
135  {
137  ns.lookup(id2string(identifier) + "#return_value");
138  where = return_value_symbol.symbol_expr();
139  break;
140  }
141 
143  {
144  unsigned nr =
145  have_this ? rule.parameter_number : rule.parameter_number - 1;
146  if(instruction.call_arguments().size() > nr)
147  where = instruction.call_arguments()[nr];
148  break;
149  }
150 
152  if(have_this)
153  {
155  !instruction.call_arguments().empty(),
156  "`this` implies at least one argument in function call");
157  where = instruction.call_arguments()[0];
158  }
159  break;
160  }
161 
162  switch(rule.kind)
163  {
165  {
166  codet code_set_may{ID_set_may};
167  code_set_may.operands().resize(2);
168  code_set_may.op0() = where;
169  code_set_may.op1() =
170  address_of_exprt(string_constantt(rule.taint));
171  insert_after.add(goto_programt::make_other(
172  code_set_may, instruction.source_location()));
173  break;
174  }
175 
177  {
178  binary_predicate_exprt get_may{
179  where,
180  ID_get_may,
181  address_of_exprt(string_constantt(rule.taint))};
183  insert_before.add(goto_programt::make_assertion(
184  not_exprt(get_may), instruction.source_location()));
185  t->source_location_nonconst().set_property_class(
186  "taint rule " + id2string(rule.id));
187  t->source_location_nonconst().set_comment(rule.message);
188  break;
189  }
190 
192  {
193  codet code_clear_may{ID_clear_may};
194  code_clear_may.operands().resize(2);
195  code_clear_may.op0() = where;
196  code_clear_may.op1() =
197  address_of_exprt(string_constantt(rule.taint));
198  insert_after.add(goto_programt::make_other(
199  code_clear_may, instruction.source_location()));
200  break;
201  }
202  }
203  }
204  }
205  }
206  }
207 
208  if(!insert_before.empty())
209  {
210  goto_function.body.insert_before_swap(it, insert_before);
211  // advance until we get back to the call
212  while(!it->is_function_call()) ++it;
213  }
214 
215  if(!insert_after.empty())
216  {
217  goto_function.body.destructive_insert(
218  std::next(it), insert_after);
219  }
220  }
221 }
222 
224  const std::string &taint_file_name,
225  const symbol_tablet &symbol_table,
226  goto_functionst &goto_functions,
227  bool show_full,
228  const optionalt<std::string> &json_file_name)
229 {
230  try
231  {
232  json_arrayt json_result;
233  bool use_json = json_file_name.has_value();
234 
235  log.status() << "Reading taint file '" << taint_file_name << "'"
236  << messaget::eom;
237 
238  if(taint_parser(taint_file_name, taint, log.get_message_handler()))
239  {
240  log.error() << "Failed to read taint definition file" << messaget::eom;
241  return true;
242  }
243 
244  log.status() << "Got " << taint.rules.size() << " taint definitions"
245  << messaget::eom;
246 
247  log.conditional_output(log.debug(), [this](messaget::mstreamt &mstream) {
248  taint.output(mstream);
249  mstream << messaget::eom;
250  });
251 
252  log.status() << "Instrumenting taint" << messaget::eom;
253 
254  class_hierarchy(symbol_table);
255 
256  const namespacet ns(symbol_table);
257  instrument(ns, goto_functions);
258  goto_functions.update();
259 
260  bool have_entry_point=
261  goto_functions.function_map.find(goto_functionst::entry_point())!=
262  goto_functions.function_map.end();
263 
264  // do we have an entry point?
265  if(have_entry_point)
266  {
267  log.status() << "Working from entry point" << messaget::eom;
268  }
269  else
270  {
271  log.status() << "No entry point found; "
272  << "we will consider the heads of all functions as reachable"
273  << messaget::eom;
274 
275  goto_programt end, gotos, calls;
276 
278 
279  for(const auto &gf_entry : goto_functions.function_map)
280  {
281  if(
282  gf_entry.second.body_available() &&
283  gf_entry.first != goto_functionst::entry_point())
284  {
285  const symbolt &symbol = ns.lookup(gf_entry.first);
286  const code_function_callt call(symbol.symbol_expr());
289  calls.add(
293  }
294  }
295 
297  goto_functions.function_map[goto_functionst::entry_point()];
298 
299  goto_programt &body=entry.body;
300 
301  body.destructive_append(gotos);
302  body.destructive_append(calls);
303  body.destructive_append(end);
304 
305  goto_functions.update();
306  }
307 
308  log.status() << "Data-flow analysis" << messaget::eom;
309 
310  custom_bitvector_analysist custom_bitvector_analysis;
311  custom_bitvector_analysis(goto_functions, ns);
312 
313  if(show_full)
314  {
315  custom_bitvector_analysis.output(ns, goto_functions, std::cout);
316  return false;
317  }
318 
319  for(const auto &gf_entry : goto_functions.function_map)
320  {
321  if(!gf_entry.second.body.has_assertion())
322  continue;
323 
324  const symbolt &symbol = ns.lookup(gf_entry.first);
325 
326  if(gf_entry.first == "__actual_thread_spawn")
327  continue;
328 
329  bool first=true;
330 
331  forall_goto_program_instructions(i_it, gf_entry.second.body)
332  {
333  if(!i_it->is_assert())
334  continue;
335 
336  if(!custom_bitvector_domaint::has_get_must_or_may(i_it->condition()))
337  {
338  continue;
339  }
340 
341  if(custom_bitvector_analysis[i_it].has_values.is_false())
342  continue;
343 
344  exprt result = custom_bitvector_analysis.eval(i_it->condition(), i_it);
345  if(simplify_expr(std::move(result), ns).is_true())
346  continue;
347 
348  if(first)
349  {
350  first=false;
351  if(!use_json)
352  std::cout << "\n"
353  << "******** Function " << symbol.display_name() << '\n';
354  }
355 
356  if(use_json)
357  {
359  {"bugClass",
360  json_stringt(i_it->source_location().get_property_class())},
361  {"file", json_stringt(i_it->source_location().get_file())},
362  {"line",
363  json_numbert(id2string(i_it->source_location().get_line()))}};
364  json_result.push_back(std::move(json));
365  }
366  else
367  {
368  std::cout << i_it->source_location();
369  if(!i_it->source_location().get_comment().empty())
370  std::cout << ": " << i_it->source_location().get_comment();
371 
372  if(!i_it->source_location().get_property_class().empty())
373  std::cout << " (" << i_it->source_location().get_property_class()
374  << ")";
375 
376  std::cout << '\n';
377  }
378  }
379  }
380 
381  if(use_json)
382  {
383  std::ofstream json_out(json_file_name.value());
384 
385  if(!json_out)
386  {
387  log.error() << "Failed to open json output '" << json_file_name.value()
388  << "'" << messaget::eom;
389  return true;
390  }
391 
392  log.status() << "Analysis result is written to '"
393  << json_file_name.value() << "'" << messaget::eom;
394 
395  json_out << json_result << '\n';
396  }
397 
398  return false;
399  }
400  catch(const char *error_msg)
401  {
402  log.error() << error_msg << messaget::eom;
403  return true;
404  }
405  catch(const std::string &error_msg)
406  {
407  log.error() << error_msg << messaget::eom;
408  return true;
409  }
410  catch(...)
411  {
412  log.error() << "Caught unexpected error in taint_analysist::operator()"
413  << messaget::eom;
414  return true;
415  }
416 }
417 
419  goto_modelt &goto_model,
420  const std::string &taint_file_name,
421  message_handlert &message_handler,
422  bool show_full,
423  const optionalt<std::string> &json_file_name)
424 {
425  taint_analysist taint_analysis(message_handler);
426  return taint_analysis(
427  taint_file_name,
428  goto_model.symbol_table,
429  goto_model.goto_functions,
430  show_full,
431  json_file_name);
432 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
goto_programt::make_other
static instructiont make_other(const goto_instruction_codet &_code, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:948
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
json_numbert
Definition: json.h:290
taint_parse_treet::rules
rulest rules
Definition: taint_parser.h:63
taint_analysist::log
messaget log
Definition: taint_analysis.cpp:48
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
class_hierarchyt
Non-graph-based representation of the class hierarchy.
Definition: class_hierarchy.h:42
taint_analysist::class_hierarchy
class_hierarchyt class_hierarchy
Definition: taint_analysis.cpp:50
symbolt::display_name
const irep_idt & display_name() const
Return language specific display name if present.
Definition: symbol.h:55
messaget::status
mstreamt & status() const
Definition: message.h:414
taint_analysist::instrument
void instrument(const namespacet &, goto_functionst &)
Definition: taint_analysis.cpp:56
custom_bitvector_analysis.h
taint_parser.h
taint_parse_treet
Definition: taint_parser.h:23
taint_analysist
Definition: taint_analysis.cpp:32
prefix.h
taint_analysis.h
invariant.h
goto_programt::make_end_function
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:992
goto_programt::add
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:709
string_constant.h
goto_programt::empty
bool empty() const
Is the program empty?
Definition: goto_program.h:790
taint_parse_treet::rulet::RETURN_VALUE
@ RETURN_VALUE
Definition: taint_parser.h:30
exprt
Base class for all expressions.
Definition: expr.h:55
goto_modelt
Definition: goto_model.h:25
custom_bitvector_analysist::eval
exprt eval(const exprt &src, locationt loc)
Definition: custom_bitvector_analysis.h:156
bool_typet
The Boolean type.
Definition: std_types.h:35
messaget::eom
static eomt eom
Definition: message.h:297
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
string_constantt
Definition: string_constant.h:14
goto_programt::instructiont::call_arguments
const exprt::operandst & call_arguments() const
Get the arguments of a FUNCTION_CALL.
Definition: goto_program.h:307
goto_programt::make_goto
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:1030
taint_analysist::taint
taint_parse_treet taint
Definition: taint_analysis.cpp:49
json_arrayt
Definition: json.h:164
json_objectt
Definition: json.h:299
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
goto_programt::make_function_call
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
Definition: goto_program.h:1081
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
code_function_callt
goto_instruction_codet representation of a function call statement.
Definition: goto_instruction_code.h:271
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
goto_programt::make_assertion
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:924
messaget::error
mstreamt & error() const
Definition: message.h:399
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
taint_parse_treet::rulet::THIS
@ THIS
Definition: taint_parser.h:30
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:510
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
goto_programt::instructiont::source_location
const source_locationt & source_location() const
Definition: goto_program.h:340
binary_predicate_exprt
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition: std_expr.h:675
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:142
nil_exprt
The NIL expression.
Definition: std_expr.h:3025
json
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:120
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
pointer_expr.h
exprt::op1
exprt & op1()
Definition: expr.h:128
taint_parse_treet::rulet::SANITIZER
@ SANITIZER
Definition: taint_parser.h:29
simplify_expr
exprt simplify_expr(exprt src, const namespacet &ns)
Definition: simplify_expr.cpp:2931
custom_bitvector_analysist
Definition: custom_bitvector_analysis.h:148
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:222
code_typet
Base type of functions.
Definition: std_types.h:538
message_handlert
Definition: message.h:27
class_hierarchyt::idst
std::vector< irep_idt > idst
Definition: class_hierarchy.h:45
taint_analysist::operator()
bool operator()(const std::string &taint_file_name, const symbol_tablet &, goto_functionst &, bool show_full, const optionalt< std::string > &json_file_name)
Definition: taint_analysis.cpp:223
taint_parser
bool taint_parser(const std::string &file_name, taint_parse_treet &dest, message_handlert &message_handler)
Definition: taint_parser.cpp:20
dstringt::empty
bool empty() const
Definition: dstring.h:88
taint_parse_treet::rulet::PARAMETER
@ PARAMETER
Definition: taint_parser.h:30
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:655
std_code.h
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
goto_programt::destructive_append
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
Definition: goto_program.h:692
simplify_expr.h
side_effect_expr_nondett
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1519
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:592
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
custom_bitvector_domaint::has_get_must_or_may
static bool has_get_must_or_may(const exprt &)
Definition: custom_bitvector_analysis.cpp:685
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:184
class_hierarchy.h
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
ai_baset::output
virtual void output(const namespacet &ns, const irep_idt &function_id, const goto_programt &goto_program, std::ostream &out) const
Output the abstract states for a single function.
Definition: ai.cpp:40
symbolt
Symbol table entry.
Definition: symbol.h:27
taint_analysis
bool taint_analysis(goto_modelt &goto_model, const std::string &taint_file_name, message_handlert &message_handler, bool show_full, const optionalt< std::string > &json_file_name)
Definition: taint_analysis.cpp:418
messaget::conditional_output
void conditional_output(mstreamt &mstream, const std::function< void(mstreamt &)> &output_generator) const
Generate output to message_stream using output_generator if the configured verbosity is at least as h...
Definition: message.cpp:139
goto_functionst::update
void update()
Definition: goto_functions.h:83
json.h
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
taint_parse_treet::rulet::SOURCE
@ SOURCE
Definition: taint_parser.h:29
return_value_symbol
symbol_exprt return_value_symbol(const irep_idt &identifier, const namespacet &ns)
produces the symbol that is used to store the return value of the function with the given identifier
Definition: remove_returns.cpp:409
messaget::mstreamt
Definition: message.h:223
exprt::operands
operandst & operands()
Definition: expr.h:94
messaget::debug
mstreamt & debug() const
Definition: message.h:429
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:92
class_hierarchyt::get_parents_trans
idst get_parents_trans(const irep_idt &id) const
Definition: class_hierarchy.h:76
address_of_exprt
Operator to return the address of an object.
Definition: pointer_expr.h:370
message.h
true_exprt
The Boolean constant true.
Definition: std_expr.h:3007
taint_parse_treet::rulet::SINK
@ SINK
Definition: taint_parser.h:29
goto_programt::instructiont::is_function_call
bool is_function_call() const
Definition: goto_program.h:466
is_true
bool is_true(const literalt &l)
Definition: literal.h:198
taint_analysist::taint_analysist
taint_analysist(message_handlert &message_handler)
Definition: taint_analysis.cpp:35
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_programt::instructiont::call_function
const exprt & call_function() const
Get the function that is called for FUNCTION_CALL.
Definition: goto_program.h:279
json_arrayt::push_back
jsont & push_back(const jsont &json)
Definition: json.h:212
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586
forall_goto_program_instructions
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:1229
dstringt::size
size_t size() const
Definition: dstring.h:120
json_stringt
Definition: json.h:269
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:28
not_exprt
Boolean negation.
Definition: std_expr.h:2277