From 187c4bb60a0033f08a1193bdab13fe2747956611 Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Wed, 14 Aug 2019 18:50:16 +0200 Subject: [PATCH] pgpencryptor: potentially fix crash on encyptor close We seem to be hitting a bug where the encryptor doesn't initialize because of a lacking gpg binary, and then crashes on .Close(). This should fix the issue, but is untested. goroutine 70 [running]: code.hackerspace.pl/hscloud/bgpwtf/cccampix/pgpencryptor/gpg.(*CLIEncryptor).Close(0x0) bgpwtf/cccampix/pgpencryptor/gpg/gpg.go:144 +0x22 main.(*service).Encrypt(0xc000345e00, 0x16d13a0, 0xc00047f260, 0x1688400, 0xc00003d4a0) bgpwtf/cccampix/pgpencryptor/main.go:132 +0x6f9 code.hackerspace.pl/hscloud/bgpwtf/cccampix/proto._PGPEncryptor_Encrypt_Handler(0x133bf00, 0xc000345e00, 0x16c6300, 0xc0000d6000, 0x2247b78, 0xc0001f8000) bazel-out/k8-fastbuild/bin/bgpwtf/cccampix/proto/linux_amd64_stripped/ix_go_proto%/code.hackerspace.pl/hscloud/bgpwtf/cccampix/proto/ix.pb.go:1816 +0xad google.golang.org/grpc.(*Server).processStreamingRPC(0xc000160c00, 0x16d6ce0, 0xc000161500, 0xc0001f8000, 0xc0004244e0, 0x21b00e0, 0xc0000c6ff0, 0x0, 0x0) external/org_golang_google_grpc/server.go:1175 +0xacd google.golang.org/grpc.(*Server).handleStream(0xc000160c00, 0x16d6ce0, 0xc000161500, 0xc0001f8000, 0xc0000c6ff0) external/org_golang_google_grpc/server.go:1254 +0xcbe google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc000404770, 0xc000160c00, 0x16d6ce0, 0xc000161500, 0xc0001f8000) external/org_golang_google_grpc/server.go:690 +0x9f created by google.golang.org/grpc.(*Server).serveStreams.func1 external/org_golang_google_grpc/server.go:688 +0xa1 created by google.golang.org/grpc.(*Server).serveStreams.func1 external/org_golang_google_grpc/server.go:688 +0xa1 Change-Id: Idd167a120e157005f44d255a61ef13dc80e8eeed --- bgpwtf/cccampix/pgpencryptor/gpg/gpg.go | 12 +++++++++--- bgpwtf/cccampix/pgpencryptor/main.go | 10 +++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/bgpwtf/cccampix/pgpencryptor/gpg/gpg.go b/bgpwtf/cccampix/pgpencryptor/gpg/gpg.go index 09fcc7fa..0c0792bb 100644 --- a/bgpwtf/cccampix/pgpencryptor/gpg/gpg.go +++ b/bgpwtf/cccampix/pgpencryptor/gpg/gpg.go @@ -141,9 +141,15 @@ func (encryptor *CLIEncryptor) Finish() { } func (encryptor *CLIEncryptor) Close() { - encryptor.stdout.Close() - encryptor.stderr.Close() - encryptor.stdin.Close() + if encryptor.stdout != nil { + encryptor.stdout.Close() + } + if encryptor.stderr != nil { + encryptor.stderr.Close() + } + if encryptor.stdin != nil { + encryptor.stdin.Close() + } os.RemoveAll(encryptor.tempDir) } diff --git a/bgpwtf/cccampix/pgpencryptor/main.go b/bgpwtf/cccampix/pgpencryptor/main.go index 3d73e01d..87338b5e 100644 --- a/bgpwtf/cccampix/pgpencryptor/main.go +++ b/bgpwtf/cccampix/pgpencryptor/main.go @@ -3,10 +3,6 @@ package main import ( "context" "flag" - "github.com/golang/glog" - "github.com/lib/pq" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "io" "time" @@ -15,6 +11,10 @@ import ( "code.hackerspace.pl/hscloud/bgpwtf/cccampix/pgpencryptor/model" pb "code.hackerspace.pl/hscloud/bgpwtf/cccampix/proto" "code.hackerspace.pl/hscloud/go/mirko" + "github.com/golang/glog" + "github.com/lib/pq" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) var ( @@ -126,11 +126,11 @@ func (s *service) Encrypt(stream pb.PGPEncryptor_EncryptServer) error { senderDone := make(chan struct{}) errors := make(chan error) enc, err := s.encryptorFactory.Get(key.Fingerprint, key.KeyData) - defer enc.Close() if err != nil { return status.Errorf(codes.Unavailable, "PGPEncryptor error while creating encryptor: %v", err) } + defer enc.Close() err = enc.WritePlainText(initialMessage.Data) if err != nil {