MallocOverflowSecurityChecker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // MallocOverflowSecurityChecker.cpp - Check for malloc overflows -*- C++ -*-=//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This checker detects a common memory allocation security flaw.
  10. // Suppose 'unsigned int n' comes from an untrusted source. If the
  11. // code looks like 'malloc (n * 4)', and an attacker can make 'n' be
  12. // say MAX_UINT/4+2, then instead of allocating the correct 'n' 4-byte
  13. // elements, this will actually allocate only two because of overflow.
  14. // Then when the rest of the program attempts to store values past the
  15. // second element, these values will actually overwrite other items in
  16. // the heap, probably allowing the attacker to execute arbitrary code.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  20. #include "clang/AST/EvaluatedExprVisitor.h"
  21. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  22. #include "clang/StaticAnalyzer/Core/Checker.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  24. #include "llvm/ADT/APSInt.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include <utility>
  27. using namespace clang;
  28. using namespace ento;
  29. using llvm::APSInt;
  30. namespace {
  31. struct MallocOverflowCheck {
  32. const CallExpr *call;
  33. const BinaryOperator *mulop;
  34. const Expr *variable;
  35. APSInt maxVal;
  36. MallocOverflowCheck(const CallExpr *call, const BinaryOperator *m,
  37. const Expr *v, APSInt val)
  38. : call(call), mulop(m), variable(v), maxVal(std::move(val)) {}
  39. };
  40. class MallocOverflowSecurityChecker : public Checker<check::ASTCodeBody> {
  41. public:
  42. void checkASTCodeBody(const Decl *D, AnalysisManager &mgr,
  43. BugReporter &BR) const;
  44. void CheckMallocArgument(
  45. SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
  46. const CallExpr *TheCall, ASTContext &Context) const;
  47. void OutputPossibleOverflows(
  48. SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
  49. const Decl *D, BugReporter &BR, AnalysisManager &mgr) const;
  50. };
  51. } // end anonymous namespace
  52. // Return true for computations which evaluate to zero: e.g., mult by 0.
  53. static inline bool EvaluatesToZero(APSInt &Val, BinaryOperatorKind op) {
  54. return (op == BO_Mul) && (Val == 0);
  55. }
  56. void MallocOverflowSecurityChecker::CheckMallocArgument(
  57. SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
  58. const CallExpr *TheCall, ASTContext &Context) const {
  59. /* Look for a linear combination with a single variable, and at least
  60. one multiplication.
  61. Reject anything that applies to the variable: an explicit cast,
  62. conditional expression, an operation that could reduce the range
  63. of the result, or anything too complicated :-). */
  64. const Expr *e = TheCall->getArg(0);
  65. const BinaryOperator * mulop = nullptr;
  66. APSInt maxVal;
  67. for (;;) {
  68. maxVal = 0;
  69. e = e->IgnoreParenImpCasts();
  70. if (const BinaryOperator *binop = dyn_cast<BinaryOperator>(e)) {
  71. BinaryOperatorKind opc = binop->getOpcode();
  72. // TODO: ignore multiplications by 1, reject if multiplied by 0.
  73. if (mulop == nullptr && opc == BO_Mul)
  74. mulop = binop;
  75. if (opc != BO_Mul && opc != BO_Add && opc != BO_Sub && opc != BO_Shl)
  76. return;
  77. const Expr *lhs = binop->getLHS();
  78. const Expr *rhs = binop->getRHS();
  79. if (rhs->isEvaluatable(Context)) {
  80. e = lhs;
  81. maxVal = rhs->EvaluateKnownConstInt(Context);
  82. if (EvaluatesToZero(maxVal, opc))
  83. return;
  84. } else if ((opc == BO_Add || opc == BO_Mul) &&
  85. lhs->isEvaluatable(Context)) {
  86. maxVal = lhs->EvaluateKnownConstInt(Context);
  87. if (EvaluatesToZero(maxVal, opc))
  88. return;
  89. e = rhs;
  90. } else
  91. return;
  92. } else if (isa<DeclRefExpr, MemberExpr>(e))
  93. break;
  94. else
  95. return;
  96. }
  97. if (mulop == nullptr)
  98. return;
  99. // We've found the right structure of malloc argument, now save
  100. // the data so when the body of the function is completely available
  101. // we can check for comparisons.
  102. PossibleMallocOverflows.push_back(
  103. MallocOverflowCheck(TheCall, mulop, e, maxVal));
  104. }
  105. namespace {
  106. // A worker class for OutputPossibleOverflows.
  107. class CheckOverflowOps :
  108. public EvaluatedExprVisitor<CheckOverflowOps> {
  109. public:
  110. typedef SmallVectorImpl<MallocOverflowCheck> theVecType;
  111. private:
  112. theVecType &toScanFor;
  113. ASTContext &Context;
  114. bool isIntZeroExpr(const Expr *E) const {
  115. if (!E->getType()->isIntegralOrEnumerationType())
  116. return false;
  117. Expr::EvalResult Result;
  118. if (E->EvaluateAsInt(Result, Context))
  119. return Result.Val.getInt() == 0;
  120. return false;
  121. }
  122. static const Decl *getDecl(const DeclRefExpr *DR) { return DR->getDecl(); }
  123. static const Decl *getDecl(const MemberExpr *ME) {
  124. return ME->getMemberDecl();
  125. }
  126. template <typename T1>
  127. void Erase(const T1 *DR,
  128. llvm::function_ref<bool(const MallocOverflowCheck &)> Pred) {
  129. auto P = [DR, Pred](const MallocOverflowCheck &Check) {
  130. if (const auto *CheckDR = dyn_cast<T1>(Check.variable))
  131. return getDecl(CheckDR) == getDecl(DR) && Pred(Check);
  132. return false;
  133. };
  134. llvm::erase_if(toScanFor, P);
  135. }
  136. void CheckExpr(const Expr *E_p) {
  137. const Expr *E = E_p->IgnoreParenImpCasts();
  138. const auto PrecedesMalloc = [E, this](const MallocOverflowCheck &c) {
  139. return Context.getSourceManager().isBeforeInTranslationUnit(
  140. E->getExprLoc(), c.call->getExprLoc());
  141. };
  142. if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
  143. Erase<DeclRefExpr>(DR, PrecedesMalloc);
  144. else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
  145. Erase<MemberExpr>(ME, PrecedesMalloc);
  146. }
  147. }
  148. // Check if the argument to malloc is assigned a value
  149. // which cannot cause an overflow.
  150. // e.g., malloc (mul * x) and,
  151. // case 1: mul = <constant value>
  152. // case 2: mul = a/b, where b > x
  153. void CheckAssignmentExpr(BinaryOperator *AssignEx) {
  154. bool assignKnown = false;
  155. bool numeratorKnown = false, denomKnown = false;
  156. APSInt denomVal;
  157. denomVal = 0;
  158. // Erase if the multiplicand was assigned a constant value.
  159. const Expr *rhs = AssignEx->getRHS();
  160. if (rhs->isEvaluatable(Context))
  161. assignKnown = true;
  162. // Discard the report if the multiplicand was assigned a value,
  163. // that can never overflow after multiplication. e.g., the assignment
  164. // is a division operator and the denominator is > other multiplicand.
  165. const Expr *rhse = rhs->IgnoreParenImpCasts();
  166. if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(rhse)) {
  167. if (BOp->getOpcode() == BO_Div) {
  168. const Expr *denom = BOp->getRHS()->IgnoreParenImpCasts();
  169. Expr::EvalResult Result;
  170. if (denom->EvaluateAsInt(Result, Context)) {
  171. denomVal = Result.Val.getInt();
  172. denomKnown = true;
  173. }
  174. const Expr *numerator = BOp->getLHS()->IgnoreParenImpCasts();
  175. if (numerator->isEvaluatable(Context))
  176. numeratorKnown = true;
  177. }
  178. }
  179. if (!assignKnown && !denomKnown)
  180. return;
  181. auto denomExtVal = denomVal.getExtValue();
  182. // Ignore negative denominator.
  183. if (denomExtVal < 0)
  184. return;
  185. const Expr *lhs = AssignEx->getLHS();
  186. const Expr *E = lhs->IgnoreParenImpCasts();
  187. auto pred = [assignKnown, numeratorKnown,
  188. denomExtVal](const MallocOverflowCheck &Check) {
  189. return assignKnown ||
  190. (numeratorKnown && (denomExtVal >= Check.maxVal.getExtValue()));
  191. };
  192. if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
  193. Erase<DeclRefExpr>(DR, pred);
  194. else if (const auto *ME = dyn_cast<MemberExpr>(E))
  195. Erase<MemberExpr>(ME, pred);
  196. }
  197. public:
  198. void VisitBinaryOperator(BinaryOperator *E) {
  199. if (E->isComparisonOp()) {
  200. const Expr * lhs = E->getLHS();
  201. const Expr * rhs = E->getRHS();
  202. // Ignore comparisons against zero, since they generally don't
  203. // protect against an overflow.
  204. if (!isIntZeroExpr(lhs) && !isIntZeroExpr(rhs)) {
  205. CheckExpr(lhs);
  206. CheckExpr(rhs);
  207. }
  208. }
  209. if (E->isAssignmentOp())
  210. CheckAssignmentExpr(E);
  211. EvaluatedExprVisitor<CheckOverflowOps>::VisitBinaryOperator(E);
  212. }
  213. /* We specifically ignore loop conditions, because they're typically
  214. not error checks. */
  215. void VisitWhileStmt(WhileStmt *S) {
  216. return this->Visit(S->getBody());
  217. }
  218. void VisitForStmt(ForStmt *S) {
  219. return this->Visit(S->getBody());
  220. }
  221. void VisitDoStmt(DoStmt *S) {
  222. return this->Visit(S->getBody());
  223. }
  224. CheckOverflowOps(theVecType &v, ASTContext &ctx)
  225. : EvaluatedExprVisitor<CheckOverflowOps>(ctx),
  226. toScanFor(v), Context(ctx)
  227. { }
  228. };
  229. }
  230. // OutputPossibleOverflows - We've found a possible overflow earlier,
  231. // now check whether Body might contain a comparison which might be
  232. // preventing the overflow.
  233. // This doesn't do flow analysis, range analysis, or points-to analysis; it's
  234. // just a dumb "is there a comparison" scan. The aim here is to
  235. // detect the most blatent cases of overflow and educate the
  236. // programmer.
  237. void MallocOverflowSecurityChecker::OutputPossibleOverflows(
  238. SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
  239. const Decl *D, BugReporter &BR, AnalysisManager &mgr) const {
  240. // By far the most common case: nothing to check.
  241. if (PossibleMallocOverflows.empty())
  242. return;
  243. // Delete any possible overflows which have a comparison.
  244. CheckOverflowOps c(PossibleMallocOverflows, BR.getContext());
  245. c.Visit(mgr.getAnalysisDeclContext(D)->getBody());
  246. // Output warnings for all overflows that are left.
  247. for (CheckOverflowOps::theVecType::iterator
  248. i = PossibleMallocOverflows.begin(),
  249. e = PossibleMallocOverflows.end();
  250. i != e;
  251. ++i) {
  252. BR.EmitBasicReport(
  253. D, this, "malloc() size overflow", categories::UnixAPI,
  254. "the computation of the size of the memory allocation may overflow",
  255. PathDiagnosticLocation::createOperatorLoc(i->mulop,
  256. BR.getSourceManager()),
  257. i->mulop->getSourceRange());
  258. }
  259. }
  260. void MallocOverflowSecurityChecker::checkASTCodeBody(const Decl *D,
  261. AnalysisManager &mgr,
  262. BugReporter &BR) const {
  263. CFG *cfg = mgr.getCFG(D);
  264. if (!cfg)
  265. return;
  266. // A list of variables referenced in possibly overflowing malloc operands.
  267. SmallVector<MallocOverflowCheck, 2> PossibleMallocOverflows;
  268. for (CFG::iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
  269. CFGBlock *block = *it;
  270. for (CFGBlock::iterator bi = block->begin(), be = block->end();
  271. bi != be; ++bi) {
  272. if (Optional<CFGStmt> CS = bi->getAs<CFGStmt>()) {
  273. if (const CallExpr *TheCall = dyn_cast<CallExpr>(CS->getStmt())) {
  274. // Get the callee.
  275. const FunctionDecl *FD = TheCall->getDirectCallee();
  276. if (!FD)
  277. continue;
  278. // Get the name of the callee. If it's a builtin, strip off the prefix.
  279. IdentifierInfo *FnInfo = FD->getIdentifier();
  280. if (!FnInfo)
  281. continue;
  282. if (FnInfo->isStr ("malloc") || FnInfo->isStr ("_MALLOC")) {
  283. if (TheCall->getNumArgs() == 1)
  284. CheckMallocArgument(PossibleMallocOverflows, TheCall,
  285. mgr.getASTContext());
  286. }
  287. }
  288. }
  289. }
  290. }
  291. OutputPossibleOverflows(PossibleMallocOverflows, D, BR, mgr);
  292. }
  293. void ento::registerMallocOverflowSecurityChecker(CheckerManager &mgr) {
  294. mgr.registerChecker<MallocOverflowSecurityChecker>();
  295. }
  296. bool ento::shouldRegisterMallocOverflowSecurityChecker(const CheckerManager &mgr) {
  297. return true;
  298. }