diff options
author | Ian Lance Taylor <iant@golang.org> | 2023-03-06 11:39:22 -0800 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-03-06 19:55:57 +0000 |
commit | 4a305be9469f340404f63298fce2575326ceaf99 (patch) | |
tree | ee6b26825eab7d838ae21fc6ec9884fc0afc56cb /src/debug | |
parent | 34d6aaae29023b9edfef2d3360d89e48c8dd118f (diff) | |
download | go-4a305be9469f340404f63298fce2575326ceaf99.tar.gz go-4a305be9469f340404f63298fce2575326ceaf99.zip |
debug/buildinfo: use saferio in ReadData methods
This avoids a very large memory allocation if corrupt data says that
we need to read a very long string.
No test case because the problem can only happen for invalid data. Let
the fuzzer find cases like this.
For #47653
Fixes #58886
Change-Id: I4e80ba62a6416d010c8804e4f49ae81bdafaadb8
Reviewed-on: https://go-review.googlesource.com/c/go/+/473657
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/debug')
-rw-r--r-- | src/debug/buildinfo/buildinfo.go | 36 |
1 files changed, 6 insertions, 30 deletions
diff --git a/src/debug/buildinfo/buildinfo.go b/src/debug/buildinfo/buildinfo.go index 8bc5753a2d..a7019a666e 100644 --- a/src/debug/buildinfo/buildinfo.go +++ b/src/debug/buildinfo/buildinfo.go @@ -19,6 +19,7 @@ import ( "encoding/binary" "errors" "fmt" + "internal/saferio" "internal/xcoff" "io" "io/fs" @@ -260,12 +261,7 @@ func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) { if n > size { n = size } - data := make([]byte, n) - _, err := prog.ReadAt(data, int64(addr-prog.Vaddr)) - if err != nil { - return nil, err - } - return data, nil + return saferio.ReadDataAt(prog, n, int64(addr-prog.Vaddr)) } } return nil, errUnrecognizedFormat @@ -308,12 +304,7 @@ func (x *peExe) ReadData(addr, size uint64) ([]byte, error) { if n > size { n = size } - data := make([]byte, n) - _, err := sect.ReadAt(data, int64(addr-uint64(sect.VirtualAddress))) - if err != nil { - return nil, errUnrecognizedFormat - } - return data, nil + return saferio.ReadDataAt(sect, n, int64(addr-uint64(sect.VirtualAddress))) } } return nil, errUnrecognizedFormat @@ -360,12 +351,7 @@ func (x *machoExe) ReadData(addr, size uint64) ([]byte, error) { if n > size { n = size } - data := make([]byte, n) - _, err := seg.ReadAt(data, int64(addr-seg.Addr)) - if err != nil { - return nil, err - } - return data, nil + return saferio.ReadDataAt(seg, n, int64(addr-seg.Addr)) } } return nil, errUnrecognizedFormat @@ -401,12 +387,7 @@ func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) { if n > size { n = size } - data := make([]byte, n) - _, err := sect.ReadAt(data, int64(addr-sect.VirtualAddress)) - if err != nil { - return nil, err - } - return data, nil + return saferio.ReadDataAt(sect, n, int64(addr-sect.VirtualAddress)) } } return nil, errors.New("address not mapped") @@ -438,12 +419,7 @@ func (x *plan9objExe) ReadData(addr, size uint64) ([]byte, error) { if n > size { n = size } - data := make([]byte, n) - _, err := sect.ReadAt(data, int64(addr-uint64(sect.Offset))) - if err != nil { - return nil, err - } - return data, nil + return saferio.ReadDataAt(sect, n, int64(addr-uint64(sect.Offset))) } } return nil, errors.New("address not mapped") |