2010-10-10 13:45:30 +08:00
|
|
|
//===- Chrootchecker.cpp -------- Basic security checks ----------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines chroot checker, which checks improper use of chroot.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-16 05:25:03 +08:00
|
|
|
#include "ClangSACheckers.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"
|
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"
|
2010-10-10 13:45:30 +08:00
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
|
|
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 };
|
|
|
|
|
|
|
|
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()--
|
2011-03-01 09:16:21 +08:00
|
|
|
class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > {
|
2011-02-24 03:38:39 +08:00
|
|
|
mutable IdentifierInfo *II_chroot, *II_chdir;
|
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
|
|
|
|
|
|
|
public:
|
2014-05-27 10:45:47 +08:00
|
|
|
ChrootChecker() : II_chroot(nullptr), II_chdir(nullptr) {}
|
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
static void *getTag() {
|
|
|
|
static int x;
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
bool evalCall(const CallExpr *CE, CheckerContext &C) const;
|
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
2010-10-10 13:45:30 +08:00
|
|
|
|
|
|
|
private:
|
2011-02-24 03:38:39 +08:00
|
|
|
void Chroot(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void Chdir(CheckerContext &C, const CallExpr *CE) const;
|
2010-10-10 13:45:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2010-10-10 13:45:30 +08:00
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
if (!II_chroot)
|
|
|
|
II_chroot = &Ctx.Idents.get("chroot");
|
|
|
|
if (!II_chdir)
|
|
|
|
II_chdir = &Ctx.Idents.get("chdir");
|
|
|
|
|
|
|
|
if (FD->getIdentifier() == II_chroot) {
|
|
|
|
Chroot(C, CE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (FD->getIdentifier() == II_chdir) {
|
|
|
|
Chdir(C, CE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) 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
|
|
|
|
|
|
|
// Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) 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.
|
|
|
|
const Expr *ArgExpr = CE->getArg(0);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
|
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().
|
2011-02-24 03:38:39 +08:00
|
|
|
void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2010-10-10 13:45:30 +08:00
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
if (!II_chroot)
|
|
|
|
II_chroot = &Ctx.Idents.get("chroot");
|
|
|
|
if (!II_chdir)
|
|
|
|
II_chdir = &Ctx.Idents.get("chdir");
|
|
|
|
|
|
|
|
// Ingnore chroot and chdir.
|
|
|
|
if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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))
|
2011-10-27 05:06:34 +08:00
|
|
|
if (ExplodedNode *N = C.addTransition()) {
|
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"));
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(llvm::make_unique<BugReport>(
|
|
|
|
*BT_BreakJail, BT_BreakJail->getDescription(), N));
|
2010-10-10 13:45:30 +08:00
|
|
|
}
|
2015-06-23 21:15:32 +08:00
|
|
|
|
2010-10-10 13:45:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-24 03:38:39 +08:00
|
|
|
|
|
|
|
void ento::registerChrootChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ChrootChecker>();
|
|
|
|
}
|