@@ -7,6 +7,8 @@ import rust
77private import codeql.rust.Concepts
88private import codeql.rust.dataflow.DataFlow
99private 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