Skip to content

Commit

Permalink
tlv: fix panic with large length
Browse files Browse the repository at this point in the history
This commit fixes a panic where a large length in a record could
cause the DVarBytes function to fail to allocate a byte slice.
  • Loading branch information
Crypt-iQ authored and cfromknecht committed Aug 21, 2019
1 parent c4ba557 commit dfd1b38
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tlv/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"io"
"io/ioutil"
"math"

"github.com/lightningnetwork/lnd/lnwire"
)

// ErrStreamNotCanonical signals that a decoded stream does not contain records
// sorting by monotonically-increasing type.
var ErrStreamNotCanonical = errors.New("tlv stream is not canonical")

// ErrRecordTooLarge signals that a decoded record has a length that is too
// long to parse.
var ErrRecordTooLarge = errors.New("record is too large")

// ErrUnknownRequiredType is an error returned when decoding an unknown and even
// type from a Stream.
type ErrUnknownRequiredType Type
Expand Down Expand Up @@ -183,6 +189,10 @@ func (s *Stream) Decode(r io.Reader) error {
return err
}

if length > lnwire.MaxMessagePayload {
return ErrRecordTooLarge
}

// Search the records known to the stream for this type. We'll
// begin the search and recordIdx and walk forward until we find
// it or the next record's type is larger.
Expand Down

0 comments on commit dfd1b38

Please sign in to comment.