1 //===-- ChrootChecker.cpp - chroot usage checks ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines chroot checker, which checks improper use of chroot.
11 //===----------------------------------------------------------------------===//
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/CallEvent.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
23 using namespace clang;
28 // enum value that represent the jail state
29 enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED };
31 bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; }
32 //bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
34 // This checker checks improper use of chroot.
35 // The state transition:
36 // NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED
38 // ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--
40 // bug<--foo()-- JAIL_ENTERED<--foo()--
41 class ChrootChecker : public Checker<eval::Call, check::PreCall> {
42 // This bug refers to possibly break out of a chroot() jail.
43 mutable std::unique_ptr<BuiltinBug> BT_BreakJail;
45 const CallDescription Chroot{"chroot", 1}, Chdir{"chdir", 1};
50 static void *getTag() {
55 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
56 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
59 void evalChroot(const CallEvent &Call, CheckerContext &C) const;
60 void evalChdir(const CallEvent &Call, CheckerContext &C) const;
63 } // end anonymous namespace
65 bool ChrootChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
66 if (Call.isCalled(Chroot)) {
70 if (Call.isCalled(Chdir)) {
78 void ChrootChecker::evalChroot(const CallEvent &Call, CheckerContext &C) const {
79 ProgramStateRef state = C.getState();
80 ProgramStateManager &Mgr = state->getStateManager();
82 // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
84 state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED);
85 C.addTransition(state);
88 void ChrootChecker::evalChdir(const CallEvent &Call, CheckerContext &C) const {
89 ProgramStateRef state = C.getState();
90 ProgramStateManager &Mgr = state->getStateManager();
92 // If there are no jail state in the GDM, just return.
93 const void *k = state->FindGDM(ChrootChecker::getTag());
97 // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.
98 const Expr *ArgExpr = Call.getArgExpr(0);
99 SVal ArgVal = C.getSVal(ArgExpr);
101 if (const MemRegion *R = ArgVal.getAsRegion()) {
103 if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) {
104 const StringLiteral* Str = StrRegion->getStringLiteral();
105 if (Str->getString() == "/")
106 state = Mgr.addGDM(state, ChrootChecker::getTag(),
107 (void*) JAIL_ENTERED);
111 C.addTransition(state);
114 // Check the jail state before any function call except chroot and chdir().
115 void ChrootChecker::checkPreCall(const CallEvent &Call,
116 CheckerContext &C) const {
117 // Ignore chroot and chdir.
118 if (Call.isCalled(Chroot) || Call.isCalled(Chdir))
121 // If jail state is ROOT_CHANGED, generate BugReport.
122 void *const* k = C.getState()->FindGDM(ChrootChecker::getTag());
124 if (isRootChanged((intptr_t) *k))
125 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
127 BT_BreakJail.reset(new BuiltinBug(
128 this, "Break out of jail", "No call of chdir(\"/\") immediately "
130 C.emitReport(std::make_unique<PathSensitiveBugReport>(
131 *BT_BreakJail, BT_BreakJail->getDescription(), N));
135 void ento::registerChrootChecker(CheckerManager &mgr) {
136 mgr.registerChecker<ChrootChecker>();
139 bool ento::shouldRegisterChrootChecker(const CheckerManager &mgr) {