blob: 76405a2db21d29b75c64bef49738a4a0e7f9caef [file] [log] [blame]
Anna Zaksdf18c5a2011-11-16 19:58:13 +00001//== GenericTaintChecker.cpp ----------------------------------- -*- C++ -*--=//
2//
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 checker defines the attack surface for generic taint propagation.
11//
12// The taint information produced by it might be useful to other checkers. For
13// example, checkers should report errors which involve tainted data more
14// aggressively, even if the involved symbols are under constrained.
15//
16//===----------------------------------------------------------------------===//
17#include "ClangSACheckers.h"
18#include "clang/StaticAnalyzer/Core/Checker.h"
19#include "clang/StaticAnalyzer/Core/CheckerManager.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22
23using namespace clang;
24using namespace ento;
25
26namespace {
27class GenericTaintChecker : public Checker< check::PostStmt<CallExpr> > {
28
Anna Zaks8f4caf52011-11-18 02:26:36 +000029 mutable llvm::OwningPtr<BugType> BT;
30 void initBugType() const;
31
32 /// Given a pointer argument, get the symbol of the value it contains
33 /// (points to).
34 SymbolRef getPointedToSymbol(CheckerContext &C,
35 const Expr* Arg,
36 bool IssueWarning = true) const;
Anna Zaksdf18c5a2011-11-16 19:58:13 +000037
38 /// Functions defining the attacke surface.
39 typedef void (GenericTaintChecker::*FnCheck)(const CallExpr *,
40 CheckerContext &C) const;
41 void processScanf(const CallExpr *CE, CheckerContext &C) const;
42 void processRetTaint(const CallExpr *CE, CheckerContext &C) const;
43
44public:
45 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
46};
47}
48
Anna Zaks8f4caf52011-11-18 02:26:36 +000049inline void GenericTaintChecker::initBugType() const {
50 if (!BT)
51 BT.reset(new BugType("Tainted data checking", "General"));
52}
53
Anna Zaksdf18c5a2011-11-16 19:58:13 +000054void GenericTaintChecker::checkPostStmt(const CallExpr *CE,
55 CheckerContext &C) const {
56 if (!C.getState())
57 return;
58
59 StringRef Name = C.getCalleeName(CE);
60
61 // Define the attack surface.
62 // Set the evaluation function by switching on the callee name.
63 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
64 .Case("scanf", &GenericTaintChecker::processScanf)
65 .Case("getchar", &GenericTaintChecker::processRetTaint)
66 .Default(NULL);
67
68 // If the callee isn't defined, it is not of security concern.
69 // Check and evaluate the call.
70 if (evalFunction)
71 (this->*evalFunction)(CE, C);
72
73}
Anna Zaks8f4caf52011-11-18 02:26:36 +000074
75SymbolRef GenericTaintChecker::getPointedToSymbol(CheckerContext &C,
76 const Expr* Arg,
77 bool IssueWarning) const {
78 const ProgramState *State = C.getState();
Anna Zaksdf18c5a2011-11-16 19:58:13 +000079 SVal AddrVal = State->getSVal(Arg->IgnoreParenCasts());
Anna Zakse3d250e2011-12-11 18:43:40 +000080
81 // TODO: Taint is not going to propagate?
82 if (AddrVal.isUnknownOrUndef())
83 return 0;
84
Anna Zaksdf18c5a2011-11-16 19:58:13 +000085 Loc *AddrLoc = dyn_cast<Loc>(&AddrVal);
Anna Zaks8f4caf52011-11-18 02:26:36 +000086
87 if (!AddrLoc && !IssueWarning)
88 return 0;
89
90 // If the Expr is not a location, issue a warning.
91 if (!AddrLoc) {
92 assert(IssueWarning);
93 if (ExplodedNode *N = C.generateSink(State)) {
94 initBugType();
95 BugReport *report = new BugReport(*BT, "Pointer argument is expected.",N);
96 report->addRange(Arg->getSourceRange());
97 C.EmitReport(report);
98 }
99 return 0;
100 }
101
Anna Zaksdf18c5a2011-11-16 19:58:13 +0000102 SVal Val = State->getSVal(*AddrLoc);
103 return Val.getAsSymbol();
104}
105
106
107void GenericTaintChecker::processScanf(const CallExpr *CE,
108 CheckerContext &C) const {
109 const ProgramState *State = C.getState();
110 assert(CE->getNumArgs() == 2);
111 SVal x = State->getSVal(CE->getArg(1));
112 // All arguments except for the very first one should get taint.
113 for (unsigned int i = 1; i < CE->getNumArgs(); ++i) {
114 // The arguments are pointer arguments. The data they are pointing at is
115 // tainted after the call.
116 const Expr* Arg = CE->getArg(i);
Anna Zaks8f4caf52011-11-18 02:26:36 +0000117 SymbolRef Sym = getPointedToSymbol(C, Arg);
Anna Zaksdf18c5a2011-11-16 19:58:13 +0000118 if (Sym)
119 State = State->addTaint(Sym);
120 }
121 C.addTransition(State);
122
123}
124
125void GenericTaintChecker::processRetTaint(const CallExpr *CE,
126 CheckerContext &C) const {
127 const ProgramState *NewState = C.getState()->addTaint(CE);
128 C.addTransition(NewState);
129}
130
131void ento::registerGenericTaintChecker(CheckerManager &mgr) {
132 mgr.registerChecker<GenericTaintChecker>();
133}