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