1 //===- Chrootchecker.cpp -------- Basic security checks ----------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines chroot checker, which checks improper use of chroot.
12 //===----------------------------------------------------------------------===//
14 #include "GRExprEngineExperimentalChecks.h"
15 #include "clang/GR/BugReporter/BugType.h"
16 #include "clang/GR/PathSensitive/CheckerVisitor.h"
17 #include "clang/GR/PathSensitive/GRState.h"
18 #include "clang/GR/PathSensitive/GRStateTrait.h"
19 #include "clang/GR/PathSensitive/SymbolManager.h"
20 #include "llvm/ADT/ImmutableMap.h"
21 using namespace clang
;
26 // enum value that represent the jail state
27 enum Kind
{ NO_CHROOT
, ROOT_CHANGED
, JAIL_ENTERED
};
29 bool isRootChanged(intptr_t k
) { return k
== ROOT_CHANGED
; }
30 //bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
32 // This checker checks improper use of chroot.
33 // The state transition:
34 // NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED
36 // ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--
38 // bug<--foo()-- JAIL_ENTERED<--foo()--
39 class ChrootChecker
: public CheckerVisitor
<ChrootChecker
> {
40 IdentifierInfo
*II_chroot
, *II_chdir
;
41 // This bug refers to possibly break out of a chroot() jail.
42 BuiltinBug
*BT_BreakJail
;
45 ChrootChecker() : II_chroot(0), II_chdir(0), BT_BreakJail(0) {}
47 static void *getTag() {
52 virtual bool evalCallExpr(CheckerContext
&C
, const CallExpr
*CE
);
53 virtual void PreVisitCallExpr(CheckerContext
&C
, const CallExpr
*CE
);
56 void Chroot(CheckerContext
&C
, const CallExpr
*CE
);
57 void Chdir(CheckerContext
&C
, const CallExpr
*CE
);
60 } // end anonymous namespace
62 void GR::RegisterChrootChecker(GRExprEngine
&Eng
) {
63 Eng
.registerCheck(new ChrootChecker());
66 bool ChrootChecker::evalCallExpr(CheckerContext
&C
, const CallExpr
*CE
) {
67 const GRState
*state
= C
.getState();
68 const Expr
*Callee
= CE
->getCallee();
69 SVal L
= state
->getSVal(Callee
);
70 const FunctionDecl
*FD
= L
.getAsFunctionDecl();
74 ASTContext
&Ctx
= C
.getASTContext();
76 II_chroot
= &Ctx
.Idents
.get("chroot");
78 II_chdir
= &Ctx
.Idents
.get("chdir");
80 if (FD
->getIdentifier() == II_chroot
) {
84 if (FD
->getIdentifier() == II_chdir
) {
92 void ChrootChecker::Chroot(CheckerContext
&C
, const CallExpr
*CE
) {
93 const GRState
*state
= C
.getState();
94 GRStateManager
&Mgr
= state
->getStateManager();
96 // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
98 state
= Mgr
.addGDM(state
, ChrootChecker::getTag(), (void*) ROOT_CHANGED
);
99 C
.addTransition(state
);
102 void ChrootChecker::Chdir(CheckerContext
&C
, const CallExpr
*CE
) {
103 const GRState
*state
= C
.getState();
104 GRStateManager
&Mgr
= state
->getStateManager();
106 // If there are no jail state in the GDM, just return.
107 const void* k
= state
->FindGDM(ChrootChecker::getTag());
111 // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.
112 const Expr
*ArgExpr
= CE
->getArg(0);
113 SVal ArgVal
= state
->getSVal(ArgExpr
);
115 if (const MemRegion
*R
= ArgVal
.getAsRegion()) {
117 if (const StringRegion
* StrRegion
= dyn_cast
<StringRegion
>(R
)) {
118 const StringLiteral
* Str
= StrRegion
->getStringLiteral();
119 if (Str
->getString() == "/")
120 state
= Mgr
.addGDM(state
, ChrootChecker::getTag(),
121 (void*) JAIL_ENTERED
);
125 C
.addTransition(state
);
128 // Check the jail state before any function call except chroot and chdir().
129 void ChrootChecker::PreVisitCallExpr(CheckerContext
&C
, const CallExpr
*CE
) {
130 const GRState
*state
= C
.getState();
131 const Expr
*Callee
= CE
->getCallee();
132 SVal L
= state
->getSVal(Callee
);
133 const FunctionDecl
*FD
= L
.getAsFunctionDecl();
137 ASTContext
&Ctx
= C
.getASTContext();
139 II_chroot
= &Ctx
.Idents
.get("chroot");
141 II_chdir
= &Ctx
.Idents
.get("chdir");
143 // Ingnore chroot and chdir.
144 if (FD
->getIdentifier() == II_chroot
|| FD
->getIdentifier() == II_chdir
)
147 // If jail state is ROOT_CHANGED, generate BugReport.
148 void* const* k
= state
->FindGDM(ChrootChecker::getTag());
150 if (isRootChanged((intptr_t) *k
))
151 if (ExplodedNode
*N
= C
.generateNode()) {
153 BT_BreakJail
= new BuiltinBug("Break out of jail",
154 "No call of chdir(\"/\") immediately "
156 BugReport
*R
= new BugReport(*BT_BreakJail
,
157 BT_BreakJail
->getDescription(), N
);