-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtaint.cpp
338 lines (311 loc) · 10.2 KB
/
taint.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Tooling.h"
#include <map>
#include <set>
namespace json {
class JSON;
}
#include "dbjson.hpp"
using namespace clang::tooling;
using namespace llvm;
using namespace clang;
using namespace ast_matchers;
typedef std::string name_t;
typedef int to_index;
typedef int from_index;
typedef std::multimap<name_t,std::pair<to_index,from_index>> db_t;
typedef std::vector<std::pair<to_index,from_index>> data_t;
typedef std::unordered_map<name_t,data_t> ndb_t;
ndb_t tracked_functions;
ndb_t tracked_functions_regex;
//database
int load_taint_function_database(std::string filepath, ndb_t &exact, ndb_t ®ex);
//loads database from file (called from main)
void load_database(std::string filepath){
load_taint_function_database(filepath, tracked_functions,tracked_functions_regex);
}
//get taint info for function from database
const data_t* match_database(name_t name){
//fullname
for(auto &f : tracked_functions){
if(name.compare(f.first) == 0)
return &f.second;
}
//regex
for(auto &f : tracked_functions_regex){
if(name.compare(0,f.first.length(),f.first) == 0)
return &f.second;
}
//not found
return nullptr;
}
//declaration counting
DeclarationMatcher DeclCountMatcher =
functionDecl(
forEachDescendant(
varDecl(
unless(
parmVarDecl()
)
).bind("v")
)
)
;
//returns number of variable declarations within funtion's body
//note, if passed decl has no body, will return 0
int internal_declcount(const FunctionDecl *F){
class : public MatchFinder::MatchCallback{
public:
virtual void run(const MatchFinder::MatchResult &Result) {
ASTContext &Context = *Result.Context;
SourceManager &SM = *Result.SourceManager;
if(auto v = Result.Nodes.getNodeAs<VarDecl>("v")){
count++;
}
}
int count = 0;
}Callback;
MatchFinder ArgFinder;
ArgFinder.addMatcher(DeclCountMatcher, &Callback);
ArgFinder.match(*F,F->getASTContext());
return Callback.count;
}
//param tainting
//matches initializations with given variable
//all variables initialized with expression referencing given variable will be matched
DeclarationMatcher InitMatcher(const Decl *arg){
return
functionDecl(
forEachDescendant(
varDecl(
hasDescendant(
declRefExpr(
to(
varDecl(
equalsNode(arg)
).bind("source_arg")
)
)
)
).bind("init_arg")
)
).bind("f")
;
}
//matches assignments with given variable
//all variables which are assigned to with expression referencing given variable will be matched
DeclarationMatcher AssignMatcher(const Decl *arg){
return
functionDecl(
forEachDescendant(
binaryOperator(
anyOf(
hasOperatorName("="),
hasOperatorName("+="),
hasOperatorName("-="),
hasOperatorName("*="),
hasOperatorName("/="),
hasOperatorName("%="),
hasOperatorName("<<="),
hasOperatorName(">>="),
hasOperatorName("&="),
hasOperatorName("^="),
hasOperatorName("|=")
),
hasLHS(
anyOf(
declRefExpr(
to(
varDecl().bind("assign_arg")
)
),
hasDescendant(
declRefExpr(
to(
varDecl().bind("assign_arg")
)
)
)
)
),
hasRHS(
hasDescendant(
declRefExpr(
to(
varDecl(
equalsNode(arg)
).bind("source_arg")
)
)
)
)
)
)
).bind("f")
;
}
//matches call expressions with given variable used in any argument
DeclarationMatcher CallMatcher(const Decl *arg){
return
functionDecl(
forEachDescendant(
callExpr(
callee(
functionDecl().bind("call_func")
),
hasAnyArgument(
anyOf(
declRefExpr(
to(
varDecl(
equalsNode(arg)
).bind("source_arg")
)
),
hasDescendant(
declRefExpr(
to(
varDecl(
equalsNode(arg)
).bind("source_arg")
)
)
)
)
)
).bind("call")
)
).bind("f")
;
}
//matches variables passed to function as certain argument
StatementMatcher ArgMatcher(to_index to_index, from_index from_index, const Decl *arg){
return
callExpr(
hasArgument(
to_index,
anyOf(
declRefExpr(
to(
varDecl().bind("function_arg")
)
),
hasDescendant(
declRefExpr(
to(
varDecl().bind("function_arg")
)
)
)
)
),
hasArgument(
from_index,
anyOf(
declRefExpr(
to(
varDecl(
equalsNode(arg)
)
)
),
hasDescendant(
declRefExpr(
to(
varDecl(
equalsNode(arg)
)
)
)
)
)
)
)
;
}
void taint_params(const FunctionDecl *f, DbJSONClassVisitor::FuncData &func_data){
DbJSONClassVisitor::taintdata_t& data = func_data.taintdata;
for(auto param : f->parameters()){
int param_index = param->getFunctionScopeIndex();
class : public MatchFinder::MatchCallback{
public:
virtual void run(const MatchFinder::MatchResult &Result) {
ASTContext &Context = *Result.Context;
SourceManager &SM = *Result.SourceManager;
if(auto local_arg = Result.Nodes.getNodeAs<VarDecl>("init_arg")){
if(!local_arg->isDefinedOutsideFunctionOrMethod() && !already(local_arg))
tainted.push_back(local_arg);
}
if(auto local_arg = Result.Nodes.getNodeAs<VarDecl>("assign_arg")){
if(!local_arg->isDefinedOutsideFunctionOrMethod() && !already(local_arg))
tainted.push_back(local_arg);
}
if(auto local_arg = Result.Nodes.getNodeAs<VarDecl>("function_arg")){
if(!local_arg->isDefinedOutsideFunctionOrMethod() && !already(local_arg))
tainted.push_back(local_arg);
}
if(auto call_func = Result.Nodes.getNodeAs<FunctionDecl>("call_func")){
auto source_arg = Result.Nodes.getNodeAs<VarDecl>("source_arg");
auto call_expr = Result.Nodes.getNodeAs<CallExpr>("call");
const data_t *data = match_database(call_func->getNameAsString());
if(!data) return;
for(auto taint : *data){
MatchFinder ArgFinder;
//normal function taint
if(taint.first && taint.second){
ArgFinder.addMatcher(ArgMatcher(taint.first-1,taint.second-1,source_arg),this);
ArgFinder.match(*call_expr,Context);
}
//to variadic argument
else if(taint.first == 0){
if(!call_func->isVariadic()) errs()<<call_func->getNameAsString()<<" IS NOT VARIADIC!\n";
for(unsigned i = call_func->getNumParams();i<call_expr->getNumArgs();i++){
ArgFinder.addMatcher(ArgMatcher(i,taint.second-1,source_arg),this);
}
ArgFinder.match(*call_expr,Context);
}
//from variadic argument
else if(taint.second == 0){
if(!call_func->isVariadic()) errs()<<call_func->getNameAsString()<<" IS NOT VARIADIC!\n";
for(unsigned i = call_func->getNumParams();i<call_expr->getNumArgs();i++){
ArgFinder.addMatcher(ArgMatcher(taint.first-1,i,source_arg),this);
}
ArgFinder.match(*call_expr,Context);
}
}
}
}
std::vector<const VarDecl*> tainted = {};
private:
bool already(const VarDecl* new_taint){
for(auto taint : this->tainted){
if(new_taint == taint) return true;
if(new_taint->getNameAsString()==taint->getNameAsString() &&
new_taint->getLocation().printToString(taint->getASTContext().getSourceManager())==taint->getLocation().printToString(taint->getASTContext().getSourceManager())){
// errs()<<new_taint->getNameAsString()<<"\n";
return true;
}
}
return false;
}
}Callback;
Callback.tainted.push_back(param);
int depth = 0;
size_t index = 0;
while(index<Callback.tainted.size()){
for(size_t size = Callback.tainted.size();index<size;index++){
const VarDecl *var = Callback.tainted[index];
std::string name = var->getNameAsString();
std::string loc = var->getLocation().printToString(var->getASTContext().getSourceManager());
data[param_index].insert({depth,var});
MatchFinder ArgFinder;
ArgFinder.addMatcher(InitMatcher(var),&Callback);
ArgFinder.addMatcher(AssignMatcher(var),&Callback);
ArgFinder.addMatcher(CallMatcher(var),&Callback);
ArgFinder.match(*f,f->getASTContext());
}
depth++;
}
}
}