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