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