-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
257 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,121 @@ | ||
# Fuzz Testing Application | ||
This repository provides a fuzz testing application called **cc_mqtt5_client_afl_fuzz**. | ||
It expects to receive a sequence of the raw bytes from the **STDIN** and intended to | ||
be used with fuzz testing tools like [AFL++](https://github.com/AFLplusplus/AFLplusplus). | ||
|
||
The **cc_mqtt5_client_afl_fuzz** application will try to perform the following steps: | ||
|
||
1. Initialize the library. | ||
2. Perform "connect" operation and wait for acknowledgement (**CONNACK**). | ||
3. Subscribe to all topics provided via command line arguments. | ||
4. Perform re-authentication if "authentication method" is provided via command line arguments. | ||
5. Echo back all the received messages from the broker, and proceed to the next step only after | ||
the necessary number of messages has been published (configured via command line). | ||
6. Unsubscribe from all the previously subscribed topics | ||
7. Gracefully disconnect from the broker | ||
8. Return to step 1. | ||
|
||
Due to the nature of the fuzz testing the intended workflow from above won't be | ||
completed as intended due to detected malformed packets, protocol errors, and/or | ||
received **DISCONNECT** messages. When such scenario is detected in any of the | ||
steps the application will return to step 1 to try from the beginning on the remaining | ||
input. | ||
|
||
# How to Build | ||
To better understand the build instructions below please read the | ||
[BUILD.md](BUILD.md) document as well as refer to the | ||
main [CMakeLists.txt](../CMakeLists.txt) file for the info on available configuration options and | ||
variables. | ||
|
||
If you're not familiar with the [AFL++](https://github.com/AFLplusplus/AFLplusplus) it | ||
is highly recommended to read through its [instructions](https://github.com/AFLplusplus/AFLplusplus/blob/stable/docs/fuzzing_in_depth.md) | ||
to properly understand what is being done. | ||
|
||
To enable the fuzz testing application use **CC_MQTT5_CLIENT_AFL_FUZZ** cmake option. When | ||
instrumenting the binaries for the fuzz testing other applications are probably not needed | ||
and their build can be disabled using the **CC_MQTT5_CLIENT_APPS** cmake option. | ||
|
||
Also remember to properly set instrumenting compilers provided by the [AFL++](https://github.com/AFLplusplus/AFLplusplus). | ||
|
||
``` | ||
CC=afl-clang-lto CXX=afl-clang-lto++ cmake /path/to/src -DCC_MQTT5_CLIENT_AFL_FUZZ=ON -DCC_MQTT5_CLIENT_APPS=OFF ... | ||
``` | ||
|
||
It is also highly recommended to use "Debug" build to enable all the assertions. | ||
``` | ||
CC=afl-clang-lto CXX=afl-clang-lto++ cmake ... -DCMAKE_BUILD_TYPE=Debug ... | ||
``` | ||
|
||
After the "Debug" build is tested for several days without showing any crashes it also | ||
beneficial to rebuild the fuzzing application as a "Release" and re-run it on the same produced corpus | ||
(passing "-i-" to afl-fuzz) to verify that there are no unexpected pitfalls for the "Release" version. | ||
|
||
Due to the fact that [AFL++](https://github.com/AFLplusplus/AFLplusplus) compilers | ||
receive their configuration via environment variable before the build it is necessary | ||
to disable "ccache" usage in case the sources are going to be built multiple times | ||
with different configurations. | ||
|
||
``` | ||
CC=afl-clang-lto CXX=afl-clang-lto++ cmake ... -DCC_MQTT5_USE_CCACHE=OFF ... | ||
``` | ||
|
||
Enable required sanitizers before the build | ||
``` | ||
export AFL_USE_ASAN=1 | ||
export AFL_USE_UBSAN=1 | ||
``` | ||
|
||
**WARNING**: It has been noticed when too many sanitizers are enabled at the same time the | ||
target either fails to compile or fails to produce proper output | ||
("Illegal instruction" is reported) when some failure happens. | ||
|
||
As the final stage, build the fuzzing application | ||
``` | ||
cmake --build . --target install | ||
``` | ||
|
||
Note that in case [custom](custom_client_build.md) client libraries are built, the | ||
fuzzing application will be built for each such library and the application name will | ||
reflect the custom name selected for the library. | ||
|
||
The typical build sequence may look like this: | ||
``` | ||
cd /path/to/cc.mqtt5.libs | ||
mkdir build | ||
cd build | ||
CC=afl-clang-lto CXX=afl-clang-lto++ cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=install \ | ||
-DCMAKE_PREFIX_PATH=/path/to/comms/install\;/path/to/cc.mqtt5.generated/install \ | ||
-DCC_MQTT5_CLIENT_AFL_FUZZ=ON -DCC_MQTT5_CLIENT_APPS=OFF -DCC_MQTT5_USE_CCACHE=OFF | ||
export AFL_USE_ASAN=1 | ||
export AFL_USE_UBSAN=1 | ||
cmake --build . --target install --parallel 8 | ||
``` | ||
|
||
# How to Fuzz Test | ||
In order to start fuzz testing the [AFL++](https://github.com/AFLplusplus/AFLplusplus) requires | ||
creation of the input corpus. Please use "-h" option to list the available command line parameters: | ||
``` | ||
./install/bin/cc_mqtt5_client_afl_fuzz -h | ||
``` | ||
Note the presence of the "-g" option which can be used to generate a valid input sequence | ||
to perform a full single iteration of the **cc_mqtt5_client_afl_fuzz** described | ||
earlier. If you intend to use some extra command line arguments in the actual | ||
fuzz testing, provide them when generating the input sequence as well. | ||
|
||
For example | ||
``` | ||
mkdir -p /path/to/fuzz/input | ||
mkdir -p /path/to/fuzz/output | ||
./install/bin/cc_mqtt5_client_afl_fuzz -g /path/to/fuzz/input/1.bin --auth-method myauth --min-pub-count 5 | ||
``` | ||
|
||
Now when the input corpus is created it is possible to actually start fuzz testing. Use the | ||
same command line option as ones used for the generation of the input (excluding the "-g" with parameter of course). | ||
``` | ||
afl-fuzz -i /path/to/fuzz/input -o /path/to/fuzz/output -- ./install/bin/cc_mqtt5_client_afl_fuzz --auth-method myauth --min-pub-count 5 | ||
``` | ||
|
||
Note that "afl-fuzz" may request to change your machine configuration before being able to fuzz test. | ||
|
||
In case fuzz testing reports any crash please open an issue for this project reporting | ||
a build configuration and attaching the input file(s) that caused the crash. |