Debian Codegraph @

internal/sys/syscall_other.go

raw symbols SCIP
1 //go:build !windows
2
3 package sys
4
5 import (
6 "errors"
7 "fmt"
8 "os"
9 "path/filepath"
10 "runtime"
11 "strings"
12 "unsafe"
13
14 "github.com/cilium/ebpf/internal/unix"
15 )
16
17 // BPF wraps SYS_BPF.
18 //
19 // Any pointers contained in attr must use the Pointer type from this package.
20 func BPF(cmd Cmd, attr unsafe.Pointer, size uintptr) (uintptr, error) {
21 // Prevent the Go profiler from repeatedly interrupting the verifier,
22 // which could otherwise lead to a livelock due to receiving EAGAIN.
23 if cmd == BPF_PROG_LOAD || cmd == BPF_PROG_RUN {
24 maskProfilerSignal()
25 defer unmaskProfilerSignal()
26 }
27
28 tok, err := tokenAttr(cmd, attr)
29 if err != nil {
30 return 0, fmt.Errorf("apply token attributes: %w", err)
31 }
32
33 for {
34 r1, _, errNo := unix.Syscall(unix.SYS_BPF, uintptr(cmd), uintptr(attr), size)
35 runtime.KeepAlive(attr)
36
37 // As of ~4.20 the verifier can be interrupted by a signal,
38 // and returns EAGAIN in that case.
39 if errNo == unix.EAGAIN && cmd == BPF_PROG_LOAD {
40 continue
41 }
42
43 var err error
44 if errNo != 0 {
45 err = wrappedErrno{errNo}
46 }
47
48 // Tokens are in use, attempt to enrich EPERM with capability information.
49 if tok != nil && errors.Is(err, unix.EPERM) {
50 // Kernels before 6.17 don't have token info, so we can't return accurate
51 // errors. Make the token error a hint by adding a question mark.
52 //
53 // Returning ErrTokenCapabilities here isn't ideal since it's not
54 // conclusive, but since this behaviour is limited to one LTS release
55 // (6.12), and only when tokens are enabled, it's better than having to
56 // account for it in all existing feature probes and tests.
57 if tok.info == nil {
58 return r1, fmt.Errorf("%w (%w?)", err, ErrTokenCapabilities)
59 }
60
61 if terr := tok.Capable(cmd, attr); terr != nil {
62 return r1, fmt.Errorf("%w: %w", terr, err)
63 }
64 }
65
66 return r1, err
67 }
68 }
69
70 // ObjGetTyped wraps [ObjGet] with a readlink call to extract the type of the
71 // underlying bpf object.
72 func ObjGetTyped(attr *ObjGetAttr) (*FD, ObjType, error) {
73 fd, err := ObjGet(attr)
74 if err != nil {
75 return nil, 0, err
76 }
77
78 typ, err := readType(fd)
79 if err != nil {
80 _ = fd.Close()
81 return nil, 0, fmt.Errorf("reading fd type: %w", err)
82 }
83
84 return fd, typ, nil
85 }
86
87 // readType returns the bpf object type of the file descriptor by calling
88 // readlink(3). Returns an error if the file descriptor does not represent a bpf
89 // object.
90 func readType(fd *FD) (ObjType, error) {
91 s, err := os.Readlink(filepath.Join("/proc/self/fd/", fd.String()))
92 if err != nil {
93 return 0, fmt.Errorf("readlink fd %d: %w", fd.Int(), err)
94 }
95
96 s = strings.TrimPrefix(s, "anon_inode:")
97
98 switch s {
99 case "bpf-map":
100 return BPF_TYPE_MAP, nil
101 case "bpf-prog":
102 return BPF_TYPE_PROG, nil
103 case "bpf-link":
104 return BPF_TYPE_LINK, nil
105 }
106
107 return 0, fmt.Errorf("unknown type %s of fd %d", s, fd.Int())
108 }