Skip to content

Commit 7413269

Browse files
committed
Rust: Adapt to changes in FlowSummaryImpl
1 parent 82b200d commit 7413269

File tree

18 files changed

+456
-476
lines changed

18 files changed

+456
-476
lines changed

rust/ql/lib/codeql/rust/dataflow/FlowSummary.qll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ module SummarizedCallable {
1717
Range() { any() }
1818

1919
override predicate propagatesFlow(
20-
string input, string output, boolean preservesValue, string model
20+
string input, string output, boolean preservesValue, Provenance p, boolean isExact,
21+
string model
2122
) {
22-
this.propagatesFlow(input, output, preservesValue) and model = ""
23+
this.propagatesFlow(input, output, preservesValue) and
24+
p = "manual" and
25+
isExact = true and
26+
model = "QL"
2327
}
2428

2529
/**
@@ -31,6 +35,6 @@ module SummarizedCallable {
3135
}
3236
}
3337

34-
final class SummarizedCallable = SummarizedCallable::Range;
38+
class SummarizedCallable = Impl::Public::RelevantSummarizedCallable;
3539

3640
final class Provenance = Impl::Public::Provenance;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module Input implements InputSig<Location, RustDataFlow> {
3030

3131
class SummarizedCallableBase = Function;
3232

33+
predicate callableFromSource(SummarizedCallableBase c) { c.fromSource() }
34+
3335
abstract private class SourceSinkBase extends AstNode {
3436
/** Gets the associated call. */
3537
abstract Call getCall();

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

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -111,60 +111,38 @@ predicate interpretModelForTest(QlBuiltins::ExtensionId madId, string model) {
111111
)
112112
}
113113

