Skip to content

Commit 4eb98fc

Browse files
authored
Merge pull request #19280 from github/redsun82/rust-doc
Rust: update docs for public preview
2 parents 12cda86 + 915b0b3 commit 4eb98fc

17 files changed

+409
-9
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
.. _analyzing-data-flow-in-rust:
2+
3+
Analyzing data flow in Rust
4+
=============================
5+
6+
You can use CodeQL to track the flow of data through a Rust program to places where the data is used.
7+
8+
About this article
9+
------------------
10+
11+
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.
12+
The following sections describe how to use the libraries for local data flow, global data flow, and taint tracking.
13+
For a more general introduction to modeling data flow, see ":ref:`About data flow analysis <about-data-flow-analysis>`."
14+
15+
.. include:: ../reusables/new-data-flow-api.rst
16+
17+
Local data flow
18+
---------------
19+
20+
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.
21+
22+
Using local data flow
23+
~~~~~~~~~~~~~~~~~~~~~
24+
25+
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.
26+
Common ``Node`` types include expression nodes (``ExprNode``) and parameter nodes (``ParameterNode``).
27+
You can use the ``asExpr`` member predicate to map a data flow ``ExprNode`` to its corresponding ``ExprCfgNode`` in the control-flow library.
28+
Similarly, you can map a data flow ``ParameterNode`` to its corresponding ``Parameter`` AST node using the ``asParameter`` member predicate.
29+
30+
.. code-block:: ql
31+
32+
class Node {
33+
/**
34+
* Gets the expression corresponding to this node, if any.
35+
*/
36+
CfgNodes::ExprCfgNode asExpr() { ... }
37+
38+
/**
39+
* Gets the parameter corresponding to this node, if any.
40+
*/
41+
Parameter asParameter() { ... }
42+
43+
...
44+
}
45+
46+
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()``.
47+
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.
48+
49+
The predicate ``localFlowStep(Node nodeFrom, Node nodeTo)`` holds if there is an immediate data flow edge from the node ``nodeFrom`` to the node ``nodeTo``.
50+
You can apply the predicate recursively by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localFlow``.
51+
52+
For example, you can find flow from an expression ``source`` to an expression ``sink`` in zero or more local steps:
53+
54+
.. code-block:: ql
55+
56+
DataFlow::localFlow(source, sink)
57+
58+
Using local taint tracking
59+
~~~~~~~~~~~~~~~~~~~~~~~~~~
60+
61+
Local taint tracking extends local data flow to include flow steps where values are not preserved, for example, string manipulation.
62+
For example:
63+
64+
.. code-block:: rust
65+
66+
let y: String = "Hello ".to_owned() + x
67+
68+
If ``x`` is a tainted string then ``y`` is also tainted.
69+
70+
The local taint tracking library is in the module ``TaintTracking``.
71+
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``.
72+
You can apply the predicate recursively by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localTaint``.
73+
74+
For example, you can find taint propagation from an expression ``source`` to an expression ``sink`` in zero or more local steps:
75+
76+
.. code-block:: ql
77+
78+
TaintTracking::localTaint(source, sink)
79+
80+
81+
Using local sources
82+
~~~~~~~~~~~~~~~~~~~
83+
84+
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.
85+
The next section provides concrete examples, but first introduces the concept of a local source.
86+
87+
A local source is a data-flow node with no local data flow into it.
88+
It is a local origin of data flow, a place where a new value is created.
89+
This includes parameters (which only receive values from global data flow) and most expressions (because they are not value-preserving).
90+
The class ``LocalSourceNode`` represents data-flow nodes that are also local sources.
91+
It includes a useful member predicate ``flowsTo(DataFlow::Node node)``, which holds if there is local data flow from the local source to ``node``.
92+
93+
Examples of local data flow
94+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
95+
96+
This query finds the argument passed in each call to ``File::create``:
97+
98+
.. code-block:: ql
99+
100+
import rust
101+
102+
from CallExpr call
103+
where call.getStaticTarget().(Function).getCanonicalPath() = "<std::fs::File>::create"
104+
select call.getArg(0)
105+
106+
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.
107+
108+
.. code-block:: ql
109+
110+
import rust
111+
import codeql.rust.dataflow.DataFlow
112+
113+
from CallExpr call, DataFlow::ExprNode source, DataFlow::ExprNode sink
114+
where
115+
call.getStaticTarget().(Function).getCanonicalPath() = "<std::fs::File>::create" and
116+
sink.asExpr().getExpr() = call.getArg(0) and
117+
DataFlow::localFlow(source, sink)
118+
select source, sink
119+
120+
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:
121+
122+
.. code-block:: ql
123+
124+
import rust
125+
import codeql.rust.dataflow.DataFlow
126+
127+
from CallExpr call, DataFlow::ParameterNode source, DataFlow::ExprNode sink
128+
where
129+
call.getStaticTarget().(Function).getCanonicalPath() = "<std::fs::File>::create" and
130+
sink.asExpr().getExpr() = call.getArg(0) and
131+
DataFlow::localFlow(source, sink)
132+
select source, sink
133+
134+
Global data flow
135+
----------------
136+
137+
Global data flow tracks data flow throughout the entire program, and is therefore more powerful than local data flow.
138+
However, global data flow is less precise than local data flow, and the analysis typically requires significantly more time and memory to perform.
139+
140+
.. pull-quote:: Note
141+
142+
.. include:: ../reusables/path-problem.rst
143+
144+
Using global data flow
145+
~~~~~~~~~~~~~~~~~~~~~~
146+
147+
We can use the global data flow library by implementing the signature ``DataFlow::ConfigSig`` and applying the module ``DataFlow::Global<ConfigSig>``:
148+
149+
.. code-block:: ql
150+
151+
import codeql.rust.dataflow.DataFlow
152+
153+
module MyDataFlowConfiguration implements DataFlow::ConfigSig {
154+
predicate isSource(DataFlow::Node source) {
155+
...
156+
}
157+
158+
predicate isSink(DataFlow::Node sink) {
159+
...
160+
}
161+
}
162+
163+
module MyDataFlow = DataFlow::Global<MyDataFlowConfiguration>;
164+
165+
These predicates are defined in the configuration:
166+
167+
- ``isSource`` - defines where data may flow from.
168+
- ``isSink`` - defines where data may flow to.
169+
- ``isBarrier`` - optional, defines where data flow is blocked.
170+
- ``isAdditionalFlowStep`` - optional, adds additional flow steps.
171+
172+
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)``:
173+
174+
.. code-block:: ql
175+
176+
from DataFlow::Node source, DataFlow::Node sink
177+
where MyDataFlow::flow(source, sink)
178+
select source, "Dataflow to $@.", sink, sink.toString()
179+
180+
Using global taint tracking
181+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
182+
183+
Global taint tracking relates to global data flow in the same way that local taint tracking relates to local data flow.
184+
In other words, global taint tracking extends global data flow with additional non-value-preserving steps.
185+
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``:
186+
187+
.. code-block:: ql
188+
189+
module MyTaintFlow = TaintTracking::Global<MyDataFlowConfiguration>;
190+
191+
from DataFlow::Node source, DataFlow::Node sink
192+
where MyTaintFlow::flow(source, sink)
193+
select source, "Taint flow to $@.", sink, sink.toString()
194+
195+
Predefined sources
196+
~~~~~~~~~~~~~~~~~~
197+
198+
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.
199+
200+
Examples of global data flow
201+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202+
203+
The following global taint-tracking query finds places where a string literal is used in a function call argument named "password".
204+
- Since this is a taint-tracking query, the ``TaintTracking::Global`` module is used.
205+
- The ``isSource`` predicate defines sources as any ``StringLiteralExpr``.
206+
- The ``isSink`` predicate defines sinks as arguments to a ``CallExpr`` called "password".
207+
- 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".
208+
209+
.. code-block:: ql
210+
211+
import rust
212+
import codeql.rust.dataflow.DataFlow
213+
import codeql.rust.dataflow.TaintTracking
214+
215+
module ConstantPasswordConfig implements DataFlow::ConfigSig {
216+
predicate isSource(DataFlow::Node node) { node.asExpr().getExpr() instanceof StringLiteralExpr }
217+
218+
predicate isSink(DataFlow::Node node) {
219+
// any argument going to a parameter called `password`
220+
exists(Function f, CallExpr call, int index |
221+
call.getArg(index) = node.asExpr().getExpr() and
222+
call.getStaticTarget() = f and
223+
f.getParam(index).getPat().(IdentPat).getName().getText() = "password"
224+
)
225+
}
226+
}
227+
228+
module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>;
229+
230+
from DataFlow::Node sourceNode, DataFlow::Node sinkNode
231+
where ConstantPasswordFlow::flow(sourceNode, sinkNode)
232+
select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString()
233+
234+
235+
Further reading
236+
---------------
237+
238+
- `Exploring data flow with path queries <https://docs.github.com/en/code-security/codeql-for-vs-code/getting-started-with-codeql-for-vs-code/exploring-data-flow-with-path-queries>`__ in the GitHub documentation.
239+
240+
241+
.. include:: ../reusables/rust-further-reading.rst
242+
.. include:: ../reusables/codeql-ref-tools-further-reading.rst
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
.. _codeql-for-rust:
3+
4+
CodeQL for Rust
5+
=========================
6+
7+
Experiment and learn how to write effective and efficient queries for CodeQL databases generated from Rust code.
8+
9+
.. toctree::
10+
:hidden:
11+
12+
codeql-library-for-rust
13+
analyzing-data-flow-in-rust
14+
15+
- :doc:`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.
16+
- :doc:`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.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.. _codeql-library-for-rust:
2+
3+
CodeQL library for Rust
4+
=================================
5+
6+
When analyzing Rust code, you can make use of the large collection of classes in the CodeQL library for Rust.
7+
8+
Overview
9+
--------
10+
11+
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
12+
abstractions and predicates to help you with common analysis tasks.
13+
14+
The library is implemented as a set of CodeQL modules, which are files with the extension ``.qll``. The
15+
module `rust.qll <https://github.com/github/codeql/blob/main/rust/ql/lib/rust.qll>`__ imports most other standard library modules, so you can include them
16+
by beginning your query with:
17+
18+
.. code-block:: ql
19+
20+
import rust
21+
22+
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
23+
to match syntactic elements in the source code. This can be used to find values, patterns, and structures.
24+
25+
The control flow graph (CFG) is imported using:
26+
27+
.. code-block:: ql
28+
29+
import codeql.rust.controlflow.ControlFlowGraph
30+
31+
The CFG models the control flow between statements and expressions. For example, it can determine whether one expression can
32+
be evaluated before another expression, or whether an expression "dominates" another one, meaning that all paths to an
33+
expression must flow through another expression first.
34+
35+
The data flow library is imported using:
36+
37+
.. code-block:: ql
38+
39+
import codeql.rust.dataflow.DataFlow
40+
41+
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.
42+
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,
43+
and helps you find how data can *influence* other values in a program, even when it is not copied exactly.
44+
45+
To summarize, the main Rust library modules are:
46+
47+
.. list-table:: Main Rust library modules
48+
:header-rows: 1
49+
50+
* - Import
51+
- Description
52+
* - ``rust``
53+
- The standard Rust library
54+
* - ``codeql.rust.elements``
55+
- The abstract syntax tree library (also imported by `rust.qll`)
56+
* - ``codeql.rust.controlflow.ControlFlowGraph``
57+
- The control flow graph library
58+
* - ``codeql.rust.dataflow.DataFlow``
59+
- The data flow library
60+
* - ``codeql.rust.dataflow.TaintTracking``
61+
- The taint tracking library

