Skip to content

Commit c666fc7

Browse files
authored
Merge pull request #21147 from hvitved/rust/fix-more-models
Rust: Add missing `.Reference` in various models
2 parents c28062a + 17441a5 commit c666fc7

File tree

13 files changed

+206
-139
lines changed

13 files changed

+206
-139
lines changed

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

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,35 @@ private predicate summaryModel(
128128
}
129129

130130
private predicate summaryModelRelevant(
131-
Function f, string input, string output, string kind, Provenance provenance,
131+
Function f, string input, string output, string kind, Provenance provenance, boolean isInherited,
132132
QlBuiltins::ExtensionId madId
133133
) {
134-
exists(boolean isInherited |
135-
summaryModel(f, input, output, kind, provenance, isInherited, madId)
136-
|
137-
// Only apply generated or inherited models to functions in library code and
138-
// when no strictly better model exists
139-
if provenance.isGenerated() or isInherited = true
140-
then
141-
not f.fromSource() and
142-
not exists(Provenance other | summaryModel(f, _, _, _, other, false, _) |
143-
provenance.isGenerated() and other.isManual()
144-
or
145-
provenance = other and isInherited = true
146-
)
147-
else any()
148-
)
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()
142+
or
143+
provenance = other and isInherited = true
144+
)
145+
else any()
149146
}
150147

