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