-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
93 lines (76 loc) · 1.76 KB
/
main.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
package main
import (
"bufio"
"bytes"
_ "embed"
"fmt"
"os"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/sirupsen/logrus"
manager "github.com/DataDog/ebpf-manager"
)
//go:embed ebpf/bin/probe.o
var Probe []byte
var m1 = &manager.Manager{
Probes: []*manager.Probe{
{
ProbeIdentificationPair: manager.ProbeIdentificationPair{
EBPFFuncName: "kprobe__security_socket_create",
},
},
},
InstructionPatcher: patchBPFTelemetry,
}
const BPFTelemetryPatchCall = -1
func getAllProgramSpecs(m *manager.Manager) ([]*ebpf.ProgramSpec, error) {
var specs []*ebpf.ProgramSpec
for _, p := range m.Probes {
s, present, err := m.GetProgramSpec(p.ProbeIdentificationPair)
if err != nil {
return nil, err
}
if !present {
return nil, fmt.Errorf("could not find ProgramSpec for probe %v", p.ProbeIdentificationPair)
}
specs = append(specs, s...)
}
return specs, nil
}
func patchBPFTelemetry(m *manager.Manager) error {
specs, err := getAllProgramSpecs(m)
if err != nil {
return err
}
for _, spec := range specs {
if spec == nil {
continue
}
iter := spec.Instructions.Iterate()
for iter.Next() {
ins := iter.Ins
if !ins.IsBuiltinCall() {
continue
}
if ins.Constant != BPFTelemetryPatchCall {
continue
}
*ins = asm.Mov.Imm(asm.R1, int32(0xff)).WithMetadata(ins.Metadata)
}
}
return nil
}
func main() {
if err := m1.Init(bytes.NewReader(Probe)); err != nil {
logrus.Fatal(err)
}
if err := m1.Start(); err != nil {
logrus.Fatal(err)
}
logrus.Println("Use 'bpftool prog dump xlated id <prog-id>' to verify that the instruction has been patched. Press 'Enter' to exit...")
_, _ = bufio.NewReader(os.Stdin).ReadBytes('\n')
cleanup()
}
func cleanup() {
_ = m1.Stop(manager.CleanAll)
}