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