Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 1 | //== GenericTaintChecker.cpp ----------------------------------- -*- C++ -*--=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This checker defines the attack surface for generic taint propagation. |
| 11 | // |
| 12 | // The taint information produced by it might be useful to other checkers. For |
| 13 | // example, checkers should report errors which involve tainted data more |
| 14 | // aggressively, even if the involved symbols are under constrained. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/Attr.h" |
| 19 | #include "clang/Basic/Builtins.h" |
| 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 22 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 25 | #include <climits> |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | using namespace ento; |
| 29 | |
| 30 | namespace { |
Anna Zaks | 099fe3f | 2011-12-14 00:56:18 +0000 | [diff] [blame] | 31 | class GenericTaintChecker : public Checker< check::PostStmt<CallExpr>, |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 32 | check::PreStmt<CallExpr> > { |
| 33 | public: |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 34 | static void *getTag() { static int Tag; return &Tag; } |
| 35 | |
| 36 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 37 | |
| 38 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 39 | |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 40 | private: |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 41 | static const unsigned InvalidArgIndex = UINT_MAX; |
| 42 | /// Denotes the return vale. |
| 43 | static const unsigned ReturnValueIndex = UINT_MAX - 1; |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 44 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 45 | mutable std::unique_ptr<BugType> BT; |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 46 | inline void initBugType() const { |
| 47 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 48 | BT.reset(new BugType(this, "Use of Untrusted Data", "Untrusted Data")); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 49 | } |
Anna Zaks | 457c687 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 50 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 51 | /// \brief Catch taint related bugs. Check if tainted data is passed to a |
| 52 | /// system call etc. |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 53 | bool checkPre(const CallExpr *CE, CheckerContext &C) const; |
| 54 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 55 | /// \brief Add taint sources on a pre-visit. |
| 56 | void addSourcesPre(const CallExpr *CE, CheckerContext &C) const; |
| 57 | |
| 58 | /// \brief Propagate taint generated at pre-visit. |
| 59 | bool propagateFromPre(const CallExpr *CE, CheckerContext &C) const; |
| 60 | |
| 61 | /// \brief Add taint sources on a post visit. |
| 62 | void addSourcesPost(const CallExpr *CE, CheckerContext &C) const; |
| 63 | |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 64 | /// Check if the region the expression evaluates to is the standard input, |
| 65 | /// and thus, is tainted. |
| 66 | static bool isStdin(const Expr *E, CheckerContext &C); |
| 67 | |
Anna Zaks | 12d0c8d | 2017-03-09 00:01:16 +0000 | [diff] [blame] | 68 | /// This is called from getPointedToSymbol() to resolve symbol references for |
| 69 | /// the region underlying a LazyCompoundVal. This is the default binding |
| 70 | /// for the LCV, which could be a conjured symbol from a function call that |
| 71 | /// initialized the region. It only returns the conjured symbol if the LCV |
| 72 | /// covers the entire region, e.g. we avoid false positives by not returning |
| 73 | /// a default bindingc for an entire struct if the symbol for only a single |
| 74 | /// field or element within it is requested. |
| 75 | // TODO: Return an appropriate symbol for sub-fields/elements of an LCV so |
| 76 | // that they are also appropriately tainted. |
| 77 | static SymbolRef getLCVSymbol(CheckerContext &C, |
| 78 | nonloc::LazyCompoundVal &LCV); |
| 79 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 80 | /// \brief Given a pointer argument, get the symbol of the value it contains |
Anna Zaks | 457c687 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 81 | /// (points to). |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 82 | static SymbolRef getPointedToSymbol(CheckerContext &C, const Expr *Arg); |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 83 | |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 84 | /// Functions defining the attack surface. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 85 | typedef ProgramStateRef (GenericTaintChecker::*FnCheck)(const CallExpr *, |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 86 | CheckerContext &C) const; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 87 | ProgramStateRef postScanf(const CallExpr *CE, CheckerContext &C) const; |
| 88 | ProgramStateRef postSocket(const CallExpr *CE, CheckerContext &C) const; |
| 89 | ProgramStateRef postRetTaint(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 90 | |
| 91 | /// Taint the scanned input if the file is tainted. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 92 | ProgramStateRef preFscanf(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 93 | |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 94 | /// Check for CWE-134: Uncontrolled Format String. |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 95 | static const char MsgUncontrolledFormatString[]; |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 96 | bool checkUncontrolledFormatString(const CallExpr *CE, |
| 97 | CheckerContext &C) const; |
| 98 | |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 99 | /// Check for: |
| 100 | /// CERT/STR02-C. "Sanitize data passed to complex subsystems" |
| 101 | /// CWE-78, "Failure to Sanitize Data into an OS Command" |
| 102 | static const char MsgSanitizeSystemArgs[]; |
| 103 | bool checkSystemCall(const CallExpr *CE, StringRef Name, |
| 104 | CheckerContext &C) const; |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 105 | |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 106 | /// Check if tainted data is used as a buffer size ins strn.. functions, |
| 107 | /// and allocators. |
| 108 | static const char MsgTaintedBufferSize[]; |
| 109 | bool checkTaintedBufferSize(const CallExpr *CE, const FunctionDecl *FDecl, |
| 110 | CheckerContext &C) const; |
| 111 | |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 112 | /// Generate a report if the expression is tainted or points to tainted data. |
| 113 | bool generateReportIfTainted(const Expr *E, const char Msg[], |
| 114 | CheckerContext &C) const; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 115 | |
Anna Zaks | d4e43ae | 2017-03-09 00:01:07 +0000 | [diff] [blame] | 116 | /// The bug visitor prints a diagnostic message at the location where a given |
| 117 | /// variable was tainted. |
| 118 | class TaintBugVisitor |
| 119 | : public BugReporterVisitorImpl<TaintBugVisitor> { |
| 120 | private: |
| 121 | const SVal V; |
| 122 | |
| 123 | public: |
| 124 | TaintBugVisitor(const SVal V) : V(V) {} |
| 125 | void Profile(llvm::FoldingSetNodeID &ID) const override { ID.Add(V); } |
| 126 | |
| 127 | std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N, |
| 128 | const ExplodedNode *PrevN, |
| 129 | BugReporterContext &BRC, |
| 130 | BugReport &BR) override; |
| 131 | }; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 132 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 133 | typedef SmallVector<unsigned, 2> ArgVector; |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 134 | |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 135 | /// \brief A struct used to specify taint propagation rules for a function. |
| 136 | /// |
| 137 | /// If any of the possible taint source arguments is tainted, all of the |
| 138 | /// destination arguments should also be tainted. Use InvalidArgIndex in the |
| 139 | /// src list to specify that all of the arguments can introduce taint. Use |
| 140 | /// InvalidArgIndex in the dst arguments to signify that all the non-const |
| 141 | /// pointer and reference arguments might be tainted on return. If |
| 142 | /// ReturnValueIndex is added to the dst list, the return value will be |
| 143 | /// tainted. |
| 144 | struct TaintPropagationRule { |
| 145 | /// List of arguments which can be taint sources and should be checked. |
| 146 | ArgVector SrcArgs; |
| 147 | /// List of arguments which should be tainted on function return. |
| 148 | ArgVector DstArgs; |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 149 | // TODO: Check if using other data structures would be more optimal. |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 150 | |
| 151 | TaintPropagationRule() {} |
| 152 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 153 | TaintPropagationRule(unsigned SArg, |
| 154 | unsigned DArg, bool TaintRet = false) { |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 155 | SrcArgs.push_back(SArg); |
| 156 | DstArgs.push_back(DArg); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 157 | if (TaintRet) |
| 158 | DstArgs.push_back(ReturnValueIndex); |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 161 | TaintPropagationRule(unsigned SArg1, unsigned SArg2, |
| 162 | unsigned DArg, bool TaintRet = false) { |
| 163 | SrcArgs.push_back(SArg1); |
| 164 | SrcArgs.push_back(SArg2); |
| 165 | DstArgs.push_back(DArg); |
| 166 | if (TaintRet) |
| 167 | DstArgs.push_back(ReturnValueIndex); |
| 168 | } |
| 169 | |
| 170 | /// Get the propagation rule for a given function. |
| 171 | static TaintPropagationRule |
| 172 | getTaintPropagationRule(const FunctionDecl *FDecl, |
| 173 | StringRef Name, |
| 174 | CheckerContext &C); |
| 175 | |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 176 | inline void addSrcArg(unsigned A) { SrcArgs.push_back(A); } |
| 177 | inline void addDstArg(unsigned A) { DstArgs.push_back(A); } |
| 178 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 179 | inline bool isNull() const { return SrcArgs.empty(); } |
| 180 | |
| 181 | inline bool isDestinationArgument(unsigned ArgNum) const { |
| 182 | return (std::find(DstArgs.begin(), |
| 183 | DstArgs.end(), ArgNum) != DstArgs.end()); |
| 184 | } |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 185 | |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 186 | static inline bool isTaintedOrPointsToTainted(const Expr *E, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 187 | ProgramStateRef State, |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 188 | CheckerContext &C) { |
| 189 | return (State->isTainted(E, C.getLocationContext()) || isStdin(E, C) || |
| 190 | (E->getType().getTypePtr()->isPointerType() && |
| 191 | State->isTainted(getPointedToSymbol(C, E)))); |
| 192 | } |
| 193 | |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 194 | /// \brief Pre-process a function which propagates taint according to the |
| 195 | /// taint rule. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 196 | ProgramStateRef process(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 197 | |
| 198 | }; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 199 | }; |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 200 | |
| 201 | const unsigned GenericTaintChecker::ReturnValueIndex; |
| 202 | const unsigned GenericTaintChecker::InvalidArgIndex; |
| 203 | |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 204 | const char GenericTaintChecker::MsgUncontrolledFormatString[] = |
Anna Zaks | 3705a1e | 2012-02-22 02:35:58 +0000 | [diff] [blame] | 205 | "Untrusted data is used as a format string " |
| 206 | "(CWE-134: Uncontrolled Format String)"; |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 207 | |
| 208 | const char GenericTaintChecker::MsgSanitizeSystemArgs[] = |
Anna Zaks | 3705a1e | 2012-02-22 02:35:58 +0000 | [diff] [blame] | 209 | "Untrusted data is passed to a system call " |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 210 | "(CERT/STR02-C. Sanitize data passed to complex subsystems)"; |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 211 | |
| 212 | const char GenericTaintChecker::MsgTaintedBufferSize[] = |
Anna Zaks | 3705a1e | 2012-02-22 02:35:58 +0000 | [diff] [blame] | 213 | "Untrusted data is used to specify the buffer size " |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 214 | "(CERT/STR31-C. Guarantee that storage for strings has sufficient space for " |
| 215 | "character data and the null terminator)"; |
| 216 | |
| 217 | } // end of anonymous namespace |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 218 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 219 | /// A set which is used to pass information from call pre-visit instruction |
| 220 | /// to the call post-visit. The values are unsigned integers, which are either |
| 221 | /// ReturnValueIndex, or indexes of the pointer/reference argument, which |
| 222 | /// points to data, which should be tainted on return. |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 223 | REGISTER_SET_WITH_PROGRAMSTATE(TaintArgsOnPostVisit, unsigned) |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 224 | |
Anna Zaks | d4e43ae | 2017-03-09 00:01:07 +0000 | [diff] [blame] | 225 | std::shared_ptr<PathDiagnosticPiece> |
| 226 | GenericTaintChecker::TaintBugVisitor::VisitNode(const ExplodedNode *N, |
| 227 | const ExplodedNode *PrevN, BugReporterContext &BRC, BugReport &BR) { |
| 228 | |
| 229 | // Find the ExplodedNode where the taint was first introduced |
| 230 | if (!N->getState()->isTainted(V) || PrevN->getState()->isTainted(V)) |
| 231 | return nullptr; |
| 232 | |
| 233 | const Stmt *S = PathDiagnosticLocation::getStmt(N); |
| 234 | if (!S) |
| 235 | return nullptr; |
| 236 | |
| 237 | const LocationContext *NCtx = N->getLocationContext(); |
| 238 | PathDiagnosticLocation L = |
| 239 | PathDiagnosticLocation::createBegin(S, BRC.getSourceManager(), NCtx); |
| 240 | if (!L.isValid() || !L.asLocation().isValid()) |
| 241 | return nullptr; |
| 242 | |
| 243 | return std::make_shared<PathDiagnosticEventPiece>( |
| 244 | L, "Taint originated here"); |
| 245 | } |
| 246 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 247 | GenericTaintChecker::TaintPropagationRule |
| 248 | GenericTaintChecker::TaintPropagationRule::getTaintPropagationRule( |
| 249 | const FunctionDecl *FDecl, |
| 250 | StringRef Name, |
| 251 | CheckerContext &C) { |
Enrico Pertoso | 4432d87 | 2015-06-03 09:10:58 +0000 | [diff] [blame] | 252 | // TODO: Currently, we might lose precision here: we always mark a return |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 253 | // value as tainted even if it's just a pointer, pointing to tainted data. |
| 254 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 255 | // Check for exact name match for functions without builtin substitutes. |
| 256 | TaintPropagationRule Rule = llvm::StringSwitch<TaintPropagationRule>(Name) |
| 257 | .Case("atoi", TaintPropagationRule(0, ReturnValueIndex)) |
| 258 | .Case("atol", TaintPropagationRule(0, ReturnValueIndex)) |
| 259 | .Case("atoll", TaintPropagationRule(0, ReturnValueIndex)) |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 260 | .Case("getc", TaintPropagationRule(0, ReturnValueIndex)) |
| 261 | .Case("fgetc", TaintPropagationRule(0, ReturnValueIndex)) |
| 262 | .Case("getc_unlocked", TaintPropagationRule(0, ReturnValueIndex)) |
| 263 | .Case("getw", TaintPropagationRule(0, ReturnValueIndex)) |
| 264 | .Case("toupper", TaintPropagationRule(0, ReturnValueIndex)) |
| 265 | .Case("tolower", TaintPropagationRule(0, ReturnValueIndex)) |
| 266 | .Case("strchr", TaintPropagationRule(0, ReturnValueIndex)) |
| 267 | .Case("strrchr", TaintPropagationRule(0, ReturnValueIndex)) |
Anna Zaks | 3b754b2 | 2012-01-20 00:11:19 +0000 | [diff] [blame] | 268 | .Case("read", TaintPropagationRule(0, 2, 1, true)) |
| 269 | .Case("pread", TaintPropagationRule(InvalidArgIndex, 1, true)) |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 270 | .Case("gets", TaintPropagationRule(InvalidArgIndex, 0, true)) |
| 271 | .Case("fgets", TaintPropagationRule(2, 0, true)) |
| 272 | .Case("getline", TaintPropagationRule(2, 0)) |
| 273 | .Case("getdelim", TaintPropagationRule(3, 0)) |
| 274 | .Case("fgetln", TaintPropagationRule(0, ReturnValueIndex)) |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 275 | .Default(TaintPropagationRule()); |
| 276 | |
| 277 | if (!Rule.isNull()) |
| 278 | return Rule; |
| 279 | |
| 280 | // Check if it's one of the memory setting/copying functions. |
| 281 | // This check is specialized but faster then calling isCLibraryFunction. |
| 282 | unsigned BId = 0; |
| 283 | if ( (BId = FDecl->getMemoryFunctionKind()) ) |
| 284 | switch(BId) { |
| 285 | case Builtin::BImemcpy: |
| 286 | case Builtin::BImemmove: |
| 287 | case Builtin::BIstrncpy: |
| 288 | case Builtin::BIstrncat: |
| 289 | return TaintPropagationRule(1, 2, 0, true); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 290 | case Builtin::BIstrlcpy: |
| 291 | case Builtin::BIstrlcat: |
| 292 | return TaintPropagationRule(1, 2, 0, false); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 293 | case Builtin::BIstrndup: |
| 294 | return TaintPropagationRule(0, 1, ReturnValueIndex); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 295 | |
| 296 | default: |
| 297 | break; |
| 298 | }; |
| 299 | |
| 300 | // Process all other functions which could be defined as builtins. |
| 301 | if (Rule.isNull()) { |
| 302 | if (C.isCLibraryFunction(FDecl, "snprintf") || |
| 303 | C.isCLibraryFunction(FDecl, "sprintf")) |
| 304 | return TaintPropagationRule(InvalidArgIndex, 0, true); |
| 305 | else if (C.isCLibraryFunction(FDecl, "strcpy") || |
| 306 | C.isCLibraryFunction(FDecl, "stpcpy") || |
| 307 | C.isCLibraryFunction(FDecl, "strcat")) |
| 308 | return TaintPropagationRule(1, 0, true); |
| 309 | else if (C.isCLibraryFunction(FDecl, "bcopy")) |
| 310 | return TaintPropagationRule(0, 2, 1, false); |
| 311 | else if (C.isCLibraryFunction(FDecl, "strdup") || |
| 312 | C.isCLibraryFunction(FDecl, "strdupa")) |
| 313 | return TaintPropagationRule(0, ReturnValueIndex); |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 314 | else if (C.isCLibraryFunction(FDecl, "wcsdup")) |
| 315 | return TaintPropagationRule(0, ReturnValueIndex); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Skipping the following functions, since they might be used for cleansing |
| 319 | // or smart memory copy: |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 320 | // - memccpy - copying until hitting a special character. |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 321 | |
| 322 | return TaintPropagationRule(); |
Anna Zaks | 457c687 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 325 | void GenericTaintChecker::checkPreStmt(const CallExpr *CE, |
| 326 | CheckerContext &C) const { |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 327 | // Check for errors first. |
| 328 | if (checkPre(CE, C)) |
| 329 | return; |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 330 | |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 331 | // Add taint second. |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 332 | addSourcesPre(CE, C); |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void GenericTaintChecker::checkPostStmt(const CallExpr *CE, |
| 336 | CheckerContext &C) const { |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 337 | if (propagateFromPre(CE, C)) |
| 338 | return; |
| 339 | addSourcesPost(CE, C); |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 342 | void GenericTaintChecker::addSourcesPre(const CallExpr *CE, |
| 343 | CheckerContext &C) const { |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 344 | ProgramStateRef State = nullptr; |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 345 | const FunctionDecl *FDecl = C.getCalleeDecl(CE); |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 346 | if (!FDecl || FDecl->getKind() != Decl::Function) |
| 347 | return; |
| 348 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 349 | StringRef Name = C.getCalleeName(FDecl); |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 350 | if (Name.empty()) |
| 351 | return; |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 352 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 353 | // First, try generating a propagation rule for this function. |
| 354 | TaintPropagationRule Rule = |
| 355 | TaintPropagationRule::getTaintPropagationRule(FDecl, Name, C); |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 356 | if (!Rule.isNull()) { |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 357 | State = Rule.process(CE, C); |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 358 | if (!State) |
| 359 | return; |
| 360 | C.addTransition(State); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 361 | return; |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 364 | // Otherwise, check if we have custom pre-processing implemented. |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 365 | FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 366 | .Case("fscanf", &GenericTaintChecker::preFscanf) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 367 | .Default(nullptr); |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 368 | // Check and evaluate the call. |
| 369 | if (evalFunction) |
| 370 | State = (this->*evalFunction)(CE, C); |
| 371 | if (!State) |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 372 | return; |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 373 | C.addTransition(State); |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 374 | |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 377 | bool GenericTaintChecker::propagateFromPre(const CallExpr *CE, |
| 378 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 379 | ProgramStateRef State = C.getState(); |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 380 | |
| 381 | // Depending on what was tainted at pre-visit, we determined a set of |
| 382 | // arguments which should be tainted after the function returns. These are |
| 383 | // stored in the state as TaintArgsOnPostVisit set. |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 384 | TaintArgsOnPostVisitTy TaintArgs = State->get<TaintArgsOnPostVisit>(); |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 385 | if (TaintArgs.isEmpty()) |
| 386 | return false; |
| 387 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 388 | for (llvm::ImmutableSet<unsigned>::iterator |
| 389 | I = TaintArgs.begin(), E = TaintArgs.end(); I != E; ++I) { |
| 390 | unsigned ArgNum = *I; |
| 391 | |
| 392 | // Special handling for the tainted return value. |
| 393 | if (ArgNum == ReturnValueIndex) { |
| 394 | State = State->addTaint(CE, C.getLocationContext()); |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | // The arguments are pointer arguments. The data they are pointing at is |
| 399 | // tainted after the call. |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 400 | if (CE->getNumArgs() < (ArgNum + 1)) |
| 401 | return false; |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 402 | const Expr* Arg = CE->getArg(ArgNum); |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 403 | SymbolRef Sym = getPointedToSymbol(C, Arg); |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 404 | if (Sym) |
| 405 | State = State->addTaint(Sym); |
| 406 | } |
| 407 | |
| 408 | // Clear up the taint info from the state. |
| 409 | State = State->remove<TaintArgsOnPostVisit>(); |
| 410 | |
| 411 | if (State != C.getState()) { |
| 412 | C.addTransition(State); |
| 413 | return true; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | void GenericTaintChecker::addSourcesPost(const CallExpr *CE, |
| 419 | CheckerContext &C) const { |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 420 | // Define the attack surface. |
| 421 | // Set the evaluation function by switching on the callee name. |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 422 | const FunctionDecl *FDecl = C.getCalleeDecl(CE); |
| 423 | if (!FDecl || FDecl->getKind() != Decl::Function) |
| 424 | return; |
| 425 | |
| 426 | StringRef Name = C.getCalleeName(FDecl); |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 427 | if (Name.empty()) |
| 428 | return; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 429 | FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 430 | .Case("scanf", &GenericTaintChecker::postScanf) |
Anna Zaks | eefc0e9 | 2011-12-14 00:56:02 +0000 | [diff] [blame] | 431 | // TODO: Add support for vfscanf & family. |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 432 | .Case("getchar", &GenericTaintChecker::postRetTaint) |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 433 | .Case("getchar_unlocked", &GenericTaintChecker::postRetTaint) |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 434 | .Case("getenv", &GenericTaintChecker::postRetTaint) |
| 435 | .Case("fopen", &GenericTaintChecker::postRetTaint) |
| 436 | .Case("fdopen", &GenericTaintChecker::postRetTaint) |
| 437 | .Case("freopen", &GenericTaintChecker::postRetTaint) |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 438 | .Case("getch", &GenericTaintChecker::postRetTaint) |
| 439 | .Case("wgetch", &GenericTaintChecker::postRetTaint) |
Anna Zaks | 3b754b2 | 2012-01-20 00:11:19 +0000 | [diff] [blame] | 440 | .Case("socket", &GenericTaintChecker::postSocket) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 441 | .Default(nullptr); |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 442 | |
| 443 | // If the callee isn't defined, it is not of security concern. |
| 444 | // Check and evaluate the call. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 445 | ProgramStateRef State = nullptr; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 446 | if (evalFunction) |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 447 | State = (this->*evalFunction)(CE, C); |
| 448 | if (!State) |
| 449 | return; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 450 | |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 451 | C.addTransition(State); |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 452 | } |
Anna Zaks | 457c687 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 453 | |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 454 | bool GenericTaintChecker::checkPre(const CallExpr *CE, CheckerContext &C) const{ |
| 455 | |
| 456 | if (checkUncontrolledFormatString(CE, C)) |
| 457 | return true; |
| 458 | |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 459 | const FunctionDecl *FDecl = C.getCalleeDecl(CE); |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 460 | if (!FDecl || FDecl->getKind() != Decl::Function) |
| 461 | return false; |
| 462 | |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 463 | StringRef Name = C.getCalleeName(FDecl); |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 464 | if (Name.empty()) |
| 465 | return false; |
| 466 | |
| 467 | if (checkSystemCall(CE, Name, C)) |
| 468 | return true; |
| 469 | |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 470 | if (checkTaintedBufferSize(CE, FDecl, C)) |
| 471 | return true; |
| 472 | |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Anna Zaks | 12d0c8d | 2017-03-09 00:01:16 +0000 | [diff] [blame] | 476 | SymbolRef GenericTaintChecker::getLCVSymbol(CheckerContext &C, |
| 477 | nonloc::LazyCompoundVal &LCV) { |
| 478 | StoreManager &StoreMgr = C.getStoreManager(); |
| 479 | |
| 480 | // getLCVSymbol() is reached in a PostStmt so we can always expect a default |
| 481 | // binding to exist if one is present. |
| 482 | if (Optional<SVal> binding = StoreMgr.getDefaultBinding(LCV)) { |
| 483 | SymbolRef Sym = binding->getAsSymbol(); |
| 484 | if (!Sym) |
| 485 | return nullptr; |
| 486 | |
| 487 | // If the LCV covers an entire base region return the default conjured symbol. |
| 488 | if (LCV.getRegion() == LCV.getRegion()->getBaseRegion()) |
| 489 | return Sym; |
| 490 | } |
| 491 | |
| 492 | // Otherwise, return a nullptr as there's not yet a functional way to taint |
| 493 | // sub-regions of LCVs. |
| 494 | return nullptr; |
| 495 | } |
| 496 | |
Anna Zaks | 457c687 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 497 | SymbolRef GenericTaintChecker::getPointedToSymbol(CheckerContext &C, |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 498 | const Expr* Arg) { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 499 | ProgramStateRef State = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 500 | SVal AddrVal = State->getSVal(Arg->IgnoreParens(), C.getLocationContext()); |
Anna Zaks | e48ee50 | 2011-12-16 18:28:50 +0000 | [diff] [blame] | 501 | if (AddrVal.isUnknownOrUndef()) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 502 | return nullptr; |
Anna Zaks | 7c96b7d | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 503 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 504 | Optional<Loc> AddrLoc = AddrVal.getAs<Loc>(); |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 505 | if (!AddrLoc) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 506 | return nullptr; |
Anna Zaks | 457c687 | 2011-11-18 02:26:36 +0000 | [diff] [blame] | 507 | |
Anna Zaks | a31f6b9 | 2012-01-13 00:56:51 +0000 | [diff] [blame] | 508 | const PointerType *ArgTy = |
| 509 | dyn_cast<PointerType>(Arg->getType().getCanonicalType().getTypePtr()); |
Anna Zaks | 97bef56 | 2012-01-21 06:59:01 +0000 | [diff] [blame] | 510 | SVal Val = State->getSVal(*AddrLoc, |
| 511 | ArgTy ? ArgTy->getPointeeType(): QualType()); |
Anna Zaks | 12d0c8d | 2017-03-09 00:01:16 +0000 | [diff] [blame] | 512 | |
| 513 | if (auto LCV = Val.getAs<nonloc::LazyCompoundVal>()) |
| 514 | return getLCVSymbol(C, *LCV); |
| 515 | |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 516 | return Val.getAsSymbol(); |
| 517 | } |
| 518 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 519 | ProgramStateRef |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 520 | GenericTaintChecker::TaintPropagationRule::process(const CallExpr *CE, |
| 521 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 522 | ProgramStateRef State = C.getState(); |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 523 | |
| 524 | // Check for taint in arguments. |
| 525 | bool IsTainted = false; |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 526 | for (ArgVector::const_iterator I = SrcArgs.begin(), |
| 527 | E = SrcArgs.end(); I != E; ++I) { |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 528 | unsigned ArgNum = *I; |
| 529 | |
| 530 | if (ArgNum == InvalidArgIndex) { |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 531 | // Check if any of the arguments is tainted, but skip the |
| 532 | // destination arguments. |
| 533 | for (unsigned int i = 0; i < CE->getNumArgs(); ++i) { |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 534 | if (isDestinationArgument(i)) |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 535 | continue; |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 536 | if ((IsTainted = isTaintedOrPointsToTainted(CE->getArg(i), State, C))) |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 537 | break; |
Anna Zaks | 5d324e5 | 2012-01-18 02:45:07 +0000 | [diff] [blame] | 538 | } |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 542 | if (CE->getNumArgs() < (ArgNum + 1)) |
| 543 | return State; |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 544 | if ((IsTainted = isTaintedOrPointsToTainted(CE->getArg(ArgNum), State, C))) |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | if (!IsTainted) |
| 548 | return State; |
| 549 | |
| 550 | // Mark the arguments which should be tainted after the function returns. |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 551 | for (ArgVector::const_iterator I = DstArgs.begin(), |
| 552 | E = DstArgs.end(); I != E; ++I) { |
Anna Zaks | 3666d2c | 2012-01-17 00:37:02 +0000 | [diff] [blame] | 553 | unsigned ArgNum = *I; |
| 554 | |
| 555 | // Should we mark all arguments as tainted? |
| 556 | if (ArgNum == InvalidArgIndex) { |
| 557 | // For all pointer and references that were passed in: |
| 558 | // If they are not pointing to const data, mark data as tainted. |
| 559 | // TODO: So far we are just going one level down; ideally we'd need to |
| 560 | // recurse here. |
| 561 | for (unsigned int i = 0; i < CE->getNumArgs(); ++i) { |
| 562 | const Expr *Arg = CE->getArg(i); |
| 563 | // Process pointer argument. |
| 564 | const Type *ArgTy = Arg->getType().getTypePtr(); |
| 565 | QualType PType = ArgTy->getPointeeType(); |
| 566 | if ((!PType.isNull() && !PType.isConstQualified()) |
| 567 | || (ArgTy->isReferenceType() && !Arg->getType().isConstQualified())) |
| 568 | State = State->add<TaintArgsOnPostVisit>(i); |
| 569 | } |
| 570 | continue; |
| 571 | } |
| 572 | |
| 573 | // Should mark the return value? |
| 574 | if (ArgNum == ReturnValueIndex) { |
| 575 | State = State->add<TaintArgsOnPostVisit>(ReturnValueIndex); |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | // Mark the given argument. |
| 580 | assert(ArgNum < CE->getNumArgs()); |
| 581 | State = State->add<TaintArgsOnPostVisit>(ArgNum); |
| 582 | } |
| 583 | |
| 584 | return State; |
| 585 | } |
| 586 | |
| 587 | |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 588 | // If argument 0 (file descriptor) is tainted, all arguments except for arg 0 |
| 589 | // and arg 1 should get taint. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 590 | ProgramStateRef GenericTaintChecker::preFscanf(const CallExpr *CE, |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 591 | CheckerContext &C) const { |
| 592 | assert(CE->getNumArgs() >= 2); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 593 | ProgramStateRef State = C.getState(); |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 594 | |
| 595 | // Check is the file descriptor is tainted. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 596 | if (State->isTainted(CE->getArg(0), C.getLocationContext()) || |
Anna Zaks | b3fa8d7 | 2012-01-12 02:22:34 +0000 | [diff] [blame] | 597 | isStdin(CE->getArg(0), C)) { |
| 598 | // All arguments except for the first two should get taint. |
| 599 | for (unsigned int i = 2; i < CE->getNumArgs(); ++i) |
| 600 | State = State->add<TaintArgsOnPostVisit>(i); |
| 601 | return State; |
| 602 | } |
| 603 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 604 | return nullptr; |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Anna Zaks | 3b754b2 | 2012-01-20 00:11:19 +0000 | [diff] [blame] | 607 | |
| 608 | // If argument 0(protocol domain) is network, the return value should get taint. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 609 | ProgramStateRef GenericTaintChecker::postSocket(const CallExpr *CE, |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 610 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 611 | ProgramStateRef State = C.getState(); |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 612 | if (CE->getNumArgs() < 3) |
| 613 | return State; |
Anna Zaks | 3b754b2 | 2012-01-20 00:11:19 +0000 | [diff] [blame] | 614 | |
| 615 | SourceLocation DomLoc = CE->getArg(0)->getExprLoc(); |
| 616 | StringRef DomName = C.getMacroNameOrSpelling(DomLoc); |
| 617 | // White list the internal communication protocols. |
| 618 | if (DomName.equals("AF_SYSTEM") || DomName.equals("AF_LOCAL") || |
| 619 | DomName.equals("AF_UNIX") || DomName.equals("AF_RESERVED_36")) |
| 620 | return State; |
| 621 | State = State->addTaint(CE, C.getLocationContext()); |
| 622 | return State; |
| 623 | } |
| 624 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 625 | ProgramStateRef GenericTaintChecker::postScanf(const CallExpr *CE, |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 626 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 627 | ProgramStateRef State = C.getState(); |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 628 | if (CE->getNumArgs() < 2) |
| 629 | return State; |
| 630 | |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 631 | // All arguments except for the very first one should get taint. |
| 632 | for (unsigned int i = 1; i < CE->getNumArgs(); ++i) { |
| 633 | // The arguments are pointer arguments. The data they are pointing at is |
| 634 | // tainted after the call. |
| 635 | const Expr* Arg = CE->getArg(i); |
Anna Zaks | 7f6a6b7 | 2012-01-18 02:45:13 +0000 | [diff] [blame] | 636 | SymbolRef Sym = getPointedToSymbol(C, Arg); |
Anna Zaks | eefc0e9 | 2011-12-14 00:56:02 +0000 | [diff] [blame] | 637 | if (Sym) |
| 638 | State = State->addTaint(Sym); |
| 639 | } |
Anna Zaks | 3b0ab20 | 2011-12-17 00:26:34 +0000 | [diff] [blame] | 640 | return State; |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 643 | ProgramStateRef GenericTaintChecker::postRetTaint(const CallExpr *CE, |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 644 | CheckerContext &C) const { |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 645 | return C.getState()->addTaint(CE, C.getLocationContext()); |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 648 | bool GenericTaintChecker::isStdin(const Expr *E, CheckerContext &C) { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 649 | ProgramStateRef State = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 650 | SVal Val = State->getSVal(E, C.getLocationContext()); |
Anna Zaks | 099fe3f | 2011-12-14 00:56:18 +0000 | [diff] [blame] | 651 | |
Anna Zaks | e48ee50 | 2011-12-16 18:28:50 +0000 | [diff] [blame] | 652 | // stdin is a pointer, so it would be a region. |
| 653 | const MemRegion *MemReg = Val.getAsRegion(); |
| 654 | |
| 655 | // The region should be symbolic, we do not know it's value. |
| 656 | const SymbolicRegion *SymReg = dyn_cast_or_null<SymbolicRegion>(MemReg); |
| 657 | if (!SymReg) |
Anna Zaks | 099fe3f | 2011-12-14 00:56:18 +0000 | [diff] [blame] | 658 | return false; |
| 659 | |
Anna Zaks | e48ee50 | 2011-12-16 18:28:50 +0000 | [diff] [blame] | 660 | // Get it's symbol and find the declaration region it's pointing to. |
| 661 | const SymbolRegionValue *Sm =dyn_cast<SymbolRegionValue>(SymReg->getSymbol()); |
| 662 | if (!Sm) |
| 663 | return false; |
| 664 | const DeclRegion *DeclReg = dyn_cast_or_null<DeclRegion>(Sm->getRegion()); |
| 665 | if (!DeclReg) |
| 666 | return false; |
Anna Zaks | 099fe3f | 2011-12-14 00:56:18 +0000 | [diff] [blame] | 667 | |
Anna Zaks | e48ee50 | 2011-12-16 18:28:50 +0000 | [diff] [blame] | 668 | // This region corresponds to a declaration, find out if it's a global/extern |
| 669 | // variable named stdin with the proper type. |
| 670 | if (const VarDecl *D = dyn_cast_or_null<VarDecl>(DeclReg->getDecl())) { |
| 671 | D = D->getCanonicalDecl(); |
| 672 | if ((D->getName().find("stdin") != StringRef::npos) && D->isExternC()) |
| 673 | if (const PointerType * PtrTy = |
| 674 | dyn_cast<PointerType>(D->getType().getTypePtr())) |
| 675 | if (PtrTy->getPointeeType() == C.getASTContext().getFILEType()) |
| 676 | return true; |
| 677 | } |
Anna Zaks | 099fe3f | 2011-12-14 00:56:18 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
| 680 | |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 681 | static bool getPrintfFormatArgumentNum(const CallExpr *CE, |
| 682 | const CheckerContext &C, |
| 683 | unsigned int &ArgNum) { |
| 684 | // Find if the function contains a format string argument. |
| 685 | // Handles: fprintf, printf, sprintf, snprintf, vfprintf, vprintf, vsprintf, |
| 686 | // vsnprintf, syslog, custom annotated functions. |
| 687 | const FunctionDecl *FDecl = C.getCalleeDecl(CE); |
| 688 | if (!FDecl) |
| 689 | return false; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 690 | for (const auto *Format : FDecl->specific_attrs<FormatAttr>()) { |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 691 | ArgNum = Format->getFormatIdx() - 1; |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 692 | if ((Format->getType()->getName() == "printf") && |
| 693 | CE->getNumArgs() > ArgNum) |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | |
| 697 | // Or if a function is named setproctitle (this is a heuristic). |
| 698 | if (C.getCalleeName(CE).find("setproctitle") != StringRef::npos) { |
| 699 | ArgNum = 0; |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | return false; |
| 704 | } |
| 705 | |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 706 | bool GenericTaintChecker::generateReportIfTainted(const Expr *E, |
| 707 | const char Msg[], |
| 708 | CheckerContext &C) const { |
| 709 | assert(E); |
| 710 | |
| 711 | // Check for taint. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 712 | ProgramStateRef State = C.getState(); |
Anna Zaks | d4e43ae | 2017-03-09 00:01:07 +0000 | [diff] [blame] | 713 | const SymbolRef PointedToSym = getPointedToSymbol(C, E); |
| 714 | SVal TaintedSVal; |
| 715 | if (State->isTainted(PointedToSym)) |
| 716 | TaintedSVal = nonloc::SymbolVal(PointedToSym); |
| 717 | else if (State->isTainted(E, C.getLocationContext())) |
| 718 | TaintedSVal = C.getSVal(E); |
| 719 | else |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 720 | return false; |
| 721 | |
| 722 | // Generate diagnostic. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 723 | if (ExplodedNode *N = C.generateNonFatalErrorNode()) { |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 724 | initBugType(); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 725 | auto report = llvm::make_unique<BugReport>(*BT, Msg, N); |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 726 | report->addRange(E->getSourceRange()); |
Anna Zaks | d4e43ae | 2017-03-09 00:01:07 +0000 | [diff] [blame] | 727 | report->addVisitor(llvm::make_unique<TaintBugVisitor>(TaintedSVal)); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 728 | C.emitReport(std::move(report)); |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | return false; |
| 732 | } |
| 733 | |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 734 | bool GenericTaintChecker::checkUncontrolledFormatString(const CallExpr *CE, |
| 735 | CheckerContext &C) const{ |
| 736 | // Check if the function contains a format string argument. |
| 737 | unsigned int ArgNum = 0; |
| 738 | if (!getPrintfFormatArgumentNum(CE, C, ArgNum)) |
| 739 | return false; |
| 740 | |
| 741 | // If either the format string content or the pointer itself are tainted, warn. |
Alexander Kornienko | 9c10490 | 2015-12-28 13:06:58 +0000 | [diff] [blame] | 742 | return generateReportIfTainted(CE->getArg(ArgNum), |
| 743 | MsgUncontrolledFormatString, C); |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | bool GenericTaintChecker::checkSystemCall(const CallExpr *CE, |
| 747 | StringRef Name, |
| 748 | CheckerContext &C) const { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 749 | // TODO: It might make sense to run this check on demand. In some cases, |
| 750 | // we should check if the environment has been cleansed here. We also might |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 751 | // need to know if the user was reset before these calls(seteuid). |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 752 | unsigned ArgNum = llvm::StringSwitch<unsigned>(Name) |
| 753 | .Case("system", 0) |
| 754 | .Case("popen", 0) |
Anna Zaks | 3b754b2 | 2012-01-20 00:11:19 +0000 | [diff] [blame] | 755 | .Case("execl", 0) |
| 756 | .Case("execle", 0) |
| 757 | .Case("execlp", 0) |
| 758 | .Case("execv", 0) |
| 759 | .Case("execvp", 0) |
| 760 | .Case("execvP", 0) |
Anna Zaks | bf74051 | 2012-01-24 19:32:25 +0000 | [diff] [blame] | 761 | .Case("execve", 0) |
| 762 | .Case("dlopen", 0) |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 763 | .Default(UINT_MAX); |
| 764 | |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 765 | if (ArgNum == UINT_MAX || CE->getNumArgs() < (ArgNum + 1)) |
Anna Zaks | 0244cd7 | 2012-01-14 02:48:40 +0000 | [diff] [blame] | 766 | return false; |
| 767 | |
Alexander Kornienko | 9c10490 | 2015-12-28 13:06:58 +0000 | [diff] [blame] | 768 | return generateReportIfTainted(CE->getArg(ArgNum), MsgSanitizeSystemArgs, C); |
Anna Zaks | 126a2ef | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 771 | // TODO: Should this check be a part of the CString checker? |
| 772 | // If yes, should taint be a global setting? |
| 773 | bool GenericTaintChecker::checkTaintedBufferSize(const CallExpr *CE, |
| 774 | const FunctionDecl *FDecl, |
| 775 | CheckerContext &C) const { |
| 776 | // If the function has a buffer size argument, set ArgNum. |
| 777 | unsigned ArgNum = InvalidArgIndex; |
| 778 | unsigned BId = 0; |
| 779 | if ( (BId = FDecl->getMemoryFunctionKind()) ) |
| 780 | switch(BId) { |
| 781 | case Builtin::BImemcpy: |
| 782 | case Builtin::BImemmove: |
| 783 | case Builtin::BIstrncpy: |
| 784 | ArgNum = 2; |
| 785 | break; |
| 786 | case Builtin::BIstrndup: |
| 787 | ArgNum = 1; |
| 788 | break; |
| 789 | default: |
| 790 | break; |
| 791 | }; |
| 792 | |
| 793 | if (ArgNum == InvalidArgIndex) { |
| 794 | if (C.isCLibraryFunction(FDecl, "malloc") || |
| 795 | C.isCLibraryFunction(FDecl, "calloc") || |
| 796 | C.isCLibraryFunction(FDecl, "alloca")) |
| 797 | ArgNum = 0; |
| 798 | else if (C.isCLibraryFunction(FDecl, "memccpy")) |
| 799 | ArgNum = 3; |
| 800 | else if (C.isCLibraryFunction(FDecl, "realloc")) |
| 801 | ArgNum = 1; |
| 802 | else if (C.isCLibraryFunction(FDecl, "bcopy")) |
| 803 | ArgNum = 2; |
| 804 | } |
| 805 | |
Alexander Kornienko | 9c10490 | 2015-12-28 13:06:58 +0000 | [diff] [blame] | 806 | return ArgNum != InvalidArgIndex && CE->getNumArgs() > ArgNum && |
| 807 | generateReportIfTainted(CE->getArg(ArgNum), MsgTaintedBufferSize, C); |
Anna Zaks | 560dbe9 | 2012-01-18 02:45:11 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Anna Zaks | 5c5bf9b | 2011-11-16 19:58:13 +0000 | [diff] [blame] | 810 | void ento::registerGenericTaintChecker(CheckerManager &mgr) { |
| 811 | mgr.registerChecker<GenericTaintChecker>(); |
| 812 | } |