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