docs/codeql/codeql-language-guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Experiment and learn how to write effective and efficient queries for CodeQL dat
1515
codeql-for-javascript
1616
codeql-for-python
1717
codeql-for-ruby
18+
codeql-for-rust
1819
codeql-for-swift

docs/codeql/codeql-overview/codeql-tools.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ maintained by GitHub are:
3939
- ``codeql/python-all`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/python/ql/lib/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/python/ql/lib>`__)
4040
- ``codeql/ruby-queries`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/ruby/ql/src/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/ruby/ql/src>`__)
4141
- ``codeql/ruby-all`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/ruby/ql/lib/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/ruby/ql/lib>`__)
42+
- ``codeql/rust-queries`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/rust/ql/src/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/rust/ql/src>`__)
43+
- ``codeql/rust-all`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/rust/ql/lib/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/rust/ql/lib>`__)
4244
- ``codeql/swift-queries`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/swift/ql/src/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/swift/ql/src>`__)
4345
- ``codeql/swift-all`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/swift/ql/lib/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/swift/ql/lib>`__)
4446

docs/codeql/query-help/codeql-cwe-coverage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ Note that the CWE coverage includes both "`supported queries <https://github.com
3535
javascript-cwe
3636
python-cwe
3737
ruby-cwe
38+
rust-cwe
3839
swift-cwe
39-

