#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/AST/ParentMap.h"
using namespace clang;
using namespace ento;
namespace {
class VforkChecker : public Checker<check::PreCall, check::PostCall,
check::Bind, check::PreStmt<ReturnStmt>> {
mutable std::unique_ptr<BuiltinBug> BT;
mutable llvm::SmallSet<const IdentifierInfo *, 10> VforkAllowlist;
mutable const IdentifierInfo *II_vfork;
static bool isChildProcess(const ProgramStateRef State);
bool isVforkCall(const Decl *D, CheckerContext &C) const;
bool isCallExplicitelyAllowed(const IdentifierInfo *II,
CheckerContext &C) const;
void reportBug(const char *What, CheckerContext &C,
const char *Details = nullptr) const;
public:
VforkChecker() : II_vfork(nullptr) {}
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
};
}
REGISTER_TRAIT_WITH_PROGRAMSTATE(VforkResultRegion, const void *)
#define VFORK_RESULT_INVALID 0
#define VFORK_RESULT_NONE ((void *)(uintptr_t)1)
bool VforkChecker::isChildProcess(const ProgramStateRef State) {
return State->get<VforkResultRegion>() != VFORK_RESULT_INVALID;
}
bool VforkChecker::isVforkCall(const Decl *D, CheckerContext &C) const {
auto FD = dyn_cast_or_null<FunctionDecl>(D);
if (!FD || !C.isCLibraryFunction(FD))
return false;
if (!II_vfork) {
ASTContext &AC = C.getASTContext();
II_vfork = &AC.Idents.get("vfork");
}
return FD->getIdentifier() == II_vfork;
}
bool VforkChecker::isCallExplicitelyAllowed(const IdentifierInfo *II,
CheckerContext &C) const {
if (VforkAllowlist.empty()) {
const char *ids[] = {
"_Exit",
"_exit",
"execl",
"execle",
"execlp",
"execv",
"execve",
"execvp",
"execvpe",
nullptr
};
ASTContext &AC = C.getASTContext();
for (const char **id = ids; *id; ++id)
VforkAllowlist.insert(&AC.Idents.get(*id));
}
return VforkAllowlist.count(II);
}
void VforkChecker::reportBug(const char *What, CheckerContext &C,
const char *Details) const {
if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
if (!BT)
BT.reset(new BuiltinBug(this,
"Dangerous construct in a vforked process"));
SmallString<256> buf;
llvm::raw_svector_ostream os(buf);
os << What << " is prohibited after a successful vfork";
if (Details)
os << "; " << Details;
auto Report = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
C.emitReport(std::move(Report));
}
}
void VforkChecker::checkPostCall(const CallEvent &Call,
CheckerContext &C) const {
ProgramStateRef State = C.getState();
if (isChildProcess(State))
return;
if (!isVforkCall(Call.getDecl(), C))
return;
SVal VforkRetVal = Call.getReturnValue();
Optional<DefinedOrUnknownSVal> DVal =
VforkRetVal.getAs<DefinedOrUnknownSVal>();
if (!DVal)
return;
const ParentMap &PM = C.getLocationContext()->getParentMap();
const Stmt *P = PM.getParentIgnoreParenCasts(Call.getOriginExpr());
const VarDecl *LhsDecl;
std::tie(LhsDecl, std::ignore) = parseAssignment(P);
MemRegionManager &M = C.getStoreManager().getRegionManager();
const MemRegion *LhsDeclReg =
LhsDecl
? M.getVarRegion(LhsDecl, C.getLocationContext())
: (const MemRegion *)VFORK_RESULT_NONE;
ProgramStateRef ParentState, ChildState;
std::tie(ParentState, ChildState) = C.getState()->assume(*DVal);
C.addTransition(ParentState);
ChildState = ChildState->set<VforkResultRegion>(LhsDeclReg);
C.addTransition(ChildState);
}
void VforkChecker::checkPreCall(const CallEvent &Call,
CheckerContext &C) const {
ProgramStateRef State = C.getState();
if (isChildProcess(State) &&
!isCallExplicitelyAllowed(Call.getCalleeIdentifier(), C))
reportBug("This function call", C);
}
void VforkChecker::checkBind(SVal L, SVal V, const Stmt *S,
CheckerContext &C) const {
ProgramStateRef State = C.getState();
if (!isChildProcess(State))
return;
const MemRegion *VforkLhs =
static_cast<const MemRegion *>(State->get<VforkResultRegion>());
const MemRegion *MR = L.getAsRegion();
if (!MR || MR == VforkLhs)
return;
reportBug("This assignment", C);
}
void VforkChecker::checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const {
ProgramStateRef State = C.getState();
if (isChildProcess(State))
reportBug("Return", C, "call _exit() instead");
}
void ento::registerVforkChecker(CheckerManager &mgr) {
mgr.registerChecker<VforkChecker>();
}
bool ento::shouldRegisterVforkChecker(const CheckerManager &mgr) {
return true;
}