1*67e74705SXin Li //== TaintTesterChecker.cpp ----------------------------------- -*- C++ -*--=//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This checker can be used for testing how taint data is propagated.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li #include "ClangSACheckers.h"
14*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
15*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
16*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18*67e74705SXin Li
19*67e74705SXin Li using namespace clang;
20*67e74705SXin Li using namespace ento;
21*67e74705SXin Li
22*67e74705SXin Li namespace {
23*67e74705SXin Li class TaintTesterChecker : public Checker< check::PostStmt<Expr> > {
24*67e74705SXin Li
25*67e74705SXin Li mutable std::unique_ptr<BugType> BT;
26*67e74705SXin Li void initBugType() const;
27*67e74705SXin Li
28*67e74705SXin Li /// Given a pointer argument, get the symbol of the value it contains
29*67e74705SXin Li /// (points to).
30*67e74705SXin Li SymbolRef getPointedToSymbol(CheckerContext &C,
31*67e74705SXin Li const Expr* Arg,
32*67e74705SXin Li bool IssueWarning = true) const;
33*67e74705SXin Li
34*67e74705SXin Li public:
35*67e74705SXin Li void checkPostStmt(const Expr *E, CheckerContext &C) const;
36*67e74705SXin Li };
37*67e74705SXin Li }
38*67e74705SXin Li
initBugType() const39*67e74705SXin Li inline void TaintTesterChecker::initBugType() const {
40*67e74705SXin Li if (!BT)
41*67e74705SXin Li BT.reset(new BugType(this, "Tainted data", "General"));
42*67e74705SXin Li }
43*67e74705SXin Li
checkPostStmt(const Expr * E,CheckerContext & C) const44*67e74705SXin Li void TaintTesterChecker::checkPostStmt(const Expr *E,
45*67e74705SXin Li CheckerContext &C) const {
46*67e74705SXin Li ProgramStateRef State = C.getState();
47*67e74705SXin Li if (!State)
48*67e74705SXin Li return;
49*67e74705SXin Li
50*67e74705SXin Li if (State->isTainted(E, C.getLocationContext())) {
51*67e74705SXin Li if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
52*67e74705SXin Li initBugType();
53*67e74705SXin Li auto report = llvm::make_unique<BugReport>(*BT, "tainted",N);
54*67e74705SXin Li report->addRange(E->getSourceRange());
55*67e74705SXin Li C.emitReport(std::move(report));
56*67e74705SXin Li }
57*67e74705SXin Li }
58*67e74705SXin Li }
59*67e74705SXin Li
registerTaintTesterChecker(CheckerManager & mgr)60*67e74705SXin Li void ento::registerTaintTesterChecker(CheckerManager &mgr) {
61*67e74705SXin Li mgr.registerChecker<TaintTesterChecker>();
62*67e74705SXin Li }
63