Skip to content

Commit 8b32679

Browse files
authored
Merge pull request #20921 from paldepind/rust/barrier-tweaks
Rust: Tweaks and improvements to data flow barriers
2 parents 982950f + 329df20 commit 8b32679

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

rust/ql/lib/codeql/rust/internal/Type.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ class EnumType extends Type, TEnum {
140140

141141
EnumType() { this = TEnum(enum) }
142142

143+
/** Gets the enum that this enum type represents. */
144+
Enum getEnum() { result = enum }
145+
143146
override TypeParameter getPositionalTypeParameter(int i) {
144147
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
145148
}

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Classes to represent barriers commonly used in dataflow and taint tracking
2+
* Classes to represent barriers commonly used in data flow and taint tracking
33
* configurations.
44
*/
55

@@ -11,35 +11,33 @@ private import codeql.rust.controlflow.ControlFlowGraph as Cfg
1111
private import codeql.rust.controlflow.CfgNodes as CfgNodes
1212
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
1313

14-
/**
15-
* A node whose type is a numeric or boolean type, which may be an appropriate
16-
* taint flow barrier for some queries.
17-
*/
14+
/** A node whose type is a numeric type. */
1815
class NumericTypeBarrier extends DataFlow::Node {
1916
NumericTypeBarrier() {
20-
exists(StructType t, Struct s |
21-
t = TypeInference::inferType(this.asExpr()) and
22-
s = t.getStruct()
23-
|
24-
s instanceof Builtins::NumericType or
25-
s instanceof Builtins::Bool
26-
)
17+
TypeInference::inferType(this.asExpr()).(StructType).getStruct() instanceof
18+
Builtins::NumericType
2719
}
2820
}
2921

30-
/**
31-
* A node whose type is an integral (integer) or boolean type, which may be an
32-
* appropriate taint flow barrier for some queries.
33-
*/
34-
class IntegralOrBooleanTypeBarrier extends DataFlow::Node {
35-
IntegralOrBooleanTypeBarrier() {
36-
exists(StructType t, Struct s |
37-
t = TypeInference::inferType(this.asExpr()) and
38-
s = t.getStruct()
39-
|
40-
s instanceof Builtins::IntegralType or
41-
s instanceof Builtins::Bool
42-
)
22+
/** A node whose type is `bool`. */
23+
class BooleanTypeBarrier extends DataFlow::Node {
24+
BooleanTypeBarrier() {
25+
TypeInference::inferType(this.asExpr()).(StructType).getStruct() instanceof Builtins::Bool
26+
}
27+
}
28+
29+
/** A node whose type is an integral (integer). */
30+
class IntegralTypeBarrier extends DataFlow::Node {
31+
IntegralTypeBarrier() {
32+
TypeInference::inferType(this.asExpr()).(StructType).getStruct() instanceof
33+
Builtins::IntegralType
34+
}
35+
}
36+
37+
/** A node whose type is a fieldless enum. */
38+
class FieldlessEnumTypeBarrier extends DataFlow::Node {
39+
FieldlessEnumTypeBarrier() {
40+
TypeInference::inferType(this.asExpr()).(EnumType).getEnum().isFieldless()
4341
}
4442
}
4543

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ private import codeql.rust.dataflow.DataFlow
88
private import codeql.rust.dataflow.FlowSink
99
private import codeql.rust.security.SensitiveData
1010
private import codeql.rust.Concepts
11+
private import codeql.rust.security.Barriers as Barriers
1112

1213
/**
1314
* Provides default sources, sinks and barriers for detecting cleartext logging
@@ -42,4 +43,9 @@ module CleartextLogging {
4243
private class ModelsAsDataSink extends Sink {
4344
ModelsAsDataSink() { sinkNode(this, "log-injection") }
4445
}
46+
47+
private class BooleanTypeBarrier extends Barrier instanceof Barriers::BooleanTypeBarrier { }
48+
49+
private class FieldlessEnumTypeBarrier extends Barrier instanceof Barriers::FieldlessEnumTypeBarrier
50+
{ }
4551
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ module LogInjection {
4949
* numeric or boolean type, which is unlikely to expose any vulnerability.
5050
*/
5151
private class NumericTypeBarrier extends Barrier instanceof Barriers::NumericTypeBarrier { }
52+
53+
private class BooleanTypeBarrier extends Barrier instanceof Barriers::BooleanTypeBarrier { }
54+
55+
private class FieldlessEnumTypeBarrier extends Barrier instanceof Barriers::FieldlessEnumTypeBarrier
56+
{ }
5257
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ module SqlInjection {
6464
* boolean type, which is unlikely to expose any vulnerability.
6565
*/
6666
private class NumericTypeBarrier extends Barrier instanceof Barriers::NumericTypeBarrier { }
67+
68+
private class BooleanTypeBarrier extends Barrier instanceof Barriers::BooleanTypeBarrier { }
69+
70+
private class FieldlessEnumTypeBarrier extends Barrier instanceof Barriers::FieldlessEnumTypeBarrier
71+
{ }
6772
}

rust/ql/lib/codeql/rust/security/regex/RegexInjectionExtensions.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ module RegexInjection {
9494
* We don't include floating point types in this barrier, as `.` is a special character
9595
* in regular expressions.
9696
*/
97-
private class IntegralOrBooleanTypeBarrier extends Barrier instanceof Barriers::IntegralOrBooleanTypeBarrier
98-
{ }
97+
private class IntegralTypeBarrier extends Barrier instanceof Barriers::IntegralTypeBarrier { }
98+
99+
private class BooleanTypeBarrier extends Barrier instanceof Barriers::BooleanTypeBarrier { }
99100
}

0 commit comments

Comments
 (0)