diff --git a/docs/codeql/codeql-language-guides/analyzing-data-flow-in-rust.rst b/docs/codeql/codeql-language-guides/analyzing-data-flow-in-rust.rst new file mode 100644 index 000000000000..8aed9fc93266 --- /dev/null +++ b/docs/codeql/codeql-language-guides/analyzing-data-flow-in-rust.rst @@ -0,0 +1,242 @@ +.. _analyzing-data-flow-in-rust: + +Analyzing data flow in Rust +============================= + +You can use CodeQL to track the flow of data through a Rust program to places where the data is used. + +About this article +------------------ + +This article describes how data flow analysis is implemented in the CodeQL libraries for Rust and includes examples to help you write your own data flow queries. +The following sections describe how to use the libraries for local data flow, global data flow, and taint tracking. +For a more general introduction to modeling data flow, see ":ref:`About data flow analysis `." + +.. include:: ../reusables/new-data-flow-api.rst + +Local data flow +--------------- + +Local data flow tracks the flow of data within a single method or callable. Local data flow is easier, faster, and more precise than global data flow. Before using more complex tracking, consider local tracking, as it is sufficient for many queries. + +Using local data flow +~~~~~~~~~~~~~~~~~~~~~ + +You can use the local data flow library by importing the ``codeql.rust.dataflow.DataFlow`` module. The library uses the class ``Node`` to represent any element through which data can flow. +Common ``Node`` types include expression nodes (``ExprNode``) and parameter nodes (``ParameterNode``). +You can use the ``asExpr`` member predicate to map a data flow ``ExprNode`` to its corresponding ``ExprCfgNode`` in the control-flow library. +Similarly, you can map a data flow ``ParameterNode`` to its corresponding ``Parameter`` AST node using the ``asParameter`` member predicate. + +.. code-block:: ql + + class Node { + /** + * Gets the expression corresponding to this node, if any. + */ + CfgNodes::ExprCfgNode asExpr() { ... } + + /** + * Gets the parameter corresponding to this node, if any. + */ + Parameter asParameter() { ... } + + ... + } + +Note that because ``asExpr`` maps from data-flow to control-flow nodes, you need to call the ``getExpr`` member predicate on the control-flow node to map to the corresponding AST node. For example, you can write ``node.asExpr().getExpr()``. +A control-flow graph considers every way control can flow through code, consequently, there can be multiple data-flow and control-flow nodes associated with a single expression node in the AST. + +The predicate ``localFlowStep(Node nodeFrom, Node nodeTo)`` holds if there is an immediate data flow edge from the node ``nodeFrom`` to the node ``nodeTo``. +You can apply the predicate recursively by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localFlow``. + +For example, you can find flow from an expression ``source`` to an expression ``sink`` in zero or more local steps: + +.. code-block:: ql + + DataFlow::localFlow(source, sink) + +Using local taint tracking +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Local taint tracking extends local data flow to include flow steps where values are not preserved, for example, string manipulation. +For example: + +.. code-block:: rust + + let y: String = "Hello ".to_owned() + x + +If ``x`` is a tainted string then ``y`` is also tainted. + +The local taint tracking library is in the module ``TaintTracking``. +Like local data flow, a predicate ``localTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo)`` holds if there is an immediate taint propagation edge from the node ``nodeFrom`` to the node ``nodeTo``. +You can apply the predicate recursively by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localTaint``. + +For example, you can find taint propagation from an expression ``source`` to an expression ``sink`` in zero or more local steps: + +.. code-block:: ql + + TaintTracking::localTaint(source, sink) + + +Using local sources +~~~~~~~~~~~~~~~~~~~ + +When exploring local data flow or taint propagation between two expressions, such as in the previous example, you typically constrain the expressions to those relevant to your investigation. +The next section provides concrete examples, but first introduces the concept of a local source. + +A local source is a data-flow node with no local data flow into it. +It is a local origin of data flow, a place where a new value is created. +This includes parameters (which only receive values from global data flow) and most expressions (because they are not value-preserving). +The class ``LocalSourceNode`` represents data-flow nodes that are also local sources. +It includes a useful member predicate ``flowsTo(DataFlow::Node node)``, which holds if there is local data flow from the local source to ``node``. + +Examples of local data flow +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This query finds the argument passed in each call to ``File::create``: + +.. code-block:: ql + + import rust + + from CallExpr call + where call.getStaticTarget().(Function).getCanonicalPath() = "::create" + select call.getArg(0) + +Unfortunately, this only returns the expression used as the argument, not the possible values that could be passed to it. To address this, you can use local data flow to find all expressions that flow into the argument. + +.. code-block:: ql + + import rust + import codeql.rust.dataflow.DataFlow + + from CallExpr call, DataFlow::ExprNode source, DataFlow::ExprNode sink + where + call.getStaticTarget().(Function).getCanonicalPath() = "::create" and + sink.asExpr().getExpr() = call.getArg(0) and + DataFlow::localFlow(source, sink) + select source, sink + +You can vary the source by making the source the parameter of a function instead of an expression. The following query finds where a parameter is used in file creation: + +.. code-block:: ql + + import rust + import codeql.rust.dataflow.DataFlow + + from CallExpr call, DataFlow::ParameterNode source, DataFlow::ExprNode sink + where + call.getStaticTarget().(Function).getCanonicalPath() = "::create" and + sink.asExpr().getExpr() = call.getArg(0) and + DataFlow::localFlow(source, sink) + select source, sink + +Global data flow +---------------- + +Global data flow tracks data flow throughout the entire program, and is therefore more powerful than local data flow. +However, global data flow is less precise than local data flow, and the analysis typically requires significantly more time and memory to perform. + +.. pull-quote:: Note + + .. include:: ../reusables/path-problem.rst + +Using global data flow +~~~~~~~~~~~~~~~~~~~~~~ + +We can use the global data flow library by implementing the signature ``DataFlow::ConfigSig`` and applying the module ``DataFlow::Global``: + +.. code-block:: ql + + import codeql.rust.dataflow.DataFlow + + module MyDataFlowConfiguration implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node source) { + ... + } + + predicate isSink(DataFlow::Node sink) { + ... + } + } + + module MyDataFlow = DataFlow::Global; + +These predicates are defined in the configuration: + +- ``isSource`` - defines where data may flow from. +- ``isSink`` - defines where data may flow to. +- ``isBarrier`` - optional, defines where data flow is blocked. +- ``isAdditionalFlowStep`` - optional, adds additional flow steps. + +The last line (``module MyDataFlow = ...``) instantiates the parameterized module for data flow analysis by passing the configuration to the parameterized module. Data flow analysis can then be performed using ``MyDataFlow::flow(DataFlow::Node source, DataFlow::Node sink)``: + +.. code-block:: ql + + from DataFlow::Node source, DataFlow::Node sink + where MyDataFlow::flow(source, sink) + select source, "Dataflow to $@.", sink, sink.toString() + +Using global taint tracking +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Global taint tracking relates to global data flow in the same way that local taint tracking relates to local data flow. +In other words, global taint tracking extends global data flow with additional non-value-preserving steps. +The global taint tracking library uses the same configuration module as the global data flow library. You can perform taint flow analysis using ``TaintTracking::Global``: + +.. code-block:: ql + + module MyTaintFlow = TaintTracking::Global; + + from DataFlow::Node source, DataFlow::Node sink + where MyTaintFlow::flow(source, sink) + select source, "Taint flow to $@.", sink, sink.toString() + +Predefined sources +~~~~~~~~~~~~~~~~~~ + +The library module ``codeql.rust.Concepts`` contains a number of predefined sources and sinks that you can use to write security queries to track data flow and taint flow. + +Examples of global data flow +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The following global taint-tracking query finds places where a string literal is used in a function call argument named "password". + - Since this is a taint-tracking query, the ``TaintTracking::Global`` module is used. + - The ``isSource`` predicate defines sources as any ``StringLiteralExpr``. + - The ``isSink`` predicate defines sinks as arguments to a ``CallExpr`` called "password". + - The sources and sinks may need to be adjusted for a particular use. For example, passwords might be represented by a type other than ``String`` or passed in arguments with a different name than "password". + +.. code-block:: ql + + import rust + import codeql.rust.dataflow.DataFlow + import codeql.rust.dataflow.TaintTracking + + module ConstantPasswordConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { node.asExpr().getExpr() instanceof StringLiteralExpr } + + predicate isSink(DataFlow::Node node) { + // any argument going to a parameter called `password` + exists(Function f, CallExpr call, int index | + call.getArg(index) = node.asExpr().getExpr() and + call.getStaticTarget() = f and + f.getParam(index).getPat().(IdentPat).getName().getText() = "password" + ) + } + } + + module ConstantPasswordFlow = TaintTracking::Global; + + from DataFlow::Node sourceNode, DataFlow::Node sinkNode + where ConstantPasswordFlow::flow(sourceNode, sinkNode) + select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString() + + +Further reading +--------------- + +- `Exploring data flow with path queries `__ in the GitHub documentation. + + +.. include:: ../reusables/rust-further-reading.rst +.. include:: ../reusables/codeql-ref-tools-further-reading.rst diff --git a/docs/codeql/codeql-language-guides/codeql-for-rust.rst b/docs/codeql/codeql-language-guides/codeql-for-rust.rst new file mode 100644 index 000000000000..24292909467e --- /dev/null +++ b/docs/codeql/codeql-language-guides/codeql-for-rust.rst @@ -0,0 +1,16 @@ + +.. _codeql-for-rust: + +CodeQL for Rust +========================= + +Experiment and learn how to write effective and efficient queries for CodeQL databases generated from Rust code. + +.. toctree:: + :hidden: + + codeql-library-for-rust + analyzing-data-flow-in-rust + +- :doc:`CodeQL library for Rust `: When analyzing Rust code, you can make use of the large collection of classes in the CodeQL library for Rust. +- :doc:`Analyzing data flow in Rust `: You can use CodeQL to track the flow of data through a Rust program to places where the data is used. diff --git a/docs/codeql/codeql-language-guides/codeql-library-for-rust.rst b/docs/codeql/codeql-language-guides/codeql-library-for-rust.rst new file mode 100644 index 000000000000..84da28c7dfc6 --- /dev/null +++ b/docs/codeql/codeql-language-guides/codeql-library-for-rust.rst @@ -0,0 +1,61 @@ +.. _codeql-library-for-rust: + +CodeQL library for Rust +================================= + +When analyzing Rust code, you can make use of the large collection of classes in the CodeQL library for Rust. + +Overview +-------- + +CodeQL ships with a library for analyzing Rust code. The classes in this library present the data from a CodeQL database in an object-oriented form and provide +abstractions and predicates to help you with common analysis tasks. + +The library is implemented as a set of CodeQL modules, which are files with the extension ``.qll``. The +module `rust.qll `__ imports most other standard library modules, so you can include them +by beginning your query with: + +.. code-block:: ql + + import rust + +The CodeQL libraries model various aspects of Rust code. The above import includes the abstract syntax tree (AST) library, which is used for locating program elements +to match syntactic elements in the source code. This can be used to find values, patterns, and structures. + +The control flow graph (CFG) is imported using: + +.. code-block:: ql + + import codeql.rust.controlflow.ControlFlowGraph + +The CFG models the control flow between statements and expressions. For example, it can determine whether one expression can +be evaluated before another expression, or whether an expression "dominates" another one, meaning that all paths to an +expression must flow through another expression first. + +The data flow library is imported using: + +.. code-block:: ql + + import codeql.rust.dataflow.DataFlow + +Data flow tracks the flow of data through the program, including across function calls (interprocedural data flow) and between steps in a job or workflow. +Data flow is particularly useful for security queries, where untrusted data flows to vulnerable parts of the program. The taint-tracking library is related to data flow, +and helps you find how data can *influence* other values in a program, even when it is not copied exactly. + +To summarize, the main Rust library modules are: + +.. list-table:: Main Rust library modules + :header-rows: 1 + + * - Import + - Description + * - ``rust`` + - The standard Rust library + * - ``codeql.rust.elements`` + - The abstract syntax tree library (also imported by `rust.qll`) + * - ``codeql.rust.controlflow.ControlFlowGraph`` + - The control flow graph library + * - ``codeql.rust.dataflow.DataFlow`` + - The data flow library + * - ``codeql.rust.dataflow.TaintTracking`` + - The taint tracking library diff --git a/docs/codeql/codeql-language-guides/index.rst b/docs/codeql/codeql-language-guides/index.rst index ca03ebffd759..5ec9a715a4d7 100644 --- a/docs/codeql/codeql-language-guides/index.rst +++ b/docs/codeql/codeql-language-guides/index.rst @@ -15,4 +15,5 @@ Experiment and learn how to write effective and efficient queries for CodeQL dat codeql-for-javascript codeql-for-python codeql-for-ruby + codeql-for-rust codeql-for-swift diff --git a/docs/codeql/codeql-overview/codeql-tools.rst b/docs/codeql/codeql-overview/codeql-tools.rst index ada1a75689ed..12d138974134 100644 --- a/docs/codeql/codeql-overview/codeql-tools.rst +++ b/docs/codeql/codeql-overview/codeql-tools.rst @@ -39,6 +39,8 @@ maintained by GitHub are: - ``codeql/python-all`` (`changelog `__, `source `__) - ``codeql/ruby-queries`` (`changelog `__, `source `__) - ``codeql/ruby-all`` (`changelog `__, `source `__) +- ``codeql/rust-queries`` (`changelog `__, `source `__) +- ``codeql/rust-all`` (`changelog `__, `source `__) - ``codeql/swift-queries`` (`changelog `__, `source `__) - ``codeql/swift-all`` (`changelog `__, `source `__) diff --git a/docs/codeql/query-help/codeql-cwe-coverage.rst b/docs/codeql/query-help/codeql-cwe-coverage.rst index 6236289a9ab5..0c0089a576da 100644 --- a/docs/codeql/query-help/codeql-cwe-coverage.rst +++ b/docs/codeql/query-help/codeql-cwe-coverage.rst @@ -35,5 +35,5 @@ Note that the CWE coverage includes both "`supported queries ` - :doc:`CodeQL query help for Python ` - :doc:`CodeQL query help for Ruby ` +- :doc:`CodeQL query help for Rust ` - :doc:`CodeQL query help for Swift ` .. pull-quote:: Information @@ -37,5 +38,6 @@ For a full list of the CWEs covered by these queries, see ":doc:`CodeQL CWE cove javascript python ruby + rust swift codeql-cwe-coverage diff --git a/docs/codeql/query-help/rust-cwe.md b/docs/codeql/query-help/rust-cwe.md new file mode 100644 index 000000000000..6468ff890acd --- /dev/null +++ b/docs/codeql/query-help/rust-cwe.md @@ -0,0 +1,7 @@ +# CWE coverage for Rust + +An overview of CWE coverage for Rust in the latest release of CodeQL. + +## Overview + + diff --git a/docs/codeql/query-help/rust.rst b/docs/codeql/query-help/rust.rst new file mode 100644 index 000000000000..b430fd3692e3 --- /dev/null +++ b/docs/codeql/query-help/rust.rst @@ -0,0 +1,8 @@ +CodeQL query help for Rust +============================ + +.. include:: ../reusables/query-help-overview.rst + +These queries are published in the CodeQL query pack ``codeql/rust-queries`` (`changelog `__, `source `__). + +.. include:: toc-rust.rst diff --git a/docs/codeql/reusables/extractors.rst b/docs/codeql/reusables/extractors.rst index 30ccef6e4654..c09926666b0a 100644 --- a/docs/codeql/reusables/extractors.rst +++ b/docs/codeql/reusables/extractors.rst @@ -6,9 +6,9 @@ - Identifier * - GitHub Actions - ``actions`` - * - C/C++ + * - C/C++ - ``cpp`` - * - C# + * - C# - ``csharp`` * - Go - ``go`` @@ -20,5 +20,7 @@ - ``python`` * - Ruby - ``ruby`` + - Rust + - ``rust`` * - Swift - - ``swift`` \ No newline at end of file + - ``swift`` diff --git a/docs/codeql/reusables/rust-further-reading.rst b/docs/codeql/reusables/rust-further-reading.rst new file mode 100644 index 000000000000..a82dee7f28e1 --- /dev/null +++ b/docs/codeql/reusables/rust-further-reading.rst @@ -0,0 +1,2 @@ +- `CodeQL queries for Rust `__ +- `CodeQL library reference for Rust `__ diff --git a/docs/codeql/reusables/supported-frameworks.rst b/docs/codeql/reusables/supported-frameworks.rst index 07a5e509fecb..3d89a6630041 100644 --- a/docs/codeql/reusables/supported-frameworks.rst +++ b/docs/codeql/reusables/supported-frameworks.rst @@ -307,6 +307,51 @@ and the CodeQL library pack ``codeql/ruby-all`` (`changelog `__, `source `__) +and the CodeQL library pack ``codeql/rust-all`` (`changelog `__, `source `__). +All support is experimental. + +.. csv-table:: + :header-rows: 1 + :class: fullWidthTable + :widths: auto + :align: left + + Name, Category + `actix-web `__, Web framework + alloc, Standard library + `clap `__, Utility library + core, Standard library + `digest `__, Cryptography library + `futures-executor `__, Utility library + `hyper `__, HTTP library + `hyper-util `__, HTTP library + `libc `__, Utility library + `log `__, Logging library + `md5 `__, Utility library + `memchr `__, Utility library + `once_cell `__, Utility library + `poem `__, Web framework + `postgres `__, Database + proc_macro, Standard library + `rand `__, Utility library + `regex `__, Utility library + `reqwest `__, HTTP client + `rocket `__, Web framework + `rusqlite `__, Database + std, Standard library + `rust-crypto `__, Cryptography library + `serde `__, Serialization + `smallvec `__, Utility library + `sqlx `__, Database + `tokio `__, Asynchronous IO + `tokio-postgres `__, Database + `url `__, Utility library + Swift built-in support ================================ diff --git a/docs/codeql/reusables/supported-versions-compilers.rst b/docs/codeql/reusables/supported-versions-compilers.rst index e85bc5d70f24..904a60b71cbd 100644 --- a/docs/codeql/reusables/supported-versions-compilers.rst +++ b/docs/codeql/reusables/supported-versions-compilers.rst @@ -25,8 +25,9 @@ JavaScript,ECMAScript 2022 or lower,Not applicable,"``.js``, ``.jsx``, ``.mjs``, ``.es``, ``.es6``, ``.htm``, ``.html``, ``.xhtm``, ``.xhtml``, ``.vue``, ``.hbs``, ``.ejs``, ``.njk``, ``.json``, ``.yaml``, ``.yml``, ``.raml``, ``.xml`` [8]_" Python [9]_,"2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13",Not applicable,``.py`` Ruby [10]_,"up to 3.3",Not applicable,"``.rb``, ``.erb``, ``.gemspec``, ``Gemfile``" - Swift [11]_,"Swift 5.4-6.1","Swift compiler","``.swift``" - TypeScript [12]_,"2.6-5.8",Standard TypeScript compiler,"``.ts``, ``.tsx``, ``.mts``, ``.cts``" + Rust [11]_,"Rust editions 2021 and 2024","Rust compiler","``.rs``, ``Cargo.toml``" + Swift [12]_,"Swift 5.4-6.1","Swift compiler","``.swift``" + TypeScript [13]_,"2.6-5.8",Standard TypeScript compiler,"``.ts``, ``.tsx``, ``.mts``, ``.cts``" .. container:: footnote-group @@ -40,5 +41,6 @@ .. [8] JSX and Flow code, YAML, JSON, HTML, and XML files may also be analyzed with JavaScript files. .. [9] The extractor requires Python 3 to run. To analyze Python 2.7 you should install both versions of Python. .. [10] Requires glibc 2.17. - .. [11] Support for the analysis of Swift requires macOS. - .. [12] TypeScript analysis is performed by running the JavaScript extractor with TypeScript enabled. This is the default. + .. [11] Requires ``rustup`` and ``cargo`` to be installed. Features from nightly toolchains are not supported. + .. [12] Support for the analysis of Swift requires macOS. + .. [13] TypeScript analysis is performed by running the JavaScript extractor with TypeScript enabled. This is the default. diff --git a/docs/codeql/writing-codeql-queries/about-codeql-queries.rst b/docs/codeql/writing-codeql-queries/about-codeql-queries.rst index f4e60b513c9a..92e4b963bff1 100644 --- a/docs/codeql/writing-codeql-queries/about-codeql-queries.rst +++ b/docs/codeql/writing-codeql-queries/about-codeql-queries.rst @@ -79,6 +79,7 @@ When writing your own alert queries, you would typically import the standard lib - :ref:`CodeQL library guide for JavaScript ` - :ref:`CodeQL library guide for Python ` - :ref:`CodeQL library guide for Ruby ` +- :ref:`CodeQL library guide for Rust ` - :ref:`CodeQL library guide for TypeScript ` There are also libraries containing commonly used predicates, types, and other modules associated with different analyses, including data flow, control flow, and taint-tracking. In order to calculate path graphs, path queries require you to import a data flow library into the query file. For more information, see ":doc:`Creating path queries `." diff --git a/docs/query-metadata-style-guide.md b/docs/query-metadata-style-guide.md index 27b9ee6635fc..18fa5d1880f5 100644 --- a/docs/query-metadata-style-guide.md +++ b/docs/query-metadata-style-guide.md @@ -25,6 +25,7 @@ For examples of query files for the languages supported by CodeQL, visit the fol * [JavaScript queries](https://codeql.github.com/codeql-query-help/javascript/) * [Python queries](https://codeql.github.com/codeql-query-help/python/) * [Ruby queries](https://codeql.github.com/codeql-query-help/ruby/) +* [Rust queries](https://codeql.github.com/codeql-query-help/rust/) * [Swift queries](https://codeql.github.com/codeql-query-help/swift/) ## Metadata area @@ -162,7 +163,7 @@ In addition to the "top-level" categories, we will also add sub-categories to fu * `@tags readability`–for queries that detect confusing patterns that make it harder for developers to read the code. * `@tags useless-code`-for queries that detect functions that are never used and other instances of unused code * `@tags complexity`-for queries that detect patterns in the code that lead to unnecesary complexity such as unclear control flow, or high cyclomatic complexity - + * `@tags reliability`–for queries that detect issues that affect whether the code will perform as expected during execution. * `@tags correctness`–for queries that detect incorrect program behavior or couse result in unintended outcomes. diff --git a/rust/ql/lib/change-notes/2025-06-13-public-preview.md b/rust/ql/lib/change-notes/2025-06-13-public-preview.md new file mode 100644 index 000000000000..d60dc3315b83 --- /dev/null +++ b/rust/ql/lib/change-notes/2025-06-13-public-preview.md @@ -0,0 +1,4 @@ +--- +category: feature +--- +* Initial public preview release. diff --git a/rust/ql/src/change-notes/2025-06-13-public-preview.md b/rust/ql/src/change-notes/2025-06-13-public-preview.md new file mode 100644 index 000000000000..ab2250e3b587 --- /dev/null +++ b/rust/ql/src/change-notes/2025-06-13-public-preview.md @@ -0,0 +1,4 @@ +--- +category: newQuery +--- +* Initial public preview release.