-
Notifications
You must be signed in to change notification settings - Fork 1.7k
QL training: C++ variant analysis slide deck #1966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lcartey
wants to merge
2
commits into
github:main
Choose a base branch
from
lcartey:training/cpp-variant-analysis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
docs/language/ql-training/cpp/variant-analysis-tips.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
================================ | ||
Variant analysis tips and tricks | ||
================================ | ||
|
||
C/C++ | ||
|
||
.. container:: semmle-logo | ||
|
||
Semmle :sup:`TM` | ||
|
||
.. rst-class:: agenda | ||
|
||
Agenda | ||
====== | ||
|
||
- Query development process | ||
- Sources of query ideas | ||
- Evaluating query ideas | ||
- Iterative query development | ||
- Useful libraries for C/C++ | ||
- Range analysis | ||
- Guards | ||
|
||
.. rst-class:: background2 | ||
|
||
Query development process | ||
========================== | ||
|
||
Sources of ideas | ||
================ | ||
|
||
- Fix commits in revision history | ||
- Issue reports | ||
- Newly discovered kind of vulnerability | ||
- Transfer from other languages/frameworks | ||
- Chance discovery while working on another query | ||
- Follow Twitter, Hacker News, Snyk, etc. | ||
|
||
Evaluating a query idea | ||
======================= | ||
|
||
- It should be *specific* e.g. not "find all buffer overflows". | ||
- ...but not too specific, only worthwhile if there are some variants to find! | ||
- There should be a concrete test case exhibiting the problem. | ||
|
||
Iterative query development | ||
=========================== | ||
|
||
- Start with a very specific query that just finds the original bug/test case | ||
- Generalize to get at the essence of the problem | ||
- This also helps forming a better understanding of the problem: “thinking in QL” | ||
- As you generalize, vet new results and refine the query to exclude false positives. | ||
- Ideally, the query should not flag the fixed version (if any). | ||
|
||
.. rst-class:: background2 | ||
|
||
Useful libraries for C/C++ | ||
========================== | ||
|
||
Range analysis | ||
============== | ||
|
||
*Range analysis* is the process of determining upper and lower bounds for arithmetic values in the program. | ||
|
||
.. literalinclude:: ../query-examples/cpp/variant-analysis/range-analysis/test.c | ||
:language: cpp | ||
|
||
This is typically useful for determining underflow, overflow or out of bounds reads/writes. | ||
|
||
Simple range analysis | ||
===================== | ||
|
||
QL comes with the ``SimpleRangeAnalysis`` library, which can be used to determine, where possible, *fixed bounds* for | ||
an arithmetic expression. For example: | ||
|
||
.. literalinclude:: ../query-examples/cpp/variant-analysis/range-analysis/SimpleIndexOutOfBounds.ql | ||
:language: ql | ||
|
||
Would find the ``val[x]`` access from the previous slide. | ||
|
||
Simple range analysis: API | ||
========================== | ||
|
||
- ``lowerBound`` and ``upperBound`` predicates provide lower and upper bounds on expressions, where possible. | ||
- ``exprWithEmptyRange`` predicate identifies expressions which have non-overlapping upper and lower bounds, indicating the expression is dead code. | ||
- Helper predicates of the form ``..MightOverflow..`` are provided for reasoning about overflow. | ||
|
||
Simple range analysis: restrictions | ||
=================================== | ||
|
||
The library only supports *constant* bounds. | ||
|
||
e.g. | ||
|
||
.. code-block:: cpp | ||
|
||
if (x >= 1) { | ||
val[x]; // lowerBound(x) = 1 | ||
} | ||
if (x >= y) { | ||
val[x]; // no lowerBound(x) | ||
} | ||
|
||
In particular, we do not deduce that ``lowerBound(x) = y``. Integer values only! | ||
|
||
Simple range analysis: notes | ||
============================ | ||
|
||
- Often used to *exclude* known safe cases e.g. a fixed size array where the index upperBound is known. | ||
- Ranges for variables modified in loops may be over approximated (see QL doc for details). | ||
- ``lowerBound(expr)`` reports the bounds *before* conversion. For post conversion, try ``lowerBound(expr.getFullyConverted())``. | ||
|
||
Guards | ||
====== | ||
|
||
A *guard* is a condition which controls whether a certain part of the program is executed. For example: | ||
|
||
.. literalinclude:: ../query-examples/cpp/variant-analysis/guards/test.c | ||
:language: cpp | ||
|
||
This is typically useful for determining whether a certain necessary check has occurred before a potentially unsafe operation. | ||
|
||
Guards library | ||
============== | ||
|
||
QL comes with the ``Guards`` library, which can be used to determine which ``BasicBlocks`` are guarded by certain conditions. For example: | ||
|
||
.. literalinclude:: ../query-examples/cpp/variant-analysis/guards/GuardCondition.ql | ||
:language: ql | ||
|
||
Would report the last two ``val[x]`` accesses from the previous slide. | ||
|
||
Guards: API | ||
=========== | ||
|
||
- ``GuardCondition`` represents an expression in the program that is a condition. | ||
- The ``GuardCondition.controls(BasicBlock, boolean)`` predicate represents the set of basic blocks controlled by each guard | ||
condition, and includes whether they are controlled in the true case or false case. | ||
2 changes: 2 additions & 0 deletions
2
docs/language/ql-training/query-examples/cpp/variant-analysis/guards/GuardCondition.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.c:6:9:6:14 | access to array | | ||
| test.c:8:7:8:12 | access to array | |
6 changes: 6 additions & 0 deletions
6
docs/language/ql-training/query-examples/cpp/variant-analysis/guards/GuardCondition.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import cpp | ||
import semmle.code.cpp.controlflow.Guards | ||
|
||
from ArrayExpr ae | ||
where not exists(GuardCondition gc | gc.controls(ae.getBasicBlock(), true)) | ||
select ae |
9 changes: 9 additions & 0 deletions
9
docs/language/ql-training/query-examples/cpp/variant-analysis/guards/test.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
int val[16]; | ||
void f(int x) { | ||
if (x > 0) { // `x > 0` is a guard condition | ||
val[x]; // guarded by `x > 0`, true case | ||
} else { | ||
val[x]; // guarded by `x > 0`, false case | ||
} | ||
val[x]; // not guarded | ||
} |
1 change: 1 addition & 0 deletions
1
...aining/query-examples/cpp/variant-analysis/range-analysis/SimpleIndexOutOfBounds.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
| test.c:6:7:6:12 | access to array | |
6 changes: 6 additions & 0 deletions
6
.../ql-training/query-examples/cpp/variant-analysis/range-analysis/SimpleIndexOutOfBounds.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import cpp | ||
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis | ||
|
||
from ArrayExpr ae | ||
where lowerBound(ae.getArrayOffset()) < 0 | ||
select ae |
7 changes: 7 additions & 0 deletions
7
docs/language/ql-training/query-examples/cpp/variant-analysis/range-analysis/test.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
int val[16]; | ||
void f(int x) { | ||
if (x < -1 || x > 15) { | ||
return; | ||
} | ||
val[x]; // What possible range of values can x hold here? | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might also mention
ensuresEq
andensuresLt
here.