]> Cypherpunks repositories - gostls13.git/commitdiff
debug/pe: omit panic in (*File).ImportedSymbols on empty optional headers
authorTobias Klauser <tklauser@distanz.ch>
Fri, 15 Feb 2019 11:15:11 +0000 (12:15 +0100)
committerTobias Klauser <tobias.klauser@gmail.com>
Wed, 27 Feb 2019 08:10:00 +0000 (08:10 +0000)
If a PE file with invalid optional header size (neither
sizeofOptionalHeader32 nor sizeofOptionalHeader64) is passed to NewFile,
the File.OptionalHeader will be nil which leads to a panic in
(*File).ImportedSymbols().

Fixes #30250

Change-Id: Ie97306de4a0e2dcfdc7b1b599891f574aa63adca
Reviewed-on: https://go-review.googlesource.com/c/162858
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
src/debug/pe/file.go
src/debug/pe/file_test.go

index 1c308b3dc3b0c9ffe67e48a1dde36c96905aefe5..1d714bf6e7d6fe7387d25a96850c79b537e29365 100644 (file)
@@ -324,6 +324,10 @@ type ImportDirectory struct {
 // satisfied by other libraries at dynamic load time.
 // It does not return weak symbols.
 func (f *File) ImportedSymbols() ([]string, error) {
+       if f.OptionalHeader == nil {
+               return nil, nil
+       }
+
        pe64 := f.Machine == IMAGE_FILE_MACHINE_AMD64
 
        // grab the number of data directory entries
index 9613af3a3c3c00aeedc54023c4a4d63df6aab9f3..f4b24f7253fad61ab95c42722e6058169a4d66f3 100644 (file)
@@ -5,6 +5,7 @@
 package pe
 
 import (
+       "bytes"
        "debug/dwarf"
        "internal/testenv"
        "io/ioutil"
@@ -627,3 +628,22 @@ func TestImportTableInUnknownSection(t *testing.T) {
                t.Fatalf("unable to locate any imported symbols within file %q.", path)
        }
 }
+
+func TestInvalidFormat(t *testing.T) {
+       crashers := [][]byte{
+               // https://golang.org/issue/30250
+               []byte("\x00\x00\x00\x0000000\x00\x00\x00\x00\x00\x00\x000000" +
+                       "00000000000000000000" +
+                       "000000000\x00\x00\x0000000000" +
+                       "00000000000000000000" +
+                       "0000000000000000"),
+       }
+
+       for _, data := range crashers {
+               f, err := NewFile(bytes.NewReader(data))
+               if err != nil {
+                       t.Error(err)
+               }
+               f.ImportedSymbols()
+       }
+}