MallocOverflowSecurityChecker.cpp 12 KB

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