2011-08-04 04:17:43 +08:00
|
|
|
// MallocOverflowSecurityChecker.cpp - Check for malloc overflows -*- C++ -*-=//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-08-04 04:17:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This checker detects a common memory allocation security flaw.
|
|
|
|
// Suppose 'unsigned int n' comes from an untrusted source. If the
|
|
|
|
// code looks like 'malloc (n * 4)', and an attacker can make 'n' be
|
|
|
|
// say MAX_UINT/4+2, then instead of allocating the correct 'n' 4-byte
|
|
|
|
// elements, this will actually allocate only two because of overflow.
|
|
|
|
// Then when the rest of the program attempts to store values past the
|
|
|
|
// second element, these values will actually overwrite other items in
|
|
|
|
// the heap, probably allowing the attacker to execute arbitrary code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2011-08-04 04:17:43 +08:00
|
|
|
#include "clang/AST/EvaluatedExprVisitor.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2015-09-24 07:27:55 +08:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2011-08-04 04:17:43 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
2015-09-24 07:27:55 +08:00
|
|
|
using llvm::APSInt;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MallocOverflowCheck {
|
|
|
|
const BinaryOperator *mulop;
|
|
|
|
const Expr *variable;
|
2015-09-24 07:27:55 +08:00
|
|
|
APSInt maxVal;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
2015-09-24 07:27:55 +08:00
|
|
|
MallocOverflowCheck(const BinaryOperator *m, const Expr *v, APSInt val)
|
2016-05-27 22:27:13 +08:00
|
|
|
: mulop(m), variable(v), maxVal(std::move(val)) {}
|
2011-08-04 04:17:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MallocOverflowSecurityChecker : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager &mgr,
|
|
|
|
BugReporter &BR) const;
|
|
|
|
|
|
|
|
void CheckMallocArgument(
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
|
2011-08-04 04:17:43 +08:00
|
|
|
const Expr *TheArgument, ASTContext &Context) const;
|
|
|
|
|
|
|
|
void OutputPossibleOverflows(
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
|
2011-08-04 04:17:43 +08:00
|
|
|
const Decl *D, BugReporter &BR, AnalysisManager &mgr) const;
|
|
|
|
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-09-24 07:27:55 +08:00
|
|
|
// Return true for computations which evaluate to zero: e.g., mult by 0.
|
|
|
|
static inline bool EvaluatesToZero(APSInt &Val, BinaryOperatorKind op) {
|
|
|
|
return (op == BO_Mul) && (Val == 0);
|
|
|
|
}
|
|
|
|
|
2011-08-04 04:17:43 +08:00
|
|
|
void MallocOverflowSecurityChecker::CheckMallocArgument(
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
|
2011-08-04 04:17:43 +08:00
|
|
|
const Expr *TheArgument,
|
|
|
|
ASTContext &Context) const {
|
|
|
|
|
|
|
|
/* Look for a linear combination with a single variable, and at least
|
|
|
|
one multiplication.
|
|
|
|
Reject anything that applies to the variable: an explicit cast,
|
|
|
|
conditional expression, an operation that could reduce the range
|
|
|
|
of the result, or anything too complicated :-). */
|
2015-09-24 07:27:55 +08:00
|
|
|
const Expr *e = TheArgument;
|
2014-05-27 10:45:47 +08:00
|
|
|
const BinaryOperator * mulop = nullptr;
|
2015-09-24 07:27:55 +08:00
|
|
|
APSInt maxVal;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
for (;;) {
|
2015-09-24 07:27:55 +08:00
|
|
|
maxVal = 0;
|
2011-08-04 04:17:43 +08:00
|
|
|
e = e->IgnoreParenImpCasts();
|
2015-09-24 07:27:55 +08:00
|
|
|
if (const BinaryOperator *binop = dyn_cast<BinaryOperator>(e)) {
|
2011-08-04 04:17:43 +08:00
|
|
|
BinaryOperatorKind opc = binop->getOpcode();
|
|
|
|
// TODO: ignore multiplications by 1, reject if multiplied by 0.
|
2014-05-27 10:45:47 +08:00
|
|
|
if (mulop == nullptr && opc == BO_Mul)
|
2011-08-04 04:17:43 +08:00
|
|
|
mulop = binop;
|
|
|
|
if (opc != BO_Mul && opc != BO_Add && opc != BO_Sub && opc != BO_Shl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Expr *lhs = binop->getLHS();
|
|
|
|
const Expr *rhs = binop->getRHS();
|
2015-09-24 07:27:55 +08:00
|
|
|
if (rhs->isEvaluatable(Context)) {
|
2011-08-04 04:17:43 +08:00
|
|
|
e = lhs;
|
2015-09-24 07:27:55 +08:00
|
|
|
maxVal = rhs->EvaluateKnownConstInt(Context);
|
|
|
|
if (EvaluatesToZero(maxVal, opc))
|
|
|
|
return;
|
|
|
|
} else if ((opc == BO_Add || opc == BO_Mul) &&
|
|
|
|
lhs->isEvaluatable(Context)) {
|
|
|
|
maxVal = lhs->EvaluateKnownConstInt(Context);
|
|
|
|
if (EvaluatesToZero(maxVal, opc))
|
|
|
|
return;
|
2011-08-04 04:17:43 +08:00
|
|
|
e = rhs;
|
2015-09-24 07:27:55 +08:00
|
|
|
} else
|
2011-08-04 04:17:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (isa<DeclRefExpr>(e) || isa<MemberExpr>(e))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
if (mulop == nullptr)
|
2011-08-04 04:17:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We've found the right structure of malloc argument, now save
|
|
|
|
// the data so when the body of the function is completely available
|
|
|
|
// we can check for comparisons.
|
|
|
|
|
|
|
|
// TODO: Could push this into the innermost scope where 'e' is
|
|
|
|
// defined, rather than the whole function.
|
2015-09-24 07:27:55 +08:00
|
|
|
PossibleMallocOverflows.push_back(MallocOverflowCheck(mulop, e, maxVal));
|
2011-08-04 04:17:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// A worker class for OutputPossibleOverflows.
|
|
|
|
class CheckOverflowOps :
|
|
|
|
public EvaluatedExprVisitor<CheckOverflowOps> {
|
|
|
|
public:
|
2013-01-13 03:30:44 +08:00
|
|
|
typedef SmallVectorImpl<MallocOverflowCheck> theVecType;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
theVecType &toScanFor;
|
|
|
|
ASTContext &Context;
|
|
|
|
|
|
|
|
bool isIntZeroExpr(const Expr *E) const {
|
2011-10-11 02:28:20 +08:00
|
|
|
if (!E->getType()->isIntegralOrEnumerationType())
|
|
|
|
return false;
|
2018-12-01 07:41:18 +08:00
|
|
|
Expr::EvalResult Result;
|
2011-10-11 02:28:20 +08:00
|
|
|
if (E->EvaluateAsInt(Result, Context))
|
2018-12-01 07:41:18 +08:00
|
|
|
return Result.Val.getInt() == 0;
|
2011-10-11 02:28:20 +08:00
|
|
|
return false;
|
2011-08-04 04:17:43 +08:00
|
|
|
}
|
|
|
|
|
2016-07-09 20:16:58 +08:00
|
|
|
static const Decl *getDecl(const DeclRefExpr *DR) { return DR->getDecl(); }
|
|
|
|
static const Decl *getDecl(const MemberExpr *ME) {
|
|
|
|
return ME->getMemberDecl();
|
|
|
|
}
|
2011-08-04 04:17:43 +08:00
|
|
|
|
2015-09-24 07:27:55 +08:00
|
|
|
template <typename T1>
|
2016-07-09 19:16:56 +08:00
|
|
|
void Erase(const T1 *DR,
|
2016-07-09 20:16:58 +08:00
|
|
|
llvm::function_ref<bool(const MallocOverflowCheck &)> Pred) {
|
|
|
|
auto P = [DR, Pred](const MallocOverflowCheck &Check) {
|
2016-07-09 19:16:56 +08:00
|
|
|
if (const auto *CheckDR = dyn_cast<T1>(Check.variable))
|
|
|
|
return getDecl(CheckDR) == getDecl(DR) && Pred(Check);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
toScanFor.erase(std::remove_if(toScanFor.begin(), toScanFor.end(), P),
|
|
|
|
toScanFor.end());
|
2015-09-24 07:27:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckExpr(const Expr *E_p) {
|
2016-07-09 20:16:58 +08:00
|
|
|
auto PredTrue = [](const MallocOverflowCheck &) { return true; };
|
2015-09-24 07:27:55 +08:00
|
|
|
const Expr *E = E_p->IgnoreParenImpCasts();
|
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
|
2016-07-09 20:16:58 +08:00
|
|
|
Erase<DeclRefExpr>(DR, PredTrue);
|
2015-04-10 19:37:55 +08:00
|
|
|
else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
|
2016-07-09 20:16:58 +08:00
|
|
|
Erase<MemberExpr>(ME, PredTrue);
|
2015-09-24 07:27:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the argument to malloc is assigned a value
|
|
|
|
// which cannot cause an overflow.
|
|
|
|
// e.g., malloc (mul * x) and,
|
|
|
|
// case 1: mul = <constant value>
|
|
|
|
// case 2: mul = a/b, where b > x
|
|
|
|
void CheckAssignmentExpr(BinaryOperator *AssignEx) {
|
|
|
|
bool assignKnown = false;
|
|
|
|
bool numeratorKnown = false, denomKnown = false;
|
|
|
|
APSInt denomVal;
|
|
|
|
denomVal = 0;
|
|
|
|
|
|
|
|
// Erase if the multiplicand was assigned a constant value.
|
|
|
|
const Expr *rhs = AssignEx->getRHS();
|
|
|
|
if (rhs->isEvaluatable(Context))
|
|
|
|
assignKnown = true;
|
|
|
|
|
|
|
|
// Discard the report if the multiplicand was assigned a value,
|
|
|
|
// that can never overflow after multiplication. e.g., the assignment
|
|
|
|
// is a division operator and the denominator is > other multiplicand.
|
|
|
|
const Expr *rhse = rhs->IgnoreParenImpCasts();
|
|
|
|
if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(rhse)) {
|
|
|
|
if (BOp->getOpcode() == BO_Div) {
|
|
|
|
const Expr *denom = BOp->getRHS()->IgnoreParenImpCasts();
|
2018-12-01 07:41:18 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (denom->EvaluateAsInt(Result, Context)) {
|
|
|
|
denomVal = Result.Val.getInt();
|
2015-09-24 07:27:55 +08:00
|
|
|
denomKnown = true;
|
2018-12-01 07:41:18 +08:00
|
|
|
}
|
2015-09-24 07:27:55 +08:00
|
|
|
const Expr *numerator = BOp->getLHS()->IgnoreParenImpCasts();
|
|
|
|
if (numerator->isEvaluatable(Context))
|
|
|
|
numeratorKnown = true;
|
2011-08-04 04:17:43 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 07:27:55 +08:00
|
|
|
if (!assignKnown && !denomKnown)
|
|
|
|
return;
|
|
|
|
auto denomExtVal = denomVal.getExtValue();
|
|
|
|
|
|
|
|
// Ignore negative denominator.
|
|
|
|
if (denomExtVal < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Expr *lhs = AssignEx->getLHS();
|
|
|
|
const Expr *E = lhs->IgnoreParenImpCasts();
|
|
|
|
|
|
|
|
auto pred = [assignKnown, numeratorKnown,
|
2016-07-09 19:16:56 +08:00
|
|
|
denomExtVal](const MallocOverflowCheck &Check) {
|
2015-09-24 07:27:55 +08:00
|
|
|
return assignKnown ||
|
2016-07-09 19:16:56 +08:00
|
|
|
(numeratorKnown && (denomExtVal >= Check.maxVal.getExtValue()));
|
2015-09-24 07:27:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
|
|
|
|
Erase<DeclRefExpr>(DR, pred);
|
|
|
|
else if (const auto *ME = dyn_cast<MemberExpr>(E))
|
|
|
|
Erase<MemberExpr>(ME, pred);
|
2011-08-04 04:17:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void VisitBinaryOperator(BinaryOperator *E) {
|
|
|
|
if (E->isComparisonOp()) {
|
|
|
|
const Expr * lhs = E->getLHS();
|
|
|
|
const Expr * rhs = E->getRHS();
|
|
|
|
// Ignore comparisons against zero, since they generally don't
|
|
|
|
// protect against an overflow.
|
2015-09-24 07:27:55 +08:00
|
|
|
if (!isIntZeroExpr(lhs) && !isIntZeroExpr(rhs)) {
|
2011-08-04 04:17:43 +08:00
|
|
|
CheckExpr(lhs);
|
|
|
|
CheckExpr(rhs);
|
|
|
|
}
|
|
|
|
}
|
2015-09-24 07:27:55 +08:00
|
|
|
if (E->isAssignmentOp())
|
|
|
|
CheckAssignmentExpr(E);
|
2011-08-04 04:17:43 +08:00
|
|
|
EvaluatedExprVisitor<CheckOverflowOps>::VisitBinaryOperator(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We specifically ignore loop conditions, because they're typically
|
|
|
|
not error checks. */
|
|
|
|
void VisitWhileStmt(WhileStmt *S) {
|
|
|
|
return this->Visit(S->getBody());
|
|
|
|
}
|
|
|
|
void VisitForStmt(ForStmt *S) {
|
|
|
|
return this->Visit(S->getBody());
|
|
|
|
}
|
|
|
|
void VisitDoStmt(DoStmt *S) {
|
|
|
|
return this->Visit(S->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckOverflowOps(theVecType &v, ASTContext &ctx)
|
|
|
|
: EvaluatedExprVisitor<CheckOverflowOps>(ctx),
|
|
|
|
toScanFor(v), Context(ctx)
|
|
|
|
{ }
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
// OutputPossibleOverflows - We've found a possible overflow earlier,
|
|
|
|
// now check whether Body might contain a comparison which might be
|
|
|
|
// preventing the overflow.
|
|
|
|
// This doesn't do flow analysis, range analysis, or points-to analysis; it's
|
|
|
|
// just a dumb "is there a comparison" scan. The aim here is to
|
|
|
|
// detect the most blatent cases of overflow and educate the
|
|
|
|
// programmer.
|
|
|
|
void MallocOverflowSecurityChecker::OutputPossibleOverflows(
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
|
2011-08-04 04:17:43 +08:00
|
|
|
const Decl *D, BugReporter &BR, AnalysisManager &mgr) const {
|
|
|
|
// By far the most common case: nothing to check.
|
|
|
|
if (PossibleMallocOverflows.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Delete any possible overflows which have a comparison.
|
|
|
|
CheckOverflowOps c(PossibleMallocOverflows, BR.getContext());
|
2011-10-24 09:32:45 +08:00
|
|
|
c.Visit(mgr.getAnalysisDeclContext(D)->getBody());
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
// Output warnings for all overflows that are left.
|
|
|
|
for (CheckOverflowOps::theVecType::iterator
|
|
|
|
i = PossibleMallocOverflows.begin(),
|
|
|
|
e = PossibleMallocOverflows.end();
|
|
|
|
i != e;
|
|
|
|
++i) {
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(
|
|
|
|
D, this, "malloc() size overflow", categories::UnixAPI,
|
|
|
|
"the computation of the size of the memory allocation may overflow",
|
|
|
|
PathDiagnosticLocation::createOperatorLoc(i->mulop,
|
|
|
|
BR.getSourceManager()),
|
|
|
|
i->mulop->getSourceRange());
|
2011-08-04 04:17:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MallocOverflowSecurityChecker::checkASTCodeBody(const Decl *D,
|
|
|
|
AnalysisManager &mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
|
|
|
|
CFG *cfg = mgr.getCFG(D);
|
|
|
|
if (!cfg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// A list of variables referenced in possibly overflowing malloc operands.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<MallocOverflowCheck, 2> PossibleMallocOverflows;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
for (CFG::iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
|
|
|
|
CFGBlock *block = *it;
|
|
|
|
for (CFGBlock::iterator bi = block->begin(), be = block->end();
|
|
|
|
bi != be; ++bi) {
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> CS = bi->getAs<CFGStmt>()) {
|
|
|
|
if (const CallExpr *TheCall = dyn_cast<CallExpr>(CS->getStmt())) {
|
2011-08-04 04:17:43 +08:00
|
|
|
// Get the callee.
|
|
|
|
const FunctionDecl *FD = TheCall->getDirectCallee();
|
|
|
|
|
|
|
|
if (!FD)
|
2015-09-24 07:27:55 +08:00
|
|
|
continue;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
// Get the name of the callee. If it's a builtin, strip off the prefix.
|
|
|
|
IdentifierInfo *FnInfo = FD->getIdentifier();
|
2011-09-28 06:25:01 +08:00
|
|
|
if (!FnInfo)
|
2015-09-24 07:27:55 +08:00
|
|
|
continue;
|
2011-08-04 04:17:43 +08:00
|
|
|
|
|
|
|
if (FnInfo->isStr ("malloc") || FnInfo->isStr ("_MALLOC")) {
|
|
|
|
if (TheCall->getNumArgs() == 1)
|
|
|
|
CheckMallocArgument(PossibleMallocOverflows, TheCall->getArg(0),
|
|
|
|
mgr.getASTContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputPossibleOverflows(PossibleMallocOverflows, D, BR, mgr);
|
|
|
|
}
|
|
|
|
|
2019-01-26 22:23:08 +08:00
|
|
|
void ento::registerMallocOverflowSecurityChecker(CheckerManager &mgr) {
|
2011-08-04 04:17:43 +08:00
|
|
|
mgr.registerChecker<MallocOverflowSecurityChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterMallocOverflowSecurityChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|