blob: 14587fb5163b327699f9c8d5db4c8c5812b21249 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines chroot checker, which checks improper use of chroot.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis2d3905f2011-02-15 21:25:03 +000014#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Zhongxing Xu271c9c72010-10-10 05:45:30 +000022#include "llvm/ADT/ImmutableMap.h"
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +000023
Zhongxing Xu271c9c72010-10-10 05:45:30 +000024using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000026
27namespace {
28
29// enum value that represent the jail state
30enum Kind { NO_CHROOT, ROOT_CHANGED, JAIL_ENTERED };
Ted Kremenek3a0678e2015-09-08 03:50:52 +000031
Zhongxing Xu271c9c72010-10-10 05:45:30 +000032bool isRootChanged(intptr_t k) { return k == ROOT_CHANGED; }
Chris Lattner29e78ac2010-10-11 05:38:10 +000033//bool isJailEntered(intptr_t k) { return k == JAIL_ENTERED; }
Zhongxing Xu271c9c72010-10-10 05:45:30 +000034
35// This checker checks improper use of chroot.
36// The state transition:
37// NO_CHROOT ---chroot(path)--> ROOT_CHANGED ---chdir(/) --> JAIL_ENTERED
38// | |
39// ROOT_CHANGED<--chdir(..)-- JAIL_ENTERED<--chdir(..)--
40// | |
41// bug<--foo()-- JAIL_ENTERED<--foo()--
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000042class ChrootChecker : public Checker<eval::Call, check::PreStmt<CallExpr> > {
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000043 mutable IdentifierInfo *II_chroot, *II_chdir;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000044 // This bug refers to possibly break out of a chroot() jail.
Ahmed Charlesb8984322014-03-07 20:03:18 +000045 mutable std::unique_ptr<BuiltinBug> BT_BreakJail;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000046
47public:
Craig Topper0dbb7832014-05-27 02:45:47 +000048 ChrootChecker() : II_chroot(nullptr), II_chdir(nullptr) {}
49
Zhongxing Xu271c9c72010-10-10 05:45:30 +000050 static void *getTag() {
51 static int x;
52 return &x;
53 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +000054
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000055 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
56 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000057
58private:
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000059 void Chroot(CheckerContext &C, const CallExpr *CE) const;
60 void Chdir(CheckerContext &C, const CallExpr *CE) const;
Zhongxing Xu271c9c72010-10-10 05:45:30 +000061};
62
63} // end anonymous namespace
64
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000065bool ChrootChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksc6aa5312011-12-01 05:57:37 +000066 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu271c9c72010-10-10 05:45:30 +000067 if (!FD)
68 return false;
69
70 ASTContext &Ctx = C.getASTContext();
71 if (!II_chroot)
72 II_chroot = &Ctx.Idents.get("chroot");
73 if (!II_chdir)
74 II_chdir = &Ctx.Idents.get("chdir");
75
76 if (FD->getIdentifier() == II_chroot) {
77 Chroot(C, CE);
78 return true;
79 }
80 if (FD->getIdentifier() == II_chdir) {
81 Chdir(C, CE);
82 return true;
83 }
84
85 return false;
86}
87
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000088void ChrootChecker::Chroot(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000089 ProgramStateRef state = C.getState();
Ted Kremenek001fd5b2011-08-15 22:09:50 +000090 ProgramStateManager &Mgr = state->getStateManager();
Ted Kremenek3a0678e2015-09-08 03:50:52 +000091
92 // Once encouter a chroot(), set the enum value ROOT_CHANGED directly in
Zhongxing Xu271c9c72010-10-10 05:45:30 +000093 // the GDM.
94 state = Mgr.addGDM(state, ChrootChecker::getTag(), (void*) ROOT_CHANGED);
Anna Zaksda4c8d62011-10-26 21:06:34 +000095 C.addTransition(state);
Zhongxing Xu271c9c72010-10-10 05:45:30 +000096}
97
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +000098void ChrootChecker::Chdir(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000099 ProgramStateRef state = C.getState();
Ted Kremenek001fd5b2011-08-15 22:09:50 +0000100 ProgramStateManager &Mgr = state->getStateManager();
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000101
102 // If there are no jail state in the GDM, just return.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000103 const void *k = state->FindGDM(ChrootChecker::getTag());
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000104 if (!k)
105 return;
106
107 // After chdir("/"), enter the jail, set the enum value JAIL_ENTERED.
108 const Expr *ArgExpr = CE->getArg(0);
Ted Kremenek632e3b72012-01-06 22:09:28 +0000109 SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000110
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000111 if (const MemRegion *R = ArgVal.getAsRegion()) {
112 R = R->StripCasts();
113 if (const StringRegion* StrRegion= dyn_cast<StringRegion>(R)) {
114 const StringLiteral* Str = StrRegion->getStringLiteral();
115 if (Str->getString() == "/")
116 state = Mgr.addGDM(state, ChrootChecker::getTag(),
117 (void*) JAIL_ENTERED);
118 }
119 }
120
Anna Zaksda4c8d62011-10-26 21:06:34 +0000121 C.addTransition(state);
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000122}
123
124// Check the jail state before any function call except chroot and chdir().
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000125void ChrootChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksc6aa5312011-12-01 05:57:37 +0000126 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000127 if (!FD)
128 return;
129
130 ASTContext &Ctx = C.getASTContext();
131 if (!II_chroot)
132 II_chroot = &Ctx.Idents.get("chroot");
133 if (!II_chdir)
134 II_chdir = &Ctx.Idents.get("chdir");
135
136 // Ingnore chroot and chdir.
137 if (FD->getIdentifier() == II_chroot || FD->getIdentifier() == II_chdir)
138 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000139
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000140 // If jail state is ROOT_CHANGED, generate BugReport.
Anna Zaksc6aa5312011-12-01 05:57:37 +0000141 void *const* k = C.getState()->FindGDM(ChrootChecker::getTag());
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000142 if (k)
143 if (isRootChanged((intptr_t) *k))
Devin Coughline39bd402015-09-16 22:03:05 +0000144 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000145 if (!BT_BreakJail)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000146 BT_BreakJail.reset(new BuiltinBug(
147 this, "Break out of jail", "No call of chdir(\"/\") immediately "
148 "after chroot"));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000149 C.emitReport(llvm::make_unique<BugReport>(
150 *BT_BreakJail, BT_BreakJail->getDescription(), N));
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000151 }
Zhongxing Xu271c9c72010-10-10 05:45:30 +0000152}
Argyrios Kyrtzidisda02a252011-02-23 19:38:39 +0000153
154void ento::registerChrootChecker(CheckerManager &mgr) {
155 mgr.registerChecker<ChrootChecker>();
156}