moby: add more fuzzers (#8889)

Signed-off-by: AdamKorcz <adam@adalogics.com>

Signed-off-by: AdamKorcz <adam@adalogics.com>
This commit is contained in:
AdamKorcz 2022-10-31 10:16:30 +00:00 committed by GitHub
parent 3e614b76f3
commit ea3286687f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 146 additions and 1 deletions

View File

@ -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/

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}