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