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