Skip to content

Commit

Permalink
Keep exit event process in DB
Browse files Browse the repository at this point in the history
With the add_session_metadata processor, don't remove processes from the process
db when the process has exited.

The processor can be run on an fork/exec events after the process has actually
exited, so the process must remain in the DB after it has exited, so the info
can be used in enrichment of these events.

Now the process is kept in the DB, and the exit code is appended, so the exit
code is also now properly enriched for exit events.
  • Loading branch information
mjwolf committed Apr 24, 2024
1 parent 4775eae commit 8f94732
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
3 changes: 3 additions & 0 deletions x-pack/auditbeat/processors/sessionmd/add_session_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func New(cfg *cfg.C) (beat.Processor, error) {
if err != nil {
return nil, fmt.Errorf("failed to create provider: %w", err)
}
logger.Info("backend=auto using procfs")
} else {
logger.Info("backend=auto using ebpf")
}
case "ebpf":
p, err = ebpf_provider.NewProvider(ctx, logger, db)
Expand Down
28 changes: 11 additions & 17 deletions x-pack/auditbeat/processors/sessionmd/processdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Process struct {
Cwd string
Env map[string]string
Filename string
ExitCode int32
}

var (
Expand Down Expand Up @@ -406,7 +407,14 @@ func (db *DB) InsertExit(exit types.ProcessExitEvent) {
defer db.mutex.Unlock()

pid := exit.PIDs.Tgid
delete(db.processes, pid)
process, ok := db.processes[pid]
if !ok {
db.logger.Errorf("could not insert exit, pid %v not found in db", pid)
}

process.ExitCode = exit.ExitCode
db.processes[pid] = process

delete(db.entryLeaders, pid)
delete(db.entryLeaderRelationships, pid)
}
Expand Down Expand Up @@ -437,6 +445,7 @@ func fullProcessFromDBProcess(p Process) types.Process {
ret.Thread.Capabilities.Effective, _ = capabilities.FromUint64(p.Creds.CapEffective)
ret.TTY.CharDevice.Major = p.CTTY.Major
ret.TTY.CharDevice.Minor = p.CTTY.Minor
ret.ExitCode = p.ExitCode

return ret
}
Expand Down Expand Up @@ -556,22 +565,7 @@ func (db *DB) GetProcess(pid uint32) (types.Process, error) {

process, ok := db.processes[pid]
if !ok {
procInfo, err := db.procfs.GetProcess(pid)
if err != nil {
return types.Process{}, errors.New("process not found in db (scraping from proc failed)")
}
process := Process{
PIDs: pidInfoFromProto(procInfo.PIDs),
Creds: credInfoFromProto(procInfo.Creds),
CTTY: ttyDevFromProto(procInfo.CTTY),
Argv: procInfo.Argv,
Cwd: procInfo.Cwd,
Env: procInfo.Env,
Filename: procInfo.Filename,
}
db.insertProcess(process)

process = db.processes[pid]
return types.Process{}, errors.New("process not found")
}

ret := fullProcessFromDBProcess(process)
Expand Down
2 changes: 1 addition & 1 deletion x-pack/auditbeat/processors/sessionmd/types/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Process struct {

// The exit code of the process, if this is a termination event.
// The field should be absent if there is no exit code for the event (e.g. process start).
ExitCode *int64 `json:"exit_code,omitempty"`
ExitCode int32 `json:"exit_code,omitempty"`

// Whether the process is connected to an interactive shell.
// Process interactivity is inferred from the processes file descriptors. If the character device for the controlling tty is the same as stdin and stderr for the process, the process is considered interactive.
Expand Down

0 comments on commit 8f94732

Please sign in to comment.