Skip to content

Commit a7bb952

Browse files
committed
Rust: Implement known{Source,Sink}Model
1 parent 868caf9 commit a7bb952

File tree

5 files changed

+115
-53
lines changed

5 files changed

+115
-53
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ module Node {
202202
}
203203

204204
/** Holds is this node is a source node of kind `kind`. */
205-
predicate isSource(string kind) {
206-
this.getSummaryNode().(FlowSummaryImpl::Private::SourceOutputNode).isEntry(kind)
205+
predicate isSource(string kind, string model) {
206+
this.getSummaryNode().(FlowSummaryImpl::Private::SourceOutputNode).isEntry(kind, model)
207207
}
208208

209209
/** Holds is this node is a sink node of kind `kind`. */
210-
predicate isSink(string kind) {
211-
this.getSummaryNode().(FlowSummaryImpl::Private::SinkInputNode).isExit(kind)
210+
predicate isSink(string kind, string model) {
211+
this.getSummaryNode().(FlowSummaryImpl::Private::SinkInputNode).isExit(kind, model)
212212
}
213213

214214
override CfgScope getCfgScope() {
@@ -1305,9 +1305,13 @@ module RustDataFlow implements InputSig<Location> {
13051305
/** Extra data flow steps needed for lambda flow analysis. */
13061306
predicate additionalLambdaFlowStep(Node nodeFrom, Node nodeTo, boolean preservesValue) { none() }
13071307

1308-
predicate knownSourceModel(Node source, string model) { none() }
1308+
predicate knownSourceModel(Node source, string model) {
1309+
source.(Node::FlowSummaryNode).isSource(_, model)
1310+
}
13091311

1310-
predicate knownSinkModel(Node sink, string model) { none() }
1312+
predicate knownSinkModel(Node sink, string model) {
1313+
sink.(Node::FlowSummaryNode).isSink(_, model)
1314+
}
13111315

13121316
class DataFlowSecondLevelScope = Void;
13131317
}
@@ -1575,11 +1579,11 @@ private module Cached {
15751579

15761580
/** Holds if `n` is a flow source of kind `kind`. */
15771581
cached
1578-
predicate sourceNode(Node n, string kind) { n.(Node::FlowSummaryNode).isSource(kind) }
1582+
predicate sourceNode(Node n, string kind) { n.(Node::FlowSummaryNode).isSource(kind, _) }
15791583

15801584
/** Holds if `n` is a flow sink of kind `kind`. */
15811585
cached
1582-
predicate sinkNode(Node n, string kind) { n.(Node::FlowSummaryNode).isSink(kind) }
1586+
predicate sinkNode(Node n, string kind) { n.(Node::FlowSummaryNode).isSink(kind, _) }
15831587
}
15841588

15851589
import Cached

rust/ql/test/library-tests/dataflow/models/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ fn test_enum_method_sink() {
222222
e.sink(); // $ hasValueFlow=15
223223
}
224224

225+
// has a source model
226+
fn simple_source(i: i64) -> i64 {
227+
0
228+
}
229+
230+
fn test_simple_source() {
231+
let s = simple_source(16);
232+
sink(s) // $ hasValueFlow=16
233+
}
234+
235+
// has a sink model
236+
fn simple_sink(i: i64) {}
237+
238+
fn test_simple_sink() {
239+
let s = source(17);
240+
simple_sink(s); // $ hasValueFlow=17
241+
}
242+
225243
fn main() {
226244
test_identify();
227245
test_get_var_pos();
@@ -238,5 +256,7 @@ fn main() {
238256
test_enum_method_source();
239257
test_enum_sink();
240258
test_enum_method_sink();
259+
test_simple_source();
260+
test_simple_sink();
241261
let dummy = Some(0); // ensure that the the `lang:core` crate is extracted
242262
}

0 commit comments

Comments
 (0)