Skip to content

Commit

Permalink
Refactor fuzzing
Browse files Browse the repository at this point in the history
Structure the fuzz implementation to be closer to what go native will support.
Add Makefile target to enable smoketesting fuzzers.
Add github workflow to test and upload crash results.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
  • Loading branch information
Paulo Gomes committed Jan 10, 2022
1 parent 945c9b1 commit b728528
Show file tree
Hide file tree
Showing 18 changed files with 2,205 additions and 181 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/cifuzz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CIFuzz
on: [pull_request]
jobs:
Fuzzing:
runs-on: ubuntu-latest
steps:
- name: Build Fuzzers
id: build
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
with:
oss-fuzz-project-name: 'fluxcd'
language: go
- name: Run Fuzzers
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
with:
oss-fuzz-project-name: 'fluxcd'
language: go
fuzz-seconds: 180
- name: Upload Crash
uses: actions/upload-artifact@v1
if: failure() && steps.build.outcome == 'success'
with:
name: artifacts
path: ./out/artifacts
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

build/
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,23 @@ SETUP_ENVTEST=$(GOBIN)/setup-envtest
else
SETUP_ENVTEST=$(shell which setup-envtest)
endif


fuzz-build:
rm -rf $(shell pwd)/build/fuzz/
mkdir -p $(shell pwd)/build/fuzz/out/

docker build . --tag local-fuzzing:latest -f tests/fuzz/Dockerfile.builder
docker run --rm -it \
-e FUZZING_LANGUAGE=go -e FUZZ_SECONDS=600 -e MODE=batch \
-e CIFUZZ_DEBUG='True' -e OSS_FUZZ_PROJECT_NAME=fluxcd \
-e SANITIZER=address \
-v "$(shell pwd)/build/fuzz/out":/out \
local-fuzzing:latest

fuzz-smoketest: fuzz-build
docker run --rm -ti \
-v "$(shell pwd)/build/fuzz/out":/out \
-v "$(shell pwd)/tests/fuzz/oss_fuzz_run.sh":/runner.sh \
gcr.io/oss-fuzz/fluxcd \
bash -c "/runner.sh"
62 changes: 0 additions & 62 deletions fuzz/Dockerfile

This file was deleted.

91 changes: 0 additions & 91 deletions fuzz/fuzz.go

This file was deleted.

25 changes: 25 additions & 0 deletions gitutil/gitutil_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build gofuzz
// +build gofuzz

/*
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitutil

import "errors"

// FuzzLibGit2Error implements a fuzzer that targets gitutil.LibGit2Error.
func FuzzLibGit2Error(data []byte) int {
err := errors.New(string(data))
_ = LibGit2Error(err)
return 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// FuzzGetterConditions implements a fuzzer that
// targets SetSummary()
// FuzzGetterConditions implements a fuzzer that targets
// conditions.SetSummary().
func FuzzGetterConditions(data []byte) int {
f := fuzz.NewConsumer(data)

Expand Down Expand Up @@ -57,8 +57,8 @@ func FuzzGetterConditions(data []byte) int {
return 1
}

// FuzzConditionsMatch implements a fuzzer that
// that targets Match()
// FuzzConditionsMatch implements a fuzzer that that targets
// MatchCondition.Match().
func FuzzConditionsMatch(data []byte) int {
f := fuzz.NewConsumer(data)
condition := metav1.Condition{}
Expand All @@ -79,8 +79,7 @@ func FuzzConditionsMatch(data []byte) int {
return 1
}

// newGetter allows the fuzzer to create a Getter
// This is just a utility
// newGetter allows the fuzzer to create a Getter.
func newGetter(f *fuzz.ConsumeFuzzer) (Getter, error) {
obj := &testdata.Fake{}
noOfConditions, err := f.GetInt()
Expand All @@ -102,8 +101,7 @@ func newGetter(f *fuzz.ConsumeFuzzer) (Getter, error) {
return obj, nil
}

// newSetter allows the fuzzer to create a Setter
// This is just a utility
// newSetter allows the fuzzer to create a Setter.
func newSetter(f *fuzz.ConsumeFuzzer) (Setter, error) {
obj := &testdata.Fake{}
noOfConditions, err := f.GetInt()
Expand All @@ -124,7 +122,7 @@ func newSetter(f *fuzz.ConsumeFuzzer) (Setter, error) {
return obj, nil
}

// FuzzPatchApply implements a fuzzer that targets patch.Apply
// FuzzPatchApply implements a fuzzer that targets Patch.Apply.
func FuzzPatchApply(data []byte) int {
f := fuzz.NewConsumer(data)

Expand All @@ -146,8 +144,8 @@ func FuzzPatchApply(data []byte) int {
return 1
}

// FuzzConditionsUnstructured implements a fuzzer
// that targets GetConditions()
// FuzzConditionsUnstructured implements a fuzzer that targets
// Getter.GetConditions.
func FuzzConditionsUnstructured(data []byte) int {
u := &unstructured.Unstructured{}
f := fuzz.NewConsumer(data)
Expand Down
Loading

0 comments on commit b728528

Please sign in to comment.