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 | |
Ted Kremenek | 88c8bc8 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 14 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | 2114258 | 2010-12-23 19:38:26 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/BugReporter/BugReporter.h" |
| 16 | #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h" |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 21 | using namespace ento; |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 22 | |
Ted Kremenek | 88c8bc8 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 23 | static bool isArc4RandomAvailable(const ASTContext &Ctx) { |
| 24 | const llvm::Triple &T = Ctx.Target.getTriple(); |
| 25 | return T.getVendor() == llvm::Triple::Apple || |
| 26 | T.getOS() == llvm::Triple::FreeBSD; |
| 27 | } |
| 28 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 29 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 30 | class WalkAST : public StmtVisitor<WalkAST> { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | BugReporter &BR; |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 32 | IdentifierInfo *II_gets; |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 33 | IdentifierInfo *II_getpw; |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 34 | IdentifierInfo *II_mktemp; |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 35 | enum { num_rands = 9 }; |
| 36 | IdentifierInfo *II_rand[num_rands]; |
| 37 | IdentifierInfo *II_random; |
| 38 | enum { num_setids = 6 }; |
| 39 | IdentifierInfo *II_setid[num_setids]; |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | 88c8bc8 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 41 | const bool CheckRand; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 43 | public: |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 44 | WalkAST(BugReporter &br) : BR(br), |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 45 | II_gets(0), II_getpw(0), II_mktemp(0), |
| 46 | II_rand(), II_random(0), II_setid(), |
Ted Kremenek | 88c8bc8 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 47 | CheckRand(isArc4RandomAvailable(BR.getContext())) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 49 | // Statement visitor methods. |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 50 | void VisitCallExpr(CallExpr *CE); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 51 | void VisitForStmt(ForStmt *S); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 52 | void VisitCompoundStmt (CompoundStmt *S); |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 53 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 54 | |
| 55 | void VisitChildren(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 57 | // Helpers. |
| 58 | IdentifierInfo *GetIdentifier(IdentifierInfo *& II, const char *str); |
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 | // Checker-specific methods. |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 61 | void CheckLoopConditionForFloat(const ForStmt *FS); |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 62 | void CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD); |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 63 | void CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD); |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 64 | void CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 65 | void CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD); |
| 66 | void CheckCall_random(const CallExpr *CE, const FunctionDecl *FD); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 67 | void CheckUncheckedReturnValue(CallExpr *CE); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 68 | }; |
| 69 | } // end anonymous namespace |
| 70 | |
| 71 | //===----------------------------------------------------------------------===// |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 72 | // Helper methods. |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | |
| 75 | IdentifierInfo *WalkAST::GetIdentifier(IdentifierInfo *& II, const char *str) { |
| 76 | if (!II) |
| 77 | II = &BR.getContext().Idents.get(str); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
| 79 | return II; |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 83 | // AST walking. |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | void WalkAST::VisitChildren(Stmt *S) { |
| 87 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 88 | if (Stmt *child = *I) |
| 89 | Visit(child); |
| 90 | } |
| 91 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 92 | void WalkAST::VisitCallExpr(CallExpr *CE) { |
| 93 | if (const FunctionDecl *FD = CE->getDirectCallee()) { |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 94 | CheckCall_gets(CE, FD); |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 95 | CheckCall_getpw(CE, FD); |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 96 | CheckCall_mktemp(CE, FD); |
Ted Kremenek | 88c8bc8 | 2010-01-15 08:20:31 +0000 | [diff] [blame] | 97 | if (CheckRand) { |
| 98 | CheckCall_rand(CE, FD); |
| 99 | CheckCall_random(CE, FD); |
| 100 | } |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 103 | // Recurse and check children. |
| 104 | VisitChildren(CE); |
| 105 | } |
| 106 | |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 107 | void WalkAST::VisitCompoundStmt(CompoundStmt *S) { |
| 108 | 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] | 109 | if (Stmt *child = *I) { |
| 110 | if (CallExpr *CE = dyn_cast<CallExpr>(child)) |
| 111 | CheckUncheckedReturnValue(CE); |
| 112 | Visit(child); |
| 113 | } |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 116 | void WalkAST::VisitForStmt(ForStmt *FS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | CheckLoopConditionForFloat(FS); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 119 | // Recurse and check children. |
| 120 | VisitChildren(FS); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 124 | // Check: floating poing variable used as loop counter. |
Ted Kremenek | 5abeb52 | 2009-07-23 21:44:18 +0000 | [diff] [blame] | 125 | // Originally: <rdar://problem/6336718> |
| 126 | // Implements: CERT security coding advisory FLP-30. |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
| 128 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 129 | static const DeclRefExpr* |
| 130 | GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { |
| 131 | expr = expr->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
| 133 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 134 | if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 135 | B->getOpcode() == BO_Comma)) |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 136 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 138 | if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y)) |
| 139 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 141 | if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y)) |
| 142 | return rhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 144 | return NULL; |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 147 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { |
| 148 | const NamedDecl *ND = DR->getDecl(); |
| 149 | return ND == x || ND == y ? DR : NULL; |
| 150 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 152 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) |
| 153 | return U->isIncrementDecrementOp() |
| 154 | ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL; |
| 155 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
| 158 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 159 | /// CheckLoopConditionForFloat - This check looks for 'for' statements that |
| 160 | /// use a floating point variable as a loop counter. |
| 161 | /// CERT: FLP30-C, FLP30-CPP. |
| 162 | /// |
| 163 | void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) { |
| 164 | // Does the loop have a condition? |
| 165 | const Expr *condition = FS->getCond(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 167 | if (!condition) |
| 168 | return; |
| 169 | |
| 170 | // Does the loop have an increment? |
| 171 | const Expr *increment = FS->getInc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 173 | if (!increment) |
| 174 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 176 | // Strip away '()' and casts. |
| 177 | condition = condition->IgnoreParenCasts(); |
| 178 | increment = increment->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 180 | // Is the loop condition a comparison? |
| 181 | const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); |
| 182 | |
| 183 | if (!B) |
| 184 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | cad9f41 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 186 | // Is this a comparison? |
| 187 | if (!(B->isRelationalOp() || B->isEqualityOp())) |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 188 | return; |
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 | // Are we comparing variables? |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 191 | const DeclRefExpr *drLHS = |
| 192 | dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenLValueCasts()); |
| 193 | const DeclRefExpr *drRHS = |
| 194 | dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParenLValueCasts()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | cad9f41 | 2009-07-24 20:26:31 +0000 | [diff] [blame] | 196 | // Does at least one of the variables have a floating point type? |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 197 | drLHS = drLHS && drLHS->getType()->isRealFloatingType() ? drLHS : NULL; |
| 198 | drRHS = drRHS && drRHS->getType()->isRealFloatingType() ? drRHS : NULL; |
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 (!drLHS && !drRHS) |
| 201 | return; |
| 202 | |
| 203 | const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL; |
| 204 | const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 206 | if (!vdLHS && !vdRHS) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | return; |
| 208 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 209 | // Does either variable appear in increment? |
| 210 | const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 212 | if (!drInc) |
| 213 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 215 | // Emit the error. First figure out which DeclRefExpr in the condition |
| 216 | // referenced the compared variable. |
| 217 | const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; |
| 218 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | llvm::SmallVector<SourceRange, 2> ranges; |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 220 | llvm::SmallString<256> sbuf; |
| 221 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 223 | os << "Variable '" << drCond->getDecl()->getName() |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 224 | << "' with floating point type '" << drCond->getType().getAsString() |
| 225 | << "' should not be used as a loop counter"; |
| 226 | |
| 227 | ranges.push_back(drCond->getSourceRange()); |
| 228 | ranges.push_back(drInc->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 230 | const char *bugType = "Floating point variable used as loop counter"; |
Benjamin Kramer | f017173 | 2009-11-29 18:27:55 +0000 | [diff] [blame] | 231 | BR.EmitBasicReport(bugType, "Security", os.str(), |
Ted Kremenek | 8baf86d | 2009-07-23 21:34:35 +0000 | [diff] [blame] | 232 | FS->getLocStart(), ranges.data(), ranges.size()); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | //===----------------------------------------------------------------------===// |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 236 | // Check: Any use of 'gets' is insecure. |
| 237 | // Originally: <rdar://problem/6335715> |
| 238 | // Implements (part of): 300-BSI (buildsecurityin.us-cert.gov) |
Zhongxing Xu | aa30b3b | 2009-11-09 08:13:04 +0000 | [diff] [blame] | 239 | // CWE-242: Use of Inherently Dangerous Function |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 240 | //===----------------------------------------------------------------------===// |
| 241 | |
| 242 | void WalkAST::CheckCall_gets(const CallExpr *CE, const FunctionDecl *FD) { |
| 243 | if (FD->getIdentifier() != GetIdentifier(II_gets, "gets")) |
| 244 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 246 | const FunctionProtoType *FPT |
| 247 | = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 248 | if (!FPT) |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 249 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 251 | // Verify that the function takes a single argument. |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 252 | if (FPT->getNumArgs() != 1) |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 253 | return; |
| 254 | |
| 255 | // Is the argument a 'char*'? |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 256 | const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0)); |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 257 | if (!PT) |
| 258 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 260 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 261 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | efcbb15 | 2009-07-23 22:29:41 +0000 | [diff] [blame] | 263 | // Issue a warning. |
| 264 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 265 | BR.EmitBasicReport("Potential buffer overflow in call to 'gets'", |
| 266 | "Security", |
| 267 | "Call to function 'gets' is extremely insecure as it can " |
| 268 | "always result in a buffer overflow", |
| 269 | CE->getLocStart(), &R, 1); |
| 270 | } |
| 271 | |
| 272 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 273 | // Check: Any use of 'getpwd' is insecure. |
| 274 | // CWE-477: Use of Obsolete Functions |
| 275 | //===----------------------------------------------------------------------===// |
| 276 | |
| 277 | void WalkAST::CheckCall_getpw(const CallExpr *CE, const FunctionDecl *FD) { |
| 278 | if (FD->getIdentifier() != GetIdentifier(II_getpw, "getpw")) |
| 279 | return; |
| 280 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 281 | const FunctionProtoType *FPT |
| 282 | = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); |
Zhongxing Xu | bd842e3 | 2009-11-09 12:19:26 +0000 | [diff] [blame] | 283 | if (!FPT) |
| 284 | return; |
| 285 | |
| 286 | // Verify that the function takes two arguments. |
| 287 | if (FPT->getNumArgs() != 2) |
| 288 | return; |
| 289 | |
| 290 | // Verify the first argument type is integer. |
| 291 | if (!FPT->getArgType(0)->isIntegerType()) |
| 292 | return; |
| 293 | |
| 294 | // Verify the second argument type is char*. |
| 295 | const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(1)); |
| 296 | if (!PT) |
| 297 | return; |
| 298 | |
| 299 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 300 | return; |
| 301 | |
| 302 | // Issue a warning. |
| 303 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 304 | BR.EmitBasicReport("Potential buffer overflow in call to 'getpw'", |
| 305 | "Security", |
| 306 | "The getpw() function is dangerous as it may overflow the " |
| 307 | "provided buffer. It is obsoleted by getpwuid().", |
| 308 | CE->getLocStart(), &R, 1); |
| 309 | } |
| 310 | |
| 311 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 312 | // Check: Any use of 'mktemp' is insecure.It is obsoleted by mkstemp(). |
| 313 | // CWE-377: Insecure Temporary File |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | |
| 316 | void WalkAST::CheckCall_mktemp(const CallExpr *CE, const FunctionDecl *FD) { |
| 317 | if (FD->getIdentifier() != GetIdentifier(II_mktemp, "mktemp")) |
| 318 | return; |
| 319 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 320 | const FunctionProtoType *FPT |
| 321 | = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 322 | if(!FPT) |
| 323 | return; |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 324 | |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 325 | // Verify that the funcion takes a single argument. |
| 326 | if (FPT->getNumArgs() != 1) |
| 327 | return; |
| 328 | |
| 329 | // Verify that the argument is Pointer Type. |
| 330 | const PointerType *PT = dyn_cast<PointerType>(FPT->getArgType(0)); |
| 331 | if (!PT) |
| 332 | return; |
| 333 | |
| 334 | // Verify that the argument is a 'char*'. |
| 335 | if (PT->getPointeeType().getUnqualifiedType() != BR.getContext().CharTy) |
| 336 | return; |
Ted Kremenek | 431a2cb | 2010-03-24 22:39:45 +0000 | [diff] [blame] | 337 | |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 338 | // Issue a waring. |
| 339 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 340 | BR.EmitBasicReport("Potential insecure temporary file in call 'mktemp'", |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 341 | "Security", |
| 342 | "Call to function 'mktemp' is insecure as it always " |
| 343 | "creates or uses insecure temporary file. Use 'mkstemp' instead", |
| 344 | CE->getLocStart(), &R, 1); |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Zhongxing Xu | 1bf4056 | 2009-12-03 09:15:23 +0000 | [diff] [blame] | 347 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 348 | // Check: Linear congruent random number generators should not be used |
| 349 | // Originally: <rdar://problem/63371000> |
| 350 | // CWE-338: Use of cryptographically weak prng |
| 351 | //===----------------------------------------------------------------------===// |
| 352 | |
| 353 | void WalkAST::CheckCall_rand(const CallExpr *CE, const FunctionDecl *FD) { |
| 354 | if (II_rand[0] == NULL) { |
| 355 | // This check applies to these functions |
| 356 | static const char * const identifiers[num_rands] = { |
| 357 | "drand48", "erand48", "jrand48", "lrand48", "mrand48", "nrand48", |
| 358 | "lcong48", |
| 359 | "rand", "rand_r" |
| 360 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 362 | for (size_t i = 0; i < num_rands; i++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | II_rand[i] = &BR.getContext().Idents.get(identifiers[i]); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 364 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 366 | const IdentifierInfo *id = FD->getIdentifier(); |
| 367 | size_t identifierid; |
| 368 | |
| 369 | for (identifierid = 0; identifierid < num_rands; identifierid++) |
| 370 | if (id == II_rand[identifierid]) |
| 371 | break; |
| 372 | |
| 373 | if (identifierid >= num_rands) |
| 374 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 376 | const FunctionProtoType *FTP |
| 377 | = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 378 | if (!FTP) |
| 379 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 381 | if (FTP->getNumArgs() == 1) { |
| 382 | // Is the argument an 'unsigned short *'? |
| 383 | // (Actually any integer type is allowed.) |
| 384 | const PointerType *PT = dyn_cast<PointerType>(FTP->getArgType(0)); |
| 385 | if (!PT) |
| 386 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 388 | if (! PT->getPointeeType()->isIntegerType()) |
| 389 | return; |
| 390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | else if (FTP->getNumArgs() != 0) |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 392 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 394 | // Issue a warning. |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 395 | llvm::SmallString<256> buf1; |
| 396 | llvm::raw_svector_ostream os1(buf1); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 397 | os1 << '\'' << FD << "' is a poor random number generator"; |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 399 | llvm::SmallString<256> buf2; |
| 400 | llvm::raw_svector_ostream os2(buf2); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 401 | os2 << "Function '" << FD |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 402 | << "' is obsolete because it implements a poor random number generator." |
| 403 | << " Use 'arc4random' instead"; |
| 404 | |
| 405 | SourceRange R = CE->getCallee()->getSourceRange(); |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 406 | BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | //===----------------------------------------------------------------------===// |
| 410 | // Check: 'random' should not be used |
| 411 | // Originally: <rdar://problem/63371000> |
| 412 | //===----------------------------------------------------------------------===// |
| 413 | |
| 414 | void WalkAST::CheckCall_random(const CallExpr *CE, const FunctionDecl *FD) { |
| 415 | if (FD->getIdentifier() != GetIdentifier(II_random, "random")) |
| 416 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 418 | const FunctionProtoType *FTP |
| 419 | = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 420 | if (!FTP) |
| 421 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 423 | // Verify that the function takes no argument. |
| 424 | if (FTP->getNumArgs() != 0) |
| 425 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 427 | // Issue a warning. |
| 428 | SourceRange R = CE->getCallee()->getSourceRange(); |
| 429 | BR.EmitBasicReport("'random' is not a secure random number generator", |
| 430 | "Security", |
| 431 | "The 'random' function produces a sequence of values that " |
| 432 | "an adversary may be able to predict. Use 'arc4random' " |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 433 | "instead", CE->getLocStart(), &R, 1); |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 437 | // Check: Should check whether privileges are dropped successfully. |
| 438 | // Originally: <rdar://problem/6337132> |
| 439 | //===----------------------------------------------------------------------===// |
| 440 | |
| 441 | void WalkAST::CheckUncheckedReturnValue(CallExpr *CE) { |
| 442 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 443 | if (!FD) |
| 444 | return; |
| 445 | |
| 446 | if (II_setid[0] == NULL) { |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 447 | static const char * const identifiers[num_setids] = { |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 448 | "setuid", "setgid", "seteuid", "setegid", |
| 449 | "setreuid", "setregid" |
| 450 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 452 | for (size_t i = 0; i < num_setids; i++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | II_setid[i] = &BR.getContext().Idents.get(identifiers[i]); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 456 | const IdentifierInfo *id = FD->getIdentifier(); |
| 457 | size_t identifierid; |
| 458 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 459 | for (identifierid = 0; identifierid < num_setids; identifierid++) |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 460 | if (id == II_setid[identifierid]) |
| 461 | break; |
| 462 | |
Ted Kremenek | 2465047 | 2009-09-02 02:47:41 +0000 | [diff] [blame] | 463 | if (identifierid >= num_setids) |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 464 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 466 | const FunctionProtoType *FTP |
| 467 | = dyn_cast<FunctionProtoType>(FD->getType().IgnoreParens()); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 468 | if (!FTP) |
| 469 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Ted Kremenek | a818783 | 2009-08-28 00:24:55 +0000 | [diff] [blame] | 471 | // Verify that the function takes one or two arguments (depending on |
| 472 | // the function). |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 473 | if (FTP->getNumArgs() != (identifierid < 4 ? 1 : 2)) |
| 474 | return; |
| 475 | |
| 476 | // The arguments must be integers. |
| 477 | for (unsigned i = 0; i < FTP->getNumArgs(); i++) |
| 478 | if (! FTP->getArgType(i)->isIntegerType()) |
| 479 | return; |
| 480 | |
| 481 | // Issue a warning. |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 482 | llvm::SmallString<256> buf1; |
| 483 | llvm::raw_svector_ostream os1(buf1); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 484 | os1 << "Return value is not checked in call to '" << FD << '\''; |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 486 | llvm::SmallString<256> buf2; |
| 487 | llvm::raw_svector_ostream os2(buf2); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 488 | os2 << "The return value from the call to '" << FD |
| 489 | << "' is not checked. If an error occurs in '" << FD |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 490 | << "', the following code may execute with unexpected privileges"; |
| 491 | |
| 492 | SourceRange R = CE->getCallee()->getSourceRange(); |
Ted Kremenek | 2c01676 | 2010-03-24 22:39:47 +0000 | [diff] [blame] | 493 | BR.EmitBasicReport(os1.str(), "Security", os2.str(),CE->getLocStart(), &R, 1); |
Ted Kremenek | 65a81a9 | 2009-08-28 00:08:09 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 497 | // Entry point for check. |
| 498 | //===----------------------------------------------------------------------===// |
| 499 | |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 500 | void ento::CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR) { |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 501 | WalkAST walker(BR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | walker.Visit(D->getBody()); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 503 | } |