Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1 | //===- Chrootchecker.cpp -------- Basic security checks ---------*- C++ -*-===// |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines chroot checker, which checks improper use of chroot. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 21 | |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 22 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 23 | using namespace ento; |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // enum value that represent the jail state |
| 28 | enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED }; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 29 | |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 30 | bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; } |
Chris Lattner | 29e78ac | 2010-10-11 05:38:10 +0000 | [diff] [blame] | 31 | //bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; } |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 32 | |
| 33 | // This checker checks improper use of chroot. |
| 34 | // The state transition: |
| 35 | // NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED |
| 36 | // | | |
| 37 | // ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)-- |
| 38 | // | | |
| 39 | // bug<--foo()-- JAIL_ENTERED<--foo()-- |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 40 | class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > { |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 41 | mutable IdentifierInfo *II_chroot, *II_chdir; |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 42 | // This bug refers to possibly break out of a chroot() jail. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 43 | mutable std::unique_ptr<BuiltinBug> BT_BreakJail; |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 44 | |
| 45 | public: |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 46 | ChrootChecker() : II_chroot(nullptr), II_chdir(nullptr) {} |
| 47 | |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 48 | static void *getTag() { |
| 49 | static int x; |
| 50 | return &x; |
| 51 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 52 | |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 53 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 54 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 57 | void Chroot(CheckerContext &C, const CallExpr *CE) const; |
| 58 | void Chdir(CheckerContext &C, const CallExpr *CE) const; |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // end anonymous namespace |
| 62 | |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 63 | bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Anna Zaks | c6aa531 | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 64 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 86 | void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 87 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 88 | ProgramStateManager &Mgr = state->getStateManager(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 89 | |
| 90 | // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 91 | // the GDM. |
| 92 | state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 93 | C.addTransition(state); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 96 | void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 97 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 98 | ProgramStateManager &Mgr = state->getStateManager(); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 99 | |
| 100 | // If there are no jail state in the GDM, just return. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 101 | const void *k = state->FindGDM(ChrootChecker::getTag()); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 102 | if (!k) |
| 103 | return; |
| 104 | |
| 105 | // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED. |
| 106 | const Expr *ArgExpr = CE->getArg(0); |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 107 | SVal ArgVal = C.getSVal(ArgExpr); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 108 | |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 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 | |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 119 | C.addTransition(state); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Check the jail state before any function call except chroot and chdir(). |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 123 | void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { |
Anna Zaks | c6aa531 | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 124 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 134 | // Ignore chroot and chdir. |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 135 | if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir) |
| 136 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 137 | |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 138 | // If jail state is ROOT_CHANGED, generate BugReport. |
Anna Zaks | c6aa531 | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 139 | void *const* k = C.getState()->FindGDM(ChrootChecker::getTag()); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 140 | if (k) |
| 141 | if (isRootChanged((intptr_t) *k)) |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 142 | if (ExplodedNode *N = C.generateNonFatalErrorNode()) { |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 143 | if (!BT_BreakJail) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 144 | BT_BreakJail.reset(new BuiltinBug( |
| 145 | this, "Break out of jail", "No call of chdir(\"/\") immediately " |
| 146 | "after chroot")); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 147 | C.emitReport(llvm::make_unique<BugReport>( |
| 148 | *BT_BreakJail, BT_BreakJail->getDescription(), N)); |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 149 | } |
Zhongxing Xu | 271c9c7 | 2010-10-10 05:45:30 +0000 | [diff] [blame] | 150 | } |
Argyrios Kyrtzidis | da02a25 | 2011-02-23 19:38:39 +0000 | [diff] [blame] | 151 | |
| 152 | void ento::registerChrootChecker(CheckerManager &mgr) { |
| 153 | mgr.registerChecker<ChrootChecker>(); |
| 154 | } |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 155 | |
| 156 | bool ento::shouldRegisterChrootChecker(const LangOptions &LO) { |
| 157 | return true; |
| 158 | } |