aboutsummaryrefslogtreecommitdiff
path: root/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
blob: 739287284bba16abafe4a97d52fc1530dda4ba84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//== GenericTaintChecker.cpp ----------------------------------- -*- C++ -*--=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This checker defines the attack surface for generic taint propagation.
//
// The taint information produced by it might be useful to other checkers. For
// example, checkers should report errors which involve tainted data more
// aggressively, even if the involved symbols are under constrained.
//
//===----------------------------------------------------------------------===//
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"

using namespace clang;
using namespace ento;

namespace {
class GenericTaintChecker : public Checker< check::PostStmt<CallExpr>,
                                            check::PostStmt<DeclRefExpr> > {

  mutable llvm::OwningPtr<BugType> BT;
  void initBugType() const;

  /// Given a pointer argument, get the symbol of the value it contains
  /// (points to).
  SymbolRef getPointedToSymbol(CheckerContext &C,
                               const Expr* Arg,
                               bool IssueWarning = true) const;

  /// Functions defining the attacke surface.
  typedef void (GenericTaintChecker::*FnCheck)(const CallExpr *,
                                               CheckerContext &C) const;
  void processScanf(const CallExpr *CE, CheckerContext &C) const;
  void processFscanf(const CallExpr *CE, CheckerContext &C) const;
  void processRetTaint(const CallExpr *CE, CheckerContext &C) const;

  bool isStdin(const Expr *E, CheckerContext &C) const;
  bool isStdin(const DeclRefExpr *E, CheckerContext &C) const;

public:
  void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
  void checkPostStmt(const DeclRefExpr *DRE, CheckerContext &C) const;
};
}

inline void GenericTaintChecker::initBugType() const {
  if (!BT)
    BT.reset(new BugType("Tainted data checking", "General"));
}

void GenericTaintChecker::checkPostStmt(const CallExpr *CE,
                                        CheckerContext &C) const {
  if (!C.getState())
    return;

  StringRef Name = C.getCalleeName(CE);
  
  // Define the attack surface.
  // Set the evaluation function by switching on the callee name.
  FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
    .Case("scanf", &GenericTaintChecker::processScanf)
    .Case("fscanf", &GenericTaintChecker::processFscanf)
    .Case("sscanf", &GenericTaintChecker::processFscanf)
    // TODO: Add support for vfscanf & family.
    .Case("getchar", &GenericTaintChecker::processRetTaint)
    .Case("getenv", &GenericTaintChecker::processRetTaint)
    .Case("fopen", &GenericTaintChecker::processRetTaint)
    .Case("fdopen", &GenericTaintChecker::processRetTaint)
    .Case("freopen", &GenericTaintChecker::processRetTaint)
    .Default(NULL);

  // If the callee isn't defined, it is not of security concern.
  // Check and evaluate the call.
  if (evalFunction)
    (this->*evalFunction)(CE, C);
}

void GenericTaintChecker::checkPostStmt(const DeclRefExpr *DRE,
                                       CheckerContext &C) const {
  if (isStdin(DRE, C)) {
    const ProgramState *NewState = C.getState()->addTaint(DRE);
    C.addTransition(NewState);
  }
}

SymbolRef GenericTaintChecker::getPointedToSymbol(CheckerContext &C,
                                                  const Expr* Arg,
                                                  bool IssueWarning) const {
  const ProgramState *State = C.getState();
  SVal AddrVal = State->getSVal(Arg->IgnoreParens());

  // TODO: Taint is not going to propagate? Should we ever peel off the casts
  // IgnoreParenImpCasts()?
  if (AddrVal.isUnknownOrUndef()) {
    assert(State->getSVal(Arg->IgnoreParenImpCasts()).isUnknownOrUndef());
    return 0;
  }

  Loc *AddrLoc = dyn_cast<Loc>(&AddrVal);

  if (!AddrLoc && !IssueWarning)
    return 0;

  // If the Expr is not a location, issue a warning.
  if (!AddrLoc) {
    assert(IssueWarning);
    if (ExplodedNode *N = C.generateSink(State)) {
      initBugType();
      BugReport *report = new BugReport(*BT, "Pointer argument is expected.",N);
      report->addRange(Arg->getSourceRange());
      C.EmitReport(report);
    }
    return 0;
  }

  SVal Val = State->getSVal(*AddrLoc);
  return Val.getAsSymbol();
}

void GenericTaintChecker::processScanf(const CallExpr *CE,
                                       CheckerContext &C) const {
  const ProgramState *State = C.getState();
  assert(CE->getNumArgs() >= 2);
  SVal x = State->getSVal(CE->getArg(1));
  // All arguments except for the very first one should get taint.
  for (unsigned int i = 1; i < CE->getNumArgs(); ++i) {
    // The arguments are pointer arguments. The data they are pointing at is
    // tainted after the call.
    const Expr* Arg = CE->getArg(i);
    SymbolRef Sym = getPointedToSymbol(C, Arg);
    if (Sym)
      State = State->addTaint(Sym);
  }
  C.addTransition(State);
}

/// If argument 0 (file descriptor) is tainted, all arguments except for arg 0
/// and arg 1 should get taint.
void GenericTaintChecker::processFscanf(const CallExpr *CE,
                                        CheckerContext &C) const {
  const ProgramState *State = C.getState();
  assert(CE->getNumArgs() >= 2);

  // Check is the file descriptor is tainted.
  if (!State->isTainted(CE->getArg(0)) && !isStdin(CE->getArg(0), C))
    return;

  // All arguments except for the first two should get taint.
  for (unsigned int i = 2; i < CE->getNumArgs(); ++i) {
    // The arguments are pointer arguments. The data they are pointing at is
    // tainted after the call.
    const Expr* Arg = CE->getArg(i);
    SymbolRef Sym = getPointedToSymbol(C, Arg);
    if (Sym)
      State = State->addTaint(Sym);
  }
  C.addTransition(State);
}

void GenericTaintChecker::processRetTaint(const CallExpr *CE,
                                          CheckerContext &C) const {
  const ProgramState *NewState = C.getState()->addTaint(CE);
  C.addTransition(NewState);
}

bool GenericTaintChecker::isStdin(const Expr *E,
                                  CheckerContext &C) const {
  if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
    return isStdin(DR, C);
  return false;
}

bool GenericTaintChecker::isStdin(const DeclRefExpr *DR,
                                  CheckerContext &C) const {
  const VarDecl *D = dyn_cast_or_null<VarDecl>(DR->getDecl());
  if (!D)
    return false;

  D = D->getCanonicalDecl();
  if ((D->getName().find("stdin") != StringRef::npos) && D->isExternC())
    if (const PointerType * PtrTy =
          dyn_cast<PointerType>(D->getType().getTypePtr()))
      if (PtrTy->getPointeeType() == C.getASTContext().getFILEType())
        return true;

  return false;
}


void ento::registerGenericTaintChecker(CheckerManager &mgr) {
  mgr.registerChecker<GenericTaintChecker>();
}