Skip to content

Commit

Permalink
Add defensive checks for CGO stripped binaries (#1725)
Browse files Browse the repository at this point in the history
* add defensive checks for stripped binaries

* add comment
  • Loading branch information
grcevski authored Jan 31, 2025
1 parent 6cf45e7 commit 33420aa
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/pkg/process/binary/funcs_stripped.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func FindFunctionsStripped(elfF *elf.File, relevantFuncs map[string]interface{})
return nil, err
}

// We need to read the Go pcln data at offset 8 + 2 * the pointer size.
// The pointer size can be found at offset 7, which should be either 4 or 8.
// We assume that we shouldn't have a gopclntab section smaller than the
// 8 + 2 * the largest possible pointer size, which is 8 + 2 * 8.
if len(pclndat) <= 8*2*8 {
return nil, errors.New(".gopclntab section too small")
}

// we extract the `textStart` value based on the header of the pclntab,
// this is used to parse the line number table, and is not necessarily the start of the `.text` section.
// when a binary is build with C code, the value of `textStart` is not the same as the start of the `.text` section.
Expand All @@ -30,8 +38,10 @@ func FindFunctionsStripped(elfF *elf.File, relevantFuncs map[string]interface{})
ptrSize := uint32(pclndat[7])
if ptrSize == 4 {
runtimeText = uint64(binary.LittleEndian.Uint32(pclndat[8+2*ptrSize:]))
} else {
} else if ptrSize == 8 {
runtimeText = binary.LittleEndian.Uint64(pclndat[8+2*ptrSize:])
} else {
return nil, errors.New("invalid pointer size of text section of .gopclntab")
}

pcln := gosym.NewLineTable(pclndat, runtimeText)
Expand Down

0 comments on commit 33420aa

Please sign in to comment.