Skip to content

Commit eb2fdda

Browse files
committed
C# pre-migration changes: titles & intros
1 parent dcb41a1 commit eb2fdda

3 files changed

Lines changed: 131 additions & 125 deletions

File tree

docs/language/learn-ql/csharp/dataflow.rst

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Analyzing data flow in C#
22
=========================
33

4-
Overview
5-
--------
4+
You can use CodeQL to track the flow of data through a C# program to its use.
65

7-
This topic describes how data flow analysis is implemented in the CodeQL libraries for C# and includes examples to help you write your own data flow queries.
8-
The following sections describe how to utilize the libraries for local data flow, global data flow, and taint tracking.
6+
About this article
7+
------------------
8+
9+
This article describes how data flow analysis is implemented in the CodeQL libraries for C# and includes examples to help you write your own data flow queries.
10+
The following sections describe how to use the libraries for local data flow, global data flow, and taint tracking.
911

1012
For a more general introduction to modeling data flow, see :doc:`Introduction to data flow analysis with CodeQL <../intro-to-data-flow>`.
1113

@@ -17,7 +19,7 @@ Local data flow is data flow within a single method or callable. Local data flow
1719
Using local data flow
1820
~~~~~~~~~~~~~~~~~~~~~
1921

20-
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``:
22+
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``). You can map between data flow nodes and expressions/parameters using the member predicates ``asExpr`` and ``asParameter``:
2123

2224
.. code-block:: ql
2325
@@ -45,9 +47,9 @@ or using the predicates ``exprNode`` and ``parameterNode``:
4547
*/
4648
ParameterNode parameterNode(Parameter p) { ... }
4749
48-
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 it is possible to use the predefined recursive predicate ``localFlow``.
50+
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``.
4951

50-
For example, finding flow from a parameter ``source`` to an expression ``sink`` in zero or more local steps can be achieved as follows:
52+
For example, you can find flow from a parameter ``source`` to an expression ``sink`` in zero or more local steps:
5153

5254
.. code-block:: ql
5355
@@ -65,9 +67,9 @@ Local taint tracking extends local data flow by including non-value-preserving f
6567
6668
If ``x`` is a tainted string then ``y`` is also tainted.
6769

68-
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 it is possible to use the predefined recursive predicate ``localTaint``.
70+
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``.
6971

70-
For example, finding taint propagation from a parameter ``source`` to an expression ``sink`` in zero or more local steps can be achieved as follows:
72+
For example, you can find taint propagation from a parameter ``source`` to an expression ``sink`` in zero or more local steps:
7173

7274
.. code-block:: ql
7375
@@ -76,7 +78,7 @@ For example, finding taint propagation from a parameter ``source`` to an express
7678
Examples
7779
~~~~~~~~
7880

79-
The following query finds the filename passed to ``System.IO.File.Open``:
81+
This query finds the filename passed to ``System.IO.File.Open``:
8082

8183
.. code-block:: ql
8284
@@ -99,7 +101,7 @@ Unfortunately this will only give the expression in the argument, not the values
99101
and DataFlow::localFlow(DataFlow::exprNode(src), DataFlow::exprNode(call.getArgument(0)))
100102
select src
101103
102-
Then we can make the source more specific, for example an access to a public parameter. The following query finds where a public parameter is used to open a file:
104+
Then we can make the source more specific, for example an access to a public parameter. This query finds instances of a public parameter being used to open a file:
103105

104106
.. code-block:: ql
105107
@@ -112,7 +114,7 @@ Then we can make the source more specific, for example an access to a public par
112114
and call.getEnclosingCallable().(Member).isPublic()
113115
select p, "Opening a file from a public method."
114116
115-
The following example finds calls to ``String.Format`` where the format string isn't hard-coded:
117+
This query finds calls to ``String.Format`` where the format string isn't hard-coded:
116118

117119
.. code-block:: ql
118120
@@ -139,7 +141,7 @@ Global data flow tracks data flow throughout the entire program, and is therefor
139141
Using global data flow
140142
~~~~~~~~~~~~~~~~~~~~~~
141143

142-
The global data flow library is used by extending the class ``DataFlow::Configuration`` as follows:
144+
The global data flow library is used by extending the class ``DataFlow::Configuration``:
143145

144146
.. code-block:: ql
145147
@@ -157,12 +159,12 @@ The global data flow library is used by extending the class ``DataFlow::Configur
157159
}
158160
}
159161
160-
The following predicates are defined in the configuration:
162+
These predicates are defined in the configuration:
161163

162-
- ``isSource`` - defines where data may flow from
163-
- ``isSink`` - defines where data may flow to
164-
- ``isBarrier`` - optionally, restricts the data flow
165-
- ``isAdditionalFlowStep`` - optionally, adds additional flow steps
164+
- ``isSource`` - defines where data may flow from.
165+
- ``isSink`` - defines where data may flow to.
166+
- ``isBarrier`` - optionally, restricts the data flow.
167+
- ``isAdditionalFlowStep`` - optionally, adds additional flow steps.
166168

