2011-08-05 01:28:06 +08:00
|
|
|
//==--- MacOSKeychainAPIChecker.cpp ------------------------------*- C++ -*-==//
|
2011-08-02 06:40:01 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This checker flags misuses of KeyChainAPI. In particular, the password data
|
|
|
|
// allocated/returned by SecKeychainItemCopyContent,
|
|
|
|
// SecKeychainFindGenericPassword, SecKeychainFindInternetPassword functions has
|
|
|
|
// to be freed using a call to SecKeychainItemFreeContent.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-08-02 06:40:01 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-08-02 06:40:01 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>,
|
|
|
|
check::PostStmt<CallExpr>,
|
2017-01-13 08:50:41 +08:00
|
|
|
check::DeadSymbols,
|
|
|
|
eval::Assume> {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2011-08-04 08:26:57 +08:00
|
|
|
|
2011-08-02 06:40:01 +08:00
|
|
|
public:
|
2011-08-13 05:14:26 +08:00
|
|
|
/// AllocationState is a part of the checker specific state together with the
|
|
|
|
/// MemRegion corresponding to the allocated data.
|
|
|
|
struct AllocationState {
|
|
|
|
/// The index of the allocator function.
|
|
|
|
unsigned int AllocatorIdx;
|
2011-08-25 08:59:06 +08:00
|
|
|
SymbolRef Region;
|
2011-08-13 05:14:26 +08:00
|
|
|
|
|
|
|
AllocationState(const Expr *E, unsigned int Idx, SymbolRef R) :
|
|
|
|
AllocatorIdx(Idx),
|
2011-08-25 08:59:06 +08:00
|
|
|
Region(R) {}
|
2011-08-13 05:14:26 +08:00
|
|
|
|
|
|
|
bool operator==(const AllocationState &X) const {
|
2011-08-25 08:59:06 +08:00
|
|
|
return (AllocatorIdx == X.AllocatorIdx &&
|
|
|
|
Region == X.Region);
|
2011-08-13 05:14:26 +08:00
|
|
|
}
|
2011-08-25 08:59:06 +08:00
|
|
|
|
2011-08-13 05:14:26 +08:00
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(AllocatorIdx);
|
2011-08-25 08:59:06 +08:00
|
|
|
ID.AddPointer(Region);
|
2011-08-13 05:14:26 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-02 06:40:01 +08:00
|
|
|
void checkPreStmt(const CallExpr *S, CheckerContext &C) const;
|
|
|
|
void checkPostStmt(const CallExpr *S, CheckerContext &C) const;
|
2011-08-13 05:56:43 +08:00
|
|
|
void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
|
2017-01-13 08:50:41 +08:00
|
|
|
ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
|
|
|
|
bool Assumption) const;
|
|
|
|
void printState(raw_ostream &Out, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const;
|
2011-08-02 06:40:01 +08:00
|
|
|
|
|
|
|
private:
|
2011-08-25 05:58:55 +08:00
|
|
|
typedef std::pair<SymbolRef, const AllocationState*> AllocationPair;
|
2013-01-13 03:30:44 +08:00
|
|
|
typedef SmallVector<AllocationPair, 2> AllocationPairVec;
|
2011-08-25 04:52:46 +08:00
|
|
|
|
|
|
|
enum APIKind {
|
2011-08-24 08:06:27 +08:00
|
|
|
/// Denotes functions tracked by this checker.
|
|
|
|
ValidAPI = 0,
|
|
|
|
/// The functions commonly/mistakenly used in place of the given API.
|
|
|
|
ErrorAPI = 1,
|
|
|
|
/// The functions which may allocate the data. These are tracked to reduce
|
|
|
|
/// the false alarm rate.
|
|
|
|
PossibleAPI = 2
|
|
|
|
};
|
2011-08-05 01:28:06 +08:00
|
|
|
/// Stores the information about the allocator and deallocator functions -
|
|
|
|
/// these are the functions the checker is tracking.
|
|
|
|
struct ADFunctionInfo {
|
|
|
|
const char* Name;
|
|
|
|
unsigned int Param;
|
|
|
|
unsigned int DeallocatorIdx;
|
2011-08-24 08:06:27 +08:00
|
|
|
APIKind Kind;
|
2011-08-05 01:28:06 +08:00
|
|
|
};
|
|
|
|
static const unsigned InvalidIdx = 100000;
|
2011-08-24 08:06:27 +08:00
|
|
|
static const unsigned FunctionsToTrackSize = 8;
|
2011-08-05 01:28:06 +08:00
|
|
|
static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize];
|
2011-08-06 07:52:45 +08:00
|
|
|
/// The value, which represents no error return value for allocator functions.
|
|
|
|
static const unsigned NoErr = 0;
|
2011-08-05 01:28:06 +08:00
|
|
|
|
|
|
|
/// Given the function name, returns the index of the allocator/deallocator
|
|
|
|
/// function.
|
2011-08-25 04:52:46 +08:00
|
|
|
static unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator);
|
2011-08-04 08:26:57 +08:00
|
|
|
|
|
|
|
inline void initBugType() const {
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BugType(this, "Improper use of SecKeychain API",
|
2013-04-04 03:28:22 +08:00
|
|
|
"API Misuse (Apple)"));
|
2011-08-04 08:26:57 +08:00
|
|
|
}
|
2011-08-13 05:56:43 +08:00
|
|
|
|
2011-08-25 08:32:42 +08:00
|
|
|
void generateDeallocatorMismatchReport(const AllocationPair &AP,
|
2011-08-24 07:47:36 +08:00
|
|
|
const Expr *ArgExpr,
|
2011-08-25 08:32:42 +08:00
|
|
|
CheckerContext &C) const;
|
2011-08-24 07:47:36 +08:00
|
|
|
|
2012-02-24 06:53:29 +08:00
|
|
|
/// Find the allocation site for Sym on the path leading to the node N.
|
2013-01-08 08:25:29 +08:00
|
|
|
const ExplodedNode *getAllocationNode(const ExplodedNode *N, SymbolRef Sym,
|
|
|
|
CheckerContext &C) const;
|
2012-02-24 06:53:29 +08:00
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
std::unique_ptr<BugReport> generateAllocatedDataNotReleasedReport(
|
|
|
|
const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const;
|
2011-08-13 05:56:43 +08:00
|
|
|
|
2012-03-09 09:13:14 +08:00
|
|
|
/// Mark an AllocationPair interesting for diagnostic reporting.
|
|
|
|
void markInteresting(BugReport *R, const AllocationPair &AP) const {
|
|
|
|
R->markInteresting(AP.first);
|
|
|
|
R->markInteresting(AP.second->Region);
|
|
|
|
}
|
2011-08-13 05:56:43 +08:00
|
|
|
|
2011-08-25 04:52:46 +08:00
|
|
|
/// The bug visitor which allows us to print extra diagnostics along the
|
|
|
|
/// BugReport path. For example, showing the allocation site of the leaked
|
|
|
|
/// region.
|
2012-03-24 10:45:35 +08:00
|
|
|
class SecKeychainBugVisitor
|
|
|
|
: public BugReporterVisitorImpl<SecKeychainBugVisitor> {
|
2011-08-25 04:52:46 +08:00
|
|
|
protected:
|
|
|
|
// The allocated region symbol tracked by the main analysis.
|
|
|
|
SymbolRef Sym;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SecKeychainBugVisitor(SymbolRef S) : Sym(S) {}
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const override {
|
2011-08-25 04:52:46 +08:00
|
|
|
static int X = 0;
|
|
|
|
ID.AddPointer(&X);
|
|
|
|
ID.AddPointer(Sym);
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR) override;
|
2011-08-25 04:52:46 +08:00
|
|
|
};
|
2011-08-02 06:40:01 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-08-02 06:40:01 +08:00
|
|
|
|
2011-08-16 07:23:15 +08:00
|
|
|
/// ProgramState traits to store the currently allocated (and not yet freed)
|
|
|
|
/// symbols. This is a map from the allocated content symbol to the
|
|
|
|
/// corresponding AllocationState.
|
2012-11-02 09:54:06 +08:00
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(AllocatedData,
|
|
|
|
SymbolRef,
|
|
|
|
MacOSKeychainAPIChecker::AllocationState)
|
2011-08-02 06:40:01 +08:00
|
|
|
|
2011-08-04 08:26:57 +08:00
|
|
|
static bool isEnclosingFunctionParam(const Expr *E) {
|
|
|
|
E = E->IgnoreParenCasts();
|
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
|
|
|
|
const ValueDecl *VD = DRE->getDecl();
|
|
|
|
if (isa<ImplicitParamDecl>(VD) || isa<ParmVarDecl>(VD))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-05 01:28:06 +08:00
|
|
|
const MacOSKeychainAPIChecker::ADFunctionInfo
|
|
|
|
MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = {
|
2011-08-24 08:06:27 +08:00
|
|
|
{"SecKeychainItemCopyContent", 4, 3, ValidAPI}, // 0
|
|
|
|
{"SecKeychainFindGenericPassword", 6, 3, ValidAPI}, // 1
|
|
|
|
{"SecKeychainFindInternetPassword", 13, 3, ValidAPI}, // 2
|
|
|
|
{"SecKeychainItemFreeContent", 1, InvalidIdx, ValidAPI}, // 3
|
|
|
|
{"SecKeychainItemCopyAttributesAndData", 5, 5, ValidAPI}, // 4
|
|
|
|
{"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx, ValidAPI}, // 5
|
|
|
|
{"free", 0, InvalidIdx, ErrorAPI}, // 6
|
|
|
|
{"CFStringCreateWithBytesNoCopy", 1, InvalidIdx, PossibleAPI}, // 7
|
2011-08-05 01:28:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name,
|
2011-08-25 04:52:46 +08:00
|
|
|
bool IsAllocator) {
|
2011-08-05 01:28:06 +08:00
|
|
|
for (unsigned I = 0; I < FunctionsToTrackSize; ++I) {
|
|
|
|
ADFunctionInfo FI = FunctionsToTrack[I];
|
|
|
|
if (FI.Name != Name)
|
|
|
|
continue;
|
|
|
|
// Make sure the function is of the right type (allocator vs deallocator).
|
|
|
|
if (IsAllocator && (FI.DeallocatorIdx == InvalidIdx))
|
|
|
|
return InvalidIdx;
|
|
|
|
if (!IsAllocator && (FI.DeallocatorIdx != InvalidIdx))
|
|
|
|
return InvalidIdx;
|
|
|
|
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
// The function is not tracked.
|
|
|
|
return InvalidIdx;
|
|
|
|
}
|
|
|
|
|
2011-08-13 05:14:26 +08:00
|
|
|
static bool isBadDeallocationArgument(const MemRegion *Arg) {
|
2012-03-11 08:08:24 +08:00
|
|
|
if (!Arg)
|
|
|
|
return false;
|
2015-12-28 21:06:58 +08:00
|
|
|
return isa<AllocaRegion>(Arg) || isa<BlockDataRegion>(Arg) ||
|
|
|
|
isa<TypedRegion>(Arg);
|
2011-08-13 05:14:26 +08:00
|
|
|
}
|
2012-03-11 08:08:24 +08:00
|
|
|
|
2011-08-05 08:37:00 +08:00
|
|
|
/// Given the address expression, retrieve the value it's pointing to. Assume
|
2011-08-13 05:14:26 +08:00
|
|
|
/// that value is itself an address, and return the corresponding symbol.
|
|
|
|
static SymbolRef getAsPointeeSymbol(const Expr *Expr,
|
|
|
|
CheckerContext &C) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal ArgV = State->getSVal(Expr, C.getLocationContext());
|
2011-08-06 07:52:45 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::MemRegionVal> X = ArgV.getAs<loc::MemRegionVal>()) {
|
2011-08-05 08:37:00 +08:00
|
|
|
StoreManager& SM = C.getStoreManager();
|
2012-03-11 08:08:24 +08:00
|
|
|
SymbolRef sym = SM.getBinding(State->getStore(), *X).getAsLocSymbol();
|
|
|
|
if (sym)
|
|
|
|
return sym;
|
2011-08-05 08:37:00 +08:00
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-08-05 08:37:00 +08:00
|
|
|
}
|
|
|
|
|
2011-08-24 07:47:36 +08:00
|
|
|
// Report deallocator mismatch. Remove the region from tracking - reporting a
|
|
|
|
// missing free error after this one is redundant.
|
|
|
|
void MacOSKeychainAPIChecker::
|
2011-08-25 08:32:42 +08:00
|
|
|
generateDeallocatorMismatchReport(const AllocationPair &AP,
|
2011-08-24 07:47:36 +08:00
|
|
|
const Expr *ArgExpr,
|
2011-08-25 08:32:42 +08:00
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2011-08-25 08:32:42 +08:00
|
|
|
State = State->remove<AllocatedData>(AP.first);
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode(State);
|
2011-08-24 07:47:36 +08:00
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
initBugType();
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<80> sbuf;
|
2011-08-24 07:47:36 +08:00
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
2011-08-25 08:32:42 +08:00
|
|
|
unsigned int PDeallocIdx =
|
|
|
|
FunctionsToTrack[AP.second->AllocatorIdx].DeallocatorIdx;
|
2011-08-24 07:47:36 +08:00
|
|
|
|
|
|
|
os << "Deallocator doesn't match the allocator: '"
|
|
|
|
<< FunctionsToTrack[PDeallocIdx].Name << "' should be used.";
|
2015-06-23 21:15:32 +08:00
|
|
|
auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N);
|
2014-09-05 07:54:33 +08:00
|
|
|
Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(AP.first));
|
2011-08-24 07:47:36 +08:00
|
|
|
Report->addRange(ArgExpr->getSourceRange());
|
2015-06-23 21:15:32 +08:00
|
|
|
markInteresting(Report.get(), AP);
|
|
|
|
C.emitReport(std::move(Report));
|
2011-08-24 07:47:36 +08:00
|
|
|
}
|
|
|
|
|
2011-08-02 06:40:01 +08:00
|
|
|
void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
2011-08-05 08:37:00 +08:00
|
|
|
unsigned idx = InvalidIdx;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2011-08-02 06:40:01 +08:00
|
|
|
|
2012-07-11 07:13:01 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
|
|
|
if (!FD || FD->getKind() != Decl::Function)
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-07-11 07:13:01 +08:00
|
|
|
StringRef funName = C.getCalleeName(FD);
|
2011-12-01 13:57:37 +08:00
|
|
|
if (funName.empty())
|
2011-08-02 06:40:01 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-05 08:37:00 +08:00
|
|
|
// If it is a call to an allocator function, it could be a double allocation.
|
|
|
|
idx = getTrackedFunctionIndex(funName, true);
|
|
|
|
if (idx != InvalidIdx) {
|
2015-02-05 09:02:56 +08:00
|
|
|
unsigned paramIdx = FunctionsToTrack[idx].Param;
|
|
|
|
if (CE->getNumArgs() <= paramIdx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Expr *ArgExpr = CE->getArg(paramIdx);
|
2011-08-13 05:14:26 +08:00
|
|
|
if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C))
|
2011-08-05 08:37:00 +08:00
|
|
|
if (const AllocationState *AS = State->get<AllocatedData>(V)) {
|
2017-01-13 08:50:41 +08:00
|
|
|
// Remove the value from the state. The new symbol will be added for
|
|
|
|
// tracking when the second allocator is processed in checkPostStmt().
|
|
|
|
State = State->remove<AllocatedData>(V);
|
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode(State);
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
initBugType();
|
|
|
|
SmallString<128> sbuf;
|
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
|
|
|
unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
|
|
|
|
os << "Allocated data should be released before another call to "
|
|
|
|
<< "the allocator: missing a call to '"
|
|
|
|
<< FunctionsToTrack[DIdx].Name
|
|
|
|
<< "'.";
|
|
|
|
auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N);
|
|
|
|
Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(V));
|
|
|
|
Report->addRange(ArgExpr->getSourceRange());
|
|
|
|
Report->markInteresting(AS->Region);
|
|
|
|
C.emitReport(std::move(Report));
|
2011-08-05 08:37:00 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a call to one of deallocator functions?
|
|
|
|
idx = getTrackedFunctionIndex(funName, false);
|
2011-08-05 01:28:06 +08:00
|
|
|
if (idx == InvalidIdx)
|
2011-08-04 08:31:38 +08:00
|
|
|
return;
|
|
|
|
|
2015-02-05 09:02:56 +08:00
|
|
|
unsigned paramIdx = FunctionsToTrack[idx].Param;
|
|
|
|
if (CE->getNumArgs() <= paramIdx)
|
|
|
|
return;
|
|
|
|
|
2011-08-13 05:14:26 +08:00
|
|
|
// Check the argument to the deallocator.
|
2015-02-05 09:02:56 +08:00
|
|
|
const Expr *ArgExpr = CE->getArg(paramIdx);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal ArgSVal = State->getSVal(ArgExpr, C.getLocationContext());
|
2011-08-13 05:14:26 +08:00
|
|
|
|
|
|
|
// Undef is reported by another checker.
|
|
|
|
if (ArgSVal.isUndef())
|
|
|
|
return;
|
|
|
|
|
2012-03-11 08:08:24 +08:00
|
|
|
SymbolRef ArgSM = ArgSVal.getAsLocSymbol();
|
2011-08-13 05:14:26 +08:00
|
|
|
|
|
|
|
// If the argument is coming from the heap, globals, or unknown, do not
|
|
|
|
// report it.
|
2012-03-11 08:08:24 +08:00
|
|
|
bool RegionArgIsBad = false;
|
|
|
|
if (!ArgSM) {
|
|
|
|
if (!isBadDeallocationArgument(ArgSVal.getAsRegion()))
|
|
|
|
return;
|
|
|
|
RegionArgIsBad = true;
|
|
|
|
}
|
2011-08-04 08:31:38 +08:00
|
|
|
|
2011-08-24 08:06:27 +08:00
|
|
|
// Is the argument to the call being tracked?
|
|
|
|
const AllocationState *AS = State->get<AllocatedData>(ArgSM);
|
2017-01-13 08:50:41 +08:00
|
|
|
if (!AS)
|
2011-08-24 08:06:27 +08:00
|
|
|
return;
|
2017-01-13 08:50:41 +08:00
|
|
|
|
|
|
|
// TODO: We might want to report double free here.
|
2011-08-16 07:23:15 +08:00
|
|
|
// (that would involve tracking all the freed symbols in the checker state).
|
2017-01-13 08:50:41 +08:00
|
|
|
if (RegionArgIsBad) {
|
2011-08-04 08:31:38 +08:00
|
|
|
// It is possible that this is a false positive - the argument might
|
|
|
|
// have entered as an enclosing function parameter.
|
|
|
|
if (isEnclosingFunctionParam(ArgExpr))
|
2011-08-02 06:40:01 +08:00
|
|
|
return;
|
2011-08-04 08:26:57 +08:00
|
|
|
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode(State);
|
2011-08-04 08:31:38 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
initBugType();
|
2015-06-23 21:15:32 +08:00
|
|
|
auto Report = llvm::make_unique<BugReport>(
|
|
|
|
*BT, "Trying to free data which has not been allocated.", N);
|
2011-08-04 08:31:38 +08:00
|
|
|
Report->addRange(ArgExpr->getSourceRange());
|
2012-03-09 09:13:14 +08:00
|
|
|
if (AS)
|
|
|
|
Report->markInteresting(AS->Region);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(Report));
|
2011-08-05 01:28:06 +08:00
|
|
|
return;
|
2011-08-02 06:40:01 +08:00
|
|
|
}
|
2011-08-04 08:31:38 +08:00
|
|
|
|
2011-08-24 08:06:27 +08:00
|
|
|
// Process functions which might deallocate.
|
|
|
|
if (FunctionsToTrack[idx].Kind == PossibleAPI) {
|
|
|
|
|
|
|
|
if (funName == "CFStringCreateWithBytesNoCopy") {
|
|
|
|
const Expr *DeallocatorExpr = CE->getArg(5)->IgnoreParenCasts();
|
|
|
|
// NULL ~ default deallocator, so warn.
|
|
|
|
if (DeallocatorExpr->isNullPointerConstant(C.getASTContext(),
|
|
|
|
Expr::NPC_ValueDependentIsNotNull)) {
|
2011-08-25 08:32:42 +08:00
|
|
|
const AllocationPair AP = std::make_pair(ArgSM, AS);
|
|
|
|
generateDeallocatorMismatchReport(AP, ArgExpr, C);
|
2011-08-24 08:06:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// One of the default allocators, so warn.
|
|
|
|
if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(DeallocatorExpr)) {
|
|
|
|
StringRef DeallocatorName = DE->getFoundDecl()->getName();
|
|
|
|
if (DeallocatorName == "kCFAllocatorDefault" ||
|
|
|
|
DeallocatorName == "kCFAllocatorSystemDefault" ||
|
|
|
|
DeallocatorName == "kCFAllocatorMalloc") {
|
2011-08-25 08:32:42 +08:00
|
|
|
const AllocationPair AP = std::make_pair(ArgSM, AS);
|
|
|
|
generateDeallocatorMismatchReport(AP, ArgExpr, C);
|
2011-08-24 08:06:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If kCFAllocatorNull, which does not deallocate, we still have to
|
2013-01-08 03:13:00 +08:00
|
|
|
// find the deallocator.
|
|
|
|
if (DE->getFoundDecl()->getName() == "kCFAllocatorNull")
|
2011-08-24 08:06:27 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-01-08 03:13:00 +08:00
|
|
|
// In all other cases, assume the user supplied a correct deallocator
|
|
|
|
// that will free memory so stop tracking.
|
|
|
|
State = State->remove<AllocatedData>(ArgSM);
|
|
|
|
C.addTransition(State);
|
|
|
|
return;
|
2011-08-24 08:06:27 +08:00
|
|
|
}
|
2013-01-08 03:13:00 +08:00
|
|
|
|
|
|
|
llvm_unreachable("We know of no other possible APIs.");
|
2011-08-24 08:06:27 +08:00
|
|
|
}
|
|
|
|
|
2011-08-16 07:23:15 +08:00
|
|
|
// The call is deallocating a value we previously allocated, so remove it
|
|
|
|
// from the next state.
|
|
|
|
State = State->remove<AllocatedData>(ArgSM);
|
|
|
|
|
2011-08-24 07:47:36 +08:00
|
|
|
// Check if the proper deallocator is used.
|
2011-08-05 05:53:01 +08:00
|
|
|
unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
|
2011-08-24 08:06:27 +08:00
|
|
|
if (PDeallocIdx != idx || (FunctionsToTrack[idx].Kind == ErrorAPI)) {
|
2011-08-25 08:32:42 +08:00
|
|
|
const AllocationPair AP = std::make_pair(ArgSM, AS);
|
|
|
|
generateDeallocatorMismatchReport(AP, ArgExpr, C);
|
2011-08-05 05:53:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(State);
|
2011-08-02 06:40:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2012-07-11 07:13:01 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
|
|
|
if (!FD || FD->getKind() != Decl::Function)
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef funName = C.getCalleeName(FD);
|
2011-08-02 06:40:01 +08:00
|
|
|
|
|
|
|
// If a value has been allocated, add it to the set for tracking.
|
2011-08-05 01:28:06 +08:00
|
|
|
unsigned idx = getTrackedFunctionIndex(funName, true);
|
|
|
|
if (idx == InvalidIdx)
|
2011-08-04 08:31:38 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-05 01:28:06 +08:00
|
|
|
const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
|
2011-08-13 06:47:22 +08:00
|
|
|
// If the argument entered as an enclosing function parameter, skip it to
|
|
|
|
// avoid false positives.
|
2012-02-21 08:00:44 +08:00
|
|
|
if (isEnclosingFunctionParam(ArgExpr) &&
|
2014-05-27 10:45:47 +08:00
|
|
|
C.getLocationContext()->getParent() == nullptr)
|
2011-08-13 06:47:22 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-13 05:14:26 +08:00
|
|
|
if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) {
|
|
|
|
// If the argument points to something that's not a symbolic region, it
|
|
|
|
// can be:
|
2011-08-04 08:31:38 +08:00
|
|
|
// - unknown (cannot reason about it)
|
|
|
|
// - undefined (already reported by other checker)
|
2011-08-05 01:28:06 +08:00
|
|
|
// - constant (null - should not be tracked,
|
|
|
|
// other constant will generate a compiler warning)
|
2011-08-04 08:31:38 +08:00
|
|
|
// - goto (should be reported by other checker)
|
2011-08-13 05:56:43 +08:00
|
|
|
|
|
|
|
// The call return value symbol should stay alive for as long as the
|
|
|
|
// allocated value symbol, since our diagnostics depend on the value
|
|
|
|
// returned by the call. Ex: Data should only be freed if noErr was
|
|
|
|
// returned during allocation.)
|
2012-01-07 06:09:28 +08:00
|
|
|
SymbolRef RetStatusSymbol =
|
|
|
|
State->getSVal(CE, C.getLocationContext()).getAsSymbol();
|
2011-08-13 05:56:43 +08:00
|
|
|
C.getSymbolManager().addSymbolDependency(V, RetStatusSymbol);
|
2011-08-06 07:52:45 +08:00
|
|
|
|
2011-08-13 05:56:43 +08:00
|
|
|
// Track the allocated value in the checker state.
|
|
|
|
State = State->set<AllocatedData>(V, AllocationState(ArgExpr, idx,
|
|
|
|
RetStatusSymbol));
|
|
|
|
assert(State);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(State);
|
2011-08-02 06:40:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-28 11:07:06 +08:00
|
|
|
// TODO: This logic is the same as in Malloc checker.
|
2013-01-08 08:25:29 +08:00
|
|
|
const ExplodedNode *
|
|
|
|
MacOSKeychainAPIChecker::getAllocationNode(const ExplodedNode *N,
|
2012-02-24 06:53:29 +08:00
|
|
|
SymbolRef Sym,
|
|
|
|
CheckerContext &C) const {
|
2012-02-28 11:07:06 +08:00
|
|
|
const LocationContext *LeakContext = N->getLocationContext();
|
2012-02-24 06:53:29 +08:00
|
|
|
// Walk the ExplodedGraph backwards and find the first node that referred to
|
|
|
|
// the tracked symbol.
|
|
|
|
const ExplodedNode *AllocNode = N;
|
|
|
|
|
|
|
|
while (N) {
|
|
|
|
if (!N->getState()->get<AllocatedData>(Sym))
|
|
|
|
break;
|
2015-02-05 09:02:53 +08:00
|
|
|
// Allocation node, is the last node in the current or parent context in
|
|
|
|
// which the symbol was tracked.
|
|
|
|
const LocationContext *NContext = N->getLocationContext();
|
|
|
|
if (NContext == LeakContext ||
|
|
|
|
NContext->isParentOf(LeakContext))
|
2012-02-28 11:07:06 +08:00
|
|
|
AllocNode = N;
|
2014-05-27 10:45:47 +08:00
|
|
|
N = N->pred_empty() ? nullptr : *(N->pred_begin());
|
2012-02-24 06:53:29 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 08:25:29 +08:00
|
|
|
return AllocNode;
|
2012-02-24 06:53:29 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
std::unique_ptr<BugReport>
|
|
|
|
MacOSKeychainAPIChecker::generateAllocatedDataNotReleasedReport(
|
|
|
|
const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const {
|
2011-08-25 05:58:55 +08:00
|
|
|
const ADFunctionInfo &FI = FunctionsToTrack[AP.second->AllocatorIdx];
|
2011-08-13 05:56:43 +08:00
|
|
|
initBugType();
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<70> sbuf;
|
2011-08-16 02:42:00 +08:00
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
2011-08-13 05:56:43 +08:00
|
|
|
os << "Allocated data is not released: missing a call to '"
|
|
|
|
<< FunctionsToTrack[FI.DeallocatorIdx].Name << "'.";
|
2012-02-24 06:53:29 +08:00
|
|
|
|
|
|
|
// Most bug reports are cached at the location where they occurred.
|
|
|
|
// With leaks, we want to unique them by the location where they were
|
|
|
|
// allocated, and only report a single path.
|
2012-02-28 11:07:06 +08:00
|
|
|
PathDiagnosticLocation LocUsedForUniqueing;
|
2013-01-08 08:25:29 +08:00
|
|
|
const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C);
|
2016-08-18 15:54:50 +08:00
|
|
|
const Stmt *AllocStmt = PathDiagnosticLocation::getStmt(AllocNode);
|
2013-01-08 08:25:29 +08:00
|
|
|
|
|
|
|
if (AllocStmt)
|
2012-02-28 11:07:06 +08:00
|
|
|
LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,
|
2013-01-08 08:25:29 +08:00
|
|
|
C.getSourceManager(),
|
|
|
|
AllocNode->getLocationContext());
|
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
auto Report =
|
|
|
|
llvm::make_unique<BugReport>(*BT, os.str(), N, LocUsedForUniqueing,
|
|
|
|
AllocNode->getLocationContext()->getDecl());
|
2012-02-24 06:53:29 +08:00
|
|
|
|
2014-09-05 07:54:33 +08:00
|
|
|
Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(AP.first));
|
2015-06-23 21:15:32 +08:00
|
|
|
markInteresting(Report.get(), AP);
|
2011-08-13 05:56:43 +08:00
|
|
|
return Report;
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:41 +08:00
|
|
|
/// If the return symbol is assumed to be error, remove the allocated info
|
|
|
|
/// from consideration.
|
|
|
|
ProgramStateRef MacOSKeychainAPIChecker::evalAssume(ProgramStateRef State,
|
|
|
|
SVal Cond,
|
|
|
|
bool Assumption) const {
|
|
|
|
AllocatedDataTy AMap = State->get<AllocatedData>();
|
|
|
|
if (AMap.isEmpty())
|
|
|
|
return State;
|
|
|
|
|
|
|
|
auto *CondBSE = dyn_cast_or_null<BinarySymExpr>(Cond.getAsSymExpr());
|
|
|
|
if (!CondBSE)
|
|
|
|
return State;
|
|
|
|
BinaryOperator::Opcode OpCode = CondBSE->getOpcode();
|
|
|
|
if (OpCode != BO_EQ && OpCode != BO_NE)
|
|
|
|
return State;
|
|
|
|
|
|
|
|
// Match for a restricted set of patterns for cmparison of error codes.
|
|
|
|
// Note, the comparisons of type '0 == st' are transformed into SymIntExpr.
|
|
|
|
SymbolRef ReturnSymbol = nullptr;
|
|
|
|
if (auto *SIE = dyn_cast<SymIntExpr>(CondBSE)) {
|
|
|
|
const llvm::APInt &RHS = SIE->getRHS();
|
|
|
|
bool ErrorIsReturned = (OpCode == BO_EQ && RHS != NoErr) ||
|
|
|
|
(OpCode == BO_NE && RHS == NoErr);
|
|
|
|
if (!Assumption)
|
|
|
|
ErrorIsReturned = !ErrorIsReturned;
|
|
|
|
if (ErrorIsReturned)
|
|
|
|
ReturnSymbol = SIE->getLHS();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReturnSymbol)
|
|
|
|
for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
|
|
|
|
if (ReturnSymbol == I->second.Region)
|
|
|
|
State = State->remove<AllocatedData>(I->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
2011-08-13 05:56:43 +08:00
|
|
|
void MacOSKeychainAPIChecker::checkDeadSymbols(SymbolReaper &SR,
|
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2017-01-13 08:50:41 +08:00
|
|
|
AllocatedDataTy AMap = State->get<AllocatedData>();
|
|
|
|
if (AMap.isEmpty())
|
2011-08-13 05:56:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
bool Changed = false;
|
2011-08-25 04:52:46 +08:00
|
|
|
AllocationPairVec Errors;
|
2017-01-13 08:50:41 +08:00
|
|
|
for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
|
|
|
|
if (!SR.isDead(I->first))
|
2011-08-13 05:56:43 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
State = State->remove<AllocatedData>(I->first);
|
2017-01-13 08:50:41 +08:00
|
|
|
// If the allocated symbol is null do not report.
|
2012-11-01 08:18:27 +08:00
|
|
|
ConstraintManager &CMgr = State->getConstraintManager();
|
|
|
|
ConditionTruthVal AllocFailed = CMgr.isNull(State, I.getKey());
|
2017-01-13 08:50:41 +08:00
|
|
|
if (AllocFailed.isConstrainedTrue())
|
2011-08-13 05:56:43 +08:00
|
|
|
continue;
|
2011-08-25 05:58:55 +08:00
|
|
|
Errors.push_back(std::make_pair(I->first, &I->second));
|
2011-08-13 05:56:43 +08:00
|
|
|
}
|
2012-02-24 06:53:29 +08:00
|
|
|
if (!Changed) {
|
|
|
|
// Generate the new, cleaned up state.
|
|
|
|
C.addTransition(State);
|
2011-08-13 05:56:43 +08:00
|
|
|
return;
|
2012-02-24 06:53:29 +08:00
|
|
|
}
|
2011-08-13 05:56:43 +08:00
|
|
|
|
2014-02-18 02:25:34 +08:00
|
|
|
static CheckerProgramPointTag Tag(this, "DeadSymbolsLeak");
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode(C.getState(), &Tag);
|
|
|
|
if (!N)
|
|
|
|
return;
|
2011-08-13 05:56:43 +08:00
|
|
|
|
|
|
|
// Generate the error reports.
|
2015-09-24 22:48:49 +08:00
|
|
|
for (const auto &P : Errors)
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(generateAllocatedDataNotReleasedReport(P, N, C));
|
2012-02-24 06:53:29 +08:00
|
|
|
|
|
|
|
// Generate the new, cleaned up state.
|
|
|
|
C.addTransition(State, N);
|
2011-08-13 05:56:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticPiece>
|
|
|
|
MacOSKeychainAPIChecker::SecKeychainBugVisitor::VisitNode(
|
|
|
|
const ExplodedNode *N, const ExplodedNode *PrevN, BugReporterContext &BRC,
|
|
|
|
BugReport &BR) {
|
2011-08-25 04:52:46 +08:00
|
|
|
const AllocationState *AS = N->getState()->get<AllocatedData>(Sym);
|
|
|
|
if (!AS)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-08-25 04:52:46 +08:00
|
|
|
const AllocationState *ASPrev = PrevN->getState()->get<AllocatedData>(Sym);
|
|
|
|
if (ASPrev)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-08-25 04:52:46 +08:00
|
|
|
|
|
|
|
// (!ASPrev && AS) ~ We started tracking symbol in node N, it must be the
|
|
|
|
// allocation site.
|
2013-02-22 06:23:56 +08:00
|
|
|
const CallExpr *CE =
|
|
|
|
cast<CallExpr>(N->getLocation().castAs<StmtPoint>().getStmt());
|
2011-08-25 04:52:46 +08:00
|
|
|
const FunctionDecl *funDecl = CE->getDirectCallee();
|
|
|
|
assert(funDecl && "We do not support indirect function calls as of now.");
|
|
|
|
StringRef funName = funDecl->getName();
|
|
|
|
|
|
|
|
// Get the expression of the corresponding argument.
|
|
|
|
unsigned Idx = getTrackedFunctionIndex(funName, true);
|
|
|
|
assert(Idx != InvalidIdx && "This should be a call to an allocator.");
|
|
|
|
const Expr *ArgExpr = CE->getArg(FunctionsToTrack[Idx].Param);
|
2011-09-15 09:08:34 +08:00
|
|
|
PathDiagnosticLocation Pos(ArgExpr, BRC.getSourceManager(),
|
|
|
|
N->getLocationContext());
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(Pos,
|
|
|
|
"Data is allocated here.");
|
2011-08-02 06:40:01 +08:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:41 +08:00
|
|
|
void MacOSKeychainAPIChecker::printState(raw_ostream &Out,
|
|
|
|
ProgramStateRef State,
|
|
|
|
const char *NL,
|
|
|
|
const char *Sep) const {
|
|
|
|
|
|
|
|
AllocatedDataTy AMap = State->get<AllocatedData>();
|
|
|
|
|
|
|
|
if (!AMap.isEmpty()) {
|
|
|
|
Out << Sep << "KeychainAPIChecker :" << NL;
|
|
|
|
for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
|
|
|
|
I.getKey()->dumpToStream(Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-02 06:40:01 +08:00
|
|
|
void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<MacOSKeychainAPIChecker>();
|
|
|
|
}
|