blob: fc2ab1d6e3f7d3f6ded20722a889366d5a64f4a9 [file] [log] [blame]
Ted Kremenek1c2fb272011-08-03 20:17:43 +00001// MallocOverflowSecurityChecker.cpp - Check for malloc overflows -*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This checker detects a common memory allocation security flaw.
11// Suppose 'unsigned int n' comes from an untrusted source. If the
12// code looks like 'malloc (n * 4)', and an attacker can make 'n' be
13// say MAX_UINT/4+2, then instead of allocating the correct 'n' 4-byte
14// elements, this will actually allocate only two because of overflow.
15// Then when the rest of the program attempts to store values past the
16// second element, these values will actually overwrite other items in
17// the heap, probably allowing the attacker to execute arbitrary code.
18//
19//===----------------------------------------------------------------------===//
20
21#include "ClangSACheckers.h"
22#include "clang/AST/EvaluatedExprVisitor.h"
Ted Kremenek1c2fb272011-08-03 20:17:43 +000023#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/StaticAnalyzer/Core/Checker.h"
25#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Devin Coughlin683dfd32015-09-23 23:27:55 +000026#include "llvm/ADT/APSInt.h"
Ted Kremenek1c2fb272011-08-03 20:17:43 +000027#include "llvm/ADT/SmallVector.h"
Benjamin Kramercfeacf52016-05-27 14:27:13 +000028#include <utility>
Ted Kremenek1c2fb272011-08-03 20:17:43 +000029
30using namespace clang;
31using namespace ento;
Devin Coughlin683dfd32015-09-23 23:27:55 +000032using llvm::APSInt;
Ted Kremenek1c2fb272011-08-03 20:17:43 +000033
34namespace {
35struct MallocOverflowCheck {
36 const BinaryOperator *mulop;
37 const Expr *variable;
Devin Coughlin683dfd32015-09-23 23:27:55 +000038 APSInt maxVal;
Ted Kremenek1c2fb272011-08-03 20:17:43 +000039
Devin Coughlin683dfd32015-09-23 23:27:55 +000040 MallocOverflowCheck(const BinaryOperator *m, const Expr *v, APSInt val)
Benjamin Kramercfeacf52016-05-27 14:27:13 +000041 : mulop(m), variable(v), maxVal(std::move(val)) {}
Ted Kremenek1c2fb272011-08-03 20:17:43 +000042};
43
44class MallocOverflowSecurityChecker : public Checker<check::ASTCodeBody> {
45public:
46 void checkASTCodeBody(const Decl *D, AnalysisManager &mgr,
47 BugReporter &BR) const;
48
49 void CheckMallocArgument(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000050 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
Ted Kremenek1c2fb272011-08-03 20:17:43 +000051 const Expr *TheArgument, ASTContext &Context) const;
52
53 void OutputPossibleOverflows(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000054 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
Ted Kremenek1c2fb272011-08-03 20:17:43 +000055 const Decl *D, BugReporter &BR, AnalysisManager &mgr) const;
56
57};
58} // end anonymous namespace
59
Devin Coughlin683dfd32015-09-23 23:27:55 +000060// Return true for computations which evaluate to zero: e.g., mult by 0.
61static inline bool EvaluatesToZero(APSInt &Val, BinaryOperatorKind op) {
62 return (op == BO_Mul) && (Val == 0);
63}
64
Ted Kremenek1c2fb272011-08-03 20:17:43 +000065void MallocOverflowSecurityChecker::CheckMallocArgument(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000066 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
Ted Kremenek1c2fb272011-08-03 20:17:43 +000067 const Expr *TheArgument,
68 ASTContext &Context) const {
69
70 /* Look for a linear combination with a single variable, and at least
71 one multiplication.
72 Reject anything that applies to the variable: an explicit cast,
73 conditional expression, an operation that could reduce the range
74 of the result, or anything too complicated :-). */
Devin Coughlin683dfd32015-09-23 23:27:55 +000075 const Expr *e = TheArgument;
Craig Topper0dbb7832014-05-27 02:45:47 +000076 const BinaryOperator * mulop = nullptr;
Devin Coughlin683dfd32015-09-23 23:27:55 +000077 APSInt maxVal;
Ted Kremenek1c2fb272011-08-03 20:17:43 +000078
79 for (;;) {
Devin Coughlin683dfd32015-09-23 23:27:55 +000080 maxVal = 0;
Ted Kremenek1c2fb272011-08-03 20:17:43 +000081 e = e->IgnoreParenImpCasts();
Devin Coughlin683dfd32015-09-23 23:27:55 +000082 if (const BinaryOperator *binop = dyn_cast<BinaryOperator>(e)) {
Ted Kremenek1c2fb272011-08-03 20:17:43 +000083 BinaryOperatorKind opc = binop->getOpcode();
84 // TODO: ignore multiplications by 1, reject if multiplied by 0.
Craig Topper0dbb7832014-05-27 02:45:47 +000085 if (mulop == nullptr && opc == BO_Mul)
Ted Kremenek1c2fb272011-08-03 20:17:43 +000086 mulop = binop;
87 if (opc != BO_Mul && opc != BO_Add && opc != BO_Sub && opc != BO_Shl)
88 return;
89
90 const Expr *lhs = binop->getLHS();
91 const Expr *rhs = binop->getRHS();
Devin Coughlin683dfd32015-09-23 23:27:55 +000092 if (rhs->isEvaluatable(Context)) {
Ted Kremenek1c2fb272011-08-03 20:17:43 +000093 e = lhs;
Devin Coughlin683dfd32015-09-23 23:27:55 +000094 maxVal = rhs->EvaluateKnownConstInt(Context);
95 if (EvaluatesToZero(maxVal, opc))
96 return;
97 } else if ((opc == BO_Add || opc == BO_Mul) &&
98 lhs->isEvaluatable(Context)) {
99 maxVal = lhs->EvaluateKnownConstInt(Context);
100 if (EvaluatesToZero(maxVal, opc))
101 return;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000102 e = rhs;
Devin Coughlin683dfd32015-09-23 23:27:55 +0000103 } else
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000104 return;
105 }
106 else if (isa<DeclRefExpr>(e) || isa<MemberExpr>(e))
107 break;
108 else
109 return;
110 }
111
Craig Topper0dbb7832014-05-27 02:45:47 +0000112 if (mulop == nullptr)
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000113 return;
114
115 // We've found the right structure of malloc argument, now save
116 // the data so when the body of the function is completely available
117 // we can check for comparisons.
118
119 // TODO: Could push this into the innermost scope where 'e' is
120 // defined, rather than the whole function.
Devin Coughlin683dfd32015-09-23 23:27:55 +0000121 PossibleMallocOverflows.push_back(MallocOverflowCheck(mulop, e, maxVal));
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000122}
123
124namespace {
125// A worker class for OutputPossibleOverflows.
126class CheckOverflowOps :
127 public EvaluatedExprVisitor<CheckOverflowOps> {
128public:
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000129 typedef SmallVectorImpl<MallocOverflowCheck> theVecType;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000130
131private:
132 theVecType &toScanFor;
133 ASTContext &Context;
134
135 bool isIntZeroExpr(const Expr *E) const {
Richard Smithcaf33902011-10-10 18:28:20 +0000136 if (!E->getType()->isIntegralOrEnumerationType())
137 return false;
138 llvm::APSInt Result;
139 if (E->EvaluateAsInt(Result, Context))
140 return Result == 0;
141 return false;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000142 }
143
Benjamin Kramer6ec90ec2016-07-09 12:16:58 +0000144 static const Decl *getDecl(const DeclRefExpr *DR) { return DR->getDecl(); }
145 static const Decl *getDecl(const MemberExpr *ME) {
146 return ME->getMemberDecl();
147 }
Devin Coughlin683dfd32015-09-23 23:27:55 +0000148
149 template <typename T1>
Benjamin Kramer951a6282016-07-09 11:16:56 +0000150 void Erase(const T1 *DR,
Benjamin Kramer6ec90ec2016-07-09 12:16:58 +0000151 llvm::function_ref<bool(const MallocOverflowCheck &)> Pred) {
152 auto P = [DR, Pred](const MallocOverflowCheck &Check) {
Benjamin Kramer951a6282016-07-09 11:16:56 +0000153 if (const auto *CheckDR = dyn_cast<T1>(Check.variable))
154 return getDecl(CheckDR) == getDecl(DR) && Pred(Check);
155 return false;
156 };
157 toScanFor.erase(std::remove_if(toScanFor.begin(), toScanFor.end(), P),
158 toScanFor.end());
Devin Coughlin683dfd32015-09-23 23:27:55 +0000159 }
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000160
Devin Coughlin683dfd32015-09-23 23:27:55 +0000161 void CheckExpr(const Expr *E_p) {
Benjamin Kramer6ec90ec2016-07-09 12:16:58 +0000162 auto PredTrue = [](const MallocOverflowCheck &) { return true; };
Devin Coughlin683dfd32015-09-23 23:27:55 +0000163 const Expr *E = E_p->IgnoreParenImpCasts();
164 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
Benjamin Kramer6ec90ec2016-07-09 12:16:58 +0000165 Erase<DeclRefExpr>(DR, PredTrue);
Benjamin Kramera008d3a2015-04-10 11:37:55 +0000166 else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
Benjamin Kramer6ec90ec2016-07-09 12:16:58 +0000167 Erase<MemberExpr>(ME, PredTrue);
Devin Coughlin683dfd32015-09-23 23:27:55 +0000168 }
169 }
170
171 // Check if the argument to malloc is assigned a value
172 // which cannot cause an overflow.
173 // e.g., malloc (mul * x) and,
174 // case 1: mul = <constant value>
175 // case 2: mul = a/b, where b > x
176 void CheckAssignmentExpr(BinaryOperator *AssignEx) {
177 bool assignKnown = false;
178 bool numeratorKnown = false, denomKnown = false;
179 APSInt denomVal;
180 denomVal = 0;
181
182 // Erase if the multiplicand was assigned a constant value.
183 const Expr *rhs = AssignEx->getRHS();
184 if (rhs->isEvaluatable(Context))
185 assignKnown = true;
186
187 // Discard the report if the multiplicand was assigned a value,
188 // that can never overflow after multiplication. e.g., the assignment
189 // is a division operator and the denominator is > other multiplicand.
190 const Expr *rhse = rhs->IgnoreParenImpCasts();
191 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(rhse)) {
192 if (BOp->getOpcode() == BO_Div) {
193 const Expr *denom = BOp->getRHS()->IgnoreParenImpCasts();
194 if (denom->EvaluateAsInt(denomVal, Context))
195 denomKnown = true;
196 const Expr *numerator = BOp->getLHS()->IgnoreParenImpCasts();
197 if (numerator->isEvaluatable(Context))
198 numeratorKnown = true;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000199 }
200 }
Devin Coughlin683dfd32015-09-23 23:27:55 +0000201 if (!assignKnown && !denomKnown)
202 return;
203 auto denomExtVal = denomVal.getExtValue();
204
205 // Ignore negative denominator.
206 if (denomExtVal < 0)
207 return;
208
209 const Expr *lhs = AssignEx->getLHS();
210 const Expr *E = lhs->IgnoreParenImpCasts();
211
212 auto pred = [assignKnown, numeratorKnown,
Benjamin Kramer951a6282016-07-09 11:16:56 +0000213 denomExtVal](const MallocOverflowCheck &Check) {
Devin Coughlin683dfd32015-09-23 23:27:55 +0000214 return assignKnown ||
Benjamin Kramer951a6282016-07-09 11:16:56 +0000215 (numeratorKnown && (denomExtVal >= Check.maxVal.getExtValue()));
Devin Coughlin683dfd32015-09-23 23:27:55 +0000216 };
217
218 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
219 Erase<DeclRefExpr>(DR, pred);
220 else if (const auto *ME = dyn_cast<MemberExpr>(E))
221 Erase<MemberExpr>(ME, pred);
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000222 }
223
224 public:
225 void VisitBinaryOperator(BinaryOperator *E) {
226 if (E->isComparisonOp()) {
227 const Expr * lhs = E->getLHS();
228 const Expr * rhs = E->getRHS();
229 // Ignore comparisons against zero, since they generally don't
230 // protect against an overflow.
Devin Coughlin683dfd32015-09-23 23:27:55 +0000231 if (!isIntZeroExpr(lhs) && !isIntZeroExpr(rhs)) {
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000232 CheckExpr(lhs);
233 CheckExpr(rhs);
234 }
235 }
Devin Coughlin683dfd32015-09-23 23:27:55 +0000236 if (E->isAssignmentOp())
237 CheckAssignmentExpr(E);
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000238 EvaluatedExprVisitor<CheckOverflowOps>::VisitBinaryOperator(E);
239 }
240
241 /* We specifically ignore loop conditions, because they're typically
242 not error checks. */
243 void VisitWhileStmt(WhileStmt *S) {
244 return this->Visit(S->getBody());
245 }
246 void VisitForStmt(ForStmt *S) {
247 return this->Visit(S->getBody());
248 }
249 void VisitDoStmt(DoStmt *S) {
250 return this->Visit(S->getBody());
251 }
252
253 CheckOverflowOps(theVecType &v, ASTContext &ctx)
254 : EvaluatedExprVisitor<CheckOverflowOps>(ctx),
255 toScanFor(v), Context(ctx)
256 { }
257 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000258}
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000259
260// OutputPossibleOverflows - We've found a possible overflow earlier,
261// now check whether Body might contain a comparison which might be
262// preventing the overflow.
263// This doesn't do flow analysis, range analysis, or points-to analysis; it's
264// just a dumb "is there a comparison" scan. The aim here is to
265// detect the most blatent cases of overflow and educate the
266// programmer.
267void MallocOverflowSecurityChecker::OutputPossibleOverflows(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000268 SmallVectorImpl<MallocOverflowCheck> &PossibleMallocOverflows,
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000269 const Decl *D, BugReporter &BR, AnalysisManager &mgr) const {
270 // By far the most common case: nothing to check.
271 if (PossibleMallocOverflows.empty())
272 return;
273
274 // Delete any possible overflows which have a comparison.
275 CheckOverflowOps c(PossibleMallocOverflows, BR.getContext());
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000276 c.Visit(mgr.getAnalysisDeclContext(D)->getBody());
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000277
278 // Output warnings for all overflows that are left.
279 for (CheckOverflowOps::theVecType::iterator
280 i = PossibleMallocOverflows.begin(),
281 e = PossibleMallocOverflows.end();
282 i != e;
283 ++i) {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000284 BR.EmitBasicReport(
285 D, this, "malloc() size overflow", categories::UnixAPI,
286 "the computation of the size of the memory allocation may overflow",
287 PathDiagnosticLocation::createOperatorLoc(i->mulop,
288 BR.getSourceManager()),
289 i->mulop->getSourceRange());
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000290 }
291}
292
293void MallocOverflowSecurityChecker::checkASTCodeBody(const Decl *D,
294 AnalysisManager &mgr,
295 BugReporter &BR) const {
296
297 CFG *cfg = mgr.getCFG(D);
298 if (!cfg)
299 return;
300
301 // A list of variables referenced in possibly overflowing malloc operands.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000302 SmallVector<MallocOverflowCheck, 2> PossibleMallocOverflows;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000303
304 for (CFG::iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
305 CFGBlock *block = *it;
306 for (CFGBlock::iterator bi = block->begin(), be = block->end();
307 bi != be; ++bi) {
David Blaikie00be69a2013-02-23 00:29:34 +0000308 if (Optional<CFGStmt> CS = bi->getAs<CFGStmt>()) {
309 if (const CallExpr *TheCall = dyn_cast<CallExpr>(CS->getStmt())) {
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000310 // Get the callee.
311 const FunctionDecl *FD = TheCall->getDirectCallee();
312
313 if (!FD)
Devin Coughlin683dfd32015-09-23 23:27:55 +0000314 continue;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000315
316 // Get the name of the callee. If it's a builtin, strip off the prefix.
317 IdentifierInfo *FnInfo = FD->getIdentifier();
Anna Zaks0070c6d2011-09-27 22:25:01 +0000318 if (!FnInfo)
Devin Coughlin683dfd32015-09-23 23:27:55 +0000319 continue;
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000320
321 if (FnInfo->isStr ("malloc") || FnInfo->isStr ("_MALLOC")) {
322 if (TheCall->getNumArgs() == 1)
323 CheckMallocArgument(PossibleMallocOverflows, TheCall->getArg(0),
324 mgr.getASTContext());
325 }
326 }
327 }
328 }
329 }
330
331 OutputPossibleOverflows(PossibleMallocOverflows, D, BR, mgr);
332}
333
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000334void
335ento::registerMallocOverflowSecurityChecker(CheckerManager &mgr) {
Ted Kremenek1c2fb272011-08-03 20:17:43 +0000336 mgr.registerChecker<MallocOverflowSecurityChecker>();
337}