|
| 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 | +package fuzz |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "context" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + |
| 24 | + fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 25 | + |
| 26 | + "github.com/containerd/containerd/archive" |
| 27 | +) |
| 28 | + |
| 29 | +// FuzzApply implements a fuzzer that applies |
| 30 | +// a fuzzed tar archive on a directory |
| 31 | +func FuzzApply(data []byte) int { |
| 32 | + f := fuzz.NewConsumer(data) |
| 33 | + iters, err := f.GetInt() |
| 34 | + if err != nil { |
| 35 | + return 0 |
| 36 | + } |
| 37 | + maxIters := 20 |
| 38 | + tmpDir, err := ioutil.TempDir("", "prefix-test") |
| 39 | + if err != nil { |
| 40 | + return 0 |
| 41 | + } |
| 42 | + defer os.RemoveAll(tmpDir) |
| 43 | + for i := 0; i < iters%maxIters; i++ { |
| 44 | + rBytes, err := f.TarBytes() |
| 45 | + if err != nil { |
| 46 | + return 0 |
| 47 | + } |
| 48 | + r := bytes.NewReader(rBytes) |
| 49 | + _, _ = archive.Apply(context.Background(), tmpDir, r) |
| 50 | + } |
| 51 | + return 1 |
| 52 | +} |
0 commit comments