@@ -7,6 +7,8 @@ import rust
7
7
private import codeql.rust.Concepts
8
8
private import codeql.rust.dataflow.DataFlow
9
9
private import codeql.rust.dataflow.FlowSink
10
+ private import codeql.rust.controlflow.ControlFlowGraph as Cfg
11
+ private import codeql.rust.controlflow.CfgNodes as CfgNodes
10
12
11
13
/**
12
14
* Provides default sources, sinks and barriers for detecting uncontrolled
@@ -26,9 +28,25 @@ module UncontrolledAllocationSize {
26
28
abstract class Barrier extends DataFlow:: Node { }
27
29
28
30
/**
29
- * sink for uncontrolled allocation size from model data.
31
+ * A sink for uncontrolled allocation size from model data.
30
32
*/
31
33
private class ModelsAsDataSink extends Sink {
32
34
ModelsAsDataSink ( ) { sinkNode ( this , [ "alloc-size" , "alloc-layout" ] ) }
33
35
}
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
+ }
34
52
}
0 commit comments