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 "ClangSACheckers.h"
15 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
21 #include "llvm/ADT/ImmutableMap.h"
22 using namespace clang
;
27 // enum value that represent the jail state
28 enum Kind
{ NO_CHROOT
, ROOT_CHANGED
, JAIL_ENTERED
};
30 bool isRootChanged(intptr_t k
) { return k
== ROOT_CHANGED
; }
31 //bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
33 // This checker checks improper use of chroot.
34 // The state transition:
35 // NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED
37 // ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--
39 // bug<--foo()-- JAIL_ENTERED<--foo()--
40 class ChrootChecker
: public CheckerVisitor
<ChrootChecker
> {
41 IdentifierInfo
*II_chroot
, *II_chdir
;
42 // This bug refers to possibly break out of a chroot() jail.
43 BuiltinBug
*BT_BreakJail
;
46 ChrootChecker() : II_chroot(0), II_chdir(0), BT_BreakJail(0) {}
48 static void *getTag() {
53 virtual bool evalCallExpr(CheckerContext
&C
, const CallExpr
*CE
);
54 virtual void PreVisitCallExpr(CheckerContext
&C
, const CallExpr
*CE
);
57 void Chroot(CheckerContext
&C
, const CallExpr
*CE
);
58 void Chdir(CheckerContext
&C
, const CallExpr
*CE
);
61 } // end anonymous namespace
63 static void RegisterChrootChecker(ExprEngine
&Eng
) {
64 Eng
.registerCheck(new ChrootChecker());
67 void ento::registerChrootChecker(CheckerManager
&mgr
) {
68 mgr
.addCheckerRegisterFunction(RegisterChrootChecker
);
71 bool ChrootChecker::evalCallExpr(CheckerContext
&C
, const CallExpr
*CE
) {
72 const GRState
*state
= C
.getState();
73 const Expr
*Callee
= CE
->getCallee();
74 SVal L
= state
->getSVal(Callee
);
75 const FunctionDecl
*FD
= L
.getAsFunctionDecl();
79 ASTContext
&Ctx
= C
.getASTContext();
81 II_chroot
= &Ctx
.Idents
.get("chroot");
83 II_chdir
= &Ctx
.Idents
.get("chdir");
85 if (FD
->getIdentifier() == II_chroot
) {
89 if (FD
->getIdentifier() == II_chdir
) {
97 void ChrootChecker::Chroot(CheckerContext
&C
, const CallExpr
*CE
) {
98 const GRState
*state
= C
.getState();
99 GRStateManager
&Mgr
= state
->getStateManager();
101 // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
103 state
= Mgr
.addGDM(state
, ChrootChecker::getTag(), (void*) ROOT_CHANGED
);
104 C
.addTransition(state
);
107 void ChrootChecker::Chdir(CheckerContext
&C
, const CallExpr
*CE
) {
108 const GRState
*state
= C
.getState();
109 GRStateManager
&Mgr
= state
->getStateManager();
111 // If there are no jail state in the GDM, just return.
112 const void* k
= state
->FindGDM(ChrootChecker::getTag());
116 // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.
117 const Expr
*ArgExpr
= CE
->getArg(0);
118 SVal ArgVal
= state
->getSVal(ArgExpr
);
120 if (const MemRegion
*R
= ArgVal
.getAsRegion()) {
122 if (const StringRegion
* StrRegion
= dyn_cast
<StringRegion
>(R
)) {
123 const StringLiteral
* Str
= StrRegion
->getStringLiteral();
124 if (Str
->getString() == "/")
125 state
= Mgr
.addGDM(state
, ChrootChecker::getTag(),
126 (void*) JAIL_ENTERED
);
130 C
.addTransition(state
);
133 // Check the jail state before any function call except chroot and chdir().
134 void ChrootChecker::PreVisitCallExpr(CheckerContext
&C
, const CallExpr
*CE
) {
135 const GRState
*state
= C
.getState();
136 const Expr
*Callee
= CE
->getCallee();
137 SVal L
= state
->getSVal(Callee
);
138 const FunctionDecl
*FD
= L
.getAsFunctionDecl();
142 ASTContext
&Ctx
= C
.getASTContext();
144 II_chroot
= &Ctx
.Idents
.get("chroot");
146 II_chdir
= &Ctx
.Idents
.get("chdir");
148 // Ingnore chroot and chdir.
149 if (FD
->getIdentifier() == II_chroot
|| FD
->getIdentifier() == II_chdir
)
152 // If jail state is ROOT_CHANGED, generate BugReport.
153 void* const* k
= state
->FindGDM(ChrootChecker::getTag());
155 if (isRootChanged((intptr_t) *k
))
156 if (ExplodedNode
*N
= C
.generateNode()) {
158 BT_BreakJail
= new BuiltinBug("Break out of jail",
159 "No call of chdir(\"/\") immediately "
161 BugReport
*R
= new BugReport(*BT_BreakJail
,
162 BT_BreakJail
->getDescription(), N
);