Skip to content

Commit 1e2c798

Browse files
committed
Rust: Speedup AccessAfterLifetime.ql
1 parent 8668473 commit 1e2c798

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

rust/ql/src/queries/security/CWE-825/AccessAfterLifetime.ql

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,22 @@ import AccessAfterLifetimeFlow::PathGraph
2323
* lifetime has ended.
2424
*/
2525
module AccessAfterLifetimeConfig implements DataFlow::ConfigSig {
26-
predicate isSource(DataFlow::Node node) { node instanceof AccessAfterLifetime::Source }
26+
predicate isSource(DataFlow::Node node) {
27+
node instanceof AccessAfterLifetime::Source and
28+
// exclude cases with sources in macros, since these results are difficult to interpret
29+
not node.asExpr().getExpr().isFromMacroExpansion()
30+
}
2731

28-
predicate isSink(DataFlow::Node node) { node instanceof AccessAfterLifetime::Sink }
32+
predicate isSink(DataFlow::Node node) {
33+
node instanceof AccessAfterLifetime::Sink and
34+
// exclude cases with sinks in macros, since these results are difficult to interpret
35+
not node.asExpr().getExpr().isFromMacroExpansion() and
36+
// include only results inside `unsafe` blocks, as other results tend to be false positives
37+
(
38+
node.asExpr().getExpr().getEnclosingBlock*().isUnsafe() or
39+
node.asExpr().getExpr().getEnclosingCallable().(Function).isUnsafe()
40+
)
41+
}
2942

3043
predicate isBarrier(DataFlow::Node barrier) { barrier instanceof AccessAfterLifetime::Barrier }
3144

@@ -36,34 +49,20 @@ module AccessAfterLifetimeConfig implements DataFlow::ConfigSig {
3649
result = [target.getLocation(), source.getLocation()]
3750
|
3851
isSink(sink) and
39-
narrowDereferenceAfterLifetime(source, sink, target)
52+
AccessAfterLifetime::dereferenceAfterLifetime(source, sink, target)
4053
)
4154
}
4255
}
4356

4457
module AccessAfterLifetimeFlow = TaintTracking::Global<AccessAfterLifetimeConfig>;
4558

46-
pragma[inline]
47-
predicate narrowDereferenceAfterLifetime(DataFlow::Node source, DataFlow::Node sink, Variable target) {
48-
// check that the dereference is outside the lifetime of the target
49-
AccessAfterLifetime::dereferenceAfterLifetime(source, sink, target) and
50-
// include only results inside `unsafe` blocks, as other results tend to be false positives
51-
(
52-
sink.asExpr().getExpr().getEnclosingBlock*().isUnsafe() or
53-
sink.asExpr().getExpr().getEnclosingCallable().(Function).isUnsafe()
54-
) and
55-
// exclude cases with sources / sinks in macros, since these results are difficult to interpret
56-
not source.asExpr().getExpr().isFromMacroExpansion() and
57-
not sink.asExpr().getExpr().isFromMacroExpansion()
58-
}
59-
6059
from
6160
AccessAfterLifetimeFlow::PathNode sourceNode, AccessAfterLifetimeFlow::PathNode sinkNode,
6261
Variable target
6362
where
6463
// flow from a pointer or reference to the dereference
6564
AccessAfterLifetimeFlow::flowPath(sourceNode, sinkNode) and
6665
// check that the dereference is outside the lifetime of the target
67-
narrowDereferenceAfterLifetime(sourceNode.getNode(), sinkNode.getNode(), target)
66+
AccessAfterLifetime::dereferenceAfterLifetime(sourceNode.getNode(), sinkNode.getNode(), target)
6867
select sinkNode.getNode(), sourceNode, sinkNode,
6968
"Access of a pointer to $@ after its lifetime has ended.", target, target.toString()

0 commit comments

Comments
 (0)