1
0
mirror of https://github.com/golang/go synced 2024-11-17 15:54:39 -07:00

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>
This commit is contained in:
Ian Lance Taylor 2023-03-06 11:39:22 -08:00 committed by Gopher Robot
parent 34d6aaae29
commit 4a305be946

View File

@ -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")