Skip to content

Commit

Permalink
debug/buildinfo: use saferio in ReadData methods
Browse files Browse the repository at this point in the history
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 <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Mar 6, 2023
1 parent 34d6aaa commit 4a305be
Showing 1 changed file with 6 additions and 30 deletions.
36 changes: 6 additions & 30 deletions src/debug/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"internal/saferio"
"internal/xcoff"
"io"
"io/fs"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 4a305be

Please sign in to comment.