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.
  • Loading branch information
ZekeLu committed Oct 24, 2022
1 parent 3617514 commit 6c62fc3
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 6c62fc3

Please sign in to comment.