forked from johnjohnsp1/capsule8
/
sensor.go
558 lines (464 loc) · 14.3 KB
/
sensor.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2017 Capsule8, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sensor
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"os"
"path/filepath"
"strings"
"time"
api "github.com/capsule8/capsule8/api/v0"
"github.com/capsule8/capsule8/pkg/config"
"github.com/capsule8/capsule8/pkg/container"
"github.com/capsule8/capsule8/pkg/stream"
"github.com/capsule8/capsule8/pkg/sys"
"github.com/capsule8/capsule8/pkg/sys/perf"
"github.com/golang/glog"
"golang.org/x/sys/unix"
)
// Number of random bytes to generate for Sensor Id
const sensorIDLengthBytes = 32
// Sensor represents the state of a sensor instance.
type Sensor struct {
// Unique Id for this sensor. Sensor Ids are ephemeral.
ID string
// Sensor-unique event sequence number. Each event sent from this
// sensor to any subscription has a unique sequence number for the
// indicated sensor Id.
sequenceNumber uint64
// Record the value of CLOCK_MONOTONIC_RAW when the sensor starts.
// All event monotimes are relative to this value.
bootMonotimeNanos int64
// Metrics counters for this sensor
Metrics MetricsCounters
// Repeater used for container event subscriptions
containerEventRepeater *containerEventRepeater
// If temporary fs mounts are made at startup, they're stored here.
perfEventMountPoint string
traceFSMountPoint string
// A sensor-global event monitor that is used for events to aid in
// caching process information
monitor *perf.EventMonitor
// Per-sensor process cache.
processCache ProcessInfoCache
// Mapping of event ids to data streams (subscriptions)
eventMap *safeSubscriptionMap
// Used by syscall events to handle syscall enter events with
// argument filters
dummySyscallEventID uint64
dummySyscallEventCount int64
}
// NewSensor creates a new Sensor instance.
func NewSensor() (*Sensor, error) {
randomBytes := make([]byte, sensorIDLengthBytes)
rand.Read(randomBytes)
sensorID := hex.EncodeToString(randomBytes[:])
ts := unix.Timespec{}
unix.ClockGettime(unix.CLOCK_MONOTONIC_RAW, &ts)
bootMonotimeNanos := ts.Nsec + (ts.Sec * int64(time.Second))
s := &Sensor{
ID: sensorID,
bootMonotimeNanos: bootMonotimeNanos,
eventMap: newSafeSubscriptionMap(),
}
cer, err := newContainerEventRepeater(s)
if err != nil {
return nil, err
}
s.containerEventRepeater = cer
return s, nil
}
// Start starts a sensor instance running.
func (s *Sensor) Start() error {
var err error
// We require that our run dir (usually /var/run/capsule8) exists.
// Ensure that now before proceeding any further.
err = os.MkdirAll(config.Global.RunDir, 0700)
if err != nil {
glog.Warningf("Couldn't mkdir %s: %s",
config.Global.RunDir, err)
return err
}
// If there is no mounted tracefs, the Sensor really can't do anything.
// Try mounting our own private mount of it.
if !config.Sensor.DontMountTracing && len(sys.TracingDir()) == 0 {
// If we couldn't find one, try mounting our own private one
glog.V(2).Info("Can't find mounted tracefs, mounting one")
err = s.mountTraceFS()
if err != nil {
glog.V(1).Info(err)
return err
}
}
// If there is no mounted cgroupfs for the perf_event cgroup, we can't
// efficiently separate processes in monitored containers from host
// processes. We can run without it, but it's better performance when
// available.
if !config.Sensor.DontMountPerfEvent && len(sys.PerfEventDir()) == 0 {
glog.V(2).Info("Can't find mounted perf_event cgroupfs, mounting one")
err = s.mountPerfEventCgroupFS()
if err != nil {
glog.V(1).Info(err)
// This is not a fatal error condition, proceed on
}
}
// Create the sensor-global event monitor. This EventMonitor instance
// will be used for all perf_event events
err = s.createEventMonitor()
if err != nil {
s.Stop()
return err
}
s.processCache = NewProcessInfoCache(s)
// Make sure that all events registered with the sensor's event monitor
// are active
s.monitor.EnableAll()
return nil
}
// Stop stops a running sensor instance.
func (s *Sensor) Stop() {
if s.monitor != nil {
glog.V(2).Info("Stopping sensor-global EventMonitor")
s.monitor.Close(true)
s.monitor = nil
glog.V(2).Info("Sensor-global EventMonitor stopped successfully")
}
if len(s.traceFSMountPoint) > 0 {
s.unmountTraceFS()
}
if len(s.perfEventMountPoint) > 0 {
s.unmountPerfEventCgroupFS()
}
}
func (s *Sensor) dispatchSample(eventID uint64, sample interface{}, err error) {
if err != nil {
glog.Warning(err)
}
if event, ok := sample.(*api.Event); ok && event != nil {
eventMap := s.eventMap.getMap()
if sub, ok := eventMap[eventID]; ok && sub != nil {
if sub.data != nil {
sub.data <- event
}
}
}
}
func (s *Sensor) mountTraceFS() error {
dir := filepath.Join(config.Global.RunDir, "tracing")
err := sys.MountTempFS("tracefs", dir, "tracefs", 0, "")
if err == nil {
s.traceFSMountPoint = dir
}
return err
}
func (s *Sensor) unmountTraceFS() {
err := sys.UnmountTempFS(s.traceFSMountPoint, "tracefs")
if err == nil {
s.traceFSMountPoint = ""
} else {
glog.V(2).Infof("Could not unmount %s: %s",
s.traceFSMountPoint, err)
}
}
func (s *Sensor) mountPerfEventCgroupFS() error {
dir := filepath.Join(config.Global.RunDir, "perf_event")
err := sys.MountTempFS("cgroup", dir, "cgroup", 0, "perf_event")
if err == nil {
s.perfEventMountPoint = dir
}
return err
}
func (s *Sensor) unmountPerfEventCgroupFS() {
err := sys.UnmountTempFS(s.perfEventMountPoint, "cgroup")
if err == nil {
s.perfEventMountPoint = ""
} else {
glog.V(2).Infof("Could not unmount %s: %s",
s.perfEventMountPoint, err)
}
}
func (s *Sensor) currentMonotimeNanos() int64 {
ts := unix.Timespec{}
unix.ClockGettime(unix.CLOCK_MONOTONIC_RAW, &ts)
d := ts.Nsec + (ts.Sec * int64(time.Second))
return d - s.bootMonotimeNanos
}
func (s *Sensor) nextSequenceNumber() uint64 {
// The first sequence number is intentionally 1 to disambiguate
// from no sequence number being included in the protobuf message.
s.sequenceNumber++
return s.sequenceNumber
}
// NewEvent creates a new API Event instance with common sensor-specific fields
// correctly populated.
func (s *Sensor) NewEvent() *api.Event {
monotime := s.currentMonotimeNanos()
sequenceNumber := s.nextSequenceNumber()
var b []byte
buf := bytes.NewBuffer(b)
binary.Write(buf, binary.LittleEndian, s.ID)
binary.Write(buf, binary.LittleEndian, sequenceNumber)
binary.Write(buf, binary.LittleEndian, monotime)
h := sha256.Sum256(buf.Bytes())
eventID := hex.EncodeToString(h[:])
s.Metrics.Events++
return &api.Event{
Id: eventID,
SensorId: s.ID,
SensorMonotimeNanos: monotime,
SensorSequenceNumber: sequenceNumber,
}
}
// NewEventFromContainer creates a new API Event instance using a specific
// container ID.
func (s *Sensor) NewEventFromContainer(containerID string) *api.Event {
e := s.NewEvent()
e.ContainerId = containerID
return e
}
// NewEventFromSample creates a new API Event instance using perf_event sample
// information.
func (s *Sensor) NewEventFromSample(sample *perf.SampleRecord,
data perf.TraceEventSampleData) *api.Event {
e := s.NewEvent()
e.SensorMonotimeNanos = int64(sample.Time) - s.bootMonotimeNanos
// When both the sensor and the process generating the sample are in
// containers, the sample.Pid and sample.Tid fields will be zero.
// Use "common_pid" from the trace event data instead.
e.ProcessPid = data["common_pid"].(int32)
e.Cpu = int32(sample.CPU)
processID, ok := s.processCache.ProcessID(int(e.ProcessPid))
if ok {
e.ProcessId = processID
}
// Add an associated containerId
containerID, ok := s.processCache.ProcessContainerID(int(e.ProcessPid))
if ok {
// Add the container Id if we have it and then try
// using it to look up additional container info
e.ContainerId = containerID
containerInfo := container.GetInfo(containerID)
if containerInfo != nil {
e.ContainerName = containerInfo.Name
e.ImageId = containerInfo.ImageID
e.ImageName = containerInfo.ImageName
}
}
return e
}
func (s *Sensor) buildMonitorGroups() ([]string, []int, error) {
var (
cgroupList []string
pidList []int
system bool
)
cgroups := make(map[string]bool)
for _, cgroup := range config.Sensor.CgroupName {
if len(cgroup) == 0 || cgroup == "/" {
system = true
continue
}
if cgroups[cgroup] {
continue
}
cgroups[cgroup] = true
cgroupList = append(cgroupList, cgroup)
}
// Try a system-wide perf event monitor if requested or as
// a fallback if no cgroups were requested
if system || len(sys.PerfEventDir()) == 0 || len(cgroupList) == 0 {
glog.V(1).Info("Creating new system-wide event monitor")
pidList = append(pidList, -1)
}
return cgroupList, pidList, nil
}
func (s *Sensor) createEventMonitor() error {
eventMonitorOptions := []perf.EventMonitorOption{}
cgroups, pids, err := s.buildMonitorGroups()
if err != nil {
return err
}
if len(cgroups) == 0 && len(pids) == 0 {
glog.Fatal("Can't create event monitor with no cgroups or pids")
}
if len(cgroups) > 0 {
var perfEventDir string
if len(s.perfEventMountPoint) > 0 {
perfEventDir = s.perfEventMountPoint
} else {
perfEventDir = sys.PerfEventDir()
}
if len(perfEventDir) > 0 {
glog.V(1).Infof("Creating new perf event monitor on cgroups %s",
strings.Join(cgroups, ","))
eventMonitorOptions = append(eventMonitorOptions,
perf.WithPerfEventDir(perfEventDir),
perf.WithCgroups(cgroups))
}
}
if len(pids) > 0 {
glog.V(1).Info("Creating new system-wide event monitor")
eventMonitorOptions = append(eventMonitorOptions,
perf.WithPids(pids))
}
s.monitor, err = perf.NewEventMonitor(eventMonitorOptions...)
if err != nil {
// If a cgroup-specific event monitor could not be created,
// fall back to a system-wide event monitor.
if len(cgroups) > 0 &&
(len(pids) == 0 || (len(pids) == 1 && pids[0] == -1)) {
glog.Warningf("Couldn't create perf event monitor on cgroups %s: %s",
strings.Join(cgroups, ","), err)
glog.V(1).Info("Creating new system-wide event monitor")
s.monitor, err = perf.NewEventMonitor()
}
if err != nil {
glog.V(1).Infof("Couldn't create event monitor: %s", err)
return err
}
}
go func() {
err := s.monitor.Run(s.dispatchSample)
if err != nil {
glog.Fatal(err)
}
glog.V(2).Info("EventMonitor.Run() returned; exiting goroutine")
}()
return nil
}
func (s *Sensor) createPerfEventStream(sub *api.Subscription) (*stream.Stream, error) {
eventMap := newSubscriptionMap()
registerFileEvents(s, eventMap, sub.EventFilter.FileEvents)
registerKernelEvents(s, eventMap, sub.EventFilter.KernelEvents)
registerNetworkEvents(s, eventMap, sub.EventFilter.NetworkEvents)
registerProcessEvents(s, eventMap, sub.EventFilter.ProcessEvents)
registerSyscallEvents(s, eventMap, sub.EventFilter.SyscallEvents)
if len(eventMap) == 0 {
return nil, nil
}
ctrl := make(chan interface{})
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
for eventID := range eventMap {
eventSub := eventMap[eventID]
eventSub.data = data
}
go func() {
defer close(data)
for {
_, ok := <-ctrl
if !ok {
glog.V(2).Info("Control channel closed")
// Remove from .eventMap first so that any
// pending events being processed get discarded
// as quickly as possible. Then remove the
// events from the EventMonitor
s.eventMap.remove(eventMap)
for eventID := range eventMap {
s.monitor.UnregisterEvent(eventID)
}
return
}
}
}()
s.eventMap.update(eventMap)
for eventID := range eventMap {
s.monitor.Enable(eventID)
}
return &stream.Stream{
Ctrl: ctrl,
Data: data,
}, nil
}
func (s *Sensor) applyModifiers(eventStream *stream.Stream, modifier api.Modifier) *stream.Stream {
if modifier.Throttle != nil {
eventStream = stream.Throttle(eventStream, *modifier.Throttle)
}
if modifier.Limit != nil {
eventStream = stream.Limit(eventStream, *modifier.Limit)
}
return eventStream
}
// NewSubscription creates a new telemetry subscription from the given
// api.Subscription descriptor. NewSubscription returns a stream.Stream of
// api.Events matching the specified filters. Closing the Stream cancels the
// subscription.
func (s *Sensor) NewSubscription(sub *api.Subscription) (*stream.Stream, error) {
glog.V(1).Infof("Subscribing to %+v", sub)
eventStream, joiner := stream.NewJoiner()
joiner.Off()
if len(sub.EventFilter.FileEvents) > 0 ||
len(sub.EventFilter.KernelEvents) > 0 ||
len(sub.EventFilter.NetworkEvents) > 0 ||
len(sub.EventFilter.ProcessEvents) > 0 ||
len(sub.EventFilter.SyscallEvents) > 0 {
pes, err := s.createPerfEventStream(sub)
if err != nil {
joiner.Close()
return nil, err
}
if pes != nil {
joiner.Add(pes)
}
}
if len(sub.EventFilter.ContainerEvents) > 0 {
ces, err := s.containerEventRepeater.newEventStream(sub)
if err != nil {
joiner.Close()
return nil, err
}
joiner.Add(ces)
}
for _, cf := range sub.EventFilter.ChargenEvents {
cs, err := newChargenSource(s, cf)
if err != nil {
joiner.Close()
return nil, err
}
joiner.Add(cs)
}
for _, tf := range sub.EventFilter.TickerEvents {
ts, err := newTickerSource(s, tf)
if err != nil {
joiner.Close()
return nil, err
}
joiner.Add(ts)
}
if sub.ContainerFilter != nil {
// Filter stream as requested by subscriber in the
// specified ContainerFilter to restrict the events to
// those matching the specified container ids, names,
// images, etc.
cef := newContainerFilter(sub.ContainerFilter)
eventStream = stream.Filter(eventStream, cef.FilterFunc)
eventStream = stream.Do(eventStream, cef.DoFunc)
}
if sub.Modifier != nil {
eventStream = s.applyModifiers(eventStream, *sub.Modifier)
}
s.Metrics.Subscriptions++
joiner.On()
return eventStream, nil
}
func filterNils(e interface{}) bool {
if e != nil {
ev := e.(*api.Event)
return ev != nil
}
return false
}