blob: 0daf147dbbdb606e24d35c9d368090163d9e1456 [file] [log] [blame]
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001//===- Chrootchecker.cpp -------- Basic security checks ---------*- C++ -*-===//
Zhongxing Xu271c9c72010-10-10 05:45:30 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Xu271c9c72010-10-10 05:45:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines chroot checker, which checks improper use of chroot.
10//
11//===----------------------------------------------------------------------===//
12
Kristof Umann76a21502018-12-15 16:23:51 +000013#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +000021
Zhongxing Xu271c9c72010-10-10 05:45:30 +000022using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000024
25namespace {
26
27// enum value that represent the jail state
28enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED };
Ted Kremenek3a0678e2015-09-08 03:50:52 +000029
Zhongxing Xu271c9c72010-10-10 05:45:30 +000030bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; }
Chris Lattner29e78ac2010-10-11 05:38:10 +000031//bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
Zhongxing Xu271c9c72010-10-10 05:45:30 +000032
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 Kyrtzidis6a5674f2011-03-01 01:16:21 +000040class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > {
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000041 mutable IdentifierInfo *II_chroot, *II_chdir;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000042 // This bug refers to possibly break out of a chroot() jail.
Ahmed Charlesb8984322014-03-07 20:03:18 +000043 mutable std::unique_ptr<BuiltinBug> BT_BreakJail;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000044
45public:
Craig Topper0dbb7832014-05-27 02:45:47 +000046 ChrootChecker() : II_chroot(nullptr), II_chdir(nullptr) {}
47
Zhongxing Xu271c9c72010-10-10 05:45:30 +000048 static void *getTag() {
49 static int x;
50 return &x;
51 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +000052
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000053 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
54 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000055
56private:
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000057 void Chroot(CheckerContext &C, const CallExpr *CE) const;
58 void Chdir(CheckerContext &C, const CallExpr *CE) const;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000059};
60
61} // end anonymous namespace
62
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000063bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksc6aa5312011-12-01 05:57:37 +000064 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu271c9c72010-10-10 05:45:30 +000065 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 Kyrtzidisda02a252011-02-23 19:38:39 +000086void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000087 ProgramStateRef state = C.getState();
Ted Kremenek001fd5b2011-08-15 22:09:50 +000088 ProgramStateManager &Mgr = state->getStateManager();
Ted Kremenek3a0678e2015-09-08 03:50:52 +000089
90 // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
Zhongxing Xu271c9c72010-10-10 05:45:30 +000091 // the GDM.
92 state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED);
Anna Zaksda4c8d62011-10-26 21:06:34 +000093 C.addTransition(state);
Zhongxing Xu271c9c72010-10-10 05:45:30 +000094}
95
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000096void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000097 ProgramStateRef state = C.getState();
Ted Kremenek001fd5b2011-08-15 22:09:50 +000098 ProgramStateManager &Mgr = state->getStateManager();
Zhongxing Xu271c9c72010-10-10 05:45:30 +000099
100 // If there are no jail state in the GDM, just return.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000101 const void *k = state->FindGDM(ChrootChecker::getTag());
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000102 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 Karpenkovd703ec92018-01-17 20:27:29 +0000107 SVal ArgVal = C.getSVal(ArgExpr);
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000108
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000109 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 Zaksda4c8d62011-10-26 21:06:34 +0000119 C.addTransition(state);
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000120}
121
122// Check the jail state before any function call except chroot and chdir().
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000123void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksc6aa5312011-12-01 05:57:37 +0000124 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000125 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 Kornienko2a8c18d2018-04-06 15:14:32 +0000134 // Ignore chroot and chdir.
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000135 if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir)
136 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000137
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000138 // If jail state is ROOT_CHANGED, generate BugReport.
Anna Zaksc6aa5312011-12-01 05:57:37 +0000139 void *const* k = C.getState()->FindGDM(ChrootChecker::getTag());
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000140 if (k)
141 if (isRootChanged((intptr_t) *k))
Devin Coughline39bd402015-09-16 22:03:05 +0000142 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000143 if (!BT_BreakJail)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000144 BT_BreakJail.reset(new BuiltinBug(
145 this, "Break out of jail", "No call of chdir(\"/\") immediately "
146 "after chroot"));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000147 C.emitReport(llvm::make_unique<BugReport>(
148 *BT_BreakJail, BT_BreakJail->getDescription(), N));
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000149 }
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000150}
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000151
152void ento::registerChrootChecker(CheckerManager &mgr) {
153 mgr.registerChecker<ChrootChecker>();
154}
Kristof Umann058a7a42019-01-26 14:23:08 +0000155
156bool ento::shouldRegisterChrootChecker(const LangOptions &LO) {
157 return true;
158}