Skip to content

Commit dd49c8e

Browse files
committed
rlp: fix list bounds check overflow (found by go-fuzz)
The list size checking overflowed if the size information for a value was bigger than the list. This is resolved by always performing the check before reading.
1 parent b6ec1c7 commit dd49c8e

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

‎rlp/decode.go‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,16 @@ func (s *Stream) Kind() (kind Kind, size uint64, err error) {
820820
func (s *Stream) readKind() (kind Kind, size uint64, err error) {
821821
b, err := s.readByte()
822822
if err != nil {
823+
if len(s.stack) == 0 {
824+
// At toplevel, Adjust the error to actual EOF. io.EOF is
825+
// used by callers to determine when to stop decoding.
826+
switch err {
827+
case io.ErrUnexpectedEOF:
828+
err = io.EOF
829+
case ErrValueTooLarge:
830+
err = io.EOF
831+
}
832+
}
823833
return 0, 0, err
824834
}
825835
s.byteval = 0
@@ -876,9 +886,6 @@ func (s *Stream) readUint(size byte) (uint64, error) {
876886
return 0, nil
877887
case 1:
878888
b, err := s.readByte()
879-
if err == io.EOF {
880-
err = io.ErrUnexpectedEOF
881-
}
882889
return uint64(b), err
883890
default:
884891
start := int(8 - size)
@@ -899,10 +906,9 @@ func (s *Stream) readUint(size byte) (uint64, error) {
899906
}
900907

901908
func (s *Stream) readFull(buf []byte) (err error) {
902-
if s.limited && s.remaining < uint64(len(buf)) {
903-
return ErrValueTooLarge
909+
if err := s.willRead(uint64(len(buf))); err != nil {
910+
return err
904911
}
905-
s.willRead(uint64(len(buf)))
906912
var nn, n int
907913
for n < len(buf) && err == nil {
908914
nn, err = s.r.Read(buf[n:])
@@ -915,23 +921,32 @@ func (s *Stream) readFull(buf []byte) (err error) {
915921
}
916922

917923
func (s *Stream) readByte() (byte, error) {
918-
if s.limited && s.remaining == 0 {
919-
return 0, io.EOF
924+
if err := s.willRead(1); err != nil {
925+
return 0, err
920926
}
921-
s.willRead(1)
922927
b, err := s.r.ReadByte()
923-
if len(s.stack) > 0 && err == io.EOF {
928+
if err == io.EOF {
924929
err = io.ErrUnexpectedEOF
925930
}
926931
return b, err
927932
}
928933

929-
func (s *Stream) willRead(n uint64) {
934+
func (s *Stream) willRead(n uint64) error {
930935
s.kind = -1 // rearm Kind
931-
if s.limited {
932-
s.remaining -= n
933-
}
936+
934937
if len(s.stack) > 0 {
938+
// check list overflow
939+
tos := s.stack[len(s.stack)-1]
940+
if n > tos.size-tos.pos {
941+
return ErrElemTooLarge
942+
}
935943
s.stack[len(s.stack)-1].pos += n
936944
}
945+
if s.limited {
946+
if n > s.remaining {
947+
return ErrValueTooLarge
948+
}
949+
s.remaining -= n
950+
}
951+
return nil
937952
}

‎rlp/decode_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func TestStreamErrors(t *testing.T) {
119119
{"8158", calls{"Uint", "Uint"}, nil, io.EOF},
120120
{"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF},
121121

122+
{"", calls{"List"}, withoutInputLimit, io.EOF},
123+
{"8158", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF},
124+
{"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF},
125+
122126
// Input limit errors.
123127
{"81", calls{"Bytes"}, nil, ErrValueTooLarge},
124128
{"81", calls{"Uint"}, nil, ErrValueTooLarge},
@@ -426,6 +430,13 @@ var decodeTests = []decodeTest{
426430
ptr: new([]io.Reader),
427431
error: "rlp: type io.Reader is not RLP-serializable",
428432
},
433+
434+
// fuzzer crashes
435+
{
436+
input: "c330f9c030f93030ce3030303030303030bd303030303030",
437+
ptr: new(interface{}),
438+
error: "rlp: element is larger than containing list",
439+
},
429440
}
430441

431442
func uintp(i uint) *uint { return &i }

0 commit comments

Comments
 (0)