forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpf_test.go
511 lines (432 loc) · 13.8 KB
/
bpf_test.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//go:build bpf && !386
// +build bpf,!386
/*
Copyright 2019 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bpf
import (
"context"
_ "embed"
"fmt"
"net/http"
"net/http/httptest"
"os"
os_exec "os/exec"
"syscall"
"testing"
"time"
"unsafe"
"github.com/aquasecurity/libbpfgo"
"github.com/gravitational/teleport/api/constants"
apidefaults "github.com/gravitational/teleport/api/defaults"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/events/eventstest"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/google/uuid"
"gopkg.in/check.v1"
)
type Suite struct{}
var _ = check.Suite(&Suite{})
func TestRootBPF(t *testing.T) {
if !bpfTestEnabled() {
t.Skip("BPF testing is disabled")
}
check.TestingT(t)
}
func (s *Suite) TestWatch(c *check.C) {
// This test must be run as root and the host has to be capable of running
// BPF programs.
if !isRoot() {
c.Skip("Tests for package bpf can only be run as root.")
}
err := IsHostCompatible()
if err != nil {
c.Skip(fmt.Sprintf("Tests for package bpf can not be run: %v.", err))
}
// Create temporary directory where cgroup2 hierarchy will be mounted.
dir, err := os.MkdirTemp("", "cgroup-test")
c.Assert(err, check.IsNil)
defer os.RemoveAll(dir)
// Create BPF service.
service, err := New(&Config{
Enabled: true,
CgroupPath: dir,
})
defer service.Close()
// Create a fake audit log that can be used to capture the events emitted.
emitter := &eventstest.MockEmitter{}
// Create and start a program that does nothing. Since sleep will run longer
// than we wait below, nothing should be emit to the Audit Log.
cmd := os_exec.Command("sleep", "10")
err = cmd.Start()
c.Assert(err, check.IsNil)
// Create a monitoring session for init. The events we execute should not
// have PID 1, so nothing should be captured in the Audit Log.
cgroupID, err := service.OpenSession(&SessionContext{
Namespace: apidefaults.Namespace,
SessionID: uuid.New().String(),
ServerID: uuid.New().String(),
Login: "foo",
User: "[email protected]",
PID: cmd.Process.Pid,
Emitter: emitter,
Events: map[string]bool{
constants.EnhancedRecordingCommand: true,
constants.EnhancedRecordingDisk: true,
constants.EnhancedRecordingNetwork: true,
},
})
c.Assert(err, check.IsNil)
c.Assert(cgroupID > 0, check.Equals, true)
// Find "ls" binary.
lsPath, err := os_exec.LookPath("ls")
c.Assert(err, check.IsNil)
// Execute "ls" a few times
for i := 0; i < 5; i++ {
// Run "ls".
err = os_exec.Command(lsPath).Run()
c.Assert(err, check.IsNil)
// Delay.
time.Sleep(25 * time.Millisecond)
}
// Make sure no events from "ls" were generated
for _, e := range emitter.Events() {
var pid uint64
switch ev := e.(type) {
case *apievents.SessionCommand:
pid = ev.BPFMetadata.PID
case *apievents.SessionDisk:
pid = ev.BPFMetadata.PID
case *apievents.SessionNetwork:
pid = ev.BPFMetadata.PID
}
c.Assert(int(pid), check.Equals, cmd.Process.Pid)
}
}
// TestObfuscate checks if execsnoop can capture Obfuscated commands.
func (s *Suite) TestObfuscate(c *check.C) {
// This test must be run as root and the host has to be capable of running
// BPF programs.
if !isRoot() {
c.Skip("Tests for package bpf can only be run as root.")
return
}
err := IsHostCompatible()
if err != nil {
c.Skip(fmt.Sprintf("Tests for package bpf can not be run: %v.", err))
return
}
// Find the programs needed to run these tests on the host.
decoderPath, err := os_exec.LookPath("base64")
c.Assert(err, check.IsNil)
shellPath, err := os_exec.LookPath("sh")
c.Assert(err, check.IsNil)
// Start execsnoop.
execsnoop, err := startExec(8192)
defer execsnoop.close()
c.Assert(err, check.IsNil)
// Create a context that will be used to signal that an event has been recieved.
doneContext, doneFunc := context.WithCancel(context.Background())
// Start two goroutines. The first writes a script which will execute "ls"
// in a loop. The second waits for an exec event to show up the reports "ls"
// has been executed.
go func() {
// Create temporary file.
file, err := os.CreateTemp("", "test-script")
c.Assert(err, check.IsNil)
defer os.Remove(file.Name())
// Write script to file.
shellContents := fmt.Sprintf("#!%v\necho bHM= | %v --decode | %v",
shellPath, decoderPath, shellPath)
_, err = file.Write([]byte(shellContents))
c.Assert(err, check.IsNil)
err = file.Close()
c.Assert(err, check.IsNil)
// Make script executable.
err = os.Chmod(file.Name(), 0700)
c.Assert(err, check.IsNil)
for {
// Run script.
err = os_exec.Command(file.Name()).Run()
c.Assert(err, check.IsNil)
// Delay.
time.Sleep(250 * time.Millisecond)
}
}()
go func() {
for {
eventBytes := <-execsnoop.events()
// Unmarshal the event.
var event rawExecEvent
err := unmarshalEvent(eventBytes, &event)
c.Assert(err, check.IsNil)
// Check the event is what we expect, in this case "ls".
if ConvertString(unsafe.Pointer(&event.Command)) == "ls" {
doneFunc()
break
}
}
}()
// Wait for an event to arrive from execsnoop. If an event does not arrive
// within 10 seconds, timeout.
select {
case <-doneContext.Done():
case <-time.After(10 * time.Second):
c.Fatalf("Timed out waiting for an event.")
}
}
// TestScript checks if execsnoop can capture what a script executes.
func (s *Suite) TestScript(c *check.C) {
// This test must be run as root and the host has to be capable of running
// BPF programs.
if !isRoot() {
c.Skip("Tests for package bpf can only be run as root.")
}
err := IsHostCompatible()
if err != nil {
c.Skip(fmt.Sprintf("Tests for package bpf can not be run: %v.", err))
}
// Start execsnoop.
execsnoop, err := startExec(8192)
defer execsnoop.close()
c.Assert(err, check.IsNil)
// Create a context that will be used to signal that an event has been recieved.
doneContext, doneFunc := context.WithCancel(context.Background())
// Start two goroutines. The first writes a script which will execute "ls"
// in a loop. The second waits for an exec event to show up the reports "ls"
// has been executed.
go func() {
// Create temporary file.
file, err := os.CreateTemp("", "test-script")
c.Assert(err, check.IsNil)
defer os.Remove(file.Name())
// Write script to file.
_, err = file.Write([]byte("#!/bin/sh\nls"))
c.Assert(err, check.IsNil)
err = file.Close()
c.Assert(err, check.IsNil)
// Make script executable.
err = os.Chmod(file.Name(), 0700)
c.Assert(err, check.IsNil)
for {
// Run script.
err = os_exec.Command(file.Name()).Run()
c.Assert(err, check.IsNil)
// Delay.
time.Sleep(250 * time.Millisecond)
}
}()
go func() {
for {
eventBytes := <-execsnoop.events()
// Unmarshal the event.
var event rawExecEvent
err := unmarshalEvent(eventBytes, &event)
c.Assert(err, check.IsNil)
// Check the event is what we expect, in this case "ls".
if ConvertString(unsafe.Pointer(&event.Command)) == "ls" {
doneFunc()
break
}
}
}()
// Wait for an event to arrive from execsnoop. If an event does not arrive
// within 10 seconds, timeout.
select {
case <-doneContext.Done():
case <-time.After(10 * time.Second):
c.Fatalf("Timed out waiting for an event.")
}
}
// TestPrograms tests execsnoop, opensnoop, and tcpconnect to make sure they
// run and receive events.
func (s *Suite) TestPrograms(c *check.C) {
// This test must be run as root. Only root can create cgroups.
if !isRoot() {
c.Skip("Tests for package bpf can only be run as root.")
}
// Check that the host is capable of running BPF programs.
err := IsHostCompatible()
if err != nil {
c.Skip(fmt.Sprintf("Tests for package bpf can not be run: %v.", err))
}
// Start a debug server that tcpconnect will connect to.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello, world")
}))
defer ts.Close()
// Start execsnoop.
execsnoop, err := startExec(8192)
c.Assert(err, check.IsNil)
defer execsnoop.close()
// Start opensnoop.
opensnoop, err := startOpen(8192)
c.Assert(err, check.IsNil)
defer opensnoop.close()
// Start tcpconnect.
tcpconnect, err := startConn(8192)
c.Assert(err, check.IsNil)
defer tcpconnect.close()
// Loop over all three programs and make sure events are received off the
// perf buffer.
var tests = []struct {
inName string
inCommand string
inCommandArgs []string
inEventCh <-chan []byte
inHTTP bool
}{
// Run execsnoop with "ls".
{
inName: "execsnoop",
inCommand: "ls",
inCommandArgs: []string{},
inEventCh: execsnoop.events(),
inHTTP: false,
},
// Run opensnoop with "ls". This is fine because "ls" will open some
// shared library.
{
inName: "opensnoop",
inCommand: "ls",
inCommandArgs: []string{},
inEventCh: opensnoop.events(),
inHTTP: false,
},
// Run tcpconnect with netcat.
{
inName: "tcpconnect",
inEventCh: tcpconnect.v4Events(),
inHTTP: true,
},
}
for _, tt := range tests {
// Create a context that will be used to signal that an event has been recieved.
doneContext, doneFunc := context.WithCancel(context.Background())
// Start two goroutines. The first will wait for the BPF program event to
// arrive, and once it has, signal over the context that it's complete. The
// second will continue to execute or a HTTP GET in a in a loop attempting to
// trigger an event.
go waitForEvent(doneContext, doneFunc, tt.inEventCh)
if tt.inHTTP {
go executeHTTP(c, doneContext, ts.URL)
} else {
go executeCommand(c, doneContext, tt.inCommand)
}
// Wait for an event to arrive from execsnoop. If an event does not arrive
// within 10 seconds, timeout.
select {
case <-doneContext.Done():
case <-time.After(10 * time.Second):
c.Fatalf("Timed out waiting for an %v event.", tt.inName)
}
}
}
// TestBPFCounter tests that BPF-to-Prometheus counter works ok
func (s *Suite) TestBPFCounter(c *check.C) {
// This test must be run as root. Only root can create cgroups.
if !isRoot() {
c.Skip("Tests for package bpf can only be run as root.")
}
// Check that the host is capable of running BPF programs.
err := IsHostCompatible()
if err != nil {
c.Skip(fmt.Sprintf("Tests for package bpf can not be run: %v.", err))
}
counterTestBPF, err := embedFS.ReadFile("bytecode/counter_test.bpf.o")
if err != nil {
c.Skip(fmt.Sprintf("Tests for package bpf can not be run: %v.", err))
}
module, err := libbpfgo.NewModuleFromBuffer(counterTestBPF, "counter_test")
c.Assert(err, check.IsNil)
// Load into the kernel
err = module.BPFLoadObject()
c.Assert(err, check.IsNil)
err = AttachSyscallTracepoint(module, "close")
c.Assert(err, check.IsNil)
promCounter := prometheus.NewCounter(prometheus.CounterOpts{Name: "test"})
counter, err := NewCounter(module, "test_counter", promCounter)
c.Assert(err, check.IsNil)
// Make sure the counter starts with 0
c.Assert(testutil.ToFloat64(promCounter), check.Equals, float64(0))
// close(1234) will cause the counter to get incremented.
magicFD := 1234
// First do it a few times as to no overflow the doorbell buffer
gentleBumps := 10
for i := 0; i < gentleBumps; i++ {
syscall.Close(magicFD)
}
// Not ideal but no other good way to know that the counter was updated
time.Sleep(time.Second)
// Make sure all are accounted for
c.Assert(testutil.ToFloat64(promCounter), check.Equals, float64(gentleBumps))
// Next, pound the counter to heopfully overflow the doorbell.
poundingBumps := 100000
for i := 0; i < poundingBumps; i++ {
syscall.Close(magicFD)
}
// Not ideal but no other good way to know that the counter was updated
time.Sleep(time.Second)
// Make sure all are accounted for
c.Assert(testutil.ToFloat64(promCounter), check.Equals, float64(gentleBumps+poundingBumps))
counter.Close()
}
// waitForEvent will wait for an event to arrive over the perf buffer and
// signal when it has.
func waitForEvent(ctx context.Context, cancel context.CancelFunc, eventCh <-chan []byte) {
for {
select {
case <-eventCh:
cancel()
case <-ctx.Done():
return
}
}
}
// executeCommand will execute some command in a loop.
func executeCommand(c *check.C, doneContext context.Context, file string) {
for {
// Lookup and run the requested command.
path, err := os_exec.LookPath(file)
if err != nil {
c.Fatalf("Failed to find execute %q: %v.", file, err)
}
err = os_exec.Command(path).Run()
if err != nil {
c.Fatalf("Failed to run command %q: %v.", file, err)
}
time.Sleep(250 * time.Millisecond)
}
}
// executeHTTP will perform a HTTP GET to some endpoint in a loop.
func executeHTTP(c *check.C, doneContext context.Context, endpoint string) {
for {
// Perform HTTP GET to the requested endpoint.
_, err := http.Get(endpoint)
c.Assert(err, check.IsNil)
time.Sleep(250 * time.Millisecond)
}
}
// isRoot returns a boolean if the test is being run as root or not. Tests
// for this package must be run as root.
func isRoot() bool {
if os.Geteuid() != 0 {
return false
}
return true
}
// bpfTestEnabled returns true if BPF tests should run. Tests can be enabled by
// setting TELEPORT_BPF_TEST environment variable to any value.
func bpfTestEnabled() bool {
return os.Getenv("TELEPORT_BPF_TEST") != ""
}