167169
The characteristic predicate (``MyDataFlowConfiguration()``) defines the name of the configuration, so ``"..."`` must be replaced with a unique name.
168170

@@ -177,7 +179,7 @@ The data flow analysis is performed using the predicate ``hasFlow(DataFlow::Node
177179
Using global taint tracking
178180
~~~~~~~~~~~~~~~~~~~~~~~~~~~
179181

180-
Global taint tracking is to global data flow what local taint tracking is to local data flow. That is, global taint tracking extends global data flow with additional non-value-preserving steps. The global taint tracking library is used by extending the class ``TaintTracking::Configuration`` as follows:
182+
Global taint tracking is to global data flow what local taint tracking is to local data flow. That is, global taint tracking extends global data flow with additional non-value-preserving steps. The global taint tracking library is used by extending the class ``TaintTracking::Configuration``:
181183

182184
.. code-block:: ql
183185
@@ -195,12 +197,12 @@ Global taint tracking is to global data flow what local taint tracking is to loc
195197
}
196198
}
197199
198-
The following predicates are defined in the configuration:
200+
These predicates are defined in the configuration:
199201

200-
- ``isSource`` - defines where taint may flow from
201-
- ``isSink`` - defines where taint may flow to
202-
- ``isSanitizer`` - optionally, restricts the taint flow
203-
- ``isAdditionalTaintStep`` - optionally, adds additional taint steps
202+
- ``isSource`` - defines where taint may flow from.
203+
- ``isSink`` - defines where taint may flow to.
204+
- ``isSanitizer`` - optionally, restricts the taint flow.
205+
- ``isAdditionalTaintStep`` - optionally, adds additional taint steps.
204206

205207
Similar to global data flow, the characteristic predicate (``MyTaintTrackingConfiguration()``) defines the unique name of the configuration and the taint analysis is performed using the predicate ``hasFlow(DataFlow::Node source, DataFlow::Node sink)``.
206208

