Upgrade github.com/AdaLogics/go-fuzz-headers

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato
2022-07-06 14:19:38 +00:00
parent c4b1b368ad
commit b316318596
15 changed files with 1331 additions and 59 deletions

View File

@@ -1,49 +1,93 @@
# go-fuzz-headers
This repository contains various helper functions to be used with [go-fuzz](https://github.com/dvyukov/go-fuzz).
This repository contains various helper functions for go fuzzing. It is mostly used in combination with [go-fuzz](https://github.com/dvyukov/go-fuzz), but compatibility with fuzzing in the standard library will also be supported. Any coverage guided fuzzing engine that provides an array or slice of bytes can be used with go-fuzz-headers.
## Goal
The current goal of go-fuzz-headers is:
To maintain a series of helper utilities that can be used for golang projects that are integrated into OSS-fuzz and use the go-fuzz engine to fuzz more complicated types than merely strings and data arrays.
While go-fuzz-headers can be used when using go-fuzz outside of OSS-fuzz, we do not test such usage and cannot confirm that it is supported.
## Status
The project is under development and will be updated regularly.
Fuzzers that use `GenerateStruct` will not require modifications as more types get supported.
## Usage
To make use of the helper functions, a ConsumeFuzzer has to be instantiated:
Using go-fuzz-headers is easy. First create a new consumer with the bytes provided by the fuzzing engine:
```go
f := NewConsumer(data)
import (
fuzz "github.com/AdaLogics/go-fuzz-headers"
)
data := []byte{'R', 'a', 'n', 'd', 'o', 'm'}
f := fuzz.NewConsumer(data)
```
To split the input data from the fuzzer into a random set of equally large chunks:
This creates a `Consumer` that consumes the bytes of the input as it uses them to fuzz different types.
After that, `f` can be used to easily create fuzzed instances of different types. Below are some examples:
### Structs
One of the most useful features of go-fuzz-headers is its ability to fill structs with the data provided by the fuzzing engine. This is done with a single line:
```go
err := f.Split(3, 6)
type Person struct {
Name string
Age int
}
p := Person{}
// Fill p with values based on the data provided by the fuzzing engine:
err := f.GenerateStruct(&p)
```
...after which the consumer has the following available attributes:
This includes nested structs too. In this example, the fuzz Consumer will also insert values in `p.BestFriend`:
```go
f.CommandPart = commandPart
f.RestOfArray = restOfArray
f.NumberOfCalls = numberOfCalls
type PersonI struct {
Name string
Age int
BestFriend PersonII
}
type PersonII struct {
Name string
Age int
}
p := PersonI{}
err := f.GenerateStruct(&p)
```
To pass the input data from the fuzzer into a struct:
If the consumer should insert values for unexported fields as well as exported, this can be enabled with:
```go
ts := new(target_struct)
err :=f.GenerateStruct(ts)
f.AllowUnexportedFields()
```
or:
...and disabled with:
```go
ts := target_struct{}
err = f.GenerateStruct(&ts)
f.DisallowUnexportedFields()
```
`GenerateStruct` will pass data from the input data to the targeted struct. Currently the following field types are supported:
1. `string`
2. `bool`
3. `int`
4. `[]string`
5. `byte`
5. `[]byte`
6. custom structures
7. `map`
### Other types:
Other useful APIs:
```go
createdString, err := f.GetString() // Gets a string
createdInt, err := f.GetInt() // Gets an integer
createdByte, err := f.GetByte() // Gets a byte
createdBytes, err := f.GetBytes() // Gets a byte slice
createdBool, err := f.GetBool() // Gets a boolean
err := f.FuzzMap(target_map) // Fills a map
createdTarBytes, err := f.TarBytes() // Gets bytes of a valid tar archive
err := f.CreateFiles(inThisDir) // Fills inThisDir with files
createdString, err := f.GetStringFrom("anyCharInThisString", ofThisLength) // Gets a string that consists of chars from "anyCharInThisString" and has the exact length "ofThisLength"
```
Most APIs are added as they are needed.
## Projects that use go-fuzz-headers
- [runC](https://github.com/opencontainers/runc)
- [Istio](https://github.com/istio/istio)
- [Vitess](https://github.com/vitessio/vitess)
- [Containerd](https://github.com/containerd/containerd)
Feel free to add your own project to the list, if you use go-fuzz-headers to fuzz it.
## Status
The project is under development and will be updated regularly.
## References
go-fuzz-headers' approach to fuzzing structs is strongly inspired by [gofuzz](https://github.com/google/gofuzz).