1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
14 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
16 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
18 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
19 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
20 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
21 | |
22 | using namespace clang; |
23 | using namespace ento; |
24 | |
25 | namespace { |
26 | |
27 | |
28 | enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED }; |
29 | |
30 | bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; } |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > { |
41 | mutable IdentifierInfo *II_chroot, *II_chdir; |
42 | |
43 | mutable std::unique_ptr<BuiltinBug> BT_BreakJail; |
44 | |
45 | public: |
46 | ChrootChecker() : II_chroot(nullptr), II_chdir(nullptr) {} |
47 | |
48 | static void *getTag() { |
49 | static int x; |
50 | return &x; |
51 | } |
52 | |
53 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
54 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
55 | |
56 | private: |
57 | void Chroot(CheckerContext &C, const CallExpr *CE) const; |
58 | void Chdir(CheckerContext &C, const CallExpr *CE) const; |
59 | }; |
60 | |
61 | } |
62 | |
63 | bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
64 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
65 | if (!FD) |
66 | return false; |
67 | |
68 | ASTContext &Ctx = C.getASTContext(); |
69 | if (!II_chroot) |
70 | II_chroot = &Ctx.Idents.get("chroot"); |
71 | if (!II_chdir) |
72 | II_chdir = &Ctx.Idents.get("chdir"); |
73 | |
74 | if (FD->getIdentifier() == II_chroot) { |
75 | Chroot(C, CE); |
76 | return true; |
77 | } |
78 | if (FD->getIdentifier() == II_chdir) { |
79 | Chdir(C, CE); |
80 | return true; |
81 | } |
82 | |
83 | return false; |
84 | } |
85 | |
86 | void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) const { |
87 | ProgramStateRef state = C.getState(); |
88 | ProgramStateManager &Mgr = state->getStateManager(); |
89 | |
90 | |
91 | |
92 | state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED); |
93 | C.addTransition(state); |
94 | } |
95 | |
96 | void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) const { |
97 | ProgramStateRef state = C.getState(); |
98 | ProgramStateManager &Mgr = state->getStateManager(); |
99 | |
100 | |
101 | const void *k = state->FindGDM(ChrootChecker::getTag()); |
102 | if (!k) |
103 | return; |
104 | |
105 | |
106 | const Expr *ArgExpr = CE->getArg(0); |
107 | SVal ArgVal = C.getSVal(ArgExpr); |
108 | |
109 | if (const MemRegion *R = ArgVal.getAsRegion()) { |
110 | R = R->StripCasts(); |
111 | if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) { |
112 | const StringLiteral* Str = StrRegion->getStringLiteral(); |
113 | if (Str->getString() == "/") |
114 | state = Mgr.addGDM(state, ChrootChecker::getTag(), |
115 | (void*) JAIL_ENTERED); |
116 | } |
117 | } |
118 | |
119 | C.addTransition(state); |
120 | } |
121 | |
122 | |
123 | void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { |
124 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
125 | if (!FD) |
126 | return; |
127 | |
128 | ASTContext &Ctx = C.getASTContext(); |
129 | if (!II_chroot) |
130 | II_chroot = &Ctx.Idents.get("chroot"); |
131 | if (!II_chdir) |
132 | II_chdir = &Ctx.Idents.get("chdir"); |
133 | |
134 | |
135 | if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir) |
136 | return; |
137 | |
138 | |
139 | void *const* k = C.getState()->FindGDM(ChrootChecker::getTag()); |
140 | if (k) |
141 | if (isRootChanged((intptr_t) *k)) |
142 | if (ExplodedNode *N = C.generateNonFatalErrorNode()) { |
143 | if (!BT_BreakJail) |
144 | BT_BreakJail.reset(new BuiltinBug( |
145 | this, "Break out of jail", "No call of chdir(\"/\") immediately " |
146 | "after chroot")); |
147 | C.emitReport(llvm::make_unique<BugReport>( |
148 | *BT_BreakJail, BT_BreakJail->getDescription(), N)); |
149 | } |
150 | } |
151 | |
152 | void ento::registerChrootChecker(CheckerManager &mgr) { |
153 | mgr.registerChecker<ChrootChecker>(); |
154 | } |
155 | |
156 | bool ento::shouldRegisterChrootChecker(const LangOptions &LO) { |
157 | return true; |
158 | } |
159 | |