Debian Codegraph @

features/prog.go

raw symbols SCIP
1 package features
2
3 import (
4 "errors"
5 "fmt"
6 "slices"
7 "strings"
8
9 "github.com/cilium/ebpf"
10 "github.com/cilium/ebpf/asm"
11 "github.com/cilium/ebpf/btf"
12 "github.com/cilium/ebpf/internal"
13 "github.com/cilium/ebpf/internal/sys"
14 "github.com/cilium/ebpf/internal/unix"
15 )
16
17 // HaveProgramType probes the running kernel for the availability of the specified program type.
18 //
19 // See the package documentation for the meaning of the error return value.
20 func HaveProgramType(pt ebpf.ProgramType) (err error) {
21 return haveProgramTypeMatrix.Result(pt)
22 }
23
24 func probeProgram(spec *ebpf.ProgramSpec) error {
25 if spec.Instructions == nil {
26 spec.Instructions = asm.Instructions{
27 asm.LoadImm(asm.R0, 0, asm.DWord),
28 asm.Return(),
29 }
30 }
31 prog, err := ebpf.NewProgramWithOptions(spec, ebpf.ProgramOptions{
32 LogDisabled: true,
33 })
34 if err == nil {
35 prog.Close()
36 }
37
38 switch {
39 // EINVAL occurs when attempting to create a program with an unknown type.
40 // E2BIG occurs when ProgLoadAttr contains non-zero bytes past the end
41 // of the struct known by the running kernel, meaning the kernel is too old
42 // to support the given prog type.
43 case errors.Is(err, unix.EINVAL), errors.Is(err, unix.E2BIG):
44 err = ebpf.ErrNotSupported
45 }
46
47 return err
48 }
49
50 var haveProgramTypeMatrix = internal.FeatureMatrix[ebpf.ProgramType]{
51 ebpf.SocketFilter: {Version: "3.19"},
52 ebpf.Kprobe: {Version: "4.1"},
53 ebpf.SchedCLS: {Version: "4.1"},
54 ebpf.SchedACT: {Version: "4.1"},
55 ebpf.TracePoint: {Version: "4.7"},
56 ebpf.XDP: {Version: "4.8"},
57 ebpf.PerfEvent: {Version: "4.9"},
58 ebpf.CGroupSKB: {Version: "4.10"},
59 ebpf.CGroupSock: {Version: "4.10"},
60 ebpf.LWTIn: {Version: "4.10"},
61 ebpf.LWTOut: {Version: "4.10"},
62 ebpf.LWTXmit: {Version: "4.10"},
63 ebpf.SockOps: {Version: "4.13"},
64 ebpf.SkSKB: {Version: "4.14"},
65 ebpf.CGroupDevice: {Version: "4.15"},
66 ebpf.SkMsg: {Version: "4.17"},
67 ebpf.RawTracepoint: {Version: "4.17"},
68 ebpf.CGroupSockAddr: {
69 Version: "4.17",
70 Fn: func() error {
71 return probeProgram(&ebpf.ProgramSpec{
72 Type: ebpf.CGroupSockAddr,
73 AttachType: ebpf.AttachCGroupInet4Connect,
74 })
75 },
76 },
77 ebpf.LWTSeg6Local: {Version: "4.18"},
78 ebpf.LircMode2: {Version: "4.18"},
79 ebpf.SkReuseport: {Version: "4.19"},
80 ebpf.FlowDissector: {Version: "4.20"},
81 ebpf.CGroupSysctl: {Version: "5.2"},
82 ebpf.RawTracepointWritable: {Version: "5.2"},
83 ebpf.CGroupSockopt: {
84 Version: "5.3",
85 Fn: func() error {
86 return probeProgram(&ebpf.ProgramSpec{
87 Type: ebpf.CGroupSockopt,
88 AttachType: ebpf.AttachCGroupGetsockopt,
89 })
90 },
91 },
92 ebpf.Tracing: {
93 Version: "5.5",
94 Fn: func() error {
95 return probeProgram(&ebpf.ProgramSpec{
96 Type: ebpf.Tracing,
97 AttachType: ebpf.AttachTraceFEntry,
98 AttachTo: "bpf_init",
99 })
100 },
101 },
102 ebpf.StructOps: {
103 Version: "5.6",
104 Fn: func() error {
105 err := probeProgram(&ebpf.ProgramSpec{
106 Type: ebpf.StructOps,
107 License: "GPL",
108 })
109 if errors.Is(err, sys.ENOTSUPP) {
110 // ENOTSUPP means the program type is at least known to the kernel.
111 return nil
112 }
113 return err
114 },
115 },
116 ebpf.Extension: {
117 Version: "5.6",
118 Fn: func() error {
119 // create btf.Func to add to first ins of target and extension so both progs are btf powered
120 btfFn := btf.Func{
121 Name: "a",
122 Type: &btf.FuncProto{
123 Return: &btf.Int{},
124 Params: []btf.FuncParam{
125 {Name: "ctx", Type: &btf.Pointer{Target: &btf.Struct{Name: "xdp_md"}}},
126 },
127 },
128 Linkage: btf.GlobalFunc,
129 }
130 insns := asm.Instructions{
131 btf.WithFuncMetadata(asm.Mov.Imm(asm.R0, 0), &btfFn),
132 asm.Return(),
133 }
134
135 // create target prog
136 prog, err := ebpf.NewProgramWithOptions(
137 &ebpf.ProgramSpec{
138 Type: ebpf.XDP,
139 Instructions: insns,
140 },
141 ebpf.ProgramOptions{
142 LogDisabled: true,
143 },
144 )
145 if err != nil {
146 return err
147 }
148 defer prog.Close()
149
150 // probe for Extension prog with target
151 return probeProgram(&ebpf.ProgramSpec{
152 Type: ebpf.Extension,
153 Instructions: insns,
154 AttachTarget: prog,
155 AttachTo: btfFn.Name,
156 })
157 },
158 },
159 ebpf.LSM: {
160 Version: "5.7",
161 Fn: func() error {
162 return probeProgram(&ebpf.ProgramSpec{
163 Type: ebpf.LSM,
164 AttachType: ebpf.AttachLSMMac,
165 AttachTo: "file_mprotect",
166 License: "GPL",
167 })
168 },
169 },
170 ebpf.SkLookup: {
171 Version: "5.9",
172 Fn: func() error {
173 return probeProgram(&ebpf.ProgramSpec{
174 Type: ebpf.SkLookup,
175 AttachType: ebpf.AttachSkLookup,
176 })
177 },
178 },
179 ebpf.Syscall: {
180 Version: "5.14",
181 Fn: func() error {
182 return probeProgram(&ebpf.ProgramSpec{
183 Type: ebpf.Syscall,
184 Flags: sys.BPF_F_SLEEPABLE,
185 })
186 },
187 },
188 ebpf.Netfilter: {
189 Version: "6.4",
190 Fn: func() error {
191 return probeProgram(&ebpf.ProgramSpec{
192 Type: ebpf.Netfilter,
193 AttachType: ebpf.AttachNetfilter,
194 })
195 },
196 },
197 }
198
199 func init() {
200 for key, ft := range haveProgramTypeMatrix {
201 ft.Name = key.String()
202 if ft.Fn == nil {
203 key := key // avoid the dreaded loop variable problem
204 ft.Fn = func() error { return probeProgram(&ebpf.ProgramSpec{Type: key}) }
205 }
206 }
207 }
208
209 type helperKey struct {
210 typ ebpf.ProgramType
211 helper asm.BuiltinFunc
212 }
213
214 var helperCache = internal.NewFeatureCache(func(key helperKey) *internal.FeatureTest {
215 return &internal.FeatureTest{
216 Name: fmt.Sprintf("%s for program type %s", key.helper, key.typ),
217 Fn: func() error {
218 return haveProgramHelper(key.typ, key.helper)
219 },
220 }
221 })
222
223 // HaveProgramHelper probes the running kernel for the availability of the specified helper
224 // function to a specified program type.
225 // Return values have the following semantics:
226 //
227 // err == nil: The feature is available.
228 // errors.Is(err, ebpf.ErrNotSupported): The feature is not available.
229 // err != nil: Any errors encountered during probe execution, wrapped.
230 //
231 // Note that the latter case may include false negatives, and that program creation may
232 // succeed despite an error being returned.
233 // Only `nil` and `ebpf.ErrNotSupported` are conclusive.
234 //
235 // Probe results are cached and persist throughout any process capability changes.
236 func HaveProgramHelper(pt ebpf.ProgramType, helper asm.BuiltinFunc) error {
237 return helperCache.Result(helperKey{pt, helper})
238 }
239
240 func haveProgramHelper(pt ebpf.ProgramType, helper asm.BuiltinFunc) error {
241 if ok := helperProbeNotImplemented(pt); ok {
242 return fmt.Errorf("no feature probe for %v/%v", pt, helper)
243 }
244
245 if err := HaveProgramType(pt); err != nil {
246 return err
247 }
248
249 spec := &ebpf.ProgramSpec{
250 Type: pt,
251 Instructions: asm.Instructions{
252 helper.Call(),
253 asm.LoadImm(asm.R0, 0, asm.DWord),
254 asm.Return(),
255 },
256 License: "GPL",
257 }
258
259 switch pt {
260 case ebpf.CGroupSockAddr:
261 spec.AttachType = ebpf.AttachCGroupInet4Connect
262 case ebpf.CGroupSockopt:
263 spec.AttachType = ebpf.AttachCGroupGetsockopt
264 case ebpf.SkLookup:
265 spec.AttachType = ebpf.AttachSkLookup
266 case ebpf.Syscall:
267 spec.Flags = sys.BPF_F_SLEEPABLE
268 case ebpf.Netfilter:
269 spec.AttachType = ebpf.AttachNetfilter
270 }
271
272 prog, err := ebpf.NewProgramWithOptions(spec, ebpf.ProgramOptions{
273 LogLevel: 1,
274 })
275 if err == nil {
276 prog.Close()
277 }
278
279 var verr *ebpf.VerifierError
280 if !errors.As(err, &verr) {
281 return err
282 }
283
284 helperTag := fmt.Sprintf("#%d", helper)
285
286 switch {
287 // EACCES occurs when attempting to create a program probe with a helper
288 // while the register args when calling this helper aren't set up properly.
289 // We interpret this as the helper being available, because the verifier
290 // returns EINVAL if the helper is not supported by the running kernel.
291 case errors.Is(err, unix.EACCES):
292 err = nil
293
294 // EINVAL occurs when attempting to create a program with an unknown helper.
295 case errors.Is(err, unix.EINVAL):
296 // https://github.com/torvalds/linux/blob/09a0fa92e5b45e99cf435b2fbf5ebcf889cf8780/kernel/bpf/verifier.c#L10663
297 if logContainsAll(verr.Log, "invalid func", helperTag) {
298 return ebpf.ErrNotSupported
299 }
300
301 // https://github.com/torvalds/linux/blob/09a0fa92e5b45e99cf435b2fbf5ebcf889cf8780/kernel/bpf/verifier.c#L10668
302 wrongProgramType := logContainsAll(verr.Log, "program of this type cannot use helper", helperTag)
303 // https://github.com/torvalds/linux/blob/59b418c7063d30e0a3e1f592d47df096db83185c/kernel/bpf/verifier.c#L10204
304 // 4.9 doesn't include # in verifier output.
305 wrongProgramType = wrongProgramType || logContainsAll(verr.Log, "unknown func")
306 if wrongProgramType {
307 return fmt.Errorf("program of this type cannot use helper: %w", ebpf.ErrNotSupported)
308 }
309 }
310
311 return err
312 }
313
314 func logContainsAll(log []string, needles ...string) bool {
315 first := max(len(log)-5, 0) // Check last 5 lines.
316 return slices.ContainsFunc(log[first:], func(line string) bool {
317 for _, needle := range needles {
318 if !strings.Contains(line, needle) {
319 return false
320 }
321 }
322 return true
323 })
324 }
325
326 func helperProbeNotImplemented(pt ebpf.ProgramType) bool {
327 switch pt {
328 case ebpf.Extension, ebpf.LSM, ebpf.StructOps, ebpf.Tracing:
329 return true
330 }
331 return false
332 }