Fuzzing: Add two more fuzzers

Signed-off-by: AdamKorcz <adam@adalogics.com>
This commit is contained in:
AdamKorcz
2021-08-03 11:32:19 +01:00
parent 7d4891783a
commit 41a04246f4
3 changed files with 169 additions and 1 deletions

View File

@@ -0,0 +1,138 @@
// +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.
*/
// nolint: golint
package fuzz
import (
"bytes"
"context"
_ "crypto/sha256" // required by go-digest
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
fuzz "github.com/AdaLogics/go-fuzz-headers"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/content/local"
)
// checkBlobPath performs some basic validation
func checkBlobPath(dgst digest.Digest, root string) error {
if err := dgst.Validate(); err != nil {
return err
}
path := filepath.Join(root, "blobs", dgst.Algorithm().String(), dgst.Hex())
_, err := os.Stat(path)
if err != nil {
return err
}
return nil
}
// generateBlobs is a helper function to create random blobs
func generateBlobs(f *fuzz.ConsumeFuzzer) (map[digest.Digest][]byte, error) {
blobs := map[digest.Digest][]byte{}
blobQty, err := f.GetInt()
if err != nil {
return blobs, err
}
maxsize := 4096
nblobs := blobQty % maxsize
for i := 0; i < nblobs; i++ {
digestBytes, err := f.GetBytes()
if err != nil {
return blobs, err
}
dgst := digest.FromBytes(digestBytes)
blobs[dgst] = digestBytes
}
return blobs, nil
}
// checkwrite is a wrapper around content.WriteBlob()
func checkWrite(ctx context.Context, cs content.Store, dgst digest.Digest, p []byte) (digest.Digest, error) {
if err := content.WriteBlob(ctx, cs, dgst.String(), bytes.NewReader(p),
ocispec.Descriptor{Size: int64(len(p)), Digest: dgst}); err != nil {
return dgst, err
}
return dgst, nil
}
// populateBlobStore creates a bunch of blobs
func populateBlobStore(ctx context.Context, cs content.Store, f *fuzz.ConsumeFuzzer) (map[digest.Digest][]byte, error) {
blobs, err := generateBlobs(f)
if err != nil {
return nil, err
}
for dgst, p := range blobs {
d, err := checkWrite(ctx, cs, dgst, p)
_ = d
if err != nil {
return blobs, err
}
}
return blobs, nil
}
// FuzzCSWalk implements a fuzzer that targets contentStore.Walk()
func FuzzCSWalk(data []byte) int {
ctx := context.Background()
expected := map[digest.Digest]struct{}{}
found := map[digest.Digest]struct{}{}
tmpdir, err := ioutil.TempDir("", "fuzzing-")
if err != nil {
return 0
}
cs, err := local.NewStore(tmpdir)
if err != nil {
return 0
}
f := fuzz.NewConsumer(data)
blobs, err := populateBlobStore(ctx, cs, f)
_ = blobs
if err != nil {
return 0
}
for dgst := range blobs {
expected[dgst] = struct{}{}
}
if err := cs.Walk(ctx, func(bi content.Info) error {
found[bi.Digest] = struct{}{}
err = checkBlobPath(bi.Digest, tmpdir)
if err != nil {
return err
}
return nil
}); err != nil {
return 0
}
if !reflect.DeepEqual(expected, found) {
panic(fmt.Sprintf("%v != %v but should be equal", found, expected))
}
return 1
}