-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Models-as-data for flow summaries #18231
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
rust/ql/lib/codeql/rust/dataflow/internal/ModelsAsData.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * Defines extensible predicates for contributing library models from data extensions. | ||
| * | ||
| * The extensible relations have the following columns: | ||
| * | ||
| * - Sources: | ||
| * `crate; path; output; kind; provenance` | ||
| * - Sinks: | ||
| * `crate; path; input; kind; provenance` | ||
| * - Summaries: | ||
| * `crate; path; input; output; kind; provenance` | ||
| * | ||
| * The interpretation of a row is similar to API-graphs with a left-to-right | ||
| * reading. | ||
| * | ||
| * 1. The `crate` column selects a crate. | ||
| * 2. The `path` column selects a function with the given canonical path within | ||
| * the crate. | ||
| * 3. The `input` column specifies how data enters the element selected by the | ||
| * first 2 columns, and the `output` column specifies how data leaves the | ||
| * element selected by the first 2 columns. Both `input` and `output` are | ||
| * `.`-separated lists of "access path tokens" to resolve, starting at the | ||
| * selected function. | ||
| * | ||
| * The following tokens are supported: | ||
| * - `Argument[n]`: the `n`-th argument to a call. May be a range of form `x..y` (inclusive) | ||
| * and/or a comma-separated list. | ||
| * - `Parameter[n]`: the `n`-th parameter of a callback. May be a range of form `x..y` (inclusive) | ||
| * and/or a comma-separated list. | ||
| * - `ReturnValue`: the value returned by a function call. | ||
| * - `ArrayElement`: an element of an array. | ||
| * - `Variant[v::f]`: field `f` of the variant with canonical path `v`, for example | ||
| * `Variant[crate::ihex::Record::Data::value]`. | ||
| * - `Variant[v(i)]`: position `i` inside the variant with canonical path `v`, for example | ||
| * `Variant[crate::option::Option::Some(0)]`. | ||
| * - `Struct[s::f]`: field `f` of the struct with canonical path `v`, for example | ||
| * `Struct[crate::process::Child::stdin]`. | ||
| * - `Tuple[i]`: the `i`th element of a tuple. | ||
| * 4. The `kind` column is a tag that can be referenced from QL to determine to | ||
| * which classes the interpreted elements should be added. For example, for | ||
| * sources `"remote"` indicates a default remote flow source, and for summaries | ||
| * `"taint"` indicates a default additional taint step and `"value"` indicates a | ||
| * globally applicable value-preserving step. | ||
| * 5. The `provenance` column is mainly used internally, and should be set to `"manual"` for | ||
| * all custom models. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great, thanks for writing it up! |
||
| */ | ||
|
|
||
| private import rust | ||
| private import codeql.rust.dataflow.FlowSummary | ||
|
|
||
| /** | ||
| * Holds if in a call to the function with canonical path `path`, defined in the | ||
| * crate `crate`, the value referred to by `output` is a flow source of the given | ||
| * `kind`. | ||
| * | ||
| * `output = "ReturnValue"` simply means the result of the call itself. | ||
| * | ||
| * For more information on the `kind` parameter, see | ||
| * https://github.com/github/codeql/blob/main/docs/codeql/reusables/threat-model-description.rst. | ||
| */ | ||
| extensible predicate sourceModel( | ||
|
||
| string crate, string path, string output, string kind, string provenance, | ||
| QlBuiltins::ExtensionId madId | ||
| ); | ||
|
|
||
| /** | ||
| * Holds if in a call to the function with canonical path `path`, defined in the | ||
| * crate `crate`, the value referred to by `input` is a flow sink of the given | ||
| * `kind`. | ||
| * | ||
| * For example, `input = Argument[0]` means the first argument of the call. | ||
| * | ||
| * The following kinds are supported: | ||
| * | ||
| * - `sql-injection`: a flow sink for SQL injection. | ||
| */ | ||
| extensible predicate sinkModel( | ||
| string crate, string path, string input, string kind, string provenance, | ||
| QlBuiltins::ExtensionId madId | ||
| ); | ||
|
|
||
| /** | ||
| * Holds if in a call to the function with canonical path `path`, defined in the | ||
| * crate `crate`, the value referred to by `input` can flow to the value referred | ||
| * to by `output`. | ||
| * | ||
| * `kind` should be either `value` or `taint`, for value-preserving or taint-preserving | ||
| * steps, respectively. | ||
| */ | ||
| extensible predicate summaryModel( | ||
| string crate, string path, string input, string output, string kind, string provenance, | ||
| QlBuiltins::ExtensionId madId | ||
| ); | ||
|
|
||
| /** | ||
| * Holds if the given extension tuple `madId` should pretty-print as `model`. | ||
| * | ||
| * This predicate should only be used in tests. | ||
| */ | ||
| predicate interpretModelForTest(QlBuiltins::ExtensionId madId, string model) { | ||
| exists(string crate, string path, string output, string kind | | ||
| sourceModel(crate, path, kind, output, _, madId) and | ||
| model = "Source: " + crate + "; " + path + "; " + output + "; " + kind | ||
| ) | ||
| or | ||
| exists(string crate, string path, string input, string kind | | ||
| sinkModel(crate, path, kind, input, _, madId) and | ||
| model = "Sink: " + crate + "; " + path + "; " + input + "; " + kind | ||
| ) | ||
| or | ||
| exists(string type, string path, string input, string output, string kind | | ||
| summaryModel(type, path, input, output, kind, _, madId) and | ||
| model = "Summary: " + type + "; " + path + "; " + input + "; " + output + "; " + kind | ||
| ) | ||
| } | ||
|
|
||
| private class SummarizedCallableFromModel extends SummarizedCallable::Range { | ||
| private string crate; | ||
| private string path; | ||
|
|
||
| SummarizedCallableFromModel() { | ||
| summaryModel(crate, path, _, _, _, _, _) and | ||
| this = crate + "::_::" + path | ||
| } | ||
|
|
||
| override predicate propagatesFlow( | ||
| string input, string output, boolean preservesValue, string model | ||
| ) { | ||
| exists(string kind, QlBuiltins::ExtensionId madId | | ||
| summaryModel(crate, path, input, output, kind, _, madId) and | ||
| model = "MaD:" + madId.toString() | ||
| | | ||
| kind = "value" and | ||
| preservesValue = true | ||
| or | ||
| kind = "taint" and | ||
| preservesValue = false | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| extensions: | ||
| # Make sure that the extensible model predicates have at least one definition | ||
| # to avoid errors about undefined extensionals. | ||
| - addsTo: | ||
| pack: codeql/rust-all | ||
| extensible: sourceModel | ||
| data: [] | ||
|
|
||
| - addsTo: | ||
| pack: codeql/rust-all | ||
| extensible: sinkModel | ||
| data: [] | ||
|
|
||
| - addsTo: | ||
| pack: codeql/rust-all | ||
| extensible: summaryModel | ||
| data: [] |
6 changes: 6 additions & 0 deletions
6
rust/ql/lib/codeql/rust/frameworks/stdlib/lang-core.model.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| extensions: | ||
| - addsTo: | ||
| pack: codeql/rust-all | ||
| extensible: summaryModel | ||
| data: | ||
| - ["lang:core", "<crate::option::Option>::unwrap", "Argument[self].Variant[crate::option::Option::Some(0)]", "ReturnValue", "value", "manual"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.