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 f266896
Show file tree
Hide file tree
Showing 19 changed files with 2,218 additions and 168 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.

26 changes: 26 additions & 0 deletions gitutil/gitutil_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//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
}
File renamed without changes.
131 changes: 131 additions & 0 deletions runtime/events/events_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//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 events

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"sync"

fuzz "github.com/AdaLogics/go-fuzz-headers"
"github.com/fluxcd/pkg/runtime/testenv"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"
)

var (
doOnce sync.Once
env *testenv.Environment
ts *httptest.Server
ctx = ctrl.SetupSignalHandler()
)

func ensureDependencies() error {
// only install dependencies when running inside a container
if _, err := os.Stat("/.dockerenv"); os.IsNotExist(err) {
return nil
}

if os.Getenv("KUBEBUILDER_ASSETS") == "" {
cmd := exec.Command("/usr/bin/bash", "-c", `go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest && \
/root/go/bin/setup-envtest use -p path 1.21.x`)
cmd.Env = append(os.Environ(), "GOPATH=/root/go")
assetsPath, err := cmd.Output()
if err == nil {
os.Setenv("KUBEBUILDER_ASSETS", string(assetsPath))
}
return err
}

return nil
}

// FuzzEventInfof implements a fuzzer that
// targets eventRecorder.Eventf()
func FuzzEventf(data []byte) int {
doOnce.Do(func() {
if err := ensureDependencies(); err != nil {
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
}
})

ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
return
}

var payload Event
err = json.Unmarshal(b, &payload)
if err != nil {
return
}
}))
defer ts.Close()

scheme := runtime.NewScheme()
utilruntime.Must(corev1.AddToScheme(scheme))

env = testenv.New(
testenv.WithScheme(scheme),
)

go func() {
fmt.Println("Starting the test environment")
if err := env.Start(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
}
}()
<-env.Manager.Elected()

eventRecorder, err := NewRecorder(env, ctrl.Log, ts.URL, "test-controller")
if err != nil {
return 0
}
eventRecorder.Client.RetryMax = 2
//TODO: Reuse the setup above across fuzzing calls
// this will be easier once fuzzing is migrated to
// native golang fuzz.

f := fuzz.NewConsumer(data)
obj := corev1.ConfigMap{}
err = f.GenerateStruct(&obj)
if err != nil {
return 0
}
eventtype, err := f.GetString()
if err != nil {
return 0
}
reason, err := f.GetString()
if err != nil {
return 0
}
eventRecorder.Eventf(&obj, eventtype, reason, obj.Name)

if err = env.Stop(); err != nil {
return 0
}

return 1
}
Loading

0 comments on commit f266896

Please sign in to comment.