containerd/contrib/fuzz/containerd_import_fuzzer.go
AdamKorcz c9b1b2fd5b Fuzzing: Add fuzzer
Signed-off-by: AdamKorcz <adam@adalogics.com>
2021-08-17 00:08:59 +01:00

78 lines
1.9 KiB
Go

// +build gofuzz
/*
Copyright The containerd 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 fuzz
import (
"bytes"
"context"
fuzz "github.com/AdaLogics/go-fuzz-headers"
"github.com/containerd/containerd"
_ "github.com/containerd/containerd/cmd/containerd"
"github.com/containerd/containerd/cmd/containerd/command"
"github.com/containerd/containerd/namespaces"
)
const (
defaultRoot = "/var/lib/containerd"
defaultState = "/tmp/containerd"
defaultAddress = "/tmp/containerd/containerd.sock"
)
func init() {
args := []string{"--log-level", "debug"}
go func() {
// This is similar to invoking the
// containerd binary.
// See contrib/fuzz/oss_fuzz_build.sh
// for more info.
command.StartDaemonForFuzzing(args)
}()
}
func fuzzContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
ctx = namespaces.WithNamespace(ctx, "fuzzing-namespace")
return ctx, cancel
}
func FuzzContainerdImport(data []byte) int {
client, err := containerd.New(defaultAddress)
if err != nil {
return 0
}
defer client.Close()
f := fuzz.NewConsumer(data)
noOfImports, err := f.GetInt()
if err != nil {
return 0
}
maxImports := 20
ctx, cancel := fuzzContext()
defer cancel()
for i := 0; i < noOfImports%maxImports; i++ {
tarBytes, err := f.GetBytes()
if err != nil {
return 0
}
_, _ = client.Import(ctx, bytes.NewReader(tarBytes))
}
return 1
}