Skip to content

Commit 2cd2e37

Browse files
committed
Shared: prevent use-use flow through implicit reads
1 parent 716133b commit 2cd2e37

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

shared/dataflow/codeql/dataflow/internal/DataFlowImpl.qll

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
179179

180180
Node asNode() { this = TNodeNormal(result) }
181181

182+
/** Gets the corresponding Node if this is a normal node or its post-implicit read node. */
183+
Node asNodeOrImplicitRead() {
184+
this = TNodeNormal(result) or this = TNodeImplicitRead(result, true)
185+
}
186+
182187
predicate isImplicitReadNode(Node n, boolean hasRead) { this = TNodeImplicitRead(n, hasRead) }
183188

184189
ParameterNode asParamReturnNode() { this = TParamReturnNode(result, _) }
@@ -247,6 +252,16 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
247252
ReturnKindExt getKind() { result = pos.getKind() }
248253
}
249254

255+
/** If `node` corresponds to a sink, gets the normal node for that sink. */
256+
pragma[nomagic]
257+
private NodeEx toNormalSinkNodeEx(NodeEx node) {
258+
exists(Node n |
259+
node.asNodeOrImplicitRead() = n and
260+
(Config::isSink(n) or Config::isSink(n, _)) and
261+
result.asNode() = n
262+
)
263+
}
264+
250265
private predicate inBarrier(NodeEx node) {
251266
exists(Node n |
252267
node.asNode() = n and
@@ -266,7 +281,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
266281

267282
private predicate outBarrier(NodeEx node) {
268283
exists(Node n |
269-
node.asNode() = n and
284+
node.asNodeOrImplicitRead() = n and
270285
Config::isBarrierOut(n)
271286
|
272287
Config::isSink(n, _)
@@ -278,7 +293,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
278293
pragma[nomagic]
279294
private predicate outBarrier(NodeEx node, FlowState state) {
280295
exists(Node n |
281-
node.asNode() = n and
296+
node.asNodeOrImplicitRead() = n and
282297
Config::isBarrierOut(n, state)
283298
|
284299
Config::isSink(n, state)
@@ -324,7 +339,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
324339

325340
pragma[nomagic]
326341
private predicate sinkNodeWithState(NodeEx node, FlowState state) {
327-
Config::isSink(node.asNode(), state) and
342+
Config::isSink(node.asNodeOrImplicitRead(), state) and
328343
not fullBarrier(node) and
329344
not stateBarrier(node, state)
330345
}
@@ -386,7 +401,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
386401
*/
387402
private predicate additionalLocalFlowStep(NodeEx node1, NodeEx node2, string model) {
388403
exists(Node n1, Node n2 |
389-
node1.asNode() = n1 and
404+
node1.asNodeOrImplicitRead() = n1 and
390405
node2.asNode() = n2 and
391406
Config::isAdditionalFlowStep(pragma[only_bind_into](n1), pragma[only_bind_into](n2), model) and
392407
getNodeEnclosingCallable(n1) = getNodeEnclosingCallable(n2) and
@@ -395,8 +410,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
395410
or
396411
exists(Node n |
397412
node1.isImplicitReadNode(n, true) and
398-
node2.asNode() = n and
399-
not fullBarrier(node2) and
413+
node2.isImplicitReadNode(n, false) and
400414
model = ""
401415
)
402416
}
@@ -405,7 +419,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
405419
NodeEx node1, FlowState s1, NodeEx node2, FlowState s2
406420
) {
407421
exists(Node n1, Node n2 |
408-
node1.asNode() = n1 and
422+
node1.asNodeOrImplicitRead() = n1 and
409423
node2.asNode() = n2 and
410424
Config::isAdditionalFlowStep(pragma[only_bind_into](n1), s1, pragma[only_bind_into](n2), s2) and
411425
getNodeEnclosingCallable(n1) = getNodeEnclosingCallable(n2) and
@@ -431,7 +445,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
431445
*/
432446
private predicate additionalJumpStep(NodeEx node1, NodeEx node2, string model) {
433447
exists(Node n1, Node n2 |
434-
node1.asNode() = n1 and
448+
node1.asNodeOrImplicitRead() = n1 and
435449
node2.asNode() = n2 and
436450
Config::isAdditionalFlowStep(pragma[only_bind_into](n1), pragma[only_bind_into](n2), model) and
437451
getNodeEnclosingCallable(n1) != getNodeEnclosingCallable(n2) and
@@ -442,7 +456,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
442456

443457
private predicate additionalJumpStateStep(NodeEx node1, FlowState s1, NodeEx node2, FlowState s2) {
444458
exists(Node n1, Node n2 |
445-
node1.asNode() = n1 and
459+
node1.asNodeOrImplicitRead() = n1 and
446460
node2.asNode() = n2 and
447461
Config::isAdditionalFlowStep(pragma[only_bind_into](n1), s1, pragma[only_bind_into](n2), s2) and
448462
getNodeEnclosingCallable(n1) != getNodeEnclosingCallable(n2) and
@@ -735,7 +749,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
735749
additional predicate sinkNode(NodeEx node, FlowState state) {
736750
fwdFlow(node) and
737751
fwdFlowState(state) and
738-
Config::isSink(node.asNode())
752+
Config::isSink(node.asNodeOrImplicitRead())
739753
or
740754
fwdFlow(node) and
741755
fwdFlowState(state) and
@@ -1058,7 +1072,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
10581072

10591073
private predicate sinkModel(NodeEx node, string model) {
10601074
sinkNode(node, _) and
1061-
exists(Node n | n = node.asNode() |
1075+
exists(Node n | n = node.asNodeOrImplicitRead() |
10621076
knownSinkModel(n, model)
10631077
or
10641078
not knownSinkModel(n, _) and model = ""
@@ -3931,7 +3945,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
39313945
TPathNodeSink(NodeEx node, FlowState state) {
39323946
exists(PathNodeMid sink |
39333947
sink.isAtSink(_) and
3934-
node = sink.getNodeEx() and
3948+
node = toNormalSinkNodeEx(sink.getNodeEx()) and
39353949
state = sink.getState()
39363950
)
39373951
} or
@@ -4416,7 +4430,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
44164430

44174431
PathNodeSink projectToSink(string model) {
44184432
this.isAtSink(model) and
4419-
result.getNodeEx() = node and
4433+
result.getNodeEx() = toNormalSinkNodeEx(node) and
44204434
result.getState() = state
44214435
}
44224436
}
@@ -5309,7 +5323,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
53095323
private predicate revSinkNode(NodeEx node, FlowState state) {
53105324
sinkNodeWithState(node, state)
53115325
or
5312-
Config::isSink(node.asNode()) and
5326+
Config::isSink(node.asNodeOrImplicitRead()) and
53135327
relevantState(state) and
53145328
not fullBarrier(node) and
53155329
not stateBarrier(node, state)

0 commit comments

Comments
 (0)