Skip to content

Commit 2750ec4

Browse files
committed
rlp: fix integer overflow in list element size validation
It is not safe to add anything to s.size.
1 parent 56a4810 commit 2750ec4

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

‎rlp/decode.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ func (s *Stream) Kind() (kind Kind, size uint64, err error) {
751751
tos = &s.stack[len(s.stack)-1]
752752
}
753753
if s.kind < 0 {
754-
// don't read further if we're at the end of the
754+
// Don't read further if we're at the end of the
755755
// innermost list.
756756
if tos != nil && tos.pos == tos.size {
757757
return 0, 0, EOL
@@ -772,7 +772,7 @@ func (s *Stream) Kind() (kind Kind, size uint64, err error) {
772772
}
773773
} else {
774774
// Inside a list, check that the value doesn't overflow the list.
775-
if tos.pos+s.size > tos.size {
775+
if s.size > tos.size-tos.pos {
776776
return 0, 0, ErrElemTooLarge
777777
}
778778
}

‎rlp/decode_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func TestStreamErrors(t *testing.T) {
112112
{"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge},
113113
{"C801", calls{"List"}, nil, ErrValueTooLarge},
114114

115+
// Test for list element size check overflow.
116+
{"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge},
117+
115118
// Test for input limit overflow. Since we are counting the limit
116119
// down toward zero in Stream.remaining, reading too far can overflow
117120
// remaining to a large value, effectively disabling the limit.

0 commit comments

Comments
 (0)