Ted Kremenek | 076d84e | 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 | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 076d84e | 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> { |
| 24 | BugReporter &BR; |
| 25 | public: |
| 26 | WalkAST(BugReporter &br) : BR(br) {} |
| 27 | |
| 28 | // Statement visitor methods. |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 29 | void VisitForStmt(ForStmt *S); |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 30 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 31 | |
| 32 | void VisitChildren(Stmt *S); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 33 | |
| 34 | // Checker-specific methods. |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 35 | void CheckLoopConditionForFloat(const ForStmt *FS); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 36 | }; |
| 37 | } // end anonymous namespace |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // AST walking. |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | void WalkAST::VisitChildren(Stmt *S) { |
| 44 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 45 | if (Stmt *child = *I) |
| 46 | Visit(child); |
| 47 | } |
| 48 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 49 | void WalkAST::VisitForStmt(ForStmt *FS) { |
| 50 | CheckLoopConditionForFloat(FS); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 52 | // Recurse and check children. |
| 53 | VisitChildren(FS); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 57 | // Check: floating poing variable used as loop counter. |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 60 | static const DeclRefExpr* |
| 61 | GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) { |
| 62 | expr = expr->IgnoreParenCasts(); |
| 63 | |
| 64 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) { |
| 65 | if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() || |
| 66 | B->getOpcode() == BinaryOperator::Comma)) |
| 67 | return NULL; |
| 68 | |
| 69 | if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y)) |
| 70 | return lhs; |
| 71 | |
| 72 | if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y)) |
| 73 | return rhs; |
| 74 | |
| 75 | return NULL; |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 76 | } |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 77 | |
| 78 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) { |
| 79 | const NamedDecl *ND = DR->getDecl(); |
| 80 | return ND == x || ND == y ? DR : NULL; |
| 81 | } |
| 82 | |
| 83 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr)) |
| 84 | return U->isIncrementDecrementOp() |
| 85 | ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL; |
| 86 | |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
Ted Kremenek | f08a6c5 | 2009-07-23 21:34:35 +0000 | [diff] [blame^] | 90 | /// CheckLoopConditionForFloat - This check looks for 'for' statements that |
| 91 | /// use a floating point variable as a loop counter. |
| 92 | /// CERT: FLP30-C, FLP30-CPP. |
| 93 | /// |
| 94 | void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) { |
| 95 | // Does the loop have a condition? |
| 96 | const Expr *condition = FS->getCond(); |
| 97 | |
| 98 | if (!condition) |
| 99 | return; |
| 100 | |
| 101 | // Does the loop have an increment? |
| 102 | const Expr *increment = FS->getInc(); |
| 103 | |
| 104 | if (!increment) |
| 105 | return; |
| 106 | |
| 107 | // Strip away '()' and casts. |
| 108 | condition = condition->IgnoreParenCasts(); |
| 109 | increment = increment->IgnoreParenCasts(); |
| 110 | |
| 111 | // Is the loop condition a comparison? |
| 112 | const BinaryOperator *B = dyn_cast<BinaryOperator>(condition); |
| 113 | |
| 114 | if (!B) |
| 115 | return; |
| 116 | |
| 117 | // The actual error condition. |
| 118 | if (!((B->isRelationalOp() || B->isEqualityOp()) && |
| 119 | ((B->getLHS()->getType()->isFloatingType() || |
| 120 | B->getRHS()->getType()->isFloatingType())))) |
| 121 | return; |
| 122 | |
| 123 | // Are we comparing variables? |
| 124 | const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens()); |
| 125 | const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens()); |
| 126 | |
| 127 | if (!drLHS && !drRHS) |
| 128 | return; |
| 129 | |
| 130 | const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL; |
| 131 | const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL; |
| 132 | |
| 133 | if (!vdLHS && !vdRHS) |
| 134 | return; |
| 135 | |
| 136 | // Does either variable appear in increment? |
| 137 | const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS); |
| 138 | |
| 139 | if (!drInc) |
| 140 | return; |
| 141 | |
| 142 | // Emit the error. First figure out which DeclRefExpr in the condition |
| 143 | // referenced the compared variable. |
| 144 | const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS; |
| 145 | |
| 146 | llvm::SmallVector<SourceRange, 2> ranges; |
| 147 | std::string sbuf; |
| 148 | llvm::raw_string_ostream os(sbuf); |
| 149 | |
| 150 | os << "Variable '" << drCond->getDecl()->getNameAsCString() |
| 151 | << "' with floating point type '" << drCond->getType().getAsString() |
| 152 | << "' should not be used as a loop counter"; |
| 153 | |
| 154 | ranges.push_back(drCond->getSourceRange()); |
| 155 | ranges.push_back(drInc->getSourceRange()); |
| 156 | |
| 157 | const char *bugType = "Floating point variable used as loop counter"; |
| 158 | BR.EmitBasicReport(bugType, "Security", os.str().c_str(), |
| 159 | FS->getLocStart(), ranges.data(), ranges.size()); |
Ted Kremenek | 076d84e | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | // Entry point for check. |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
| 166 | void clang::CheckSecuritySyntaxOnly(Decl *D, BugReporter &BR) { |
| 167 | WalkAST walker(BR); |
| 168 | walker.Visit(D->getBody()); |
| 169 | } |