2019-03-01 14:49:51 +08:00
|
|
|
//===-- ChrootChecker.cpp - chroot usage checks ---------------------------===//
|
2010-10-10 13:45:30 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-10-10 13:45:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines chroot checker, which checks improper use of chroot.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2019-06-20 07:33:42 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-02-24 03:38:39 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
|
2016-02-11 03:11:58 +08:00
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-10-10 13:45:30 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// enum value that represent the jail state
|
|
|
|
enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED };
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; }
|
2010-10-11 13:38:10 +08:00
|
|
|
//bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
|
2010-10-10 13:45:30 +08:00
|
|
|
|
|
|
|
// This checker checks improper use of chroot.
|
|
|
|
// The state transition:
|
|
|
|
// NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED
|
|
|
|
// | |
|
|
|
|
// ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--
|
|
|
|
// | |
|
|
|
|
// bug<--foo()-- JAIL_ENTERED<--foo()--
|
2019-06-20 07:33:42 +08:00
|
|
|
class ChrootChecker : public Checker<eval::Call, check::PreCall> {
|
2010-10-10 13:45:30 +08:00
|
|
|
// This bug refers to possibly break out of a chroot() jail.
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_BreakJail;
|
2010-10-10 13:45:30 +08:00
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
const CallDescription Chroot{"chroot", 1}, Chdir{"chdir", 1};
|
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
public:
|
2019-06-20 07:33:42 +08:00
|
|
|
ChrootChecker() {}
|
2014-05-27 10:45:47 +08:00
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
static void *getTag() {
|
|
|
|
static int x;
|
|
|
|
return &x;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
bool evalCall(const CallEvent &Call, CheckerContext &C) const;
|
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
2010-10-10 13:45:30 +08:00
|
|
|
|
|
|
|
private:
|
2019-06-20 07:33:42 +08:00
|
|
|
void evalChroot(const CallEvent &Call, CheckerContext &C) const;
|
|
|
|
void evalChdir(const CallEvent &Call, CheckerContext &C) const;
|
2010-10-10 13:45:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
bool ChrootChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
|
|
|
|
if (Call.isCalled(Chroot)) {
|
|
|
|
evalChroot(Call, C);
|
2010-10-10 13:45:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-20 07:33:42 +08:00
|
|
|
if (Call.isCalled(Chdir)) {
|
|
|
|
evalChdir(Call, C);
|
2010-10-10 13:45:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
void ChrootChecker::evalChroot(const CallEvent &Call, CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-08-16 06:09:50 +08:00
|
|
|
ProgramStateManager &Mgr = state->getStateManager();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
|
|
|
// Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
|
2010-10-10 13:45:30 +08:00
|
|
|
// the GDM.
|
|
|
|
state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-10-10 13:45:30 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
void ChrootChecker::evalChdir(const CallEvent &Call, CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-08-16 06:09:50 +08:00
|
|
|
ProgramStateManager &Mgr = state->getStateManager();
|
2010-10-10 13:45:30 +08:00
|
|
|
|
|
|
|
// If there are no jail state in the GDM, just return.
|
2011-08-13 07:37:29 +08:00
|
|
|
const void *k = state->FindGDM(ChrootChecker::getTag());
|
2010-10-10 13:45:30 +08:00
|
|
|
if (!k)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.
|
2019-06-20 07:33:42 +08:00
|
|
|
const Expr *ArgExpr = Call.getArgExpr(0);
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal ArgVal = C.getSVal(ArgExpr);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
if (const MemRegion *R = ArgVal.getAsRegion()) {
|
|
|
|
R = R->StripCasts();
|
|
|
|
if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) {
|
|
|
|
const StringLiteral* Str = StrRegion->getStringLiteral();
|
|
|
|
if (Str->getString() == "/")
|
|
|
|
state = Mgr.addGDM(state, ChrootChecker::getTag(),
|
|
|
|
(void*) JAIL_ENTERED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-10-10 13:45:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the jail state before any function call except chroot and chdir().
|
2019-06-20 07:33:42 +08:00
|
|
|
void ChrootChecker::checkPreCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2018-04-06 23:14:32 +08:00
|
|
|
// Ignore chroot and chdir.
|
2019-06-20 07:33:42 +08:00
|
|
|
if (Call.isCalled(Chroot) || Call.isCalled(Chdir))
|
2010-10-10 13:45:30 +08:00
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
// If jail state is ROOT_CHANGED, generate BugReport.
|
2011-12-01 13:57:37 +08:00
|
|
|
void *const* k = C.getState()->FindGDM(ChrootChecker::getTag());
|
2010-10-10 13:45:30 +08:00
|
|
|
if (k)
|
|
|
|
if (isRootChanged((intptr_t) *k))
|
2015-09-17 06:03:05 +08:00
|
|
|
if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
|
2010-10-10 13:45:30 +08:00
|
|
|
if (!BT_BreakJail)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT_BreakJail.reset(new BuiltinBug(
|
|
|
|
this, "Break out of jail", "No call of chdir(\"/\") immediately "
|
|
|
|
"after chroot"));
|
2019-09-10 04:34:40 +08:00
|
|
|
C.emitReport(std::make_unique<PathSensitiveBugReport>(
|
2015-06-23 21:15:32 +08:00
|
|
|
*BT_BreakJail, BT_BreakJail->getDescription(), N));
|
2010-10-10 13:45:30 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-24 03:38:39 +08:00
|
|
|
|
|
|
|
void ento::registerChrootChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ChrootChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterChrootChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|