blob: 8ba0bcbceeca4947f4974c9105761a88d2c1357b [file] [log] [blame]
Anna Zaks08be9b92011-08-04 17:28:06 +00001//==--- MacOSKeychainAPIChecker.cpp ------------------------------*- C++ -*-==//
Anna Zaks15f496c2011-08-01 22:40:01 +00002//
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// This checker flags misuses of KeyChainAPI. In particular, the password data
10// allocated/returned by SecKeychainItemCopyContent,
11// SecKeychainFindGenericPassword, SecKeychainFindInternetPassword functions has
12// to be freed using a call to SecKeychainItemFreeContent.
13//===----------------------------------------------------------------------===//
14
15#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Anna Zaks15f496c2011-08-01 22:40:01 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
18#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000023#include "llvm/Support/raw_ostream.h"
Anna Zaks15f496c2011-08-01 22:40:01 +000024
25using namespace clang;
26using namespace ento;
27
28namespace {
29class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>,
Anna Zaks15f496c2011-08-01 22:40:01 +000030 check::PostStmt<CallExpr>,
Anna Zaks14b1af52017-01-13 00:50:41 +000031 check::DeadSymbols,
32 eval::Assume> {
Ahmed Charlesb8984322014-03-07 20:03:18 +000033 mutable std::unique_ptr<BugType> BT;
Anna Zaks388c18e2011-08-04 00:26:57 +000034
Anna Zaks15f496c2011-08-01 22:40:01 +000035public:
Anna Zaksc94894f2011-08-12 21:14:26 +000036 /// AllocationState is a part of the checker specific state together with the
37 /// MemRegion corresponding to the allocated data.
38 struct AllocationState {
Anna Zaksc94894f2011-08-12 21:14:26 +000039 /// The index of the allocator function.
40 unsigned int AllocatorIdx;
Anna Zaks5be8a4d2011-08-25 00:59:06 +000041 SymbolRef Region;
Anna Zaksc94894f2011-08-12 21:14:26 +000042
43 AllocationState(const Expr *E, unsigned int Idx, SymbolRef R) :
Anna Zaksc94894f2011-08-12 21:14:26 +000044 AllocatorIdx(Idx),
Anna Zaks5be8a4d2011-08-25 00:59:06 +000045 Region(R) {}
Anna Zaksc94894f2011-08-12 21:14:26 +000046
47 bool operator==(const AllocationState &X) const {
Anna Zaks5be8a4d2011-08-25 00:59:06 +000048 return (AllocatorIdx == X.AllocatorIdx &&
49 Region == X.Region);
Anna Zaksc94894f2011-08-12 21:14:26 +000050 }
Anna Zaks5be8a4d2011-08-25 00:59:06 +000051
Anna Zaksc94894f2011-08-12 21:14:26 +000052 void Profile(llvm::FoldingSetNodeID &ID) const {
Anna Zaksc94894f2011-08-12 21:14:26 +000053 ID.AddInteger(AllocatorIdx);
Anna Zaks5be8a4d2011-08-25 00:59:06 +000054 ID.AddPointer(Region);
Anna Zaksc94894f2011-08-12 21:14:26 +000055 }
56 };
57
Anna Zaks15f496c2011-08-01 22:40:01 +000058 void checkPreStmt(const CallExpr *S, CheckerContext &C) const;
Anna Zaks15f496c2011-08-01 22:40:01 +000059 void checkPostStmt(const CallExpr *S, CheckerContext &C) const;
Anna Zaksfdd0aca2011-08-12 21:56:43 +000060 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
Anna Zaks14b1af52017-01-13 00:50:41 +000061 ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
62 bool Assumption) const;
63 void printState(raw_ostream &Out, ProgramStateRef State,
64 const char *NL, const char *Sep) const;
Anna Zaks15f496c2011-08-01 22:40:01 +000065
66private:
Anna Zaksf880cff2011-08-24 21:58:55 +000067 typedef std::pair<SymbolRef, const AllocationState*> AllocationPair;
Dmitri Gribenkof8579502013-01-12 19:30:44 +000068 typedef SmallVector<AllocationPair, 2> AllocationPairVec;
Anna Zaksbe460892011-08-24 20:52:46 +000069
70 enum APIKind {
Anna Zaks0897a232011-08-24 00:06:27 +000071 /// Denotes functions tracked by this checker.
72 ValidAPI = 0,
73 /// The functions commonly/mistakenly used in place of the given API.
74 ErrorAPI = 1,
75 /// The functions which may allocate the data. These are tracked to reduce
76 /// the false alarm rate.
77 PossibleAPI = 2
78 };
Anna Zaks08be9b92011-08-04 17:28:06 +000079 /// Stores the information about the allocator and deallocator functions -
80 /// these are the functions the checker is tracking.
81 struct ADFunctionInfo {
82 const char* Name;
83 unsigned int Param;
84 unsigned int DeallocatorIdx;
Anna Zaks0897a232011-08-24 00:06:27 +000085 APIKind Kind;
Anna Zaks08be9b92011-08-04 17:28:06 +000086 };
87 static const unsigned InvalidIdx = 100000;
Anna Zaks0897a232011-08-24 00:06:27 +000088 static const unsigned FunctionsToTrackSize = 8;
Anna Zaks08be9b92011-08-04 17:28:06 +000089 static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize];
Anna Zaks177ecfa2011-08-05 23:52:45 +000090 /// The value, which represents no error return value for allocator functions.
91 static const unsigned NoErr = 0;
Anna Zaks15f496c2011-08-01 22:40:01 +000092
Anna Zaks08be9b92011-08-04 17:28:06 +000093 /// Given the function name, returns the index of the allocator/deallocator
94 /// function.
Anna Zaksbe460892011-08-24 20:52:46 +000095 static unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator);
Anna Zaks388c18e2011-08-04 00:26:57 +000096
97 inline void initBugType() const {
98 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000099 BT.reset(new BugType(this, "Improper use of SecKeychain API",
Anna Zaks8ef07e52013-04-03 19:28:22 +0000100 "API Misuse (Apple)"));
Anna Zaks388c18e2011-08-04 00:26:57 +0000101 }
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000102
Anna Zaks85913db2011-08-25 00:32:42 +0000103 void generateDeallocatorMismatchReport(const AllocationPair &AP,
Anna Zaksbb167012011-08-23 23:47:36 +0000104 const Expr *ArgExpr,
Anna Zaks85913db2011-08-25 00:32:42 +0000105 CheckerContext &C) const;
Anna Zaksbb167012011-08-23 23:47:36 +0000106
Anna Zaks4b062cb2012-02-23 22:53:29 +0000107 /// Find the allocation site for Sym on the path leading to the node N.
Anna Zaksa043d0c2013-01-08 00:25:29 +0000108 const ExplodedNode *getAllocationNode(const ExplodedNode *N, SymbolRef Sym,
109 CheckerContext &C) const;
Anna Zaks4b062cb2012-02-23 22:53:29 +0000110
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000111 std::unique_ptr<BugReport> generateAllocatedDataNotReleasedReport(
112 const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const;
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000113
Ted Kremenek1e809b42012-03-09 01:13:14 +0000114 /// Mark an AllocationPair interesting for diagnostic reporting.
115 void markInteresting(BugReport *R, const AllocationPair &AP) const {
116 R->markInteresting(AP.first);
117 R->markInteresting(AP.second->Region);
118 }
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000119
Anna Zaksbe460892011-08-24 20:52:46 +0000120 /// The bug visitor which allows us to print extra diagnostics along the
121 /// BugReport path. For example, showing the allocation site of the leaked
122 /// region.
Jordy Rosef78877e2012-03-24 02:45:35 +0000123 class SecKeychainBugVisitor
124 : public BugReporterVisitorImpl<SecKeychainBugVisitor> {
Anna Zaksbe460892011-08-24 20:52:46 +0000125 protected:
126 // The allocated region symbol tracked by the main analysis.
127 SymbolRef Sym;
128
129 public:
130 SecKeychainBugVisitor(SymbolRef S) : Sym(S) {}
Anna Zaksbe460892011-08-24 20:52:46 +0000131
Craig Topperfb6b25b2014-03-15 04:29:04 +0000132 void Profile(llvm::FoldingSetNodeID &ID) const override {
Anna Zaksbe460892011-08-24 20:52:46 +0000133 static int X = 0;
134 ID.AddPointer(&X);
135 ID.AddPointer(Sym);
136 }
137
David Blaikie0a0c2752017-01-05 17:26:53 +0000138 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
139 const ExplodedNode *PrevN,
140 BugReporterContext &BRC,
141 BugReport &BR) override;
Anna Zaksbe460892011-08-24 20:52:46 +0000142 };
Anna Zaks15f496c2011-08-01 22:40:01 +0000143};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000144}
Anna Zaks15f496c2011-08-01 22:40:01 +0000145
Anna Zaks5443a642011-08-15 23:23:15 +0000146/// ProgramState traits to store the currently allocated (and not yet freed)
147/// symbols. This is a map from the allocated content symbol to the
148/// corresponding AllocationState.
Jordan Rose0c153cb2012-11-02 01:54:06 +0000149REGISTER_MAP_WITH_PROGRAMSTATE(AllocatedData,
150 SymbolRef,
151 MacOSKeychainAPIChecker::AllocationState)
Anna Zaks15f496c2011-08-01 22:40:01 +0000152
Anna Zaks388c18e2011-08-04 00:26:57 +0000153static bool isEnclosingFunctionParam(const Expr *E) {
154 E = E->IgnoreParenCasts();
155 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
156 const ValueDecl *VD = DRE->getDecl();
157 if (isa<ImplicitParamDecl>(VD) || isa<ParmVarDecl>(VD))
158 return true;
159 }
160 return false;
161}
162
Anna Zaks08be9b92011-08-04 17:28:06 +0000163const MacOSKeychainAPIChecker::ADFunctionInfo
164 MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = {
Anna Zaks0897a232011-08-24 00:06:27 +0000165 {"SecKeychainItemCopyContent", 4, 3, ValidAPI}, // 0
166 {"SecKeychainFindGenericPassword", 6, 3, ValidAPI}, // 1
167 {"SecKeychainFindInternetPassword", 13, 3, ValidAPI}, // 2
168 {"SecKeychainItemFreeContent", 1, InvalidIdx, ValidAPI}, // 3
169 {"SecKeychainItemCopyAttributesAndData", 5, 5, ValidAPI}, // 4
170 {"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx, ValidAPI}, // 5
171 {"free", 0, InvalidIdx, ErrorAPI}, // 6
172 {"CFStringCreateWithBytesNoCopy", 1, InvalidIdx, PossibleAPI}, // 7
Anna Zaks08be9b92011-08-04 17:28:06 +0000173};
174
175unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name,
Anna Zaksbe460892011-08-24 20:52:46 +0000176 bool IsAllocator) {
Anna Zaks08be9b92011-08-04 17:28:06 +0000177 for (unsigned I = 0; I < FunctionsToTrackSize; ++I) {
178 ADFunctionInfo FI = FunctionsToTrack[I];
179 if (FI.Name != Name)
180 continue;
181 // Make sure the function is of the right type (allocator vs deallocator).
182 if (IsAllocator && (FI.DeallocatorIdx == InvalidIdx))
183 return InvalidIdx;
184 if (!IsAllocator && (FI.DeallocatorIdx != InvalidIdx))
185 return InvalidIdx;
186
187 return I;
188 }
189 // The function is not tracked.
190 return InvalidIdx;
191}
192
Anna Zaksc94894f2011-08-12 21:14:26 +0000193static bool isBadDeallocationArgument(const MemRegion *Arg) {
Jordy Rosef80b2cc2012-03-11 00:08:24 +0000194 if (!Arg)
195 return false;
Alexander Kornienko9c104902015-12-28 13:06:58 +0000196 return isa<AllocaRegion>(Arg) || isa<BlockDataRegion>(Arg) ||
197 isa<TypedRegion>(Arg);
Anna Zaksc94894f2011-08-12 21:14:26 +0000198}
Jordy Rosef80b2cc2012-03-11 00:08:24 +0000199
Anna Zaksc52bed12011-08-05 00:37:00 +0000200/// Given the address expression, retrieve the value it's pointing to. Assume
Anna Zaksc94894f2011-08-12 21:14:26 +0000201/// that value is itself an address, and return the corresponding symbol.
202static SymbolRef getAsPointeeSymbol(const Expr *Expr,
203 CheckerContext &C) {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000204 ProgramStateRef State = C.getState();
George Karpenkovd703ec92018-01-17 20:27:29 +0000205 SVal ArgV = C.getSVal(Expr);
Anna Zaks177ecfa2011-08-05 23:52:45 +0000206
David Blaikie05785d12013-02-20 22:23:23 +0000207 if (Optional<loc::MemRegionVal> X = ArgV.getAs<loc::MemRegionVal>()) {
Anna Zaksc52bed12011-08-05 00:37:00 +0000208 StoreManager& SM = C.getStoreManager();
Jordy Rosef80b2cc2012-03-11 00:08:24 +0000209 SymbolRef sym = SM.getBinding(State->getStore(), *X).getAsLocSymbol();
210 if (sym)
211 return sym;
Anna Zaksc52bed12011-08-05 00:37:00 +0000212 }
Craig Topper0dbb7832014-05-27 02:45:47 +0000213 return nullptr;
Anna Zaksc52bed12011-08-05 00:37:00 +0000214}
215
Anna Zaksbb167012011-08-23 23:47:36 +0000216// Report deallocator mismatch. Remove the region from tracking - reporting a
217// missing free error after this one is redundant.
218void MacOSKeychainAPIChecker::
Anna Zaks85913db2011-08-25 00:32:42 +0000219 generateDeallocatorMismatchReport(const AllocationPair &AP,
Anna Zaksbb167012011-08-23 23:47:36 +0000220 const Expr *ArgExpr,
Anna Zaks85913db2011-08-25 00:32:42 +0000221 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000222 ProgramStateRef State = C.getState();
Anna Zaks85913db2011-08-25 00:32:42 +0000223 State = State->remove<AllocatedData>(AP.first);
Devin Coughline39bd402015-09-16 22:03:05 +0000224 ExplodedNode *N = C.generateNonFatalErrorNode(State);
Anna Zaksbb167012011-08-23 23:47:36 +0000225
226 if (!N)
227 return;
228 initBugType();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000229 SmallString<80> sbuf;
Anna Zaksbb167012011-08-23 23:47:36 +0000230 llvm::raw_svector_ostream os(sbuf);
Anna Zaks85913db2011-08-25 00:32:42 +0000231 unsigned int PDeallocIdx =
232 FunctionsToTrack[AP.second->AllocatorIdx].DeallocatorIdx;
Anna Zaksbb167012011-08-23 23:47:36 +0000233
234 os << "Deallocator doesn't match the allocator: '"
235 << FunctionsToTrack[PDeallocIdx].Name << "' should be used.";
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000236 auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N);
David Blaikie91e79022014-09-04 23:54:33 +0000237 Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(AP.first));
Anna Zaksbb167012011-08-23 23:47:36 +0000238 Report->addRange(ArgExpr->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000239 markInteresting(Report.get(), AP);
240 C.emitReport(std::move(Report));
Anna Zaksbb167012011-08-23 23:47:36 +0000241}
242
Anna Zaks15f496c2011-08-01 22:40:01 +0000243void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
244 CheckerContext &C) const {
Anna Zaksc52bed12011-08-05 00:37:00 +0000245 unsigned idx = InvalidIdx;
Ted Kremenek49b1e382012-01-26 21:29:00 +0000246 ProgramStateRef State = C.getState();
Anna Zaks15f496c2011-08-01 22:40:01 +0000247
Jordan Rose6cd16c52012-07-10 23:13:01 +0000248 const FunctionDecl *FD = C.getCalleeDecl(CE);
249 if (!FD || FD->getKind() != Decl::Function)
250 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000251
Jordan Rose6cd16c52012-07-10 23:13:01 +0000252 StringRef funName = C.getCalleeName(FD);
Anna Zaksc6aa5312011-12-01 05:57:37 +0000253 if (funName.empty())
Anna Zaks15f496c2011-08-01 22:40:01 +0000254 return;
Anna Zaks15f496c2011-08-01 22:40:01 +0000255
Anna Zaksc52bed12011-08-05 00:37:00 +0000256 // If it is a call to an allocator function, it could be a double allocation.
257 idx = getTrackedFunctionIndex(funName, true);
258 if (idx != InvalidIdx) {
Anna Zaks33f06322015-02-05 01:02:56 +0000259 unsigned paramIdx = FunctionsToTrack[idx].Param;
260 if (CE->getNumArgs() <= paramIdx)
261 return;
262
263 const Expr *ArgExpr = CE->getArg(paramIdx);
Anna Zaksc94894f2011-08-12 21:14:26 +0000264 if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C))
Anna Zaksc52bed12011-08-05 00:37:00 +0000265 if (const AllocationState *AS = State->get<AllocatedData>(V)) {
Anna Zaks14b1af52017-01-13 00:50:41 +0000266 // Remove the value from the state. The new symbol will be added for
267 // tracking when the second allocator is processed in checkPostStmt().
268 State = State->remove<AllocatedData>(V);
269 ExplodedNode *N = C.generateNonFatalErrorNode(State);
270 if (!N)
271 return;
272 initBugType();
273 SmallString<128> sbuf;
274 llvm::raw_svector_ostream os(sbuf);
275 unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
276 os << "Allocated data should be released before another call to "
277 << "the allocator: missing a call to '"
278 << FunctionsToTrack[DIdx].Name
279 << "'.";
280 auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N);
281 Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(V));
282 Report->addRange(ArgExpr->getSourceRange());
283 Report->markInteresting(AS->Region);
284 C.emitReport(std::move(Report));
Anna Zaksc52bed12011-08-05 00:37:00 +0000285 }
286 return;
287 }
288
289 // Is it a call to one of deallocator functions?
290 idx = getTrackedFunctionIndex(funName, false);
Anna Zaks08be9b92011-08-04 17:28:06 +0000291 if (idx == InvalidIdx)
Anna Zaks4aa34a532011-08-04 00:31:38 +0000292 return;
293
Anna Zaks33f06322015-02-05 01:02:56 +0000294 unsigned paramIdx = FunctionsToTrack[idx].Param;
295 if (CE->getNumArgs() <= paramIdx)
296 return;
297
Anna Zaksc94894f2011-08-12 21:14:26 +0000298 // Check the argument to the deallocator.
Anna Zaks33f06322015-02-05 01:02:56 +0000299 const Expr *ArgExpr = CE->getArg(paramIdx);
George Karpenkovd703ec92018-01-17 20:27:29 +0000300 SVal ArgSVal = C.getSVal(ArgExpr);
Anna Zaksc94894f2011-08-12 21:14:26 +0000301
302 // Undef is reported by another checker.
303 if (ArgSVal.isUndef())
304 return;
305
Jordy Rosef80b2cc2012-03-11 00:08:24 +0000306 SymbolRef ArgSM = ArgSVal.getAsLocSymbol();
Anna Zaksc94894f2011-08-12 21:14:26 +0000307
Anna Zaksc94894f2011-08-12 21:14:26 +0000308 // If the argument is coming from the heap, globals, or unknown, do not
309 // report it.
Jordy Rosef80b2cc2012-03-11 00:08:24 +0000310 bool RegionArgIsBad = false;
311 if (!ArgSM) {
312 if (!isBadDeallocationArgument(ArgSVal.getAsRegion()))
313 return;
314 RegionArgIsBad = true;
315 }
Anna Zaks4aa34a532011-08-04 00:31:38 +0000316
Anna Zaks0897a232011-08-24 00:06:27 +0000317 // Is the argument to the call being tracked?
318 const AllocationState *AS = State->get<AllocatedData>(ArgSM);
Anna Zaks14b1af52017-01-13 00:50:41 +0000319 if (!AS)
Anna Zaks0897a232011-08-24 00:06:27 +0000320 return;
Anna Zaks14b1af52017-01-13 00:50:41 +0000321
322 // TODO: We might want to report double free here.
Anna Zaks5443a642011-08-15 23:23:15 +0000323 // (that would involve tracking all the freed symbols in the checker state).
Anna Zaks14b1af52017-01-13 00:50:41 +0000324 if (RegionArgIsBad) {
Anna Zaks4aa34a532011-08-04 00:31:38 +0000325 // It is possible that this is a false positive - the argument might
326 // have entered as an enclosing function parameter.
327 if (isEnclosingFunctionParam(ArgExpr))
Anna Zaks15f496c2011-08-01 22:40:01 +0000328 return;
Anna Zaks388c18e2011-08-04 00:26:57 +0000329
Devin Coughline39bd402015-09-16 22:03:05 +0000330 ExplodedNode *N = C.generateNonFatalErrorNode(State);
Anna Zaks4aa34a532011-08-04 00:31:38 +0000331 if (!N)
332 return;
333 initBugType();
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000334 auto Report = llvm::make_unique<BugReport>(
335 *BT, "Trying to free data which has not been allocated.", N);
Anna Zaks4aa34a532011-08-04 00:31:38 +0000336 Report->addRange(ArgExpr->getSourceRange());
Ted Kremenek1e809b42012-03-09 01:13:14 +0000337 if (AS)
338 Report->markInteresting(AS->Region);
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000339 C.emitReport(std::move(Report));
Anna Zaks08be9b92011-08-04 17:28:06 +0000340 return;
Anna Zaks15f496c2011-08-01 22:40:01 +0000341 }
Anna Zaks4aa34a532011-08-04 00:31:38 +0000342
Anna Zaks0897a232011-08-24 00:06:27 +0000343 // Process functions which might deallocate.
344 if (FunctionsToTrack[idx].Kind == PossibleAPI) {
345
346 if (funName == "CFStringCreateWithBytesNoCopy") {
347 const Expr *DeallocatorExpr = CE->getArg(5)->IgnoreParenCasts();
348 // NULL ~ default deallocator, so warn.
349 if (DeallocatorExpr->isNullPointerConstant(C.getASTContext(),
350 Expr::NPC_ValueDependentIsNotNull)) {
Anna Zaks85913db2011-08-25 00:32:42 +0000351 const AllocationPair AP = std::make_pair(ArgSM, AS);
352 generateDeallocatorMismatchReport(AP, ArgExpr, C);
Anna Zaks0897a232011-08-24 00:06:27 +0000353 return;
354 }
355 // One of the default allocators, so warn.
356 if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(DeallocatorExpr)) {
357 StringRef DeallocatorName = DE->getFoundDecl()->getName();
358 if (DeallocatorName == "kCFAllocatorDefault" ||
359 DeallocatorName == "kCFAllocatorSystemDefault" ||
360 DeallocatorName == "kCFAllocatorMalloc") {
Anna Zaks85913db2011-08-25 00:32:42 +0000361 const AllocationPair AP = std::make_pair(ArgSM, AS);
362 generateDeallocatorMismatchReport(AP, ArgExpr, C);
Anna Zaks0897a232011-08-24 00:06:27 +0000363 return;
364 }
365 // If kCFAllocatorNull, which does not deallocate, we still have to
Anna Zaks030e65d2013-01-07 19:13:00 +0000366 // find the deallocator.
367 if (DE->getFoundDecl()->getName() == "kCFAllocatorNull")
Anna Zaks0897a232011-08-24 00:06:27 +0000368 return;
Anna Zaks0897a232011-08-24 00:06:27 +0000369 }
Anna Zaks030e65d2013-01-07 19:13:00 +0000370 // In all other cases, assume the user supplied a correct deallocator
371 // that will free memory so stop tracking.
372 State = State->remove<AllocatedData>(ArgSM);
373 C.addTransition(State);
374 return;
Anna Zaks0897a232011-08-24 00:06:27 +0000375 }
Anna Zaks030e65d2013-01-07 19:13:00 +0000376
377 llvm_unreachable("We know of no other possible APIs.");
Anna Zaks0897a232011-08-24 00:06:27 +0000378 }
379
Anna Zaks5443a642011-08-15 23:23:15 +0000380 // The call is deallocating a value we previously allocated, so remove it
381 // from the next state.
382 State = State->remove<AllocatedData>(ArgSM);
383
Anna Zaksbb167012011-08-23 23:47:36 +0000384 // Check if the proper deallocator is used.
Anna Zaksc6861772011-08-04 21:53:01 +0000385 unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
Anna Zaks0897a232011-08-24 00:06:27 +0000386 if (PDeallocIdx != idx || (FunctionsToTrack[idx].Kind == ErrorAPI)) {
Anna Zaks85913db2011-08-25 00:32:42 +0000387 const AllocationPair AP = std::make_pair(ArgSM, AS);
388 generateDeallocatorMismatchReport(AP, ArgExpr, C);
Anna Zaksc6861772011-08-04 21:53:01 +0000389 return;
390 }
391
Anna Zaksda4c8d62011-10-26 21:06:34 +0000392 C.addTransition(State);
Anna Zaks15f496c2011-08-01 22:40:01 +0000393}
394
395void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE,
396 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000397 ProgramStateRef State = C.getState();
Jordan Rose6cd16c52012-07-10 23:13:01 +0000398 const FunctionDecl *FD = C.getCalleeDecl(CE);
399 if (!FD || FD->getKind() != Decl::Function)
400 return;
401
402 StringRef funName = C.getCalleeName(FD);
Anna Zaks15f496c2011-08-01 22:40:01 +0000403
404 // If a value has been allocated, add it to the set for tracking.
Anna Zaks08be9b92011-08-04 17:28:06 +0000405 unsigned idx = getTrackedFunctionIndex(funName, true);
406 if (idx == InvalidIdx)
Anna Zaks4aa34a532011-08-04 00:31:38 +0000407 return;
Anna Zaks388c18e2011-08-04 00:26:57 +0000408
Anna Zaks08be9b92011-08-04 17:28:06 +0000409 const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
Anna Zaks59d741f2011-08-12 22:47:22 +0000410 // If the argument entered as an enclosing function parameter, skip it to
411 // avoid false positives.
Anna Zaks19a66672012-02-21 00:00:44 +0000412 if (isEnclosingFunctionParam(ArgExpr) &&
Craig Topper0dbb7832014-05-27 02:45:47 +0000413 C.getLocationContext()->getParent() == nullptr)
Anna Zaks59d741f2011-08-12 22:47:22 +0000414 return;
415
Anna Zaksc94894f2011-08-12 21:14:26 +0000416 if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) {
417 // If the argument points to something that's not a symbolic region, it
418 // can be:
Anna Zaks4aa34a532011-08-04 00:31:38 +0000419 // - unknown (cannot reason about it)
420 // - undefined (already reported by other checker)
Anna Zaks08be9b92011-08-04 17:28:06 +0000421 // - constant (null - should not be tracked,
422 // other constant will generate a compiler warning)
Anna Zaks4aa34a532011-08-04 00:31:38 +0000423 // - goto (should be reported by other checker)
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000424
425 // The call return value symbol should stay alive for as long as the
426 // allocated value symbol, since our diagnostics depend on the value
427 // returned by the call. Ex: Data should only be freed if noErr was
428 // returned during allocation.)
George Karpenkovd703ec92018-01-17 20:27:29 +0000429 SymbolRef RetStatusSymbol = C.getSVal(CE).getAsSymbol();
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000430 C.getSymbolManager().addSymbolDependency(V, RetStatusSymbol);
431
432 // Track the allocated value in the checker state.
433 State = State->set<AllocatedData>(V, AllocationState(ArgExpr, idx,
Anna Zaksc94894f2011-08-12 21:14:26 +0000434 RetStatusSymbol));
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000435 assert(State);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000436 C.addTransition(State);
Anna Zaks15f496c2011-08-01 22:40:01 +0000437 }
438}
439
Anna Zaks6ca4fd52012-02-28 03:07:06 +0000440// TODO: This logic is the same as in Malloc checker.
Anna Zaksa043d0c2013-01-08 00:25:29 +0000441const ExplodedNode *
442MacOSKeychainAPIChecker::getAllocationNode(const ExplodedNode *N,
Anna Zaks4b062cb2012-02-23 22:53:29 +0000443 SymbolRef Sym,
444 CheckerContext &C) const {
Anna Zaks6ca4fd52012-02-28 03:07:06 +0000445 const LocationContext *LeakContext = N->getLocationContext();
Anna Zaks4b062cb2012-02-23 22:53:29 +0000446 // Walk the ExplodedGraph backwards and find the first node that referred to
447 // the tracked symbol.
448 const ExplodedNode *AllocNode = N;
449
450 while (N) {
451 if (!N->getState()->get<AllocatedData>(Sym))
452 break;
Anna Zaks486a0ff2015-02-05 01:02:53 +0000453 // Allocation node, is the last node in the current or parent context in
454 // which the symbol was tracked.
455 const LocationContext *NContext = N->getLocationContext();
456 if (NContext == LeakContext ||
457 NContext->isParentOf(LeakContext))
Anna Zaks6ca4fd52012-02-28 03:07:06 +0000458 AllocNode = N;
Craig Topper0dbb7832014-05-27 02:45:47 +0000459 N = N->pred_empty() ? nullptr : *(N->pred_begin());
Anna Zaks4b062cb2012-02-23 22:53:29 +0000460 }
461
Anna Zaksa043d0c2013-01-08 00:25:29 +0000462 return AllocNode;
Anna Zaks4b062cb2012-02-23 22:53:29 +0000463}
464
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000465std::unique_ptr<BugReport>
466MacOSKeychainAPIChecker::generateAllocatedDataNotReleasedReport(
467 const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const {
Anna Zaksf880cff2011-08-24 21:58:55 +0000468 const ADFunctionInfo &FI = FunctionsToTrack[AP.second->AllocatorIdx];
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000469 initBugType();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000470 SmallString<70> sbuf;
Anna Zaks29f9b7a2011-08-15 18:42:00 +0000471 llvm::raw_svector_ostream os(sbuf);
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000472 os << "Allocated data is not released: missing a call to '"
473 << FunctionsToTrack[FI.DeallocatorIdx].Name << "'.";
Anna Zaks4b062cb2012-02-23 22:53:29 +0000474
475 // Most bug reports are cached at the location where they occurred.
476 // With leaks, we want to unique them by the location where they were
477 // allocated, and only report a single path.
Anna Zaks6ca4fd52012-02-28 03:07:06 +0000478 PathDiagnosticLocation LocUsedForUniqueing;
Anna Zaksa043d0c2013-01-08 00:25:29 +0000479 const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C);
Gabor Horvath6ee4f902016-08-18 07:54:50 +0000480 const Stmt *AllocStmt = PathDiagnosticLocation::getStmt(AllocNode);
Anna Zaks4b062cb2012-02-23 22:53:29 +0000481
Anna Zaksa043d0c2013-01-08 00:25:29 +0000482 if (AllocStmt)
483 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,
484 C.getSourceManager(),
485 AllocNode->getLocationContext());
486
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000487 auto Report =
488 llvm::make_unique<BugReport>(*BT, os.str(), N, LocUsedForUniqueing,
489 AllocNode->getLocationContext()->getDecl());
Anna Zaksa043d0c2013-01-08 00:25:29 +0000490
David Blaikie91e79022014-09-04 23:54:33 +0000491 Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(AP.first));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000492 markInteresting(Report.get(), AP);
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000493 return Report;
494}
495
Anna Zaks14b1af52017-01-13 00:50:41 +0000496/// If the return symbol is assumed to be error, remove the allocated info
497/// from consideration.
498ProgramStateRef MacOSKeychainAPIChecker::evalAssume(ProgramStateRef State,
499 SVal Cond,
500 bool Assumption) const {
501 AllocatedDataTy AMap = State->get<AllocatedData>();
502 if (AMap.isEmpty())
503 return State;
504
505 auto *CondBSE = dyn_cast_or_null<BinarySymExpr>(Cond.getAsSymExpr());
506 if (!CondBSE)
507 return State;
508 BinaryOperator::Opcode OpCode = CondBSE->getOpcode();
509 if (OpCode != BO_EQ && OpCode != BO_NE)
510 return State;
511
512 // Match for a restricted set of patterns for cmparison of error codes.
513 // Note, the comparisons of type '0 == st' are transformed into SymIntExpr.
514 SymbolRef ReturnSymbol = nullptr;
515 if (auto *SIE = dyn_cast<SymIntExpr>(CondBSE)) {
516 const llvm::APInt &RHS = SIE->getRHS();
517 bool ErrorIsReturned = (OpCode == BO_EQ && RHS != NoErr) ||
518 (OpCode == BO_NE && RHS == NoErr);
519 if (!Assumption)
520 ErrorIsReturned = !ErrorIsReturned;
521 if (ErrorIsReturned)
522 ReturnSymbol = SIE->getLHS();
523 }
524
525 if (ReturnSymbol)
526 for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
527 if (ReturnSymbol == I->second.Region)
528 State = State->remove<AllocatedData>(I->first);
529 }
530
531 return State;
532}
533
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000534void MacOSKeychainAPIChecker::checkDeadSymbols(SymbolReaper &SR,
535 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000536 ProgramStateRef State = C.getState();
Anna Zaks14b1af52017-01-13 00:50:41 +0000537 AllocatedDataTy AMap = State->get<AllocatedData>();
538 if (AMap.isEmpty())
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000539 return;
540
541 bool Changed = false;
Anna Zaksbe460892011-08-24 20:52:46 +0000542 AllocationPairVec Errors;
Anna Zaks14b1af52017-01-13 00:50:41 +0000543 for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
544 if (!SR.isDead(I->first))
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000545 continue;
546
547 Changed = true;
548 State = State->remove<AllocatedData>(I->first);
Anna Zaks14b1af52017-01-13 00:50:41 +0000549 // If the allocated symbol is null do not report.
Jordan Rose14fe9f32012-11-01 00:18:27 +0000550 ConstraintManager &CMgr = State->getConstraintManager();
551 ConditionTruthVal AllocFailed = CMgr.isNull(State, I.getKey());
Anna Zaks14b1af52017-01-13 00:50:41 +0000552 if (AllocFailed.isConstrainedTrue())
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000553 continue;
Anna Zaksf880cff2011-08-24 21:58:55 +0000554 Errors.push_back(std::make_pair(I->first, &I->second));
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000555 }
Anna Zaks4b062cb2012-02-23 22:53:29 +0000556 if (!Changed) {
557 // Generate the new, cleaned up state.
558 C.addTransition(State);
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000559 return;
Anna Zaks4b062cb2012-02-23 22:53:29 +0000560 }
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000561
Anton Yartsev6a619222014-02-17 18:25:34 +0000562 static CheckerProgramPointTag Tag(this, "DeadSymbolsLeak");
Devin Coughline39bd402015-09-16 22:03:05 +0000563 ExplodedNode *N = C.generateNonFatalErrorNode(C.getState(), &Tag);
564 if (!N)
565 return;
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000566
567 // Generate the error reports.
Benjamin Kramer72e64312015-09-24 14:48:49 +0000568 for (const auto &P : Errors)
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000569 C.emitReport(generateAllocatedDataNotReleasedReport(P, N, C));
Anna Zaks4b062cb2012-02-23 22:53:29 +0000570
571 // Generate the new, cleaned up state.
572 C.addTransition(State, N);
Anna Zaksfdd0aca2011-08-12 21:56:43 +0000573}
574
David Blaikie0a0c2752017-01-05 17:26:53 +0000575std::shared_ptr<PathDiagnosticPiece>
576MacOSKeychainAPIChecker::SecKeychainBugVisitor::VisitNode(
577 const ExplodedNode *N, const ExplodedNode *PrevN, BugReporterContext &BRC,
578 BugReport &BR) {
Anna Zaksbe460892011-08-24 20:52:46 +0000579 const AllocationState *AS = N->getState()->get<AllocatedData>(Sym);
580 if (!AS)
Craig Topper0dbb7832014-05-27 02:45:47 +0000581 return nullptr;
Anna Zaksbe460892011-08-24 20:52:46 +0000582 const AllocationState *ASPrev = PrevN->getState()->get<AllocatedData>(Sym);
583 if (ASPrev)
Craig Topper0dbb7832014-05-27 02:45:47 +0000584 return nullptr;
Anna Zaksbe460892011-08-24 20:52:46 +0000585
586 // (!ASPrev && AS) ~ We started tracking symbol in node N, it must be the
587 // allocation site.
David Blaikie87396b92013-02-21 22:23:56 +0000588 const CallExpr *CE =
589 cast<CallExpr>(N->getLocation().castAs<StmtPoint>().getStmt());
Anna Zaksbe460892011-08-24 20:52:46 +0000590 const FunctionDecl *funDecl = CE->getDirectCallee();
591 assert(funDecl && "We do not support indirect function calls as of now.");
592 StringRef funName = funDecl->getName();
593
594 // Get the expression of the corresponding argument.
595 unsigned Idx = getTrackedFunctionIndex(funName, true);
596 assert(Idx != InvalidIdx && "This should be a call to an allocator.");
597 const Expr *ArgExpr = CE->getArg(FunctionsToTrack[Idx].Param);
Anna Zaks3a769bd2011-09-15 01:08:34 +0000598 PathDiagnosticLocation Pos(ArgExpr, BRC.getSourceManager(),
599 N->getLocationContext());
David Blaikie0a0c2752017-01-05 17:26:53 +0000600 return std::make_shared<PathDiagnosticEventPiece>(Pos,
601 "Data is allocated here.");
Anna Zaks15f496c2011-08-01 22:40:01 +0000602}
603
Anna Zaks14b1af52017-01-13 00:50:41 +0000604void MacOSKeychainAPIChecker::printState(raw_ostream &Out,
605 ProgramStateRef State,
606 const char *NL,
607 const char *Sep) const {
608
609 AllocatedDataTy AMap = State->get<AllocatedData>();
610
611 if (!AMap.isEmpty()) {
612 Out << Sep << "KeychainAPIChecker :" << NL;
613 for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
614 I.getKey()->dumpToStream(Out);
615 }
616 }
617}
618
619
Anna Zaks15f496c2011-08-01 22:40:01 +0000620void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) {
621 mgr.registerChecker<MacOSKeychainAPIChecker>();
622}