Debian Codegraph @

internal/linux/vdso.go

raw symbols SCIP
1 package linux
2
3 import (
4 "debug/elf"
5 "encoding/binary"
6 "errors"
7 "fmt"
8 "io"
9 "math"
10 "os"
11
12 "github.com/cilium/ebpf/internal"
13 "github.com/cilium/ebpf/internal/unix"
14 )
15
16 var (
17 errAuxvNoVDSO = errors.New("no vdso address found in auxv")
18 )
19
20 // vdsoVersion returns the LINUX_VERSION_CODE embedded in the vDSO library
21 // linked into the current process image.
22 func vdsoVersion() (uint32, error) {
23 av, err := newAuxvRuntimeReader()
24 if err != nil {
25 return 0, err
26 }
27
28 defer av.Close()
29
30 vdsoAddr, err := vdsoMemoryAddress(av)
31 if err != nil {
32 return 0, fmt.Errorf("finding vDSO memory address: %w", err)
33 }
34
35 // Use /proc/self/mem rather than unsafe.Pointer tricks.
36 mem, err := os.Open("/proc/self/mem")
37 if err != nil {
38 return 0, fmt.Errorf("opening mem: %w", err)
39 }
40 defer mem.Close()
41
42 // Open ELF at provided memory address, as offset into /proc/self/mem.
43 c, err := vdsoLinuxVersionCode(io.NewSectionReader(mem, int64(vdsoAddr), math.MaxInt64))
44 if err != nil {
45 return 0, fmt.Errorf("reading linux version code: %w", err)
46 }
47
48 return c, nil
49 }
50
51 // vdsoMemoryAddress returns the memory address of the vDSO library
52 // linked into the current process image. r is an io.Reader into an auxv blob.
53 func vdsoMemoryAddress(r auxvPairReader) (uintptr, error) {
54 // Loop through all tag/value pairs in auxv until we find `AT_SYSINFO_EHDR`,
55 // the address of a page containing the virtual Dynamic Shared Object (vDSO).
56 for {
57 tag, value, err := r.ReadAuxvPair()
58 if err != nil {
59 return 0, err
60 }
61
62 switch tag {
63 case _AT_SYSINFO_EHDR:
64 if value != 0 {
65 return uintptr(value), nil
66 }
67 return 0, fmt.Errorf("invalid vDSO address in auxv")
68 // _AT_NULL is always the last tag/val pair in the aux vector
69 // and can be treated like EOF.
70 case _AT_NULL:
71 return 0, errAuxvNoVDSO
72 }
73 }
74 }
75
76 // format described at https://www.man7.org/linux/man-pages/man5/elf.5.html in section 'Notes (Nhdr)'
77 type elfNoteHeader struct {
78 NameSize int32
79 DescSize int32
80 Type int32
81 }
82
83 // vdsoLinuxVersionCode returns the LINUX_VERSION_CODE embedded in
84 // the ELF notes section of the binary provided by the reader.
85 func vdsoLinuxVersionCode(r io.ReaderAt) (uint32, error) {
86 hdr, err := internal.NewSafeELFFile(r)
87 if err != nil {
88 return 0, fmt.Errorf("reading vDSO ELF: %w", err)
89 }
90
91 sections := hdr.SectionsByType(elf.SHT_NOTE)
92 if len(sections) == 0 {
93 return 0, fmt.Errorf("no note section found in vDSO ELF")
94 }
95
96 for _, sec := range sections {
97 sr := sec.Open()
98 var n elfNoteHeader
99
100 // Read notes until we find one named 'Linux'.
101 for {
102 if err := binary.Read(sr, hdr.ByteOrder, &n); err != nil {
103 if errors.Is(err, io.EOF) {
104 // We looked at all the notes in this section
105 break
106 }
107 return 0, fmt.Errorf("reading note header: %w", err)
108 }
109
110 // If a note name is defined, it follows the note header.
111 var name string
112 if n.NameSize > 0 {
113 // Read the note name, aligned to 4 bytes.
114 buf := make([]byte, internal.Align(n.NameSize, 4))
115 if err := binary.Read(sr, hdr.ByteOrder, &buf); err != nil {
116 return 0, fmt.Errorf("reading note name: %w", err)
117 }
118
119 // Read nul-terminated string.
120 name = unix.ByteSliceToString(buf[:n.NameSize])
121 }
122
123 // If a note descriptor is defined, it follows the name.
124 // It is possible for a note to have a descriptor but not a name.
125 if n.DescSize > 0 {
126 // LINUX_VERSION_CODE is a uint32 value.
127 if name == "Linux" && n.DescSize == 4 && n.Type == 0 {
128 var version uint32
129 if err := binary.Read(sr, hdr.ByteOrder, &version); err != nil {
130 return 0, fmt.Errorf("reading note descriptor: %w", err)
131 }
132 return version, nil
133 }
134
135 // Discard the note descriptor if it exists but we're not interested in it.
136 if _, err := io.CopyN(io.Discard, sr, int64(internal.Align(n.DescSize, 4))); err != nil {
137 return 0, err
138 }
139 }
140 }
141 }
142
143 return 0, fmt.Errorf("no Linux note in ELF")
144 }