Skip to content

Commit f05cce8

Browse files
committed
C++: Add a member predicate to phi nodes for checking if a phi is a read-phi and use it to restrict flow in 'cpp/invalid-pointer-deref'.
1 parent c3a7f98 commit f05cce8

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ class SsaPhiNode extends Node, TSsaPhiNode {
552552
*/
553553
final Node getAnInput(boolean fromBackEdge) {
554554
localFlowStep(result, this) and
555-
if phi.getBasicBlock().strictlyDominates(result.getBasicBlock())
555+
if phi.getBasicBlock().dominates(result.getBasicBlock())
556556
then fromBackEdge = true
557557
else fromBackEdge = false
558558
}
@@ -562,6 +562,14 @@ class SsaPhiNode extends Node, TSsaPhiNode {
562562

563563
/** Gets the source variable underlying this phi node. */
564564
Ssa::SourceVariable getSourceVariable() { result = phi.getSourceVariable() }
565+
566+
/**
567+
* Holds if this phi node is a phi-read node.
568+
*
569+
* Phi-read nodes are like normal phi nodes, but they are inserted based
570+
* on reads instead of writes.
571+
*/
572+
predicate isPhiRead() { phi.isPhiRead() }
565573
}
566574

567575
/**

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,14 @@ class PhiNode extends SsaImpl::DefinitionExt {
10121012
this instanceof SsaImpl::PhiNode or
10131013
this instanceof SsaImpl::PhiReadNode
10141014
}
1015+
1016+
/**
1017+
* Holds if this phi node is a phi-read node.
1018+
*
1019+
* Phi-read nodes are like normal phi nodes, but they are inserted based
1020+
* on reads instead of writes.
1021+
*/
1022+
predicate isPhiRead() { this instanceof SsaImpl::PhiReadNode }
10151023
}
10161024

10171025
class DefinitionExt = SsaImpl::DefinitionExt;

cpp/ql/src/experimental/Security/CWE/CWE-193/InvalidPointerDeref.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ module InvalidPointerToDerefConfig implements DataFlow::ConfigSig {
230230
pragma[inline]
231231
predicate isSink(DataFlow::Node sink) { isInvalidPointerDerefSink(sink, _, _) }
232232

233-
predicate isBarrier(DataFlow::Node node) { node = any(DataFlow::SsaPhiNode phi).getAnInput(true) }
233+
predicate isBarrier(DataFlow::Node node) {
234+
node = any(DataFlow::SsaPhiNode phi | not phi.isPhiRead()).getAnInput(true)
235+
}
234236
}
235237

236238
module InvalidPointerToDerefFlow = DataFlow::Global<InvalidPointerToDerefConfig>;

0 commit comments

Comments
 (0)