]> Cypherpunks repositories - gostls13.git/commitdiff
debug/macho: use saferio to read segment and section data
authorDan Kortschak <dan@kortschak.io>
Wed, 24 Aug 2022 12:30:11 +0000 (22:00 +0930)
committerGopher Robot <gobot@golang.org>
Fri, 26 Aug 2022 17:44:36 +0000 (17:44 +0000)
Avoid allocating large amounts of memory for corrupt input.

No test case because the problem can only happen for invalid data.
Let the fuzzer find cases like this.

Change-Id: Ib09d5fea54aabcb6941e541b42689222fba69632
Reviewed-on: https://go-review.googlesource.com/c/go/+/425303
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Dan Kortschak <dan@kortschak.io>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: hopehook <hopehook@qq.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/debug/macho/file.go

index b57dba8496bf6978ed382e313c137a9ba96eab24..e6b170a7cdb78f8483315dcc94ea1da49608fa96 100644 (file)
@@ -13,6 +13,7 @@ import (
        "debug/dwarf"
        "encoding/binary"
        "fmt"
+       "internal/saferio"
        "io"
        "os"
        "strings"
@@ -73,12 +74,7 @@ type Segment struct {
 
 // Data reads and returns the contents of the segment.
 func (s *Segment) Data() ([]byte, error) {
-       dat := make([]byte, s.sr.Size())
-       n, err := s.sr.ReadAt(dat, 0)
-       if n == len(dat) {
-               err = nil
-       }
-       return dat[0:n], err
+       return saferio.ReadDataAt(s.sr, s.Filesz, 0)
 }
 
 // Open returns a new ReadSeeker reading the segment.
@@ -126,12 +122,7 @@ type Section struct {
 
 // Data reads and returns the contents of the Mach-O section.
 func (s *Section) Data() ([]byte, error) {
-       dat := make([]byte, s.sr.Size())
-       n, err := s.sr.ReadAt(dat, 0)
-       if n == len(dat) {
-               err = nil
-       }
-       return dat[0:n], err
+       return saferio.ReadDataAt(s.sr, s.Size, 0)
 }
 
 // Open returns a new ReadSeeker reading the Mach-O section.