docs/codeql/query-help/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ View the query help for the queries included in the ``default``, ``security-exte
1111
- :doc:`CodeQL query help for JavaScript and TypeScript <javascript>`
1212
- :doc:`CodeQL query help for Python <python>`
1313
- :doc:`CodeQL query help for Ruby <ruby>`
14+
- :doc:`CodeQL query help for Rust <rust>`
1415
- :doc:`CodeQL query help for Swift <swift>`
1516

1617
.. pull-quote:: Information
@@ -37,5 +38,6 @@ For a full list of the CWEs covered by these queries, see ":doc:`CodeQL CWE cove
3738
javascript
3839
python
3940
ruby
41+
rust
4042
swift
4143
codeql-cwe-coverage

docs/codeql/query-help/rust-cwe.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CWE coverage for Rust
2+
3+
An overview of CWE coverage for Rust in the latest release of CodeQL.
4+
5+
## Overview
6+
7+
<!-- autogenerated CWE coverage table will be added below -->

docs/codeql/query-help/rust.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CodeQL query help for Rust
2+
============================
3+
4+
.. include:: ../reusables/query-help-overview.rst
5+
6+
These queries are published in the CodeQL query pack ``codeql/rust-queries`` (`changelog <https://github.com/github/codeql/tree/codeql-cli/latest/rust/ql/src/CHANGELOG.md>`__, `source <https://github.com/github/codeql/tree/codeql-cli/latest/rust/ql/src>`__).
7+
8+
.. include:: toc-rust.rst

docs/codeql/reusables/extractors.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
- Identifier
77
* - GitHub Actions
88
- ``actions``
9-
* - C/C++
9+
* - C/C++
1010
- ``cpp``
11-
* - C#
11+
* - C#
1212
- ``csharp``
1313
* - Go
1414
- ``go``
@@ -20,5 +20,7 @@
2020
- ``python``
2121
* - Ruby
2222
- ``ruby``
23+
- Rust
24+
- ``rust``
2325
* - Swift
24-
- ``swift``
26+
- ``swift``

0 commit comments

Comments
 (0)