Skip to content

Commit 5b91c25

Browse files
events_derived: merge into existing files
Move the derivation pipeline stage to events_pipeline Move the derivation table initialization to tracee.go
1 parent f1ebce6 commit 5b91c25

File tree

3 files changed

+107
-117
lines changed

3 files changed

+107
-117
lines changed

pkg/ebpf/events_derived.go

Lines changed: 0 additions & 111 deletions
This file was deleted.

pkg/ebpf/events_pipeline.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package ebpf
22

33
import (
44
"bytes"
5-
gocontext "context"
5+
"context"
66
"encoding/binary"
77
"fmt"
88
"strconv"
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/aquasecurity/tracee/pkg/bufferdecoder"
1313
"github.com/aquasecurity/tracee/pkg/events"
14+
"github.com/aquasecurity/tracee/pkg/events/derive"
1415
"github.com/aquasecurity/tracee/types/trace"
1516
)
1617

@@ -19,7 +20,7 @@ import (
1920
const maxStackDepth int = 20
2021

2122
// handleEvents is a high-level function that starts all operations related to events processing
22-
func (t *Tracee) handleEvents(ctx gocontext.Context) {
23+
func (t *Tracee) handleEvents(ctx context.Context) {
2324
var errcList []<-chan error
2425

2526
// Source pipeline stage.
@@ -87,7 +88,7 @@ func (t *Tracee) handleEvents(ctx gocontext.Context) {
8788
// 3) create an internal, to tracee-ebpf, buffer based on the node size.
8889

8990
// queueEvents implements an internal FIFO queue for caching events
90-
func (t *Tracee) queueEvents(ctx gocontext.Context, in <-chan *trace.Event) (chan *trace.Event, chan error) {
91+
func (t *Tracee) queueEvents(ctx context.Context, in <-chan *trace.Event) (chan *trace.Event, chan error) {
9192
out := make(chan *trace.Event, 10000)
9293
errc := make(chan error, 1)
9394
done := make(chan struct{}, 1)
@@ -129,7 +130,7 @@ func (t *Tracee) queueEvents(ctx gocontext.Context, in <-chan *trace.Event) (cha
129130
}
130131

131132
// decodeEvents read the events received from the BPF programs and parse it into trace.Event type
132-
func (t *Tracee) decodeEvents(outerCtx gocontext.Context) (<-chan *trace.Event, <-chan error) {
133+
func (t *Tracee) decodeEvents(outerCtx context.Context) (<-chan *trace.Event, <-chan error) {
133134
out := make(chan *trace.Event, 10000)
134135
errc := make(chan error, 1)
135136
go func() {
@@ -234,7 +235,7 @@ func parseContextFlags(flags uint32) trace.ContextFlags {
234235
}
235236
}
236237

237-
func (t *Tracee) processEvents(ctx gocontext.Context, in <-chan *trace.Event) (<-chan *trace.Event, <-chan error) {
238+
func (t *Tracee) processEvents(ctx context.Context, in <-chan *trace.Event) (<-chan *trace.Event, <-chan error) {
238239
out := make(chan *trace.Event, 10000)
239240
errc := make(chan error, 1)
240241
go func() {
@@ -270,7 +271,41 @@ func (t *Tracee) processEvents(ctx gocontext.Context, in <-chan *trace.Event) (<
270271
return out, errc
271272
}
272273

273-
func (t *Tracee) sinkEvents(ctx gocontext.Context, in <-chan *trace.Event) <-chan error {
274+
// deriveEvents is the derivation pipeline stage
275+
func (t *Tracee) deriveEvents(ctx context.Context, in <-chan *trace.Event) (<-chan *trace.Event, <-chan error) {
276+
out := make(chan *trace.Event)
277+
errc := make(chan error, 1)
278+
279+
go func() {
280+
defer close(out)
281+
defer close(errc)
282+
283+
for {
284+
select {
285+
case event := <-in:
286+
out <- event
287+
288+
// Derive event before parse its arguments
289+
derivatives, errors := derive.DeriveEvent(*event, t.eventDerivations)
290+
291+
for _, err := range errors {
292+
t.handleError(err)
293+
}
294+
295+
for _, derivative := range derivatives {
296+
out <- &derivative
297+
}
298+
299+
case <-ctx.Done():
300+
return
301+
}
302+
}
303+
}()
304+
305+
return out, errc
306+
}
307+
308+
func (t *Tracee) sinkEvents(ctx context.Context, in <-chan *trace.Event) <-chan error {
274309
errc := make(chan error, 1)
275310

276311
go func() {

pkg/ebpf/tracee.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/aquasecurity/tracee/pkg/metrics"
3434
"github.com/aquasecurity/tracee/pkg/procinfo"
3535
"github.com/aquasecurity/tracee/pkg/utils"
36+
"github.com/aquasecurity/tracee/pkg/utils/sharedobjs"
3637
"github.com/aquasecurity/tracee/types/trace"
3738
lru "github.com/hashicorp/golang-lru"
3839
"golang.org/x/sys/unix"
@@ -511,6 +512,71 @@ func (t *Tracee) initTailCall(mapName string, mapIndexes []uint32, progName stri
511512
return nil
512513
}
513514

515+
// initDerivationTable initializes tracee's events.DerivationTable.
516+
// we declare for each Event (represented through it's ID) to which other
517+
// events it can be derived and the corresponding function to derive into that Event.
518+
func (t *Tracee) initDerivationTable() error {
519+
// sanity check for containers dependency
520+
if t.containers == nil {
521+
return fmt.Errorf("nil tracee containers")
522+
}
523+
524+
pathResolver := containers.InitPathResolver(&t.pidsInMntns)
525+
soLoader := sharedobjs.InitContainersSymbolsLoader(&pathResolver, 1024)
526+
527+
t.eventDerivations = derive.Table{
528+
events.CgroupMkdir: {
529+
events.ContainerCreate: {
530+
Enabled: t.events[events.ContainerCreate].submit,
531+
DeriveFunction: derive.ContainerCreate(t.containers),
532+
},
533+
},
534+
events.CgroupRmdir: {
535+
events.ContainerRemove: {
536+
Enabled: t.events[events.ContainerRemove].submit,
537+
DeriveFunction: derive.ContainerRemove(t.containers),
538+
},
539+
},
540+
events.PrintSyscallTable: {
541+
events.HookedSyscalls: {
542+
Enabled: t.events[events.PrintSyscallTable].submit,
543+
DeriveFunction: derive.DetectHookedSyscall(t.kernelSymbols),
544+
},
545+
},
546+
events.DnsRequest: {
547+
events.NetPacket: {
548+
Enabled: t.events[events.NetPacket].submit,
549+
DeriveFunction: derive.NetPacket(),
550+
},
551+
},
552+
events.DnsResponse: {
553+
events.NetPacket: {
554+
Enabled: t.events[events.NetPacket].submit,
555+
DeriveFunction: derive.NetPacket(),
556+
},
557+
},
558+
events.PrintNetSeqOps: {
559+
events.HookedSeqOps: {
560+
Enabled: t.events[events.HookedSeqOps].submit,
561+
DeriveFunction: derive.HookedSeqOps(t.kernelSymbols),
562+
},
563+
},
564+
events.SharedObjectLoaded: {
565+
events.SymbolsLoaded: {
566+
Enabled: t.events[events.SymbolsLoaded].submit,
567+
DeriveFunction: derive.SymbolsLoaded(
568+
soLoader,
569+
t.config.Filter.ArgFilter.Filters[events.SymbolsLoaded]["symbols"].Equal,
570+
t.config.Filter.ArgFilter.Filters[events.SymbolsLoaded]["library_path"].NotEqual,
571+
t.config.Debug,
572+
),
573+
},
574+
},
575+
}
576+
577+
return nil
578+
}
579+
514580
// options config should match defined values in ebpf code
515581
const (
516582
optDetectOrigSyscall uint32 = 1 << iota

0 commit comments

Comments
 (0)