|
| 1 | +// +build gofuzz |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +// nolint: golint |
| 17 | +package fuzz |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + _ "crypto/sha256" // required by go-digest |
| 23 | + "fmt" |
| 24 | + "io/ioutil" |
| 25 | + "os" |
| 26 | + "path/filepath" |
| 27 | + "reflect" |
| 28 | + |
| 29 | + fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 30 | + digest "github.com/opencontainers/go-digest" |
| 31 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 32 | + |
| 33 | + "github.com/containerd/containerd/content" |
| 34 | + "github.com/containerd/containerd/content/local" |
| 35 | +) |
| 36 | + |
| 37 | +// checkBlobPath performs some basic validation |
| 38 | +func checkBlobPath(dgst digest.Digest, root string) error { |
| 39 | + if err := dgst.Validate(); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + path := filepath.Join(root, "blobs", dgst.Algorithm().String(), dgst.Hex()) |
| 43 | + _, err := os.Stat(path) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// generateBlobs is a helper function to create random blobs |
| 51 | +func generateBlobs(f *fuzz.ConsumeFuzzer) (map[digest.Digest][]byte, error) { |
| 52 | + blobs := map[digest.Digest][]byte{} |
| 53 | + blobQty, err := f.GetInt() |
| 54 | + if err != nil { |
| 55 | + return blobs, err |
| 56 | + } |
| 57 | + maxsize := 4096 |
| 58 | + nblobs := blobQty % maxsize |
| 59 | + |
| 60 | + for i := 0; i < nblobs; i++ { |
| 61 | + digestBytes, err := f.GetBytes() |
| 62 | + if err != nil { |
| 63 | + return blobs, err |
| 64 | + } |
| 65 | + |
| 66 | + dgst := digest.FromBytes(digestBytes) |
| 67 | + blobs[dgst] = digestBytes |
| 68 | + } |
| 69 | + |
| 70 | + return blobs, nil |
| 71 | +} |
| 72 | + |
| 73 | +// checkwrite is a wrapper around content.WriteBlob() |
| 74 | +func checkWrite(ctx context.Context, cs content.Store, dgst digest.Digest, p []byte) (digest.Digest, error) { |
| 75 | + if err := content.WriteBlob(ctx, cs, dgst.String(), bytes.NewReader(p), |
| 76 | + ocispec.Descriptor{Size: int64(len(p)), Digest: dgst}); err != nil { |
| 77 | + return dgst, err |
| 78 | + } |
| 79 | + return dgst, nil |
| 80 | +} |
| 81 | + |
| 82 | +// populateBlobStore creates a bunch of blobs |
| 83 | +func populateBlobStore(ctx context.Context, cs content.Store, f *fuzz.ConsumeFuzzer) (map[digest.Digest][]byte, error) { |
| 84 | + blobs, err := generateBlobs(f) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + for dgst, p := range blobs { |
| 90 | + d, err := checkWrite(ctx, cs, dgst, p) |
| 91 | + _ = d |
| 92 | + if err != nil { |
| 93 | + return blobs, err |
| 94 | + } |
| 95 | + } |
| 96 | + return blobs, nil |
| 97 | +} |
| 98 | + |
| 99 | +// FuzzCSWalk implements a fuzzer that targets contentStore.Walk() |
| 100 | +func FuzzCSWalk(data []byte) int { |
| 101 | + ctx := context.Background() |
| 102 | + expected := map[digest.Digest]struct{}{} |
| 103 | + found := map[digest.Digest]struct{}{} |
| 104 | + tmpdir, err := ioutil.TempDir("", "fuzzing-") |
| 105 | + if err != nil { |
| 106 | + return 0 |
| 107 | + } |
| 108 | + cs, err := local.NewStore(tmpdir) |
| 109 | + if err != nil { |
| 110 | + return 0 |
| 111 | + } |
| 112 | + |
| 113 | + f := fuzz.NewConsumer(data) |
| 114 | + blobs, err := populateBlobStore(ctx, cs, f) |
| 115 | + _ = blobs |
| 116 | + if err != nil { |
| 117 | + return 0 |
| 118 | + } |
| 119 | + |
| 120 | + for dgst := range blobs { |
| 121 | + expected[dgst] = struct{}{} |
| 122 | + } |
| 123 | + |
| 124 | + if err := cs.Walk(ctx, func(bi content.Info) error { |
| 125 | + found[bi.Digest] = struct{}{} |
| 126 | + err = checkBlobPath(bi.Digest, tmpdir) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + return nil |
| 131 | + }); err != nil { |
| 132 | + return 0 |
| 133 | + } |
| 134 | + if !reflect.DeepEqual(expected, found) { |
| 135 | + panic(fmt.Sprintf("%v != %v but should be equal", found, expected)) |
| 136 | + } |
| 137 | + return 1 |
| 138 | +} |
0 commit comments