Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Basic support for future content and .await #18685

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,15 @@ final class ElementContent extends Content, TElementContent {
override Location getLocation() { result instanceof EmptyLocation }
}

/**
* A value that a future resolves to.
*/
final class FutureContent extends Content, TFutureContent {
override string toString() { result = "future" }

override Location getLocation() { result instanceof EmptyLocation }
}

/**
* Content stored at a position in a tuple.
*
Expand Down Expand Up @@ -1229,6 +1238,12 @@ module RustDataFlow implements InputSig<Location> {
c instanceof FunctionCallReturnContent
)
or
exists(AwaitExprCfgNode await |
c instanceof FutureContent and
node1.asExpr() = await.getExpr() and
node2.asExpr() = await
)
or
VariableCapture::readStep(node1, c, node2)
)
or
Expand Down Expand Up @@ -1593,7 +1608,8 @@ private module Cached {
[
any(IndexExprCfgNode i).getBase(), any(FieldExprCfgNode access).getExpr(),
any(TryExprCfgNode try).getExpr(),
any(PrefixExprCfgNode pe | pe.getOperatorName() = "*").getExpr()
any(PrefixExprCfgNode pe | pe.getOperatorName() = "*").getExpr(),
any(AwaitExprCfgNode a).getExpr()
]
} or
TSsaNode(SsaImpl::DataFlowIntegration::SsaNode node) or
Expand Down Expand Up @@ -1649,6 +1665,7 @@ private module Cached {
TVariantInLibTupleFieldContent(VariantInLib::VariantInLib v, int pos) { pos = v.getAPosition() } or
TVariantRecordFieldContent(Variant v, string field) { exists(getVariantRecordField(v, field)) } or
TElementContent() or
TFutureContent() or
TTuplePositionContent(int pos) {
pos in [0 .. max([
any(TuplePat pat).getNumberOfFields(),
Expand Down
4 changes: 4 additions & 0 deletions rust/ql/lib/codeql/rust/dataflow/internal/FlowSummaryImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ module Input implements InputSig<Location, RustDataFlow> {
c = TTuplePositionContent(pos) and
arg = pos.toString()
)
or
result = "Future" and
c = TFutureContent() and
arg = ""
)
}

Expand Down
15 changes: 14 additions & 1 deletion rust/ql/test/library-tests/dataflow/models/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ fn test_apply_flow_through() {
sink(t); // $ hasValueFlow=33
}

// has a flow model with value flow from argument to returned future
async fn get_async_number(a: i64) -> i64 {
37
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a comment that this function has a summary model with taint through it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Done :)


async fn test_get_async_number() {
let s = source(46);
let t = get_async_number(s).await;
sink(t); // $ hasValueFlow=46
}

impl MyFieldEnum {
// has a source model
fn source(&self, i: i64) -> MyFieldEnum {
Expand Down Expand Up @@ -268,7 +279,8 @@ fn test_simple_sink() {
simple_sink(s); // $ hasValueFlow=17
}

fn main() {
#[tokio::main]
async fn main() {
test_identify();
test_get_var_pos();
test_set_var_pos();
Expand All @@ -286,5 +298,6 @@ fn main() {
test_enum_method_sink();
test_simple_source();
test_simple_sink();
test_get_async_number().await;
let dummy = Some(0); // ensure that the the `lang:core` crate is extracted
}
Loading