Skip to content

Commit 6e9f803

Browse files
committed
rlp: stricter validation of canonical integer format
All integers (including size information in type tags) need to be encoded using the smallest possible encoding. This commit expands the stricter validation introduced for *big.Int in commit 59597d2 to all integer types and size tags.
1 parent 6788f95 commit 6e9f803

2 files changed

Lines changed: 93 additions & 42 deletions

File tree

‎rlp/decode.go‎

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ func (err *decodeError) Error() string {
109109
func wrapStreamError(err error, typ reflect.Type) error {
110110
switch err {
111111
case ErrCanonInt:
112-
return &decodeError{msg: "canon int error appends zero's", typ: typ}
112+
return &decodeError{msg: "non-canonical integer (leading zero bytes)", typ: typ}
113+
case ErrCanonSize:
114+
return &decodeError{msg: "non-canonical size information", typ: typ}
113115
case ErrExpectedList:
114116
return &decodeError{msg: "expected input list", typ: typ}
115117
case ErrExpectedString:
@@ -195,12 +197,10 @@ func decodeBigInt(s *Stream, val reflect.Value) error {
195197
i = new(big.Int)
196198
val.Set(reflect.ValueOf(i))
197199
}
198-
199-
// Reject big integers which are zero appended
200+
// Reject leading zero bytes
200201
if len(b) > 0 && b[0] == 0 {
201202
return wrapStreamError(ErrCanonInt, val.Type())
202203
}
203-
204204
i.SetBytes(b)
205205
return nil
206206
}
@@ -270,7 +270,7 @@ func decodeListSlice(s *Stream, val reflect.Value, elemdec decoder) error {
270270
func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error {
271271
size, err := s.List()
272272
if err != nil {
273-
return err
273+
return wrapStreamError(err, val.Type())
274274
}
275275
if size == 0 {
276276
zero(val, 0)
@@ -474,10 +474,11 @@ var (
474474
// has been reached during streaming.
475475
EOL = errors.New("rlp: end of list")
476476

477-
// Other errors
477+
// Actual Errors
478478
ErrExpectedString = errors.New("rlp: expected String or Byte")
479479
ErrExpectedList = errors.New("rlp: expected List")
480-
ErrCanonInt = errors.New("rlp: expected Int")
480+
ErrCanonInt = errors.New("rlp: non-canonical (leading zero bytes) integer")
481+
ErrCanonSize = errors.New("rlp: non-canonical size information")
481482
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
482483
ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")
483484

@@ -519,6 +520,7 @@ type Stream struct {
519520
kind Kind // kind of value ahead
520521
size uint64 // size of value ahead
521522
byteval byte // value of single byte in type tag
523+
kinderr error // error from last readKind
522524
stack []listpos
523525
}
524526

@@ -619,13 +621,21 @@ func (s *Stream) uint(maxbits int) (uint64, error) {
619621
}
620622
switch kind {
621623
case Byte:
624+
if s.byteval == 0 {
625+
return 0, ErrCanonInt
626+
}
622627
s.kind = -1 // rearm Kind
623628
return uint64(s.byteval), nil
624629
case String:
625630
if size > uint64(maxbits/8) {
626631
return 0, errUintOverflow
627632
}
628-
return s.readUint(byte(size))
633+
v, err := s.readUint(byte(size))
634+
if err == ErrCanonSize {
635+
// Adjust error because we're not reading a size right now.
636+
err = ErrCanonInt
637+
}
638+
return v, err
629639
default:
630640
return 0, ErrExpectedString
631641
}
@@ -729,6 +739,7 @@ func (s *Stream) Reset(r io.Reader, inputLimit uint64) {
729739
s.stack = s.stack[:0]
730740
s.size = 0
731741
s.kind = -1
742+
s.kinderr = nil
732743
if s.uintbuf == nil {
733744
s.uintbuf = make([]byte, 8)
734745
}
@@ -751,32 +762,31 @@ func (s *Stream) Kind() (kind Kind, size uint64, err error) {
751762
tos = &s.stack[len(s.stack)-1]
752763
}
753764
if s.kind < 0 {
765+
s.kinderr = nil
754766
// Don't read further if we're at the end of the
755767
// innermost list.
756768
if tos != nil && tos.pos == tos.size {
757769
return 0, 0, EOL
758770
}
759-
kind, size, err = s.readKind()
760-
if err != nil {
761-
return 0, 0, err
762-
}
763-
s.kind, s.size = kind, size
764-
}
765-
// Make sure size is reasonable. This is done always
766-
// so Kind returns the same error when called multiple times.
767-
if tos == nil {
768-
// At toplevel, check that the value is smaller
769-
// than the remaining input length.
770-
if s.limited && s.size > s.remaining {
771-
return 0, 0, ErrValueTooLarge
772-
}
773-
} else {
774-
// Inside a list, check that the value doesn't overflow the list.
775-
if s.size > tos.size-tos.pos {
776-
return 0, 0, ErrElemTooLarge
771+
s.kind, s.size, s.kinderr = s.readKind()
772+
if s.kinderr == nil {
773+
if tos == nil {
774+
// At toplevel, check that the value is smaller
775+
// than the remaining input length.
776+
if s.limited && s.size > s.remaining {
777+
s.kinderr = ErrValueTooLarge
778+
}
779+
} else {
780+
// Inside a list, check that the value doesn't overflow the list.
781+
if s.size > tos.size-tos.pos {
782+
s.kinderr = ErrElemTooLarge
783+
}
784+
}
777785
}
778786
}
779-
return s.kind, s.size, nil
787+
// Note: this might return a sticky error generated
788+
// by an earlier call to readKind.
789+
return s.kind, s.size, s.kinderr
780790
}
781791

782792
func (s *Stream) readKind() (kind Kind, size uint64, err error) {
@@ -805,6 +815,9 @@ func (s *Stream) readKind() (kind Kind, size uint64, err error) {
805815
// would be encoded as 0xB90400 followed by the string. The range of
806816
// the first byte is thus [0xB8, 0xBF].
807817
size, err = s.readUint(b - 0xB7)
818+
if err == nil && size < 56 {
819+
err = ErrCanonSize
820+
}
808821
return String, size, err
809822
case b < 0xF8:
810823
// If the total payload of a list
@@ -821,24 +834,40 @@ func (s *Stream) readKind() (kind Kind, size uint64, err error) {
821834
// the concatenation of the RLP encodings of the items. The
822835
// range of the first byte is thus [0xF8, 0xFF].
823836
size, err = s.readUint(b - 0xF7)
837+
if err == nil && size < 56 {
838+
err = ErrCanonSize
839+
}
824840
return List, size, err
825841
}
826842
}
827843

828844
func (s *Stream) readUint(size byte) (uint64, error) {
829-
if size == 1 {
845+
switch size {
846+
case 0:
847+
s.kind = -1 // rearm Kind
848+
return 0, nil
849+
case 1:
830850
b, err := s.readByte()
831851
if err == io.EOF {
832852
err = io.ErrUnexpectedEOF
833853
}
834854
return uint64(b), err
855+
default:
856+
start := int(8 - size)
857+
for i := 0; i < start; i++ {
858+
s.uintbuf[i] = 0
859+
}
860+
if err := s.readFull(s.uintbuf[start:]); err != nil {
861+
return 0, err
862+
}
863+
if s.uintbuf[start] == 0 {
864+
// Note: readUint is also used to decode integer
865+
// values. The error needs to be adjusted to become
866+
// ErrCanonInt in this case.
867+
return 0, ErrCanonSize
868+
}
869+
return binary.BigEndian.Uint64(s.uintbuf), nil
835870
}
836-
start := int(8 - size)
837-
for i := 0; i < start; i++ {
838-
s.uintbuf[i] = 0
839-
}
840-
err := s.readFull(s.uintbuf[start:])
841-
return binary.BigEndian.Uint64(s.uintbuf), err
842871
}
843872

844873
func (s *Stream) readFull(buf []byte) (err error) {

‎rlp/decode_test.go‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ func TestStreamKind(t *testing.T) {
2121
{"7F", Byte, 0},
2222
{"80", String, 0},
2323
{"B7", String, 55},
24-
{"B800", String, 0},
2524
{"B90400", String, 1024},
26-
{"BA000400", String, 1024},
27-
{"BB00000400", String, 1024},
2825
{"BFFFFFFFFFFFFFFFFF", String, ^uint64(0)},
2926
{"C0", List, 0},
3027
{"C8", List, 8},
3128
{"F7", List, 55},
32-
{"F800", List, 0},
33-
{"F804", List, 4},
3429
{"F90400", List, 1024},
3530
{"FFFFFFFFFFFFFFFFFF", List, ^uint64(0)},
3631
}
@@ -85,7 +80,7 @@ func TestStreamErrors(t *testing.T) {
8580
string
8681
calls
8782
newStream func([]byte) *Stream // uses bytes.Reader if nil
88-
error
83+
error error
8984
}{
9085
{"C0", calls{"Bytes"}, nil, ErrExpectedString},
9186
{"C0", calls{"Uint"}, nil, ErrExpectedString},
@@ -98,6 +93,21 @@ func TestStreamErrors(t *testing.T) {
9893
{"00", calls{"ListEnd"}, nil, errNotInList},
9994
{"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL},
10095

96+
// Leading zero bytes are rejected when reading integers.
97+
{"00", calls{"Uint"}, nil, ErrCanonInt},
98+
{"820002", calls{"Uint"}, nil, ErrCanonInt},
99+
100+
// Size tags must use the smallest possible encoding.
101+
// Leading zero bytes in the size tag are also rejected.
102+
{"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
103+
{"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
104+
{"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
105+
{"BA0002FFFF", calls{"Bytes"}, withoutInputLimit, ErrCanonSize},
106+
{"F800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
107+
{"F90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
108+
{"F90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
109+
{"FA0002FFFF", calls{"List"}, withoutInputLimit, ErrCanonSize},
110+
101111
// Expected EOF
102112
{"", calls{"Kind"}, nil, io.EOF},
103113
{"", calls{"Uint"}, nil, io.EOF},
@@ -170,10 +180,12 @@ testfor:
170180
want = test.error.Error()
171181
}
172182
if err != want {
183+
t.Log(test)
173184
t.Errorf("test %d: last call (%s) error mismatch\ngot: %s\nwant: %s",
174185
i, call, err, test.error)
175186
}
176187
} else if err != "<nil>" {
188+
t.Log(test)
177189
t.Errorf("test %d: call %d (%s) unexpected error: %q", i, j, call, err)
178190
continue testfor
179191
}
@@ -289,15 +301,20 @@ var decodeTests = []decodeTest{
289301
{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
290302
{input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"},
291303
{input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"},
304+
{input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
305+
{input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
306+
{input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
292307

293308
// slices
294309
{input: "C0", ptr: new([]uint), value: []uint{}},
295310
{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
311+
{input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"},
296312

297313
// arrays
298314
{input: "C0", ptr: new([5]uint), value: [5]uint{}},
299315
{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
300316
{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
317+
{input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"},
301318

302319
// byte slices
303320
{input: "01", ptr: new([]byte), value: []byte{1}},
@@ -352,7 +369,7 @@ var decodeTests = []decodeTest{
352369
// big ints
353370
{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
354371
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
355-
{input: "820001", ptr: new(big.Int), error: "rlp: canon int error appends zero's for *big.Int"},
372+
{input: "820001", ptr: new(big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
356373
{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
357374
{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
358375

@@ -366,6 +383,11 @@ var decodeTests = []decodeTest{
366383
value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
367384
},
368385

386+
{
387+
input: "83222222",
388+
ptr: new(simplestruct),
389+
error: "rlp: expected input list for rlp.simplestruct",
390+
},
369391
{
370392
input: "C3010101",
371393
ptr: new(simplestruct),
@@ -378,7 +400,7 @@ var decodeTests = []decodeTest{
378400
},
379401

380402
// pointers
381-
{input: "00", ptr: new(*uint), value: uintp(0)},
403+
{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
382404
{input: "80", ptr: new(*uint), value: (*uint)(nil)},
383405
{input: "C0", ptr: new(*uint), value: (*uint)(nil)},
384406
{input: "07", ptr: new(*uint), value: uintp(7)},

0 commit comments

Comments
 (0)