114-
private predicate summaryModel(
115-
Function f, string input, string output, string kind, Provenance provenance, boolean isInherited,
116-
QlBuiltins::ExtensionId madId
117-
) {
118-
exists(string path, Function f0 |
119-
summaryModel(path, input, output, kind, provenance, madId) and
120-
f0.getCanonicalPath() = path
121-
|
122-
f = f0 and
123-
isInherited = false
124-
or
125-
f.implements(f0) and
126-
isInherited = true
127-
)
128-
}
129-
130-
private predicate summaryModelRelevant(
131-
Function f, string input, string output, string kind, Provenance provenance, boolean isInherited,
132-
QlBuiltins::ExtensionId madId
133-
) {
134-
summaryModel(f, input, output, kind, provenance, isInherited, madId) and
135-
// Only apply generated or inherited models to functions in library code and
136-
// when no strictly better model exists
137-
if provenance.isGenerated() or isInherited = true
138-
then
139-
not f.fromSource() and
140-
not exists(Provenance other | summaryModel(f, _, _, _, other, false, _) |
141-
provenance.isGenerated() and other.isManual()
114+
private class SummarizedCallableFromModel extends SummarizedCallable::Range {
115+
string input_;
116+
string output_;
117+
string kind;
118+
Provenance p_;
119+
boolean isExact_;
120+
QlBuiltins::ExtensionId madId;
121+
122+
SummarizedCallableFromModel() {
123+
exists(string path, Function f, Provenance p |
124+
summaryModel(path, input_, output_, kind, p, madId) and
125+
f.getCanonicalPath() = path
126+
|
127+
this = f and isExact_ = true and p_ = p
142128
or
143-
provenance = other and isInherited = true
129+
this.implements(f) and
130+
isExact_ = false and
131+
// making inherited models generated means that source code definitions and
132+
// exact generated models take precedence
133+
p_ = "hq-generated"
144134
)
145-
else any()
146-
}
147-
148-
private class SummarizedCallableFromModel extends SummarizedCallable::Range {
149-
SummarizedCallableFromModel() { summaryModelRelevant(this, _, _, _, _, _, _) }
150-
151-
override predicate hasProvenance(Provenance provenance) {
152-
summaryModelRelevant(this, _, _, _, provenance, _, _)
153135
}
154136

155137
override predicate propagatesFlow(
156-
string input, string output, boolean preservesValue, string model
138+
string input, string output, boolean preservesValue, Provenance p, boolean isExact, string model
157139
) {
158-
exists(string kind, QlBuiltins::ExtensionId madId |
159-
summaryModelRelevant(this, input, output, kind, _, _, madId) and
160-
model = "MaD:" + madId.toString()
161-
|
162-
kind = "value" and
163-
preservesValue = true
164-
or
165-
kind = "taint" and
166-
preservesValue = false
167-
)
140+
input = input_ and
141+
output = output_ and
142+
(if kind = "value" then preservesValue = true else preservesValue = false) and
143+
p = p_ and
144+
isExact = isExact_ and
145+
model = "MaD:" + madId.toString()
168146
}
169147
}
170148

@@ -211,7 +189,7 @@ private module Debug {
211189
private predicate relevantManualModel(SummarizedCallableImpl sc, string can) {
212190
exists(Provenance manual |
213191
can = sc.getCanonicalPath() and
214-
summaryModelRelevant(sc, _, _, _, manual, false, _) and
192+
sc.(SummarizedCallableFromModel).propagatesFlow(_, _, _, manual, true, _) and
215193
manual.isManual()
216194
)
217195
}
@@ -221,7 +199,7 @@ private module Debug {
221199
) {
222200
exists(RustDataFlow::ParameterPosition pos, TypeMention tm |
223201
relevantManualModel(sc, can) and
224-
sc.propagatesFlow(input, _, _, _) and
202+
sc.propagatesFlow(input, _, _, _, _, _) and
225203
input.head() = SummaryComponent::argument(pos) and
226204
p = pos.getParameterIn(sc.getParamList()) and
227205
tm.resolveType() instanceof RefType and
@@ -238,7 +216,7 @@ private module Debug {
238216
) {
239217
exists(TypeMention tm |
240218
relevantManualModel(sc, can) and
241-
sc.propagatesFlow(_, output, _, _) and
219+
sc.propagatesFlow(_, output, _, _, _, _) and
242220
tm.resolveType() instanceof RefType and
243221
output.head() = SummaryComponent::return(_) and
244222
not output.tail().head() =

rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ private class ReflexiveFrom extends SummarizedCallable::Range {
4141
}
4242

4343
override predicate propagatesFlow(
44-
string input, string output, boolean preservesValue, string model
44+
string input, string output, boolean preservesValue, Provenance p, boolean isExact, string model
4545
) {
4646
input = "Argument[0]" and
4747
output = "ReturnValue" and
4848
preservesValue = true and
49+
p = "manual" and
50+
isExact = true and
4951
model = "ReflexiveFrom"
5052
}
5153
}

rust/ql/lib/codeql/rust/frameworks/stdlib/ffi.model.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ extensions:
55
data:
66
- ["<std::ffi::os_str::OsStr>::to_str", "Argument[self].Reference.Field[std::ffi::os_str::OsStr::inner]", "ReturnValue.Field[core::option::Option::Some(0)].Reference", "taint", "manual"]
77
- ["<std::ffi::os_str::OsStr>::to_string_lossy", "Argument[self].Reference.Field[std::ffi::os_str::OsStr::inner]", "ReturnValue.Field[alloc::borrow::Cow::Owned(0)]", "taint", "manual"]
8-
- ["<std::ffi::os_str::OsStr>::as_encoded_bytes", "Argument[self].Reference.Field[std::ffi::os_str::OsStr::inner]", "ReturnValue.Reference", "taint", "manual"]
8+
- ["<std::ffi::os_str::OsStr>::as_encoded_bytes", "Argument[self].Reference.Field[std::ffi::os_str::OsStr::inner]", "ReturnValue.Reference", "taint", "manual"]
9+
# Overwrite generated model
10+
- ["<std::ffi::os_str::OsString as core::ops::deref::Deref>::deref", "Argument[self].Reference", "ReturnValue.Reference", "taint", "manual"]

rust/ql/lib/codeql/rust/frameworks/stdlib/fs.model.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ extensions:
8686
- ["<std::fs::Metadata>::len", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
8787
- ["<std::fs::Metadata>::modified", "Argument[self].Reference", "ReturnValue.Field[core::result::Result::Ok(0)]", "taint", "manual"]
8888
- ["<std::fs::Metadata>::permissions", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
89+
# Overwrite generated models
90+
- ["<std::fs::File as std::io::Read>::read_to_end", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
91+
- ["<std::fs::File as std::io::Read>::read_to_string", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
92+

rust/ql/lib/codeql/rust/frameworks/stdlib/io.model.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ extensions:
2626
- ["<std::io::stdio::Stdin>::lock", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
2727
- ["<std::io::stdio::Stdin>::read_line", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
2828
- ["<std::io::Split as core::iter::traits::iterator::Iterator>::next", "Argument[self].Reference.Element", "ReturnValue.Field[core::option::Option::Some(0)].Field[core::result::Result::Ok(0)]", "value", "manual"]
29+
# Overwrite generated models
30+
- ["<std::io::stdio::Stdin as std::io::Read>::read", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
31+
- ["<std::io::stdio::Stdin as std::io::Read>::read_to_string", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
32+
- ["<std::io::stdio::Stdin as std::io::Read>::read_to_end", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
33+
- ["<std::io::stdio::Stdin as std::io::Read>::read_exact", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
34+
- ["<std::io::stdio::StdinLock as std::io::Read>::read", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
35+
- ["<std::io::stdio::StdinLock as std::io::Read>::read_to_string", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
36+
- ["<std::io::stdio::StdinLock as std::io::Read>::read_to_end", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
37+
- ["<std::io::stdio::StdinLock as std::io::Read>::read_exact", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
38+
- ["<std::io::stdio::StdinLock as std::io::BufRead>::fill_buf", "Argument[self].Reference", "ReturnValue.Field[core::result::Result::Ok(0)]", "taint", "manual"]
39+
- ["<std::io::stdio::StdinRaw as std::io::Read>::read", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
40+
- ["<std::io::stdio::StdinRaw as std::io::Read>::read_to_string", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
41+
- ["<std::io::stdio::StdinRaw as std::io::Read>::read_to_end", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
42+
- ["<std::io::stdio::StdinRaw as std::io::Read>::read_exact", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
43+
- ["<std::io::buffered::bufreader::BufReader as std::io::BufRead>::fill_buf", "Argument[self].Reference", "ReturnValue.Field[core::result::Result::Ok(0)]", "taint", "manual"]

0 commit comments

Comments
 (0)