Skip to content

Commit bd6ed23

Browse files
committed
accounts/abi: harden unpacking against malicious input
1 parent 08c5d4d commit bd6ed23

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎accounts/abi/unpack.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func readFixedBytes(t Type, word []byte) (interface{}, error) {
9595

9696
// iteratively unpack elements
9797
func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) {
98+
if size < 0 {
99+
return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size)
100+
}
98101
if start+32*size > len(output) {
99102
return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size)
100103
}
@@ -181,16 +184,22 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
181184

182185
// interprets a 32 byte slice as an offset and then determines which indice to look to decode the type.
183186
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
184-
offset := int(binary.BigEndian.Uint64(output[index+24 : index+32]))
187+
offsetBig := big.NewInt(0).SetBytes(output[index : index+32])
188+
if !offsetBig.IsInt64() {
189+
return 0, 0, fmt.Errorf("abi offset larger than int64: %v", offsetBig)
190+
}
191+
offset := int(offsetBig.Int64())
185192
if offset+32 > len(output) {
186193
return 0, 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %d would go over slice boundary (len=%d)", len(output), offset+32)
187194
}
188-
length = int(binary.BigEndian.Uint64(output[offset+24 : offset+32]))
195+
lengthBig := big.NewInt(0).SetBytes(output[offset : offset+32])
196+
if !lengthBig.IsInt64() {
197+
return 0, 0, fmt.Errorf("abi length larger than int64: %v", lengthBig)
198+
}
199+
length = int(lengthBig.Int64())
189200
if offset+32+length > len(output) {
190201
return 0, 0, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32+length)
191202
}
192203
start = offset + 32
193-
194-
//fmt.Printf("LENGTH PREFIX INFO: \nsize: %v\noffset: %v\nstart: %v\n", length, offset, start)
195204
return
196205
}

‎accounts/abi/unpack_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,73 @@ func TestUnmarshal(t *testing.T) {
683683
t.Fatal("expected error:", err)
684684
}
685685
}
686+
687+
func TestOOMMaliciousInput(t *testing.T) {
688+
oomTests := []unpackTest{
689+
{
690+
def: `[{"type": "uint8[]"}]`,
691+
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
692+
"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
693+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
694+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
695+
},
696+
{ // Length larger than 64 bits
697+
def: `[{"type": "uint8[]"}]`,
698+
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
699+
"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
700+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
701+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
702+
},
703+
{ // Offset very large (over 64 bits)
704+
def: `[{"type": "uint8[]"}]`,
705+
enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
706+
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
707+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
708+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
709+
},
710+
{ // Offset very large (below 64 bits)
711+
def: `[{"type": "uint8[]"}]`,
712+
enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
713+
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
714+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
715+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
716+
},
717+
{ // Offset negative (as 64 bit)
718+
def: `[{"type": "uint8[]"}]`,
719+
enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
720+
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
721+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
722+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
723+
},
724+
725+
{ // Negative length
726+
def: `[{"type": "uint8[]"}]`,
727+
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
728+
"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
729+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
730+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
731+
},
732+
{ // Very large length
733+
def: `[{"type": "uint8[]"}]`,
734+
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
735+
"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
736+
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
737+
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
738+
},
739+
}
740+
for i, test := range oomTests {
741+
def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
742+
abi, err := JSON(strings.NewReader(def))
743+
if err != nil {
744+
t.Fatalf("invalid ABI definition %s: %v", def, err)
745+
}
746+
encb, err := hex.DecodeString(test.enc)
747+
if err != nil {
748+
t.Fatalf("invalid hex: %s" + test.enc)
749+
}
750+
_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
751+
if err == nil {
752+
t.Fatalf("Expected error on malicious input, test %d", i)
753+
}
754+
}
755+
}

0 commit comments

Comments
 (0)