forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fluxcd#212 from pjbgf/fuzz
Refactor Fuzz implementation
- Loading branch information
Showing
14 changed files
with
605 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: CIFuzz | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
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: 60 | ||
- name: Upload Crash | ||
uses: actions/upload-artifact@v1 | ||
if: failure() && steps.build.outcome == 'success' | ||
with: | ||
name: artifacts | ||
path: ./out/artifacts |
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 |
---|---|---|
|
@@ -13,3 +13,5 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
build/ |
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,6 @@ | ||
FROM gcr.io/oss-fuzz-base/base-builder-go | ||
|
||
COPY ./ $GOPATH/src/github.com/fluxcd/pkg/ | ||
COPY ./tests/fuzz/oss_fuzz_build.sh $SRC/build.sh | ||
|
||
WORKDIR $SRC |
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,45 @@ | ||
# fuzz testing | ||
|
||
Flux is part of Google's [oss fuzz] program which provides continuous fuzzing for | ||
open source projects. | ||
|
||
The long running fuzzing execution is configured in the [oss-fuzz repository]. | ||
Shorter executions are done on a per-PR basis, configured as a [github workflow]. | ||
|
||
For fuzzers to be called, they must be compiled within [oss_fuzz_build.sh](./oss_fuzz_build.sh). | ||
|
||
### Testing locally | ||
|
||
Build fuzzers: | ||
|
||
```bash | ||
make fuzz-build | ||
``` | ||
All fuzzers will be built into `./build/fuzz/out`. | ||
|
||
Smoke test fuzzers: | ||
|
||
```bash | ||
make fuzz-smoketest | ||
``` | ||
|
||
The smoke test runs each fuzzer once to ensure they are fully functional. | ||
|
||
Run fuzzer locally: | ||
```bash | ||
./build/fuzz/out/fuzz_conditions_match | ||
``` | ||
|
||
Run fuzzer inside a container: | ||
|
||
```bash | ||
docker run --rm -ti \ | ||
-v "$(pwd)/build/fuzz/out":/out \ | ||
gcr.io/oss-fuzz/fluxcd \ | ||
/out/fuzz_conditions_match | ||
``` | ||
|
||
|
||
[oss fuzz]: https://github.com/google/oss-fuzz | ||
[oss-fuzz repository]: https://github.com/google/oss-fuzz/tree/master/projects/fluxcd | ||
[github workflow]: .github/workflows/cifuzz.yaml |
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,162 @@ | ||
//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 conditions | ||
|
||
import ( | ||
fuzz "github.com/AdaLogics/go-fuzz-headers" | ||
"github.com/fluxcd/pkg/runtime/conditions/testdata" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
// FuzzGetterConditions implements a fuzzer that targets | ||
// conditions.SetSummary(). | ||
func FuzzGetterConditions(data []byte) int { | ||
f := fuzz.NewConsumer(data) | ||
|
||
// Create slice of metav1.Condition | ||
noOfConditions, err := f.GetInt() | ||
if err != nil { | ||
return 0 | ||
} | ||
maxNoOfConditions := 30 | ||
conditions := make([]metav1.Condition, 0) | ||
|
||
// Add Conditions in the slice | ||
for i := 0; i < noOfConditions%maxNoOfConditions; i++ { | ||
c := metav1.Condition{} | ||
err = f.GenerateStruct(&c) | ||
if err != nil { | ||
return 0 | ||
} | ||
conditions = append(conditions, c) | ||
} | ||
obj := &testdata.Fake{} | ||
obj.SetConditions(conditions) | ||
|
||
targetCondition, err := f.GetString() | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
// Call the target | ||
SetSummary(obj, targetCondition) | ||
return 1 | ||
} | ||
|
||
// FuzzConditionsMatch implements a fuzzer that that targets | ||
// MatchCondition.Match(). | ||
func FuzzConditionsMatch(data []byte) int { | ||
f := fuzz.NewConsumer(data) | ||
condition := metav1.Condition{} | ||
err := f.GenerateStruct(&condition) | ||
if err != nil { | ||
return 0 | ||
} | ||
m := MatchCondition(condition) | ||
|
||
actual := metav1.Condition{} | ||
err = f.GenerateStruct(&actual) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
// Call the target | ||
_, _ = m.Match(actual) | ||
return 1 | ||
} | ||
|
||
// newGetter allows the fuzzer to create a Getter. | ||
func newGetter(f *fuzz.ConsumeFuzzer) (Getter, error) { | ||
obj := &testdata.Fake{} | ||
noOfConditions, err := f.GetInt() | ||
if err != nil { | ||
return obj, err | ||
} | ||
maxNoOfConditions := 30 | ||
conditions := make([]metav1.Condition, 0) | ||
for i := 0; i < noOfConditions%maxNoOfConditions; i++ { | ||
c := metav1.Condition{} | ||
err = f.GenerateStruct(&c) | ||
if err != nil { | ||
return obj, err | ||
} | ||
conditions = append(conditions, c) | ||
} | ||
|
||
obj.SetConditions(conditions) | ||
return obj, nil | ||
} | ||
|
||
// newSetter allows the fuzzer to create a Setter. | ||
func newSetter(f *fuzz.ConsumeFuzzer) (Setter, error) { | ||
obj := &testdata.Fake{} | ||
noOfConditions, err := f.GetInt() | ||
if err != nil { | ||
return obj, err | ||
} | ||
maxNoOfConditions := 30 | ||
conditions := make([]metav1.Condition, 0) | ||
for i := 0; i < noOfConditions%maxNoOfConditions; i++ { | ||
c := metav1.Condition{} | ||
err = f.GenerateStruct(&c) | ||
if err != nil { | ||
return obj, err | ||
} | ||
conditions = append(conditions, c) | ||
} | ||
obj.SetConditions(conditions) | ||
return obj, nil | ||
} | ||
|
||
// FuzzPatchApply implements a fuzzer that targets Patch.Apply. | ||
func FuzzPatchApply(data []byte) int { | ||
f := fuzz.NewConsumer(data) | ||
|
||
before, err := newGetter(f) | ||
if err != nil { | ||
return 0 | ||
} | ||
after, err := newGetter(f) | ||
if err != nil { | ||
return 0 | ||
} | ||
patch := NewPatch(before, after) | ||
|
||
setter, err := newSetter(f) | ||
if err != nil { | ||
return 0 | ||
} | ||
_ = patch.Apply(setter) | ||
return 1 | ||
} | ||
|
||
// FuzzConditionsUnstructured implements a fuzzer that targets | ||
// Getter.GetConditions. | ||
func FuzzConditionsUnstructured(data []byte) int { | ||
u := &unstructured.Unstructured{} | ||
f := fuzz.NewConsumer(data) | ||
err := f.GenerateStruct(u) | ||
if err != nil { | ||
return 0 | ||
} | ||
g := UnstructuredGetter(u) | ||
_ = g.GetConditions() | ||
return 1 | ||
} |
Oops, something went wrong.