Skip to content

fix: amd64, offset read error issue for PIE executable. PR #516 #517

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

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 10 additions & 26 deletions user/config/config_gotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const (
)

var (
ErrorGoBINNotFound = errors.New("The executable program (compiled by Golang) was not found")
ErrorSymbolEmpty = errors.New("symbol is empty")
ErrorSymbolNotFound = errors.New("symbol not found")
ErrorSymbolNotFoundFromTable = errors.New("symbol not found from table")
ErrorNoRetFound = errors.New("no RET instructions found")
ErrorNoRetFoundFromSymTabFun = errors.New("no RET instructions found from golang symbol table with Fun")
ErrorGoBINNotFound = errors.New("The executable program (compiled by Golang) was not found")
ErrorSymbolEmpty = errors.New("symbol is empty")
ErrorSymbolNotFound = errors.New("symbol not found")
ErrorSymbolNotFoundFromTable = errors.New("symbol not found from table")
ErrorNoRetFound = errors.New("no RET instructions found")
ErrorNoFuncFoundFromSymTabFun = errors.New("no function found from golang symbol table with Func Name")
)

// From go/src/debug/gosym/pclntab.go
Expand Down Expand Up @@ -339,8 +339,9 @@ func (gc *GoTLSConfig) findRetOffsetsPie(lfunc string) ([]int, error) {
if prog.Type != elf.PT_LOAD || (prog.Flags&elf.PF_X) == 0 {
continue
}
// via https://github.com/golang/go/blob/a65a2bbd8e58cd77dbff8a751dbd6079424beb05/src/cmd/internal/objfile/elf.go#L174
data := make([]byte, funcLen)
_, err = prog.ReadAt(data, int64(address))
_, err = prog.ReadAt(data, int64(address-prog.Vaddr))
if err != nil {
return offsets, fmt.Errorf("finding function return: %w", err)
}
Expand All @@ -359,24 +360,7 @@ func (gc *GoTLSConfig) findRetOffsetsPie(lfunc string) ([]int, error) {
func (gc *GoTLSConfig) findPieSymbolAddr(lfunc string) (uint64, error) {
f := gc.goSymTab.LookupFunc(lfunc)
if f == nil {
return 0, errors.New("Cant found symbol address on pie model.")
return 0, ErrorNoFuncFoundFromSymTabFun
}
var err error
for _, prog := range gc.goElf.Progs {
if prog.Type != elf.PT_LOAD || (prog.Flags&elf.PF_X) == 0 {
continue
}
// For more info on this calculation: stackoverflow.com/a/40249502
if prog.Vaddr <= f.Value && f.Value < (prog.Vaddr+prog.Memsz) {
funcLen := f.End - f.Entry
data := make([]byte, funcLen)
address := f.Value - prog.Vaddr + prog.Off
_, err = prog.ReadAt(data, int64(address))
if err != nil {
return 0, fmt.Errorf("search function return: %w", err)
}
return address, nil
}
}
return 0, ErrorNoRetFoundFromSymTabFun
return f.Value, nil
}