blob: fa79f2186613f11dbe214bf49c335eee66edeb46 [file] [log] [blame]
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +00001//===- Chrootchecker.cpp -------- Basic security checks ----------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines chroot checker, which checks improper use of chroot.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidisa0decc92011-02-15 21:25:03 +000014#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000022#include "llvm/ADT/ImmutableMap.h"
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000025
26namespace {
27
28// enum value that represent the jail state
29enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED };
30
31bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; }
Chris Lattner4505d892010-10-11 05:38:10 +000032//bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000033
34// This checker checks improper use of chroot.
35// The state transition:
36// NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED
37// | |
38// ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--
39// | |
40// bug<--foo()-- JAIL_ENTERED<--foo()--
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000041class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > {
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000042 mutable IdentifierInfo *II_chroot, *II_chdir;
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000043 // This bug refers to possibly break out of a chroot() jail.
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000044 mutable llvm::OwningPtr<BuiltinBug> BT_BreakJail;
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000045
46public:
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000047 ChrootChecker() : II_chroot(0), II_chdir(0) {}
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000048
49 static void *getTag() {
50 static int x;
51 return &x;
52 }
53
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000054 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
55 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000056
57private:
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000058 void Chroot(CheckerContext &C, const CallExpr *CE) const;
59 void Chdir(CheckerContext &C, const CallExpr *CE) const;
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000060};
61
62} // end anonymous namespace
63
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000064bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +000065 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000066 if (!FD)
67 return false;
68
69 ASTContext &Ctx = C.getASTContext();
70 if (!II_chroot)
71 II_chroot = &Ctx.Idents.get("chroot");
72 if (!II_chdir)
73 II_chdir = &Ctx.Idents.get("chdir");
74
75 if (FD->getIdentifier() == II_chroot) {
76 Chroot(C, CE);
77 return true;
78 }
79 if (FD->getIdentifier() == II_chdir) {
80 Chdir(C, CE);
81 return true;
82 }
83
84 return false;
85}
86
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000087void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000088 const ProgramState *state = C.getState();
89 ProgramStateManager &Mgr = state->getStateManager();
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000090
91 // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
92 // the GDM.
93 state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED);
Anna Zaks0bd6b112011-10-26 21:06:34 +000094 C.addTransition(state);
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +000095}
96
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +000097void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000098 const ProgramState *state = C.getState();
99 ProgramStateManager &Mgr = state->getStateManager();
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000100
101 // If there are no jail state in the GDM, just return.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000102 const void *k = state->FindGDM(ChrootChecker::getTag());
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000103 if (!k)
104 return;
105
106 // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.
107 const Expr *ArgExpr = CE->getArg(0);
108 SVal ArgVal = state->getSVal(ArgExpr);
109
110 if (const MemRegion *R = ArgVal.getAsRegion()) {
111 R = R->StripCasts();
112 if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) {
113 const StringLiteral* Str = StrRegion->getStringLiteral();
114 if (Str->getString() == "/")
115 state = Mgr.addGDM(state, ChrootChecker::getTag(),
116 (void*) JAIL_ENTERED);
117 }
118 }
119
Anna Zaks0bd6b112011-10-26 21:06:34 +0000120 C.addTransition(state);
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000121}
122
123// Check the jail state before any function call except chroot and chdir().
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +0000124void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000125 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000126 if (!FD)
127 return;
128
129 ASTContext &Ctx = C.getASTContext();
130 if (!II_chroot)
131 II_chroot = &Ctx.Idents.get("chroot");
132 if (!II_chdir)
133 II_chdir = &Ctx.Idents.get("chdir");
134
135 // Ingnore chroot and chdir.
136 if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir)
137 return;
138
139 // If jail state is ROOT_CHANGED, generate BugReport.
Anna Zaksb805c8f2011-12-01 05:57:37 +0000140 void *const* k = C.getState()->FindGDM(ChrootChecker::getTag());
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000141 if (k)
142 if (isRootChanged((intptr_t) *k))
Anna Zaks0bd6b112011-10-26 21:06:34 +0000143 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000144 if (!BT_BreakJail)
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +0000145 BT_BreakJail.reset(new BuiltinBug("Break out of jail",
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000146 "No call of chdir(\"/\") immediately "
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +0000147 "after chroot"));
Zhongxing Xu1c9e6b12010-10-10 05:45:30 +0000148 BugReport *R = new BugReport(*BT_BreakJail,
149 BT_BreakJail->getDescription(), N);
150 C.EmitReport(R);
151 }
152
153 return;
154}
Argyrios Kyrtzidise1bfb7a2011-02-23 19:38:39 +0000155
156void ento::registerChrootChecker(CheckerManager &mgr) {
157 mgr.registerChecker<ChrootChecker>();
158}