Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 1 | //===- 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 Kyrtzidis | a0decc9 | 2011-02-15 21:25:03 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ImmutableMap.h" |
| 23 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 24 | using namespace ento; |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // enum value that represent the jail state |
| 29 | enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED }; |
| 30 | |
| 31 | bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; } |
Chris Lattner | 4505d89 | 2010-10-11 05:38:10 +0000 | [diff] [blame] | 32 | //bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; } |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 33 | |
| 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 Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 41 | class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > { |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 42 | mutable IdentifierInfo *II_chroot, *II_chdir; |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 43 | // This bug refers to possibly break out of a chroot() jail. |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 44 | mutable llvm::OwningPtr<BuiltinBug> BT_BreakJail; |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 45 | |
| 46 | public: |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 47 | ChrootChecker() : II_chroot(0), II_chdir(0) {} |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 48 | |
| 49 | static void *getTag() { |
| 50 | static int x; |
| 51 | return &x; |
| 52 | } |
| 53 | |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 54 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 55 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 56 | |
| 57 | private: |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 58 | void Chroot(CheckerContext &C, const CallExpr *CE) const; |
| 59 | void Chdir(CheckerContext &C, const CallExpr *CE) const; |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // end anonymous namespace |
| 63 | |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 64 | bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 65 | const ProgramState *state = C.getState(); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 66 | const Expr *Callee = CE->getCallee(); |
| 67 | SVal L = state->getSVal(Callee); |
| 68 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
| 69 | if (!FD) |
| 70 | return false; |
| 71 | |
| 72 | ASTContext &Ctx = C.getASTContext(); |
| 73 | if (!II_chroot) |
| 74 | II_chroot = &Ctx.Idents.get("chroot"); |
| 75 | if (!II_chdir) |
| 76 | II_chdir = &Ctx.Idents.get("chdir"); |
| 77 | |
| 78 | if (FD->getIdentifier() == II_chroot) { |
| 79 | Chroot(C, CE); |
| 80 | return true; |
| 81 | } |
| 82 | if (FD->getIdentifier() == II_chdir) { |
| 83 | Chdir(C, CE); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 90 | void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 91 | const ProgramState *state = C.getState(); |
| 92 | ProgramStateManager &Mgr = state->getStateManager(); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 93 | |
| 94 | // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in |
| 95 | // the GDM. |
| 96 | state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED); |
Anna Zaks | 063e088 | 2011-10-25 19:57:06 +0000 | [diff] [blame] | 97 | C.generateNode(state); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 100 | void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 101 | const ProgramState *state = C.getState(); |
| 102 | ProgramStateManager &Mgr = state->getStateManager(); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 103 | |
| 104 | // If there are no jail state in the GDM, just return. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 105 | const void *k = state->FindGDM(ChrootChecker::getTag()); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 106 | if (!k) |
| 107 | return; |
| 108 | |
| 109 | // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED. |
| 110 | const Expr *ArgExpr = CE->getArg(0); |
| 111 | SVal ArgVal = state->getSVal(ArgExpr); |
| 112 | |
| 113 | if (const MemRegion *R = ArgVal.getAsRegion()) { |
| 114 | R = R->StripCasts(); |
| 115 | if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) { |
| 116 | const StringLiteral* Str = StrRegion->getStringLiteral(); |
| 117 | if (Str->getString() == "/") |
| 118 | state = Mgr.addGDM(state, ChrootChecker::getTag(), |
| 119 | (void*) JAIL_ENTERED); |
| 120 | } |
| 121 | } |
| 122 | |
Anna Zaks | 063e088 | 2011-10-25 19:57:06 +0000 | [diff] [blame] | 123 | C.generateNode(state); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Check the jail state before any function call except chroot and chdir(). |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 127 | void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 128 | const ProgramState *state = C.getState(); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 129 | const Expr *Callee = CE->getCallee(); |
| 130 | SVal L = state->getSVal(Callee); |
| 131 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
| 132 | if (!FD) |
| 133 | return; |
| 134 | |
| 135 | ASTContext &Ctx = C.getASTContext(); |
| 136 | if (!II_chroot) |
| 137 | II_chroot = &Ctx.Idents.get("chroot"); |
| 138 | if (!II_chdir) |
| 139 | II_chdir = &Ctx.Idents.get("chdir"); |
| 140 | |
| 141 | // Ingnore chroot and chdir. |
| 142 | if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir) |
| 143 | return; |
| 144 | |
| 145 | // If jail state is ROOT_CHANGED, generate BugReport. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 146 | void *const* k = state->FindGDM(ChrootChecker::getTag()); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 147 | if (k) |
| 148 | if (isRootChanged((intptr_t) *k)) |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 149 | if (ExplodedNode *N = C.generateNode()) { |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 150 | if (!BT_BreakJail) |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 151 | BT_BreakJail.reset(new BuiltinBug("Break out of jail", |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 152 | "No call of chdir(\"/\") immediately " |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 153 | "after chroot")); |
Zhongxing Xu | 1c9e6b1 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 154 | BugReport *R = new BugReport(*BT_BreakJail, |
| 155 | BT_BreakJail->getDescription(), N); |
| 156 | C.EmitReport(R); |
| 157 | } |
| 158 | |
| 159 | return; |
| 160 | } |
Argyrios Kyrtzidis | e1bfb7a | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 161 | |
| 162 | void ento::registerChrootChecker(CheckerManager &mgr) { |
| 163 | mgr.registerChecker<ChrootChecker>(); |
| 164 | } |