Skip to content

Commit 4a305be

Browse files
ianlancetaylorgopherbot
authored andcommitted
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 golang#47653 Fixes golang#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]>
1 parent 34d6aaa commit 4a305be

File tree

1 file changed

+6
-30
lines changed

1 file changed

+6
-30
lines changed

src/debug/buildinfo/buildinfo.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/binary"
2020
"errors"
2121
"fmt"
22+
"internal/saferio"
2223
"internal/xcoff"
2324
"io"
2425
"io/fs"
@@ -260,12 +261,7 @@ func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) {
260261
if n > size {
261262
n = size
262263
}
263-
data := make([]byte, n)
264-
_, err := prog.ReadAt(data, int64(addr-prog.Vaddr))
265-
if err != nil {
266-
return nil, err
267-
}
268-
return data, nil
264+
return saferio.ReadDataAt(prog, n, int64(addr-prog.Vaddr))
269265
}
270266
}
271267
return nil, errUnrecognizedFormat
@@ -308,12 +304,7 @@ func (x *peExe) ReadData(addr, size uint64) ([]byte, error) {
308304
if n > size {
309305
n = size
310306
}
311-
data := make([]byte, n)
312-
_, err := sect.ReadAt(data, int64(addr-uint64(sect.VirtualAddress)))
313-
if err != nil {
314-
return nil, errUnrecognizedFormat
315-
}
316-
return data, nil
307+
return saferio.ReadDataAt(sect, n, int64(addr-uint64(sect.VirtualAddress)))
317308
}
318309
}
319310
return nil, errUnrecognizedFormat
@@ -360,12 +351,7 @@ func (x *machoExe) ReadData(addr, size uint64) ([]byte, error) {
360351
if n > size {
361352
n = size
362353
}
363-
data := make([]byte, n)
364-
_, err := seg.ReadAt(data, int64(addr-seg.Addr))
365-
if err != nil {
366-
return nil, err
367-
}
368-
return data, nil
354+
return saferio.ReadDataAt(seg, n, int64(addr-seg.Addr))
369355
}
370356
}
371357
return nil, errUnrecognizedFormat
@@ -401,12 +387,7 @@ func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) {
401387
if n > size {
402388
n = size
403389
}
404-
data := make([]byte, n)
405-
_, err := sect.ReadAt(data, int64(addr-sect.VirtualAddress))
406-
if err != nil {
407-
return nil, err
408-
}
409-
return data, nil
390+
return saferio.ReadDataAt(sect, n, int64(addr-sect.VirtualAddress))
410391
}
411392
}
412393
return nil, errors.New("address not mapped")
@@ -438,12 +419,7 @@ func (x *plan9objExe) ReadData(addr, size uint64) ([]byte, error) {
438419
if n > size {
439420
n = size
440421
}
441-
data := make([]byte, n)
442-
_, err := sect.ReadAt(data, int64(addr-uint64(sect.Offset)))
443-
if err != nil {
444-
return nil, err
445-
}
446-
return data, nil
422+
return saferio.ReadDataAt(sect, n, int64(addr-uint64(sect.Offset)))
447423
}
448424
}
449425
return nil, errors.New("address not mapped")

0 commit comments

Comments
 (0)