Path: blob/main/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
35266 views
//===-- ChrootChecker.cpp - chroot usage checks ---------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines chroot checker, which checks improper use of chroot.9//10//===----------------------------------------------------------------------===//1112#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"13#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"14#include "clang/StaticAnalyzer/Core/Checker.h"15#include "clang/StaticAnalyzer/Core/CheckerManager.h"16#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.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"2223using namespace clang;24using namespace ento;2526namespace {2728// enum value that represent the jail state29enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED };3031bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; }32//bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }3334// This checker checks improper use of chroot.35// The state transition:36// NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED37// | |38// ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--39// | |40// bug<--foo()-- JAIL_ENTERED<--foo()--41class ChrootChecker : public Checker<eval::Call, check::PreCall> {42// This bug refers to possibly break out of a chroot() jail.43const BugType BT_BreakJail{this, "Break out of jail"};4445const CallDescription Chroot{CDM::CLibrary, {"chroot"}, 1},46Chdir{CDM::CLibrary, {"chdir"}, 1};4748public:49ChrootChecker() {}5051static void *getTag() {52static int x;53return &x;54}5556bool evalCall(const CallEvent &Call, CheckerContext &C) const;57void checkPreCall(const CallEvent &Call, CheckerContext &C) const;5859private:60void evalChroot(const CallEvent &Call, CheckerContext &C) const;61void evalChdir(const CallEvent &Call, CheckerContext &C) const;62};6364} // end anonymous namespace6566bool ChrootChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {67if (Chroot.matches(Call)) {68evalChroot(Call, C);69return true;70}71if (Chdir.matches(Call)) {72evalChdir(Call, C);73return true;74}7576return false;77}7879void ChrootChecker::evalChroot(const CallEvent &Call, CheckerContext &C) const {80ProgramStateRef state = C.getState();81ProgramStateManager &Mgr = state->getStateManager();8283// Once encouter a chroot(), set the enum value ROOT_CHANGED directly in84// the GDM.85state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED);86C.addTransition(state);87}8889void ChrootChecker::evalChdir(const CallEvent &Call, CheckerContext &C) const {90ProgramStateRef state = C.getState();91ProgramStateManager &Mgr = state->getStateManager();9293// If there are no jail state in the GDM, just return.94const void *k = state->FindGDM(ChrootChecker::getTag());95if (!k)96return;9798// After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.99const Expr *ArgExpr = Call.getArgExpr(0);100SVal ArgVal = C.getSVal(ArgExpr);101102if (const MemRegion *R = ArgVal.getAsRegion()) {103R = R->StripCasts();104if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) {105const StringLiteral* Str = StrRegion->getStringLiteral();106if (Str->getString() == "/")107state = Mgr.addGDM(state, ChrootChecker::getTag(),108(void*) JAIL_ENTERED);109}110}111112C.addTransition(state);113}114115// Check the jail state before any function call except chroot and chdir().116void ChrootChecker::checkPreCall(const CallEvent &Call,117CheckerContext &C) const {118// Ignore chroot and chdir.119if (matchesAny(Call, Chroot, Chdir))120return;121122// If jail state is ROOT_CHANGED, generate BugReport.123void *const* k = C.getState()->FindGDM(ChrootChecker::getTag());124if (k)125if (isRootChanged((intptr_t) *k))126if (ExplodedNode *N = C.generateNonFatalErrorNode()) {127constexpr llvm::StringLiteral Msg =128"No call of chdir(\"/\") immediately after chroot";129C.emitReport(130std::make_unique<PathSensitiveBugReport>(BT_BreakJail, Msg, N));131}132}133134void ento::registerChrootChecker(CheckerManager &mgr) {135mgr.registerChecker<ChrootChecker>();136}137138bool ento::shouldRegisterChrootChecker(const CheckerManager &mgr) {139return true;140}141142143