Skip to content

Commit

Permalink
Enrich events with binary and ancestors
Browse files Browse the repository at this point in the history
Enrich Tracee's events with information about current process executed
binary and ancestors information.
This is done using the process tree.
  • Loading branch information
AlonZivony committed May 4, 2023
1 parent fdfd3df commit 03ef9da
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ require (
kernel.org/pub/linux/libs/security/libcap/psx v1.2.64 // indirect
)

replace github.com/aquasecurity/tracee/types => ./types
replace github.com/kubernetes/cri-api => k8s.io/cri-api v0.23.5-rc.0
52 changes: 52 additions & 0 deletions pkg/ebpf/events_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (t *Tracee) handleEvents(ctx context.Context) {
eventsChan, errc = t.processTree.StartProcessingPipeline(ctx, eventsChan)
errcList = append(errcList, errc)

eventsChan, errc = t.enrichProcess(ctx, eventsChan)
errcList = append(errcList, errc)

// Enrichment stage
// In this stage container events are enriched with additional runtime data
// Events may be enriched in the initial decode state if the enrichment data has been stored in the Containers structure
Expand Down Expand Up @@ -558,6 +561,55 @@ func (t *Tracee) getStackAddresses(stackID uint32) ([]uint64, error) {
return stackAddresses[0:stackCounter], nil
}

func (t *Tracee) enrichProcess(ctx context.Context, in <-chan *trace.Event) (<-chan *trace.Event, <-chan error) {
out := make(chan *trace.Event)
errc := make(chan error, 1)

go func() {
defer close(out)
defer close(errc)

for {
select {
case event := <-in:
currentProcess, err := t.processTree.GetProcessInfo(event.HostProcessID, event.Timestamp)
if err != nil {
logger.Errorw("error enriching event with process info", "error", err)
} else {
event.ExecutedBinary = trace.BinaryInfo{
Inode: currentProcess.ExecutionBinary.Inode,
Device: currentProcess.ExecutionBinary.Device,
Ctime: int(currentProcess.ExecutionBinary.Ctime),
Path: currentProcess.ExecutionBinary.Path,
}

// Currently we only add parent information, as each new field we add increase the load significantly.
// We don't necessary have information on the parent process, so we do best effort.
parentProcess, err := t.processTree.GetProcessInfo(event.HostParentProcessID, event.Timestamp)
if err == nil {
parent := trace.Process{
ID: parentProcess.InHostIDs.Pid,
Name: parentProcess.ProcessName,
Binary: trace.BinaryInfo{
Inode: parentProcess.ExecutionBinary.Inode,
Device: parentProcess.ExecutionBinary.Device,
Ctime: int(parentProcess.ExecutionBinary.Ctime),
Path: parentProcess.ExecutionBinary.Path,
},
}
event.Ancestors = append(event.Ancestors, parent)
}
}
out <- event
case <-ctx.Done():
return
}
}
}()

return out, errc
}

// WaitForPipeline waits for results from all error channels.
func (t *Tracee) WaitForPipeline(errs ...<-chan error) error {
errc := MergeErrors(errs...)
Expand Down
6 changes: 3 additions & 3 deletions pkg/ebpf/tracee.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ func (t *Tracee) Stats() *metrics.Stats {
func GetEssentialEventsList() map[events.ID]eventConfig {
// Set essential events
return map[events.ID]eventConfig{
events.SchedProcessExec: {},
events.SchedProcessExit: {},
events.SchedProcessFork: {},
events.SchedProcessExec: {submit: 0xFFFFFFFFFFFFFFFF},
events.SchedProcessExit: {submit: 0xFFFFFFFFFFFFFFFF},
events.SchedProcessFork: {submit: 0xFFFFFFFFFFFFFFFF},
events.CgroupMkdir: {submit: 0xFFFFFFFFFFFFFFFF},
events.CgroupRmdir: {submit: 0xFFFFFFFFFFFFFFFF},
}
Expand Down
15 changes: 15 additions & 0 deletions types/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Event struct {
MountNS int `json:"mountNamespace"`
PIDNS int `json:"pidNamespace"`
ProcessName string `json:"processName"`
ExecutedBinary BinaryInfo `json:"executedBinary"`
HostName string `json:"hostName"`
Container Container `json:"container,omitempty"`
Kubernetes Kubernetes `json:"kubernetes,omitempty"`
Expand All @@ -40,6 +41,7 @@ type Event struct {
Syscall string `json:"syscall"`
StackAddresses []uint64 `json:"stackAddresses"`
ContextFlags ContextFlags `json:"contextFlags"`
Ancestors []Process `json:"ancestors"`
Args []Argument `json:"args"` // Arguments are ordered according their appearance in the original event
Metadata *Metadata `json:"metadata,omitempty"`
}
Expand Down Expand Up @@ -72,6 +74,19 @@ type ContextFlags struct {
IsCompat bool `json:"isCompat"`
}

type BinaryInfo struct {
Inode uint `json:"inode"`
Device uint `json:"device"`
Ctime int `json:"ctime"`
Path string `json:"path"`
}

type Process struct {
ID int `json:"id"`
Name string `json:"name"`
Binary BinaryInfo `json:"binary"`
}

// EventOrigin is where a trace.Event occured, it can either be from the host machine or from a container
type EventOrigin string

Expand Down

0 comments on commit 03ef9da

Please sign in to comment.