diff --git a/debug/elf/file.go b/debug/elf/file.go index a35386d..24fd2b4 100644 --- a/debug/elf/file.go +++ b/debug/elf/file.go @@ -244,6 +244,7 @@ func (f *File) SectionByType(typ SectionType) *Section { // NewFile creates a new File for accessing an ELF binary in an underlying reader. // The ELF binary is expected to start at position 0 in the ReaderAt. func NewFile(r io.ReaderAt) (*File, error) { + sr := io.NewSectionReader(r, 0, 1<<63-1) // Read and decode ELF identifier var ident [16]uint8 @@ -255,6 +256,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } f := new(File) + f.dataAfterSectionCache = make(map[uint64][]byte) f.Class = Class(ident[EI_CLASS]) switch f.Class { case ELFCLASS32: @@ -565,8 +567,6 @@ func NewFile(r io.ReaderAt) (*File, error) { return nil, &FormatError{shoff + int64(i*shentsize), "bad section name index", names[i]} } } - - f.dataAfterSectionCache = make(map[uint64][]byte) return f, nil } diff --git a/debug/macho/file.go b/debug/macho/file.go index 9647ae2..564a921 100644 --- a/debug/macho/file.go +++ b/debug/macho/file.go @@ -231,6 +231,7 @@ func (f *File) Close() error { // The Mach-O binary is expected to start at position 0 in the ReaderAt. func NewFile(r io.ReaderAt) (*File, error) { f := new(File) + f.dataAfterSectionCache = make(map[uint64][]byte) sr := io.NewSectionReader(r, 0, 1<<63-1) // Read and decode Mach magic to determine byte order, size. @@ -449,7 +450,6 @@ func NewFile(r io.ReaderAt) (*File, error) { s.ReaderAt = s.sr } } - f.dataAfterSectionCache = make(map[uint64][]byte) return f, nil } diff --git a/debug/pe/file.go b/debug/pe/file.go index c176715..5f16fb9 100644 --- a/debug/pe/file.go +++ b/debug/pe/file.go @@ -67,6 +67,7 @@ func (f *File) Close() error { // NewFile creates a new File for accessing a PE binary in an underlying reader. func NewFile(r io.ReaderAt) (*File, error) { f := new(File) + f.dataAfterSectionCache = make(map[uint64][]byte) sr := io.NewSectionReader(r, 0, 1<<63-1) var dosheader [96]byte @@ -172,8 +173,6 @@ func NewFile(r io.ReaderAt) (*File, error) { return nil, err } } - - f.dataAfterSectionCache = make(map[uint64][]byte) return f, nil } diff --git a/main_test.go b/main_test.go index 6f7c7ab..f80bd56 100644 --- a/main_test.go +++ b/main_test.go @@ -188,6 +188,20 @@ func TestWeirdBins(t *testing.T) { t.Errorf("GoReSym found pclntab in a non-go binary, this is not possible.") } }) + + // reading the buildid with notes section at the start and alignment of 0 previously caused underflow in offset calculations + t.Run("zero_elf_palignment", func(t *testing.T) { + filePath := fmt.Sprintf("%s/test/weirdbins/%s", workingDirectory, "zero_elf_palignment") + if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) { + t.Errorf("Test file %s doesn't exist\n", filePath) + return + } + + _, err := main_impl(filePath, true, true, true, 0, "") + if err == nil { + t.Errorf("GoReSym found pclntab in a non-go binary, this is not possible.") + } + }) } func TestBig(t *testing.T) { diff --git a/test/weirdbins.zip b/test/weirdbins.zip index d061120..f51d14a 100644 Binary files a/test/weirdbins.zip and b/test/weirdbins.zip differ