Taint.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //=== Taint.h - Taint tracking and basic propagation rules. --------*- C++ -*-//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Defines basic, non-domain-specific mechanisms for tracking tainted values.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_TAINT_H
  13. #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_TAINT_H
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  16. namespace clang {
  17. namespace ento {
  18. namespace taint {
  19. /// The type of taint, which helps to differentiate between different types of
  20. /// taint.
  21. using TaintTagType = unsigned;
  22. static constexpr TaintTagType TaintTagGeneric = 0;
  23. /// Create a new state in which the value of the statement is marked as tainted.
  24. LLVM_NODISCARD ProgramStateRef addTaint(ProgramStateRef State, const Stmt *S,
  25. const LocationContext *LCtx,
  26. TaintTagType Kind = TaintTagGeneric);
  27. /// Create a new state in which the value is marked as tainted.
  28. LLVM_NODISCARD ProgramStateRef addTaint(ProgramStateRef State, SVal V,
  29. TaintTagType Kind = TaintTagGeneric);
  30. /// Create a new state in which the symbol is marked as tainted.
  31. LLVM_NODISCARD ProgramStateRef addTaint(ProgramStateRef State, SymbolRef Sym,
  32. TaintTagType Kind = TaintTagGeneric);
  33. /// Create a new state in which the pointer represented by the region
  34. /// is marked as tainted.
  35. LLVM_NODISCARD ProgramStateRef addTaint(ProgramStateRef State,
  36. const MemRegion *R,
  37. TaintTagType Kind = TaintTagGeneric);
  38. LLVM_NODISCARD ProgramStateRef removeTaint(ProgramStateRef State, SVal V);
  39. LLVM_NODISCARD ProgramStateRef removeTaint(ProgramStateRef State,
  40. const MemRegion *R);
  41. LLVM_NODISCARD ProgramStateRef removeTaint(ProgramStateRef State,
  42. SymbolRef Sym);
  43. /// Create a new state in a which a sub-region of a given symbol is tainted.
  44. /// This might be necessary when referring to regions that can not have an
  45. /// individual symbol, e.g. if they are represented by the default binding of
  46. /// a LazyCompoundVal.
  47. LLVM_NODISCARD ProgramStateRef addPartialTaint(
  48. ProgramStateRef State, SymbolRef ParentSym, const SubRegion *SubRegion,
  49. TaintTagType Kind = TaintTagGeneric);
  50. /// Check if the statement has a tainted value in the given state.
  51. bool isTainted(ProgramStateRef State, const Stmt *S,
  52. const LocationContext *LCtx,
  53. TaintTagType Kind = TaintTagGeneric);
  54. /// Check if the value is tainted in the given state.
  55. bool isTainted(ProgramStateRef State, SVal V,
  56. TaintTagType Kind = TaintTagGeneric);
  57. /// Check if the symbol is tainted in the given state.
  58. bool isTainted(ProgramStateRef State, SymbolRef Sym,
  59. TaintTagType Kind = TaintTagGeneric);
  60. /// Check if the pointer represented by the region is tainted in the given
  61. /// state.
  62. bool isTainted(ProgramStateRef State, const MemRegion *Reg,
  63. TaintTagType Kind = TaintTagGeneric);
  64. void printTaint(ProgramStateRef State, raw_ostream &Out, const char *nl = "\n",
  65. const char *sep = "");
  66. LLVM_DUMP_METHOD void dumpTaint(ProgramStateRef State);
  67. /// The bug visitor prints a diagnostic message at the location where a given
  68. /// variable was tainted.
  69. class TaintBugVisitor final : public BugReporterVisitor {
  70. private:
  71. const SVal V;
  72. public:
  73. TaintBugVisitor(const SVal V) : V(V) {}
  74. void Profile(llvm::FoldingSetNodeID &ID) const override { ID.Add(V); }
  75. PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
  76. BugReporterContext &BRC,
  77. PathSensitiveBugReport &BR) override;
  78. };
  79. } // namespace taint
  80. } // namespace ento
  81. } // namespace clang
  82. #endif