-
Notifications
You must be signed in to change notification settings - Fork 699
/
syscalls.go
355 lines (309 loc) · 7.98 KB
/
syscalls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package ebpf
import (
"bytes"
"errors"
"fmt"
"math"
"os"
"runtime"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/linux"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/tracefs"
"github.com/cilium/ebpf/internal/unix"
)
var (
// pre-allocating these here since they may
// get called in hot code paths and cause
// unnecessary memory allocations
sysErrKeyNotExist = sys.Error(ErrKeyNotExist, unix.ENOENT)
sysErrKeyExist = sys.Error(ErrKeyExist, unix.EEXIST)
sysErrNotSupported = sys.Error(ErrNotSupported, sys.ENOTSUPP)
)
// invalidBPFObjNameChar returns true if char may not appear in
// a BPF object name.
func invalidBPFObjNameChar(char rune) bool {
dotAllowed := objNameAllowsDot() == nil
switch {
case char >= 'A' && char <= 'Z':
return false
case char >= 'a' && char <= 'z':
return false
case char >= '0' && char <= '9':
return false
case dotAllowed && char == '.':
return false
case char == '_':
return false
default:
return true
}
}
func progLoad(insns asm.Instructions, typ ProgramType, license string) (*sys.FD, error) {
buf := bytes.NewBuffer(make([]byte, 0, insns.Size()))
if err := insns.Marshal(buf, internal.NativeEndian); err != nil {
return nil, err
}
bytecode := buf.Bytes()
return sys.ProgLoad(&sys.ProgLoadAttr{
ProgType: sys.ProgType(typ),
License: sys.NewStringPointer(license),
Insns: sys.NewSlicePointer(bytecode),
InsnCnt: uint32(len(bytecode) / asm.InstructionSize),
})
}
var haveNestedMaps = internal.NewFeatureTest("nested maps", func() error {
_, err := sys.MapCreate(&sys.MapCreateAttr{
MapType: sys.MapType(ArrayOfMaps),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
// Invalid file descriptor.
InnerMapFd: ^uint32(0),
})
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
if errors.Is(err, unix.EBADF) {
return nil
}
return err
}, "4.12")
var haveMapMutabilityModifiers = internal.NewFeatureTest("read- and write-only maps", func() error {
// This checks BPF_F_RDONLY_PROG and BPF_F_WRONLY_PROG. Since
// BPF_MAP_FREEZE appeared in 5.2 as well we don't do a separate check.
m, err := sys.MapCreate(&sys.MapCreateAttr{
MapType: sys.MapType(Array),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
MapFlags: sys.BPF_F_RDONLY_PROG,
})
if err != nil {
return internal.ErrNotSupported
}
_ = m.Close()
return nil
}, "5.2")
var haveMmapableMaps = internal.NewFeatureTest("mmapable maps", func() error {
// This checks BPF_F_MMAPABLE, which appeared in 5.5 for array maps.
m, err := sys.MapCreate(&sys.MapCreateAttr{
MapType: sys.MapType(Array),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
MapFlags: sys.BPF_F_MMAPABLE,
})
if err != nil {
return internal.ErrNotSupported
}
_ = m.Close()
return nil
}, "5.5")
var haveInnerMaps = internal.NewFeatureTest("inner maps", func() error {
// This checks BPF_F_INNER_MAP, which appeared in 5.10.
m, err := sys.MapCreate(&sys.MapCreateAttr{
MapType: sys.MapType(Array),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
MapFlags: sys.BPF_F_INNER_MAP,
})
if err != nil {
return internal.ErrNotSupported
}
_ = m.Close()
return nil
}, "5.10")
var haveNoPreallocMaps = internal.NewFeatureTest("prealloc maps", func() error {
// This checks BPF_F_NO_PREALLOC, which appeared in 4.6.
m, err := sys.MapCreate(&sys.MapCreateAttr{
MapType: sys.MapType(Hash),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
MapFlags: sys.BPF_F_NO_PREALLOC,
})
if err != nil {
return internal.ErrNotSupported
}
_ = m.Close()
return nil
}, "4.6")
func wrapMapError(err error) error {
if err == nil {
return nil
}
if errors.Is(err, unix.ENOENT) {
return sysErrKeyNotExist
}
if errors.Is(err, unix.EEXIST) {
return sysErrKeyExist
}
if errors.Is(err, sys.ENOTSUPP) {
return sysErrNotSupported
}
if errors.Is(err, unix.E2BIG) {
return fmt.Errorf("key too big for map: %w", err)
}
return err
}
var haveObjName = internal.NewFeatureTest("object names", func() error {
attr := sys.MapCreateAttr{
MapType: sys.MapType(Array),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
MapName: sys.NewObjName("feature_test"),
}
// Tolerate EPERM as this runs during ELF loading which is potentially
// unprivileged. Only EINVAL is conclusive, thrown from CHECK_ATTR.
fd, err := sys.MapCreate(&attr)
if errors.Is(err, unix.EPERM) {
return nil
}
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
if err != nil {
return err
}
_ = fd.Close()
return nil
}, "4.15")
var objNameAllowsDot = internal.NewFeatureTest("dot in object names", func() error {
if err := haveObjName(); err != nil {
return err
}
attr := sys.MapCreateAttr{
MapType: sys.MapType(Array),
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
MapName: sys.NewObjName(".test"),
}
// Tolerate EPERM, otherwise MapSpec.Name has its dots removed when run by
// unprivileged tools. (bpf2go, other code gen). Only EINVAL is conclusive,
// thrown from bpf_obj_name_cpy().
fd, err := sys.MapCreate(&attr)
if errors.Is(err, unix.EPERM) {
return nil
}
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
if err != nil {
return err
}
_ = fd.Close()
return nil
}, "5.2")
var haveBatchAPI = internal.NewFeatureTest("map batch api", func() error {
var maxEntries uint32 = 2
attr := sys.MapCreateAttr{
MapType: sys.MapType(Hash),
KeySize: 4,
ValueSize: 4,
MaxEntries: maxEntries,
}
fd, err := sys.MapCreate(&attr)
if err != nil {
return internal.ErrNotSupported
}
defer fd.Close()
keys := []uint32{1, 2}
values := []uint32{3, 4}
kp, _ := marshalMapSyscallInput(keys, 8)
vp, _ := marshalMapSyscallInput(values, 8)
err = sys.MapUpdateBatch(&sys.MapUpdateBatchAttr{
MapFd: fd.Uint(),
Keys: kp,
Values: vp,
Count: maxEntries,
})
if err != nil {
return internal.ErrNotSupported
}
return nil
}, "5.6")
var haveProbeReadKernel = internal.NewFeatureTest("bpf_probe_read_kernel", func() error {
insns := asm.Instructions{
asm.Mov.Reg(asm.R1, asm.R10),
asm.Add.Imm(asm.R1, -8),
asm.Mov.Imm(asm.R2, 8),
asm.Mov.Imm(asm.R3, 0),
asm.FnProbeReadKernel.Call(),
asm.Return(),
}
fd, err := progLoad(insns, Kprobe, "GPL")
if err != nil {
return internal.ErrNotSupported
}
_ = fd.Close()
return nil
}, "5.5")
var haveBPFToBPFCalls = internal.NewFeatureTest("bpf2bpf calls", func() error {
insns := asm.Instructions{
asm.Call.Label("prog2").WithSymbol("prog1"),
asm.Return(),
asm.Mov.Imm(asm.R0, 0).WithSymbol("prog2"),
asm.Return(),
}
fd, err := progLoad(insns, SocketFilter, "MIT")
if err != nil {
return internal.ErrNotSupported
}
_ = fd.Close()
return nil
}, "4.16")
var haveSyscallWrapper = internal.NewFeatureTest("syscall wrapper", func() error {
prefix := linux.PlatformPrefix()
if prefix == "" {
return fmt.Errorf("unable to find the platform prefix for (%s)", runtime.GOARCH)
}
args := tracefs.ProbeArgs{
Type: tracefs.Kprobe,
Symbol: prefix + "sys_bpf",
Pid: -1,
}
var err error
args.Group, err = tracefs.RandomGroup("ebpf_probe")
if err != nil {
return err
}
evt, err := tracefs.NewEvent(args)
if errors.Is(err, os.ErrNotExist) {
return internal.ErrNotSupported
}
if err != nil {
return err
}
return evt.Close()
}, "4.17")
var haveProgramExtInfos = internal.NewFeatureTest("program ext_infos", func() error {
insns := asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
}
buf := bytes.NewBuffer(make([]byte, 0, insns.Size()))
if err := insns.Marshal(buf, internal.NativeEndian); err != nil {
return err
}
bytecode := buf.Bytes()
_, err := sys.ProgLoad(&sys.ProgLoadAttr{
ProgType: sys.ProgType(SocketFilter),
License: sys.NewStringPointer("MIT"),
Insns: sys.NewSlicePointer(bytecode),
InsnCnt: uint32(len(bytecode) / asm.InstructionSize),
FuncInfoCnt: 1,
ProgBtfFd: math.MaxUint32,
})
if errors.Is(err, unix.EBADF) {
return nil
}
if errors.Is(err, unix.E2BIG) {
return ErrNotSupported
}
return err
}, "5.0")