You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/ql-training-rst/cpp/bad-overflow-guard.rst
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,11 @@ For this example you should download:
24
24
25
25
You can query the project in `the query console <https://lgtm.com/query/project:2034240708/lang:cpp/>`__ on LGTM.com.
26
26
27
-
Note that results generated in the query console are likely to differ to those generated in the QL plugin as LGTM.com analyzes the most recent revisions of each project that has been added–the snapshot available to download above is based on an historical version of the code base.
27
+
.. insert snapshot-note.rst to explain differences between snapshot available to download and the version available in the query console.
Copy file name to clipboardExpand all lines: docs/language/ql-training-rst/cpp/control-flow-cpp.rst
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,11 @@ For this example you should download:
26
26
27
27
You can query the project in `the query console <https://lgtm.com/query/project:2034240708/lang:cpp/>`__ on LGTM.com.
28
28
29
-
Note that results generated in the query console are likely to differ to those generated in the QL plugin as LGTM.com analyzes the most recent revisions of each project that has been added–the snapshot available to download above is based on an historical version of the code base.
29
+
.. insert snapshot-note.rst to explain differences between snapshot available to download and the version available in the query console.
Copy file name to clipboardExpand all lines: docs/language/ql-training-rst/cpp/data-flow-cpp.rst
+9-156Lines changed: 9 additions & 156 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,11 @@ For this example you should download:
24
24
25
25
You can query the project in `the query console <https://lgtm.com/query/projects:1505958977333/lang:cpp/>`__ on LGTM.com.
26
26
27
-
Note that results generated in the query console are likely to differ to those generated in the QL plugin as LGTM.com analyzes the most recent revisions of each project that has been added–the snapshot available to download above is based on an historical version of the code base.
27
+
.. insert snapshot-note.rst to explain differences between snapshot available to download and the version available in the query console.
28
+
29
+
.. include:: ../slide-snippets/snapshot-note.rst
30
+
31
+
.. resume slides
28
32
29
33
.. rst-class:: agenda
30
34
@@ -114,162 +118,11 @@ We need something better.
114
118
115
119
What we need is a way to determine whether the format argument is ever set to something that is not constant.
116
120
117
-
Data flow analysis
118
-
==================
119
-
120
-
- Models flow of data through the program.
121
-
- Implemented in the module ``semmle.code.cpp.dataflow.DataFlow``.
122
-
- Class ``DataFlow::Node`` represents program elements that have a value, such as expressions and function parameters.
123
-
124
-
- Nodes of the data flow graph.
125
-
126
-
- Various predicated represent flow between these nodes.
127
-
128
-
- Edges of the data flow graph.
129
-
130
-
.. note::
131
-
132
-
The solution here is to use *data flow*. Data flow is, as the name suggests, about tracking the flow of data through the program. It helps answers questions like: *does this expression ever hold a value that originates from a particular other place in the program*?
133
-
134
-
We can visualize the data flow problem as one of finding paths through a directed graph, where the nodes of the graph are elements in the program, and the edges represent the flow of data between those elements. If a path exists, then the data flows between those two edges.
a [label=<tainted<BR /><FONT POINT-SIZE="10">ParameterNode</FONT>>]
166
-
b [label=<tainted<BR /><FONT POINT-SIZE="10">ExprNode</FONT>>]
167
-
c [label=<x<BR /><FONT POINT-SIZE="10">ExprNode</FONT>>]
168
-
d [label=<x<BR /><FONT POINT-SIZE="10">ExprNode</FONT>>]
169
-
e [label=<y<BR /><FONT POINT-SIZE="10">ExprNode</FONT>>]
170
-
171
-
a -> b
172
-
b -> {c, d}
173
-
c -> e
174
-
175
-
}
176
-
177
-
Local vs global data flow
178
-
=========================
179
-
180
-
- Local (“intra-procedural”) data flow models flow within one function; feasible to compute for all functions in a snapshot
181
-
- Global (“inter-procedural”) data flow models flow across function calls; not feasible to compute for all functions in a snapshot
182
-
- Different APIs, so discussed separately
183
-
- This slide deck focuses on the former.
184
-
185
-
.. note::
186
-
187
-
For further information, see:
188
-
189
-
- `Introduction to data flow analysis in QL <https://help.semmle.com/QL/learn-ql/ql/intro-to-data-flow.html>`__
190
-
- `Analyzing data flow in C/C++ <https://help.semmle.com/QL/learn-ql/ql/cpp/dataflow.html>`__
191
-
192
-
.. rst-class:: background2
193
-
194
-
Local data flow
195
-
===============
196
-
197
-
Importing data flow
198
-
===================
199
-
200
-
To use the data flow library, add the following import:
201
-
202
-
.. code-block:: ql
203
-
204
-
import semmle.code.cpp.dataflow.DataFlow
205
-
206
-
**Note**: this library contains an explicit “module” declaration:
207
-
208
-
.. code-block:: ql
209
-
210
-
module DataFlow {
211
-
class Node extends ... { ... }
212
-
predicate localFlow(Node source, Node sink) {
213
-
localFlowStep*(source, sink)
214
-
}
215
-
...
216
-
}
217
-
218
-
So all references will need to be qualified (that is, ``DataFlow::Node``)
219
-
220
-
.. note::
221
-
222
-
A **query library** is file with the extension ``.qll``. Query libraries do not contain a query clause, but may contain modules, classes, and predicates. For example, the `C/C++ data flow library <https://help.semmle.com/qldoc/cpp/semmle/code/cpp/dataflow/DataFlow.qll/module.DataFlow.html>`__ is contained in the ``semmle/code/cpp/dataflow/DataFlow.qll`` QLL file, and can be imported as shown above.
223
-
224
-
A **module** is a way of organizing QL code by grouping together related predicates, classes, and (sub-)modules. They can be either explicitly declared or implicit. A query library implicitly declares a module with the same name as the QLL file.
225
-
226
-
For further information on libraries and modules in QL, see the chapter on `Modules <https://help.semmle.com/QL/ql-handbook/modules.html>`__ in the QL language handbook.
227
-
228
-
For further information on importing QL libraries and modules, see the chapter on `Name resolution <https://help.semmle.com/QL/ql-handbook/name-resolution.html>`__ in the QL language handbook.
229
-
230
-
Data flow graph
231
-
===============
232
-
233
-
- Class ``DataFlow::Node`` represents data flow graph nodes
234
-
- Predicate ``DataFlow::localFlowStep`` represents local data flow graph edges, ``DataFlow::localFlow`` is its transitive closure
235
-
- Data flow graph nodes are *not* AST nodes, but they correspond to AST nodes, and there are predicates for mapping between them:
The ``DataFlow::Node`` class is shared between both the local and global data flow graphs–the primary difference is the edges, which in the “global” case can link different functions.
246
-
247
-
``localFlowStep`` is the “single step” flow relation–that is, it describes single edges in the local data flow graph. ``localFlow`` represents the `transitive <https://help.semmle.com/QL/ql-handbook/recursion.html#transitive-closures>`__ closure of this relation–in other words, it contains every pair of nodes where the second node is reachable from the first in the data flow graph.
248
-
249
-
The data flow graph is separate from the `AST <https://en.wikipedia.org/wiki/Abstract_syntax_tree>`__, to allow for flexibility in how data flow is modeled. There are a small number of data flow node types–expression nodes, parameter nodes, uninitialized variable nodes, and definition by reference nodes. Each node provides mapping functions to and from the relevant AST (for example ``Expr``, ``Parameter`` etc.) or symbol table (for example ``Variable``) classes.
250
-
251
-
Taint tracking
252
-
==============
253
-
254
-
- Usually, we want to generalise slightly by not only considering plain data flow, but also “taint” propagation, that is, whether a value is influenced by or derived from another.
255
-
256
-
- Examples:
257
-
258
-
.. code-block:: cpp
259
-
260
-
sink = source; // source -> sink: data and taint
261
-
strcat(sink, source); // source -> sink: taint, not data
262
-
263
-
- Library ``semmle.code.cpp.dataflow.TaintTracking`` provides predicates for tracking taint:
264
-
265
-
- ``TaintTracking::localTaintStep`` represents one (local) taint step
266
-
- ``TaintTracking::localTaint`` is its transitive closure.
267
-
268
-
.. note::
121
+
.. include general data flow slides
269
122
270
-
Taint tracking can be thought of as another type of data flow graph. It usually extends the standard dataflow graph for a problem by adding edges between nodes where one one node influences or *taints* another.
The `API <https://help.semmle.com/qldoc/cpp/semmle/code/cpp/dataflow/TaintTracking.qll/module.TaintTracking.html>`__ is almost identical to that of the local data flow. All we need to do to switch to taint tracking is ``import semmle.code.cpp.dataflow.TaintTracking`` instead of ``semmle.code.cpp.dataflow.DataFlow``, and instead of using ``localFlow``, we use ``localTaint``.
125
+
.. resume language-specific slides
273
126
274
127
Exercise: source nodes
275
128
======================
@@ -343,4 +196,4 @@ Beyond local data flow
343
196
- Results are still underwhelming.
344
197
- Dealing with parameter passing becomes cumbersome.
345
198
- Instead, let’s turn the problem around and find user-controlled data that flows into a ``printf`` format argument, potentially through calls.
346
-
- This needs :doc:`global data flow <global-data-flow-cpp>`.
199
+
- This needs :doc:`global data flow <global-data-flow-cpp>`.
0 commit comments