Skip to content

Commit 055113e

Browse files
math/big: check buffer lengths in GobDecode
In Float.GobDecode and Rat.GobDecode, check buffer sizes before indexing slices. Fixes #53871 Change-Id: I1b652c32c2bc7a0e8aa7620f7be9b2740c568b0a Reviewed-on: https://go-review.googlesource.com/c/go/+/417774 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Tatiana Bradley <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]>
1 parent 4248146 commit 055113e

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/math/big/floatmarsh.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package big
88

99
import (
1010
"encoding/binary"
11+
"errors"
1112
"fmt"
1213
)
1314

@@ -67,6 +68,9 @@ func (z *Float) GobDecode(buf []byte) error {
6768
*z = Float{}
6869
return nil
6970
}
71+
if len(buf) < 6 {
72+
return errors.New("Float.GobDecode: buffer too small")
73+
}
7074

7175
if buf[0] != floatGobVersion {
7276
return fmt.Errorf("Float.GobDecode: encoding version %d not supported", buf[0])
@@ -83,6 +87,9 @@ func (z *Float) GobDecode(buf []byte) error {
8387
z.prec = binary.BigEndian.Uint32(buf[2:])
8488

8589
if z.form == finite {
90+
if len(buf) < 10 {
91+
return errors.New("Float.GobDecode: buffer too small for finite form float")
92+
}
8693
z.exp = int32(binary.BigEndian.Uint32(buf[6:]))
8794
z.mant = z.mant.setBytes(buf[10:])
8895
}

src/math/big/floatmarsh_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,15 @@ func TestFloatJSONEncoding(t *testing.T) {
137137
}
138138
}
139139
}
140+
141+
func TestFloatGobDecodeShortBuffer(t *testing.T) {
142+
for _, tc := range [][]byte{
143+
[]byte{0x1, 0x0, 0x0, 0x0},
144+
[]byte{0x1, 0xfa, 0x0, 0x0, 0x0, 0x0},
145+
} {
146+
err := NewFloat(0).GobDecode(tc)
147+
if err == nil {
148+
t.Error("expected GobDecode to return error for malformed input")
149+
}
150+
}
151+
}

src/math/big/ratmarsh.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ func (z *Rat) GobDecode(buf []byte) error {
4545
*z = Rat{}
4646
return nil
4747
}
48+
if len(buf) < 5 {
49+
return errors.New("Rat.GobDecode: buffer too small")
50+
}
4851
b := buf[0]
4952
if b>>1 != ratGobVersion {
5053
return fmt.Errorf("Rat.GobDecode: encoding version %d not supported", b>>1)
5154
}
5255
const j = 1 + 4
5356
i := j + binary.BigEndian.Uint32(buf[j-4:j])
57+
if len(buf) < int(i) {
58+
return errors.New("Rat.GobDecode: buffer too small")
59+
}
5460
z.a.neg = b&1 != 0
5561
z.a.abs = z.a.abs.setBytes(buf[j:i])
5662
z.b.abs = z.b.abs.setBytes(buf[i:])

src/math/big/ratmarsh_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,15 @@ func TestRatXMLEncoding(t *testing.T) {
123123
}
124124
}
125125
}
126+
127+
func TestRatGobDecodeShortBuffer(t *testing.T) {
128+
for _, tc := range [][]byte{
129+
[]byte{0x2},
130+
[]byte{0x2, 0x0, 0x0, 0x0, 0xff},
131+
} {
132+
err := NewRat(1, 2).GobDecode(tc)
133+
if err == nil {
134+
t.Error("expected GobDecode to return error for malformed input")
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)