Debian Codegraph @

internal/kallsyms/kallsyms.go

raw symbols SCIP
1 package kallsyms
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "slices"
10 "strconv"
11
12 "github.com/cilium/ebpf/internal"
13 "github.com/cilium/ebpf/internal/platform"
14 )
15
16 var errAmbiguousKsym = errors.New("multiple kernel symbols with the same name")
17
18 var symAddrs cache[string, uint64]
19
20 // AssignAddresses looks up the addresses of the requested symbols in the kernel
21 // and assigns them to their corresponding values in the symbols map. Results
22 // of all lookups are cached, successful or otherwise.
23 //
24 // Any symbols missing in the kernel are ignored. Returns an error if multiple
25 // addresses were found for a symbol.
26 func AssignAddresses(symbols map[string]uint64) error {
27 if !platform.IsLinux {
28 return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS)
29 }
30
31 if len(symbols) == 0 {
32 return nil
33 }
34
35 // Attempt to fetch symbols from cache.
36 request := make(map[string]uint64)
37 for name := range symbols {
38 if addr, ok := symAddrs.Load(name); ok {
39 symbols[name] = addr
40 continue
41 }
42
43 // Mark the symbol to be read from /proc/kallsyms.
44 request[name] = 0
45 }
46 if len(request) == 0 {
47 // All symbols satisfied from cache.
48 return nil
49 }
50
51 f, err := os.Open("/proc/kallsyms")
52 if err != nil {
53 return err
54 }
55 defer f.Close()
56
57 if err := assignAddresses(f, request); err != nil {
58 return fmt.Errorf("loading symbol addresses: %w", err)
59 }
60
61 // Update the cache with the new symbols. Cache all requested symbols even if
62 // they weren't found, to avoid repeated lookups.
63 for name, addr := range request {
64 symAddrs.Store(name, addr)
65 symbols[name] = addr
66 }
67
68 return nil
69 }
70
71 // assignAddresses assigns kernel symbol addresses read from f to values
72 // requested by symbols. Always scans the whole input to make sure the user
73 // didn't request an ambiguous symbol.
74 func assignAddresses(f io.Reader, symbols map[string]uint64) error {
75 if len(symbols) == 0 {
76 return nil
77 }
78 r := newReader(f)
79 for r.Line() {
80 s, err, skip := parseSymbol(r, nil)
81 if err != nil {
82 return fmt.Errorf("parsing kallsyms line: %w", err)
83 }
84 if skip {
85 continue
86 }
87
88 existing, requested := symbols[string(s.name)]
89 if existing != 0 {
90 // Multiple addresses for a symbol have been found. Return a friendly
91 // error to avoid silently attaching to the wrong symbol. libbpf also
92 // rejects referring to ambiguous symbols.
93 return fmt.Errorf("symbol %s(0x%x): duplicate found at address 0x%x: %w", s.name, existing, s.addr, errAmbiguousKsym)
94 }
95 if requested {
96 // Reading a symbol with a zero address is a strong indication that
97 // kptr_restrict is set and the process doesn't have CAP_SYSLOG, or
98 // kptr_restrict is set to 2 (never show addresses).
99 //
100 // When running the kernel with KASLR disabled (like CI kernels running in
101 // microVMs), kallsyms will display many absolute symbols at address 0.
102 // This memory is unlikely to contain anything useful, and production
103 // machines are unlikely to run without KASLR.
104 //
105 // Return a helpful error instead of silently returning zero addresses.
106 if s.addr == 0 {
107 return fmt.Errorf("symbol %s: %w", s.name, internal.ErrRestrictedKernel)
108 }
109 symbols[string(s.name)] = s.addr
110 }
111 }
112 if err := r.Err(); err != nil {
113 return fmt.Errorf("reading kallsyms: %w", err)
114 }
115
116 return nil
117 }
118
119 type ksym struct {
120 addr uint64
121 name []byte
122 mod []byte
123 }
124
125 // parseSymbol parses a line from /proc/kallsyms into an address, type, name and
126 // module. Skip will be true if the symbol doesn't match any of the given symbol
127 // types. See `man 1 nm` for all available types.
128 //
129 // Only yields symbols whose type is contained in types. An empty value for types
130 // disables this filtering.
131 //
132 // Example line: `ffffffffc1682010 T nf_nat_init\t[nf_nat]`
133 func parseSymbol(r *reader, types []rune) (s ksym, err error, skip bool) {
134 for i := 0; r.Word(); i++ {
135 switch i {
136 // Address of the symbol.
137 case 0:
138 s.addr, err = strconv.ParseUint(r.Text(), 16, 64)
139 if err != nil {
140 return s, fmt.Errorf("parsing address: %w", err), false
141 }
142 // Type of the symbol. Assume the character is ASCII-encoded by converting
143 // it directly to a rune, since it's a fixed field controlled by the kernel.
144 case 1:
145 if len(types) > 0 && !slices.Contains(types, rune(r.Bytes()[0])) {
146 return s, nil, true
147 }
148 // Name of the symbol.
149 case 2:
150 s.name = r.Bytes()
151 // Kernel module the symbol is provided by.
152 case 3:
153 s.mod = bytes.Trim(r.Bytes(), "[]")
154 // Ignore any future fields.
155 default:
156 return
157 }
158 }
159
160 return
161 }