/
resolver_ebpfless.go
368 lines (296 loc) · 9.57 KB
/
resolver_ebpfless.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
// 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 process holds process related files
package process
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"sync"
"time"
"go.uber.org/atomic"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/DataDog/datadog-agent/pkg/process/procutil"
"github.com/DataDog/datadog-agent/pkg/security/metrics"
"github.com/DataDog/datadog-agent/pkg/security/probe/config"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
)
// CacheResolverKey is used to store and retrieve processes from the cache
type CacheResolverKey struct {
Pid uint32 // Pid of the related process (namespaced)
NSID uint64 // NSID represents the pids namespace ID of the related container
}
// EBPFLessResolver defines a resolver
type EBPFLessResolver struct {
sync.RWMutex
entryCache map[CacheResolverKey]*model.ProcessCacheEntry
opts ResolverOpts
scrubber *procutil.DataScrubber
statsdClient statsd.ClientInterface
// stats
cacheSize *atomic.Int64
processCacheEntryPool *Pool
}
// NewEBPFLessResolver returns a new process resolver
func NewEBPFLessResolver(_ *config.Config, statsdClient statsd.ClientInterface, scrubber *procutil.DataScrubber, opts *ResolverOpts) (*EBPFLessResolver, error) {
p := &EBPFLessResolver{
entryCache: make(map[CacheResolverKey]*model.ProcessCacheEntry),
opts: *opts,
scrubber: scrubber,
cacheSize: atomic.NewInt64(0),
statsdClient: statsdClient,
}
p.processCacheEntryPool = NewProcessCacheEntryPool(func() { p.cacheSize.Dec() })
return p, nil
}
func (p *EBPFLessResolver) deleteEntry(key CacheResolverKey, exitTime time.Time) {
entry, ok := p.entryCache[key]
if !ok {
return
}
entry.Exit(exitTime)
delete(p.entryCache, key)
entry.Release()
}
// DeleteEntry tries to delete an entry in the process cache
func (p *EBPFLessResolver) DeleteEntry(key CacheResolverKey, exitTime time.Time) {
p.Lock()
defer p.Unlock()
p.deleteEntry(key, exitTime)
}
// AddForkEntry adds an entry to the local cache and returns the newly created entry
func (p *EBPFLessResolver) AddForkEntry(key CacheResolverKey, ppid uint32, ts uint64) *model.ProcessCacheEntry {
if key.Pid == 0 {
return nil
}
entry := p.processCacheEntryPool.Get()
entry.PIDContext.Pid = key.Pid
entry.PPid = ppid
entry.ForkTime = time.Unix(0, int64(ts))
entry.IsThread = true
entry.Source = model.ProcessCacheEntryFromEvent
p.Lock()
defer p.Unlock()
p.insertForkEntry(key, entry)
return entry
}
// NewEntry returns a new entry
func (p *EBPFLessResolver) NewEntry(key CacheResolverKey, ppid uint32, file string, argv []string, argsTruncated bool,
envs []string, envsTruncated bool, ctrID string, ts uint64, tty string, source uint64) *model.ProcessCacheEntry {
entry := p.processCacheEntryPool.Get()
entry.PIDContext.Pid = key.Pid
entry.PPid = ppid
entry.Source = source
entry.Process.ArgsEntry = &model.ArgsEntry{
Values: argv,
Truncated: argsTruncated,
}
if len(argv) > 0 {
entry.Process.Argv0 = argv[0]
}
entry.Process.Comm = filepath.Base(file)
if len(entry.Process.Comm) > 16 {
// truncate comm to max 16 chars to be ebpf ISO
entry.Process.Comm = entry.Process.Comm[:16]
}
entry.Process.TTYName = tty
entry.Process.EnvsEntry = &model.EnvsEntry{
Values: envs,
Truncated: envsTruncated,
}
if strings.HasPrefix(file, "memfd:") {
entry.Process.FileEvent.PathnameStr = ""
entry.Process.FileEvent.BasenameStr = file
} else {
entry.Process.FileEvent.PathnameStr = file
entry.Process.FileEvent.BasenameStr = filepath.Base(entry.Process.FileEvent.PathnameStr)
}
entry.Process.ContainerID = ctrID
entry.ExecTime = time.Unix(0, int64(ts))
return entry
}
// AddExecEntry adds an entry to the local cache and returns the newly created entry
func (p *EBPFLessResolver) AddExecEntry(key CacheResolverKey, ppid uint32, file string, argv []string, argsTruncated bool,
envs []string, envsTruncated bool, ctrID string, ts uint64, tty string) *model.ProcessCacheEntry {
if key.Pid == 0 {
return nil
}
entry := p.NewEntry(key, ppid, file, argv, argsTruncated, envs, envsTruncated, ctrID, ts, tty, model.ProcessCacheEntryFromEvent)
p.Lock()
defer p.Unlock()
p.insertExecEntry(key, entry)
return entry
}
// AddProcFSEntry add a procfs entry
func (p *EBPFLessResolver) AddProcFSEntry(key CacheResolverKey, ppid uint32, file string, argv []string, argsTruncated bool,
envs []string, envsTruncated bool, ctrID string, ts uint64, tty string) *model.ProcessCacheEntry {
if key.Pid == 0 {
return nil
}
entry := p.NewEntry(key, ppid, file, argv, argsTruncated, envs, envsTruncated, ctrID, ts, tty, model.ProcessCacheEntryFromProcFS)
p.Lock()
defer p.Unlock()
parentKey := CacheResolverKey{NSID: key.NSID, Pid: ppid}
if parent := p.entryCache[parentKey]; parent != nil {
if parent.Equals(entry) {
entry.SetParentOfForkChild(parent)
} else {
entry.SetAncestor(parent)
}
}
p.insertEntry(key, entry, p.entryCache[key])
return entry
}
func (p *EBPFLessResolver) insertEntry(key CacheResolverKey, entry, prev *model.ProcessCacheEntry) {
p.entryCache[key] = entry
entry.Retain()
if prev != nil {
prev.Release()
}
p.cacheSize.Inc()
}
func (p *EBPFLessResolver) insertForkEntry(key CacheResolverKey, entry *model.ProcessCacheEntry) {
if key.Pid == 0 {
return
}
prev := p.entryCache[key]
if prev != nil {
// this shouldn't happen but it is better to exit the prev and let the new one replace it
prev.Exit(entry.ForkTime)
}
if entry.Pid != 1 {
parent := p.entryCache[CacheResolverKey{
Pid: entry.PPid,
NSID: key.NSID,
}]
if parent != nil {
parent.Fork(entry)
}
}
p.insertEntry(key, entry, prev)
}
func (p *EBPFLessResolver) insertExecEntry(key CacheResolverKey, entry *model.ProcessCacheEntry) {
if key.Pid == 0 {
return
}
prev := p.entryCache[key]
if prev != nil {
prev.Exec(entry)
// procfs entry should have the ppid already set
if entry.PPid == 0 {
entry.PPid = prev.PPid
}
// inheritate credentials as exec event, uid/gid can be update by setuid/setgid events
entry.Credentials = prev.Credentials
}
p.insertEntry(key, entry, prev)
}
// Resolve returns the cache entry for the given pid
func (p *EBPFLessResolver) Resolve(key CacheResolverKey) *model.ProcessCacheEntry {
if key.Pid == 0 {
return nil
}
p.Lock()
defer p.Unlock()
if e, ok := p.entryCache[key]; ok {
return e
}
return nil
}
// UpdateUID updates the credentials of the provided pid
func (p *EBPFLessResolver) UpdateUID(key CacheResolverKey, uid int32, euid int32) {
p.Lock()
defer p.Unlock()
entry := p.entryCache[key]
if entry != nil {
if uid != -1 {
entry.Credentials.UID = uint32(uid)
}
if euid != -1 {
entry.Credentials.EUID = uint32(euid)
}
}
}
// UpdateGID updates the credentials of the provided pid
func (p *EBPFLessResolver) UpdateGID(key CacheResolverKey, gid int32, egid int32) {
p.Lock()
defer p.Unlock()
entry := p.entryCache[key]
if entry != nil {
if gid != -1 {
entry.Credentials.GID = uint32(gid)
}
if egid != -1 {
entry.Credentials.EGID = uint32(egid)
}
}
}
// Walk iterates through the entire tree and call the provided callback on each entry
func (p *EBPFLessResolver) Walk(callback func(entry *model.ProcessCacheEntry)) {
p.RLock()
defer p.RUnlock()
for _, entry := range p.entryCache {
callback(entry)
}
}
// getCacheSize returns the cache size of the process resolver
func (p *EBPFLessResolver) getCacheSize() float64 {
p.RLock()
defer p.RUnlock()
return float64(len(p.entryCache))
}
// SendStats sends process resolver metrics
func (p *EBPFLessResolver) SendStats() error {
if err := p.statsdClient.Gauge(metrics.MetricProcessResolverCacheSize, p.getCacheSize(), []string{}, 1.0); err != nil {
return fmt.Errorf("failed to send process_resolver cache_size metric: %w", err)
}
return nil
}
// Start starts the resolver
func (p *EBPFLessResolver) Start(_ context.Context) error {
return nil
}
// Snapshot snapshot existing entryCache
func (p *EBPFLessResolver) Snapshot() {}
// Dump create a temp file and dump the cache
func (p *EBPFLessResolver) Dump(_ bool) (string, error) {
return "", errors.New("not supported")
}
// GetProcessArgvScrubbed returns the scrubbed args of the event as an array
func (p *EBPFLessResolver) GetProcessArgvScrubbed(pr *model.Process) ([]string, bool) {
if pr.ArgsEntry == nil || pr.ScrubbedArgvResolved {
return pr.Argv, pr.ArgsTruncated
}
if p.scrubber != nil && len(pr.ArgsEntry.Values) > 0 {
// replace with the scrubbed version
argv, _ := p.scrubber.ScrubCommand(pr.ArgsEntry.Values[1:])
pr.ArgsEntry.Values = []string{pr.ArgsEntry.Values[0]}
pr.ArgsEntry.Values = append(pr.ArgsEntry.Values, argv...)
}
pr.ScrubbedArgvResolved = true
return GetProcessArgv(pr)
}
// GetProcessEnvs returns the envs of the event
func (p *EBPFLessResolver) GetProcessEnvs(pr *model.Process) ([]string, bool) {
if pr.EnvsEntry == nil {
return pr.Envs, pr.EnvsTruncated
}
keys, truncated := pr.EnvsEntry.FilterEnvs(p.opts.envsWithValue)
pr.Envs = keys
pr.EnvsTruncated = pr.EnvsTruncated || truncated
return pr.Envs, pr.EnvsTruncated
}
// GetProcessEnvp returns the unscrubbed envs of the event with their values. Use with caution.
func (p *EBPFLessResolver) GetProcessEnvp(pr *model.Process) ([]string, bool) {
if pr.EnvsEntry == nil {
return pr.Envp, pr.EnvsTruncated
}
pr.Envp = pr.EnvsEntry.Values
pr.EnvsTruncated = pr.EnvsTruncated || pr.EnvsEntry.Truncated
return pr.Envp, pr.EnvsTruncated
}