提交 c6caafb0 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

fuzzer: much more doc

上级 2a14b570
...@@ -48,29 +48,18 @@ Builds the `mediasoup-worker-test` test unit binary in `Debug` mode at `worker/o ...@@ -48,29 +48,18 @@ Builds the `mediasoup-worker-test` test unit binary in `Debug` mode at `worker/o
### `fuzzer` ### `fuzzer`
Builds the `mediasoup-worker-fuzzer` target (which uses [libFuzzer](http://llvm.org/docs/LibFuzzer.html)) and generates the `worker/out/mediasoup-worker-fuzzer` binary. Builds the `mediasoup-worker-fuzzer` target (which uses [libFuzzer](http://llvm.org/docs/LibFuzzer.html)) and generates the `worker/out/Release/mediasoup-worker-fuzzer` binary.
**NOTE:** Linux is required with `fuzzer` capable `clang++`. `CC` environment variable must point to `clang` and `CXX` to `clang++`. * Linux is required with `fuzzer` capable `clang++`.
* `CC` environment variable must point to `clang`.
* `CXX` environment variable must point to `clang++`.
It's recommended to pass the following fuzzer options: Read the [Fuzzer](Fuzzer.md) documentation to run the fuzzer binary.
* `-artifact_prefix=fuzzer/reports/`: To tell fuzzer store crash reports in the `fuzzer/reports` folder.
* `-max_len=1400`: We don't need much more input size.
Also, there are corpus folders in `fuzzer/corpora`. If used, it's recommended to use `fuzzer/new-corpus` as first directory, so fuzzer generated test inputs are saved in there.
For memory leak detection, set `LSAN_OPTIONS=verbosity=1:log_threads=1` environment variable.
Example:
```bash
$ LSAN_OPTIONS=verbosity=1:log_threads=1 ./out/Release/mediasoup-worker-fuzzer -artifact_prefix=fuzzer/reports/ -max_len=1800 -workers=1 fuzzer/new-corpus fuzzer/corpora/rtcp-corpus
```
### `fuzzer-docker-build` ### `fuzzer-docker-build`
Builds a Linux image with `fuzzer` capable `clang++` that builds the `mediasoup-worker-fuzzer` target. Builds a Linux image with `fuzzer` capable `clang++`.
**NOTE:** Before running this command, a specific version of Linux `clang` must be downloaded. To get it, run: **NOTE:** Before running this command, a specific version of Linux `clang` must be downloaded. To get it, run:
...@@ -82,7 +71,7 @@ $ ./scripts/get-dep.sh clang-fuzzer ...@@ -82,7 +71,7 @@ $ ./scripts/get-dep.sh clang-fuzzer
### `fuzzer-docker-run` ### `fuzzer-docker-run`
Runs a container of the Docker image created with `fuzzer-docker-build`. It automatically executes a `bash` session in `/mediasoup/worker` volume to run `make fuzzer` and `make fuzzer-run`, etc. Runs a container of the Docker image created with `fuzzer-docker-build`. It automatically executes a `bash` session in the `/mediasoup/worker` directory, which is a Docker volume that points to the real `mediasoup/worker` directory (so we can do `make fuzzer`, etc).
### `make xcode` ### `make xcode`
......
# Fuzzer
Once we have built the `mediasoup-worker-fuzzer` in a Linux environment with `fuzzer` capable `clang` (see `make fuzzer` documentation) we can execute the buffer binary at `worker/out/Release/mediasoup-worker-fuzzer`.
*NOTE:* From now on, we assume we are in the `worker` directory.
## Related documentation
* [libFuzzer documentation](http://llvm.org/docs/LibFuzzer.html)
* [libFuzzer Tutorial](https://github.com/google/fuzzer-test-suite/blob/master/tutorial/libFuzzerTutorial.md)
* [webrtcH4cKS ~ Lets get better at fuzzing in 2019](https://webrtchacks.com/lets-get-better-at-fuzzing-in-2019-heres-how/)
* [OSS-Fuzz](https://github.com/google/oss-fuzz) - Continuous fuzzing of open source software ("fuzz for me")
## Corpus files
The `fuzzer/corpora` directory has corpus directories taken from Chromium projects. They should be use to feed fuzzer with appropriate input.
However, given how `libFuzzer` works, the first directory given as command line parameter is not just used for reading corpus files, but also to store newly generated ones. So, it's recommended to pass `fuzzer/new-corpus` as first directory. Such a directory is gitignored (for now).
## Crash reports
When the fuzzer detects an issue it generates a crash report file which contains the bytes given as input. Those files can be individually used later (instead of passing corpus directories) to the fuzzer to verify that the issue has been fixed.
The `fuzzer/reports` directory should be used to store those new crash reports. In order to use it, the following option must be given to the fuzzer executable:
```bash
`-artifact_prefix=fuzzer/reports/`
```
## Others
It's recommended to (also) pass the following options to the fuzzer:
* `-max_len=1400`: We don't need much more input size.
For memory leak detection enable the following environment variable:
* `LSAN_OPTIONS=verbosity=1:log_threads=1`
The `fuzzer/fuzzer.cpp` file reads some custom environment variables to decide which kind of checks perform:
* `MS_FUZZ_STUN=1`: Do STUN checks.
* `MS_FUZZ_RTCP=1`: Do RTCP checks.
* `MS_FUZZ_RTP=1`: Do RTP checks.
* If none of them is given, then **all** checks are enabled.
## Usage examples
* Detect memory leaks and just check STUN:
```bash
$ MS_FUZZ_STUN=1 LSAN_OPTIONS=verbosity=1:log_threads=1 ./out/Release/mediasoup-worker-fuzzer -artifact_prefix=fuzzer/reports/ -max_len=1400 fuzzer/new-corpus fuzzer/corpora/stun-corpus
```
* Detect memory leaks and check everything:
```bash
$ LSAN_OPTIONS=verbosity=1:log_threads=1 ./out/Release/mediasoup-worker-fuzzer -artifact_prefix=fuzzer/reports/ -max_len=1400 fuzzer/new-corpus fuzzer/corpora/stun-corpus fuzzer/corpora/rtcp-corpus fuzzer/corpora/rtp-corpus
```
* Verify that a specific crash is fixed:
```bash
$ LSAN_OPTIONS=verbosity=1:log_threads=1 ./out/Release/mediasoup-worker-fuzzer fuzzer/reports/crash-f39771f7a03c0e7e539d4e52f48f7adad8976404
```
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
Internal documentation for developming purposes. Get the **mediasoup** public documentation at [mediasoup.org](https://mediasoup.org). Internal documentation for developming purposes. Get the **mediasoup** public documentation at [mediasoup.org](https://mediasoup.org).
* [Building](Building.md) * [Building](Building.md)
* [Fuzzer](Fuzzer.md)
* [RTCP](RTCP.md) * [RTCP](RTCP.md)
* [Consumer](Consumer.md) * [Consumer](Consumer.md)
* [Charts](Charts.md) * [Charts](Charts.md)
......
#define MS_CLASS "fuzzer"
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "DepOpenSSL.hpp" #include "DepOpenSSL.hpp"
#include "Utils.hpp" #include "LogLevel.hpp"
#include "Logger.hpp"
#include "Settings.hpp"
#include "RTC/StunMessage.hpp" #include "RTC/StunMessage.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "RTC/RTCP/Packet.hpp" #include "RTC/RTCP/Packet.hpp"
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <iostream> // TODO: Remove. #include <cstdlib> // std::getenv()
#include <iostream>
using namespace RTC; using namespace RTC;
bool fuzzStun = false;
bool fuzzRtcp = false;
bool fuzzRtp = false;
int init() int init()
{ {
// Initialize static stuff. std::string loggerId = "fuzzer";
Settings::configuration.logLevel = LogLevel::LOG_DEBUG;
Logger::Init(loggerId);
DepLibUV::ClassInit(); DepLibUV::ClassInit();
DepOpenSSL::ClassInit(); DepOpenSSL::ClassInit();
Utils::Crypto::ClassInit(); Utils::Crypto::ClassInit();
// Select what to fuzz.
if (std::getenv("MS_FUZZ_STUN") && std::string(std::getenv("MS_FUZZ_STUN")) == "1")
{
std::cout << "[fuzzer] STUN checks enabled" << std::endl;
fuzzStun = true;
}
if (std::getenv("MS_FUZZ_RTCP") && std::string(std::getenv("MS_FUZZ_RTCP")) == "1")
{
std::cout << "[fuzzer] RTCP checks enabled" << std::endl;
fuzzRtcp = true;
}
if (std::getenv("MS_FUZZ_RTP") && std::string(std::getenv("MS_FUZZ_RTP")) == "1")
{
std::cout << "[fuzzer] RTP checks enabled" << std::endl;
fuzzRtp = true;
}
if (!fuzzStun && !fuzzRtcp && !fuzzRtp)
{
std::cout << "[fuzzer] all checks enabled" << std::endl;
fuzzStun = true;
fuzzRtcp = true;
fuzzRtp = true;
}
return 0; return 0;
} }
...@@ -29,24 +69,23 @@ void fuzz(const uint8_t* data, size_t len) ...@@ -29,24 +69,23 @@ void fuzz(const uint8_t* data, size_t len)
// NOTE: This code could be split in different files under a new src/ folder // NOTE: This code could be split in different files under a new src/ folder
// with its corresponding new include/ folder. // with its corresponding new include/ folder.
// Check if it's STUN. if (fuzzStun && StunMessage::IsStun(data, len))
if (StunMessage::IsStun(data, len))
{ {
StunMessage* msg = StunMessage::Parse(data, len); StunMessage* msg = StunMessage::Parse(data, len);
if (msg) if (msg)
delete msg; delete msg;
} }
// Check if it's RTCP.
else if (RTCP::Packet::IsRtcp(data, len)) if (fuzzRtcp && RTCP::Packet::IsRtcp(data, len))
{ {
RTCP::Packet* packet = RTCP::Packet::Parse(data, len); RTCP::Packet* packet = RTCP::Packet::Parse(data, len);
if (packet) if (packet)
delete packet; delete packet;
} }
// Check if it's RTP.
else if (RtpPacket::IsRtp(data, len)) if (fuzzRtp && RtpPacket::IsRtp(data, len))
{ {
RtpPacket* packet = RtpPacket::Parse(data, len); RtpPacket* packet = RtpPacket::Parse(data, len);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论