1*67e74705SXin Li //===- VforkChecker.cpp -------- Vfork usage checks --------------*- C++ -*-==//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file defines vfork checker which checks for dangerous uses of vfork.
11*67e74705SXin Li // Vforked process shares memory (including stack) with parent so it's
12*67e74705SXin Li // range of actions is significantly limited: can't write variables,
13*67e74705SXin Li // can't call functions not in whitelist, etc. For more details, see
14*67e74705SXin Li // http://man7.org/linux/man-pages/man2/vfork.2.html
15*67e74705SXin Li //
16*67e74705SXin Li // This checker checks for prohibited constructs in vforked process.
17*67e74705SXin Li // The state transition diagram:
18*67e74705SXin Li // PARENT ---(vfork() == 0)--> CHILD
19*67e74705SXin Li // |
20*67e74705SXin Li // --(*p = ...)--> bug
21*67e74705SXin Li // |
22*67e74705SXin Li // --foo()--> bug
23*67e74705SXin Li // |
24*67e74705SXin Li // --return--> bug
25*67e74705SXin Li //
26*67e74705SXin Li //===----------------------------------------------------------------------===//
27*67e74705SXin Li
28*67e74705SXin Li #include "ClangSACheckers.h"
29*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
30*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
31*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
32*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
33*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
34*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
35*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
36*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
37*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
38*67e74705SXin Li #include "clang/AST/ParentMap.h"
39*67e74705SXin Li
40*67e74705SXin Li using namespace clang;
41*67e74705SXin Li using namespace ento;
42*67e74705SXin Li
43*67e74705SXin Li namespace {
44*67e74705SXin Li
45*67e74705SXin Li class VforkChecker : public Checker<check::PreCall, check::PostCall,
46*67e74705SXin Li check::Bind, check::PreStmt<ReturnStmt>> {
47*67e74705SXin Li mutable std::unique_ptr<BuiltinBug> BT;
48*67e74705SXin Li mutable llvm::SmallSet<const IdentifierInfo *, 10> VforkWhitelist;
49*67e74705SXin Li mutable const IdentifierInfo *II_vfork;
50*67e74705SXin Li
51*67e74705SXin Li static bool isChildProcess(const ProgramStateRef State);
52*67e74705SXin Li
53*67e74705SXin Li bool isVforkCall(const Decl *D, CheckerContext &C) const;
54*67e74705SXin Li bool isCallWhitelisted(const IdentifierInfo *II, CheckerContext &C) const;
55*67e74705SXin Li
56*67e74705SXin Li void reportBug(const char *What, CheckerContext &C,
57*67e74705SXin Li const char *Details = nullptr) const;
58*67e74705SXin Li
59*67e74705SXin Li public:
VforkChecker()60*67e74705SXin Li VforkChecker() : II_vfork(nullptr) {}
61*67e74705SXin Li
62*67e74705SXin Li void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
63*67e74705SXin Li void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
64*67e74705SXin Li void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
65*67e74705SXin Li void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
66*67e74705SXin Li };
67*67e74705SXin Li
68*67e74705SXin Li } // end anonymous namespace
69*67e74705SXin Li
70*67e74705SXin Li // This trait holds region of variable that is assigned with vfork's
71*67e74705SXin Li // return value (this is the only region child is allowed to write).
72*67e74705SXin Li // VFORK_RESULT_INVALID means that we are in parent process.
73*67e74705SXin Li // VFORK_RESULT_NONE means that vfork's return value hasn't been assigned.
74*67e74705SXin Li // Other values point to valid regions.
REGISTER_TRAIT_WITH_PROGRAMSTATE(VforkResultRegion,const void *)75*67e74705SXin Li REGISTER_TRAIT_WITH_PROGRAMSTATE(VforkResultRegion, const void *)
76*67e74705SXin Li #define VFORK_RESULT_INVALID 0
77*67e74705SXin Li #define VFORK_RESULT_NONE ((void *)(uintptr_t)1)
78*67e74705SXin Li
79*67e74705SXin Li bool VforkChecker::isChildProcess(const ProgramStateRef State) {
80*67e74705SXin Li return State->get<VforkResultRegion>() != VFORK_RESULT_INVALID;
81*67e74705SXin Li }
82*67e74705SXin Li
isVforkCall(const Decl * D,CheckerContext & C) const83*67e74705SXin Li bool VforkChecker::isVforkCall(const Decl *D, CheckerContext &C) const {
84*67e74705SXin Li auto FD = dyn_cast_or_null<FunctionDecl>(D);
85*67e74705SXin Li if (!FD || !C.isCLibraryFunction(FD))
86*67e74705SXin Li return false;
87*67e74705SXin Li
88*67e74705SXin Li if (!II_vfork) {
89*67e74705SXin Li ASTContext &AC = C.getASTContext();
90*67e74705SXin Li II_vfork = &AC.Idents.get("vfork");
91*67e74705SXin Li }
92*67e74705SXin Li
93*67e74705SXin Li return FD->getIdentifier() == II_vfork;
94*67e74705SXin Li }
95*67e74705SXin Li
96*67e74705SXin Li // Returns true iff ok to call function after successful vfork.
isCallWhitelisted(const IdentifierInfo * II,CheckerContext & C) const97*67e74705SXin Li bool VforkChecker::isCallWhitelisted(const IdentifierInfo *II,
98*67e74705SXin Li CheckerContext &C) const {
99*67e74705SXin Li if (VforkWhitelist.empty()) {
100*67e74705SXin Li // According to manpage.
101*67e74705SXin Li const char *ids[] = {
102*67e74705SXin Li "_exit",
103*67e74705SXin Li "_Exit",
104*67e74705SXin Li "execl",
105*67e74705SXin Li "execlp",
106*67e74705SXin Li "execle",
107*67e74705SXin Li "execv",
108*67e74705SXin Li "execvp",
109*67e74705SXin Li "execvpe",
110*67e74705SXin Li nullptr
111*67e74705SXin Li };
112*67e74705SXin Li
113*67e74705SXin Li ASTContext &AC = C.getASTContext();
114*67e74705SXin Li for (const char **id = ids; *id; ++id)
115*67e74705SXin Li VforkWhitelist.insert(&AC.Idents.get(*id));
116*67e74705SXin Li }
117*67e74705SXin Li
118*67e74705SXin Li return VforkWhitelist.count(II);
119*67e74705SXin Li }
120*67e74705SXin Li
reportBug(const char * What,CheckerContext & C,const char * Details) const121*67e74705SXin Li void VforkChecker::reportBug(const char *What, CheckerContext &C,
122*67e74705SXin Li const char *Details) const {
123*67e74705SXin Li if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
124*67e74705SXin Li if (!BT)
125*67e74705SXin Li BT.reset(new BuiltinBug(this,
126*67e74705SXin Li "Dangerous construct in a vforked process"));
127*67e74705SXin Li
128*67e74705SXin Li SmallString<256> buf;
129*67e74705SXin Li llvm::raw_svector_ostream os(buf);
130*67e74705SXin Li
131*67e74705SXin Li os << What << " is prohibited after a successful vfork";
132*67e74705SXin Li
133*67e74705SXin Li if (Details)
134*67e74705SXin Li os << "; " << Details;
135*67e74705SXin Li
136*67e74705SXin Li auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N);
137*67e74705SXin Li // TODO: mark vfork call in BugReportVisitor
138*67e74705SXin Li C.emitReport(std::move(Report));
139*67e74705SXin Li }
140*67e74705SXin Li }
141*67e74705SXin Li
142*67e74705SXin Li // Detect calls to vfork and split execution appropriately.
checkPostCall(const CallEvent & Call,CheckerContext & C) const143*67e74705SXin Li void VforkChecker::checkPostCall(const CallEvent &Call,
144*67e74705SXin Li CheckerContext &C) const {
145*67e74705SXin Li // We can't call vfork in child so don't bother
146*67e74705SXin Li // (corresponding warning has already been emitted in checkPreCall).
147*67e74705SXin Li ProgramStateRef State = C.getState();
148*67e74705SXin Li if (isChildProcess(State))
149*67e74705SXin Li return;
150*67e74705SXin Li
151*67e74705SXin Li if (!isVforkCall(Call.getDecl(), C))
152*67e74705SXin Li return;
153*67e74705SXin Li
154*67e74705SXin Li // Get return value of vfork.
155*67e74705SXin Li SVal VforkRetVal = Call.getReturnValue();
156*67e74705SXin Li Optional<DefinedOrUnknownSVal> DVal =
157*67e74705SXin Li VforkRetVal.getAs<DefinedOrUnknownSVal>();
158*67e74705SXin Li if (!DVal)
159*67e74705SXin Li return;
160*67e74705SXin Li
161*67e74705SXin Li // Get assigned variable.
162*67e74705SXin Li const ParentMap &PM = C.getLocationContext()->getParentMap();
163*67e74705SXin Li const Stmt *P = PM.getParentIgnoreParenCasts(Call.getOriginExpr());
164*67e74705SXin Li const VarDecl *LhsDecl;
165*67e74705SXin Li std::tie(LhsDecl, std::ignore) = parseAssignment(P);
166*67e74705SXin Li
167*67e74705SXin Li // Get assigned memory region.
168*67e74705SXin Li MemRegionManager &M = C.getStoreManager().getRegionManager();
169*67e74705SXin Li const MemRegion *LhsDeclReg =
170*67e74705SXin Li LhsDecl
171*67e74705SXin Li ? M.getVarRegion(LhsDecl, C.getLocationContext())
172*67e74705SXin Li : (const MemRegion *)VFORK_RESULT_NONE;
173*67e74705SXin Li
174*67e74705SXin Li // Parent branch gets nonzero return value (according to manpage).
175*67e74705SXin Li ProgramStateRef ParentState, ChildState;
176*67e74705SXin Li std::tie(ParentState, ChildState) = C.getState()->assume(*DVal);
177*67e74705SXin Li C.addTransition(ParentState);
178*67e74705SXin Li ChildState = ChildState->set<VforkResultRegion>(LhsDeclReg);
179*67e74705SXin Li C.addTransition(ChildState);
180*67e74705SXin Li }
181*67e74705SXin Li
182*67e74705SXin Li // Prohibit calls to non-whitelist functions in child process.
checkPreCall(const CallEvent & Call,CheckerContext & C) const183*67e74705SXin Li void VforkChecker::checkPreCall(const CallEvent &Call,
184*67e74705SXin Li CheckerContext &C) const {
185*67e74705SXin Li ProgramStateRef State = C.getState();
186*67e74705SXin Li if (isChildProcess(State)
187*67e74705SXin Li && !isCallWhitelisted(Call.getCalleeIdentifier(), C))
188*67e74705SXin Li reportBug("This function call", C);
189*67e74705SXin Li }
190*67e74705SXin Li
191*67e74705SXin Li // Prohibit writes in child process (except for vfork's lhs).
checkBind(SVal L,SVal V,const Stmt * S,CheckerContext & C) const192*67e74705SXin Li void VforkChecker::checkBind(SVal L, SVal V, const Stmt *S,
193*67e74705SXin Li CheckerContext &C) const {
194*67e74705SXin Li ProgramStateRef State = C.getState();
195*67e74705SXin Li if (!isChildProcess(State))
196*67e74705SXin Li return;
197*67e74705SXin Li
198*67e74705SXin Li const MemRegion *VforkLhs =
199*67e74705SXin Li static_cast<const MemRegion *>(State->get<VforkResultRegion>());
200*67e74705SXin Li const MemRegion *MR = L.getAsRegion();
201*67e74705SXin Li
202*67e74705SXin Li // Child is allowed to modify only vfork's lhs.
203*67e74705SXin Li if (!MR || MR == VforkLhs)
204*67e74705SXin Li return;
205*67e74705SXin Li
206*67e74705SXin Li reportBug("This assignment", C);
207*67e74705SXin Li }
208*67e74705SXin Li
209*67e74705SXin Li // Prohibit return from function in child process.
checkPreStmt(const ReturnStmt * RS,CheckerContext & C) const210*67e74705SXin Li void VforkChecker::checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const {
211*67e74705SXin Li ProgramStateRef State = C.getState();
212*67e74705SXin Li if (isChildProcess(State))
213*67e74705SXin Li reportBug("Return", C, "call _exit() instead");
214*67e74705SXin Li }
215*67e74705SXin Li
registerVforkChecker(CheckerManager & mgr)216*67e74705SXin Li void ento::registerVforkChecker(CheckerManager &mgr) {
217*67e74705SXin Li mgr.registerChecker<VforkChecker>();
218*67e74705SXin Li }
219