Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug/pe: fails to parse variable length optional header #32126

Open
prash2611 opened this issue May 18, 2019 · 3 comments

Comments

Projects
None yet
4 participants
@prash2611
Copy link

commented May 18, 2019

What version of Go are you using (go version)?

$ go version
go version go1.11.5 linux/amd64

Does this issue reproduce with the latest release?

Yes it does! The issue is present is current master.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/prashant/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/prashant/go2"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build980405246=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I was trying to parse a PE file (linux Kernel to be specific) using debug/pe package.
A toy program for the same would look like:

package main

import (
    "debug/pe"
    "fmt"
)

func main() {
    file, err := pe.Open("/boot/vmlinuz-4.15.0-48-generic")
    if err != nil {
        file.Close()
        return
    }
    defer file.Close()

    fmt.Println(file.OptionalHeader)
}

What did you expect to see?

I would like to see parsed optional header.

What did you see instead?

Optional header turned out to be nil.

What is actual bug?

The debug/pe package assumes there are always 16 entries in [16]DataDirectory in OptionalHeader32/64 (ref pe.go)
NumberOfRvaAndSizes uint32
DataDirectory [16]DataDirectory
}

But that is not always the case, there could be less no of entries (PE signed linux kernel for example):
prashant@Pra-Work:~$ sudo pev /boot/vmlinuz-4.15.0-47-generic
....
Data-dictionary entries: 6
....

In such case, the parsing gives incorrect results.

@gopherbot

This comment has been minimized.

Copy link

commented May 18, 2019

Change https://golang.org/cl/177959 mentions this issue: debug/pe: enables parsing of variable length optional header in PE file

@ALTree ALTree added this to the Go1.14 milestone May 18, 2019

@alexbrainman

This comment has been minimized.

Copy link
Member

commented May 19, 2019

@prash2611 thank you for reporting the problem. I will review your change when I have time.

Alex

@prash2611

This comment has been minimized.

Copy link
Author

commented May 19, 2019

Thanks @alexbrainman, looking forward to your review comments.

@prash2611 prash2611 closed this May 19, 2019

@prash2611 prash2611 reopened this May 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.