From 703c8ab7e5ba75c95553d4e249309297abad7102 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Fri, 15 Jul 2022 10:43:44 -0700 Subject: [release-branch.go1.17] math/big: check buffer lengths in GobDecode In Float.GobDecode and Rat.GobDecode, check buffer sizes before indexing slices. Updates #53871 Fixes #54094 Change-Id: I1b652c32c2bc7a0e8aa7620f7be9b2740c568b0a Reviewed-on: https://go-review.googlesource.com/c/go/+/417774 TryBot-Result: Gopher Robot Reviewed-by: Tatiana Bradley Run-TryBot: Roland Shoemaker (cherry picked from commit 055113ef364337607e3e72ed7d48df67fde6fc66) Reviewed-on: https://go-review.googlesource.com/c/go/+/419814 Reviewed-by: Julie Qiu --- src/math/big/ratmarsh.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/math/big/ratmarsh.go') diff --git a/src/math/big/ratmarsh.go b/src/math/big/ratmarsh.go index fbc7b6002d..56102e845b 100644 --- a/src/math/big/ratmarsh.go +++ b/src/math/big/ratmarsh.go @@ -45,12 +45,18 @@ func (z *Rat) GobDecode(buf []byte) error { *z = Rat{} return nil } + if len(buf) < 5 { + return errors.New("Rat.GobDecode: buffer too small") + } b := buf[0] if b>>1 != ratGobVersion { return fmt.Errorf("Rat.GobDecode: encoding version %d not supported", b>>1) } const j = 1 + 4 i := j + binary.BigEndian.Uint32(buf[j-4:j]) + if len(buf) < int(i) { + return errors.New("Rat.GobDecode: buffer too small") + } z.a.neg = b&1 != 0 z.a.abs = z.a.abs.setBytes(buf[j:i]) z.b.abs = z.b.abs.setBytes(buf[i:]) -- cgit v1.2.3-54-g00ecf