Skip to content

Commit cb32d3b

Browse files
mareteagl
authored andcommitted
openpgp: Fix panic on opaque subpackets with length 0.
Some invalid input may be parsed so that the length of an opaque subpacket turns out to be 0. In such cases, arrange for a StructuralError to be returned indicating truncation. Found using gofuzz. Fixes golang/go#11503 Change-Id: Ib9ce8c604f35a31f852adfcd56a22dfd143a9443 Reviewed-on: https://go-review.googlesource.com/12634 Reviewed-by: Adam Langley <agl@golang.org>
1 parent 7d5b0be commit cb32d3b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

openpgp/packet/opaque.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ package packet
66

77
import (
88
"bytes"
9-
"golang.org/x/crypto/openpgp/errors"
109
"io"
1110
"io/ioutil"
11+
12+
"golang.org/x/crypto/openpgp/errors"
1213
)
1314

1415
// OpaquePacket represents an OpenPGP packet as raw, unparsed data. This is
@@ -138,7 +139,7 @@ func nextSubpacket(contents []byte) (subHeaderLen int, subPacket *OpaqueSubpacke
138139
uint32(contents[4])
139140
contents = contents[5:]
140141
}
141-
if subLen > uint32(len(contents)) {
142+
if subLen > uint32(len(contents)) || subLen == 0 {
142143
goto Truncated
143144
}
144145
subPacket.SubType = contents[0]

openpgp/read_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"bytes"
99
_ "crypto/sha512"
1010
"encoding/hex"
11-
"golang.org/x/crypto/openpgp/errors"
1211
"io"
1312
"io/ioutil"
1413
"strings"
1514
"testing"
15+
16+
"golang.org/x/crypto/openpgp/errors"
1617
)
1718

1819
func readerFromHex(s string) io.Reader {
@@ -368,6 +369,29 @@ func TestNoArmoredData(t *testing.T) {
368369
}
369370
}
370371

372+
func TestIssue11503(t *testing.T) {
373+
data := "8c040402000aa430aa8228b9248b01fc899a91197130303030"
374+
375+
buf, err := hex.DecodeString(data)
376+
if err != nil {
377+
t.Errorf("hex.DecodeSting(): %v", err)
378+
}
379+
380+
kr, err := ReadKeyRing(new(bytes.Buffer))
381+
if err != nil {
382+
t.Errorf("ReadKeyring(): %v", err)
383+
}
384+
385+
_, err = ReadMessage(bytes.NewBuffer(buf), kr,
386+
func([]Key, bool) ([]byte, error) {
387+
return []byte("insecure"), nil
388+
}, nil)
389+
390+
if err == nil {
391+
t.Errorf("ReadMessage(): Unexpected nil error")
392+
}
393+
}
394+
371395
const testKey1KeyId = 0xA34D7E18C20C31BB
372396
const testKey3KeyId = 0x338934250CCC0360
373397

0 commit comments

Comments
 (0)