MacOSKeychainAPIChecker.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. //==--- MacOSKeychainAPIChecker.cpp ------------------------------*- C++ -*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // This checker flags misuses of KeyChainAPI. In particular, the password data
  9. // allocated/returned by SecKeychainItemCopyContent,
  10. // SecKeychainFindGenericPassword, SecKeychainFindInternetPassword functions has
  11. // to be freed using a call to SecKeychainItemFreeContent.
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  20. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace clang;
  24. using namespace ento;
  25. namespace {
  26. class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>,
  27. check::PostStmt<CallExpr>,
  28. check::DeadSymbols,
  29. check::PointerEscape,
  30. eval::Assume> {
  31. mutable std::unique_ptr<BugType> BT;
  32. public:
  33. /// AllocationState is a part of the checker specific state together with the
  34. /// MemRegion corresponding to the allocated data.
  35. struct AllocationState {
  36. /// The index of the allocator function.
  37. unsigned int AllocatorIdx;
  38. SymbolRef Region;
  39. AllocationState(const Expr *E, unsigned int Idx, SymbolRef R) :
  40. AllocatorIdx(Idx),
  41. Region(R) {}
  42. bool operator==(const AllocationState &X) const {
  43. return (AllocatorIdx == X.AllocatorIdx &&
  44. Region == X.Region);
  45. }
  46. void Profile(llvm::FoldingSetNodeID &ID) const {
  47. ID.AddInteger(AllocatorIdx);
  48. ID.AddPointer(Region);
  49. }
  50. };
  51. void checkPreStmt(const CallExpr *S, CheckerContext &C) const;
  52. void checkPostStmt(const CallExpr *S, CheckerContext &C) const;
  53. void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
  54. ProgramStateRef checkPointerEscape(ProgramStateRef State,
  55. const InvalidatedSymbols &Escaped,
  56. const CallEvent *Call,
  57. PointerEscapeKind Kind) const;
  58. ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
  59. bool Assumption) const;
  60. void printState(raw_ostream &Out, ProgramStateRef State,
  61. const char *NL, const char *Sep) const override;
  62. private:
  63. typedef std::pair<SymbolRef, const AllocationState*> AllocationPair;
  64. typedef SmallVector<AllocationPair, 2> AllocationPairVec;
  65. enum APIKind {
  66. /// Denotes functions tracked by this checker.
  67. ValidAPI = 0,
  68. /// The functions commonly/mistakenly used in place of the given API.
  69. ErrorAPI = 1,
  70. /// The functions which may allocate the data. These are tracked to reduce
  71. /// the false alarm rate.
  72. PossibleAPI = 2
  73. };
  74. /// Stores the information about the allocator and deallocator functions -
  75. /// these are the functions the checker is tracking.
  76. struct ADFunctionInfo {
  77. const char* Name;
  78. unsigned int Param;
  79. unsigned int DeallocatorIdx;
  80. APIKind Kind;
  81. };
  82. static const unsigned InvalidIdx = 100000;
  83. static const unsigned FunctionsToTrackSize = 8;
  84. static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize];
  85. /// The value, which represents no error return value for allocator functions.
  86. static const unsigned NoErr = 0;
  87. /// Given the function name, returns the index of the allocator/deallocator
  88. /// function.
  89. static unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator);
  90. inline void initBugType() const {
  91. if (!BT)
  92. BT.reset(new BugType(this, "Improper use of SecKeychain API",
  93. "API Misuse (Apple)"));
  94. }
  95. void generateDeallocatorMismatchReport(const AllocationPair &AP,
  96. const Expr *ArgExpr,
  97. CheckerContext &C) const;
  98. /// Find the allocation site for Sym on the path leading to the node N.
  99. const ExplodedNode *getAllocationNode(const ExplodedNode *N, SymbolRef Sym,
  100. CheckerContext &C) const;
  101. std::unique_ptr<PathSensitiveBugReport>
  102. generateAllocatedDataNotReleasedReport(const AllocationPair &AP,
  103. ExplodedNode *N,
  104. CheckerContext &C) const;
  105. /// Mark an AllocationPair interesting for diagnostic reporting.
  106. void markInteresting(PathSensitiveBugReport *R,
  107. const AllocationPair &AP) const {
  108. R->markInteresting(AP.first);
  109. R->markInteresting(AP.second->Region);
  110. }
  111. /// The bug visitor which allows us to print extra diagnostics along the
  112. /// BugReport path. For example, showing the allocation site of the leaked
  113. /// region.
  114. class SecKeychainBugVisitor : public BugReporterVisitor {
  115. protected:
  116. // The allocated region symbol tracked by the main analysis.
  117. SymbolRef Sym;
  118. public:
  119. SecKeychainBugVisitor(SymbolRef S) : Sym(S) {}
  120. void Profile(llvm::FoldingSetNodeID &ID) const override {
  121. static int X = 0;
  122. ID.AddPointer(&X);
  123. ID.AddPointer(Sym);
  124. }
  125. PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
  126. BugReporterContext &BRC,
  127. PathSensitiveBugReport &BR) override;
  128. };
  129. };
  130. }
  131. /// ProgramState traits to store the currently allocated (and not yet freed)
  132. /// symbols. This is a map from the allocated content symbol to the
  133. /// corresponding AllocationState.
  134. REGISTER_MAP_WITH_PROGRAMSTATE(AllocatedData,
  135. SymbolRef,
  136. MacOSKeychainAPIChecker::AllocationState)
  137. static bool isEnclosingFunctionParam(const Expr *E) {
  138. E = E->IgnoreParenCasts();
  139. if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
  140. const ValueDecl *VD = DRE->getDecl();
  141. if (isa<ImplicitParamDecl, ParmVarDecl>(VD))
  142. return true;
  143. }
  144. return false;
  145. }
  146. const MacOSKeychainAPIChecker::ADFunctionInfo
  147. MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = {
  148. {"SecKeychainItemCopyContent", 4, 3, ValidAPI}, // 0
  149. {"SecKeychainFindGenericPassword", 6, 3, ValidAPI}, // 1
  150. {"SecKeychainFindInternetPassword", 13, 3, ValidAPI}, // 2
  151. {"SecKeychainItemFreeContent", 1, InvalidIdx, ValidAPI}, // 3
  152. {"SecKeychainItemCopyAttributesAndData", 5, 5, ValidAPI}, // 4
  153. {"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx, ValidAPI}, // 5
  154. {"free", 0, InvalidIdx, ErrorAPI}, // 6
  155. {"CFStringCreateWithBytesNoCopy", 1, InvalidIdx, PossibleAPI}, // 7
  156. };
  157. unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name,
  158. bool IsAllocator) {
  159. for (unsigned I = 0; I < FunctionsToTrackSize; ++I) {
  160. ADFunctionInfo FI = FunctionsToTrack[I];
  161. if (FI.Name != Name)
  162. continue;
  163. // Make sure the function is of the right type (allocator vs deallocator).
  164. if (IsAllocator && (FI.DeallocatorIdx == InvalidIdx))
  165. return InvalidIdx;
  166. if (!IsAllocator && (FI.DeallocatorIdx != InvalidIdx))
  167. return InvalidIdx;
  168. return I;
  169. }
  170. // The function is not tracked.
  171. return InvalidIdx;
  172. }
  173. static bool isBadDeallocationArgument(const MemRegion *Arg) {
  174. if (!Arg)
  175. return false;
  176. return isa<AllocaRegion, BlockDataRegion, TypedRegion>(Arg);
  177. }
  178. /// Given the address expression, retrieve the value it's pointing to. Assume
  179. /// that value is itself an address, and return the corresponding symbol.
  180. static SymbolRef getAsPointeeSymbol(const Expr *Expr,
  181. CheckerContext &C) {
  182. ProgramStateRef State = C.getState();
  183. SVal ArgV = C.getSVal(Expr);
  184. if (Optional<loc::MemRegionVal> X = ArgV.getAs<loc::MemRegionVal>()) {
  185. StoreManager& SM = C.getStoreManager();
  186. SymbolRef sym = SM.getBinding(State->getStore(), *X).getAsLocSymbol();
  187. if (sym)
  188. return sym;
  189. }
  190. return nullptr;
  191. }
  192. // Report deallocator mismatch. Remove the region from tracking - reporting a
  193. // missing free error after this one is redundant.
  194. void MacOSKeychainAPIChecker::
  195. generateDeallocatorMismatchReport(const AllocationPair &AP,
  196. const Expr *ArgExpr,
  197. CheckerContext &C) const {
  198. ProgramStateRef State = C.getState();
  199. State = State->remove<AllocatedData>(AP.first);
  200. ExplodedNode *N = C.generateNonFatalErrorNode(State);
  201. if (!N)
  202. return;
  203. initBugType();
  204. SmallString<80> sbuf;
  205. llvm::raw_svector_ostream os(sbuf);
  206. unsigned int PDeallocIdx =
  207. FunctionsToTrack[AP.second->AllocatorIdx].DeallocatorIdx;
  208. os << "Deallocator doesn't match the allocator: '"
  209. << FunctionsToTrack[PDeallocIdx].Name << "' should be used.";
  210. auto Report = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
  211. Report->addVisitor(std::make_unique<SecKeychainBugVisitor>(AP.first));
  212. Report->addRange(ArgExpr->getSourceRange());
  213. markInteresting(Report.get(), AP);
  214. C.emitReport(std::move(Report));
  215. }
  216. void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
  217. CheckerContext &C) const {
  218. unsigned idx = InvalidIdx;
  219. ProgramStateRef State = C.getState();
  220. const FunctionDecl *FD = C.getCalleeDecl(CE);
  221. if (!FD || FD->getKind() != Decl::Function)
  222. return;
  223. StringRef funName = C.getCalleeName(FD);
  224. if (funName.empty())
  225. return;
  226. // If it is a call to an allocator function, it could be a double allocation.
  227. idx = getTrackedFunctionIndex(funName, true);
  228. if (idx != InvalidIdx) {
  229. unsigned paramIdx = FunctionsToTrack[idx].Param;
  230. if (CE->getNumArgs() <= paramIdx)
  231. return;
  232. const Expr *ArgExpr = CE->getArg(paramIdx);
  233. if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C))
  234. if (const AllocationState *AS = State->get<AllocatedData>(V)) {
  235. // Remove the value from the state. The new symbol will be added for
  236. // tracking when the second allocator is processed in checkPostStmt().
  237. State = State->remove<AllocatedData>(V);
  238. ExplodedNode *N = C.generateNonFatalErrorNode(State);
  239. if (!N)
  240. return;
  241. initBugType();
  242. SmallString<128> sbuf;
  243. llvm::raw_svector_ostream os(sbuf);
  244. unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
  245. os << "Allocated data should be released before another call to "
  246. << "the allocator: missing a call to '"
  247. << FunctionsToTrack[DIdx].Name
  248. << "'.";
  249. auto Report =
  250. std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
  251. Report->addVisitor(std::make_unique<SecKeychainBugVisitor>(V));
  252. Report->addRange(ArgExpr->getSourceRange());
  253. Report->markInteresting(AS->Region);
  254. C.emitReport(std::move(Report));
  255. }
  256. return;
  257. }
  258. // Is it a call to one of deallocator functions?
  259. idx = getTrackedFunctionIndex(funName, false);
  260. if (idx == InvalidIdx)
  261. return;
  262. unsigned paramIdx = FunctionsToTrack[idx].Param;
  263. if (CE->getNumArgs() <= paramIdx)
  264. return;
  265. // Check the argument to the deallocator.
  266. const Expr *ArgExpr = CE->getArg(paramIdx);
  267. SVal ArgSVal = C.getSVal(ArgExpr);
  268. // Undef is reported by another checker.
  269. if (ArgSVal.isUndef())
  270. return;
  271. SymbolRef ArgSM = ArgSVal.getAsLocSymbol();
  272. // If the argument is coming from the heap, globals, or unknown, do not
  273. // report it.
  274. bool RegionArgIsBad = false;
  275. if (!ArgSM) {
  276. if (!isBadDeallocationArgument(ArgSVal.getAsRegion()))
  277. return;
  278. RegionArgIsBad = true;
  279. }
  280. // Is the argument to the call being tracked?
  281. const AllocationState *AS = State->get<AllocatedData>(ArgSM);
  282. if (!AS)
  283. return;
  284. // TODO: We might want to report double free here.
  285. // (that would involve tracking all the freed symbols in the checker state).
  286. if (RegionArgIsBad) {
  287. // It is possible that this is a false positive - the argument might
  288. // have entered as an enclosing function parameter.
  289. if (isEnclosingFunctionParam(ArgExpr))
  290. return;
  291. ExplodedNode *N = C.generateNonFatalErrorNode(State);
  292. if (!N)
  293. return;
  294. initBugType();
  295. auto Report = std::make_unique<PathSensitiveBugReport>(
  296. *BT, "Trying to free data which has not been allocated.", N);
  297. Report->addRange(ArgExpr->getSourceRange());
  298. if (AS)
  299. Report->markInteresting(AS->Region);
  300. C.emitReport(std::move(Report));
  301. return;
  302. }
  303. // Process functions which might deallocate.
  304. if (FunctionsToTrack[idx].Kind == PossibleAPI) {
  305. if (funName == "CFStringCreateWithBytesNoCopy") {
  306. const Expr *DeallocatorExpr = CE->getArg(5)->IgnoreParenCasts();
  307. // NULL ~ default deallocator, so warn.
  308. if (DeallocatorExpr->isNullPointerConstant(C.getASTContext(),
  309. Expr::NPC_ValueDependentIsNotNull)) {
  310. const AllocationPair AP = std::make_pair(ArgSM, AS);
  311. generateDeallocatorMismatchReport(AP, ArgExpr, C);
  312. return;
  313. }
  314. // One of the default allocators, so warn.
  315. if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(DeallocatorExpr)) {
  316. StringRef DeallocatorName = DE->getFoundDecl()->getName();
  317. if (DeallocatorName == "kCFAllocatorDefault" ||
  318. DeallocatorName == "kCFAllocatorSystemDefault" ||
  319. DeallocatorName == "kCFAllocatorMalloc") {
  320. const AllocationPair AP = std::make_pair(ArgSM, AS);
  321. generateDeallocatorMismatchReport(AP, ArgExpr, C);
  322. return;
  323. }
  324. // If kCFAllocatorNull, which does not deallocate, we still have to
  325. // find the deallocator.
  326. if (DE->getFoundDecl()->getName() == "kCFAllocatorNull")
  327. return;
  328. }
  329. // In all other cases, assume the user supplied a correct deallocator
  330. // that will free memory so stop tracking.
  331. State = State->remove<AllocatedData>(ArgSM);
  332. C.addTransition(State);
  333. return;
  334. }
  335. llvm_unreachable("We know of no other possible APIs.");
  336. }
  337. // The call is deallocating a value we previously allocated, so remove it
  338. // from the next state.
  339. State = State->remove<AllocatedData>(ArgSM);
  340. // Check if the proper deallocator is used.
  341. unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
  342. if (PDeallocIdx != idx || (FunctionsToTrack[idx].Kind == ErrorAPI)) {
  343. const AllocationPair AP = std::make_pair(ArgSM, AS);
  344. generateDeallocatorMismatchReport(AP, ArgExpr, C);
  345. return;
  346. }
  347. C.addTransition(State);
  348. }
  349. void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE,
  350. CheckerContext &C) const {
  351. ProgramStateRef State = C.getState();
  352. const FunctionDecl *FD = C.getCalleeDecl(CE);
  353. if (!FD || FD->getKind() != Decl::Function)
  354. return;
  355. StringRef funName = C.getCalleeName(FD);
  356. // If a value has been allocated, add it to the set for tracking.
  357. unsigned idx = getTrackedFunctionIndex(funName, true);
  358. if (idx == InvalidIdx)
  359. return;
  360. const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
  361. // If the argument entered as an enclosing function parameter, skip it to
  362. // avoid false positives.
  363. if (isEnclosingFunctionParam(ArgExpr) &&
  364. C.getLocationContext()->getParent() == nullptr)
  365. return;
  366. if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) {
  367. // If the argument points to something that's not a symbolic region, it
  368. // can be:
  369. // - unknown (cannot reason about it)
  370. // - undefined (already reported by other checker)
  371. // - constant (null - should not be tracked,
  372. // other constant will generate a compiler warning)
  373. // - goto (should be reported by other checker)
  374. // The call return value symbol should stay alive for as long as the
  375. // allocated value symbol, since our diagnostics depend on the value
  376. // returned by the call. Ex: Data should only be freed if noErr was
  377. // returned during allocation.)
  378. SymbolRef RetStatusSymbol = C.getSVal(CE).getAsSymbol();
  379. C.getSymbolManager().addSymbolDependency(V, RetStatusSymbol);
  380. // Track the allocated value in the checker state.
  381. State = State->set<AllocatedData>(V, AllocationState(ArgExpr, idx,
  382. RetStatusSymbol));
  383. assert(State);
  384. C.addTransition(State);
  385. }
  386. }
  387. // TODO: This logic is the same as in Malloc checker.
  388. const ExplodedNode *
  389. MacOSKeychainAPIChecker::getAllocationNode(const ExplodedNode *N,
  390. SymbolRef Sym,
  391. CheckerContext &C) const {
  392. const LocationContext *LeakContext = N->getLocationContext();
  393. // Walk the ExplodedGraph backwards and find the first node that referred to
  394. // the tracked symbol.
  395. const ExplodedNode *AllocNode = N;
  396. while (N) {
  397. if (!N->getState()->get<AllocatedData>(Sym))
  398. break;
  399. // Allocation node, is the last node in the current or parent context in
  400. // which the symbol was tracked.
  401. const LocationContext *NContext = N->getLocationContext();
  402. if (NContext == LeakContext ||
  403. NContext->isParentOf(LeakContext))
  404. AllocNode = N;
  405. N = N->pred_empty() ? nullptr : *(N->pred_begin());
  406. }
  407. return AllocNode;
  408. }
  409. std::unique_ptr<PathSensitiveBugReport>
  410. MacOSKeychainAPIChecker::generateAllocatedDataNotReleasedReport(
  411. const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const {
  412. const ADFunctionInfo &FI = FunctionsToTrack[AP.second->AllocatorIdx];
  413. initBugType();
  414. SmallString<70> sbuf;
  415. llvm::raw_svector_ostream os(sbuf);
  416. os << "Allocated data is not released: missing a call to '"
  417. << FunctionsToTrack[FI.DeallocatorIdx].Name << "'.";
  418. // Most bug reports are cached at the location where they occurred.
  419. // With leaks, we want to unique them by the location where they were
  420. // allocated, and only report a single path.
  421. PathDiagnosticLocation LocUsedForUniqueing;
  422. const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C);
  423. const Stmt *AllocStmt = AllocNode->getStmtForDiagnostics();
  424. if (AllocStmt)
  425. LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,
  426. C.getSourceManager(),
  427. AllocNode->getLocationContext());
  428. auto Report = std::make_unique<PathSensitiveBugReport>(
  429. *BT, os.str(), N, LocUsedForUniqueing,
  430. AllocNode->getLocationContext()->getDecl());
  431. Report->addVisitor(std::make_unique<SecKeychainBugVisitor>(AP.first));
  432. markInteresting(Report.get(), AP);
  433. return Report;
  434. }
  435. /// If the return symbol is assumed to be error, remove the allocated info
  436. /// from consideration.
  437. ProgramStateRef MacOSKeychainAPIChecker::evalAssume(ProgramStateRef State,
  438. SVal Cond,
  439. bool Assumption) const {
  440. AllocatedDataTy AMap = State->get<AllocatedData>();
  441. if (AMap.isEmpty())
  442. return State;
  443. auto *CondBSE = dyn_cast_or_null<BinarySymExpr>(Cond.getAsSymbol());
  444. if (!CondBSE)
  445. return State;
  446. BinaryOperator::Opcode OpCode = CondBSE->getOpcode();
  447. if (OpCode != BO_EQ && OpCode != BO_NE)
  448. return State;
  449. // Match for a restricted set of patterns for cmparison of error codes.
  450. // Note, the comparisons of type '0 == st' are transformed into SymIntExpr.
  451. SymbolRef ReturnSymbol = nullptr;
  452. if (auto *SIE = dyn_cast<SymIntExpr>(CondBSE)) {
  453. const llvm::APInt &RHS = SIE->getRHS();
  454. bool ErrorIsReturned = (OpCode == BO_EQ && RHS != NoErr) ||
  455. (OpCode == BO_NE && RHS == NoErr);
  456. if (!Assumption)
  457. ErrorIsReturned = !ErrorIsReturned;
  458. if (ErrorIsReturned)
  459. ReturnSymbol = SIE->getLHS();
  460. }
  461. if (ReturnSymbol)
  462. for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
  463. if (ReturnSymbol == I->second.Region)
  464. State = State->remove<AllocatedData>(I->first);
  465. }
  466. return State;
  467. }
  468. void MacOSKeychainAPIChecker::checkDeadSymbols(SymbolReaper &SR,
  469. CheckerContext &C) const {
  470. ProgramStateRef State = C.getState();
  471. AllocatedDataTy AMap = State->get<AllocatedData>();
  472. if (AMap.isEmpty())
  473. return;
  474. bool Changed = false;
  475. AllocationPairVec Errors;
  476. for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
  477. if (!SR.isDead(I->first))
  478. continue;
  479. Changed = true;
  480. State = State->remove<AllocatedData>(I->first);
  481. // If the allocated symbol is null do not report.
  482. ConstraintManager &CMgr = State->getConstraintManager();
  483. ConditionTruthVal AllocFailed = CMgr.isNull(State, I.getKey());
  484. if (AllocFailed.isConstrainedTrue())
  485. continue;
  486. Errors.push_back(std::make_pair(I->first, &I->second));
  487. }
  488. if (!Changed) {
  489. // Generate the new, cleaned up state.
  490. C.addTransition(State);
  491. return;
  492. }
  493. static CheckerProgramPointTag Tag(this, "DeadSymbolsLeak");
  494. ExplodedNode *N = C.generateNonFatalErrorNode(C.getState(), &Tag);
  495. if (!N)
  496. return;
  497. // Generate the error reports.
  498. for (const auto &P : Errors)
  499. C.emitReport(generateAllocatedDataNotReleasedReport(P, N, C));
  500. // Generate the new, cleaned up state.
  501. C.addTransition(State, N);
  502. }
  503. ProgramStateRef MacOSKeychainAPIChecker::checkPointerEscape(
  504. ProgramStateRef State, const InvalidatedSymbols &Escaped,
  505. const CallEvent *Call, PointerEscapeKind Kind) const {
  506. // FIXME: This branch doesn't make any sense at all, but it is an overfitted
  507. // replacement for a previous overfitted code that was making even less sense.
  508. if (!Call || Call->getDecl())
  509. return State;
  510. for (auto I : State->get<AllocatedData>()) {
  511. SymbolRef Sym = I.first;
  512. if (Escaped.count(Sym))
  513. State = State->remove<AllocatedData>(Sym);
  514. // This checker is special. Most checkers in fact only track symbols of
  515. // SymbolConjured type, eg. symbols returned from functions such as
  516. // malloc(). This checker tracks symbols returned as out-parameters.
  517. //
  518. // When a function is evaluated conservatively, the out-parameter's pointee
  519. // base region gets invalidated with a SymbolConjured. If the base region is
  520. // larger than the region we're interested in, the value we're interested in
  521. // would be SymbolDerived based on that SymbolConjured. However, such
  522. // SymbolDerived will never be listed in the Escaped set when the base
  523. // region is invalidated because ExprEngine doesn't know which symbols
  524. // were derived from a given symbol, while there can be infinitely many
  525. // valid symbols derived from any given symbol.
  526. //
  527. // Hence the extra boilerplate: remove the derived symbol when its parent
  528. // symbol escapes.
  529. //
  530. if (const auto *SD = dyn_cast<SymbolDerived>(Sym)) {
  531. SymbolRef ParentSym = SD->getParentSymbol();
  532. if (Escaped.count(ParentSym))
  533. State = State->remove<AllocatedData>(Sym);
  534. }
  535. }
  536. return State;
  537. }
  538. PathDiagnosticPieceRef
  539. MacOSKeychainAPIChecker::SecKeychainBugVisitor::VisitNode(
  540. const ExplodedNode *N, BugReporterContext &BRC,
  541. PathSensitiveBugReport &BR) {
  542. const AllocationState *AS = N->getState()->get<AllocatedData>(Sym);
  543. if (!AS)
  544. return nullptr;
  545. const AllocationState *ASPrev =
  546. N->getFirstPred()->getState()->get<AllocatedData>(Sym);
  547. if (ASPrev)
  548. return nullptr;
  549. // (!ASPrev && AS) ~ We started tracking symbol in node N, it must be the
  550. // allocation site.
  551. const CallExpr *CE =
  552. cast<CallExpr>(N->getLocation().castAs<StmtPoint>().getStmt());
  553. const FunctionDecl *funDecl = CE->getDirectCallee();
  554. assert(funDecl && "We do not support indirect function calls as of now.");
  555. StringRef funName = funDecl->getName();
  556. // Get the expression of the corresponding argument.
  557. unsigned Idx = getTrackedFunctionIndex(funName, true);
  558. assert(Idx != InvalidIdx && "This should be a call to an allocator.");
  559. const Expr *ArgExpr = CE->getArg(FunctionsToTrack[Idx].Param);
  560. PathDiagnosticLocation Pos(ArgExpr, BRC.getSourceManager(),
  561. N->getLocationContext());
  562. return std::make_shared<PathDiagnosticEventPiece>(Pos,
  563. "Data is allocated here.");
  564. }
  565. void MacOSKeychainAPIChecker::printState(raw_ostream &Out,
  566. ProgramStateRef State,
  567. const char *NL,
  568. const char *Sep) const {
  569. AllocatedDataTy AMap = State->get<AllocatedData>();
  570. if (!AMap.isEmpty()) {
  571. Out << Sep << "KeychainAPIChecker :" << NL;
  572. for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
  573. I.getKey()->dumpToStream(Out);
  574. }
  575. }
  576. }
  577. void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) {
  578. mgr.registerChecker<MacOSKeychainAPIChecker>();
  579. }
  580. bool ento::shouldRegisterMacOSKeychainAPIChecker(const CheckerManager &mgr) {
  581. return true;
  582. }