Skip to content

Commit f7d3a51

Browse files
committed
Rust: Implement barrier guard.
1 parent cdd5cb0 commit f7d3a51

File tree

3 files changed

+41
-289
lines changed

3 files changed

+41
-289
lines changed

rust/ql/lib/codeql/rust/security/UncontrolledAllocationSizeExtensions.qll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import rust
77
private import codeql.rust.Concepts
88
private import codeql.rust.dataflow.DataFlow
99
private import codeql.rust.dataflow.FlowSink
10+
private import codeql.rust.controlflow.ControlFlowGraph as Cfg
11+
private import codeql.rust.controlflow.CfgNodes as CfgNodes
1012

1113
/**
1214
* Provides default sources, sinks and barriers for detecting uncontrolled
@@ -26,9 +28,25 @@ module UncontrolledAllocationSize {
2628
abstract class Barrier extends DataFlow::Node { }
2729

2830
/**
29-
* sink for uncontrolled allocation size from model data.
31+
* A sink for uncontrolled allocation size from model data.
3032
*/
3133
private class ModelsAsDataSink extends Sink {
3234
ModelsAsDataSink() { sinkNode(this, ["alloc-size", "alloc-layout"]) }
3335
}
36+
37+
/**
38+
* A barrier for uncontrolled allocation size that is an guard / bound check.
39+
*/
40+
private class BoundCheckBarrier extends Barrier {
41+
BoundCheckBarrier() { this = DataFlow::BarrierGuard<isBoundCheck/3>::getABarrierNode() }
42+
}
43+
44+
private predicate isBoundCheck(CfgNodes::AstCfgNode g, Cfg::CfgNode node, boolean branch) {
45+
// any comparison (`g` / `cmp`) guards the expression on either side (`node`)
46+
exists(BinaryExpr cmp |
47+
g = cmp.getACfgNode() and
48+
[cmp.getLhs(), cmp.getRhs()].getACfgNode() = node and
49+
branch = [true, false]
50+
)
51+
}
3452
}

0 commit comments

Comments
 (0)