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/codeql/codeql-language-guides/customizing-library-models-for-cpp.rst
+33-66Lines changed: 33 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
-
.. _customizing-library-models-for-csharp:
1
+
.. _customizing-library-models-for-cpp:
2
2
3
-
Customizing library models for C#
4
-
=================================
3
+
Customizing library models for C and C++
4
+
========================================
5
5
6
6
You can model the methods and callables that control data flow in any framework or library. This is especially useful for custom frameworks or niche libraries, that are not supported by the standard CodeQL libraries.
7
7
@@ -10,28 +10,28 @@ You can model the methods and callables that control data flow in any framework
10
10
About this article
11
11
------------------
12
12
13
-
This article contains reference material about how to define custom models for sources, sinks, and flow summaries for C# dependencies in data extension files.
13
+
This article contains reference material about how to define custom models for sources, sinks, and flow summaries for C and C++ dependencies in data extension files.
14
14
15
15
About data extensions
16
16
---------------------
17
17
18
-
You can customize analysis by defining models (summaries, sinks, and sources) of your code's C#/.NET dependencies in data extension files. Each model defines the behavior of one or more elements of your library or framework, such as methods, properties, and callables. When you run dataflow analysis, these models expand the potential sources and sinks tracked by dataflow analysis and improve the precision of results.
18
+
You can customize analysis by defining models (summaries, sinks, and sources) of your code's C and C++ dependencies in data extension files. Each model defines the behavior of one or more elements of your library or framework, such as callables. When you run dataflow analysis, these models expand the potential sources and sinks tracked by dataflow analysis and improve the precision of results.
19
19
20
-
Most of the security queries search for paths from a source of untrusted input to a sink that represents a vulnerability. This is known as taint tracking. Each source is a starting point for dataflow analysis to track tainted data and each sink is an end point.
20
+
Many of the security queries search for paths from a source of untrusted input to a sink that represents a vulnerability. This is known as taint tracking. Each source is a starting point for dataflow analysis to track tainted data and each sink is an end point.
21
21
22
22
Taint tracking queries also need to know how data can flow through elements that are not included in the source code. These are modeled as summaries. A summary model enables queries to synthesize the flow behavior through elements in dependency code that is not stored in your repository.
23
23
24
24
Syntax used to define an element in an extension file
Each model of an element is defined using a data extension where each tuple constitutes a model.
28
-
A data extension file to extend the standard C# queries included with CodeQL is a YAML file with the form:
28
+
A data extension file to extend the standard CPP queries included with CodeQL is a YAML file with the form:
29
29
30
30
.. code-block:: yaml
31
31
32
32
extensions:
33
33
- addsTo:
34
-
pack: codeql/csharp-all
34
+
pack: codeql/cpp-all
35
35
extensible: <name of extensible predicate>
36
36
data:
37
37
- <tuple1>
@@ -50,30 +50,31 @@ Publish data extension files in a CodeQL model pack to share
50
50
51
51
You can group one or more data extension files into a CodeQL model pack and publish it to the GitHub Container Registry. This makes it easy for anyone to download the model pack and use it to extend their analysis. For more information, see `Creating a CodeQL model pack <https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/creating-and-working-with-codeql-packs#creating-a-codeql-model-pack>`__ and `Publishing and using CodeQL packs <https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/publishing-and-using-codeql-packs/>`__ in the CodeQL CLI documentation.
52
52
53
-
Extensible predicates used to create custom models in C#
The CodeQL library for C# analysis exposes the following extensible predicates:
56
+
The CodeQL library for CPP analysis exposes the following extensible predicates:
57
57
58
-
- ``sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance)``. This is used to model sources of potentially tainted data. The ``kind`` of the sources defined using this predicate determine which threat model they are associated with. Different threat models can be used to customize the sources used in an analysis. For more information, see ":ref:`Threat models <threat-models-csharp>`."
58
+
- ``sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance)``. This is used to model sources of potentially tainted data. The ``kind`` of the sources defined using this predicate determine which threat model they are associated with. Different threat models can be used to customize the sources used in an analysis. For more information, see ":ref:`Threat models <threat-models-cpp>`."
59
59
- ``sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, provenance)``. This is used to model sinks where tainted data may be used in a way that makes the code vulnerable.
60
60
- ``summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, provenance)``. This is used to model flow through elements.
61
-
- ``neutralModel(namespace, type, name, signature, kind, provenance)``. This is similar to a summary model but used to model the flow of values that have only a minor impact on the dataflow analysis. Manual neutral models (those with a provenance such as ``manual`` or ``ai-manual``) can be used to override generated summary models (those with a provenance such as ``df-generated``), so that the summary model will be ignored. Other than that, neutral models have no effect.
62
61
63
62
The extensible predicates are populated using the models defined in data extension files.
64
63
65
64
Examples of custom model definitions
66
65
------------------------------------
67
66
68
-
The examples in this section are taken from the standard CodeQL C# query pack published by GitHub. They demonstrate how to add tuples to extend extensible predicates that are used by the standard queries.
67
+
TODO: one good example might do, but we currently have zero.
68
+
69
+
The examples in this section are taken from the standard CodeQL CPP query pack published by GitHub. They demonstrate how to add tuples to extend extensible predicates that are used by the standard queries.
69
70
70
71
Example: Taint sink in the ``System.Data.SqlClient`` namespace
@@ -144,15 +145,15 @@ The sixth value should be left empty and is out of scope for this documentation.
144
145
The remaining values are used to define the ``access path``, the ``kind``, and the ``provenance`` (origin) of the source.
145
146
146
147
- The seventh value ``ReturnValue`` is the access path to the return of the method, which means that it is the return value that should be considered a source of tainted input.
147
-
- The eighth value ``remote`` is the kind of the source. The source kind is used to define the threat model where the source is in scope. ``remote`` applies to many of the security related queries as it means a remote source of untrusted data. As an example the SQL injection query uses ``remote`` sources. For more information, see ":ref:`Threat models <threat-models-csharp>`."
148
+
- The eighth value ``remote`` is the kind of the source. The source kind is used to define the threat model where the source is in scope. ``remote`` applies to many of the security related queries as it means a remote source of untrusted data. As an example the SQL injection query uses ``remote`` sources. For more information, see ":ref:`Threat models <threat-models-cpp>`."
148
149
- The ninth value ``manual`` is the provenance of the source, which is used to identify the origin of the source.
149
150
150
151
Example: Add flow through the ``Concat`` method
151
152
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152
-
This example shows how the C# query pack models flow through a method for a simple case.
153
+
This example shows how the CPP query pack models flow through a method for a simple case.
153
154
This pattern covers many of the cases where we need to summarize flow through a method that is stored in a library or framework outside the repository.
154
155
155
-
.. code-block:: csharp
156
+
.. code-block:: cpp TODO
156
157
157
158
public static void TaintFlow(string s1, string s2) {
158
159
string t = String.Concat(s1, s2); // There is taint flow from s1 and s2 to t.
@@ -165,7 +166,7 @@ We need to add tuples to the ``summaryModel``\(namespace, type, subtypes, name,
@@ -307,41 +308,7 @@ For the remaining values for both rows:
307
308
308
309
That is, the first row specifies that values can flow from the elements of the qualifier enumerable into the first argument of the function provided to ``Select``. The second row specifies that values can flow from the return value of the function to the elements of the enumerable returned from ``Select``.
309
310
310
-
Example: Add a ``neutral`` method
311
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312
-
This example shows how we can model a method as being neutral with respect to flow. We will also cover how to model a property by modeling the getter of the ``Now`` property of the ``DateTime`` class as neutral.
313
-
A neutral model is used to define that there is no flow through a method.
314
-
315
-
.. code-block:: csharp
316
-
317
-
publicstaticvoidTaintFlow() {
318
-
System.DateTimet=System.DateTime.Now; // There is no flow from Now to t.
319
-
...
320
-
}
321
-
322
-
We need to add a tuple to the ``neutralModel``\(namespace, type, name, signature, kind, provenance) extensible predicate by updating a data extension file.
Since we are adding a neutral model, we need to add tuples to the ``neutralModel`` extensible predicate.
335
-
The first four values identify the callable (in this case the getter of the ``Now`` property) to be modeled as a neutral, the fifth value is the kind, and the sixth value is the provenance (origin) of the neutral.
336
-
337
-
- The first value ``System`` is the namespace name.
338
-
- The second value ``DateTime`` is the class (type) name.
339
-
- The third value ``get_Now`` is the method name. Getter and setter methods are named ``get_<name>`` and ``set_<name>`` respectively.
340
-
- The fourth value ``()`` is the method input type signature.
341
-
- The fifth value ``summary`` is the kind of the neutral.
342
-
- The sixth value ``manual`` is the provenance of the neutral.
0 commit comments