@@ -214,7 +216,7 @@ The class ``RemoteSourceFlow`` (defined in module ``semmle.code.csharp.dataflow.
214216
Example
215217
~~~~~~~
216218

217-
The following example shows a data flow configuration that uses all public API parameters as data sources.
219+
This query shows a data flow configuration that uses all public API parameters as data sources:
218220

219221
.. code-block:: ql
220222
@@ -236,30 +238,30 @@ The following example shows a data flow configuration that uses all public API p
236238
Class hierarchy
237239
~~~~~~~~~~~~~~~
238240

239-
- ``DataFlow::Configuration`` - base class for custom global data flow analysis
240-
- ``DataFlow::Node`` - an element behaving as a data flow node
241+
- ``DataFlow::Configuration`` - base class for custom global data flow analysis.
242+
- ``DataFlow::Node`` - an element behaving as a data flow node.
241243

242-
- ``DataFlow::ExprNode`` - an expression behaving as a data flow node
243-
- ``DataFlow::ParameterNode`` - a parameter data flow node representing the value of a parameter at function entry
244+
- ``DataFlow::ExprNode`` - an expression behaving as a data flow node.
245+
- ``DataFlow::ParameterNode`` - a parameter data flow node representing the value of a parameter at function entry.
244246

245-
- ``PublicCallableParameter`` - a parameter to a public method/callable in a public class
247+
- ``PublicCallableParameter`` - a parameter to a public method/callable in a public class.
246248

247-
- ``RemoteSourceFlow`` - data flow from network/remote input
249+
- ``RemoteSourceFlow`` - data flow from network/remote input.
248250

249-
- ``AspNetRemoteFlowSource`` - data flow from remote ASP.NET user input
251+
- ``AspNetRemoteFlowSource`` - data flow from remote ASP.NET user input.
250252

251-
- ``AspNetQueryStringRemoteFlowSource`` - data flow from ``System.Web.HttpRequest``
252-
- ``AspNetUserInputRemoveFlowSource`` - data flow from ``System.Web.IO.WebControls.TextBox``
253+
- ``AspNetQueryStringRemoteFlowSource`` - data flow from ``System.Web.HttpRequest``.
254+
- ``AspNetUserInputRemoveFlowSource`` - data flow from ``System.Web.IO.WebControls.TextBox``.
253255

254-
- ``WcfRemoteFlowSource`` - data flow from a WCF web service
255-
- ``AspNetServiceRemoteFlowSource`` - data flow from an ASP.NET web service
256+
- ``WcfRemoteFlowSource`` - data flow from a WCF web service.
257+
- ``AspNetServiceRemoteFlowSource`` - data flow from an ASP.NET web service.
256258

257-
- ``TaintTracking::Configuration`` - base class for custom global taint tracking analysis
259+
- ``TaintTracking::Configuration`` - base class for custom global taint tracking analysis.
258260

259261
Examples
260262
~~~~~~~~
261263

262-
The following data flow configuration tracks data flow from environment variables to opening files:
264+
This data flow configuration tracks data flow from environment variables to opening files:
263265

264266
.. code-block:: ql
265267
@@ -300,42 +302,42 @@ Exercise 4: Using the answers from 2 and 3, write a query to find all global dat
300302
Extending library data flow
301303
---------------------------
302304

303-
*Library* data flow defines how data flows through libraries where the source code is not available, such as the .NET Framework, third-party libraries or proprietary libraries.
305+
Library data flow defines how data flows through libraries where the source code is not available, such as the .NET Framework, third-party libraries or proprietary libraries.
304306

305307
To define new library data flow, extend the class ``LibraryTypeDataFlow`` from the module ``semmle.code.csharp.dataflow.LibraryTypeDataFlow``. Override the predicate ``callableFlow`` to define how data flows through the methods in the class. ``callableFlow`` has the signature
306308

307309
.. code-block:: ql
308310
309311
predicate callableFlow(CallableFlowSource source, CallableFlowSink sink, SourceDeclarationCallable callable, boolean preservesValue)
310312
311-
- ``callable`` - the ``Callable`` (such as a method, constructor, property getter or setter) performing the data flow
312-
- ``source`` - the data flow input
313-
- ``sink`` - the data flow output
313+
- ``callable`` - the ``Callable`` (such as a method, constructor, property getter or setter) performing the data flow.
314+
- ``source`` - the data flow input.
315+
- ``sink`` - the data flow output.
314316
- ``preservesValue`` - whether the flow step preserves the value, for example if ``x`` is a string then ``x.ToString()`` preserves the value where as ``x.ToLower()`` does not.
315317

316318
Class hierarchy
317319
~~~~~~~~~~~~~~~
318320

319321
- ``Callable`` - a callable (methods, accessors, constructors etc.)
320322

321-
- ``SourceDeclarationCallable`` - an unconstructed callable
323+
- ``SourceDeclarationCallable`` - an unconstructed callable.
322324

323-
- ``CallableFlowSource`` - the input of data flow into the callable
325+
- ``CallableFlowSource`` - the input of data flow into the callable.
324326

325-
- ``CallableFlowSourceQualifier`` - the data flow comes from the object itself
326-
- ``CallableFlowSourceArg`` - the data flow comes from an argument to the call
327+
- ``CallableFlowSourceQualifier`` - the data flow comes from the object itself.
328+
- ``CallableFlowSourceArg`` - the data flow comes from an argument to the call.
327329

328-
- ``CallableFlowSink`` - the output of data flow from the callable
330+
- ``CallableFlowSink`` - the output of data flow from the callable.
329331

330-
- ``CallableFlowSinkQualifier`` - the output is to the object itself
331-
- ``CallableFlowSinkReturn`` - the output is returned from the call
332-
- ``CallableFlowSinkArg`` - the output is an argument
333-
- ``CallableFlowSinkDelegateArg`` - the output flows through a delegate argument (for example, LINQ)
332+
- ``CallableFlowSinkQualifier`` - the output is to the object itself.
333+
- ``CallableFlowSinkReturn`` - the output is returned from the call.
334+
- ``CallableFlowSinkArg`` - the output is an argument.
335+
- ``CallableFlowSinkDelegateArg`` - the output flows through a delegate argument (for example, LINQ).
334336

335337
Example
336338
~~~~~~~
337339

338-
The following example is adapted from ``LibraryTypeDataFlow.qll``. It declares data flow through the class ``System.Uri``, including the constructor, the ``ToString`` method, and the properties ``Query``, ``OriginalString``, and ``PathAndQuery``.
340+
This example is adapted from ``LibraryTypeDataFlow.qll``. It declares data flow through the class ``System.Uri``, including the constructor, the ``ToString`` method, and the properties ``Query``, ``OriginalString``, and ``PathAndQuery``.
339341

340342
.. code-block:: ql
341343
@@ -489,7 +491,7 @@ Exercise 4
489491
Exercise 5
490492
~~~~~~~~~~
491493

492-
All properties can flow data. We can declare this as follows:
494+
All properties can flow data:
493495

494496
.. code-block:: ql
495497
@@ -545,8 +547,8 @@ This can be adapted from the ``SystemUriFlow`` class:
545547
}
546548
}
547549
548-
What next?
549-
----------
550+
Further reading
551+
---------------
550552

551553
- Learn about the standard libraries used to write queries for C# in :doc:`Introducing the C# libraries <introduce-libraries-csharp>`.
552554
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.

0 commit comments

Comments
 (0)