Skip to content

Commit a2260b9

Browse files
committed
Return error when parsing packet with fewer bytes than TKL field indicates
This fix solves a panic which is raised when parsing a packet which indicates a non-zero token length (TKL) but the packet itself is shorter than the indicated token length. In such a case, a "truncated" error is return from UnmarshalBinary. The bug was found with the help of go-fuzz (https://github.com/dvyukov/go-fuzz).
1 parent 2ee4122 commit a2260b9

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

message.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ func (m *Message) UnmarshalBinary(data []byte) error {
573573
if tokenLen > 0 {
574574
m.Token = make([]byte, tokenLen)
575575
}
576+
if len(data) < 4+tokenLen {
577+
return errors.New("truncated")
578+
}
576579
copy(m.Token, data[4:4+tokenLen])
577580
b := data[4+tokenLen:]
578581
prev := 0

message_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ func TestInvalidMessageParsing(t *testing.T) {
185185
if err == nil {
186186
t.Errorf("Unexpected success parsing invalid message: %v", msg)
187187
}
188+
189+
// TKL=5 but packet is truncated
190+
msg, err = parseMessage([]byte{0x45, 0, 0, 0, 0, 0})
191+
if err == nil {
192+
t.Errorf("Unexpected success parsing invalid message: %v", msg)
193+
}
188194
}
189195

190196
func TestOptionsWithIllegalLengthAreIgnoredDuringParsing(t *testing.T) {

0 commit comments

Comments
 (0)