clang API Documentation
00001 // MallocOverflowSecurityChecker.cpp - Check for malloc overflows -*- C++ -*-=// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This checker detects a common memory allocation security flaw. 00011 // Suppose 'unsigned int n' comes from an untrusted source. If the 00012 // code looks like 'malloc (n * 4)', and an attacker can make 'n' be 00013 // say MAX_UINT/4+2, then instead of allocating the correct 'n' 4-byte 00014 // elements, this will actually allocate only two because of overflow. 00015 // Then when the rest of the program attempts to store values past the 00016 // second element, these values will actually overwrite other items in 00017 // the heap, probably allowing the attacker to execute arbitrary code. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #include "ClangSACheckers.h" 00022 #include "clang/AST/EvaluatedExprVisitor.h" 00023 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 00024 #include "clang/StaticAnalyzer/Core/Checker.h" 00025 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 00026 #include "llvm/ADT/SmallVector.h" 00027 00028 using namespace clang; 00029 using namespace ento; 00030 00031 namespace { 00032 struct MallocOverflowCheck { 00033 const BinaryOperator *mulop; 00034 const Expr *variable; 00035 00036 MallocOverflowCheck (const BinaryOperator *m, const Expr *v) 00037 : mulop(m), variable (v) 00038 {} 00039 }; 00040 00041 class MallocOverflowSecurityChecker : public Checker<check::ASTCodeBody> { 00042 public: 00043 void checkASTCodeBody(const Decl *D, AnalysisManager &mgr, 00044 BugReporter &BR) const; 00045 00046 void CheckMallocArgument( 00047 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows, 00048 const Expr *TheArgument, ASTContext &Context) const; 00049 00050 void OutputPossibleOverflows( 00051 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows, 00052 const Decl *D, BugReporter &BR, AnalysisManager &mgr) const; 00053 00054 }; 00055 } // end anonymous namespace 00056 00057 void MallocOverflowSecurityChecker::CheckMallocArgument( 00058 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows, 00059 const Expr *TheArgument, 00060 ASTContext &Context) const { 00061 00062 /* Look for a linear combination with a single variable, and at least 00063 one multiplication. 00064 Reject anything that applies to the variable: an explicit cast, 00065 conditional expression, an operation that could reduce the range 00066 of the result, or anything too complicated :-). */ 00067 const Expr * e = TheArgument; 00068 const BinaryOperator * mulop = nullptr; 00069 00070 for (;;) { 00071 e = e->IgnoreParenImpCasts(); 00072 if (isa<BinaryOperator>(e)) { 00073 const BinaryOperator * binop = dyn_cast<BinaryOperator>(e); 00074 BinaryOperatorKind opc = binop->getOpcode(); 00075 // TODO: ignore multiplications by 1, reject if multiplied by 0. 00076 if (mulop == nullptr && opc == BO_Mul) 00077 mulop = binop; 00078 if (opc != BO_Mul && opc != BO_Add && opc != BO_Sub && opc != BO_Shl) 00079 return; 00080 00081 const Expr *lhs = binop->getLHS(); 00082 const Expr *rhs = binop->getRHS(); 00083 if (rhs->isEvaluatable(Context)) 00084 e = lhs; 00085 else if ((opc == BO_Add || opc == BO_Mul) 00086 && lhs->isEvaluatable(Context)) 00087 e = rhs; 00088 else 00089 return; 00090 } 00091 else if (isa<DeclRefExpr>(e) || isa<MemberExpr>(e)) 00092 break; 00093 else 00094 return; 00095 } 00096 00097 if (mulop == nullptr) 00098 return; 00099 00100 // We've found the right structure of malloc argument, now save 00101 // the data so when the body of the function is completely available 00102 // we can check for comparisons. 00103 00104 // TODO: Could push this into the innermost scope where 'e' is 00105 // defined, rather than the whole function. 00106 PossibleMallocOverflows.push_back(MallocOverflowCheck(mulop, e)); 00107 } 00108 00109 namespace { 00110 // A worker class for OutputPossibleOverflows. 00111 class CheckOverflowOps : 00112 public EvaluatedExprVisitor<CheckOverflowOps> { 00113 public: 00114 typedef SmallVectorImpl<MallocOverflowCheck> theVecType; 00115 00116 private: 00117 theVecType &toScanFor; 00118 ASTContext &Context; 00119 00120 bool isIntZeroExpr(const Expr *E) const { 00121 if (!E->getType()->isIntegralOrEnumerationType()) 00122 return false; 00123 llvm::APSInt Result; 00124 if (E->EvaluateAsInt(Result, Context)) 00125 return Result == 0; 00126 return false; 00127 } 00128 00129 void CheckExpr(const Expr *E_p) { 00130 const Expr *E = E_p->IgnoreParenImpCasts(); 00131 00132 theVecType::iterator i = toScanFor.end(); 00133 theVecType::iterator e = toScanFor.begin(); 00134 00135 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { 00136 const Decl * EdreD = DR->getDecl(); 00137 while (i != e) { 00138 --i; 00139 if (const DeclRefExpr *DR_i = dyn_cast<DeclRefExpr>(i->variable)) { 00140 if (DR_i->getDecl() == EdreD) 00141 i = toScanFor.erase(i); 00142 } 00143 } 00144 } 00145 else if (isa<MemberExpr>(E)) { 00146 // No points-to analysis, just look at the member 00147 const Decl * EmeMD = dyn_cast<MemberExpr>(E)->getMemberDecl(); 00148 while (i != e) { 00149 --i; 00150 if (isa<MemberExpr>(i->variable)) { 00151 if (dyn_cast<MemberExpr>(i->variable)->getMemberDecl() == EmeMD) 00152 i = toScanFor.erase (i); 00153 } 00154 } 00155 } 00156 } 00157 00158 public: 00159 void VisitBinaryOperator(BinaryOperator *E) { 00160 if (E->isComparisonOp()) { 00161 const Expr * lhs = E->getLHS(); 00162 const Expr * rhs = E->getRHS(); 00163 // Ignore comparisons against zero, since they generally don't 00164 // protect against an overflow. 00165 if (!isIntZeroExpr(lhs) && ! isIntZeroExpr(rhs)) { 00166 CheckExpr(lhs); 00167 CheckExpr(rhs); 00168 } 00169 } 00170 EvaluatedExprVisitor<CheckOverflowOps>::VisitBinaryOperator(E); 00171 } 00172 00173 /* We specifically ignore loop conditions, because they're typically 00174 not error checks. */ 00175 void VisitWhileStmt(WhileStmt *S) { 00176 return this->Visit(S->getBody()); 00177 } 00178 void VisitForStmt(ForStmt *S) { 00179 return this->Visit(S->getBody()); 00180 } 00181 void VisitDoStmt(DoStmt *S) { 00182 return this->Visit(S->getBody()); 00183 } 00184 00185 CheckOverflowOps(theVecType &v, ASTContext &ctx) 00186 : EvaluatedExprVisitor<CheckOverflowOps>(ctx), 00187 toScanFor(v), Context(ctx) 00188 { } 00189 }; 00190 } 00191 00192 // OutputPossibleOverflows - We've found a possible overflow earlier, 00193 // now check whether Body might contain a comparison which might be 00194 // preventing the overflow. 00195 // This doesn't do flow analysis, range analysis, or points-to analysis; it's 00196 // just a dumb "is there a comparison" scan. The aim here is to 00197 // detect the most blatent cases of overflow and educate the 00198 // programmer. 00199 void MallocOverflowSecurityChecker::OutputPossibleOverflows( 00200 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows, 00201 const Decl *D, BugReporter &BR, AnalysisManager &mgr) const { 00202 // By far the most common case: nothing to check. 00203 if (PossibleMallocOverflows.empty()) 00204 return; 00205 00206 // Delete any possible overflows which have a comparison. 00207 CheckOverflowOps c(PossibleMallocOverflows, BR.getContext()); 00208 c.Visit(mgr.getAnalysisDeclContext(D)->getBody()); 00209 00210 // Output warnings for all overflows that are left. 00211 for (CheckOverflowOps::theVecType::iterator 00212 i = PossibleMallocOverflows.begin(), 00213 e = PossibleMallocOverflows.end(); 00214 i != e; 00215 ++i) { 00216 BR.EmitBasicReport( 00217 D, this, "malloc() size overflow", categories::UnixAPI, 00218 "the computation of the size of the memory allocation may overflow", 00219 PathDiagnosticLocation::createOperatorLoc(i->mulop, 00220 BR.getSourceManager()), 00221 i->mulop->getSourceRange()); 00222 } 00223 } 00224 00225 void MallocOverflowSecurityChecker::checkASTCodeBody(const Decl *D, 00226 AnalysisManager &mgr, 00227 BugReporter &BR) const { 00228 00229 CFG *cfg = mgr.getCFG(D); 00230 if (!cfg) 00231 return; 00232 00233 // A list of variables referenced in possibly overflowing malloc operands. 00234 SmallVector<MallocOverflowCheck, 2> PossibleMallocOverflows; 00235 00236 for (CFG::iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) { 00237 CFGBlock *block = *it; 00238 for (CFGBlock::iterator bi = block->begin(), be = block->end(); 00239 bi != be; ++bi) { 00240 if (Optional<CFGStmt> CS = bi->getAs<CFGStmt>()) { 00241 if (const CallExpr *TheCall = dyn_cast<CallExpr>(CS->getStmt())) { 00242 // Get the callee. 00243 const FunctionDecl *FD = TheCall->getDirectCallee(); 00244 00245 if (!FD) 00246 return; 00247 00248 // Get the name of the callee. If it's a builtin, strip off the prefix. 00249 IdentifierInfo *FnInfo = FD->getIdentifier(); 00250 if (!FnInfo) 00251 return; 00252 00253 if (FnInfo->isStr ("malloc") || FnInfo->isStr ("_MALLOC")) { 00254 if (TheCall->getNumArgs() == 1) 00255 CheckMallocArgument(PossibleMallocOverflows, TheCall->getArg(0), 00256 mgr.getASTContext()); 00257 } 00258 } 00259 } 00260 } 00261 } 00262 00263 OutputPossibleOverflows(PossibleMallocOverflows, D, BR, mgr); 00264 } 00265 00266 void 00267 ento::registerMallocOverflowSecurityChecker(CheckerManager &mgr) { 00268 mgr.registerChecker<MallocOverflowSecurityChecker>(); 00269 }