From 66d7e5f1cf47b2f6de54fd9bf1694902822f61a0 Mon Sep 17 00:00:00 2001 From: AdamKorcz <44787359+AdamKorcz@users.noreply.github.com> Date: Wed, 14 Apr 2021 18:42:35 +0100 Subject: [PATCH] [golang] Added 2 fuzzers (#5437) * [golang] Added 2 fuzzers * Change fuzzer to call exported targets * Added fuzzer --- projects/golang/Dockerfile | 2 +- projects/golang/build.sh | 6 +++ projects/golang/math_big_fuzzer.go | 63 ++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 projects/golang/math_big_fuzzer.go diff --git a/projects/golang/Dockerfile b/projects/golang/Dockerfile index 53287ccd3..41ef616c7 100644 --- a/projects/golang/Dockerfile +++ b/projects/golang/Dockerfile @@ -17,6 +17,6 @@ FROM gcr.io/oss-fuzz-base/base-builder RUN git clone --depth 1 https://github.com/dvyukov/go-fuzz-corpus golang -COPY build.sh $SRC/ +COPY build.sh math_big_fuzzer.go $SRC/ WORKDIR $SRC/golang diff --git a/projects/golang/build.sh b/projects/golang/build.sh index ef404b8c9..fda77c370 100755 --- a/projects/golang/build.sh +++ b/projects/golang/build.sh @@ -15,8 +15,14 @@ # These two dependencies cause build issues and are not used by oss-fuzz: rm -r sqlparser rm -r parser + +mkdir math && cp $SRC/math_big_fuzzer.go ./math/ + go mod init "github.com/dvyukov/go-fuzz-corpus" export FUZZ_ROOT="github.com/dvyukov/go-fuzz-corpus" +compile_go_fuzzer $FUZZ_ROOT/math FuzzBigIntCmp1 big_cmp_fuzzer1 +compile_go_fuzzer $FUZZ_ROOT/math FuzzBigIntCmp2 big_cmp_fuzzer2 +compile_go_fuzzer $FUZZ_ROOT/math FuzzRatSetString big_rat_fuzzer compile_go_fuzzer $FUZZ_ROOT/asn1 Fuzz asn_fuzzer compile_go_fuzzer $FUZZ_ROOT/csv Fuzz csv_fuzzer compile_go_fuzzer $FUZZ_ROOT/elliptic Fuzz elliptic_fuzzer diff --git a/projects/golang/math_big_fuzzer.go b/projects/golang/math_big_fuzzer.go new file mode 100644 index 000000000..60946bc46 --- /dev/null +++ b/projects/golang/math_big_fuzzer.go @@ -0,0 +1,63 @@ +// Copyright 2021 Google LLC +// +// 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 mathfuzzer + +import "math/big" + +func FuzzBigIntCmp1(data []byte) int { + if !isDivisibleBy(len(data), 2) { + return -1 + } + i1 := new(big.Int) + i2 := new(big.Int) + + half := len(data) / 2 + + halfOne := data[:half] + halfTwo := data[half:] + + i1.SetBytes(halfOne) + i2.SetBytes(halfTwo) + + i1.Cmp(i2) + return 1 +} + +func FuzzBigIntCmp2(data []byte) int { + if !isDivisibleBy(len(data), 2) { + return -1 + } + x, y := new(big.Int), new(big.Int) + half := len(data)/2 + if err := x.UnmarshalText(data[:half]); err != nil { + return 0 + } + if err := y.UnmarshalText(data[half:]); err != nil { + return 0 + } + x.Cmp(y) + return 1 +} + +func FuzzRatSetString(data []byte) int { + _, _ = new(big.Rat).SetString(string(data)) + return 1 +} + +func isDivisibleBy(n int, divisibleby int) bool { + return (n % divisibleby) == 0 +}