Skip to content

Commit

Permalink
internal: fix reading auxv on 32-bit platforms
Browse files Browse the repository at this point in the history
It turns out that the auxiliary vector has a platform specific size.
Adjust the code to use uintptr to approximate "unsigned long" from C.

Fixes cilium#1133

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Oct 2, 2023
1 parent 0eafd16 commit c1dc8e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions internal/vdso.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,35 @@ func vdsoVersion() (uint32, error) {
return c, nil
}

// auxvPair represents a tag and value pair in the auxiliary vector.
//
// The size depends on the width of a pointer. In C, each entry is of type
// "unsigned long".
type auxvPair struct {
Tag, Value uintptr
}

// vdsoMemoryAddress returns the memory address of the vDSO library
// linked into the current process image. r is an io.Reader into an auxv blob.
func vdsoMemoryAddress(r io.Reader) (uint64, error) {
func vdsoMemoryAddress(r io.Reader) (uintptr, error) {
// See https://elixir.bootlin.com/linux/v6.5.5/source/include/uapi/linux/auxvec.h
const (
_AT_NULL = 0 // End of vector
_AT_SYSINFO_EHDR = 33 // Offset to vDSO blob in process image
)

// Loop through all tag/value pairs in auxv until we find `AT_SYSINFO_EHDR`,
// the address of a page containing the virtual Dynamic Shared Object (vDSO).
aux := struct{ Tag, Val uint64 }{}
var aux auxvPair
for {
if err := binary.Read(r, NativeEndian, &aux); err != nil {
return 0, fmt.Errorf("reading auxv entry: %w", err)
}

switch aux.Tag {
case _AT_SYSINFO_EHDR:
if aux.Val != 0 {
return aux.Val, nil
if aux.Value != 0 {
return aux.Value, nil
}
return 0, fmt.Errorf("invalid vDSO address in auxv")
// _AT_NULL is always the last tag/val pair in the aux vector
Expand Down
2 changes: 1 addition & 1 deletion internal/vdso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAuxvVDSOMemoryAddress(t *testing.T) {
t.Fatal(err)
}

expected := uint64(0x7ffd377e5000)
expected := uintptr(0x7ffd377e5000)
if addr != expected {
t.Errorf("Expected vDSO memory address %x, got %x", expected, addr)
}
Expand Down

0 comments on commit c1dc8e7

Please sign in to comment.