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