From ea3286687fffaf0ecc269b342ee3b4556e2469b1 Mon Sep 17 00:00:00 2001 From: AdamKorcz <44787359+AdamKorcz@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:16:30 +0000 Subject: [PATCH] moby: add more fuzzers (#8889) Signed-off-by: AdamKorcz Signed-off-by: AdamKorcz --- projects/moby/Dockerfile | 2 +- projects/moby/archive_fuzzers.go | 57 +++++++++++++++++++++++++++++ projects/moby/build.sh | 12 ++++++ projects/moby/jsonfilelog_fuzzer.go | 27 ++++++++++++++ projects/moby/tailfile_fuzzer.go | 49 +++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 projects/moby/archive_fuzzers.go create mode 100644 projects/moby/jsonfilelog_fuzzer.go create mode 100644 projects/moby/tailfile_fuzzer.go diff --git a/projects/moby/Dockerfile b/projects/moby/Dockerfile index c81fded6b..c243bc0a9 100644 --- a/projects/moby/Dockerfile +++ b/projects/moby/Dockerfile @@ -17,4 +17,4 @@ FROM gcr.io/oss-fuzz-base/base-builder-go RUN git clone --depth 1 https://github.com/moby/moby WORKDIR moby -COPY build.sh mounts_fuzzer.go $SRC/ +COPY build.sh archive_fuzzers.go tailfile_fuzzer.go jsonfilelog_fuzzer.go mounts_fuzzer.go $SRC/ diff --git a/projects/moby/archive_fuzzers.go b/projects/moby/archive_fuzzers.go new file mode 100644 index 000000000..196a19fd9 --- /dev/null +++ b/projects/moby/archive_fuzzers.go @@ -0,0 +1,57 @@ +// Copyright 2022 Google LLC. All Rights Reserved. +// +// 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 archive + +import ( + "bytes" + fuzz "github.com/AdaLogics/go-fuzz-headers" + "os" +) + +func FuzzDecompressStream(data []byte) int { + r := bytes.NewReader(data) + _, _ = DecompressStream(r) + return 1 +} + +func FuzzUntar(data []byte) int { + f := fuzz.NewConsumer(data) + tarBytes, err := f.TarBytes() + if err != nil { + return 0 + } + options := &TarOptions{} + err = f.GenerateStruct(options) + if err != nil { + return 0 + } + defer os.Remove("testdir") + err = os.Mkdir("testdir", 0750) + if err != nil && !os.IsExist(err) { + return 0 + } + Untar(bytes.NewReader(tarBytes), "testdir", options) + return 1 +} + +func FuzzApplyLayer(data []byte) int { + defer os.Remove("testDir") + err := os.Mkdir("testDir", 0750) + if err != nil { + return 0 + } + _, _ = ApplyLayer("testDir", bytes.NewReader(data)) + return 1 +} diff --git a/projects/moby/build.sh b/projects/moby/build.sh index 6abd454c2..d45641871 100644 --- a/projects/moby/build.sh +++ b/projects/moby/build.sh @@ -18,9 +18,21 @@ mv $SRC/moby/vendor.mod $SRC/moby/go.mod cp $SRC/mounts_fuzzer.go $SRC/moby/volume/mounts/ +cp $SRC/tailfile_fuzzer.go $SRC/moby/pkg/tailfile/ +cp $SRC/archive_fuzzers.go $SRC/moby/pkg/archive/ +cp $SRC/jsonfilelog_fuzzer.go $SRC/moby/daemon/logger/jsonfilelog/ go mod tidy +go get github.com/AdaLogics/go-fuzz-headers@latest +go mod vendor mv $SRC/moby/volume/mounts/parser_test.go $SRC/moby/volume/mounts/parser_test_fuzz.go mv $SRC/moby/volume/mounts/validate_unix_test.go $SRC/moby/volume/mounts/validate_unix_test_fuzz.go compile_go_fuzzer github.com/docker/docker/volume/mounts FuzzParseLinux FuzzParseLinux + +compile_go_fuzzer github.com/docker/docker/pkg/archive FuzzUntar FuzzUntar +compile_go_fuzzer github.com/docker/docker/pkg/archive FuzzDecompressStream FuzzDecompressStream +compile_go_fuzzer github.com/docker/docker/pkg/archive FuzzApplyLayer FuzzApplyLayer +compile_go_fuzzer github.com/docker/docker/pkg/tailfile FuzzTailfile FuzzTailfile +compile_go_fuzzer github.com/docker/docker/daemon/logger/jsonfilelog FuzzLoggerDecode FuzzLoggerDecode + diff --git a/projects/moby/jsonfilelog_fuzzer.go b/projects/moby/jsonfilelog_fuzzer.go new file mode 100644 index 000000000..b4157e69c --- /dev/null +++ b/projects/moby/jsonfilelog_fuzzer.go @@ -0,0 +1,27 @@ +// Copyright 2022 Google LLC. All Rights Reserved. +// +// 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 jsonfilelog + +import ( + "bytes" +) + +func FuzzLoggerDecode(data []byte) int { + dec := decodeFunc(bytes.NewBuffer(data)) + defer dec.Close() + + _, _ = dec.Decode() + return 1 +} diff --git a/projects/moby/tailfile_fuzzer.go b/projects/moby/tailfile_fuzzer.go new file mode 100644 index 000000000..797ac551f --- /dev/null +++ b/projects/moby/tailfile_fuzzer.go @@ -0,0 +1,49 @@ +// Copyright 2022 Google LLC. All Rights Reserved. +// +// 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 tailfile + +import ( + fuzz "github.com/AdaLogics/go-fuzz-headers" + "os" +) + +func FuzzTailfile(data []byte) int { + if len(data) < 5 { + return 0 + } + f := fuzz.NewConsumer(data) + n, err := f.GetUint64() + if err != nil { + return 0 + } + fileBytes, err := f.GetBytes() + if err != nil { + return 0 + } + defer os.Remove("tailfile") + fil, err := os.Create("tailfile") + if err != nil { + return 0 + } + defer fil.Close() + + _, err = fil.Write(fileBytes) + if err != nil { + return 0 + } + fil.Seek(0, 0) + _, _ = TailFile(fil, int(n)) + return 1 +}