-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
resolver_linux.go
426 lines (378 loc) · 12.7 KB
/
resolver_linux.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
// 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.
// Package hash holds hash related files
package hash
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"io/fs"
"os"
"slices"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/glaslos/ssdeep"
lru "github.com/hashicorp/golang-lru/v2"
"go.uber.org/atomic"
"golang.org/x/time/rate"
"github.com/DataDog/datadog-agent/pkg/security/config"
"github.com/DataDog/datadog-agent/pkg/security/metrics"
"github.com/DataDog/datadog-agent/pkg/security/resolvers/cgroup"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/utils"
)
var (
// ErrSizeLimitReached indicates that the size limit was reached
ErrSizeLimitReached = fmt.Errorf("size limit reached")
)
// SizeLimitedWriter implements io.Writer and returns an error if more than the configured amount of data is read
type SizeLimitedWriter struct {
dst io.Writer
limit int
}
// newSizeLimitedWriter create a new SizeLimitedWriter that accepts at most 'limit' bytes.
func newSizeLimitedWriter(dst io.Writer, limit int) *SizeLimitedWriter {
return &SizeLimitedWriter{
dst: dst,
limit: limit,
}
}
// Write attempts to write to the writer
func (l *SizeLimitedWriter) Write(p []byte) (int, error) {
lp := len(p)
if lp > l.limit {
return 0, ErrSizeLimitReached
}
written, err := l.dst.Write(p)
l.limit -= written
return written, err
}
// ResolverOpts defines hash resolver options
type ResolverOpts struct {
// Enabled defines if the hash resolver should be enabled
Enabled bool
// MaxFileSize defines the maximum size of the files that the hash resolver is allowed to hash
MaxFileSize int64
// HashAlgorithms defines the hashes that hash resolver needs to compute
HashAlgorithms []model.HashAlgorithm
// EventTypes defines the list of event types for which we may compute hashes. Warning: enabling a FIM event will
// automatically make the hash resolver also hash process binary files.
EventTypes []model.EventType
}
// LRUCacheKey is the structure used to access cached hashes
type LRUCacheKey struct {
path string
containerID string
inode uint64
pathID uint32
}
// LRUCacheEntry is the structure used to cache hashes
type LRUCacheEntry struct {
state model.HashState
hashes []string
}
// Resolver represents a cache for mountpoints and the corresponding file systems
type Resolver struct {
opts ResolverOpts
statsdClient statsd.ClientInterface
limiter *rate.Limiter
cgroupResolver *cgroup.Resolver
replace map[string]string
cache *lru.Cache[LRUCacheKey, *LRUCacheEntry]
// stats
hashCount map[model.EventType]map[model.HashAlgorithm]*atomic.Uint64
hashMiss map[model.EventType]map[model.HashState]*atomic.Uint64
hashCacheHit map[model.EventType]*atomic.Uint64
}
// NewResolver returns a new instance of the hash resolver
func NewResolver(c *config.RuntimeSecurityConfig, statsdClient statsd.ClientInterface, cgroupResolver *cgroup.Resolver) (*Resolver, error) {
if !c.HashResolverEnabled {
return &Resolver{
opts: ResolverOpts{
Enabled: false,
},
}, nil
}
var cache *lru.Cache[LRUCacheKey, *LRUCacheEntry]
if c.HashResolverCacheSize > 0 {
var err error
cache, err = lru.New[LRUCacheKey, *LRUCacheEntry](c.HashResolverCacheSize)
if err != nil {
return nil, fmt.Errorf("couldn't create hash resolver cache: %w", err)
}
}
r := &Resolver{
opts: ResolverOpts{
Enabled: true,
MaxFileSize: c.HashResolverMaxFileSize,
HashAlgorithms: c.HashResolverHashAlgorithms,
EventTypes: c.HashResolverEventTypes,
},
cgroupResolver: cgroupResolver,
statsdClient: statsdClient,
limiter: rate.NewLimiter(rate.Limit(c.HashResolverMaxHashRate), c.HashResolverMaxHashBurst),
cache: cache,
hashCount: make(map[model.EventType]map[model.HashAlgorithm]*atomic.Uint64),
hashMiss: make(map[model.EventType]map[model.HashState]*atomic.Uint64),
hashCacheHit: make(map[model.EventType]*atomic.Uint64),
replace: c.HashResolverReplace,
}
// generate counters
for i := model.EventType(0); i < model.MaxKernelEventType; i++ {
r.hashCount[i] = make(map[model.HashAlgorithm]*atomic.Uint64, model.MaxHashAlgorithm)
for j := model.HashAlgorithm(0); j < model.MaxHashAlgorithm; j++ {
r.hashCount[i][j] = atomic.NewUint64(0)
}
r.hashMiss[i] = make(map[model.HashState]*atomic.Uint64, model.MaxHashState)
for j := model.HashState(0); j < model.MaxHashState; j++ {
r.hashMiss[i][j] = atomic.NewUint64(0)
}
r.hashCacheHit[i] = atomic.NewUint64(0)
}
return r, nil
}
// ComputeHashesFromEvent calls ComputeHashes using the provided event
func (resolver *Resolver) ComputeHashesFromEvent(event *model.Event, file *model.FileEvent) []string {
// resolve FileEvent
event.FieldHandlers.ResolveFilePath(event, file)
return resolver.ComputeHashes(event.GetEventType(), &event.ProcessContext.Process, file)
}
// ComputeHashes computes the hashes of the provided file event.
// Disclaimer: This resolver considers that the FileEvent has already been resolved
func (resolver *Resolver) ComputeHashes(eventType model.EventType, process *model.Process, file *model.FileEvent) []string {
if !resolver.opts.Enabled {
return nil
}
// check state
if file.HashState == model.Done {
return file.Hashes
}
if file.HashState != model.NoHash && file.HashState != model.HashWasRateLimited {
// this file was already processed and an error occurred, nothing else to do
return nil
}
// check if the resolver is allowed to hash this event type
if !slices.Contains(resolver.opts.EventTypes, eventType) {
file.HashState = model.EventTypeNotConfigured
resolver.hashMiss[eventType][model.EventTypeNotConfigured].Inc()
return nil
}
resolver.hash(eventType, process, file)
return file.Hashes
}
// getHashFunction returns the hash function for the provided algorithm
func (resolver *Resolver) getHashFunction(algorithm model.HashAlgorithm) hash.Hash {
switch algorithm {
case model.SHA1:
return sha1.New()
case model.SHA256:
return sha256.New()
case model.MD5:
return md5.New()
case model.SSDEEP:
return ssdeep.New()
default:
return nil
}
}
func getFileInfo(path string) (fs.FileMode, int64, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return 0, 0, err
}
return fileInfo.Mode(), fileInfo.Size(), nil
}
// hash hashes the provided file event
func (resolver *Resolver) hash(eventType model.EventType, process *model.Process, file *model.FileEvent) {
if !file.IsPathnameStrResolved || len(file.PathnameStr) == 0 {
resolver.hashMiss[eventType][model.PathnameResolutionError].Inc()
file.HashState = model.PathnameResolutionError
return
}
if hashStr, exists := resolver.replace[file.PathnameStr]; exists {
file.Hashes = append(file.Hashes, hashStr)
file.HashState = model.Done
return
}
// check if the hash(es) of this file is in cache
fileKey := LRUCacheKey{
path: file.PathnameStr,
containerID: process.ContainerID,
inode: file.Inode,
pathID: file.PathKey.PathID,
}
if resolver.cache != nil {
cacheEntry, ok := resolver.cache.Get(fileKey)
if ok {
file.HashState = cacheEntry.state
file.Hashes = cacheEntry.hashes
resolver.hashCacheHit[eventType].Inc()
return
}
}
rootPIDs := []uint32{process.Pid}
if resolver.cgroupResolver != nil {
w, ok := resolver.cgroupResolver.GetWorkload(process.ContainerID)
if ok {
rootPIDs = w.GetPIDs()
}
}
// open the target file
var (
lastErr error
f *os.File
mode fs.FileMode
size int64
)
for _, pidCandidate := range rootPIDs {
path := utils.ProcRootFilePath(pidCandidate, file.PathnameStr)
if mode, size, lastErr = getFileInfo(path); !mode.IsRegular() {
continue
}
f, lastErr = os.Open(path)
if lastErr == nil {
break
}
}
if lastErr != nil {
if os.IsNotExist(lastErr) {
file.HashState = model.FileNotFound
resolver.hashMiss[eventType][model.FileNotFound].Inc()
return
}
// We can't open this file, most likely because it isn't a regular file. Example seen in production:
// - open(/host/proc/2129077/root/tmp/plugin3037415914) => no such device or address
// - open(/host/proc/576833/root/run/containerd/runc/k8s.io/2b100...96104/runc.WUXTJB) => permission denied
// - open(/host/proc/313599/root/proc/10987/task/10988/status/10987/task) => not a directory
// - open(/host/proc/263082/root/usr/local/bin/runc) => no such process
resolver.hashMiss[eventType][model.FileOpenError].Inc()
file.HashState = model.FileOpenError
return
}
if f == nil {
file.HashState = model.FileNotFound
resolver.hashMiss[eventType][model.FileNotFound].Inc()
return
}
defer f.Close()
// is the file size above the configured limit
if size > resolver.opts.MaxFileSize {
resolver.hashMiss[eventType][model.FileTooBig].Inc()
file.HashState = model.FileTooBig
return
}
// is the file empty ?
if size == 0 {
resolver.hashMiss[eventType][model.FileEmpty].Inc()
file.HashState = model.FileEmpty
return
}
// check the rate limiter
if !resolver.limiter.Allow() {
// better luck next time
resolver.hashMiss[eventType][model.HashWasRateLimited].Inc()
file.HashState = model.HashWasRateLimited
return
}
var hashers []io.Writer
for _, algorithm := range resolver.opts.HashAlgorithms {
h := resolver.getHashFunction(algorithm)
if h == nil {
// shouldn't happen, ignore
continue
}
hashers = append(hashers, h)
}
multiWriter := newSizeLimitedWriter(io.MultiWriter(hashers...), int(resolver.opts.MaxFileSize))
if _, err := io.Copy(multiWriter, f); err != nil {
if errors.Is(err, ErrSizeLimitReached) {
resolver.hashMiss[eventType][model.FileTooBig].Inc()
file.HashState = model.FileTooBig
return
}
// We can't read this file, most likely because it isn't a regular file (despite the check above). Example seen
// in production:
// - read(/host/proc/2076/root/proc/1/fdinfo/64) => no such file or directory
// - read(/host/proc/2328/root/run/netns/a574a27c) => invalid argument
resolver.hashMiss[eventType][model.FileOpenError].Inc()
file.HashState = model.FileOpenError
return
}
for i, algorithm := range resolver.opts.HashAlgorithms {
var hashStr string
if hashStr = algorithm.String(); len(hashStr) > 0 {
hashStr += ":"
}
if algorithm == model.SSDEEP {
digest := hashers[i].(hash.Hash).Sum(nil)
if len(digest) == 0 {
// we failed to compute the digest
resolver.hashMiss[eventType][model.HashFailed].Inc()
continue
}
hashStr += string(digest)
} else {
hashStr += hex.EncodeToString(hashers[i].(hash.Hash).Sum(nil))
}
file.Hashes = append(file.Hashes, hashStr)
resolver.hashCount[eventType][algorithm].Inc()
}
file.HashState = model.Done
// cache entry
if resolver.cache != nil {
cacheEntry := &LRUCacheEntry{
state: model.Done,
hashes: make([]string, len(file.Hashes)),
}
copy(cacheEntry.hashes, file.Hashes)
resolver.cache.Add(fileKey, cacheEntry)
}
}
// SendStats sends the resolver metrics
func (resolver *Resolver) SendStats() error {
if !resolver.opts.Enabled {
return nil
}
for evtType, hashCounts := range resolver.hashCount {
for algorithm, count := range hashCounts {
tags := []string{fmt.Sprintf("event_type:%s", evtType), fmt.Sprintf("hash:%s", algorithm)}
if value := count.Swap(0); value > 0 {
if err := resolver.statsdClient.Count(metrics.MetricHashResolverHashCount, int64(value), tags, 1.0); err != nil {
return fmt.Errorf("couldn't send MetricHashResolverHashCount metric: %w", err)
}
}
}
}
for evtType, hashMisses := range resolver.hashMiss {
for reason, count := range hashMisses {
tags := []string{fmt.Sprintf("event_type:%s", evtType), fmt.Sprintf("reason:%s", reason)}
if value := count.Swap(0); value > 0 {
if err := resolver.statsdClient.Count(metrics.MetricHashResolverHashMiss, int64(value), tags, 1.0); err != nil {
return fmt.Errorf("couldn't send MetricHashResolverHashMiss metric: %w", err)
}
}
}
}
for evtType, count := range resolver.hashCacheHit {
tags := []string{fmt.Sprintf("event_type:%s", evtType)}
if value := count.Swap(0); value > 0 {
if err := resolver.statsdClient.Count(metrics.MetricHashResolverHashCacheHit, int64(value), tags, 1.0); err != nil {
return fmt.Errorf("couldn't send MetricHashResolverHashCacheHit metric: %w", err)
}
}
}
if resolver.cache != nil {
if value := resolver.cache.Len(); value > 0 {
if err := resolver.statsdClient.Gauge(metrics.MetricHashResolverHashCacheLen, float64(value), []string{}, 1.0); err != nil {
return fmt.Errorf("couldn't send MetricHashResolverHashCacheLen metric: %w", err)
}
}
}
return nil
}