2022-08-26 08:19:49 +00:00
|
|
|
# Dr. Chaos
|
|
|
|
|
2022-08-28 11:54:19 +00:00
|
|
|
Fuzzing is an automated bug finding technique, where randomized inputs are fed to a target
|
|
|
|
program in order to get it to crash. With fuzzing, you can increase your test coverage to
|
|
|
|
find edge cases and trigger bugs more effectively.
|
|
|
|
|
|
|
|
Dr. Chaos extends the Nim interface to LLVM/Clang libFuzzer, an in-process,
|
|
|
|
coverage-guided, evolutionary fuzzing engine. And adds support for
|
|
|
|
[structured fuzzing](https://github.com/google/fuzzing/blob/master/docs/structure-aware-fuzzing.md).
|
2022-08-28 16:19:57 +00:00
|
|
|
The user should define the input type, as a parameter to the target function and the
|
2022-08-28 11:54:19 +00:00
|
|
|
fuzzer is responsible for providing valid inputs. Behind the scenes it uses value profiling
|
|
|
|
to guide the fuzzer past these comparisons much more efficiently than simply hoping to
|
|
|
|
stumble on the exact sequence of bytes by chance.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
For most cases, it is fairly trivial to define a data type and a target function that
|
|
|
|
performs some operations and checks if the invariants expressed as assert conditions still
|
2022-08-29 18:52:33 +00:00
|
|
|
hold. See [What makes a good fuzz target](https://github.com/google/fuzzing/blob/master/docs/good-fuzz-target.md)
|
2022-08-29 19:12:56 +00:00
|
|
|
for more information. Then call `defaultMutator` with that function as parameter. That fuzz target can be as basic as
|
2022-08-30 06:39:03 +00:00
|
|
|
defining a fixed-size type and ensuring the software under test doesn't crash like:
|
2022-08-28 11:54:19 +00:00
|
|
|
|
2022-08-30 06:39:03 +00:00
|
|
|
```nim
|
|
|
|
import drchaos
|
|
|
|
|
|
|
|
proc fuzzMe(s: string, a, b, c: int32) =
|
2022-08-30 07:02:09 +00:00
|
|
|
# function under test
|
|
|
|
if a == 0xdeadc0de'i32 and b == 0x11111111'i32 and c == 0x22222222'i32:
|
|
|
|
if s.len == 100: doAssert false
|
2022-08-30 06:39:03 +00:00
|
|
|
|
|
|
|
func fuzzTarget(data: (string, int32, int32, int32)) =
|
|
|
|
let (s, a, b, c) = data
|
|
|
|
fuzzMe(s, a, b, c)
|
|
|
|
|
|
|
|
defaultMutator(fuzzTarget)
|
|
|
|
```
|
2022-08-28 11:54:19 +00:00
|
|
|
|
2022-08-30 06:39:03 +00:00
|
|
|
Or complex as shown bellow:
|
2022-08-28 11:54:19 +00:00
|
|
|
|
|
|
|
```nim
|
|
|
|
import drchaos
|
|
|
|
|
|
|
|
type
|
|
|
|
ContentNodeKind = enum
|
|
|
|
P, Br, Text
|
|
|
|
ContentNode = object
|
|
|
|
case kind: ContentNodeKind
|
|
|
|
of P: pChildren: seq[ContentNode]
|
|
|
|
of Br: discard
|
|
|
|
of Text: textStr: string
|
|
|
|
|
|
|
|
func `==`(a, b: ContentNode): bool =
|
|
|
|
if a.kind != b.kind: return false
|
|
|
|
case a.kind
|
|
|
|
of P: return a.pChildren == b.pChildren
|
|
|
|
of Br: return true
|
|
|
|
of Text: return a.textStr == b.textStr
|
|
|
|
|
|
|
|
func fuzzTarget(x: ContentNode) =
|
|
|
|
# Convert or translate `x` to any format (JSON, HMTL, binary, etc...)
|
|
|
|
# and feed it to the API you are testing.
|
|
|
|
|
|
|
|
defaultMutator(fuzzTarget)
|
|
|
|
```
|
|
|
|
|
|
|
|
Dr. Chaos will generate millions of inputs and run `fuzzTarget` under a few seconds.
|
|
|
|
More articulate examples, such as fuzzing a graph library are in the `examples/` directory.
|
|
|
|
|
2022-08-29 18:52:33 +00:00
|
|
|
Defining a `==` proc for the input type is necessary.
|
2022-08-28 11:54:19 +00:00
|
|
|
|
|
|
|
### Post-processors
|
|
|
|
|
|
|
|
Sometimes it is necessary to adjust the random input in order to add magic values or
|
|
|
|
dependencies between some fields. This is supported with a post-processing step, which for
|
|
|
|
performance and clarity reasons only runs on compound types such as
|
|
|
|
object/tuple/ref/seq/string/array/set and by exception distinct types.
|
|
|
|
|
|
|
|
```nim
|
|
|
|
proc postProcess(x: var ContentNode; r: var Rand) =
|
|
|
|
if x.kind == Text:
|
|
|
|
x.textStr = "The man the professor the student has studies Rome."
|
|
|
|
```
|
|
|
|
|
|
|
|
### Custom mutator
|
|
|
|
|
|
|
|
Besides `defaultMutator` there is also `customMutator` which allows more fine-grained
|
|
|
|
control of the mutation procedure, like uncompressing a `seq[byte]` then calling
|
|
|
|
`runMutator` on the raw data and compressing the output again.
|
|
|
|
|
|
|
|
```nim
|
2022-08-28 16:19:57 +00:00
|
|
|
func myTarget(x: seq[byte]) =
|
|
|
|
var data = uncompress(x)
|
|
|
|
...
|
|
|
|
|
2022-08-28 11:54:19 +00:00
|
|
|
proc myMutator(x: var seq[byte]; sizeIncreaseHint: Natural; r: var Rand) =
|
2022-08-28 16:19:57 +00:00
|
|
|
var data = uncompress(x)
|
|
|
|
runMutator(data, sizeIncreaseHint, r)
|
|
|
|
x = compress(data)
|
|
|
|
|
|
|
|
customMutator(myTarget, myMutator)
|
2022-08-28 11:54:19 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### User-defined mutate procs
|
|
|
|
|
|
|
|
It's possible to use distinct types to provide a mutate overload for fields that have
|
|
|
|
interesting values, like file signatures or to limit the search space.
|
|
|
|
|
|
|
|
```nim
|
|
|
|
# Fuzzed library
|
|
|
|
when defined(runFuzzTests):
|
|
|
|
type
|
|
|
|
ClientId = distinct int
|
2022-08-28 19:17:52 +00:00
|
|
|
|
|
|
|
proc `==`(a, b: ClientId): bool {.borrow.}
|
2022-08-28 11:54:19 +00:00
|
|
|
else:
|
|
|
|
type
|
|
|
|
ClientId = int
|
|
|
|
|
|
|
|
# In a test file
|
|
|
|
import drchaos/mutator
|
|
|
|
|
|
|
|
const
|
2022-08-28 16:19:57 +00:00
|
|
|
idA = 0.ClientId
|
|
|
|
idB = 2.ClientId
|
|
|
|
idC = 4.ClientId
|
2022-08-28 11:54:19 +00:00
|
|
|
|
|
|
|
proc mutate(value: var ClientId; sizeIncreaseHint: int; enforceChanges: bool; r: var Rand) =
|
|
|
|
# use `rand()` to return a new value.
|
2022-08-28 16:19:57 +00:00
|
|
|
repeatMutate(r.sample([idA, idB, idC]))
|
2022-08-28 11:54:19 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
For aiding the creation of mutate functions, mutators for every supported type are
|
|
|
|
exported by `drchaos/mutator`.
|
|
|
|
|
|
|
|
## What's not supported
|
|
|
|
|
|
|
|
- Polymorphic types, missing serialization support.
|
|
|
|
- References with cycles. A `.noFuzz` custom pragma will be added soon for cursors.
|
2022-08-30 06:39:03 +00:00
|
|
|
- Object variants work only with the lastest memory management model `--mm:arc/orc`.
|
2022-08-28 11:54:19 +00:00
|
|
|
|
2022-08-29 17:24:00 +00:00
|
|
|
## Why choose Dr. Chaos
|
|
|
|
|
2022-08-29 19:04:49 +00:00
|
|
|
Dr. Chaos has several advantages over frameworks derived from
|
2022-08-29 17:24:00 +00:00
|
|
|
[FuzzDataProvider](https://github.com/google/fuzzing/blob/master/docs/split-inputs.md)
|
2022-08-29 19:04:49 +00:00
|
|
|
which struggle with dynamic types that in particular are nested. For a better explanation
|
|
|
|
read an article written by the author of
|
|
|
|
[Fuzzcheck](https://github.com/loiclec/fuzzcheck-rs/blob/main/articles/why_not_bytes.md).
|
2022-08-29 17:24:00 +00:00
|
|
|
|
2022-09-04 09:04:20 +00:00
|
|
|
## Bugs found with help of the library
|
|
|
|
|
|
|
|
### Nim reference implementation
|
|
|
|
|
|
|
|
* [use-after-free bugs in object variants](https://github.com/nim-lang/Nim/issues/20305)
|
|
|
|
* [openArray on empty seq triggers UB](openArray on empty seq triggers UB)
|
|
|
|
|
2022-08-26 08:19:49 +00:00
|
|
|
## License
|
|
|
|
|
|
|
|
Licensed and distributed under either of
|
|
|
|
|
|
|
|
* MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
* Apache License, Version 2.0, ([LICENSE-APACHEv2](LICENSE-APACHEv2) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
|
|
|
|
at your option. These files may not be copied, modified, or distributed except according to those terms.
|