-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
all.go
276 lines (245 loc) · 8.29 KB
/
all.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
// Package probes holds probes related files
package probes
import (
"math"
"os"
manager "github.com/DataDog/ebpf-manager"
"github.com/cilium/ebpf"
"golang.org/x/sys/unix"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/utils"
)
const (
minPathnamesEntries = 64000 // ~27 MB
maxPathnamesEntries = 96000
minProcEntries = 16394
maxProcEntries = 131072
)
var (
// EventsPerfRingBufferSize is the buffer size of the perf buffers used for events.
// PLEASE NOTE: for the perf ring buffer usage metrics to be accurate, the provided value must have the
// following form: (1 + 2^n) * pages. Checkout https://github.com/DataDog/ebpf for more.
EventsPerfRingBufferSize = 256 * os.Getpagesize()
)
// computeDefaultEventsRingBufferSize is the default buffer size of the ring buffers for events.
// Must be a power of 2 and a multiple of the page size
func computeDefaultEventsRingBufferSize() uint32 {
numCPU, err := utils.NumCPU()
if err != nil {
numCPU = 1
}
if numCPU <= 16 {
return uint32(8 * 256 * os.Getpagesize())
}
return uint32(16 * 256 * os.Getpagesize())
}
// AllProbes returns the list of all the probes of the runtime security module
func AllProbes(fentry bool) []*manager.Probe {
var allProbes []*manager.Probe
allProbes = append(allProbes, getAttrProbes(fentry)...)
allProbes = append(allProbes, getExecProbes(fentry)...)
allProbes = append(allProbes, getLinkProbe(fentry)...)
allProbes = append(allProbes, getMkdirProbes(fentry)...)
allProbes = append(allProbes, getMountProbes(fentry)...)
allProbes = append(allProbes, getOpenProbes(fentry)...)
allProbes = append(allProbes, getRenameProbes(fentry)...)
allProbes = append(allProbes, getRmdirProbe(fentry)...)
allProbes = append(allProbes, getSharedProbes()...)
allProbes = append(allProbes, getIouringProbes()...)
allProbes = append(allProbes, getUnlinkProbes(fentry)...)
allProbes = append(allProbes, getXattrProbes(fentry)...)
allProbes = append(allProbes, getIoctlProbes()...)
allProbes = append(allProbes, getSELinuxProbes()...)
allProbes = append(allProbes, getBPFProbes(fentry)...)
allProbes = append(allProbes, getPTraceProbes(fentry)...)
allProbes = append(allProbes, getMMapProbes()...)
allProbes = append(allProbes, getMProtectProbes(fentry)...)
allProbes = append(allProbes, getModuleProbes(fentry)...)
allProbes = append(allProbes, getSignalProbes(fentry)...)
allProbes = append(allProbes, getSpliceProbes(fentry)...)
allProbes = append(allProbes, getFlowProbes()...)
allProbes = append(allProbes, getNetDeviceProbes()...)
allProbes = append(allProbes, GetTCProbes()...)
allProbes = append(allProbes, getBindProbes(fentry)...)
allProbes = append(allProbes, getSyscallMonitorProbes()...)
allProbes = append(allProbes, getChdirProbes(fentry)...)
allProbes = append(allProbes,
&manager.Probe{
ProbeIdentificationPair: manager.ProbeIdentificationPair{
UID: SecurityAgentUID,
EBPFFuncName: "sys_exit",
},
},
// Snapshot probe
&manager.Probe{
ProbeIdentificationPair: manager.ProbeIdentificationPair{
UID: SecurityAgentUID,
EBPFFuncName: "hook_security_inode_getattr",
},
},
)
return allProbes
}
// AllMaps returns the list of maps of the runtime security module
func AllMaps() []*manager.Map {
return []*manager.Map{
// Syscall table map
getSyscallTableMap(),
// Filters
{Name: "filter_policy"},
{Name: "inode_discarders"},
{Name: "pid_discarders"},
{Name: "inode_disc_revisions"},
{Name: "basename_approvers"},
// Dentry resolver table
{Name: "pathnames"},
// Snapshot table
{Name: "exec_file_cache"},
// Open tables
{Name: "open_flags_approvers"},
// Exec tables
{Name: "proc_cache"},
{Name: "pid_cache"},
{Name: "str_array_buffers"},
// SELinux tables
{Name: "selinux_write_buffer"},
{Name: "selinux_enforce_status"},
// Enabled event mask
{Name: "enabled_events"},
// Syscall stats monitor (inflight syscall)
{Name: "syscalls_stats_enabled"},
{Name: "kill_list"},
}
}
func getMaxEntries(numCPU int, min int, max int) uint32 {
maxEntries := int(math.Min(float64(max), float64(min*numCPU)/4))
if maxEntries < min {
maxEntries = min
}
return uint32(maxEntries)
}
// MapSpecEditorOpts defines some options of the map spec editor
type MapSpecEditorOpts struct {
TracedCgroupSize int
UseMmapableMaps bool
UseRingBuffers bool
RingBufferSize uint32
PathResolutionEnabled bool
SecurityProfileMaxCount int
}
// AllMapSpecEditors returns the list of map editors
func AllMapSpecEditors(numCPU int, opts MapSpecEditorOpts) map[string]manager.MapSpecEditor {
editors := map[string]manager.MapSpecEditor{
"syscalls": {
MaxEntries: 8192,
EditorFlag: manager.EditMaxEntries,
},
"proc_cache": {
MaxEntries: getMaxEntries(numCPU, minProcEntries, maxProcEntries),
EditorFlag: manager.EditMaxEntries,
},
"pid_cache": {
MaxEntries: getMaxEntries(numCPU, minProcEntries, maxProcEntries),
EditorFlag: manager.EditMaxEntries,
},
"activity_dumps_config": {
MaxEntries: model.MaxTracedCgroupsCount,
EditorFlag: manager.EditMaxEntries,
},
"activity_dump_rate_limiters": {
MaxEntries: model.MaxTracedCgroupsCount,
EditorFlag: manager.EditMaxEntries,
},
"cgroup_wait_list": {
MaxEntries: model.MaxTracedCgroupsCount,
EditorFlag: manager.EditMaxEntries,
},
"security_profiles": {
MaxEntries: uint32(opts.SecurityProfileMaxCount),
EditorFlag: manager.EditMaxEntries,
},
"secprofs_syscalls": {
MaxEntries: uint32(opts.SecurityProfileMaxCount),
EditorFlag: manager.EditMaxEntries,
},
}
if opts.PathResolutionEnabled {
editors["pathnames"] = manager.MapSpecEditor{
MaxEntries: getMaxEntries(numCPU, minPathnamesEntries, maxPathnamesEntries),
EditorFlag: manager.EditMaxEntries,
}
}
if opts.TracedCgroupSize > 0 {
editors["traced_cgroups"] = manager.MapSpecEditor{
MaxEntries: uint32(opts.TracedCgroupSize),
EditorFlag: manager.EditMaxEntries,
}
}
if opts.UseMmapableMaps {
editors["dr_erpc_buffer"] = manager.MapSpecEditor{
Flags: unix.BPF_F_MMAPABLE,
EditorFlag: manager.EditFlags,
}
}
if opts.UseRingBuffers {
if opts.RingBufferSize == 0 {
opts.RingBufferSize = computeDefaultEventsRingBufferSize()
}
editors["events"] = manager.MapSpecEditor{
MaxEntries: opts.RingBufferSize,
Type: ebpf.RingBuf,
EditorFlag: manager.EditMaxEntries | manager.EditType | manager.EditKeyValue,
}
}
return editors
}
// AllPerfMaps returns the list of perf maps of the runtime security module
func AllPerfMaps() []*manager.PerfMap {
return []*manager.PerfMap{
{
Map: manager.Map{Name: "events"},
},
}
}
// AllRingBuffers returns the list of ring buffers of the runtime security module
func AllRingBuffers() []*manager.RingBuffer {
return []*manager.RingBuffer{
{
Map: manager.Map{Name: "events"},
},
}
}
// AllTailRoutes returns the list of all the tail call routes
func AllTailRoutes(ERPCDentryResolutionEnabled, networkEnabled, supportMmapableMaps bool) []manager.TailCallRoute {
var routes []manager.TailCallRoute
routes = append(routes, getExecTailCallRoutes()...)
routes = append(routes, getDentryResolverTailCallRoutes(ERPCDentryResolutionEnabled, supportMmapableMaps)...)
routes = append(routes, getSysExitTailCallRoutes()...)
if networkEnabled {
routes = append(routes, getTCTailCallRoutes()...)
}
return routes
}
// AllBPFProbeWriteUserProgramFunctions returns the list of program functions that use the bpf_probe_write_user helper
func AllBPFProbeWriteUserProgramFunctions() []string {
return []string{
"tail_call_target_dentry_resolver_erpc_write_user",
}
}
// GetPerfBufferStatisticsMaps returns the list of maps used to monitor the performances of each perf buffers
func GetPerfBufferStatisticsMaps() map[string]string {
return map[string]string{
"events": "events_stats",
}
}
// GetRingBufferStatisticsMaps returns the list of maps used to monitor the performances of each ring buffer
func GetRingBufferStatisticsMaps() map[string]string {
return map[string]string{
"events": "events_ringbuf_stats",
}
}