forked from cilium/ebpf
/
uprobe_test.go
431 lines (351 loc) · 10.6 KB
/
uprobe_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
package link
import (
"errors"
"fmt"
"go/build"
"os"
"os/exec"
"path"
"testing"
qt "github.com/frankban/quicktest"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/unix"
)
var (
bashEx, _ = OpenExecutable("/bin/bash")
bashSym = "main"
)
func TestExecutable(t *testing.T) {
_, err := OpenExecutable("")
if err == nil {
t.Fatal("create executable: expected error on empty path")
}
if bashEx.path != "/bin/bash" {
t.Fatalf("create executable: unexpected path '%s'", bashEx.path)
}
_, err = bashEx.offset(bashSym)
if err != nil {
t.Fatalf("find offset: %v", err)
}
_, err = bashEx.offset("bogus")
if err == nil {
t.Fatal("find symbol: expected error")
}
}
func TestUprobe(t *testing.T) {
c := qt.New(t)
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")
up, err := bashEx.Uprobe(bashSym, prog, nil)
c.Assert(err, qt.IsNil)
defer up.Close()
testLink(t, up, prog)
}
func TestUprobeExtNotFound(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")
// This symbol will not be present in Executable (elf.SHN_UNDEF).
_, err := bashEx.Uprobe("open", prog, nil)
if err == nil {
t.Fatal("expected error")
}
}
func TestUprobeExtWithOpts(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")
// This Uprobe is broken and will not work because the offset is not
// correct. This is expected since the offset is provided by the user.
up, err := bashEx.Uprobe("open", prog, &UprobeOptions{Offset: 0x1})
if err != nil {
t.Fatal(err)
}
defer up.Close()
}
func TestUprobeWithPID(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")
up, err := bashEx.Uprobe(bashSym, prog, &UprobeOptions{PID: os.Getpid()})
if err != nil {
t.Fatal(err)
}
defer up.Close()
}
func TestUprobeWithNonExistentPID(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")
// trying to open a perf event on a non-existent PID will return ESRCH.
_, err := bashEx.Uprobe(bashSym, prog, &UprobeOptions{PID: -2})
if !errors.Is(err, unix.ESRCH) {
t.Fatalf("expected ESRCH, got %v", err)
}
}
func TestUretprobe(t *testing.T) {
c := qt.New(t)
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")
up, err := bashEx.Uretprobe(bashSym, prog, nil)
c.Assert(err, qt.IsNil)
defer up.Close()
testLink(t, up, prog)
}
// Test u(ret)probe creation using perf_uprobe PMU.
func TestUprobeCreatePMU(t *testing.T) {
// Requires at least 4.17 (e12f03d7031a "perf/core: Implement the 'perf_kprobe' PMU")
testutils.SkipOnOldKernel(t, "4.17", "perf_kprobe PMU")
c := qt.New(t)
// Fetch the offset from the /bin/bash Executable already defined.
off, err := bashEx.offset(bashSym)
c.Assert(err, qt.IsNil)
// Prepare probe args.
args := probeArgs{
symbol: bashSym,
path: bashEx.path,
offset: off,
pid: perfAllThreads,
}
// uprobe PMU
pu, err := pmuUprobe(args)
c.Assert(err, qt.IsNil)
defer pu.Close()
c.Assert(pu.typ, qt.Equals, uprobeEvent)
// uretprobe PMU
args.ret = true
pr, err := pmuUprobe(args)
c.Assert(err, qt.IsNil)
defer pr.Close()
c.Assert(pr.typ, qt.Equals, uretprobeEvent)
}
// Test fallback behaviour on kernels without perf_uprobe PMU available.
func TestUprobePMUUnavailable(t *testing.T) {
c := qt.New(t)
// Fetch the offset from the /bin/bash Executable already defined.
off, err := bashEx.offset(bashSym)
c.Assert(err, qt.IsNil)
// Prepare probe args.
args := probeArgs{
symbol: bashSym,
path: bashEx.path,
offset: off,
pid: perfAllThreads,
}
pk, err := pmuUprobe(args)
if err == nil {
pk.Close()
t.Skipf("Kernel supports perf_uprobe PMU, not asserting error.")
}
// Expect ErrNotSupported.
c.Assert(errors.Is(err, ErrNotSupported), qt.IsTrue, qt.Commentf("got error: %s", err))
}
// Test tracefs u(ret)probe creation on all kernel versions.
func TestUprobeTraceFS(t *testing.T) {
c := qt.New(t)
// Fetch the offset from the /bin/bash Executable already defined.
off, err := bashEx.offset(bashSym)
c.Assert(err, qt.IsNil)
// Prepare probe args.
args := probeArgs{
symbol: sanitizedSymbol(bashSym),
path: bashEx.path,
offset: off,
pid: perfAllThreads,
}
// Open and close tracefs u(ret)probes, checking all errors.
up, err := tracefsUprobe(args)
c.Assert(err, qt.IsNil)
c.Assert(up.Close(), qt.IsNil)
c.Assert(up.typ, qt.Equals, uprobeEvent)
args.ret = true
up, err = tracefsUprobe(args)
c.Assert(err, qt.IsNil)
c.Assert(up.Close(), qt.IsNil)
c.Assert(up.typ, qt.Equals, uretprobeEvent)
// Create two identical trace events, ensure their IDs differ.
args.ret = false
u1, err := tracefsUprobe(args)
c.Assert(err, qt.IsNil)
defer u1.Close()
c.Assert(u1.tracefsID, qt.Not(qt.Equals), 0)
u2, err := tracefsUprobe(args)
c.Assert(err, qt.IsNil)
defer u2.Close()
c.Assert(u2.tracefsID, qt.Not(qt.Equals), 0)
// Compare the uprobes' tracefs IDs.
c.Assert(u1.tracefsID, qt.Not(qt.CmpEquals()), u2.tracefsID)
}
// Test u(ret)probe creation writing directly to <tracefs>/uprobe_events.
// Only runs on 5.0 and over. Earlier versions ignored writes of duplicate
// events, while 5.0 started returning -EEXIST when a uprobe event already
// exists.
func TestUprobeCreateTraceFS(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.0", "<tracefs>/uprobe_events doesn't reject duplicate events")
c := qt.New(t)
// Fetch the offset from the /bin/bash Executable already defined.
off, err := bashEx.offset(bashSym)
c.Assert(err, qt.IsNil)
// Sanitize the symbol in order to be used in tracefs API.
ssym := sanitizedSymbol(bashSym)
pg, _ := randomGroup("ebpftest")
rg, _ := randomGroup("ebpftest")
// Tee up cleanups in case any of the Asserts abort the function.
defer func() {
_ = closeTraceFSProbeEvent(uprobeType, pg, ssym)
_ = closeTraceFSProbeEvent(uprobeType, rg, ssym)
}()
// Prepare probe args.
args := probeArgs{
group: pg,
symbol: ssym,
path: bashEx.path,
offset: off,
}
// Create a uprobe.
err = createTraceFSProbeEvent(uprobeType, args)
c.Assert(err, qt.IsNil)
// Attempt to create an identical uprobe using tracefs,
// expect it to fail with os.ErrExist.
err = createTraceFSProbeEvent(uprobeType, args)
c.Assert(errors.Is(err, os.ErrExist), qt.IsTrue,
qt.Commentf("expected consecutive uprobe creation to contain os.ErrExist, got: %v", err))
// Expect a successful close of the kprobe.
c.Assert(closeTraceFSProbeEvent(uprobeType, pg, ssym), qt.IsNil)
args.group = rg
args.ret = true
// Same test for a kretprobe.
err = createTraceFSProbeEvent(uprobeType, args)
c.Assert(err, qt.IsNil)
err = createTraceFSProbeEvent(uprobeType, args)
c.Assert(os.IsExist(err), qt.IsFalse,
qt.Commentf("expected consecutive uretprobe creation to contain os.ErrExist, got: %v", err))
// Expect a successful close of the uretprobe.
c.Assert(closeTraceFSProbeEvent(uprobeType, rg, ssym), qt.IsNil)
}
func TestUprobeSanitizedSymbol(t *testing.T) {
tests := []struct {
symbol string
expected string
}{
{"readline", "readline"},
{"main.Func", "main_Func"},
{"a.....a", "a_a"},
{"./;'{}[]a", "_a"},
}
for i, tt := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
sanitized := sanitizedSymbol(tt.symbol)
if tt.expected != sanitized {
t.Errorf("Expected sanitized symbol to be '%s', got '%s'", tt.expected, sanitized)
}
})
}
}
func TestUprobeToken(t *testing.T) {
tests := []struct {
args probeArgs
expected string
}{
{probeArgs{path: "/bin/bash"}, "/bin/bash:0x0"},
{probeArgs{path: "/bin/bash", offset: 1}, "/bin/bash:0x1"},
{probeArgs{path: "/bin/bash", offset: 65535}, "/bin/bash:0xffff"},
{probeArgs{path: "/bin/bash", offset: 65536}, "/bin/bash:0x10000"},
{probeArgs{path: "/bin/bash", offset: 1, refCtrOffset: 1}, "/bin/bash:0x1(0x1)"},
{probeArgs{path: "/bin/bash", offset: 1, refCtrOffset: 65535}, "/bin/bash:0x1(0xffff)"},
}
for i, tt := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
po := uprobeToken(tt.args)
if tt.expected != po {
t.Errorf("Expected path:offset to be '%s', got '%s'", tt.expected, po)
}
})
}
}
func TestUprobeProgramCall(t *testing.T) {
tests := []struct {
name string
elf string
args []string
sym string
}{
{
"bash",
"/bin/bash",
[]string{"--help"},
"main",
},
{
"go-binary",
path.Join(build.Default.GOROOT, "bin/go"),
[]string{"version"},
"main.main",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "go-binary" {
// https://github.com/cilium/ebpf/issues/406
testutils.SkipOnOldKernel(t, "4.14", "uprobes on Go binaries silently fail on kernel < 4.14")
}
m, p := newUpdaterMapProg(t, ebpf.Kprobe)
// Load the executable.
ex, err := OpenExecutable(tt.elf)
if err != nil {
t.Fatal(err)
}
// Open Uprobe on the executable for the given symbol
// and attach it to the ebpf program created above.
u, err := ex.Uprobe(tt.sym, p, nil)
if errors.Is(err, ErrNoSymbol) {
// Assume bash::main and go::main.main always exists
// and skip the test if the symbol can't be found as
// certain OS (eg. Debian) strip binaries.
t.Skipf("executable %s appear to be stripped, skipping", tt.elf)
}
if err != nil {
t.Fatal(err)
}
// Trigger ebpf program call.
trigger := func(t *testing.T) {
if err := exec.Command(tt.elf, tt.args...).Run(); err != nil {
t.Fatal(err)
}
}
trigger(t)
// Assert that the value at index 0 has been updated to 1.
assertMapValue(t, m, 0, 1)
// Detach the Uprobe.
if err := u.Close(); err != nil {
t.Fatal(err)
}
// Reset map value to 0 at index 0.
if err := m.Update(uint32(0), uint32(0), ebpf.UpdateExist); err != nil {
t.Fatal(err)
}
// Retrigger the ebpf program call.
trigger(t)
// Assert that this time the value has not been updated.
assertMapValue(t, m, 0, 0)
})
}
}
func TestUprobeProgramWrongPID(t *testing.T) {
m, p := newUpdaterMapProg(t, ebpf.Kprobe)
// Load the '/bin/bash' executable.
ex, err := OpenExecutable("/bin/bash")
if err != nil {
t.Fatal(err)
}
// Open Uprobe on '/bin/bash' for the symbol 'main'
// and attach it to the ebpf program created above.
// Create the perf-event with the current process' PID
// to make sure the event is not fired when we will try
// to trigger the program execution via exec.
u, err := ex.Uprobe("main", p, &UprobeOptions{PID: os.Getpid()})
if err != nil {
t.Fatal(err)
}
defer u.Close()
// Trigger ebpf program call.
if err := exec.Command("/bin/bash", "--help").Run(); err != nil {
t.Fatal(err)
}
// Assert that the value at index 0 is still 0.
assertMapValue(t, m, 0, 0)
}
func TestHaveRefCtrOffsetPMU(t *testing.T) {
testutils.CheckFeatureTest(t, haveRefCtrOffsetPMU)
}