Skip to content

Commit 89d736d

Browse files
committed
Rust: Add extensible predicate to exclude fields and block fieldless enum types
1 parent dd8f094 commit 89d736d

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

rust/ql/lib/codeql/rust/dataflow/internal/TaintTrackingImpl.qll

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ private import codeql.rust.internal.TypeInference as TypeInference
1111
private import codeql.rust.internal.Type as Type
1212
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
1313

14+
/**
15+
* Holds if the field `field` should, by default, be excluded from taint steps.
16+
* The syntax used to denote field is the same as `Field` for models as data.
17+
*/
18+
extensible predicate excludeFieldTaintStep(string field);
19+
20+
private predicate excludedTaintStepContent(Content c) {
21+
exists(string arg | excludeFieldTaintStep(arg) |
22+
FlowSummaryImpl::encodeContentStructField(c, arg) or
23+
FlowSummaryImpl::encodeContentTupleField(c, arg)
24+
)
25+
}
26+
1427
module RustTaintTracking implements InputSig<Location, RustDataFlow> {
1528
predicate defaultTaintSanitizer(DataFlow::Node node) { none() }
1629

@@ -48,13 +61,17 @@ module RustTaintTracking implements InputSig<Location, RustDataFlow> {
4861
// taint is propagated. We limit this to not apply if the type of the
4962
// operation is a small primitive type as these are often uninteresting
5063
// (for instance in the case of an injection query).
51-
RustDataFlow::readContentStep(pred, _, succ) and
52-
not exists(Struct s |
53-
s = TypeInference::inferType(succ.asExpr()).(Type::StructType).getStruct()
54-
|
55-
s instanceof Builtins::NumericType or
56-
s instanceof Builtins::Bool or
57-
s instanceof Builtins::Char
64+
exists(Content c |
65+
RustDataFlow::readContentStep(pred, c, succ) and
66+
forex(Type::Type t | t = TypeInference::inferType(succ.asExpr()) |
67+
not exists(Struct s | s = t.(Type::StructType).getStruct() |
68+
s instanceof Builtins::NumericType or
69+
s instanceof Builtins::Bool or
70+
s instanceof Builtins::Char
71+
)
72+
) and
73+
not excludedTaintStepContent(c) and
74+
not TypeInference::inferType(succ.asExpr()).(Type::EnumType).getEnum().isFieldless()
5875
)
5976
or
6077
// Let all read steps (including those from flow summaries and those that

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

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

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

143+
Enum getEnum() { result = enum }
144+
143145
override TypeParameter getPositionalTypeParameter(int i) {
144146
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
145147
}

0 commit comments

Comments
 (0)