Skip to content

Commit 9f90549

Browse files
committed
Java: Add support for additional read and store steps and additional nodes.
1 parent 7e04ac5 commit 9f90549

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

java/ql/lib/semmle/code/java/dataflow/FlowSteps.qll

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ abstract class FluentMethod extends ValuePreservingMethod {
5757
override predicate returnsValue(int arg) { arg = -1 }
5858
}
5959

60+
/**
61+
* A unit class for adding additional data flow nodes.
62+
*
63+
* Extend this class to add additional data flow nodes for use in globally
64+
* applicable additional steps.
65+
*/
66+
class AdditionalDataFlowNode extends Unit {
67+
/**
68+
* Holds if an additional node is needed in relation to `e`. The pair `(e,id)`
69+
* must uniquely identify the node.
70+
* The added node can be selected for use in a predicate by the corresponding
71+
* `DataFlow::AdditionalNode.nodeAt(Expr e, string id)` predicate.
72+
*/
73+
abstract predicate nodeAt(Expr e, string id);
74+
}
75+
6076
/**
6177
* A unit class for adding additional taint steps.
6278
*
@@ -85,6 +101,36 @@ class AdditionalValueStep extends Unit {
85101
abstract predicate step(DataFlow::Node node1, DataFlow::Node node2);
86102
}
87103

104+
/**
105+
* A unit class for adding additional store steps.
106+
*
107+
* Extend this class to add additional store steps that should apply to all
108+
* data flow configurations. A store step must be local, so non-local steps are
109+
* ignored.
110+
*/
111+
class AdditionalStoreStep extends Unit {
112+
/**
113+
* Holds if the step from `node1` to `node2` is a store step of `c` and should
114+
* apply to all data flow configurations.
115+
*/
116+
abstract predicate step(DataFlow::Node node1, DataFlow::Content c, DataFlow::Node node2);
117+
}
118+
119+
/**
120+
* A unit class for adding additional read steps.
121+
*
122+
* Extend this class to add additional read steps that should apply to all
123+
* data flow configurations. A read step must be local, so non-local steps are
124+
* ignored.
125+
*/
126+
class AdditionalReadStep extends Unit {
127+
/**
128+
* Holds if the step from `node1` to `node2` is a read step of `c` and should
129+
* apply to all data flow configurations.
130+
*/
131+
abstract predicate step(DataFlow::Node node1, DataFlow::Content c, DataFlow::Node node2);
132+
}
133+
88134
/**
89135
* A method or constructor that preserves taint.
90136
*

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ private import semmle.code.java.dataflow.InstanceAccess
33
private import semmle.code.java.dataflow.ExternalFlow
44
private import semmle.code.java.dataflow.FlowSummary
55
private import semmle.code.java.dataflow.TypeFlow
6+
private import semmle.code.java.dataflow.FlowSteps
67
private import DataFlowPrivate
78
private import DataFlowUtil
89
private import FlowSummaryImpl as FlowSummaryImpl
@@ -56,7 +57,8 @@ private module Cached {
5657
} or
5758
TFlowSummaryNode(FlowSummaryImpl::Private::SummaryNode sn) or
5859
TFieldValueNode(Field f) or
59-
TCaptureNode(CaptureFlow::SynthesizedCaptureNode cn)
60+
TCaptureNode(CaptureFlow::SynthesizedCaptureNode cn) or
61+
TAdditionalNode(Expr e, string id) { any(AdditionalDataFlowNode adfn).nodeAt(e, id) }
6062

6163
cached
6264
newtype TContent =
@@ -133,6 +135,8 @@ module Public {
133135
result = this.(CaptureNode).getTypeImpl()
134136
or
135137
result = this.(FieldValueNode).getField().getType()
138+
or
139+
result instanceof TypeObject and this instanceof AdditionalNode
136140
}
137141

138142
/** Gets the callable in which this node occurs. */
@@ -335,6 +339,21 @@ module Public {
335339
/** Holds if this is an access to an object's own instance. */
336340
predicate isOwnInstanceAccess() { this.getInstanceAccess().isOwnInstanceAccess() }
337341
}
342+
343+
/** A node introduced by an extension of `AdditionalDataFlowNode`. */
344+
class AdditionalNode extends Node, TAdditionalNode {
345+
Expr e_;
346+
string id_;
347+
348+
AdditionalNode() { this = TAdditionalNode(e_, id_) }
349+
350+
override string toString() { result = e_.toString() + " (" + id_ + ")" }
351+
352+
override Location getLocation() { result = e_.getLocation() }
353+
354+
/** Holds if this node was introduced by `AdditionalDataFlowNode.nodeAt(e, id)`. */
355+
predicate nodeAt(Expr e, string id) { e = e_ and id = id_ }
356+
}
338357
}
339358

340359
private import Public
@@ -378,7 +397,8 @@ module Private {
378397
result = nodeGetEnclosingCallable(n.(ImplicitPostUpdateNode).getPreUpdateNode()) or
379398
result.asSummarizedCallable() = n.(FlowSummaryNode).getSummarizedCallable() or
380399
result.asCallable() = n.(CaptureNode).getSynthesizedCaptureNode().getEnclosingCallable() or
381-
result.asFieldScope() = n.(FieldValueNode).getField()
400+
result.asFieldScope() = n.(FieldValueNode).getField() or
401+
result.asCallable() = any(Expr e | n.(AdditionalNode).nodeAt(e, _)).getEnclosingCallable()
382402
}
383403

384404
/** Holds if `p` is a `ParameterNode` of `c` with position `pos`. */

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ predicate storeStep(Node node1, ContentSet f, Node node2) {
228228
node2.(FlowSummaryNode).getSummaryNode())
229229
or
230230
captureStoreStep(node1, f, node2)
231+
or
232+
any(AdditionalStoreStep a).step(node1, f, node2) and
233+
pragma[only_bind_out](node1.getEnclosingCallable()) =
234+
pragma[only_bind_out](node2.getEnclosingCallable())
231235
}
232236

233237
/**
@@ -262,6 +266,10 @@ predicate readStep(Node node1, ContentSet f, Node node2) {
262266
node2.(FlowSummaryNode).getSummaryNode())
263267
or
264268
captureReadStep(node1, f, node2)
269+
or
270+
any(AdditionalReadStep a).step(node1, f, node2) and
271+
pragma[only_bind_out](node1.getEnclosingCallable()) =
272+
pragma[only_bind_out](node2.getEnclosingCallable())
265273
}
266274

267275
/**

0 commit comments

Comments
 (0)