Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CWS] Refactor process resolver test #32306

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 12 additions & 35 deletions pkg/security/probe/probe_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,33 +882,23 @@ func (p *EBPFProbe) handleEvent(CPU int, data []byte) {
return
}

if process.IsKThread(event.ProcessCacheEntry.PPid, event.ProcessCacheEntry.Pid) {
if err := p.Resolvers.ProcessResolver.AddForkEntry(event, newEntryCb); err != nil {
seclog.Errorf("failed to insert fork event: %s (pid %d, offset %d, len %d)", err, event.PIDContext.Pid, offset, len(data))
return
}

p.Resolvers.ProcessResolver.ApplyBootTime(event.ProcessCacheEntry)
event.ProcessCacheEntry.SetSpan(event.SpanContext.SpanID, event.SpanContext.TraceID)

p.Resolvers.ProcessResolver.AddForkEntry(event.ProcessCacheEntry, event.PIDContext.ExecInode, newEntryCb)
case model.ExecEventType:
// unmarshal and fill event.processCacheEntry
if _, err = p.unmarshalProcessCacheEntry(event, data[offset:]); err != nil {
seclog.Errorf("failed to decode exec event: %s (offset %d, len %d)", err, offset, len(data))
return
}

if err = p.Resolvers.ProcessResolver.ResolveNewProcessCacheEntry(event.ProcessCacheEntry, event.ContainerContext); err != nil {
seclog.Debugf("failed to resolve new process cache entry context for pid %d: %s", event.PIDContext.Pid, err)

var errResolution *path.ErrPathResolution
if errors.As(err, &errResolution) {
event.SetPathResolutionError(&event.ProcessCacheEntry.FileEvent, err)
}
} else {
p.Resolvers.ProcessResolver.AddExecEntry(event.ProcessCacheEntry, event.PIDContext.ExecInode)
err = p.Resolvers.ProcessResolver.AddExecEntry(event)
if err != nil {
seclog.Errorf("failed to insert exec event: %s (pid %d, offset %d, len %d)", err, event.PIDContext.Pid, offset, len(data))
return
}

event.Exec.Process = &event.ProcessCacheEntry.Process
}

if !p.setProcessContext(eventType, event, newEntryCb) {
Expand Down Expand Up @@ -1019,26 +1009,13 @@ func (p *EBPFProbe) handleEvent(CPU int, data []byte) {
seclog.Errorf("failed to decode exit event: %s (offset %d, len %d)", err, offset, len(data))
return
}

var exists bool
event.ProcessCacheEntry, exists = p.fieldHandlers.GetProcessCacheEntry(event, newEntryCb)
if !exists {
// no need to dispatch an exit event that don't have the corresponding cache entry
return
exists := p.Resolvers.ProcessResolver.ApplyExitEntry(event, newEntryCb)
if exists {
p.Resolvers.MountResolver.DelPid(event.Exit.Pid)
// update action reports
p.processKiller.HandleProcessExited(event)
p.fileHasher.HandleProcessExited(event)
}

// Use the event timestamp as exit time
// The local process cache hasn't been updated yet with the exit time when the exit event is first seen
// The pid_cache kernel map has the exit_time but it's only accessed if there's a local miss
event.ProcessCacheEntry.Process.ExitTime = p.fieldHandlers.ResolveEventTime(event, &event.BaseEvent)
event.Exit.Process = &event.ProcessCacheEntry.Process

// update mount pid mapping
p.Resolvers.MountResolver.DelPid(event.Exit.Pid)

// update action reports
p.processKiller.HandleProcessExited(event)
p.fileHasher.HandleProcessExited(event)
case model.SetuidEventType:
// the process context may be incorrect, do not modify it
if event.Error != nil {
Expand Down
61 changes: 46 additions & 15 deletions pkg/security/resolvers/process/resolver_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,61 @@ func (p *EBPFResolver) UpdateArgsEnvs(event *model.ArgsEnvsEvent) {
}

// AddForkEntry adds an entry to the local cache and returns the newly created entry
func (p *EBPFResolver) AddForkEntry(entry *model.ProcessCacheEntry, inode uint64, newEntryCb func(*model.ProcessCacheEntry, error)) {
if entry.Pid == 0 {
return
func (p *EBPFResolver) AddForkEntry(event *model.Event, newEntryCb func(*model.ProcessCacheEntry, error)) error {
p.ApplyBootTime(event.ProcessCacheEntry)
event.ProcessCacheEntry.SetSpan(event.SpanContext.SpanID, event.SpanContext.TraceID)

if event.ProcessCacheEntry.Pid == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this at the very beginning of the function ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should return an error as we do for the AddExecEntry

return errors.New("no pid")
}
if IsKThread(event.ProcessCacheEntry.PPid, event.ProcessCacheEntry.Pid) {
return errors.New("process is kthread")
}

p.Lock()
defer p.Unlock()

p.insertForkEntry(entry, inode, model.ProcessCacheEntryFromEvent, newEntryCb)
p.insertForkEntry(event.ProcessCacheEntry, event.PIDContext.ExecInode, model.ProcessCacheEntryFromEvent, newEntryCb)
return nil
}

// AddExecEntry adds an entry to the local cache and returns the newly created entry
func (p *EBPFResolver) AddExecEntry(entry *model.ProcessCacheEntry, inode uint64) {
if entry.Pid == 0 {
return
}

func (p *EBPFResolver) AddExecEntry(event *model.Event) error {
p.Lock()
defer p.Unlock()

p.insertExecEntry(entry, inode, model.ProcessCacheEntryFromEvent)
var err error
if err := p.ResolveNewProcessCacheEntry(event.ProcessCacheEntry, event.ContainerContext); err != nil {
var errResolution *spath.ErrPathResolution
if errors.As(err, &errResolution) {
event.SetPathResolutionError(&event.ProcessCacheEntry.FileEvent, err)
}
} else {
if event.ProcessCacheEntry.Pid == 0 {
return errors.New("no pid context")
}
p.insertExecEntry(event.ProcessCacheEntry, event.PIDContext.ExecInode, model.ProcessCacheEntryFromEvent)
}

event.Exec.Process = &event.ProcessCacheEntry.Process

return err
}

// ApplyExitEntry delete entry from the local cache if present
func (p *EBPFResolver) ApplyExitEntry(event *model.Event, newEntryCb func(*model.ProcessCacheEntry, error)) bool {
event.ProcessCacheEntry = p.resolve(event.PIDContext.Pid, event.PIDContext.Tid, event.PIDContext.ExecInode, false, newEntryCb)
if event.ProcessCacheEntry == nil {
// no need to dispatch an exit event that don't have the corresponding cache entry
return false
}

// Use the event timestamp as exit time
// The local process cache hasn't been updated yet with the exit time when the exit event is first seen
// The pid_cache kernel map has the exit_time but it's only accessed if there's a local miss
event.ProcessCacheEntry.ExitTime = event.FieldHandlers.ResolveEventTime(event, &event.BaseEvent)
event.Exit.Process = &event.ProcessCacheEntry.Process
return true

}

// enrichEventFromProc uses /proc to enrich a ProcessCacheEntry with additional metadata
Expand Down Expand Up @@ -520,6 +554,7 @@ func (p *EBPFResolver) insertEntry(entry, prev *model.ProcessCacheEntry, source
}

func (p *EBPFResolver) insertForkEntry(entry *model.ProcessCacheEntry, inode uint64, source uint64, newEntryCb func(*model.ProcessCacheEntry, error)) {

if entry.Pid == 0 {
return
}
Expand All @@ -529,7 +564,6 @@ func (p *EBPFResolver) insertForkEntry(entry *model.ProcessCacheEntry, inode uin
// 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[entry.PPid]
if entry.PPid >= 1 && inode != 0 && (parent == nil || parent.FileEvent.Inode != inode) {
Expand Down Expand Up @@ -568,7 +602,6 @@ func (p *EBPFResolver) insertExecEntry(entry *model.ProcessCacheEntry, inode uin
prev.ApplyExecTimeOf(entry)
return
}

prev.Exec(entry)
} else {
entry.IsParentMissing = true
Expand Down Expand Up @@ -654,7 +687,6 @@ func (p *EBPFResolver) resolveFileFieldsPath(e *model.FileFields, pce *model.Pro
err error
maxDepthRetry = 3
)

for maxDepthRetry > 0 {
pathnameStr, mountPath, source, origin, err = p.pathResolver.ResolveFileFieldsPath(e, &pce.PIDContext, ctrCtx)
if err == nil {
Expand Down Expand Up @@ -686,7 +718,6 @@ func (p *EBPFResolver) SetProcessPath(fileEvent *model.FileEvent, pce *model.Pro
if fileEvent.Inode == 0 {
return onError("", &model.ErrInvalidKeyPath{Inode: fileEvent.Inode, MountID: fileEvent.MountID})
}

pathnameStr, mountPath, source, origin, err := p.resolveFileFieldsPath(&fileEvent.FileFields, pce, ctrCtx)
if err != nil {
return onError(pathnameStr, err)
Expand Down
Loading
Loading