|
| 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 | + |
| 22 | + fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 23 | + |
| 24 | + "github.com/containerd/containerd" |
| 25 | + _ "github.com/containerd/containerd/cmd/containerd" |
| 26 | + "github.com/containerd/containerd/cmd/containerd/command" |
| 27 | + "github.com/containerd/containerd/namespaces" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultRoot = "/var/lib/containerd" |
| 32 | + defaultState = "/tmp/containerd" |
| 33 | + defaultAddress = "/tmp/containerd/containerd.sock" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + args := []string{"--log-level", "debug"} |
| 38 | + go func() { |
| 39 | + // This is similar to invoking the |
| 40 | + // containerd binary. |
| 41 | + // See contrib/fuzz/oss_fuzz_build.sh |
| 42 | + // for more info. |
| 43 | + command.StartDaemonForFuzzing(args) |
| 44 | + }() |
| 45 | +} |
| 46 | + |
| 47 | +func fuzzContext() (context.Context, context.CancelFunc) { |
| 48 | + ctx, cancel := context.WithCancel(context.Background()) |
| 49 | + ctx = namespaces.WithNamespace(ctx, "fuzzing-namespace") |
| 50 | + return ctx, cancel |
| 51 | +} |
| 52 | + |
| 53 | +func FuzzContainerdImport(data []byte) int { |
| 54 | + client, err := containerd.New(defaultAddress) |
| 55 | + if err != nil { |
| 56 | + return 0 |
| 57 | + } |
| 58 | + defer client.Close() |
| 59 | + |
| 60 | + f := fuzz.NewConsumer(data) |
| 61 | + |
| 62 | + noOfImports, err := f.GetInt() |
| 63 | + if err != nil { |
| 64 | + return 0 |
| 65 | + } |
| 66 | + maxImports := 20 |
| 67 | + ctx, cancel := fuzzContext() |
| 68 | + defer cancel() |
| 69 | + for i := 0; i < noOfImports%maxImports; i++ { |
| 70 | + tarBytes, err := f.GetBytes() |
| 71 | + if err != nil { |
| 72 | + return 0 |
| 73 | + } |
| 74 | + _, _ = client.Import(ctx, bytes.NewReader(tarBytes)) |
| 75 | + } |
| 76 | + return 1 |
| 77 | +} |
0 commit comments