Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 1 | //==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- 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 file defines a set of flow-insensitive security checks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/AnalysisContext.h" |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Lenny Maiorani | fca2e96 | 2011-04-03 05:07:11 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 26 | using namespace ento; |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | abf6ba1 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 28 | static bool isArc4RandomAvailable(const ASTContext &Ctx) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 29 | const llvm::Triple &T = Ctx.getTargetInfo().getTriple(); |
Ted Kremenek | abf6ba1 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 30 | return T.getVendor() == llvm::Triple::Apple || |
Ed Schouten | e5bdc85 | 2015-03-11 08:48:55 +0000 | [diff] [blame] | 31 | T.getOS() == llvm::Triple::CloudABI || |
Douglas Gregor | 45e84b0 | 2011-01-17 19:16:24 +0000 | [diff] [blame] | 32 | T.getOS() == llvm::Triple::FreeBSD || |
| 33 | T.getOS() == llvm::Triple::NetBSD || |
| 34 | T.getOS() == llvm::Triple::OpenBSD || |
Eli Friedman | 9fa2885 | 2012-08-08 23:57:20 +0000 | [diff] [blame] | 35 | T.getOS() == llvm::Triple::Bitrig || |
Douglas Gregor | 45e84b0 | 2011-01-17 19:16:24 +0000 | [diff] [blame] | 36 | T.getOS() == llvm::Triple::DragonFly; |
Ted Kremenek | abf6ba1 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 39 | namespace { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 40 | struct ChecksFilter { |
| 41 | DefaultBool check_gets; |
| 42 | DefaultBool check_getpw; |
| 43 | DefaultBool check_mktemp; |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 44 | DefaultBool check_mkstemp; |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 45 | DefaultBool check_strcpy; |
| 46 | DefaultBool check_rand; |
| 47 | DefaultBool check_vfork; |
| 48 | DefaultBool check_FloatLoopCounter; |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 49 | DefaultBool check_UncheckedReturn; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 50 | |
| 51 | CheckName checkName_gets; |
| 52 | CheckName checkName_getpw; |
| 53 | CheckName checkName_mktemp; |
| 54 | CheckName checkName_mkstemp; |
| 55 | CheckName checkName_strcpy; |
| 56 | CheckName checkName_rand; |
| 57 | CheckName checkName_vfork; |
| 58 | CheckName checkName_FloatLoopCounter; |
| 59 | CheckName checkName_UncheckedReturn; |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 61 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 62 | class WalkAST : public StmtVisitor<WalkAST> { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | BugReporter &BR; |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 64 | AnalysisDeclContext* AC; |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 65 | enum { num_setids = 6 }; |
| 66 | IdentifierInfo *II_setid[num_setids]; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | abf6ba1 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 68 | const bool CheckRand; |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 69 | const ChecksFilter &filter; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 71 | public: |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 72 | WalkAST(BugReporter &br, AnalysisDeclContext* ac, |
| 73 | const ChecksFilter &f) |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 74 | : BR(br), AC(ac), II_setid(), |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 75 | CheckRand(isArc4RandomAvailable(BR.getContext())), |
| 76 | filter(f) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 78 | // Statement visitor methods. |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 79 | void VisitCallExpr(CallExpr *CE); |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 80 | void VisitForStmt(ForStmt *S); |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 81 | void VisitCompoundStmt (CompoundStmt *S); |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 82 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 83 | |
| 84 | void VisitChildren(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 86 | // Helpers. |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 87 | bool checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Lenny Maiorani | fca2e96 | 2011-04-03 05:07:11 +0000 | [diff] [blame] | 89 | typedef void (WalkAST::*FnCheck)(const CallExpr *, |
| 90 | const FunctionDecl *); |
| 91 | |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 92 | // Checker-specific methods. |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 93 | void checkLoopConditionForFloat(const ForStmt *FS); |
| 94 | void checkCall_gets(const CallExpr *CE, const FunctionDecl *FD); |
| 95 | void checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD); |
| 96 | void checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD); |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 97 | void checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD); |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 98 | void checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD); |
| 99 | void checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD); |
| 100 | void checkCall_rand(const CallExpr *CE, const FunctionDecl *FD); |
| 101 | void checkCall_random(const CallExpr *CE, const FunctionDecl *FD); |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 102 | void checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD); |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 103 | void checkUncheckedReturnValue(CallExpr *CE); |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 104 | }; |
| 105 | } // end anonymous namespace |
| 106 | |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | // AST walking. |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | |
| 111 | void WalkAST::VisitChildren(Stmt *S) { |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame^] | 112 | for (Stmt *Child : S->children()) |
| 113 | if (Child) |
| 114 | Visit(Child); |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 117 | void WalkAST::VisitCallExpr(CallExpr *CE) { |
Lenny Maiorani | fca2e96 | 2011-04-03 05:07:11 +0000 | [diff] [blame] | 118 | // Get the callee. |
| 119 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 120 | |
| 121 | if (!FD) |
| 122 | return; |
| 123 | |
| 124 | // Get the name of the callee. If it's a builtin, strip off the prefix. |
| 125 | IdentifierInfo *II = FD->getIdentifier(); |
| 126 | if (!II) // if no identifier, not a simple C function |
| 127 | return; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | StringRef Name = II->getName(); |
Lenny Maiorani | fca2e96 | 2011-04-03 05:07:11 +0000 | [diff] [blame] | 129 | if (Name.startswith("__builtin_")) |
| 130 | Name = Name.substr(10); |
| 131 | |
| 132 | // Set the evaluation function by switching on the callee name. |
| 133 | FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 134 | .Case("gets", &WalkAST::checkCall_gets) |
| 135 | .Case("getpw", &WalkAST::checkCall_getpw) |
| 136 | .Case("mktemp", &WalkAST::checkCall_mktemp) |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 137 | .Case("mkstemp", &WalkAST::checkCall_mkstemp) |
| 138 | .Case("mkdtemp", &WalkAST::checkCall_mkstemp) |
| 139 | .Case("mkstemps", &WalkAST::checkCall_mkstemp) |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 140 | .Cases("strcpy", "__strcpy_chk", &WalkAST::checkCall_strcpy) |
| 141 | .Cases("strcat", "__strcat_chk", &WalkAST::checkCall_strcat) |
| 142 | .Case("drand48", &WalkAST::checkCall_rand) |
| 143 | .Case("erand48", &WalkAST::checkCall_rand) |
| 144 | .Case("jrand48", &WalkAST::checkCall_rand) |
| 145 | .Case("lrand48", &WalkAST::checkCall_rand) |
| 146 | .Case("mrand48", &WalkAST::checkCall_rand) |
| 147 | .Case("nrand48", &WalkAST::checkCall_rand) |
| 148 | .Case("lcong48", &WalkAST::checkCall_rand) |
| 149 | .Case("rand", &WalkAST::checkCall_rand) |
| 150 | .Case("rand_r", &WalkAST::checkCall_rand) |
| 151 | .Case("random", &WalkAST::checkCall_random) |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 152 | .Case("vfork", &WalkAST::checkCall_vfork) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 153 | .Default(nullptr); |
Lenny Maiorani | fca2e96 | 2011-04-03 05:07:11 +0000 | [diff] [blame] | 154 | |
| 155 | // If the callee isn't defined, it is not of security concern. |
| 156 | // Check and evaluate the call. |
| 157 | if (evalFunction) |
| 158 | (this->*evalFunction)(CE, FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 160 | // Recurse and check children. |
| 161 | VisitChildren(CE); |
| 162 | } |
| 163 | |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 164 | void WalkAST::VisitCompoundStmt(CompoundStmt *S) { |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame^] | 165 | for (Stmt *Child : S->children()) |
| 166 | if (Child) { |
| 167 | if (CallExpr *CE = dyn_cast<CallExpr>(Child)) |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 168 | checkUncheckedReturnValue(CE); |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame^] | 169 | Visit(Child); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 173 | void WalkAST::VisitForStmt(ForStmt *FS) { |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 174 | checkLoopConditionForFloat(FS); |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 176 | // Recurse and check children. |
| 177 | VisitChildren(FS); |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 181 | // Check: floating poing variable used as loop counter. |
Ted Kremenek | 70e5526 | 2009-07-23 21:44:18 +0000 | [diff] [blame] | 182 | // Originally: <rdar://problem/6336718> |
| 183 | // Implements: CERT security coding advisory FLP-30. |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
| 185 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 186 | static const DeclRefExpr* |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 187 | getIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 188 | expr = expr->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
| 190 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 191 | if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 192 | B->getOpcode() == BO_Comma)) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 193 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 195 | if (const DeclRefExpr *lhs = getIncrementedVar(B->getLHS(), x, y)) |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 196 | return lhs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 198 | if (const DeclRefExpr *rhs = getIncrementedVar(B->getRHS(), x, y)) |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 199 | return rhs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 201 | return nullptr; |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 202 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 204 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { |
| 205 | const NamedDecl *ND = DR->getDecl(); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 206 | return ND == x || ND == y ? DR : nullptr; |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 207 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 209 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) |
| 210 | return U->isIncrementDecrementOp() |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 211 | ? getIncrementedVar(U->getSubExpr(), x, y) : nullptr; |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 212 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 213 | return nullptr; |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 216 | /// CheckLoopConditionForFloat - This check looks for 'for' statements that |
| 217 | /// use a floating point variable as a loop counter. |
| 218 | /// CERT: FLP30-C, FLP30-CPP. |
| 219 | /// |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 220 | void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 221 | if (!filter.check_FloatLoopCounter) |
| 222 | return; |
| 223 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 224 | // Does the loop have a condition? |
| 225 | const Expr *condition = FS->getCond(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 227 | if (!condition) |
| 228 | return; |
| 229 | |
| 230 | // Does the loop have an increment? |
| 231 | const Expr *increment = FS->getInc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 233 | if (!increment) |
| 234 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 236 | // Strip away '()' and casts. |
| 237 | condition = condition->IgnoreParenCasts(); |
| 238 | increment = increment->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 240 | // Is the loop condition a comparison? |
| 241 | const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); |
| 242 | |
| 243 | if (!B) |
| 244 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | b9cb113 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 246 | // Is this a comparison? |
| 247 | if (!(B->isRelationalOp() || B->isEqualityOp())) |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 248 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 250 | // Are we comparing variables? |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 251 | const DeclRefExpr *drLHS = |
| 252 | dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts()); |
| 253 | const DeclRefExpr *drRHS = |
| 254 | dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | b9cb113 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 256 | // Does at least one of the variables have a floating point type? |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 257 | drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : nullptr; |
| 258 | drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 260 | if (!drLHS && !drRHS) |
| 261 | return; |
| 262 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 263 | const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : nullptr; |
| 264 | const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 266 | if (!vdLHS && !vdRHS) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | return; |
| 268 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 269 | // Does either variable appear in increment? |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 270 | const DeclRefExpr *drInc = getIncrementedVar(increment, vdLHS, vdRHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 272 | if (!drInc) |
| 273 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 275 | // Emit the error. First figure out which DeclRefExpr in the condition |
| 276 | // referenced the compared variable. |
Ted Kremenek | 26a3661 | 2012-10-12 22:56:42 +0000 | [diff] [blame] | 277 | assert(drInc->getDecl()); |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 278 | const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; |
| 279 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 280 | SmallVector<SourceRange, 2> ranges; |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 281 | SmallString<256> sbuf; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 282 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 284 | os << "Variable '" << drCond->getDecl()->getName() |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 285 | << "' with floating point type '" << drCond->getType().getAsString() |
| 286 | << "' should not be used as a loop counter"; |
| 287 | |
| 288 | ranges.push_back(drCond->getSourceRange()); |
| 289 | ranges.push_back(drInc->getSourceRange()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 9c49762 | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 291 | const char *bugType = "Floating point variable used as loop counter"; |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 292 | |
| 293 | PathDiagnosticLocation FSLoc = |
| 294 | PathDiagnosticLocation::createBegin(FS, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 295 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_FloatLoopCounter, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 296 | bugType, "Security", os.str(), |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 297 | FSLoc, ranges); |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 301 | // Check: Any use of 'gets' is insecure. |
| 302 | // Originally: <rdar://problem/6335715> |
| 303 | // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov) |
Zhongxing Xu | f69973c | 2009-11-09 08:13:04 +0000 | [diff] [blame] | 304 | // CWE-242: Use of Inherently Dangerous Function |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
| 306 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 307 | void WalkAST::checkCall_gets(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 308 | if (!filter.check_gets) |
| 309 | return; |
| 310 | |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 311 | const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>(); |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 312 | if (!FPT) |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 313 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 315 | // Verify that the function takes a single argument. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 316 | if (FPT->getNumParams() != 1) |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 317 | return; |
| 318 | |
| 319 | // Is the argument a 'char*'? |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 320 | const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>(); |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 321 | if (!PT) |
| 322 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 324 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 325 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 327 | // Issue a warning. |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 328 | PathDiagnosticLocation CELoc = |
| 329 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 330 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_gets, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 331 | "Potential buffer overflow in call to 'gets'", |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 332 | "Security", |
| 333 | "Call to function 'gets' is extremely insecure as it can " |
| 334 | "always result in a buffer overflow", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 335 | CELoc, CE->getCallee()->getSourceRange()); |
Ted Kremenek | 6610c03 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 339 | // Check: Any use of 'getpwd' is insecure. |
| 340 | // CWE-477: Use of Obsolete Functions |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 343 | void WalkAST::checkCall_getpw(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 344 | if (!filter.check_getpw) |
| 345 | return; |
| 346 | |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 347 | const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>(); |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 348 | if (!FPT) |
| 349 | return; |
| 350 | |
| 351 | // Verify that the function takes two arguments. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 352 | if (FPT->getNumParams() != 2) |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 353 | return; |
| 354 | |
| 355 | // Verify the first argument type is integer. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 356 | if (!FPT->getParamType(0)->isIntegralOrUnscopedEnumerationType()) |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 357 | return; |
| 358 | |
| 359 | // Verify the second argument type is char*. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 360 | const PointerType *PT = FPT->getParamType(1)->getAs<PointerType>(); |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 361 | if (!PT) |
| 362 | return; |
| 363 | |
| 364 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 365 | return; |
| 366 | |
| 367 | // Issue a warning. |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 368 | PathDiagnosticLocation CELoc = |
| 369 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 370 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_getpw, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 371 | "Potential buffer overflow in call to 'getpw'", |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 372 | "Security", |
| 373 | "The getpw() function is dangerous as it may overflow the " |
| 374 | "provided buffer. It is obsoleted by getpwuid().", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 375 | CELoc, CE->getCallee()->getSourceRange()); |
Zhongxing Xu | d6e7f9d | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 379 | // Check: Any use of 'mktemp' is insecure. It is obsoleted by mkstemp(). |
Zhongxing Xu | 39bba62 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 380 | // CWE-377: Insecure Temporary File |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 383 | void WalkAST::checkCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | afddb9c | 2012-06-29 21:01:35 +0000 | [diff] [blame] | 384 | if (!filter.check_mktemp) { |
| 385 | // Fall back to the security check of looking for enough 'X's in the |
| 386 | // format string, since that is a less severe warning. |
| 387 | checkCall_mkstemp(CE, FD); |
| 388 | return; |
| 389 | } |
| 390 | |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 391 | const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>(); |
Zhongxing Xu | 39bba62 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 392 | if(!FPT) |
| 393 | return; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 394 | |
Lenny Maiorani | 70568c2 | 2011-03-31 21:26:55 +0000 | [diff] [blame] | 395 | // Verify that the function takes a single argument. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 396 | if (FPT->getNumParams() != 1) |
Zhongxing Xu | 39bba62 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 397 | return; |
| 398 | |
| 399 | // Verify that the argument is Pointer Type. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 400 | const PointerType *PT = FPT->getParamType(0)->getAs<PointerType>(); |
Zhongxing Xu | 39bba62 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 401 | if (!PT) |
| 402 | return; |
| 403 | |
| 404 | // Verify that the argument is a 'char*'. |
| 405 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 406 | return; |
Ted Kremenek | aeaf3d2 | 2010-03-24 22:39:45 +0000 | [diff] [blame] | 407 | |
Alp Toker | c3f36af | 2014-05-15 01:35:53 +0000 | [diff] [blame] | 408 | // Issue a warning. |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 409 | PathDiagnosticLocation CELoc = |
| 410 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 411 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_mktemp, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 412 | "Potential insecure temporary file in call 'mktemp'", |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 413 | "Security", |
| 414 | "Call to function 'mktemp' is insecure as it always " |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 415 | "creates or uses insecure temporary file. Use 'mkstemp' " |
| 416 | "instead", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 417 | CELoc, CE->getCallee()->getSourceRange()); |
Zhongxing Xu | 39bba62 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 420 | |
| 421 | //===----------------------------------------------------------------------===// |
| 422 | // Check: Use of 'mkstemp', 'mktemp', 'mkdtemp' should contain at least 6 X's. |
| 423 | //===----------------------------------------------------------------------===// |
| 424 | |
| 425 | void WalkAST::checkCall_mkstemp(const CallExpr *CE, const FunctionDecl *FD) { |
| 426 | if (!filter.check_mkstemp) |
| 427 | return; |
| 428 | |
| 429 | StringRef Name = FD->getIdentifier()->getName(); |
| 430 | std::pair<signed, signed> ArgSuffix = |
| 431 | llvm::StringSwitch<std::pair<signed, signed> >(Name) |
| 432 | .Case("mktemp", std::make_pair(0,-1)) |
| 433 | .Case("mkstemp", std::make_pair(0,-1)) |
| 434 | .Case("mkdtemp", std::make_pair(0,-1)) |
| 435 | .Case("mkstemps", std::make_pair(0,1)) |
| 436 | .Default(std::make_pair(-1, -1)); |
| 437 | |
| 438 | assert(ArgSuffix.first >= 0 && "Unsupported function"); |
| 439 | |
| 440 | // Check if the number of arguments is consistent with out expectations. |
| 441 | unsigned numArgs = CE->getNumArgs(); |
| 442 | if ((signed) numArgs <= ArgSuffix.first) |
| 443 | return; |
| 444 | |
| 445 | const StringLiteral *strArg = |
| 446 | dyn_cast<StringLiteral>(CE->getArg((unsigned)ArgSuffix.first) |
| 447 | ->IgnoreParenImpCasts()); |
| 448 | |
| 449 | // Currently we only handle string literals. It is possible to do better, |
| 450 | // either by looking at references to const variables, or by doing real |
| 451 | // flow analysis. |
| 452 | if (!strArg || strArg->getCharByteWidth() != 1) |
| 453 | return; |
| 454 | |
| 455 | // Count the number of X's, taking into account a possible cutoff suffix. |
| 456 | StringRef str = strArg->getString(); |
| 457 | unsigned numX = 0; |
| 458 | unsigned n = str.size(); |
| 459 | |
| 460 | // Take into account the suffix. |
| 461 | unsigned suffix = 0; |
| 462 | if (ArgSuffix.second >= 0) { |
| 463 | const Expr *suffixEx = CE->getArg((unsigned)ArgSuffix.second); |
| 464 | llvm::APSInt Result; |
| 465 | if (!suffixEx->EvaluateAsInt(Result, BR.getContext())) |
| 466 | return; |
| 467 | // FIXME: Issue a warning. |
| 468 | if (Result.isNegative()) |
| 469 | return; |
| 470 | suffix = (unsigned) Result.getZExtValue(); |
| 471 | n = (n > suffix) ? n - suffix : 0; |
| 472 | } |
| 473 | |
| 474 | for (unsigned i = 0; i < n; ++i) |
| 475 | if (str[i] == 'X') ++numX; |
| 476 | |
| 477 | if (numX >= 6) |
| 478 | return; |
| 479 | |
| 480 | // Issue a warning. |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 481 | PathDiagnosticLocation CELoc = |
| 482 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 483 | SmallString<512> buf; |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 484 | llvm::raw_svector_ostream out(buf); |
| 485 | out << "Call to '" << Name << "' should have at least 6 'X's in the" |
| 486 | " format string to be secure (" << numX << " 'X'"; |
| 487 | if (numX != 1) |
| 488 | out << 's'; |
| 489 | out << " seen"; |
| 490 | if (suffix) { |
| 491 | out << ", " << suffix << " character"; |
| 492 | if (suffix > 1) |
| 493 | out << 's'; |
| 494 | out << " used as a suffix"; |
| 495 | } |
| 496 | out << ')'; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 497 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_mkstemp, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 498 | "Insecure temporary file creation", "Security", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 499 | out.str(), CELoc, strArg->getSourceRange()); |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Zhongxing Xu | 39bba62 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 502 | //===----------------------------------------------------------------------===// |
Lenny Maiorani | 6ffe738 | 2011-03-31 22:09:14 +0000 | [diff] [blame] | 503 | // Check: Any use of 'strcpy' is insecure. |
| 504 | // |
| 505 | // CWE-119: Improper Restriction of Operations within |
| 506 | // the Bounds of a Memory Buffer |
| 507 | //===----------------------------------------------------------------------===// |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 508 | void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 509 | if (!filter.check_strcpy) |
| 510 | return; |
| 511 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 512 | if (!checkCall_strCommon(CE, FD)) |
Lenny Maiorani | 6ffe738 | 2011-03-31 22:09:14 +0000 | [diff] [blame] | 513 | return; |
| 514 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 515 | // Issue a warning. |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 516 | PathDiagnosticLocation CELoc = |
| 517 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 518 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 519 | "Potential insecure memory buffer bounds restriction in " |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 520 | "call 'strcpy'", |
| 521 | "Security", |
| 522 | "Call to function 'strcpy' is insecure as it does not " |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 523 | "provide bounding of the memory buffer. Replace " |
| 524 | "unbounded copy functions with analogous functions that " |
| 525 | "support length arguments such as 'strlcpy'. CWE-119.", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 526 | CELoc, CE->getCallee()->getSourceRange()); |
Lenny Maiorani | 6ffe738 | 2011-03-31 22:09:14 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | //===----------------------------------------------------------------------===// |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 530 | // Check: Any use of 'strcat' is insecure. |
| 531 | // |
| 532 | // CWE-119: Improper Restriction of Operations within |
| 533 | // the Bounds of a Memory Buffer |
| 534 | //===----------------------------------------------------------------------===// |
| 535 | void WalkAST::checkCall_strcat(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 536 | if (!filter.check_strcpy) |
| 537 | return; |
| 538 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 539 | if (!checkCall_strCommon(CE, FD)) |
| 540 | return; |
| 541 | |
| 542 | // Issue a warning. |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 543 | PathDiagnosticLocation CELoc = |
| 544 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 545 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 546 | "Potential insecure memory buffer bounds restriction in " |
| 547 | "call 'strcat'", |
| 548 | "Security", |
| 549 | "Call to function 'strcat' is insecure as it does not " |
| 550 | "provide bounding of the memory buffer. Replace " |
| 551 | "unbounded copy functions with analogous functions that " |
| 552 | "support length arguments such as 'strlcat'. CWE-119.", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 553 | CELoc, CE->getCallee()->getSourceRange()); |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | //===----------------------------------------------------------------------===// |
| 557 | // Common check for str* functions with no bounds parameters. |
| 558 | //===----------------------------------------------------------------------===// |
| 559 | bool WalkAST::checkCall_strCommon(const CallExpr *CE, const FunctionDecl *FD) { |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 560 | const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>(); |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 561 | if (!FPT) |
| 562 | return false; |
| 563 | |
| 564 | // Verify the function takes two arguments, three in the _chk version. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 565 | int numArgs = FPT->getNumParams(); |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 566 | if (numArgs != 2 && numArgs != 3) |
| 567 | return false; |
| 568 | |
| 569 | // Verify the type for both arguments. |
| 570 | for (int i = 0; i < 2; i++) { |
| 571 | // Verify that the arguments are pointers. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 572 | const PointerType *PT = FPT->getParamType(i)->getAs<PointerType>(); |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 573 | if (!PT) |
| 574 | return false; |
| 575 | |
| 576 | // Verify that the argument is a 'char*'. |
| 577 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 585 | // Check: Linear congruent random number generators should not be used |
| 586 | // Originally: <rdar://problem/63371000> |
| 587 | // CWE-338: Use of cryptographically weak prng |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 590 | void WalkAST::checkCall_rand(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 591 | if (!filter.check_rand || !CheckRand) |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 592 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 594 | const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>(); |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 595 | if (!FTP) |
| 596 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 598 | if (FTP->getNumParams() == 1) { |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 599 | // Is the argument an 'unsigned short *'? |
| 600 | // (Actually any integer type is allowed.) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 601 | const PointerType *PT = FTP->getParamType(0)->getAs<PointerType>(); |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 602 | if (!PT) |
| 603 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Jordan Rose | 61e221f | 2013-04-09 02:30:33 +0000 | [diff] [blame] | 605 | if (! PT->getPointeeType()->isIntegralOrUnscopedEnumerationType()) |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 606 | return; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 607 | } else if (FTP->getNumParams() != 0) |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 608 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 610 | // Issue a warning. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 611 | SmallString<256> buf1; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 612 | llvm::raw_svector_ostream os1(buf1); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 613 | os1 << '\'' << *FD << "' is a poor random number generator"; |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 614 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 615 | SmallString<256> buf2; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 616 | llvm::raw_svector_ostream os2(buf2); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 617 | os2 << "Function '" << *FD |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 618 | << "' is obsolete because it implements a poor random number generator." |
| 619 | << " Use 'arc4random' instead"; |
| 620 | |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 621 | PathDiagnosticLocation CELoc = |
| 622 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 623 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, os1.str(), |
| 624 | "Security", os2.str(), CELoc, |
| 625 | CE->getCallee()->getSourceRange()); |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | // Check: 'random' should not be used |
| 630 | // Originally: <rdar://problem/63371000> |
| 631 | //===----------------------------------------------------------------------===// |
| 632 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 633 | void WalkAST::checkCall_random(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 634 | if (!CheckRand || !filter.check_rand) |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 635 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 637 | const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>(); |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 638 | if (!FTP) |
| 639 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 641 | // Verify that the function takes no argument. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 642 | if (FTP->getNumParams() != 0) |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 643 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 645 | // Issue a warning. |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 646 | PathDiagnosticLocation CELoc = |
| 647 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 648 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_rand, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 649 | "'random' is not a secure random number generator", |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 650 | "Security", |
| 651 | "The 'random' function produces a sequence of values that " |
| 652 | "an adversary may be able to predict. Use 'arc4random' " |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 653 | "instead", CELoc, CE->getCallee()->getSourceRange()); |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | //===----------------------------------------------------------------------===// |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 657 | // Check: 'vfork' should not be used. |
| 658 | // POS33-C: Do not use vfork(). |
| 659 | //===----------------------------------------------------------------------===// |
| 660 | |
| 661 | void WalkAST::checkCall_vfork(const CallExpr *CE, const FunctionDecl *FD) { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 662 | if (!filter.check_vfork) |
| 663 | return; |
| 664 | |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 665 | // All calls to vfork() are insecure, issue a warning. |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 666 | PathDiagnosticLocation CELoc = |
| 667 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 668 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_vfork, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 669 | "Potential insecure implementation-specific behavior in " |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 670 | "call 'vfork'", |
| 671 | "Security", |
| 672 | "Call to function 'vfork' is insecure as it can lead to " |
| 673 | "denial of service situations in the parent process. " |
| 674 | "Replace calls to vfork with calls to the safer " |
| 675 | "'posix_spawn' function", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 676 | CELoc, CE->getCallee()->getSourceRange()); |
Anna Zaks | fedf5df | 2011-10-11 04:34:54 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 680 | // Check: Should check whether privileges are dropped successfully. |
| 681 | // Originally: <rdar://problem/6337132> |
| 682 | //===----------------------------------------------------------------------===// |
| 683 | |
Lenny Maiorani | de909e4 | 2011-04-05 20:18:46 +0000 | [diff] [blame] | 684 | void WalkAST::checkUncheckedReturnValue(CallExpr *CE) { |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 685 | if (!filter.check_UncheckedReturn) |
| 686 | return; |
| 687 | |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 688 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 689 | if (!FD) |
| 690 | return; |
| 691 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 692 | if (II_setid[0] == nullptr) { |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 693 | static const char * const identifiers[num_setids] = { |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 694 | "setuid", "setgid", "seteuid", "setegid", |
| 695 | "setreuid", "setregid" |
| 696 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 698 | for (size_t i = 0; i < num_setids; i++) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | II_setid[i] = &BR.getContext().Idents.get(identifiers[i]); |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 700 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 702 | const IdentifierInfo *id = FD->getIdentifier(); |
| 703 | size_t identifierid; |
| 704 | |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 705 | for (identifierid = 0; identifierid < num_setids; identifierid++) |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 706 | if (id == II_setid[identifierid]) |
| 707 | break; |
| 708 | |
Ted Kremenek | ad5a600 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 709 | if (identifierid >= num_setids) |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 710 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Eli Friedman | 57d1f1c | 2013-06-24 18:47:11 +0000 | [diff] [blame] | 712 | const FunctionProtoType *FTP = FD->getType()->getAs<FunctionProtoType>(); |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 713 | if (!FTP) |
| 714 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | |
Ted Kremenek | aeca095 | 2009-08-28 00:24:55 +0000 | [diff] [blame] | 716 | // Verify that the function takes one or two arguments (depending on |
| 717 | // the function). |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 718 | if (FTP->getNumParams() != (identifierid < 4 ? 1 : 2)) |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 719 | return; |
| 720 | |
| 721 | // The arguments must be integers. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 722 | for (unsigned i = 0; i < FTP->getNumParams(); i++) |
| 723 | if (!FTP->getParamType(i)->isIntegralOrUnscopedEnumerationType()) |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 724 | return; |
| 725 | |
| 726 | // Issue a warning. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 727 | SmallString<256> buf1; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 728 | llvm::raw_svector_ostream os1(buf1); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 729 | os1 << "Return value is not checked in call to '" << *FD << '\''; |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 730 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 731 | SmallString<256> buf2; |
Ted Kremenek | 8edc6df | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 732 | llvm::raw_svector_ostream os2(buf2); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 733 | os2 << "The return value from the call to '" << *FD |
| 734 | << "' is not checked. If an error occurs in '" << *FD |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 735 | << "', the following code may execute with unexpected privileges"; |
| 736 | |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 737 | PathDiagnosticLocation CELoc = |
| 738 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 739 | BR.EmitBasicReport(AC->getDecl(), filter.checkName_UncheckedReturn, os1.str(), |
| 740 | "Security", os2.str(), CELoc, |
| 741 | CE->getCallee()->getSourceRange()); |
Ted Kremenek | d032fcc | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 745 | // SecuritySyntaxChecker |
Ted Kremenek | c5b4c0e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 746 | //===----------------------------------------------------------------------===// |
| 747 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 748 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 749 | class SecuritySyntaxChecker : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 750 | public: |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 751 | ChecksFilter filter; |
| 752 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 753 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 754 | BugReporter &BR) const { |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 755 | WalkAST walker(BR, mgr.getAnalysisDeclContext(D), filter); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 756 | walker.Visit(D->getBody()); |
| 757 | } |
| 758 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 759 | } |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 760 | |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 761 | #define REGISTER_CHECKER(name) \ |
| 762 | void ento::register##name(CheckerManager &mgr) { \ |
| 763 | SecuritySyntaxChecker *checker = \ |
| 764 | mgr.registerChecker<SecuritySyntaxChecker>(); \ |
| 765 | checker->filter.check_##name = true; \ |
| 766 | checker->filter.checkName_##name = mgr.getCurrentCheckName(); \ |
| 767 | } |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 768 | |
| 769 | REGISTER_CHECKER(gets) |
| 770 | REGISTER_CHECKER(getpw) |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 771 | REGISTER_CHECKER(mkstemp) |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 772 | REGISTER_CHECKER(mktemp) |
| 773 | REGISTER_CHECKER(strcpy) |
| 774 | REGISTER_CHECKER(rand) |
| 775 | REGISTER_CHECKER(vfork) |
| 776 | REGISTER_CHECKER(FloatLoopCounter) |
Ted Kremenek | 89eaf8d | 2012-01-20 05:35:06 +0000 | [diff] [blame] | 777 | REGISTER_CHECKER(UncheckedReturn) |
| 778 | |
Ted Kremenek | c54dc95 | 2012-01-20 01:44:29 +0000 | [diff] [blame] | 779 | |