1f4a2713aSLionel Sambuc // MallocOverflowSecurityChecker.cpp - Check for malloc overflows -*- C++ -*-=//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This checker detects a common memory allocation security flaw.
11f4a2713aSLionel Sambuc // Suppose 'unsigned int n' comes from an untrusted source. If the
12f4a2713aSLionel Sambuc // code looks like 'malloc (n * 4)', and an attacker can make 'n' be
13f4a2713aSLionel Sambuc // say MAX_UINT/4+2, then instead of allocating the correct 'n' 4-byte
14f4a2713aSLionel Sambuc // elements, this will actually allocate only two because of overflow.
15f4a2713aSLionel Sambuc // Then when the rest of the program attempts to store values past the
16f4a2713aSLionel Sambuc // second element, these values will actually overwrite other items in
17f4a2713aSLionel Sambuc // the heap, probably allowing the attacker to execute arbitrary code.
18f4a2713aSLionel Sambuc //
19f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
20f4a2713aSLionel Sambuc
21f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
22f4a2713aSLionel Sambuc #include "clang/AST/EvaluatedExprVisitor.h"
23f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
24f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
25f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc using namespace ento;
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc namespace {
32f4a2713aSLionel Sambuc struct MallocOverflowCheck {
33f4a2713aSLionel Sambuc const BinaryOperator *mulop;
34f4a2713aSLionel Sambuc const Expr *variable;
35f4a2713aSLionel Sambuc
MallocOverflowCheck__anonc437099a0111::MallocOverflowCheck36f4a2713aSLionel Sambuc MallocOverflowCheck (const BinaryOperator *m, const Expr *v)
37f4a2713aSLionel Sambuc : mulop(m), variable (v)
38f4a2713aSLionel Sambuc {}
39f4a2713aSLionel Sambuc };
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc class MallocOverflowSecurityChecker : public Checker<check::ASTCodeBody> {
42f4a2713aSLionel Sambuc public:
43f4a2713aSLionel Sambuc void checkASTCodeBody(const Decl *D, AnalysisManager &mgr,
44f4a2713aSLionel Sambuc BugReporter &BR) const;
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc void CheckMallocArgument(
47f4a2713aSLionel Sambuc SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
48f4a2713aSLionel Sambuc const Expr *TheArgument, ASTContext &Context) const;
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc void OutputPossibleOverflows(
51f4a2713aSLionel Sambuc SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
52f4a2713aSLionel Sambuc const Decl *D, BugReporter &BR, AnalysisManager &mgr) const;
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc };
55f4a2713aSLionel Sambuc } // end anonymous namespace
56f4a2713aSLionel Sambuc
CheckMallocArgument(SmallVectorImpl<MallocOverflowCheck> & PossibleMallocOverflows,const Expr * TheArgument,ASTContext & Context) const57f4a2713aSLionel Sambuc void MallocOverflowSecurityChecker::CheckMallocArgument(
58f4a2713aSLionel Sambuc SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
59f4a2713aSLionel Sambuc const Expr *TheArgument,
60f4a2713aSLionel Sambuc ASTContext &Context) const {
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc /* Look for a linear combination with a single variable, and at least
63f4a2713aSLionel Sambuc one multiplication.
64f4a2713aSLionel Sambuc Reject anything that applies to the variable: an explicit cast,
65f4a2713aSLionel Sambuc conditional expression, an operation that could reduce the range
66f4a2713aSLionel Sambuc of the result, or anything too complicated :-). */
67f4a2713aSLionel Sambuc const Expr * e = TheArgument;
68*0a6a1f1dSLionel Sambuc const BinaryOperator * mulop = nullptr;
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc for (;;) {
71f4a2713aSLionel Sambuc e = e->IgnoreParenImpCasts();
72f4a2713aSLionel Sambuc if (isa<BinaryOperator>(e)) {
73f4a2713aSLionel Sambuc const BinaryOperator * binop = dyn_cast<BinaryOperator>(e);
74f4a2713aSLionel Sambuc BinaryOperatorKind opc = binop->getOpcode();
75f4a2713aSLionel Sambuc // TODO: ignore multiplications by 1, reject if multiplied by 0.
76*0a6a1f1dSLionel Sambuc if (mulop == nullptr && opc == BO_Mul)
77f4a2713aSLionel Sambuc mulop = binop;
78f4a2713aSLionel Sambuc if (opc != BO_Mul && opc != BO_Add && opc != BO_Sub && opc != BO_Shl)
79f4a2713aSLionel Sambuc return;
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc const Expr *lhs = binop->getLHS();
82f4a2713aSLionel Sambuc const Expr *rhs = binop->getRHS();
83f4a2713aSLionel Sambuc if (rhs->isEvaluatable(Context))
84f4a2713aSLionel Sambuc e = lhs;
85f4a2713aSLionel Sambuc else if ((opc == BO_Add || opc == BO_Mul)
86f4a2713aSLionel Sambuc && lhs->isEvaluatable(Context))
87f4a2713aSLionel Sambuc e = rhs;
88f4a2713aSLionel Sambuc else
89f4a2713aSLionel Sambuc return;
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc else if (isa<DeclRefExpr>(e) || isa<MemberExpr>(e))
92f4a2713aSLionel Sambuc break;
93f4a2713aSLionel Sambuc else
94f4a2713aSLionel Sambuc return;
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc
97*0a6a1f1dSLionel Sambuc if (mulop == nullptr)
98f4a2713aSLionel Sambuc return;
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc // We've found the right structure of malloc argument, now save
101f4a2713aSLionel Sambuc // the data so when the body of the function is completely available
102f4a2713aSLionel Sambuc // we can check for comparisons.
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc // TODO: Could push this into the innermost scope where 'e' is
105f4a2713aSLionel Sambuc // defined, rather than the whole function.
106f4a2713aSLionel Sambuc PossibleMallocOverflows.push_back(MallocOverflowCheck(mulop, e));
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc namespace {
110f4a2713aSLionel Sambuc // A worker class for OutputPossibleOverflows.
111f4a2713aSLionel Sambuc class CheckOverflowOps :
112f4a2713aSLionel Sambuc public EvaluatedExprVisitor<CheckOverflowOps> {
113f4a2713aSLionel Sambuc public:
114f4a2713aSLionel Sambuc typedef SmallVectorImpl<MallocOverflowCheck> theVecType;
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc private:
117f4a2713aSLionel Sambuc theVecType &toScanFor;
118f4a2713aSLionel Sambuc ASTContext &Context;
119f4a2713aSLionel Sambuc
isIntZeroExpr(const Expr * E) const120f4a2713aSLionel Sambuc bool isIntZeroExpr(const Expr *E) const {
121f4a2713aSLionel Sambuc if (!E->getType()->isIntegralOrEnumerationType())
122f4a2713aSLionel Sambuc return false;
123f4a2713aSLionel Sambuc llvm::APSInt Result;
124f4a2713aSLionel Sambuc if (E->EvaluateAsInt(Result, Context))
125f4a2713aSLionel Sambuc return Result == 0;
126f4a2713aSLionel Sambuc return false;
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
CheckExpr(const Expr * E_p)129f4a2713aSLionel Sambuc void CheckExpr(const Expr *E_p) {
130f4a2713aSLionel Sambuc const Expr *E = E_p->IgnoreParenImpCasts();
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc theVecType::iterator i = toScanFor.end();
133f4a2713aSLionel Sambuc theVecType::iterator e = toScanFor.begin();
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) {
136f4a2713aSLionel Sambuc const Decl * EdreD = DR->getDecl();
137f4a2713aSLionel Sambuc while (i != e) {
138f4a2713aSLionel Sambuc --i;
139f4a2713aSLionel Sambuc if (const DeclRefExpr *DR_i = dyn_cast<DeclRefExpr>(i->variable)) {
140f4a2713aSLionel Sambuc if (DR_i->getDecl() == EdreD)
141f4a2713aSLionel Sambuc i = toScanFor.erase(i);
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc }
145f4a2713aSLionel Sambuc else if (isa<MemberExpr>(E)) {
146f4a2713aSLionel Sambuc // No points-to analysis, just look at the member
147f4a2713aSLionel Sambuc const Decl * EmeMD = dyn_cast<MemberExpr>(E)->getMemberDecl();
148f4a2713aSLionel Sambuc while (i != e) {
149f4a2713aSLionel Sambuc --i;
150f4a2713aSLionel Sambuc if (isa<MemberExpr>(i->variable)) {
151f4a2713aSLionel Sambuc if (dyn_cast<MemberExpr>(i->variable)->getMemberDecl() == EmeMD)
152f4a2713aSLionel Sambuc i = toScanFor.erase (i);
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc public:
VisitBinaryOperator(BinaryOperator * E)159f4a2713aSLionel Sambuc void VisitBinaryOperator(BinaryOperator *E) {
160f4a2713aSLionel Sambuc if (E->isComparisonOp()) {
161f4a2713aSLionel Sambuc const Expr * lhs = E->getLHS();
162f4a2713aSLionel Sambuc const Expr * rhs = E->getRHS();
163f4a2713aSLionel Sambuc // Ignore comparisons against zero, since they generally don't
164f4a2713aSLionel Sambuc // protect against an overflow.
165f4a2713aSLionel Sambuc if (!isIntZeroExpr(lhs) && ! isIntZeroExpr(rhs)) {
166f4a2713aSLionel Sambuc CheckExpr(lhs);
167f4a2713aSLionel Sambuc CheckExpr(rhs);
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc }
170f4a2713aSLionel Sambuc EvaluatedExprVisitor<CheckOverflowOps>::VisitBinaryOperator(E);
171f4a2713aSLionel Sambuc }
172f4a2713aSLionel Sambuc
173f4a2713aSLionel Sambuc /* We specifically ignore loop conditions, because they're typically
174f4a2713aSLionel Sambuc not error checks. */
VisitWhileStmt(WhileStmt * S)175f4a2713aSLionel Sambuc void VisitWhileStmt(WhileStmt *S) {
176f4a2713aSLionel Sambuc return this->Visit(S->getBody());
177f4a2713aSLionel Sambuc }
VisitForStmt(ForStmt * S)178f4a2713aSLionel Sambuc void VisitForStmt(ForStmt *S) {
179f4a2713aSLionel Sambuc return this->Visit(S->getBody());
180f4a2713aSLionel Sambuc }
VisitDoStmt(DoStmt * S)181f4a2713aSLionel Sambuc void VisitDoStmt(DoStmt *S) {
182f4a2713aSLionel Sambuc return this->Visit(S->getBody());
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc
CheckOverflowOps(theVecType & v,ASTContext & ctx)185f4a2713aSLionel Sambuc CheckOverflowOps(theVecType &v, ASTContext &ctx)
186f4a2713aSLionel Sambuc : EvaluatedExprVisitor<CheckOverflowOps>(ctx),
187f4a2713aSLionel Sambuc toScanFor(v), Context(ctx)
188f4a2713aSLionel Sambuc { }
189f4a2713aSLionel Sambuc };
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc // OutputPossibleOverflows - We've found a possible overflow earlier,
193f4a2713aSLionel Sambuc // now check whether Body might contain a comparison which might be
194f4a2713aSLionel Sambuc // preventing the overflow.
195f4a2713aSLionel Sambuc // This doesn't do flow analysis, range analysis, or points-to analysis; it's
196f4a2713aSLionel Sambuc // just a dumb "is there a comparison" scan. The aim here is to
197f4a2713aSLionel Sambuc // detect the most blatent cases of overflow and educate the
198f4a2713aSLionel Sambuc // programmer.
OutputPossibleOverflows(SmallVectorImpl<MallocOverflowCheck> & PossibleMallocOverflows,const Decl * D,BugReporter & BR,AnalysisManager & mgr) const199f4a2713aSLionel Sambuc void MallocOverflowSecurityChecker::OutputPossibleOverflows(
200f4a2713aSLionel Sambuc SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
201f4a2713aSLionel Sambuc const Decl *D, BugReporter &BR, AnalysisManager &mgr) const {
202f4a2713aSLionel Sambuc // By far the most common case: nothing to check.
203f4a2713aSLionel Sambuc if (PossibleMallocOverflows.empty())
204f4a2713aSLionel Sambuc return;
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc // Delete any possible overflows which have a comparison.
207f4a2713aSLionel Sambuc CheckOverflowOps c(PossibleMallocOverflows, BR.getContext());
208f4a2713aSLionel Sambuc c.Visit(mgr.getAnalysisDeclContext(D)->getBody());
209f4a2713aSLionel Sambuc
210f4a2713aSLionel Sambuc // Output warnings for all overflows that are left.
211f4a2713aSLionel Sambuc for (CheckOverflowOps::theVecType::iterator
212f4a2713aSLionel Sambuc i = PossibleMallocOverflows.begin(),
213f4a2713aSLionel Sambuc e = PossibleMallocOverflows.end();
214f4a2713aSLionel Sambuc i != e;
215f4a2713aSLionel Sambuc ++i) {
216*0a6a1f1dSLionel Sambuc BR.EmitBasicReport(
217*0a6a1f1dSLionel Sambuc D, this, "malloc() size overflow", categories::UnixAPI,
218f4a2713aSLionel Sambuc "the computation of the size of the memory allocation may overflow",
219f4a2713aSLionel Sambuc PathDiagnosticLocation::createOperatorLoc(i->mulop,
220f4a2713aSLionel Sambuc BR.getSourceManager()),
221f4a2713aSLionel Sambuc i->mulop->getSourceRange());
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc }
224f4a2713aSLionel Sambuc
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const225f4a2713aSLionel Sambuc void MallocOverflowSecurityChecker::checkASTCodeBody(const Decl *D,
226f4a2713aSLionel Sambuc AnalysisManager &mgr,
227f4a2713aSLionel Sambuc BugReporter &BR) const {
228f4a2713aSLionel Sambuc
229f4a2713aSLionel Sambuc CFG *cfg = mgr.getCFG(D);
230f4a2713aSLionel Sambuc if (!cfg)
231f4a2713aSLionel Sambuc return;
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc // A list of variables referenced in possibly overflowing malloc operands.
234f4a2713aSLionel Sambuc SmallVector<MallocOverflowCheck, 2> PossibleMallocOverflows;
235f4a2713aSLionel Sambuc
236f4a2713aSLionel Sambuc for (CFG::iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
237f4a2713aSLionel Sambuc CFGBlock *block = *it;
238f4a2713aSLionel Sambuc for (CFGBlock::iterator bi = block->begin(), be = block->end();
239f4a2713aSLionel Sambuc bi != be; ++bi) {
240f4a2713aSLionel Sambuc if (Optional<CFGStmt> CS = bi->getAs<CFGStmt>()) {
241f4a2713aSLionel Sambuc if (const CallExpr *TheCall = dyn_cast<CallExpr>(CS->getStmt())) {
242f4a2713aSLionel Sambuc // Get the callee.
243f4a2713aSLionel Sambuc const FunctionDecl *FD = TheCall->getDirectCallee();
244f4a2713aSLionel Sambuc
245f4a2713aSLionel Sambuc if (!FD)
246f4a2713aSLionel Sambuc return;
247f4a2713aSLionel Sambuc
248f4a2713aSLionel Sambuc // Get the name of the callee. If it's a builtin, strip off the prefix.
249f4a2713aSLionel Sambuc IdentifierInfo *FnInfo = FD->getIdentifier();
250f4a2713aSLionel Sambuc if (!FnInfo)
251f4a2713aSLionel Sambuc return;
252f4a2713aSLionel Sambuc
253f4a2713aSLionel Sambuc if (FnInfo->isStr ("malloc") || FnInfo->isStr ("_MALLOC")) {
254f4a2713aSLionel Sambuc if (TheCall->getNumArgs() == 1)
255f4a2713aSLionel Sambuc CheckMallocArgument(PossibleMallocOverflows, TheCall->getArg(0),
256f4a2713aSLionel Sambuc mgr.getASTContext());
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc }
260f4a2713aSLionel Sambuc }
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc
263f4a2713aSLionel Sambuc OutputPossibleOverflows(PossibleMallocOverflows, D, BR, mgr);
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc
266*0a6a1f1dSLionel Sambuc void
registerMallocOverflowSecurityChecker(CheckerManager & mgr)267*0a6a1f1dSLionel Sambuc ento::registerMallocOverflowSecurityChecker(CheckerManager &mgr) {
268f4a2713aSLionel Sambuc mgr.registerChecker<MallocOverflowSecurityChecker>();
269f4a2713aSLionel Sambuc }
270