Skip to content

Commit

Permalink
debug/elf: use saferio.SliceCap when decoding ELF sections
Browse files Browse the repository at this point in the history
This avoids allocating an overly large slice for corrupt input.

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

Updates #33121.

Change-Id: Ie2d947a3865d3499034286f2d08d3e3204015f3e
GitHub-Last-Rev: 6c62fc3
GitHub-Pull-Request: #56405
Reviewed-on: https://go-review.googlesource.com/c/go/+/445076
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Heschi Kreinick <[email protected]>
  • Loading branch information
ZekeLu authored and gopherbot committed Oct 25, 2022
1 parent 90a67d0 commit 2bdb5c5
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/debug/elf/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,12 @@ func NewFile(r io.ReaderAt) (*File, error) {
}

// Read section headers
f.Sections = make([]*Section, shnum)
names := make([]uint32, shnum)
c := saferio.SliceCap((*Section)(nil), uint64(shnum))
if c < 0 {
return nil, &FormatError{0, "too many sections", shnum}
}
f.Sections = make([]*Section, 0, c)
names := make([]uint32, 0, c)
for i := 0; i < shnum; i++ {
off := shoff + int64(i)*int64(shentsize)
sr.Seek(off, seekStart)
Expand All @@ -479,7 +483,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
if err := binary.Read(sr, f.ByteOrder, sh); err != nil {
return nil, err
}
names[i] = sh.Name
names = append(names, sh.Name)
s.SectionHeader = SectionHeader{
Type: SectionType(sh.Type),
Flags: SectionFlag(sh.Flags),
Expand All @@ -496,7 +500,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
if err := binary.Read(sr, f.ByteOrder, sh); err != nil {
return nil, err
}
names[i] = sh.Name
names = append(names, sh.Name)
s.SectionHeader = SectionHeader{
Type: SectionType(sh.Type),
Flags: SectionFlag(sh.Flags),
Expand Down Expand Up @@ -544,7 +548,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
}
}

f.Sections[i] = s
f.Sections = append(f.Sections, s)
}

if len(f.Sections) == 0 {
Expand Down

0 comments on commit 2bdb5c5

Please sign in to comment.