From 69f39bc373c3869b17eec99148890b67b9badca6 Mon Sep 17 00:00:00 2001 From: Catena cyber <35799796+catenacyber@users.noreply.github.com> Date: Fri, 15 Jan 2021 22:12:37 +0100 Subject: [PATCH] Adds grpc-go project (#4971) * Adds grpc-go project * Add dfawley@ Co-authored-by: Abhishek Arya --- projects/grpc-go/Dockerfile | 23 +++++++ projects/grpc-go/build.sh | 23 +++++++ projects/grpc-go/fuzz_hello.go | 113 +++++++++++++++++++++++++++++++++ projects/grpc-go/project.yaml | 11 ++++ 4 files changed, 170 insertions(+) create mode 100644 projects/grpc-go/Dockerfile create mode 100755 projects/grpc-go/build.sh create mode 100644 projects/grpc-go/fuzz_hello.go create mode 100644 projects/grpc-go/project.yaml diff --git a/projects/grpc-go/Dockerfile b/projects/grpc-go/Dockerfile new file mode 100644 index 000000000..dc00af586 --- /dev/null +++ b/projects/grpc-go/Dockerfile @@ -0,0 +1,23 @@ +# 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. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder +ENV GO111MODULE=on +RUN go get google.golang.org/protobuf/cmd/protoc-gen-go google.golang.org/grpc/cmd/protoc-gen-go-grpc +RUN git clone --depth 1 https://github.com/grpc/grpc-go $GOPATH/src/google.golang.org/grpc + +COPY build.sh fuzz_*.go $SRC/ +WORKDIR $SRC/ diff --git a/projects/grpc-go/build.sh b/projects/grpc-go/build.sh new file mode 100755 index 000000000..09f3df64c --- /dev/null +++ b/projects/grpc-go/build.sh @@ -0,0 +1,23 @@ +#!/bin/bash -eu +# 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. +# +################################################################################ + +cp $SRC/fuzz*.go $GOPATH/src/google.golang.org/grpc/examples/ + +# seems needed to build the targets with the golang modules... +cd $GOPATH/src/google.golang.org/grpc/examples/ +compile_go_fuzzer google.golang.org/grpc/examples FuzzHelloClient fuzz_helloclient +compile_go_fuzzer google.golang.org/grpc/examples FuzzHelloServer fuzz_helloserver diff --git a/projects/grpc-go/fuzz_hello.go b/projects/grpc-go/fuzz_hello.go new file mode 100644 index 000000000..1202b96bf --- /dev/null +++ b/projects/grpc-go/fuzz_hello.go @@ -0,0 +1,113 @@ +package grpc_hello_fuzz + +import ( + "context" + "log" + "net" + "time" + + "google.golang.org/grpc" + pb "google.golang.org/grpc/examples/helloworld/helloworld" +) + +var initialized = 0 + +type server struct { + pb.UnimplementedGreeterServer +} + +// SayHello implements helloworld.GreeterServer +func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { + log.Printf("Received: %v", in.GetName()) + return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil +} + +func FuzzHelloServer(data []byte) int { + if initialized == 0 { + lis, err := net.Listen("tcp", ":50051") + if err != nil { + log.Printf("failed to listen: %v\n", err) + return 0 + } + s := grpc.NewServer() + pb.RegisterGreeterServer(s, &server{}) + // start server as a separate goroutine + go func() { + if err := s.Serve(lis); err != nil { + log.Printf("failed to serve: %v\n", err) + } + }() + initialized = 1 + } + + conn, err := net.Dial("tcp", "localhost:50051") + if err != nil { + log.Printf("failed to dial: %v\n", err) + return 0 + } + conn.Write(data) + response := make([]byte, 1+len(data)) + n, err := conn.Read(response) + conn.Close() + if err != nil || n == 0 { + return 0 + } + return 1 +} + +var fuzzdata []byte + +func FuzzHelloClient(data []byte) int { + if len(data) == 0 { + return 0 + } + if initialized == 0 { + lis, err := net.Listen("tcp", ":50051") + if err != nil { + log.Printf("failed to listen: %v\n", err) + return 0 + } + go func() { + for { + conn, err := lis.Accept() + if err != nil { + log.Printf("did not accept: %v", err) + break + } + conn.SetDeadline(time.Now().Add(time.Millisecond * 100)) + request := make([]byte, 24) + n, err := conn.Read(request) + if err != nil || n == 0 { + log.Printf("did not read: %v", err) + conn.Close() + break + } + n, err = conn.Write(fuzzdata) + if err != nil || n == 0 { + log.Printf("did not write: %v", err) + } + conn.Close() + } + }() + initialized = 1 + } + + fuzzdata = data + // Set up a connection to the server. + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10) + defer cancel() + conn, err := grpc.DialContext(ctx, "localhost:50051", grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + return 0 + } + defer conn.Close() + c := pb.NewGreeterClient(conn) + + // Contact the server and print out its response. + r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "world"}) + if err != nil { + return 0 + } + r.GetMessage() + return 1 +} diff --git a/projects/grpc-go/project.yaml b/projects/grpc-go/project.yaml new file mode 100644 index 000000000..10821f861 --- /dev/null +++ b/projects/grpc-go/project.yaml @@ -0,0 +1,11 @@ +homepage: "https://grpc.io/" +primary_contact: "menghanl@google.com" +auto_ccs: + - "dfawley@google.com" + - "p.antoine@catenacyber.fr" +language: go +fuzzing_engines: + - libfuzzer +sanitizers: + - address +main_repo: 'https://github.com/grpc/grpc-go'