Skip to content

Commit c6dc55e

Browse files
committed
C++: Update use-use dataflow docs to reflect what is going on in the library
1 parent 2f797ff commit c6dc55e

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

docs/codeql/codeql-language-guides/analyzing-data-flow-in-cpp-new.rst

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
.. _analyzing-data-flow-in-cpp-new:
22

3+
.. pull-quote:: Note
4+
5+
The data flow library described here is available from CodeQL 2.13.0 onwards. See :ref:`here <analyzing-data-flow-in-cpp>` for the library available in earlier versions.
6+
37
Analyzing data flow in C and C++
48
================================
59

6-
:ref:`Data flow <analyzing-data-flow-in-cpp>`
7-
810
You can use data flow analysis to track the flow of potentially malicious or insecure data that can cause vulnerabilities in your codebase.
911

1012
About data flow
@@ -20,27 +22,45 @@ Local data flow is data flow within a single function. Local data flow is usuall
2022
Using local data flow
2123
~~~~~~~~~~~~~~~~~~~~~
2224

23-
The local data flow library is in the module ``DataFlow``, which defines the class ``Node`` denoting any element that data can flow through. ``Node``\ s are divided into expression nodes (``ExprNode``) and parameter nodes (``ParameterNode``). It is possible to map between data flow nodes and expressions/parameters using the member predicates ``asExpr`` and ``asParameter``:
25+
The local data flow library is in the module ``DataFlow``, which defines the class ``Node`` denoting any element that data can flow through. ``Node``\ s are divided into expression nodes (``ExprNode``) and parameter nodes (``ParameterNode``). It is possible to map between data flow nodes and expressions/parameters using the member predicates ``asExpr``, ``asIndirectExpr``, and ``asParameter``:
2426

2527
.. code-block:: ql
2628
2729
class Node {
28-
/** Gets the expression corresponding to this node, if any. */
30+
/**
31+
* Gets the expression corresponding to this node, if any.
32+
*/
2933
Expr asExpr() { ... }
3034
31-
/** Gets the parameter corresponding to this node, if any. */
35+
/**
36+
* Gets the expression corresponding to this node, if any, after dereferencing
37+
* the expression `index` times.
38+
*/
39+
Expr asIndirectExpr(int index) { ... }
40+
41+
/**
42+
* Gets the parameter corresponding to this node, if any.
43+
*/
3244
Parameter asParameter() { ... }
3345
46+
/**
47+
* Gets the parameter corresponding to this node, if any, after dereferencing
48+
* the expression `index` times.
49+
*/
50+
Parameter asParameter(int index) { ... }
51+
3452
...
3553
}
3654
3755
The predicate ``localFlowStep(Node nodeFrom, Node nodeTo)`` holds if there is an immediate data flow edge from the node ``nodeFrom`` to the node ``nodeTo``. The predicate can be applied recursively (using the ``+`` and ``*`` operators), or through the predefined recursive predicate ``localFlow``, which is equivalent to ``localFlowStep*``.
3856

39-
For example, finding flow from a parameter ``source`` to an expression ``sink`` in zero or more local steps can be achieved as follows:
57+
For example, finding flow from a parameter ``source`` to an expression ``sink`` in zero or more local steps can be achieved as follows, where ``nodeFrom`` and ``nodeTo`` are of type ``DataFlow::Node``:
4058

4159
.. code-block:: ql
4260
43-
DataFlow::localFlow(DataFlow::parameterNode(source), DataFlow::exprNode(sink))
61+
nodeFrom.asParameter() = source and
62+
nodeTo.asExpr() = sink and
63+
DataFlow::localFlow(nodeFrom, nodeTo)
4464
4565
Using local taint tracking
4666
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -56,11 +76,13 @@ In this case, the argument to ``malloc`` is tainted.
5676

5777
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``. The predicate can be applied recursively (using the ``+`` and ``*`` operators), or through the predefined recursive predicate ``localTaint``, which is equivalent to ``localTaintStep*``.
5878

59-
For example, finding taint propagation from a parameter ``source`` to an expression ``sink`` in zero or more local steps can be achieved as follows:
79+
For example, finding taint propagation from a parameter ``source`` to an expression ``sink`` in zero or more local steps can be achieved as follows, where ``nodeFrom`` and ``nodeTo`` are of type ``DataFlow::Node``:
6080

6181
.. code-block:: ql
6282
63-
TaintTracking::localTaint(DataFlow::parameterNode(source), DataFlow::exprNode(sink))
83+
nodeFrom.asParameter() = source and
84+
nodeTo.asExpr() = sink and
85+
TaintTracking::localTaint(nodeFrom, nodeTo):
6486
6587
Examples
6688
~~~~~~~~
@@ -77,7 +99,7 @@ The following query finds the filename passed to ``fopen``.
7799
fc.getTarget() = fopen
78100
select fc.getArgument(0)
79101
80-
Unfortunately, this will only give the expression in the argument, not the values which could be passed to it. So we use local data flow to find all expressions that flow into the argument:
102+
Unfortunately, this will only give the expression in the argument, not the values which could be passed to it. So we use local data flow to find all expressions that flow into the argument, where we use ``asIndirectExpr(1)`` as we are interested in the value of the string passed to `fopen`, not the pointer pointing to it:
81103

82104
.. code-block:: ql
83105

docs/codeql/codeql-language-guides/analyzing-data-flow-in-cpp.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
.. _analyzing-data-flow-in-cpp:
22

3+
.. pull-quote:: Note
4+
5+
The data flow library described here will be deprecated in the near future. For the replacement library see :ref:`here <analyzing-data-flow-in-cpp-new>`.
6+
37
Analyzing data flow in C and C++
48
================================
59

6-
:ref:`Data flow <analyzing-data-flow-in-cpp-new>`
7-
810
You can use data flow analysis to track the flow of potentially malicious or insecure data that can cause vulnerabilities in your codebase.
911

1012
About data flow

0 commit comments

Comments
 (0)