-
Notifications
You must be signed in to change notification settings - Fork 172
/
TaintAnalysis.java
191 lines (171 loc) · 7.16 KB
/
TaintAnalysis.java
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
/*
* Tai-e: A Static Analysis Framework for Java
*
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
*
* This file is part of Tai-e.
*
* Tai-e is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
*/
package pascal.taie.analysis.pta.plugin.taint;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pascal.taie.analysis.graph.callgraph.Edge;
import pascal.taie.analysis.pta.PointerAnalysisResult;
import pascal.taie.analysis.pta.core.cs.context.Context;
import pascal.taie.analysis.pta.core.cs.element.CSCallSite;
import pascal.taie.analysis.pta.core.cs.element.CSManager;
import pascal.taie.analysis.pta.core.cs.element.CSMethod;
import pascal.taie.analysis.pta.core.cs.element.CSObj;
import pascal.taie.analysis.pta.core.cs.element.CSVar;
import pascal.taie.analysis.pta.core.heap.Obj;
import pascal.taie.analysis.pta.core.solver.Solver;
import pascal.taie.analysis.pta.plugin.Plugin;
import pascal.taie.analysis.pta.pts.PointsToSet;
import pascal.taie.ir.exp.InvokeExp;
import pascal.taie.ir.exp.InvokeInstanceExp;
import pascal.taie.ir.exp.Var;
import pascal.taie.ir.stmt.Invoke;
import pascal.taie.language.classes.JMethod;
import pascal.taie.language.type.Type;
import pascal.taie.util.collection.Maps;
import pascal.taie.util.collection.MultiMap;
import pascal.taie.util.collection.Pair;
import java.util.Set;
import java.util.TreeSet;
public class TaintAnalysis implements Plugin {
private static final Logger logger = LogManager.getLogger(TaintAnalysis.class);
/**
* Map from method (which is source method) to set of types of
* taint objects returned by the method calls.
*/
private final MultiMap<JMethod, Type> sources = Maps.newMultiMap();
/**
* Map from method (which causes taint transfer) to set of relevant
* {@link TaintTransfer}.
*/
private final MultiMap<JMethod, TaintTransfer> transfers = Maps.newMultiMap();
/**
* Map from variable to taint transfer information.
* The taint objects pointed to by the "key" variable are supposed
* to be transferred to "value" variable with specified type.
*/
private final MultiMap<Var, Pair<Var, Type>> varTransfers = Maps.newMultiMap();
private Solver solver;
private CSManager csManager;
private Context emptyContext;
private TaintManager manager;
private TaintConfig config;
@Override
public void setSolver(Solver solver) {
this.solver = solver;
csManager = solver.getCSManager();
emptyContext = solver.getContextSelector().getEmptyContext();
manager = new TaintManager(solver.getHeapModel());
config = TaintConfig.readConfig(
solver.getOptions().getString("taint-config"),
solver.getHierarchy(),
solver.getTypeSystem());
logger.info(config);
config.getSources().forEach(s ->
sources.put(s.method(), s.type()));
config.getTransfers().forEach(t ->
transfers.put(t.method(), t));
}
@Override
public void onNewCallEdge(Edge<CSCallSite, CSMethod> edge) {
Invoke callSite = edge.getCallSite().getCallSite();
JMethod callee = edge.getCallee().getMethod();
// generate taint value from source call
Var lhs = callSite.getLValue();
if (lhs != null && sources.containsKey(callee)) {
sources.get(callee).forEach(type -> {
Obj taint = manager.makeTaint(callSite, type);
solver.addVarPointsTo(edge.getCallSite().getContext(), lhs,
emptyContext, taint);
});
}
// process taint transfer
transfers.get(callee).forEach(transfer -> {
Var from = getVar(callSite, transfer.from());
Var to = getVar(callSite, transfer.to());
// when transfer to result variable, and the call site
// does not have result variable, then "to" is null.
if (to != null) {
Type type = transfer.type();
varTransfers.put(from, new Pair<>(to, type));
Context ctx = edge.getCallSite().getContext();
CSVar csFrom = csManager.getCSVar(ctx, from);
transferTaint(solver.getPointsToSetOf(csFrom), ctx, to, type);
}
});
}
/**
* Retrieves variable from a call site and index.
*/
private static Var getVar(Invoke callSite, int index) {
InvokeExp invokeExp = callSite.getInvokeExp();
return switch (index) {
case TaintTransfer.BASE -> ((InvokeInstanceExp) invokeExp).getBase();
case TaintTransfer.RESULT -> callSite.getResult();
default -> invokeExp.getArg(index);
};
}
private void transferTaint(PointsToSet pts, Context ctx, Var to, Type type) {
PointsToSet newTaints = solver.makePointsToSet();
pts.objects()
.map(CSObj::getObject)
.filter(manager::isTaint)
.map(manager::getSourceCall)
.map(source -> manager.makeTaint(source, type))
.map(taint -> csManager.getCSObj(emptyContext, taint))
.forEach(newTaints::addObject);
if (!newTaints.isEmpty()) {
solver.addVarPointsTo(ctx, to, newTaints);
}
}
@Override
public void onNewPointsToSet(CSVar csVar, PointsToSet pts) {
varTransfers.get(csVar.getVar()).forEach(p -> {
Var to = p.first();
Type type = p.second();
transferTaint(pts, csVar.getContext(), to, type);
});
}
@Override
public void onFinish() {
Set<TaintFlow> taintFlows = collectTaintFlows();
solver.getResult().storeResult(getClass().getName(), taintFlows);
}
private Set<TaintFlow> collectTaintFlows() {
PointerAnalysisResult result = solver.getResult();
Set<TaintFlow> taintFlows = new TreeSet<>();
config.getSinks().forEach(sink -> {
int i = sink.index();
result.getCallGraph()
.getCallersOf(sink.method())
.forEach(sinkCall -> {
Var arg = sinkCall.getInvokeExp().getArg(i);
result.getPointsToSet(arg)
.stream()
.filter(manager::isTaint)
.map(manager::getSourceCall)
.map(sourceCall -> new TaintFlow(sourceCall, sinkCall, i))
.forEach(taintFlows::add);
});
});
return taintFlows;
}
}