Skip to content

Commit

Permalink
rlp: fix integer overflow in list element size validation
Browse files Browse the repository at this point in the history
It is not safe to add anything to s.size.
  • Loading branch information
fjl committed Apr 17, 2015
1 parent 56a4810 commit 2750ec4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ func (s *Stream) Kind() (kind Kind, size uint64, err error) {
tos = &s.stack[len(s.stack)-1]
}
if s.kind < 0 {
// don't read further if we're at the end of the
// Don't read further if we're at the end of the
// innermost list.
if tos != nil && tos.pos == tos.size {
return 0, 0, EOL
Expand All @@ -772,7 +772,7 @@ func (s *Stream) Kind() (kind Kind, size uint64, err error) {
}
} else {
// Inside a list, check that the value doesn't overflow the list.
if tos.pos+s.size > tos.size {
if s.size > tos.size-tos.pos {
return 0, 0, ErrElemTooLarge
}
}
Expand Down
3 changes: 3 additions & 0 deletions rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func TestStreamErrors(t *testing.T) {
{"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge},
{"C801", calls{"List"}, nil, ErrValueTooLarge},

// Test for list element size check overflow.
{"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge},

// Test for input limit overflow. Since we are counting the limit
// down toward zero in Stream.remaining, reading too far can overflow
// remaining to a large value, effectively disabling the limit.
Expand Down

0 comments on commit 2750ec4

Please sign in to comment.