151148
private class SummarizedCallableFromModel extends SummarizedCallable::Range {
152-
SummarizedCallableFromModel() { summaryModelRelevant(this, _, _, _, _, _) }
149+
SummarizedCallableFromModel() { summaryModelRelevant(this, _, _, _, _, _, _) }
153150

154151
override predicate hasProvenance(Provenance provenance) {
155-
summaryModelRelevant(this, _, _, _, provenance, _)
152+
summaryModelRelevant(this, _, _, _, provenance, _, _)
156153
}
157154

158155
override predicate propagatesFlow(
159156
string input, string output, boolean preservesValue, string model
160157
) {
161158
exists(string kind, QlBuiltins::ExtensionId madId |
162-
summaryModelRelevant(this, input, output, kind, _, madId) and
159+
summaryModelRelevant(this, input, output, kind, _, _, madId) and
163160
model = "MaD:" + madId.toString()
164161
|
165162
kind = "value" and
@@ -202,3 +199,56 @@ private class FlowSinkFromModel extends FlowSink::Range {
202199
)
203200
}
204201
}
202+
203+
private module Debug {
204+
private import FlowSummaryImpl
205+
private import Private
206+
private import Content
207+
private import codeql.rust.dataflow.internal.DataFlowImpl
208+
private import codeql.rust.internal.TypeMention
209+
private import codeql.rust.internal.Type
210+
211+
private predicate relevantManualModel(SummarizedCallableImpl sc, string can) {
212+
exists(Provenance manual |
213+
can = sc.getCanonicalPath() and
214+
summaryModelRelevant(sc, _, _, _, manual, false, _) and
215+
manual.isManual()
216+
)
217+
}
218+
219+
predicate manualModelMissingParameterReference(
220+
SummarizedCallableImpl sc, string can, SummaryComponentStack input, ParamBase p
221+
) {
222+
exists(RustDataFlow::ParameterPosition pos, TypeMention tm |
223+
relevantManualModel(sc, can) and
224+
sc.propagatesFlow(input, _, _, _) and
225+
input.head() = SummaryComponent::argument(pos) and
226+
p = pos.getParameterIn(sc.getParamList()) and
227+
tm.resolveType() instanceof RefType and
228+
not input.tail().head() = SummaryComponent::content(TSingletonContentSet(TReferenceContent()))
229+
|
230+
tm = p.getTypeRepr()
231+
or
232+
tm = getSelfParamTypeMention(p)
233+
)
234+
}
235+
236+
predicate manualModelMissingReturnReference(
237+
SummarizedCallableImpl sc, string can, SummaryComponentStack output
238+
) {
239+
exists(TypeMention tm |
240+
relevantManualModel(sc, can) and
241+
sc.propagatesFlow(_, output, _, _) and
242+
tm.resolveType() instanceof RefType and
243+
output.head() = SummaryComponent::return(_) and
244+
not output.tail().head() =
245+
SummaryComponent::content(TSingletonContentSet(TReferenceContent())) and
246+
tm = getReturnTypeMention(sc) and
247+
not can =
248+
[
249+
"<& as core::ops::deref::Deref>::deref",
250+
"<&mut as core::ops::deref::Deref>::deref"
251+
]
252+
)
253+
}
254+
}

rust/ql/lib/codeql/rust/frameworks/futures.model.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ extensions:
55
data:
66
- ["futures_executor::local_pool::block_on", "Argument[0]", "ReturnValue", "value", "manual"]
77
- ["<futures_util::io::buf_reader::BufReader>::new", "Argument[0]", "ReturnValue", "taint", "manual"]
8-
- ["<_ as futures_util::io::AsyncReadExt>::read", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
98
- ["<_ as futures_util::io::AsyncReadExt>::read", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
109
- ["<_ as futures_util::io::AsyncReadExt>::read_to_end", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]
1110
- ["<_ as futures_util::io::AsyncBufReadExt>::read_line", "Argument[self].Reference", "Argument[0].Reference", "taint", "manual"]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ extensions:
4747
- ["<core::alloc::layout::Layout>::pad_to_align", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
4848
- ["<core::alloc::layout::Layout>::size", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
4949
# String
50-
- ["<alloc::string::String>::as_str", "Argument[self]", "ReturnValue", "value", "manual"]
51-
- ["<alloc::string::String>::as_bytes", "Argument[self]", "ReturnValue", "value", "manual"]
50+
- ["<alloc::string::String>::as_str", "Argument[self].Reference", "ReturnValue.Reference", "taint", "manual"]
51+
- ["<alloc::string::String>::as_bytes", "Argument[self].Reference", "ReturnValue.Reference.Element", "taint", "manual"]
5252
- ["<_ as alloc::string::ToString>::to_string", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
5353
# Overwrite generated model
54-
- ["<alloc::string::String as core::ops::arith::Add>::add", "Argument[self,0]", "ReturnValue", "taint", "manual"]
54+
- ["<alloc::string::String as core::ops::arith::Add>::add", "Argument[self]", "ReturnValue", "taint", "manual"]
5555
- ["<alloc::string::String as core::ops::arith::Add>::add", "Argument[0].Reference", "ReturnValue", "taint", "manual"]
5656
# Vec
5757
- ["alloc::vec::from_elem", "Argument[0]", "ReturnValue.Element", "value", "manual"]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extensions:
114114
- ["<core::pin::Pin as core::ops::deref::Deref>::deref", "Argument[self].Reference.Field[core::pin::Pin::pointer].Reference", "ReturnValue.Reference", "value", "manual"]
115115
- ["<core::pin::Pin as core::ops::deref::Deref>::deref", "Argument[self].Reference.Field[core::pin::Pin::pointer].Field[alloc::boxed::Box(0)]", "ReturnValue.Reference", "value", "manual"]
116116
# Str
117-
- ["<core::str>::as_str", "Argument[self]", "ReturnValue", "value", "manual"]
117+
- ["<core::str>::as_str", "Argument[self].Reference", "ReturnValue.Reference", "taint", "manual"]
118118
- ["<core::str>::as_bytes", "Argument[self].Reference", "ReturnValue.Reference", "taint", "manual"]
119119
- ["<core::str>::parse", "Argument[self].Reference", "ReturnValue.Field[core::result::Result::Ok(0)]", "taint", "manual"]
120120
- ["<core::str>::trim", "Argument[self].Reference", "ReturnValue.Reference", "taint", "manual"]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extensions:
6161
- ["<std::path::PathBuf>::as_path", "Argument[self].Reference", "ReturnValue.Reference", "value", "manual"]
6262
- ["<std::path::PathBuf>::into_boxed_path", "Argument[self]", "ReturnValue.Field[alloc::boxed::Box(0)]", "taint", "manual"]
6363
- ["<std::path::Path>::new", "Argument[0].Reference", "ReturnValue.Reference", "value", "manual"]
64-
- ["<std::path::Path>::join", "Argument[self]", "ReturnValue", "taint", "manual"]
64+
- ["<std::path::Path>::join", "Argument[self].Reference", "ReturnValue", "taint", "manual"]
6565
- ["<std::path::Path>::join", "Argument[0]", "ReturnValue", "taint", "manual"]
6666
- ["<std::path::Path>::as_os_str", "Argument[self].Reference.Field[std::path::Path::inner]", "ReturnValue.Reference", "value", "manual"]
6767
- ["<std::path::Path>::as_mut_os_str", "Argument[self].Reference.Field[std::path::Path::inner]", "ReturnValue.Reference", "value", "manual"]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ extensions:
99
pack: codeql/rust-all
1010
extensible: summaryModel
1111
data:
12-
- ["<std::net::tcp::TcpStream>::try_clone", "Argument[self]", "ReturnValue.Field[core::result::Result::Ok(0)]", "taint", "manual"]
12+
- ["<std::net::tcp::TcpStream>::try_clone", "Argument[self].Reference", "ReturnValue.Field[core::result::Result::Ok(0)]", "taint", "manual"]

0 commit comments

Comments
 (0)