-
Notifications
You must be signed in to change notification settings - Fork 339
/
detect.go
225 lines (198 loc) · 4.97 KB
/
detect.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
//go:build linux
// +build linux
package bpf
import (
"errors"
"fmt"
"sync"
"unsafe"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/features"
"github.com/cilium/ebpf/link"
"github.com/cilium/tetragon/pkg/arch"
"github.com/cilium/tetragon/pkg/logger"
"golang.org/x/sys/unix"
)
type Feature struct {
init sync.Once
detected bool
}
var (
kprobeMulti Feature
uprobeMulti Feature
buildid Feature
modifyReturn Feature
modifyReturnSyscall Feature
)
func HasOverrideHelper() bool {
return features.HaveProgramHelper(ebpf.Kprobe, asm.FnOverrideReturn) == nil
}
func HasSignalHelper() bool {
return features.HaveProgramHelper(ebpf.Kprobe, asm.FnSendSignal) == nil
}
func detectKprobeMulti() bool {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Name: "probe_bpf_kprobe_multi_link",
Type: ebpf.Kprobe,
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
AttachType: ebpf.AttachTraceKprobeMulti,
License: "MIT",
})
if err != nil {
return false
}
defer prog.Close()
syms := []string{"vprintk"}
opts := link.KprobeMultiOptions{Symbols: syms}
_, err = link.KprobeMulti(prog, opts)
return err == nil
}
func HasKprobeMulti() bool {
kprobeMulti.init.Do(func() {
kprobeMulti.detected = detectKprobeMulti()
})
return kprobeMulti.detected
}
func detectUprobeMulti() bool {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Name: "probe_upm_link",
Type: ebpf.Kprobe,
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
AttachType: ebpf.AttachTraceUprobeMulti,
License: "MIT",
})
if errors.Is(err, unix.E2BIG) {
// Kernel doesn't support AttachType field.
return false
}
if err != nil {
return false
}
defer prog.Close()
ex, err := link.OpenExecutable("/proc/self/exe")
if err != nil {
return false
}
um, err := ex.UprobeMulti(nil, prog, &link.UprobeMultiOptions{Addresses: []uint64{1}})
if err != nil {
return false
}
um.Close()
return true
}
func HasUprobeMulti() bool {
uprobeMulti.init.Do(func() {
uprobeMulti.detected = detectUprobeMulti()
})
return uprobeMulti.detected
}
func detectBuildId() bool {
attr := &unix.PerfEventAttr{
Type: unix.PERF_TYPE_SOFTWARE,
Config: unix.PERF_COUNT_SW_BPF_OUTPUT,
Bits: unix.PerfBitWatermark | unix.PerfBitMmap | unix.PerfBitMmap2 | PerfBitBuildId,
Sample_type: unix.PERF_SAMPLE_RAW,
Wakeup: 1,
}
attr.Size = uint32(unsafe.Sizeof(*attr))
fd, err := unix.PerfEventOpen(attr, -1, 0, -1, unix.PERF_FLAG_FD_CLOEXEC)
if err == nil {
unix.Close(fd)
return true
}
return false
}
func HasBuildId() bool {
buildid.init.Do(func() {
buildid.detected = detectBuildId()
})
return buildid.detected
}
func detectModifyReturn() bool {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Name: "probe_fmod_ret",
Type: ebpf.Tracing,
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
AttachType: ebpf.AttachModifyReturn,
License: "MIT",
AttachTo: "security_task_prctl",
})
if err != nil {
return false
}
defer prog.Close()
link, err := link.AttachTracing(link.TracingOptions{
Program: prog,
})
if err != nil {
return false
}
link.Close()
return true
}
func detectModifyReturnSyscall() (bool, error) {
sysGetcpu, err := arch.AddSyscallPrefix("sys_getcpu")
if err != nil {
return false, fmt.Errorf("failed to add arch specific syscall prefix: %w", err)
}
logger.GetLogger().Debugf("probing detectModifyReturnSyscall using %s", sysGetcpu)
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Name: "probe_sys_fmod_ret",
Type: ebpf.Tracing,
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
AttachType: ebpf.AttachModifyReturn,
AttachTo: sysGetcpu,
License: "MIT",
})
if err != nil {
return false, fmt.Errorf("failed to load: %w", err)
}
defer prog.Close()
link, err := link.AttachTracing(link.TracingOptions{
Program: prog,
})
if err != nil {
return false, fmt.Errorf("failed to attach: %w", err)
}
link.Close()
return true, nil
}
func HasModifyReturn() bool {
modifyReturn.init.Do(func() {
modifyReturn.detected = detectModifyReturn()
})
return modifyReturn.detected
}
func HasModifyReturnSyscall() bool {
modifyReturnSyscall.init.Do(func() {
var err error
modifyReturnSyscall.detected, err = detectModifyReturnSyscall()
if err != nil {
logger.GetLogger().WithError(err).Error("detect modify return syscall")
}
})
return modifyReturnSyscall.detected
}
func HasProgramLargeSize() bool {
return features.HaveLargeInstructions() == nil
}
func LogFeatures() string {
return fmt.Sprintf("override_return: %t, buildid: %t, kprobe_multi: %t, uprobe_multi %t, fmodret: %t, fmodret_syscall: %t, signal: %t, large: %t",
HasOverrideHelper(), HasBuildId(), HasKprobeMulti(), HasUprobeMulti(),
HasModifyReturn(), HasModifyReturnSyscall(), HasSignalHelper(), HasProgramLargeSize())
}