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