2015-11-06 19:16:31 +08:00
|
|
|
//===- VforkChecker.cpp -------- Vfork usage checks --------------*- C++ -*-==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-11-06 19:16:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines vfork checker which checks for dangerous uses of vfork.
|
|
|
|
// Vforked process shares memory (including stack) with parent so it's
|
|
|
|
// range of actions is significantly limited: can't write variables,
|
2021-10-30 02:32:03 +08:00
|
|
|
// can't call functions not in the allowed list, etc. For more details, see
|
2015-11-06 19:16:31 +08:00
|
|
|
// http://man7.org/linux/man-pages/man2/vfork.2.html
|
|
|
|
//
|
|
|
|
// This checker checks for prohibited constructs in vforked process.
|
|
|
|
// The state transition diagram:
|
|
|
|
// PARENT ---(vfork() == 0)--> CHILD
|
|
|
|
// |
|
|
|
|
// --(*p = ...)--> bug
|
|
|
|
// |
|
|
|
|
// --foo()--> bug
|
|
|
|
// |
|
|
|
|
// --return--> bug
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2015-11-06 19:16:31 +08:00
|
|
|
#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;
|
2021-10-30 02:32:03 +08:00
|
|
|
mutable llvm::SmallSet<const IdentifierInfo *, 10> VforkAllowlist;
|
2015-11-06 19:16:31 +08:00
|
|
|
mutable const IdentifierInfo *II_vfork;
|
|
|
|
|
|
|
|
static bool isChildProcess(const ProgramStateRef State);
|
|
|
|
|
|
|
|
bool isVforkCall(const Decl *D, CheckerContext &C) const;
|
2021-10-30 02:32:03 +08:00
|
|
|
bool isCallExplicitelyAllowed(const IdentifierInfo *II,
|
|
|
|
CheckerContext &C) const;
|
2015-11-06 19:16:31 +08:00
|
|
|
|
|
|
|
void reportBug(const char *What, CheckerContext &C,
|
2016-01-27 03:01:06 +08:00
|
|
|
const char *Details = nullptr) const;
|
2015-11-06 19:16:31 +08:00
|
|
|
|
|
|
|
public:
|
2016-01-27 03:01:06 +08:00
|
|
|
VforkChecker() : II_vfork(nullptr) {}
|
2015-11-06 19:16:31 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
// This trait holds region of variable that is assigned with vfork's
|
|
|
|
// return value (this is the only region child is allowed to write).
|
|
|
|
// VFORK_RESULT_INVALID means that we are in parent process.
|
|
|
|
// VFORK_RESULT_NONE means that vfork's return value hasn't been assigned.
|
|
|
|
// Other values point to valid regions.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff ok to call function after successful vfork.
|
2021-10-30 02:32:03 +08:00
|
|
|
bool VforkChecker::isCallExplicitelyAllowed(const IdentifierInfo *II,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
if (VforkAllowlist.empty()) {
|
2015-11-06 19:16:31 +08:00
|
|
|
// According to manpage.
|
|
|
|
const char *ids[] = {
|
|
|
|
"_Exit",
|
2020-02-18 13:40:02 +08:00
|
|
|
"_exit",
|
2015-11-06 19:16:31 +08:00
|
|
|
"execl",
|
|
|
|
"execle",
|
2020-02-18 13:40:02 +08:00
|
|
|
"execlp",
|
2015-11-06 19:16:31 +08:00
|
|
|
"execv",
|
2020-02-18 13:40:02 +08:00
|
|
|
"execve",
|
2015-11-06 19:16:31 +08:00
|
|
|
"execvp",
|
|
|
|
"execvpe",
|
2016-01-27 03:01:06 +08:00
|
|
|
nullptr
|
2015-11-06 19:16:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ASTContext &AC = C.getASTContext();
|
|
|
|
for (const char **id = ids; *id; ++id)
|
2021-10-30 02:32:03 +08:00
|
|
|
VforkAllowlist.insert(&AC.Idents.get(*id));
|
2015-11-06 19:16:31 +08:00
|
|
|
}
|
|
|
|
|
2021-10-30 02:32:03 +08:00
|
|
|
return VforkAllowlist.count(II);
|
2015-11-06 19:16:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-09-10 04:34:40 +08:00
|
|
|
auto Report = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
|
2015-11-06 19:16:31 +08:00
|
|
|
// TODO: mark vfork call in BugReportVisitor
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect calls to vfork and split execution appropriately.
|
|
|
|
void VforkChecker::checkPostCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
// We can't call vfork in child so don't bother
|
|
|
|
// (corresponding warning has already been emitted in checkPreCall).
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
if (isChildProcess(State))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!isVforkCall(Call.getDecl(), C))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get return value of vfork.
|
|
|
|
SVal VforkRetVal = Call.getReturnValue();
|
|
|
|
Optional<DefinedOrUnknownSVal> DVal =
|
|
|
|
VforkRetVal.getAs<DefinedOrUnknownSVal>();
|
|
|
|
if (!DVal)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get assigned variable.
|
|
|
|
const ParentMap &PM = C.getLocationContext()->getParentMap();
|
|
|
|
const Stmt *P = PM.getParentIgnoreParenCasts(Call.getOriginExpr());
|
|
|
|
const VarDecl *LhsDecl;
|
|
|
|
std::tie(LhsDecl, std::ignore) = parseAssignment(P);
|
|
|
|
|
|
|
|
// Get assigned memory region.
|
|
|
|
MemRegionManager &M = C.getStoreManager().getRegionManager();
|
|
|
|
const MemRegion *LhsDeclReg =
|
|
|
|
LhsDecl
|
|
|
|
? M.getVarRegion(LhsDecl, C.getLocationContext())
|
|
|
|
: (const MemRegion *)VFORK_RESULT_NONE;
|
|
|
|
|
|
|
|
// Parent branch gets nonzero return value (according to manpage).
|
|
|
|
ProgramStateRef ParentState, ChildState;
|
|
|
|
std::tie(ParentState, ChildState) = C.getState()->assume(*DVal);
|
|
|
|
C.addTransition(ParentState);
|
|
|
|
ChildState = ChildState->set<VforkResultRegion>(LhsDeclReg);
|
|
|
|
C.addTransition(ChildState);
|
|
|
|
}
|
|
|
|
|
2021-10-30 02:32:03 +08:00
|
|
|
// Prohibit calls to functions in child process which are not explicitly
|
|
|
|
// allowed.
|
2015-11-06 19:16:31 +08:00
|
|
|
void VforkChecker::checkPreCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef State = C.getState();
|
2021-10-30 02:32:03 +08:00
|
|
|
if (isChildProcess(State) &&
|
|
|
|
!isCallExplicitelyAllowed(Call.getCalleeIdentifier(), C))
|
2015-11-06 19:16:31 +08:00
|
|
|
reportBug("This function call", C);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prohibit writes in child process (except for vfork's lhs).
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Child is allowed to modify only vfork's lhs.
|
|
|
|
if (!MR || MR == VforkLhs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
reportBug("This assignment", C);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prohibit return from function in child process.
|
|
|
|
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>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterVforkChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|